📄 explorertree.java
字号:
if (obj instanceof DefaultMutableTreeNode) obj = ((DefaultMutableTreeNode)obj).getUserObject(); int numChildren = treeModel.getChildCount(node); if (numChildren == 0) return false; if (openLib && (obj instanceof Library)) { Library lib = (Library)obj; // Only expands library and its node. Doesn't contineu with rest of nodes in Explorer if (lib == library) { expandPath(path); // Counting position from library to cell selected if (cell != null) { count = countChildrenAndExpandInPath(cell, treeModel, path, node, count); } setSelectionRow(count); return true; // found location in explorer } } else if (obj instanceof String) { String msg = (String)obj; if ((msg.equalsIgnoreCase("libraries") && openLib) || (msg.equalsIgnoreCase("signals") && !openLib)) expandPath(path); } // now recurse for(int i=0; i<numChildren; i++) { Object child = treeModel.getChild(node, i); if (!(child instanceof DefaultMutableTreeNode)) continue; TreePath descentPath = path.pathByAddingChild(child); if (descentPath == null) continue; count++; if (openLibraryInExplorerTree(library, cell, descentPath, openLib, count)) return true; } return false; } /** * Method to redo the explorer tree, keeping expansion. * @param contentNodes nodes that changed. */ void redoContentTrees(List<MutableTreeNode> contentNodes) { assert SwingUtilities.isEventDispatchThread(); // remember the state of the tree KeepTreeExpansion kte = new KeepTreeExpansion(this, rootNode, treeModel, new TreePath(rootNode)); model().updateContentExplorerNodes(contentNodes); kte.restore(); } /** * Class to remember the expansion state of a JTree and restore it after a change. */ public static class KeepTreeExpansion { private List<TreePath> expanded = new ArrayList<TreePath>(); private JTree theTree; private TreeModel theTreeModel; private Object theRootNode; KeepTreeExpansion(JTree tt, Object rn, TreeModel tm, TreePath tp) { theTree = tt; theTreeModel = tm; theRootNode = rn; recursivelyCacheExpandedPaths(tp); } /** * Recursively save the expansion state. */ private void recursivelyCacheExpandedPaths(TreePath path) { if (!theTree.isExpanded(path)) return; expanded.add(path); // now recurse Object node = path.getLastPathComponent(); for(int i=0; i< theTreeModel.getChildCount(node); i++) { Object child = theTreeModel.getChild(node, i); if (theTreeModel.isLeaf(child)) continue; TreePath descentPath = path.pathByAddingChild(child); if (descentPath == null) continue; recursivelyCacheExpandedPaths(descentPath); } } public void restore() { for (TreePath path: expanded) expandCachedPath(path); } private void expandCachedPath(TreePath oldPath) { Object[] path = oldPath.getPath(); TreePath newPath = new TreePath(theRootNode); Object topNode = theRootNode; for (int i = 1; i < path.length; i++) { Object oldChild = path[i]; Object newChild = null; if (oldChild instanceof DefaultMutableTreeNode) newChild = findChildByUserObject(topNode, ((DefaultMutableTreeNode)oldChild).getUserObject()); if (newChild == null) { int k = theTreeModel.getIndexOfChild(topNode, oldChild); if (k >= 0) newChild = theTreeModel.getChild(topNode, k); } if (newChild == null) return; topNode = newChild; newPath = newPath.pathByAddingChild(topNode); theTree.expandPath(newPath); } } private Object findChildByUserObject(Object parent, Object userObject) { for (int i = 0, childCount = theTreeModel.getChildCount(parent); i < childCount; i++) { Object newChild = theTreeModel.getChild(parent, i); if (!(newChild instanceof DefaultMutableTreeNode)) continue; if (((DefaultMutableTreeNode)newChild).getUserObject().equals(userObject)) return newChild; } return null; } } public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { if (!(value instanceof DefaultMutableTreeNode)) return value.toString(); Object nodeInfo = ((DefaultMutableTreeNode)value).getUserObject(); if (nodeInfo instanceof Cell) { Cell cell = (Cell)nodeInfo; if (cell.isLinked() && cell.isSchematic()) { Cell.CellGroup group = cell.getCellGroup(); Cell mainSchematic = group.getMainSchematics(); int numSchematics = 0; for(Iterator<Cell> gIt = group.getCells(); gIt.hasNext(); ) { Cell cellInGroup = gIt.next(); if (cellInGroup.isSchematic()) numSchematics++; } if (numSchematics > 1 && cell == mainSchematic) return cell.noLibDescribe() + " **"; } return cell.noLibDescribe(); } if (nodeInfo instanceof Library) { Library lib = (Library)nodeInfo; String nodeName = lib.getName(); if (lib == Library.getCurrent() && Library.getNumLibraries() > 1) { nodeName += " [Current]"; if (iconLibraryChecked == null) iconLibraryChecked = Resources.getResource(getClass(), "IconLibraryCheck.gif"); iconLibrary = iconLibraryChecked; } else { if (iconLibraryNormal == null) iconLibraryNormal = Resources.getResource(getClass(), "IconLibrary.gif"); iconLibrary = iconLibraryNormal; } return nodeName; } if (nodeInfo instanceof Cell.CellGroup) { Cell.CellGroup group = (Cell.CellGroup)nodeInfo; return group.getName(); } if (nodeInfo instanceof ErrorLoggerTree.ErrorLoggerTreeNode) { ErrorLoggerTree.ErrorLoggerTreeNode node = (ErrorLoggerTree.ErrorLoggerTreeNode)nodeInfo; ErrorLogger el = node.getLogger(); String s = el.getSystem(); if (ErrorLoggerTree.currentLogger != null && node == ErrorLoggerTree.currentLogger.getUserObject()) s += " [Current]"; return s; } if (nodeInfo instanceof ErrorLogger.MessageLog) { ErrorLogger.MessageLog el = (ErrorLogger.MessageLog)nodeInfo; return el.getMessage(); } if (nodeInfo instanceof Signal) { Signal sig = (Signal)nodeInfo; return sig.getSignalName(); } if (nodeInfo == null) return ""; return nodeInfo.toString(); } // *********************************** DRAG AND DROP *********************************** /** Variables needed for DnD */ private ExplorerTreeDropTarget dropTarget; private Point dragOffset; private BufferedImage dragImage; private boolean subCells; private void initDND() { setTransferHandler(new MyTransferHandler(getTransferHandler(), this)); setDragEnabled(true); /* * There are two ways to do dragging from the Explorer tree: * (1) the old way (pre Java 1.5) required a "drag gesture recognizer" * It has the advantage that it can recognize right-click-drag * (which was used to request dragging of the cell and all subcells) * It has the disadvantage that it cannot drag multiple objects * (2) the new way requires a "Transfer Handler". * To use the old dragging method (1): * comment-out the previous two lines * uncomment the next two lines * comment-out the implementation of the MyTransferHandler class * uncomment the "dragGestureRecognized" method below * have this class implement "DragGestureListener" */// dragSource = DragSource.getDefaultDragSource();// DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_LINK, this); // a drop target for the explorer tree dropTarget = new ExplorerTreeDropTarget(); new DropTarget(this, DnDConstants.ACTION_LINK, dropTarget, true); } /** * This override method fixes a bug where clicking and then dragging fails * if the tree node is not already selected. * This should not be necessary anymore after Java 1.5v6 but somehow is. */ public void setUI(TreeUI ui) { super.setUI(ui); // the default dnd implementation needs to first select and then drag try { // this is only valid on Java 1.5...in 1.6 or later, the code is not needed Class<?> clazz = Class.forName("javax.swing.plaf.basic.BasicDragGestureRecognizer"); MouseListener[] mouseListeners = getMouseListeners(); MouseListener dragListener = null; for(int i = 0; i<mouseListeners.length; i++) { if (clazz.isAssignableFrom(mouseListeners[i].getClass())) { dragListener = mouseListeners[i]; break; } } if (dragListener != null) { removeMouseListener(dragListener); removeMouseMotionListener((MouseMotionListener)dragListener); addMouseListener(dragListener); addMouseMotionListener((MouseMotionListener)dragListener); } } catch (ClassNotFoundException e) { } } private class MyTransferHandler extends TransferHandler { private TransferHandler real; private ExplorerTree tree; MyTransferHandler(TransferHandler real, ExplorerTree tree) { this.real = real; this.tree = tree; } protected Transferable createTransferable(JComponent c) { if (numCurrentlySelectedObjects() == 0) return null; // handle signal dragging when in a WaveformWindow setting if (getCurrentlySelectedObject(0) instanceof Signal) { // Get the Transferable Object StringBuffer buf = new StringBuffer(); for(int i=0; i<numCurrentlySelectedObjects(); i++) { Signal sSig = (Signal)getCurrentlySelectedObject(i); String sigName = sSig.getFullName(); if (sSig instanceof AnalogSignal) { AnalogSignal as = (AnalogSignal)sSig; if (as.getAnalysis().getAnalysisType() == Analysis.ANALYSIS_TRANS) sigName = "TRANS " + sigName; else if (as.getAnalysis().getAnalysisType() == Analysis.ANALYSIS_AC) sigName = "AC " + sigName; else if (as.getAnalysis().getAnalysisType() == Analysis.ANALYSIS_DC) sigName = "DC " + sigName; else if (as.getAnalysis().getAnalysisType() == Analysis.ANALYSIS_MEAS) sigName = "MEASUREMENT " + sigName; } buf.append(sigName); buf.append("\n"); } Transferable transferable = new StringSelection(buf.toString()); return transferable; } // cells and groups must be dragged one-at-a-time if (numCurrentlySelectedObjects() > 1) { Job.getUserInterface().showErrorMessage("Can drag only one Cell or Group", "Too Much Selected"); return null; } // make a Transferable object EditWindow.NodeProtoTransferable transferable = new EditWindow.NodeProtoTransferable(getCurrentlySelectedObject(0), tree); if (!transferable.isValid()) return null; // find out the offset of the cursor to the selected tree node TreePath path = currentSelectedPaths[0]; Rectangle pathRect = getPathBounds(path); if (pathRect == null) return null; dragOffset = new Point(0, 0); // render the dragged stuff Component comp = getCellRenderer().getTreeCellRendererComponent(tree, path.getLastPathComponent(), false, isExpanded(path), getModel().isLeaf(path.getLastPathComponent()), 0, false); int wid = (int)pathRect.getWidth(); int hei = (int)pathRect.getHeight(); // there is no way to tell whether the context menu button was used subCells = false;// if (e.getTriggerEvent() instanceof MouseEvent)// subCells = ClickZoomWireListener.isRightMouse((MouseEvent)e.getTriggerEvent()); if (subCells) hei *= 2; comp.setSize(wid, hei); dragImage = new BufferedImage(wid, hei, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2 = dragImage.createGraphics(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f)); AffineTransform saveAT = g2.getTransform(); if (subCells) g2.translate(0, -hei/4); comp.paint(g2); g2.setTransform(saveAT); if (subCells) { g2.setColor(Color.BLACK); g2.drawLine(wid/2, hei/2, 0, hei); g2.drawLine(wid/2, hei/2, wid/3, hei); g2.drawLine(wid/2, hei/2, wid/3*2, hei); g2.drawLine(wid/2, hei/2, wid, hei); } g2.dispose(); return transferable; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -