📄 lwtree.java
字号:
/**
* Caption: Zaval Light-Weight Visual Components Library
* $Revision: 2.79 $
* $Date: 2003/08/22 11:24:16 $
*
* @author: Andrei Vishnevsky
* @version: 3.50
*
* Zaval Light-Weight Visual Components Library (LwVCL) is a pure Java
* alternative to humble AWT-based and SWING-based GUI interfaces for
* wide ranges of platforms, including J2SE, PersonalJava and J2ME.
*
* Designed as light-weight but, alternatively to Swing, built separately
* from AWT (not on top of the java.awt library like Swing), the LwVCL is
* the good alternative to highly performant, memory-efficient, flexible
* GUI solution for embedded, stand-alone and applet applications.
*
* For more info on this product read Zaval Light-Weight Visual Components Library Tutorial
* (It comes within this package).
* The latest product version is always available from the product's homepage:
* http://www.zaval.org/products/lwvcl/
* and from the SourceForge:
* http://sourceforge.net/projects/zaval0003/
*
* Contacts:
* Support : support@zaval.org
* Change Requests : change-request@zaval.org
* Feedback : feedback@zaval.org
* Other : info@zaval.org
*
* Copyright (C) 2001-2003 Zaval Creative Engineering Group (http://www.zaval.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* (version 2) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.zaval.lw.tree;import java.util.*;import java.awt.*;import java.awt.event.*;import org.zaval.data.*;import org.zaval.data.event.*;import org.zaval.lw.event.*;import org.zaval.lw.*;import org.zaval.misc.*;import org.zaval.util.*;/** * This is tree view component. The component renders the specified <code>TreeModel</code> * and organizes navigation through the tree. The sample below illustrates * the component usage: * <pre> * ... * Item root = new Item("root"); * TreeModel data = new Tree(root); * data.add(data.getRoot(), new Item("First Child root")); * data.add(data.getRoot(), new Item("Second Child root")); * LwTree tree = new LwTree(data); * ... * </pre> * <p> * The class provides ability to customize the tree node rendering by defining the * <code>LwViewProvider</code> provider. It allows to specify the view for the tree item. * The default tree item view is LwTextRender. * <p> * Use <code>addSelectionListener</code> and <code>removeSelectionListener</code> methods to * handle selection events. The events are performed by the component whenever the tree node * has been selected, deselected. * <p> * Use <code>addActionListener</code> and <code>removeActionListener</code> methods to * be notified whenever the node has been expanded or collapsed. The action event contains * the tree expanded or collapsed item that can be got by <code>getData</code> method of the * action event object. * <p> * It is possible to disable node selection by the <code>enableSelection</code> method. * <p> * It is possible to customize views for some elements of the tree by <code>setView</code> * The default views of the elements are read as static objects by following keys: * <ul> * <li>The toggle off view is fetched by "toggle.off" key.</li> * <li>The toggle on view is fetched by "toggle.on" key.</li> * <li>The list node view is fetched by the "tree.least" key.</li> * <li>The opened node view is fetched by the "tree.open" key.</li> * <li>The closed node view is fetched by the "tree.close" key.</li> * </ul> * <p> * The tree nodes height and width can be extended by <code>setGaps</code> method that * defines horizontal and vertical gaps between the nodes. * <p> * It is possible disables tree view line rendering by <code>showLines</code> method. */public class LwTreeextends LwCanvasimplements TreeListener, LwViewProvider, LwMouseListener, LwFocusListener, LwKeyListener , ScrollObj{ /** * Defines list view element type. */ public static final int LEAST_VIEW = 1; /** * Defines opened view element type. */ public static final int OPENED_VIEW = 2; /** * Defines closed view element type. */ public static final int CLOSED_VIEW = 3; /** * Defines toggle off view element type. */ public static final int TOGGLE_OFF_VIEW = 4; /** * Defines toggle on view element type. */ public static final int TOGGLE_ON_VIEW = 5; /* To speed up */ private static final Dimension ZERO = new Dimension(); private TreeModel data; private LwActionSupport actions; private LwActionSupport selection; private boolean isOpenVal; private LwViewProvider provider; private LwView openedImg, closedImg, leastImg, toggleOnView, toggleOffView; private Hashtable nodes; private int gapx = 5, gapy = 2, dx, dy; private Item selected, firstVisible; private Rectangle visibleArea; private Dimension max,openedImgSize, closedImgSize, leastImgSize, toggleOnSize, toggleOffSize; private ScrollMan man; private LwTextRender defaultRender = new LwTextRender(""); private Color selectColor1 = LwToolkit.darkBlue, selectColor2 = Color.yellow, linesColor = Color.gray; /** * Constructs a tree view component with the specified tree model. * @param <code>d</code> the specified tree model. */ public LwTree(TreeModel d) { this(d, true); } /** * Constructs a tree view component with the specified tree model and tree node state. * The state defines if all nodes that have children should be opened or closed. * @param <code>d</code> the specified tree model. * @param <code>b</code> the specified tree node state. */ public LwTree(TreeModel d, boolean b) { isOpenVal = b; setModel(d); setViewProvider(this); setView(TOGGLE_ON_VIEW, LwManager.getView("toggle.on")); setView(TOGGLE_OFF_VIEW, LwManager.getView("toggle.off")); setView(LEAST_VIEW, LwManager.getView("tree.least")); setView(OPENED_VIEW, LwManager.getView("tree.open")); setView(CLOSED_VIEW, LwManager.getView("tree.close")); enableSelection(true); showLines(true); } public /*C#override*/ boolean canHaveFocus() { return true; } /** * Enables the tree node selecting. * @param <code>b</code> use <code>true</code> to enable the tree node selecting; * <code>false</code> otherwise. */ public void enableSelection(boolean b) { if (isSelectionEnabled() != b) { if (!b && selected != null) select(null); bits = MathBox.getBits(bits, ENSEL_BIT, b); repaint(); } } /** * Tests if the tree items selection is enabled. * @return <code>true</code> if the tree items selection is enabled; * <code>false</code> otherwise. */ public boolean isSelectionEnabled() { return MathBox.checkBit(bits, ENSEL_BIT); } /** * Enables the tree lines rendering. * @param <code>b</code> use <code>true</code> to enable the tree line rendering; * <code>false</code> otherwise. */ public void showLines(boolean b) { if (isLinesShown() != b) { bits = MathBox.getBits(bits, ENLINES_BIT, b); repaint(); } } /** * Tests if the tree line rendering is enabled. * @return <code>true</code> if the line rendering is enabled; * <code>false</code> otherwise. */ public boolean isLinesShown() { return MathBox.checkBit(bits, ENLINES_BIT); } /** * Gets the color that is used to paint tree lines. * @param a color. */ public Color getLinesColor() { return linesColor; } /** * Sets the specified color to paint tree lines. * @param <code>c</code> the specified color. */ public void setLinesColor(Color c) { if (!c.equals (linesColor)) { linesColor = c; repaint(); } } /** * Sets the specified vertical and horizontal gaps. The method doesn't touch * a gap in case if it is less zero, so it is possible to use the method to * set only vertical or only horizontal gap. * @param <code>gx</code> the specified horizontal gap. * @param <code>gy</code> the specified vertical gap. */ public void setGaps (int gx, int gy) { if ((gx >= 0 && gx != gapx)|| (gy >= 0 && gy != gapy) ) { gapx = gx<0?gapx:gx; gapy = gy<0?gapy:gy; invalidateMetrics(); repaint(); } } /** * Gets the tree model that is rendered with the tree view component. * @return a tree model. */ public TreeModel getModel() { return data; } /** * Sets the specified view provider. The provider is used to define a view that * will be used to paint the node. It is possible to use <code>null</code> value * as the input argument, in this case the LwTextRender view is used (the method * sets itself as the view provider) to paint the node. * @param <code>p</code> the specified view provider. */ public void setViewProvider (LwViewProvider p) { if (p == null) p = this; if (provider != p) { provider = p; invalidateData(); repaint(); } } /** * Gets the view provider. * @return a view provider. */ public LwViewProvider getViewProvider () { return provider; } /** * Sets the specified view for the specified tree view element. Use one of the following * constants as the view element id: * <ul> * <li> * LEAST_VIEW - to define least node element. * </li> * <li> * OPENED_VIEW - to define opened node element. * </li> * <li> * CLOSED_VIEW - to define closed node element. * </li> * <li> * TOGGLE_OFF_VIEW - to define toggle off node element. * </li> * <li> * TOGGLE_ON_VIEW - to define toggle on node element. * </li> * </ul> * @param <code>id</code> the specified element id. * @param <code>v</code> the specified view. */ public void setView(int id, LwView v) { switch (id) { case LEAST_VIEW : if (v != leastImg) { leastImg = v; leastImgSize = viewPS(v); } break; case OPENED_VIEW : if (v != openedImg) { openedImg = v; openedImgSize = viewPS(v); } break; case CLOSED_VIEW : if (v != closedImg) { closedImg = v; closedImgSize = viewPS(v); } break; case TOGGLE_OFF_VIEW: if (v != toggleOffView) { toggleOffView = v; toggleOffSize = viewPS(v); } break; case TOGGLE_ON_VIEW : if (v != toggleOnView) { toggleOnView = v; toggleOnSize = viewPS(v); } break; default: throw new IllegalArgumentException(); } invalidateMetrics(); repaint(); } /** * Sets the specified tree model to be rendered with the component. * @param <code>d</code> the specified tree model. */ public void setModel(TreeModel d) { if (data != d) { if (data != null) data.removeTreeListener(this); data = d; if (data != null) data.addTreeListener(this); invalidateData(); repaint(); } } /** * Paints this component. * @param <code>g</code> the graphics context to be used for painting. */ public /*C#override*/ void paint (Graphics g) { if (data != null) { vVisibility(); if (MathBox.checkBit(bits, VALVIS_BIT) && firstVisible != null) paintTree (g, firstVisible); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -