📄 treeview.java
字号:
for (Enumeration e = ((DefaultMutableTreeNode) model.getRoot()).breadthFirstEnumeration(); e.hasMoreElements();) { TreeNode node = (DefaultMutableTreeNode) e.nextElement(); ((TVObject) node).deselect(); } } public Vector<TVObject> getCurrentSelectedItems() { Vector<TVObject> v = new Vector<TVObject>(); if (model == null) return v; for (Enumeration e = ((DefaultMutableTreeNode) model.getRoot()).breadthFirstEnumeration(); e.hasMoreElements();) { TVObject node = (TVObject) e.nextElement(); if (((TVObject) node).isSelected()) v.addElement(node); } return v; } public void addNodeToTree(final TVObject treeNode) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ // Normal case model.insertNodeInto(treeNode, (DefaultMutableTreeNode) model.getRoot(), ((DefaultMutableTreeNode) model.getRoot()).getChildCount()); scrollPathToVisible(new TreePath(treeNode.getPath())); treeNode.setView(TreeView.this); if (treeNode instanceof TVGroup) { // We have a group so go through all our UI objects and see if their // proxies are in the group. Vector<IVirtualObject> v = ((TVGroup) treeNode).getElements(); for (IVirtualObject vo : v) { for (Enumeration e = ((DefaultMutableTreeNode) model.getRoot()).breadthFirstEnumeration(); e.hasMoreElements();) { TVVirtualEntity tvNode = (TVVirtualEntity) e.nextElement(); if (tvNode.getVirtualObject().equals(vo)) { // VO is in our view, so move it under the group node model.removeNodeFromParent(tvNode); model.insertNodeInto(tvNode, treeNode, treeNode.getChildCount()); scrollPathToVisible(new TreePath(tvNode.getPath())); } } } // Show all elements in our new group for (Enumeration e = treeNode.breadthFirstEnumeration(); e.hasMoreElements();) scrollPathToVisible(new TreePath(((DefaultMutableTreeNode) e.nextElement()).getPath())); } noteState(treeNode); }}); } public void addVirtualObject(IVirtualObject obj) { obj.msg("registering with treeview ... "); Class cls = objectMap.findMapping(obj.getClass()); if (cls == null) { obj.warning("couldnt find proper mapping"); } else { try { // create the instance msg("Creating instance of " + cls.getName()); IUIObject uiObj = (IUIObject) cls.newInstance(); // register forward and back pointers between the two. // NOTE: may want to change this mechanism so that its up to the // objects themselves if they want uni/bi directional link or none at all* uiObj.setVirtualObject(obj); ((TVObject) uiObj).registerStateChangeListener(this); obj.addUIObject(uiObj); // add it to this viewer addNodeToTree((TVObject) uiObj); } catch (InstantiationException ex) { obj.error("messed up instantiation during register process"); ex.printStackTrace(); } catch (IllegalAccessException ex) { obj.error("illegal access during registration in gridview"); ex.printStackTrace(); } catch (ClassCastException ex) { ex.printStackTrace(); } } } // Autoscroll methods. Makes the tree view autoscroll, why I have to code this // I don't know. --Robert private static int margin = 12; public Insets getAutoscrollInsets() { Rectangle outer = getBounds(); Rectangle inner = getParent().getBounds(); return new Insets( inner.y - outer.y + margin, inner.x - outer.x + margin, outer.height - inner.height - inner.y + outer.y + margin, outer.width - inner.width - inner.x + outer.x + margin); } public void autoscroll(Point p) { int realrow = getRowForLocation(p.x, p.y); Rectangle outer = getBounds(); realrow = (p.y + outer.y <= margin ? realrow < 1 ? 0 : realrow - 1 : realrow < getRowCount() - 1 ? realrow + 1 : realrow); scrollRowToVisible(realrow); } // TVObjects call this to tell TreeView they have changed public void noteState(Object obj) { if (obj instanceof TVApplication) { // Find out if the Application has a spot to run on TVObject parent = (TVObject) ((TVApplication) obj).getHostUIObject(); TreePath objPath = new TreePath(model.getPathToRoot(((TVApplication) obj))); if (objPath.getPathCount() > 1) { if ((parent != null) && (objPath.getParentPath().getLastPathComponent() != parent)) { model.removeNodeFromParent((TVApplication) obj); model.insertNodeInto((TVApplication) obj, parent, parent.getChildCount()); scrollPathToVisible(new TreePath(((TVApplication) obj).getPath())); } else model.reload((TVApplication) obj); } else { Vector<TVObject> expandedItems = new Vector<TVObject>(); for (Enumeration e = ((DefaultMutableTreeNode) model.getRoot()).breadthFirstEnumeration(); e.hasMoreElements();) { TVObject node = (TVObject) e.nextElement(); if (isVisible(new TreePath(node.getPath())) && !node.equals(obj)) expandedItems.addElement(node); } model.reload(); for (TVObject element : expandedItems) expandPath(new TreePath(element.getPath())); } } if (obj instanceof TVGroup) { // We have a group so go through all our UI objects and see if their // proxies are in the group. Vector<IVirtualObject> v = ((TVGroup) obj).getElements(); for (IVirtualObject vo : v) { for (Enumeration e = ((DefaultMutableTreeNode) model.getRoot()).breadthFirstEnumeration(); e.hasMoreElements();) { TVVirtualEntity tvNode = (TVVirtualEntity) e.nextElement(); // If vo is already in the tree and hasn't previously moved if (tvNode.getVirtualObject().equals(vo) && !(tvNode.getParent() instanceof TVGroup)) { // VO is in our view, so move it under the group node model.removeNodeFromParent(tvNode); model.insertNodeInto(tvNode, ((TVObject) obj), ((TVObject) obj).getChildCount()); scrollPathToVisible(new TreePath(tvNode.getPath())); } } } } // Needs optimising // Currently clears entire tree selection, gets what VOs are selected // and then selects them in tree view. clearSelection(); Vector<TVObject> selectedItems = getCurrentSelectedItems(); for (TVObject tvObj : selectedItems) { scrollPathToVisible(new TreePath(tvObj.getPath())); addSelectionPath(new TreePath(tvObj.getPath())); } } public void addVirtualObjectMapping(Class vObj, Class uiObj) { // Decide if we want to add a mapping of this object if (uiObj.getPackage().toString().equals(TVObject.class.getPackage().toString())) { msg("Mapping " + vObj + " to " + uiObj); objectMap.addMap(vObj, uiObj); } } public SpotWorld getWorld() { return world; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -