📄 treeview2.java
字号:
{ requestFocus(); //int x = event.getX(); JDK1.1 //int y = event.getY(); int index = (y / cellSize) + sbVPosition; //If clicked below the last node if (index > viewCount-1) return true; TreeNode2 oldNode = selectedNode; TreeNode2 newNode = (TreeNode2)v.elementAt(index); int newDepth = newNode.getDepth(); changeSelection(newNode); // check for toggle box click // todo: make it a bit bigger Rectangle toggleBox = new Rectangle(cellSize*newDepth + cellSize/4 - sbHPosition, (index-sbVPosition)*cellSize + clickSize/2, clickSize, clickSize); if (toggleBox.inside(x,y)) { newNode.toggle(); //sendActionEvent(); triggerRedraw(); } else { // check for double click if ((newNode==oldNode) && event.clickCount == 2) { //newNode.toggle(); //triggerRedraw(); sendActionEvent(); } else { //single click action could be added here //timeMouseDown = event.getWhen(); } } return true; } //} //class Key extends KeyAdapter JDK1.1 //{ /** * Processes KEY_PRESS and KEY_ACTION events. * This is a standard Java AWT method which gets called by the AWT * method handleEvent() in response to receiving a KEY_PRESS or * KEY_ACTION event. These events occur when this component has the focus * and the user presses a "normal" or an "action" (F1, page up, etc) key. * * @param event the Event * @param key the key that was pressed * @return true if the event was handled * @see java.awt.Component#keyUp * @see #handleEvent */ public boolean keyDown(Event event, int key) { int index = v.indexOf(selectedNode); switch (key) { //case KeyEvent.VK_ENTER: //enter key case 13: sendActionEvent(); requestFocus(); break; case Event.LEFT: //left arrow if(event.controlDown()) { if(sbHPosition > 0) { horizontalScrollBar.setValue(Math.max(sbHPosition-=sbHLineIncrement,0)); repaint(); } break; } else if (selectedNode.isExpanded()) { selectedNode.toggle(); triggerRedraw(); break; } // else drop through to "UP" with no "break;" case Event.UP: if (index > 0) { index--; changeSelection((TreeNode2)v.elementAt(index)); requestFocus(); } break; case Event.RIGHT: if(event.controlDown()) { int max = horizontalScrollBar.getMaximum()-(isSun1_1?size().width-sbVWidth:0); if(sbHShow && sbHPosition < max) { horizontalScrollBar.setValue(Math.min(sbHPosition+=sbHLineIncrement, max)); repaint(); } break; } else if (selectedNode.isExpandable() && (!selectedNode.isExpanded())) { selectedNode.toggle(); //sendActionEvent(); triggerRedraw(); break; } if (!selectedNode.isExpandable()) { break; } // else drop thru' to DOWN case Event.DOWN: if (index < viewCount-1) { index++; changeSelection((TreeNode2)v.elementAt(index)); requestFocus(); break; } break; default: return false; } return true; } //}//class Focus extends FocusAdapter JDK1.1//{ public boolean gotFocus (Event e, Object what) { hasFocus = true; if (selectedNode != null && v != null) drawNodeText(selectedNode, (v.indexOf(selectedNode) - sbVPosition)*cellSize, true); return true; } public boolean focusLost(Event e, Object what) { hasFocus = false; if (selectedNode != null && v != null) drawNodeText(selectedNode, (v.indexOf(selectedNode) - sbVPosition)*cellSize, true); return true; }//} protected void sendActionEvent() { /* if (actionListener != null) actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, new String(selectedNode.getText()))); */ deliverEvent (new Event (this, Event.ACTION_EVENT, selectedNode.getText())); } /** * Gets the currently selected node. * @return the currently selected node, or null if none selected */ public TreeNode2 getSelectedNode() { return selectedNode; } /** * Gets the text of the currently selected node. * @return the text of the currently selected node or null if no node * is selected */ public String getSelectedText() { if (selectedNode == null) return null; return selectedNode.getText(); } protected void changeSelection(TreeNode2 node) { if (node == selectedNode) return; TreeNode2 oldNode = selectedNode; selectedNode = node; drawNodeText(oldNode, (v.indexOf(oldNode) - sbVPosition)*cellSize, true); drawNodeText(node, (v.indexOf(node) - sbVPosition)*cellSize, true); // send select event int index = v.indexOf(selectedNode); /* FIX: replace with SEL_CHANGED event? if (itemListener != null) { itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, oldNode, ItemEvent.DESELECTED)); itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, selectedNode, ItemEvent.SELECTED)); } */ if (index < sbVPosition) { //scroll up sbVPosition--; verticalScrollBar.setValue(sbVPosition); triggerRedraw(); return; } if (index >= sbVPosition + (viewHeight-cellSize/2)/cellSize) { sbVPosition++; verticalScrollBar.setValue(sbVPosition); triggerRedraw(); return; } repaint(); } // ----------------------------------------- // --------- graphics related methods ------ // ----------------------------------------- /** * Handles redrawing of this component on the screen. * This is a standard Java AWT method which gets called by the Java * AWT (repaint()) to handle repainting this component on the screen. * The graphics context clipping region is set to the bounding rectangle * of this component and its [0,0] coordinate is this component's * top-left corner. * Typically this method paints the background color to clear the * component's drawing space, sets graphics context to be the foreground * color, and then calls paint() to draw the component. * * It is overridden here to reduce flicker by eliminating the uneeded * clearing of the background. * * @param g the graphics context * @see java.awt.Component#repaint * @see #paint */ public synchronized void update (Graphics g) { //(eliminates background draw to reduce flicker) paint(g); } /** * Paints this component using the given graphics context. * This is a standard Java AWT method which typically gets called * by the AWT to handle painting this component. It paints this component * using the given graphics context. The graphics context clipping region * is set to the bounding rectangle of this component and its [0,0] * coordinate is this component's top-left corner. * * @param g the graphics context used for painting * @see java.awt.Component#repaint * @see #update */ public void paint (Graphics g) { Dimension d = size(); if ((d.width != viewWidth) || (d.height != viewHeight)) triggerRedraw (); if (redrawTriggered) { // redraw needed, or size has changed redraw(g); } /* //???RKM??? Is this really needed else if (java.beans.Beans.isDesignTime()) { resetVector(); newWidth = compWidth(g); drawTree(); } */ g.translate(-sbHPosition, 0); //g.clearRect(sbHPosition,0,d.width-sbVWidth,d.height-sbHHeight); if (sbVShow && sbHShow) { g.setColor(Color.lightGray); g.fillRect(sbHPosition+d.width-sbVWidth, d.height-sbHHeight, sbVWidth, sbHHeight); } g.clipRect(sbHPosition,0,d.width-sbVWidth,d.height-sbHHeight); g.drawImage(im1, 0, 0, this); g.setColor(Color.black); g.drawRect(sbHPosition,0, d.width-sbVWidth-1, d.height-sbHHeight-1); } /** * Lays out the vertical scrollbar as needed, then draws the TreeView2 into an offscreen image. This is used for cleaner refresh. */ public void redraw() { //For backward compatibality. Do not allow to call only redraw() without recalculation. triggerRedraw(); } public void redraw(Graphics g) { boolean recalculate = treeChanged; int inRectCount = 0; redrawTriggered = false; treeChanged = false; Dimension d = size(); if (recalculate) { resetVector(); newWidth = compWidth(g); inRectCount = ((d.height-sbHHeight)/cellSize); if (viewCount > inRectCount) { // need the vertical scrollbar sbVShow = true; sbVWidth = verticalScrollBar.preferredSize().width; } else { sbVShow = false; sbVWidth = 0; sbVPosition = 0; } if (newWidth > (d.width-sbVWidth)) { // need the horizontal scrollbar sbHShow = true; sbHHeight = horizontalScrollBar.preferredSize().height; } else { sbHShow = false; sbHHeight = 0; sbHPosition = 0; } } drawTree(); if (recalculate) { verticalScrollBar.setValues(sbVPosition, inRectCount, 0, viewCount-(isSun1_1?0:inRectCount)); verticalScrollBar.setPageIncrement(inRectCount-1); horizontalScrollBar.setValues(sbHPosition, d.width-sbVWidth, 0, sbHSize-(isSun1_1?0:(d.width-sbVWidth))); horizontalScrollBar.setPageIncrement(d.width-sbVWidth); horizontalScrollBar.setLineIncrement(sbHLineIncrement); if (sbVShow) { verticalScrollBar.reshape(d.width-sbVWidth,0,sbVWidth,d.height-sbHHeight); verticalScrollBar.show(); } else { verticalScrollBar.hide(); } if (sbHShow) { horizontalScrollBar.reshape(0,d.height-sbHHeight,d.width-sbVWidth,sbHHeight); horizontalScrollBar.show(); } else { horizontalScrollBar.hide(); } } } private int compWidth(Graphics gg) { int size = 0; int textOffset; TreeNode2 node; Font f = getFont(); //unix version might not provide a default font //Make certain there is a font if (f == null) { f = new Font("TimesRoman", Font.PLAIN, 13); gg.setFont(f); setFont(f); } fm = gg.getFontMetrics(); for (int i=0; i<v.size(); i++) { node = (TreeNode2)v.elementAt(i); textOffset = ((node.depth + 1) * (cellSize)) + cellSize + textInset - (node.getImage()==null ? 12:0); if (size < (textOffset+fm.stringWidth(node.text)+6)) size = textOffset+fm.stringWidth(node.text)+6; } return size; } /** * Draws the TreeView2 into an offscreen image. This is used for cleaner refresh. */ public void drawTree() { Dimension d = size(); if ((d.width != viewWidth) || (d.height != viewHeight) || (g1==null) || (sbHSize!=newWidth)) { // size has changed, must resize image im1 = createImage(Math.max(sbHSize=newWidth, d.width), d.height); if (g1 != null) { g1.dispose(); } g1 = im1.getGraphics(); viewWidth=d.width; viewHeight=d.height; } Font f = getFont(); //unix version might not provide a default font //Make certain there is a font if (f == null) { f = new Font("TimesRoman", Font.PLAIN, 13); g1.setFont(f); setFont(f); } //Make certain the graphics object has a font (Mac doesn't seem to) if (f != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -