0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package javax.swing;
0019:
0020: import java.awt.Color;
0021: import java.awt.Component;
0022: import java.awt.Cursor;
0023: import java.awt.Dimension;
0024: import java.awt.Font;
0025: import java.awt.FontMetrics;
0026: import java.awt.GraphicsEnvironment;
0027: import java.awt.HeadlessException;
0028: import java.awt.Point;
0029: import java.awt.Rectangle;
0030: import java.awt.event.FocusListener;
0031: import java.awt.event.MouseEvent;
0032: import java.beans.PropertyChangeListener;
0033: import java.io.Serializable;
0034: import java.util.ArrayList;
0035: import java.util.Enumeration;
0036: import java.util.HashMap;
0037: import java.util.HashSet;
0038: import java.util.Hashtable;
0039: import java.util.Iterator;
0040: import java.util.LinkedList;
0041: import java.util.List;
0042: import java.util.Locale;
0043: import java.util.Map;
0044: import java.util.NoSuchElementException;
0045: import java.util.Vector;
0046: import javax.accessibility.Accessible;
0047: import javax.accessibility.AccessibleAction;
0048: import javax.accessibility.AccessibleComponent;
0049: import javax.accessibility.AccessibleContext;
0050: import javax.accessibility.AccessibleRole;
0051: import javax.accessibility.AccessibleSelection;
0052: import javax.accessibility.AccessibleStateSet;
0053: import javax.accessibility.AccessibleText;
0054: import javax.accessibility.AccessibleValue;
0055: import javax.swing.event.TreeExpansionEvent;
0056: import javax.swing.event.TreeExpansionListener;
0057: import javax.swing.event.TreeModelEvent;
0058: import javax.swing.event.TreeModelListener;
0059: import javax.swing.event.TreeSelectionEvent;
0060: import javax.swing.event.TreeSelectionListener;
0061: import javax.swing.event.TreeWillExpandListener;
0062: import javax.swing.plaf.TreeUI;
0063: import javax.swing.text.Position;
0064: import javax.swing.tree.DefaultMutableTreeNode;
0065: import javax.swing.tree.DefaultTreeModel;
0066: import javax.swing.tree.DefaultTreeSelectionModel;
0067: import javax.swing.tree.ExpandVetoException;
0068: import javax.swing.tree.TreeCellEditor;
0069: import javax.swing.tree.TreeCellRenderer;
0070: import javax.swing.tree.TreeModel;
0071: import javax.swing.tree.TreeNode;
0072: import javax.swing.tree.TreePath;
0073: import javax.swing.tree.TreeSelectionModel;
0074: import org.apache.harmony.luni.util.NotImplementedException;
0075:
0076: import org.apache.harmony.x.swing.internal.nls.Messages;
0077:
0078: /**
0079: * <p>
0080: * <i>JTree</i>
0081: * </p>
0082: * <h3>Implementation Notes:</h3>
0083: * <ul>
0084: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
0085: * optimization, not as a guarantee of serialization compatibility.</li>
0086: * </ul>
0087: */
0088: public class JTree extends JComponent implements Scrollable, Accessible {
0089: private static final long serialVersionUID = -3884445419090632712L;
0090:
0091: protected class AccessibleJTree extends AccessibleJComponent
0092: implements AccessibleSelection, TreeSelectionListener,
0093: TreeModelListener, TreeExpansionListener {
0094: private static final long serialVersionUID = -8714565563782619758L;
0095:
0096: protected class AccessibleJTreeNode extends AccessibleContext
0097: implements Accessible, AccessibleComponent,
0098: AccessibleSelection, AccessibleAction {
0099:
0100: private Accessible accessibleParent;
0101:
0102: public AccessibleJTreeNode(JTree t, TreePath p,
0103: Accessible ap) throws NotImplementedException {
0104: throw new NotImplementedException();
0105: }
0106:
0107: public AccessibleContext getAccessibleContext()
0108: throws NotImplementedException {
0109: throw new NotImplementedException();
0110: }
0111:
0112: @Override
0113: public String getAccessibleName()
0114: throws NotImplementedException {
0115: throw new NotImplementedException();
0116: }
0117:
0118: @Override
0119: public void setAccessibleName(String s)
0120: throws NotImplementedException {
0121: throw new NotImplementedException();
0122: }
0123:
0124: @Override
0125: public String getAccessibleDescription()
0126: throws NotImplementedException {
0127: throw new NotImplementedException();
0128: }
0129:
0130: @Override
0131: public void setAccessibleDescription(String s)
0132: throws NotImplementedException {
0133: throw new NotImplementedException();
0134: }
0135:
0136: @Override
0137: public AccessibleRole getAccessibleRole()
0138: throws NotImplementedException {
0139: throw new NotImplementedException();
0140: }
0141:
0142: @Override
0143: public AccessibleStateSet getAccessibleStateSet()
0144: throws NotImplementedException {
0145: throw new NotImplementedException();
0146: }
0147:
0148: @Override
0149: public Accessible getAccessibleParent()
0150: throws NotImplementedException {
0151: throw new NotImplementedException();
0152: }
0153:
0154: @Override
0155: public int getAccessibleIndexInParent()
0156: throws NotImplementedException {
0157: throw new NotImplementedException();
0158: }
0159:
0160: @Override
0161: public int getAccessibleChildrenCount()
0162: throws NotImplementedException {
0163: throw new NotImplementedException();
0164: }
0165:
0166: @Override
0167: public Accessible getAccessibleChild(int i)
0168: throws NotImplementedException {
0169: throw new NotImplementedException();
0170: }
0171:
0172: @Override
0173: public Locale getLocale() throws NotImplementedException {
0174: throw new NotImplementedException();
0175: }
0176:
0177: @Override
0178: public void addPropertyChangeListener(
0179: PropertyChangeListener l)
0180: throws NotImplementedException {
0181: throw new NotImplementedException();
0182: }
0183:
0184: @Override
0185: public void removePropertyChangeListener(
0186: PropertyChangeListener l)
0187: throws NotImplementedException {
0188: throw new NotImplementedException();
0189: }
0190:
0191: @Override
0192: public AccessibleAction getAccessibleAction()
0193: throws NotImplementedException {
0194: throw new NotImplementedException();
0195: }
0196:
0197: @Override
0198: public AccessibleComponent getAccessibleComponent()
0199: throws NotImplementedException {
0200: throw new NotImplementedException();
0201: }
0202:
0203: @Override
0204: public AccessibleSelection getAccessibleSelection()
0205: throws NotImplementedException {
0206: throw new NotImplementedException();
0207: }
0208:
0209: @Override
0210: public AccessibleText getAccessibleText()
0211: throws NotImplementedException {
0212: throw new NotImplementedException();
0213: }
0214:
0215: @Override
0216: public AccessibleValue getAccessibleValue()
0217: throws NotImplementedException {
0218: throw new NotImplementedException();
0219: }
0220:
0221: public Color getBackground() throws NotImplementedException {
0222: throw new NotImplementedException();
0223: }
0224:
0225: public void setBackground(Color c)
0226: throws NotImplementedException {
0227: throw new NotImplementedException();
0228: }
0229:
0230: public Color getForeground() throws NotImplementedException {
0231: throw new NotImplementedException();
0232: }
0233:
0234: public void setForeground(Color c)
0235: throws NotImplementedException {
0236: throw new NotImplementedException();
0237: }
0238:
0239: public Cursor getCursor() throws NotImplementedException {
0240: throw new NotImplementedException();
0241: }
0242:
0243: public void setCursor(Cursor c)
0244: throws NotImplementedException {
0245: throw new NotImplementedException();
0246: }
0247:
0248: public Font getFont() throws NotImplementedException {
0249: throw new NotImplementedException();
0250: }
0251:
0252: public void setFont(Font f) throws NotImplementedException {
0253: throw new NotImplementedException();
0254: }
0255:
0256: public FontMetrics getFontMetrics(Font f)
0257: throws NotImplementedException {
0258: throw new NotImplementedException();
0259: }
0260:
0261: public boolean isEnabled() throws NotImplementedException {
0262: throw new NotImplementedException();
0263: }
0264:
0265: public void setEnabled(boolean b)
0266: throws NotImplementedException {
0267: throw new NotImplementedException();
0268: }
0269:
0270: public boolean isVisible() throws NotImplementedException {
0271: throw new NotImplementedException();
0272: }
0273:
0274: public void setVisible(boolean b)
0275: throws NotImplementedException {
0276: throw new NotImplementedException();
0277: }
0278:
0279: public boolean isShowing() throws NotImplementedException {
0280: throw new NotImplementedException();
0281: }
0282:
0283: public boolean contains(Point p)
0284: throws NotImplementedException {
0285: throw new NotImplementedException();
0286: }
0287:
0288: public Point getLocationOnScreen()
0289: throws NotImplementedException {
0290: throw new NotImplementedException();
0291: }
0292:
0293: protected Point getLocationInJTree()
0294: throws NotImplementedException {
0295: throw new NotImplementedException();
0296: }
0297:
0298: public Point getLocation() throws NotImplementedException {
0299: throw new NotImplementedException();
0300: }
0301:
0302: public void setLocation(Point p)
0303: throws NotImplementedException {
0304: throw new NotImplementedException();
0305: }
0306:
0307: public Rectangle getBounds() throws NotImplementedException {
0308: throw new NotImplementedException();
0309: }
0310:
0311: public void setBounds(Rectangle r)
0312: throws NotImplementedException {
0313: throw new NotImplementedException();
0314: }
0315:
0316: public Dimension getSize() throws NotImplementedException {
0317: throw new NotImplementedException();
0318: }
0319:
0320: public void setSize(Dimension d)
0321: throws NotImplementedException {
0322: throw new NotImplementedException();
0323: }
0324:
0325: public Accessible getAccessibleAt(Point p)
0326: throws NotImplementedException {
0327: throw new NotImplementedException();
0328: }
0329:
0330: public boolean isFocusTraversable()
0331: throws NotImplementedException {
0332: throw new NotImplementedException();
0333: }
0334:
0335: public void requestFocus() throws NotImplementedException {
0336: throw new NotImplementedException();
0337: }
0338:
0339: public void addFocusListener(FocusListener l)
0340: throws NotImplementedException {
0341: throw new NotImplementedException();
0342: }
0343:
0344: public void removeFocusListener(FocusListener l)
0345: throws NotImplementedException {
0346: throw new NotImplementedException();
0347: }
0348:
0349: public int getAccessibleSelectionCount()
0350: throws NotImplementedException {
0351: throw new NotImplementedException();
0352: }
0353:
0354: public Accessible getAccessibleSelection(int i)
0355: throws NotImplementedException {
0356: throw new NotImplementedException();
0357: }
0358:
0359: public boolean isAccessibleChildSelected(int i)
0360: throws NotImplementedException {
0361: throw new NotImplementedException();
0362: }
0363:
0364: public void addAccessibleSelection(int i)
0365: throws NotImplementedException {
0366: throw new NotImplementedException();
0367: }
0368:
0369: public void removeAccessibleSelection(int i)
0370: throws NotImplementedException {
0371: throw new NotImplementedException();
0372: }
0373:
0374: public void clearAccessibleSelection()
0375: throws NotImplementedException {
0376: throw new NotImplementedException();
0377: }
0378:
0379: public void selectAllAccessibleSelection()
0380: throws NotImplementedException {
0381: throw new NotImplementedException();
0382: }
0383:
0384: public int getAccessibleActionCount()
0385: throws NotImplementedException {
0386: throw new NotImplementedException();
0387: }
0388:
0389: public String getAccessibleActionDescription(int i)
0390: throws NotImplementedException {
0391: throw new NotImplementedException();
0392: }
0393:
0394: public boolean doAccessibleAction(int i)
0395: throws NotImplementedException {
0396: throw new NotImplementedException();
0397: }
0398: }
0399:
0400: public AccessibleJTree() {
0401: super ();
0402: }
0403:
0404: public void valueChanged(TreeSelectionEvent e)
0405: throws NotImplementedException {
0406: throw new NotImplementedException();
0407: }
0408:
0409: public void fireVisibleDataPropertyChange()
0410: throws NotImplementedException {
0411: throw new NotImplementedException();
0412: }
0413:
0414: public void treeNodesChanged(TreeModelEvent e)
0415: throws NotImplementedException {
0416: throw new NotImplementedException();
0417: }
0418:
0419: public void treeNodesInserted(TreeModelEvent e)
0420: throws NotImplementedException {
0421: throw new NotImplementedException();
0422: }
0423:
0424: public void treeNodesRemoved(TreeModelEvent e)
0425: throws NotImplementedException {
0426: throw new NotImplementedException();
0427: }
0428:
0429: public void treeStructureChanged(TreeModelEvent e)
0430: throws NotImplementedException {
0431: throw new NotImplementedException();
0432: }
0433:
0434: public void treeCollapsed(TreeExpansionEvent e)
0435: throws NotImplementedException {
0436: throw new NotImplementedException();
0437: }
0438:
0439: public void treeExpanded(TreeExpansionEvent e)
0440: throws NotImplementedException {
0441: throw new NotImplementedException();
0442: }
0443:
0444: @Override
0445: public AccessibleRole getAccessibleRole()
0446: throws NotImplementedException {
0447: throw new NotImplementedException();
0448: }
0449:
0450: @Override
0451: public Accessible getAccessibleAt(Point p)
0452: throws NotImplementedException {
0453: throw new NotImplementedException();
0454: }
0455:
0456: @Override
0457: public int getAccessibleChildrenCount()
0458: throws NotImplementedException {
0459: throw new NotImplementedException();
0460: }
0461:
0462: @Override
0463: public Accessible getAccessibleChild(int i)
0464: throws NotImplementedException {
0465: throw new NotImplementedException();
0466: }
0467:
0468: @Override
0469: public int getAccessibleIndexInParent()
0470: throws NotImplementedException {
0471: throw new NotImplementedException();
0472: }
0473:
0474: @Override
0475: public AccessibleSelection getAccessibleSelection()
0476: throws NotImplementedException {
0477: throw new NotImplementedException();
0478: }
0479:
0480: public int getAccessibleSelectionCount()
0481: throws NotImplementedException {
0482: throw new NotImplementedException();
0483: }
0484:
0485: public Accessible getAccessibleSelection(int i)
0486: throws NotImplementedException {
0487: throw new NotImplementedException();
0488: }
0489:
0490: public boolean isAccessibleChildSelected(int i)
0491: throws NotImplementedException {
0492: throw new NotImplementedException();
0493: }
0494:
0495: public void addAccessibleSelection(int i)
0496: throws NotImplementedException {
0497: throw new NotImplementedException();
0498: }
0499:
0500: public void removeAccessibleSelection(int i)
0501: throws NotImplementedException {
0502: throw new NotImplementedException();
0503: }
0504:
0505: public void clearAccessibleSelection()
0506: throws NotImplementedException {
0507: throw new NotImplementedException();
0508: }
0509:
0510: public void selectAllAccessibleSelection()
0511: throws NotImplementedException {
0512: throw new NotImplementedException();
0513: }
0514: }
0515:
0516: public static class DynamicUtilTreeNode extends
0517: DefaultMutableTreeNode {
0518: private static final long serialVersionUID = -2795134038906279615L;
0519:
0520: protected boolean hasChildren;
0521:
0522: protected Object childValue;
0523:
0524: protected boolean loadedChildren;
0525:
0526: public static void createChildren(
0527: DefaultMutableTreeNode parent, Object children) {
0528: loadChildren(parent, children);
0529: }
0530:
0531: public DynamicUtilTreeNode(Object value, Object children) {
0532: super (value, false);
0533: childValue = children;
0534: loadedChildren = !(children instanceof Object[])
0535: && !(children instanceof Vector)
0536: && !(children instanceof Hashtable);
0537: setAllowsChildren(!loadedChildren);
0538: }
0539:
0540: @Override
0541: public boolean isLeaf() {
0542: return !getAllowsChildren();
0543: }
0544:
0545: @Override
0546: public int getChildCount() {
0547: loadChildrenIfRequired();
0548: return super .getChildCount();
0549: }
0550:
0551: protected void loadChildren() {
0552: loadedChildren = true;
0553: loadChildren(this , childValue);
0554: }
0555:
0556: @Override
0557: public TreeNode getChildAt(int index) {
0558: loadChildrenIfRequired();
0559: return super .getChildAt(index);
0560: }
0561:
0562: @SuppressWarnings("unchecked")
0563: @Override
0564: public Enumeration children() {
0565: loadChildrenIfRequired();
0566: return super .children();
0567: }
0568:
0569: private void loadChildrenIfRequired() {
0570: if (!loadedChildren) {
0571: loadChildren();
0572: }
0573: }
0574:
0575: private static void loadChildren(DefaultMutableTreeNode node,
0576: Object nodeChildren) {
0577: boolean hasChildren = false;
0578: if (nodeChildren instanceof Object[]) {
0579: Object[] children = (Object[]) nodeChildren;
0580: for (Object element : children) {
0581: node.add(new DynamicUtilTreeNode(element, element));
0582: hasChildren = true;
0583: }
0584: } else if (nodeChildren instanceof Vector) {
0585: for (Iterator<?> it = ((Vector) nodeChildren)
0586: .iterator(); it.hasNext();) {
0587: Object child = it.next();
0588: node.add(new DynamicUtilTreeNode(child, child));
0589: hasChildren = true;
0590: }
0591: } else if (nodeChildren instanceof Hashtable) {
0592: for (Iterator<?> it = ((Hashtable) nodeChildren)
0593: .keySet().iterator(); it.hasNext();) {
0594: Object child = it.next();
0595: node.add(new DynamicUtilTreeNode(child, child));
0596: hasChildren = true;
0597: }
0598: }
0599: if (hasChildren) {
0600: node.setAllowsChildren(true);
0601: }
0602: }
0603: }
0604:
0605: protected static class EmptySelectionModel extends
0606: DefaultTreeSelectionModel {
0607: private static final long serialVersionUID = -2866787372484669512L;
0608:
0609: protected static final EmptySelectionModel sharedInstance = new EmptySelectionModel();
0610:
0611: public static EmptySelectionModel sharedInstance() {
0612: return sharedInstance;
0613: }
0614:
0615: @Override
0616: public void setSelectionPaths(TreePath[] pPaths) {
0617: }
0618:
0619: @Override
0620: public void addSelectionPaths(TreePath[] paths) {
0621: }
0622:
0623: @Override
0624: public void removeSelectionPaths(TreePath[] paths) {
0625: }
0626: }
0627:
0628: protected class TreeModelHandler implements TreeModelListener {
0629: public void treeNodesChanged(TreeModelEvent e) {
0630: }
0631:
0632: public void treeNodesInserted(TreeModelEvent e) {
0633: }
0634:
0635: public void treeNodesRemoved(TreeModelEvent e) {
0636: TreePath parentPath = e.getTreePath();
0637: Object[] children = e.getChildren();
0638: if (parentPath == null || children == null) {
0639: return;
0640: }
0641: for (Object element : children) {
0642: TreePath childPath = parentPath
0643: .pathByAddingChild(element);
0644: removeDescendantToggledPaths(getDescendantToggledPaths(childPath));
0645: }
0646: removeDescendantSelectedPaths(parentPath, false);
0647: }
0648:
0649: public void treeStructureChanged(TreeModelEvent e) {
0650: TreePath parentPath = e.getTreePath();
0651: if (parentPath == null) {
0652: return;
0653: }
0654: Object parentPathToggleStatus = togglePaths.get(parentPath);
0655: removeDescendantToggledPaths(getDescendantToggledPaths(parentPath));
0656: togglePaths.put(parentPath, parentPathToggleStatus);
0657: removeDescendantSelectedPaths(parentPath, false);
0658: }
0659: }
0660:
0661: protected class TreeSelectionRedirector implements Serializable,
0662: TreeSelectionListener {
0663: private static final long serialVersionUID = -5457497600720267892L;
0664:
0665: public void valueChanged(TreeSelectionEvent e) {
0666: fireValueChanged((TreeSelectionEvent) e
0667: .cloneWithSource(JTree.this ));
0668: }
0669: }
0670:
0671: public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
0672:
0673: public static final String TREE_MODEL_PROPERTY = "model";
0674:
0675: public static final String ROOT_VISIBLE_PROPERTY = "rootVisible";
0676:
0677: public static final String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
0678:
0679: public static final String ROW_HEIGHT_PROPERTY = "rowHeight";
0680:
0681: public static final String CELL_EDITOR_PROPERTY = "cellEditor";
0682:
0683: public static final String EDITABLE_PROPERTY = "editable";
0684:
0685: public static final String LARGE_MODEL_PROPERTY = "largeModel";
0686:
0687: public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
0688:
0689: public static final String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
0690:
0691: public static final String INVOKES_STOP_CELL_EDITING_PROPERTY = "invokesStopCellEditing";
0692:
0693: public static final String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
0694:
0695: public static final String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
0696:
0697: public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
0698:
0699: public static final String ANCHOR_SELECTION_PATH_PROPERTY = "anchorSelectionPath";
0700:
0701: public static final String EXPANDS_SELECTED_PATHS_PROPERTY = "expandsSelectedPaths";
0702:
0703: protected transient TreeModel treeModel;
0704:
0705: protected transient TreeSelectionModel selectionModel;
0706:
0707: protected boolean rootVisible;
0708:
0709: protected transient TreeCellRenderer cellRenderer;
0710:
0711: protected int rowHeight;
0712:
0713: protected boolean showsRootHandles;
0714:
0715: protected transient TreeSelectionRedirector selectionRedirector;
0716:
0717: protected transient TreeCellEditor cellEditor;
0718:
0719: protected boolean editable;
0720:
0721: protected boolean largeModel;
0722:
0723: protected int visibleRowCount;
0724:
0725: protected boolean invokesStopCellEditing;
0726:
0727: protected boolean scrollsOnExpand;
0728:
0729: protected int toggleClickCount;
0730:
0731: protected transient TreeModelListener treeModelListener = createTreeModelListener();
0732:
0733: private boolean expandsSelectedPaths;
0734:
0735: private boolean dragEnabled;
0736:
0737: private TreePath leadSelectionPath;
0738:
0739: private TreePath anchorSelectionPath;
0740:
0741: private final Map<TreePath, Object> togglePaths = new HashMap<TreePath, Object>();
0742:
0743: private static final String UI_CLASS_ID = "TreeUI";
0744:
0745: public JTree() {
0746: this (getDefaultTreeModel());
0747: }
0748:
0749: public JTree(Object[] value) {
0750: this (createTreeModel(value));
0751: setRootVisible(false);
0752: setShowsRootHandles(true);
0753: }
0754:
0755: public JTree(Vector<?> value) {
0756: this (createTreeModel(value));
0757: setRootVisible(false);
0758: setShowsRootHandles(true);
0759: }
0760:
0761: public JTree(Hashtable<?, ?> value) {
0762: this (createTreeModel(value));
0763: setRootVisible(false);
0764: setShowsRootHandles(true);
0765: }
0766:
0767: public JTree(TreeNode root) {
0768: this (new DefaultTreeModel(root));
0769: }
0770:
0771: public JTree(TreeNode root, boolean asksAllowsChildren) {
0772: this (new DefaultTreeModel(root));
0773: ((DefaultTreeModel) getModel())
0774: .setAsksAllowsChildren(asksAllowsChildren);
0775: }
0776:
0777: public JTree(TreeModel model) {
0778: setModel(model);
0779: selectionModel = new DefaultTreeSelectionModel();
0780: updateUI();
0781: }
0782:
0783: public TreeUI getUI() {
0784: return (TreeUI) ui;
0785: }
0786:
0787: public void setUI(TreeUI ui) {
0788: super .setUI(ui);
0789: }
0790:
0791: @Override
0792: public void updateUI() {
0793: setUI((TreeUI) UIManager.getUI(this ));
0794: }
0795:
0796: @Override
0797: public String getUIClassID() {
0798: return UI_CLASS_ID;
0799: }
0800:
0801: public TreeCellRenderer getCellRenderer() {
0802: return cellRenderer;
0803: }
0804:
0805: public void setCellRenderer(TreeCellRenderer renderer) {
0806: TreeCellRenderer oldValue = cellRenderer;
0807: cellRenderer = renderer;
0808: firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, renderer);
0809: }
0810:
0811: public void setEditable(boolean flag) {
0812: boolean oldValue = editable;
0813: editable = flag;
0814: firePropertyChange(EDITABLE_PROPERTY, oldValue, flag);
0815: }
0816:
0817: public boolean isEditable() {
0818: return editable;
0819: }
0820:
0821: public void setCellEditor(TreeCellEditor editor) {
0822: TreeCellEditor oldValue = cellEditor;
0823: cellEditor = editor;
0824: firePropertyChange(CELL_EDITOR_PROPERTY, oldValue, editor);
0825: }
0826:
0827: public TreeCellEditor getCellEditor() {
0828: return cellEditor;
0829: }
0830:
0831: public TreeModel getModel() {
0832: return treeModel;
0833: }
0834:
0835: public void setModel(TreeModel model) {
0836: TreeModel oldValue = treeModel;
0837: if (treeModel != null) {
0838: treeModel.removeTreeModelListener(treeModelListener);
0839: }
0840: clearToggledPaths();
0841: if (getSelectionModel() != null) {
0842: clearSelection();
0843: }
0844: treeModel = model;
0845: if (treeModel != null) {
0846: treeModel.addTreeModelListener(treeModelListener);
0847: Object root = treeModel.getRoot();
0848: if (root != null && !treeModel.isLeaf(root)) {
0849: togglePaths.put(new TreePath(root), Boolean.TRUE);
0850: }
0851: }
0852: firePropertyChange(TREE_MODEL_PROPERTY, oldValue, model);
0853: }
0854:
0855: public boolean isRootVisible() {
0856: return rootVisible;
0857: }
0858:
0859: public void setRootVisible(boolean visible) {
0860: boolean oldValue = rootVisible;
0861: rootVisible = visible;
0862: firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, visible);
0863: }
0864:
0865: public void setShowsRootHandles(boolean showHandles) {
0866: boolean oldValue = showsRootHandles;
0867: showsRootHandles = showHandles;
0868: firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue,
0869: showHandles);
0870: }
0871:
0872: public boolean getShowsRootHandles() {
0873: return showsRootHandles;
0874: }
0875:
0876: public void setRowHeight(int height) {
0877: int oldValue = rowHeight;
0878: rowHeight = height;
0879: firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, height);
0880: }
0881:
0882: public int getRowHeight() {
0883: return rowHeight;
0884: }
0885:
0886: public boolean isFixedRowHeight() {
0887: return getRowHeight() > 0;
0888: }
0889:
0890: public void setLargeModel(boolean large) {
0891: boolean oldValue = largeModel;
0892: largeModel = large;
0893: firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, large);
0894: }
0895:
0896: public boolean isLargeModel() {
0897: return largeModel;
0898: }
0899:
0900: public void setInvokesStopCellEditing(boolean invokesStop) {
0901: boolean oldValue = invokesStopCellEditing;
0902: invokesStopCellEditing = invokesStop;
0903: firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY,
0904: oldValue, invokesStop);
0905: }
0906:
0907: public boolean getInvokesStopCellEditing() {
0908: return invokesStopCellEditing;
0909: }
0910:
0911: public void setScrollsOnExpand(boolean scroll) {
0912: boolean oldValue = scrollsOnExpand;
0913: scrollsOnExpand = scroll;
0914: firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue, scroll);
0915: }
0916:
0917: public boolean getScrollsOnExpand() {
0918: return scrollsOnExpand;
0919: }
0920:
0921: public void setToggleClickCount(int clickCount) {
0922: int oldValue = toggleClickCount;
0923: toggleClickCount = clickCount;
0924: firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue,
0925: toggleClickCount);
0926: }
0927:
0928: public int getToggleClickCount() {
0929: return toggleClickCount;
0930: }
0931:
0932: public void setExpandsSelectedPaths(boolean expand) {
0933: boolean oldValue = expandsSelectedPaths;
0934: expandsSelectedPaths = expand;
0935: firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue,
0936: expand);
0937: }
0938:
0939: public boolean getExpandsSelectedPaths() {
0940: return expandsSelectedPaths;
0941: }
0942:
0943: public void setDragEnabled(boolean enabled) {
0944: if (enabled && GraphicsEnvironment.isHeadless()) {
0945: throw new HeadlessException();
0946: }
0947: dragEnabled = enabled;
0948: }
0949:
0950: public boolean getDragEnabled() {
0951: return dragEnabled;
0952: }
0953:
0954: public boolean isPathEditable(TreePath path) {
0955: return isEditable();
0956: }
0957:
0958: @Override
0959: public String getToolTipText(MouseEvent event) {
0960: if (event == null) {
0961: return null;
0962: }
0963: TreePath path = getPathForLocation(event.getX(), event.getY());
0964: if (path != null) {
0965: Object node = path.getLastPathComponent();
0966: boolean isLeaf = getModel().isLeaf(node);
0967: Component renderer = getCellRenderer()
0968: .getTreeCellRendererComponent(this , node,
0969: isPathSelected(path), isExpanded(path),
0970: isLeaf, getRowForPath(path),
0971: path.equals(getLeadSelectionPath()));
0972: if (renderer instanceof JComponent) {
0973: return ((JComponent) renderer)
0974: .getToolTipText(SwingUtilities
0975: .convertMouseEvent(this , event,
0976: renderer));
0977: }
0978: }
0979: return super .getToolTipText(event);
0980: }
0981:
0982: public String convertValueToText(Object value, boolean selected,
0983: boolean expanded, boolean leaf, int row, boolean hasFocus) {
0984: return value != null ? value.toString() : "";
0985: }
0986:
0987: public int getRowCount() {
0988: return getUI().getRowCount(this );
0989: }
0990:
0991: public void setSelectionPath(TreePath path) {
0992: getSelectionModel().setSelectionPath(path);
0993: }
0994:
0995: public void setSelectionPaths(TreePath[] paths) {
0996: getSelectionModel().setSelectionPaths(paths);
0997: }
0998:
0999: public void setLeadSelectionPath(TreePath path) {
1000: TreePath oldValue = leadSelectionPath;
1001: leadSelectionPath = path;
1002: firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path);
1003: }
1004:
1005: public void setAnchorSelectionPath(TreePath path) {
1006: TreePath oldValue = anchorSelectionPath;
1007: anchorSelectionPath = path;
1008: firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue,
1009: path);
1010: }
1011:
1012: public void setSelectionRow(int row) {
1013: setSelectionRows(new int[] { row });
1014: }
1015:
1016: public void setSelectionRows(int[] rows) {
1017: getSelectionModel().setSelectionPaths(rowsToPaths(rows));
1018: }
1019:
1020: public void addSelectionPath(TreePath path) {
1021: getSelectionModel().addSelectionPath(path);
1022: }
1023:
1024: public void addSelectionPaths(TreePath[] paths) {
1025: getSelectionModel().addSelectionPaths(paths);
1026: }
1027:
1028: public void addSelectionRow(int row) {
1029: addSelectionRows(new int[] { row });
1030: }
1031:
1032: public void addSelectionRows(int[] rows) {
1033: getSelectionModel().addSelectionPaths(rowsToPaths(rows));
1034: }
1035:
1036: public Object getLastSelectedPathComponent() {
1037: TreePath selectionPath = getSelectionPath();
1038: return selectionPath != null ? selectionPath
1039: .getLastPathComponent() : null;
1040: }
1041:
1042: public TreePath getLeadSelectionPath() {
1043: return leadSelectionPath;
1044: }
1045:
1046: public TreePath getAnchorSelectionPath() {
1047: return anchorSelectionPath;
1048: }
1049:
1050: public TreePath getSelectionPath() {
1051: return getSelectionModel().getSelectionPath();
1052: }
1053:
1054: public TreePath[] getSelectionPaths() {
1055: return getSelectionModel().getSelectionPaths();
1056: }
1057:
1058: public int[] getSelectionRows() {
1059: return getSelectionModel().getSelectionRows();
1060: }
1061:
1062: public int getSelectionCount() {
1063: return getSelectionModel().getSelectionCount();
1064: }
1065:
1066: public int getMinSelectionRow() {
1067: return getSelectionModel().getMinSelectionRow();
1068: }
1069:
1070: public int getMaxSelectionRow() {
1071: return getSelectionModel().getMaxSelectionRow();
1072: }
1073:
1074: public int getLeadSelectionRow() {
1075: return getRowForPath(getLeadSelectionPath());
1076: }
1077:
1078: public boolean isPathSelected(TreePath path) {
1079: return getSelectionModel().isPathSelected(path);
1080: }
1081:
1082: public boolean isRowSelected(int row) {
1083: return getSelectionModel().isRowSelected(row);
1084: }
1085:
1086: public Enumeration<TreePath> getExpandedDescendants(TreePath parent) {
1087: final Enumeration<TreePath> toggled = getDescendantToggledPaths(parent);
1088:
1089: if (toggled == null || !isExpanded(parent)) {
1090: return null;
1091: }
1092:
1093: return new Enumeration<TreePath>() {
1094: private TreePath nextElement = getNextExpandedPath();
1095:
1096: public TreePath nextElement() {
1097: if (nextElement == null) {
1098: throw new NoSuchElementException(Messages
1099: .getString("swing.4B")); //$NON-NLS-1$
1100: }
1101: TreePath currentValue = nextElement;
1102: nextElement = getNextExpandedPath();
1103: return currentValue;
1104: }
1105:
1106: public boolean hasMoreElements() {
1107: return nextElement != null;
1108: }
1109:
1110: private TreePath getNextExpandedPath() {
1111: while (toggled.hasMoreElements()) {
1112: TreePath nextPath = toggled.nextElement();
1113:
1114: if (isExpanded(nextPath)) {
1115: return nextPath;
1116: }
1117: }
1118: return null;
1119: }
1120: };
1121: }
1122:
1123: public boolean hasBeenExpanded(TreePath path) {
1124: return togglePaths.containsKey(path);
1125: }
1126:
1127: public boolean isExpanded(TreePath path) {
1128: Boolean state = (Boolean) togglePaths.get(path);
1129: if (state == null || !state.booleanValue()) {
1130: return false;
1131: }
1132: TreePath parentPath = path.getParentPath();
1133: return parentPath == null || isExpanded(parentPath);
1134: }
1135:
1136: public boolean isExpanded(int row) {
1137: return isExpanded(getPathForRow(row));
1138: }
1139:
1140: public boolean isCollapsed(TreePath path) {
1141: return !isExpanded(path);
1142: }
1143:
1144: public boolean isCollapsed(int row) {
1145: return !isExpanded(row);
1146: }
1147:
1148: public void makeVisible(TreePath path) {
1149: expandPath(path.getParentPath());
1150: }
1151:
1152: public boolean isVisible(TreePath path) {
1153: return getRowForPath(path) >= 0;
1154: }
1155:
1156: public Rectangle getPathBounds(TreePath path) {
1157: return getUI().getPathBounds(this , path);
1158: }
1159:
1160: public Rectangle getRowBounds(int row) {
1161: return getPathBounds(getPathForRow(row));
1162: }
1163:
1164: public void scrollPathToVisible(TreePath path) {
1165: if (path == null) {
1166: return;
1167: }
1168: makeVisible(path);
1169: Rectangle pathBounds = getPathBounds(path);
1170: if (pathBounds != null) {
1171: scrollRectToVisible(pathBounds);
1172: }
1173: }
1174:
1175: public void scrollRowToVisible(int row) {
1176: scrollPathToVisible(getPathForRow(row));
1177: }
1178:
1179: public TreePath getPathForRow(int row) {
1180: return getUI().getPathForRow(this , row);
1181: }
1182:
1183: public int getRowForPath(TreePath path) {
1184: return getUI().getRowForPath(this , path);
1185: }
1186:
1187: public void expandPath(TreePath path) {
1188: if (path == null || getModel() == null) {
1189: return;
1190: }
1191: if (getModel().isLeaf(path.getLastPathComponent())) {
1192: return;
1193: }
1194: setExpandedState(path, true);
1195: }
1196:
1197: public void expandRow(int row) {
1198: expandPath(getPathForRow(row));
1199: }
1200:
1201: public void collapsePath(TreePath path) {
1202: setExpandedState(path, false);
1203: }
1204:
1205: public void collapseRow(int row) {
1206: collapsePath(getPathForRow(row));
1207: }
1208:
1209: public TreePath getPathForLocation(int x, int y) {
1210: TreePath closestPath = getClosestPathForLocation(x, y);
1211: if (closestPath == null) {
1212: return null;
1213: }
1214: Rectangle pathBounds = getPathBounds(closestPath);
1215: return pathBounds.contains(x, y) ? closestPath : null;
1216: }
1217:
1218: public int getRowForLocation(int x, int y) {
1219: return getRowForPath(getPathForLocation(x, y));
1220: }
1221:
1222: public TreePath getClosestPathForLocation(int x, int y) {
1223: return getUI().getClosestPathForLocation(this , x, y);
1224: }
1225:
1226: public int getClosestRowForLocation(int x, int y) {
1227: return getRowForPath(getClosestPathForLocation(x, y));
1228: }
1229:
1230: public boolean isEditing() {
1231: return getUI().isEditing(this );
1232: }
1233:
1234: public boolean stopEditing() {
1235: return getUI().stopEditing(this );
1236: }
1237:
1238: public void cancelEditing() {
1239: getUI().cancelEditing(this );
1240: }
1241:
1242: public void startEditingAtPath(TreePath path) {
1243: getUI().startEditingAtPath(this , path);
1244: }
1245:
1246: public TreePath getEditingPath() {
1247: return getUI().getEditingPath(this );
1248: }
1249:
1250: public void setSelectionModel(TreeSelectionModel model) {
1251: TreeSelectionModel oldValue = selectionModel;
1252: selectionModel.removeTreeSelectionListener(selectionRedirector);
1253: selectionModel = model != null ? model : EmptySelectionModel
1254: .sharedInstance();
1255: if (selectionRedirector != null) {
1256: selectionModel
1257: .addTreeSelectionListener(selectionRedirector);
1258: }
1259: firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue,
1260: selectionModel);
1261: }
1262:
1263: public TreeSelectionModel getSelectionModel() {
1264: return selectionModel;
1265: }
1266:
1267: public void setSelectionInterval(int index0, int index1) {
1268: clearSelection();
1269: addSelectionInterval(index0, index1);
1270: }
1271:
1272: public void addSelectionInterval(int index0, int index1) {
1273: addSelectionPaths(getPathBetweenRows(index0, index1));
1274: }
1275:
1276: public void removeSelectionInterval(int index0, int index1) {
1277: removeSelectionPaths(getPathBetweenRows(index0, index1));
1278: }
1279:
1280: public void removeSelectionPath(TreePath path) {
1281: getSelectionModel().removeSelectionPath(path);
1282: }
1283:
1284: public void removeSelectionPaths(TreePath[] paths) {
1285: getSelectionModel().removeSelectionPaths(paths);
1286: }
1287:
1288: public void removeSelectionRow(int row) {
1289: removeSelectionPath(getPathForRow(row));
1290: }
1291:
1292: public void removeSelectionRows(int[] rows) {
1293: removeSelectionPaths(rowsToPaths(rows));
1294: }
1295:
1296: public void clearSelection() {
1297: getSelectionModel().clearSelection();
1298: }
1299:
1300: public boolean isSelectionEmpty() {
1301: return getSelectionModel().isSelectionEmpty();
1302: }
1303:
1304: public void addTreeExpansionListener(TreeExpansionListener l) {
1305: listenerList.add(TreeExpansionListener.class, l);
1306: }
1307:
1308: public void removeTreeExpansionListener(TreeExpansionListener l) {
1309: listenerList.remove(TreeExpansionListener.class, l);
1310: }
1311:
1312: public TreeExpansionListener[] getTreeExpansionListeners() {
1313: return listenerList.getListeners(TreeExpansionListener.class);
1314: }
1315:
1316: public void addTreeWillExpandListener(TreeWillExpandListener l) {
1317: listenerList.add(TreeWillExpandListener.class, l);
1318: }
1319:
1320: public void removeTreeWillExpandListener(TreeWillExpandListener l) {
1321: listenerList.remove(TreeWillExpandListener.class, l);
1322: }
1323:
1324: public TreeWillExpandListener[] getTreeWillExpandListeners() {
1325: return listenerList.getListeners(TreeWillExpandListener.class);
1326: }
1327:
1328: public void fireTreeExpanded(TreePath path) {
1329: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1330: if (listeners.length == 0) {
1331: return;
1332: }
1333: TreeExpansionEvent event = new TreeExpansionEvent(this , path);
1334: for (TreeExpansionListener element : listeners) {
1335: element.treeExpanded(event);
1336: }
1337: }
1338:
1339: public void fireTreeCollapsed(TreePath path) {
1340: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1341: if (listeners.length == 0) {
1342: return;
1343: }
1344: TreeExpansionEvent event = new TreeExpansionEvent(this , path);
1345: for (TreeExpansionListener element : listeners) {
1346: element.treeCollapsed(event);
1347: }
1348: }
1349:
1350: public void fireTreeWillExpand(TreePath path)
1351: throws ExpandVetoException {
1352: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1353: if (listeners.length == 0) {
1354: return;
1355: }
1356: TreeExpansionEvent event = new TreeExpansionEvent(this , path);
1357: for (TreeWillExpandListener element : listeners) {
1358: element.treeWillExpand(event);
1359: }
1360: }
1361:
1362: public void fireTreeWillCollapse(TreePath path)
1363: throws ExpandVetoException {
1364: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1365: if (listeners.length == 0) {
1366: return;
1367: }
1368: TreeExpansionEvent event = new TreeExpansionEvent(this , path);
1369: for (TreeWillExpandListener element : listeners) {
1370: element.treeWillCollapse(event);
1371: }
1372: }
1373:
1374: public void addTreeSelectionListener(TreeSelectionListener l) {
1375: if (selectionRedirector == null) {
1376: selectionRedirector = new TreeSelectionRedirector();
1377: selectionModel
1378: .addTreeSelectionListener(selectionRedirector);
1379: }
1380: listenerList.add(TreeSelectionListener.class, l);
1381: }
1382:
1383: public void removeTreeSelectionListener(TreeSelectionListener l) {
1384: listenerList.remove(TreeSelectionListener.class, l);
1385: }
1386:
1387: public TreeSelectionListener[] getTreeSelectionListeners() {
1388: return listenerList.getListeners(TreeSelectionListener.class);
1389: }
1390:
1391: protected void fireValueChanged(TreeSelectionEvent e) {
1392: TreeSelectionListener[] listeners = getTreeSelectionListeners();
1393: for (TreeSelectionListener element : listeners) {
1394: element.valueChanged(e);
1395: }
1396: }
1397:
1398: public void treeDidChange() {
1399: revalidate();
1400: repaint();
1401: }
1402:
1403: public void setVisibleRowCount(int count) {
1404: int oldValue = visibleRowCount;
1405: visibleRowCount = count;
1406: firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldValue, count);
1407: }
1408:
1409: public int getVisibleRowCount() {
1410: return visibleRowCount;
1411: }
1412:
1413: public TreePath getNextMatch(String prefix, int startingRow,
1414: Position.Bias bias) {
1415: if (prefix == null) {
1416: throw new IllegalArgumentException(Messages
1417: .getString("swing.4C")); //$NON-NLS-1$
1418: }
1419: if (startingRow < 0 || startingRow >= getRowCount()) {
1420: throw new IllegalArgumentException(Messages
1421: .getString("swing.2D")); //$NON-NLS-1$
1422: }
1423: if (bias == Position.Bias.Forward) {
1424: int rowCount = getRowCount();
1425: for (int i = startingRow; i < rowCount; i++) {
1426: TreePath path = getPathForRow(i);
1427: if (pathMatches(prefix, path, i)) {
1428: return path;
1429: }
1430: }
1431: for (int i = 0; i < startingRow; i++) {
1432: TreePath path = getPathForRow(i);
1433: if (pathMatches(prefix, path, i)) {
1434: return path;
1435: }
1436: }
1437: } else {
1438: for (int i = startingRow; i >= 0; i--) {
1439: TreePath path = getPathForRow(i);
1440: if (pathMatches(prefix, path, i)) {
1441: return path;
1442: }
1443: }
1444: for (int i = getRowCount() - 1; i > startingRow; i--) {
1445: TreePath path = getPathForRow(i);
1446: if (pathMatches(prefix, path, i)) {
1447: return path;
1448: }
1449: }
1450: }
1451: return null;
1452: }
1453:
1454: public Dimension getPreferredScrollableViewportSize() {
1455: int width = getPreferredSize().width;
1456: int height;
1457: if (isFixedRowHeight()) {
1458: height = getRowHeight();
1459: } else {
1460: Rectangle rootBounds = getModel() != null ? getPathBounds(new TreePath(
1461: getModel().getRoot()))
1462: : null;
1463: height = rootBounds != null ? rootBounds.height : 16;
1464: }
1465: return new Dimension(width, getVisibleRowCount() * height);
1466: }
1467:
1468: public int getScrollableUnitIncrement(Rectangle visibleRect,
1469: int orientation, int direction) {
1470: if (orientation == SwingConstants.HORIZONTAL) {
1471: return 4;
1472: }
1473: TreePath closestPath = getClosestPathForLocation(visibleRect.x,
1474: visibleRect.y);
1475: if (closestPath == null) {
1476: return 0;
1477: }
1478: Rectangle pathBounds = getPathBounds(closestPath);
1479: if (direction >= 0) {
1480: return pathBounds.y + pathBounds.height - visibleRect.y;
1481: }
1482: int increment = visibleRect.y - pathBounds.y;
1483: if (increment > 0) {
1484: return increment;
1485: }
1486: int row = getRowForPath(closestPath);
1487: if (row == 0) {
1488: return 0;
1489: }
1490: pathBounds = getRowBounds(row - 1);
1491: return pathBounds.height;
1492: }
1493:
1494: public int getScrollableBlockIncrement(Rectangle visibleRect,
1495: int orientation, int direction) {
1496: return orientation == SwingConstants.VERTICAL ? visibleRect.height
1497: : visibleRect.width;
1498: }
1499:
1500: public boolean getScrollableTracksViewportWidth() {
1501: Component parent = getParent();
1502: if (!(parent instanceof JViewport)) {
1503: return false;
1504: }
1505: return parent.getSize().width > getPreferredSize().width;
1506: }
1507:
1508: public boolean getScrollableTracksViewportHeight() {
1509: Component parent = getParent();
1510: if (!(parent instanceof JViewport)) {
1511: return false;
1512: }
1513: return parent.getSize().height > getPreferredSize().height;
1514: }
1515:
1516: @Override
1517: public AccessibleContext getAccessibleContext() {
1518: if (accessibleContext == null) {
1519: accessibleContext = new AccessibleJTree();
1520: }
1521: return accessibleContext;
1522: }
1523:
1524: protected void setExpandedState(TreePath path, boolean state) {
1525: doSetExpandedState(path, state);
1526: if (!state) {
1527: if (removeDescendantSelectedPaths(path, false)) {
1528: addSelectionPath(path);
1529: }
1530: }
1531: getSelectionModel().resetRowSelection();
1532: }
1533:
1534: protected Enumeration<TreePath> getDescendantToggledPaths(
1535: final TreePath parent) {
1536: if (parent == null) {
1537: return null;
1538: }
1539: final Iterator<TreePath> toggled = (new HashSet<TreePath>(
1540: togglePaths.keySet())).iterator();
1541: return new Enumeration<TreePath>() {
1542: private TreePath nextElement = getNextDescendPath();
1543:
1544: public TreePath nextElement() {
1545: if (nextElement == null) {
1546: throw new NoSuchElementException(Messages
1547: .getString("swing.4B")); //$NON-NLS-1$
1548: }
1549: TreePath currentValue = nextElement;
1550: nextElement = getNextDescendPath();
1551: return currentValue;
1552: }
1553:
1554: public boolean hasMoreElements() {
1555: return nextElement != null;
1556: }
1557:
1558: private TreePath getNextDescendPath() {
1559: while (toggled.hasNext()) {
1560: TreePath nextPath = toggled.next();
1561: if (parent.isDescendant(nextPath)) {
1562: return nextPath;
1563: }
1564: }
1565: return null;
1566: }
1567: };
1568: }
1569:
1570: protected void removeDescendantToggledPaths(
1571: Enumeration<TreePath> toRemove) {
1572: if (toRemove == null) {
1573: return;
1574: }
1575: while (toRemove.hasMoreElements()) {
1576: togglePaths.remove(toRemove.nextElement());
1577: }
1578: }
1579:
1580: protected void clearToggledPaths() {
1581: togglePaths.clear();
1582: }
1583:
1584: protected TreeModelListener createTreeModelListener() {
1585: return new TreeModelHandler();
1586: }
1587:
1588: protected boolean removeDescendantSelectedPaths(TreePath path,
1589: boolean includePath) {
1590: if (path == null) {
1591: return false;
1592: }
1593: TreePath[] selectedPaths = getSelectionPaths();
1594: if (selectedPaths == null) {
1595: return false;
1596: }
1597: List<TreePath> toRemove = new LinkedList<TreePath>();
1598: for (TreePath selectedPath : selectedPaths) {
1599: if (path.isDescendant(selectedPath)
1600: && (includePath || !path.equals(selectedPath))) {
1601: toRemove.add(selectedPath);
1602: }
1603: }
1604: if (toRemove.isEmpty()) {
1605: return false;
1606: }
1607: removeSelectionPaths(toRemove.toArray(new TreePath[toRemove
1608: .size()]));
1609: return true;
1610: }
1611:
1612: protected static TreeModel getDefaultTreeModel() {
1613: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
1614: "JTree");
1615: DefaultMutableTreeNode colorsNode = new DefaultMutableTreeNode(
1616: "towns");
1617: colorsNode.add(new DefaultMutableTreeNode("Saint-Petersburg"));
1618: colorsNode.add(new DefaultMutableTreeNode("New-York"));
1619: colorsNode.add(new DefaultMutableTreeNode("Munchen"));
1620: colorsNode.add(new DefaultMutableTreeNode("Oslo"));
1621: root.add(colorsNode);
1622: DefaultMutableTreeNode sportsNode = new DefaultMutableTreeNode(
1623: "animals");
1624: sportsNode.add(new DefaultMutableTreeNode("dog"));
1625: sportsNode.add(new DefaultMutableTreeNode("tiger"));
1626: sportsNode.add(new DefaultMutableTreeNode("wolf"));
1627: sportsNode.add(new DefaultMutableTreeNode("bear"));
1628: root.add(sportsNode);
1629: DefaultMutableTreeNode foodNode = new DefaultMutableTreeNode(
1630: "computers");
1631: foodNode.add(new DefaultMutableTreeNode("notebook"));
1632: foodNode.add(new DefaultMutableTreeNode("desktop"));
1633: foodNode.add(new DefaultMutableTreeNode("server"));
1634: foodNode.add(new DefaultMutableTreeNode("mainframe"));
1635: root.add(foodNode);
1636: return new DefaultTreeModel(root);
1637: }
1638:
1639: protected static TreeModel createTreeModel(Object value) {
1640: return new DefaultTreeModel(new DynamicUtilTreeNode("root",
1641: value));
1642: }
1643:
1644: protected TreePath[] getPathBetweenRows(int index0, int index1) {
1645: int minRow = Math.max(Math.min(index0, index1), 0);
1646: int maxRow = Math.min(Math.max(index0, index1),
1647: getRowCount() - 1);
1648: if (minRow > maxRow) {
1649: return null;
1650: }
1651: TreePath[] paths = new TreePath[maxRow - minRow + 1];
1652: for (int i = minRow; i <= maxRow; i++) {
1653: paths[i - minRow] = getPathForRow(i);
1654: }
1655: return paths;
1656: }
1657:
1658: private void doSetExpandedState(TreePath path, boolean state) {
1659: if (path == null) {
1660: return;
1661: }
1662: doSetExpandedState(path.getParentPath(), true);
1663: if (isExpanded(path) == state) {
1664: return;
1665: }
1666: try {
1667: if (state) {
1668: fireTreeWillExpand(path);
1669: } else {
1670: fireTreeWillCollapse(path);
1671: }
1672: } catch (ExpandVetoException e) {
1673: return;
1674: }
1675: togglePaths.put(path, Boolean.valueOf(state));
1676: if (state) {
1677: fireTreeExpanded(path);
1678: } else {
1679: fireTreeCollapsed(path);
1680: }
1681: }
1682:
1683: private TreePath[] rowsToPaths(int[] rows) {
1684: if (rows == null || rows.length == 0) {
1685: return new TreePath[0];
1686: }
1687: List<TreePath> paths = new ArrayList<TreePath>();
1688: for (int row : rows) {
1689: TreePath path = getPathForRow(row);
1690: if (path != null) {
1691: paths.add(path);
1692: }
1693: }
1694: return paths.toArray(new TreePath[paths.size()]);
1695: }
1696:
1697: private boolean pathMatches(String prefix, TreePath path, int row) {
1698: if (path == null) {
1699: return false;
1700: }
1701: boolean isLeaf = getModel() != null
1702: && getModel().isLeaf(path.getLastPathComponent());
1703: boolean isFocused = path.equals(getLeadSelectionPath());
1704: String value = convertValueToText(path.getLastPathComponent(),
1705: isPathSelected(path), isExpanded(path), isLeaf, row,
1706: isFocused);
1707: return value != null
1708: && value.toUpperCase().startsWith(prefix.toUpperCase());
1709: }
1710: }
|