📄 treeview2.java
字号:
if (g1.getFont() == null) g1.setFont(f); } fm = g1.getFontMetrics(); g1.setColor(getBackground()); g1.fillRect(0, 0, im1.getWidth(this), d.height);// clear image //do drawing for each visible node int lastOne=sbVPosition+viewHeight/cellSize+1; if (lastOne > viewCount) { lastOne = viewCount; } TreeNode2 outerNode = null; if (!v.isEmpty()) outerNode = (TreeNode2)v.elementAt(sbVPosition); for (int i=sbVPosition; i<lastOne; i++) { TreeNode2 node=(TreeNode2)v.elementAt(i); int x = cellSize*(node.depth + 1); int y = (i-sbVPosition)*cellSize; // draw lines g1.setColor(getForeground()); // draw vertical sibling line if (node.sibling != null) { int k = v.indexOf(node.sibling) - i; if (k > lastOne) { k = lastOne; } drawDotLine(x - cellSize/2, y + cellSize/2, x - cellSize/2, y + cellSize/2 + k*cellSize); } // if sibling is above page, draw up to top of page for this level for (int m=0; m<i; m++) { TreeNode2 sib = (TreeNode2)v.elementAt(m); if ((sib.sibling == node) && (m<sbVPosition)) { drawDotLine (x - cellSize/2, 0, x - cellSize/2, y + cellSize/2); } } // draw vertical child lines if (node.isExpanded()) { drawDotLine(x + cellSize/2, y + cellSize -2 , x + cellSize/2, y + cellSize + cellSize/2); } // draw node horizontal line g1.setColor(getForeground()); drawDotLine(x - cellSize/2, y + cellSize/2, x + cellSize/2, y + cellSize/2); // draw toggle box if (node.isExpandable()) { g1.setColor(getBackground()); g1.fillRect(cellSize*(node.depth) + cellSize/4, y + clickSize/2, clickSize, clickSize ); g1.setColor(getForeground()); g1.drawRect(cellSize*(node.depth) + cellSize/4, y + clickSize/2, clickSize, clickSize ); // cross hair g1.drawLine(cellSize*(node.depth) + cellSize/4 +2, y + cellSize/2, cellSize*(node.depth) + cellSize/4 + clickSize -2, y + cellSize/2); if (!(node.isExpanded())) { g1.drawLine(cellSize*(node.depth) + cellSize/2, y + clickSize/2 +2, cellSize*(node.depth) + cellSize/2, y + clickSize/2 + clickSize -2); } } // draw node image Image nodeImage = node.getImage(); if (nodeImage != null) { g1.drawImage(nodeImage, x+imageInset, y, this); } // draw node text if (node.text != null) { drawNodeText(node, y, /*node==selectedNode*/ true); } if(outerNode.depth>node.depth) outerNode = node; } // draw outer vertical lines if (outerNode!=null) { while((outerNode = outerNode.parent) != null) { if (outerNode.sibling != null) drawDotLine (cellSize*(outerNode.depth + 1) - cellSize/2, 0, cellSize*(outerNode.depth + 1) - cellSize/2, d.height);; } } } void drawNodeText(TreeNode2 node, int yPosition, boolean eraseBackground) { Color fg, bg; int depth=node.depth; Image nodeImage = node.getImage(); int textOffset = ((depth + 1) * (cellSize)) + cellSize + textInset - (nodeImage==null ? 12:0); /* if (node == selectedNode && hasFocus) { //??RKM?? Temp until these return some good values //if (symantec.itools.lang.OS.isMacintosh()) if (node.color != null) fg = node.color; else fg = Color.white; //else //fg = SystemColor.textHighlightText; //if (symantec.itools.lang.OS.isMacintosh()) bg = Color.black; //else //bg = SystemColor.textHighlight; } */ if (node.color != null) { fg = isLight (node.color) ? Color.black : Color.white; bg = node.color; } else { fg = getForeground(); bg = getBackground(); } if (eraseBackground) { g1.setColor(bg); g1.fillRect(textOffset-1, yPosition+1, fm.stringWidth(node.text)+4, cellSize-1); } if (node == selectedNode) { g1.setColor(getForeground()); g1.drawRect(textOffset-1, yPosition+1, fm.stringWidth(node.text)+3, cellSize-2); repaint(Math.max(0,textOffset-1-sbHPosition), yPosition+1, fm.stringWidth(node.text)+4, cellSize-1); } g1.setColor(fg); g1.drawString(node.text, textOffset, yPosition + cellSize - textBaseLine); } private boolean isLight (Color c) { int r = c.getRed(); int g = c.getGreen(); int b = c.getBlue (); int min = (r < g) ? r : g; if (b < min) min = b; return min > 128; } private void drawDotLine(int x0, int y0, int x1, int y1) { if (y0==y1) { for (int i = x0; i<x1; i+=2) { g1.drawLine(i,y0, i, y1); } } else { for (int i = y0; i<y1; i+=2) { g1.drawLine(x0, i, x1, i); } } } private void parseTreeStructure(String tempStructure[]) throws InvalidTreeNode2Exception { for(int i = 0; i < tempStructure.length; i++) { String entry = tempStructure[i]; int indentLevel = findLastPreSpace(entry)/*+1*/; if (indentLevel == -1) throw new InvalidTreeNode2Exception(); TreeNode2 node = new TreeNode2(entry.trim()); node.setDepth(indentLevel); if (rootNode == null) { if (indentLevel != 0) throw new InvalidTreeNode2Exception(); append(node); } else { TreeNode2 currentNode = rootNode; while(currentNode.sibling != null) currentNode = currentNode.sibling; for(int j = 1; j < indentLevel; j++) { int numberOfChildren = currentNode.numberOfChildren; TreeNode2 tempNode = null; if (numberOfChildren > 0) { tempNode = currentNode.child; while(tempNode.sibling != null) tempNode = tempNode.sibling; } if (tempNode != null) currentNode = tempNode; else break; } int diff = indentLevel - currentNode.getDepth(); if (diff > 1) throw new InvalidTreeNode2Exception(); if (diff == 1) insert(node, currentNode, CHILD); else insert(node, currentNode, NEXT); } } } private int findLastPreSpace(String s) { int length; length = s.length(); if(s.charAt(0) != ' ' && s.charAt(0) != '\t') { return 0; } for(int i = 1; i < length; i++) { if(s.charAt(i) != ' ' && s.charAt(i) != '\t') { return i; } } return -1; } /** * Returns the recommended dimensions to properly display this component. * This is a standard Java AWT method which gets called to determine * the recommended size of this component. * * @see #minimumSize */ public synchronized Dimension preferredSize() { //???RKM??? This is bogus, shouldn't this have something to do with the data ??? return new Dimension(175, 125); } /** * Returns the minimum dimensions to properly display this component. * This is a standard Java AWT method which gets called to determine * the minimum size of this component. * * @see #preferredSize */ public synchronized Dimension minimumSize() { return new Dimension(50, 50); } /** * Takes no action. * This is a standard Java AWT method which gets called to specify * which layout manager should be used to layout the components in * standard containers. * * Since layout managers CANNOT BE USED with this container the standard * setLayout has been OVERRIDDEN for this container and does nothing. * * @param lm the layout manager to use to layout this container's components * (IGNORED) * @see java.awt.Container#getLayout **/ public void setLayout(LayoutManager lm) { } /** * Tells this component that it has been added to a container. * This is a standard Java AWT method which gets called by the AWT when * this component is added to a container. Typically, it is used to * create this component's peer. * * It has been overridden here to hook-up event listeners. * * @see #removeNotify */ /* JDK1.1 //Hook up listeners public synchronized void addNotify() { super.addNotify(); if (mouse == null) { mouse = new Mouse(); addMouseListener(mouse); } if (key == null) { key = new Key(); addKeyListener(key); } if (adjustment == null) { adjustment = new Adjustment(); verticalScrollBar.addAdjustmentListener(adjustment); horizontalScrollBar.addAdjustmentListener(adjustment); } if (focus == null) { focus = new Focus(); addFocusListener(focus); } } */ /** * Tells this component that it is being removed from a container. * This is a standard Java AWT method which gets called by the AWT when * this component is removed from a container. Typically, it is used to * destroy the peers of this component and all its subcomponents. * * It has been overridden here to unhook event listeners. * * @see #addNotify */ /*JDK1.1 public synchronized void removeNotify() { //Unhook listeners if (mouse != null) { removeMouseListener(mouse); mouse = null; } if (key != null) { removeKeyListener(key); key = null; } if (adjustment != null) { verticalScrollBar.removeAdjustmentListener(adjustment); horizontalScrollBar.removeAdjustmentListener(adjustment); adjustment = null; } if (focus != null) { removeFocusListener(focus); focus = null; } super.removeNotify(); } */ /** * Adds the specified action listener to receive action events * from this button. * @param l the action listener */ /* JDK1.1 public void addActionListener(ActionListener l) { actionListener = AWTEventMulticaster.add(actionListener, l); } */ /** * Removes the specified action listener so it no longer receives * action events from this button. * @param l the action listener */ /* JDK1.1 public void removeActionListener(ActionListener l) { actionListener = AWTEventMulticaster.remove(actionListener, l); } */ void scrolled () { redrawTriggered = true; repaint(); } protected void triggerRedraw() { redrawTriggered = true; treeChanged = true; repaint(); } // Private members /* JDK1.1 protected Key key = null; protected Mouse mouse = null; protected Adjustment adjustment = null; protected ActionListener actionListener = null; protected ItemListener itemListener = null; protected Focus focus = null; */ protected boolean redrawTriggered = false; protected boolean treeChanged = false; protected boolean hasFocus = false;}class InvalidTreeNode2Exception extends Exception{}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -