jdk/src/share/demo/jfc/SampleTree/SampleTree.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.BorderLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.FlowLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.event.ActionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * A demo for illustrating how to do different things with JTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  * The data that this displays is rather boring, that is each node will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * have 7 children that have random names based on the fonts.  Each node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * is then drawn with that font and in a different color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  * While the data isn't interesting the example illustrates a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  * of things:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  * For an example of dynamicaly loading children refer to DynamicTreeNode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  * For an example of adding/removing/inserting/reloading refer to the inner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  *     classes of this class, AddAction, RemovAction, InsertAction and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  *     ReloadAction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  * For an example of creating your own cell renderer refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  *     SampleTreeCellRenderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  * For an example of subclassing JTreeModel for editing refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  *     SampleTreeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class SampleTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Window for showing Tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected JFrame            frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** Tree used for the example. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected JTree             tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** Tree model. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected DefaultTreeModel        treeModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
      * Constructs a new instance of SampleTree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public SampleTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // Force SampleTree to come up in the Cross Platform L&F
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // If you want the System L&F instead, comment out the above line and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // uncomment the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.err.println("Error loading L&F: " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        JMenuBar         menuBar = constructMenuBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        JPanel           panel = new JPanel(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        frame = new JFrame("SampleTree");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        frame.getContentPane().add("Center", panel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        frame.setJMenuBar(menuBar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        frame.setBackground(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        /* Create the JTreeModel. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        DefaultMutableTreeNode root = createNewNode("Root");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        treeModel = new SampleTreeModel(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /* Create the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        tree = new JTree(treeModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* Enable tool tips for the tree, without this tool tips will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
           be picked up. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ToolTipManager.sharedInstance().registerComponent(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /* Make the tree use an instance of SampleTreeCellRenderer for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
           drawing. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        tree.setCellRenderer(new SampleTreeCellRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /* Make tree ask for the height of each row. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        tree.setRowHeight(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* Put the Tree in a scroller. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        JScrollPane        sp = new JScrollPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        sp.setPreferredSize(new Dimension(300, 300));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        sp.getViewport().add(tree);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /* And show it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        panel.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        panel.add("Center", sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        panel.add("South", constructOptionsPanel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        frame.addWindowListener( new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            public void windowClosing(WindowEvent e) {System.exit(0);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        frame.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        frame.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** Constructs a JPanel containing check boxes for the different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
      * options that tree supports. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private JPanel constructOptionsPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        JCheckBox               aCheckbox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        JPanel           retPanel = new JPanel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        JPanel           borderPane = new JPanel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        borderPane.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        retPanel.setLayout(new FlowLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        aCheckbox = new JCheckBox("show top level handles");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        aCheckbox.setSelected(tree.getShowsRootHandles());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        aCheckbox.addChangeListener(new ShowHandlesChangeListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        retPanel.add(aCheckbox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        aCheckbox = new JCheckBox("show root");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        aCheckbox.setSelected(tree.isRootVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        aCheckbox.addChangeListener(new ShowRootChangeListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        retPanel.add(aCheckbox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        aCheckbox = new JCheckBox("editable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        aCheckbox.setSelected(tree.isEditable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        aCheckbox.addChangeListener(new TreeEditableChangeListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        aCheckbox.setToolTipText("Triple click to edit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        retPanel.add(aCheckbox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        borderPane.add(retPanel, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        /* Create a set of radio buttons that dictate what selection should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
           be allowed in the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ButtonGroup           group = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        JPanel         buttonPane = new JPanel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        JRadioButton          button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        buttonPane.setLayout(new FlowLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        buttonPane.setBorder(new TitledBorder("Selection Mode"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        button = new JRadioButton("Single");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        button.addActionListener(new AbstractAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            public boolean isEnabled() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                tree.getSelectionModel().setSelectionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    (TreeSelectionModel.SINGLE_TREE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        group.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        buttonPane.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        button = new JRadioButton("Contiguous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        button.addActionListener(new AbstractAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            public boolean isEnabled() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                tree.getSelectionModel().setSelectionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    (TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        group.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        buttonPane.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        button = new JRadioButton("Discontiguous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        button.addActionListener(new AbstractAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            public boolean isEnabled() { return true; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                tree.getSelectionModel().setSelectionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        button.setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        group.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        buttonPane.add(button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        borderPane.add(buttonPane, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // NOTE: This will be enabled in a future release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // Create a label and combobox to determine how many clicks are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // needed to expand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        JPanel               clickPanel = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Object[]             values = { "Never", new Integer(1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                        new Integer(2), new Integer(3) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        final JComboBox      clickCBox = new JComboBox(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        clickPanel.setLayout(new FlowLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        clickPanel.add(new JLabel("Click count to expand:"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        clickCBox.setSelectedIndex(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        clickCBox.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                Object       selItem = clickCBox.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if(selItem instanceof Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    tree.setToggleClickCount(((Integer)selItem).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                else // Don't toggle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    tree.setToggleClickCount(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        clickPanel.add(clickCBox);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        borderPane.add(clickPanel, BorderLayout.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return borderPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /** Construct a menu. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private JMenuBar constructMenuBar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        JMenu            menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        JMenuBar         menuBar = new JMenuBar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        JMenuItem        menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        /* Good ol exit. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        menu = new JMenu("File");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        menuBar.add(menu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        menuItem = menu.add(new JMenuItem("Exit"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        menuItem.addActionListener(new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        /* Tree related stuff. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        menu = new JMenu("Tree");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        menuBar.add(menu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        menuItem = menu.add(new JMenuItem("Add"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        menuItem.addActionListener(new AddAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        menuItem = menu.add(new JMenuItem("Insert"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        menuItem.addActionListener(new InsertAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        menuItem = menu.add(new JMenuItem("Reload"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        menuItem.addActionListener(new ReloadAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        menuItem = menu.add(new JMenuItem("Remove"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        menuItem.addActionListener(new RemoveAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return menuBar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
      * Returns the TreeNode instance that is selected in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      * If nothing is selected, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected DefaultMutableTreeNode getSelectedNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        TreePath   selPath = tree.getSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if(selPath != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return (DefaultMutableTreeNode)selPath.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns the selected TreePaths in the tree, may return null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * nothing is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    protected TreePath[] getSelectedPaths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return tree.getSelectionPaths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    protected DefaultMutableTreeNode createNewNode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return new DynamicTreeNode(new SampleData(null, Color.black, name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
      * AddAction is used to add a new item after the selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    class AddAction extends Object implements ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /** Number of nodes that have been added. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public int               addCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
          * Messaged when the user clicks on the Add menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
          * Determines the selection from the Tree and adds an item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
          * after that.  If nothing is selected, an item is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
          * the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            DefaultMutableTreeNode          lastItem = getSelectedNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            DefaultMutableTreeNode          parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            /* Determine where to create the new node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if(lastItem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                parent = (DefaultMutableTreeNode)lastItem.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if(parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    parent = (DefaultMutableTreeNode)treeModel.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    lastItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                parent = (DefaultMutableTreeNode)treeModel.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // new root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                treeModel.setRoot(createNewNode("Added " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                                Integer.toString(addCount++)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                int               newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if(lastItem == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    newIndex = treeModel.getChildCount(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    newIndex = parent.getIndex(lastItem) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                /* Let the treemodel know. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                treeModel.insertNodeInto(createNewNode("Added " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                    Integer.toString(addCount++)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                    parent, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    } // End of SampleTree.AddAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
      * InsertAction is used to insert a new item before the selected item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    class InsertAction extends Object implements ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        /** Number of nodes that have been added. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        public int               insertCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
          * Messaged when the user clicks on the Insert menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
          * Determines the selection from the Tree and inserts an item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
          * after that.  If nothing is selected, an item is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
          * the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            DefaultMutableTreeNode          lastItem = getSelectedNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            DefaultMutableTreeNode          parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            /* Determine where to create the new node. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if(lastItem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                parent = (DefaultMutableTreeNode)lastItem.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if(parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    parent = (DefaultMutableTreeNode)treeModel.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    lastItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                parent = (DefaultMutableTreeNode)treeModel.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                // new root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                treeModel.setRoot(createNewNode("Inserted " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                             Integer.toString(insertCount++)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                int               newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if(lastItem == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    newIndex = treeModel.getChildCount(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    newIndex = parent.getIndex(lastItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                /* Let the treemodel know. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                treeModel.insertNodeInto(createNewNode("Inserted " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                         Integer.toString(insertCount++)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                         parent, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    } // End of SampleTree.InsertAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
      * ReloadAction is used to reload from the selected node.  If nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
      * is selected, reload is not issued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    class ReloadAction extends Object implements ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
          * Messaged when the user clicks on the Reload menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
          * Determines the selection from the Tree and asks the treemodel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
          * to reload from that node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            DefaultMutableTreeNode          lastItem = getSelectedNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if(lastItem != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                treeModel.reload(lastItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    } // End of SampleTree.ReloadAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
      * RemoveAction removes the selected node from the tree.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
      * The root or nothing is selected nothing is removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    class RemoveAction extends Object implements ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
          * Removes the selected item as long as it isn't root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            TreePath[] selected = getSelectedPaths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (selected != null && selected.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                TreePath shallowest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // The remove process consists of the following steps:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // 1 - find the shallowest selected TreePath, the shallowest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                //     path is the path with the smallest number of path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                //     components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                // 2 - Find the siblings of this TreePath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                // 3 - Remove from selected the TreePaths that are descendants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                //     of the paths that are going to be removed. They will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                //     be removed as a result of their ancestors being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                //     removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                // 4 - continue until selected contains only null paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                while ((shallowest = findShallowestPath(selected)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    removeSiblings(shallowest, selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * Removes the sibling TreePaths of <code>path</code>, that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * located in <code>paths</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        private void removeSiblings(TreePath path, TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // Find the siblings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (path.getPathCount() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // Special case, set the root to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                for (int counter = paths.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    paths[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                treeModel.setRoot(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                // Find the siblings of path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                TreePath parent = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                MutableTreeNode parentNode = (MutableTreeNode)parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                ArrayList toRemove = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                int depth = parent.getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                // First pass, find paths with a parent TreePath of parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                for (int counter = paths.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    if (paths[counter] != null && paths[counter].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                              getParentPath().equals(parent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        toRemove.add(paths[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        paths[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                // Second pass, remove any paths that are descendants of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                // paths that are going to be removed. These paths are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                // implicitly removed as a result of removing the paths in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                // toRemove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                int rCount = toRemove.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                for (int counter = paths.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    if (paths[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        for (int rCounter = rCount - 1; rCounter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                             rCounter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                            if (((TreePath)toRemove.get(rCounter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                           isDescendant(paths[counter])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                paths[counter] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                // Sort the siblings based on position in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (rCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    Collections.sort(toRemove, new PositionComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                int[] indices = new int[rCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                Object[] removedNodes = new Object[rCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                for (int counter = rCount - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    removedNodes[counter] = ((TreePath)toRemove.get(counter)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    indices[counter] = treeModel.getIndexOfChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                        (parentNode, removedNodes[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    parentNode.remove(indices[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                treeModel.nodesWereRemoved(parentNode, indices, removedNodes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * Returns the TreePath with the smallest path count in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * <code>paths</code>. Will return null if there is no non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * TreePath is <code>paths</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        private TreePath findShallowestPath(TreePath[] paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            int shallowest = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            TreePath shallowestPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            for (int counter = paths.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                if (paths[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    if (shallowest != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        if (paths[counter].getPathCount() < shallowest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                            shallowest = paths[counter].getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            shallowestPath = paths[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            if (shallowest == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                return shallowestPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        shallowestPath = paths[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        shallowest = paths[counter].getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            return shallowestPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * An Comparator that bases the return value on the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         * passed in objects in the TreeModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * This is actually rather expensive, it would be more efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * to extract the indices and then do the comparision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        private class PositionComparator implements Comparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                TreePath p1 = (TreePath)o1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                int o1Index = treeModel.getIndexOfChild(p1.getParentPath().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                          getLastPathComponent(), p1.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                TreePath p2 = (TreePath)o2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                int o2Index = treeModel.getIndexOfChild(p2.getParentPath().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                          getLastPathComponent(), p2.getLastPathComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                return o1Index - o2Index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                return super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    } // End of SampleTree.RemoveAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
      * ShowHandlesChangeListener implements the ChangeListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
      * to toggle the state of showing the handles in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    class ShowHandlesChangeListener extends Object implements ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            tree.setShowsRootHandles(((JCheckBox)e.getSource()).isSelected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    } // End of class SampleTree.ShowHandlesChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
      * ShowRootChangeListener implements the ChangeListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
      * to toggle the state of showing the root node in the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    class ShowRootChangeListener extends Object implements ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            tree.setRootVisible(((JCheckBox)e.getSource()).isSelected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    } // End of class SampleTree.ShowRootChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
      * TreeEditableChangeListener implements the ChangeListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
      * to toggle between allowing editing and now allowing editing in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
      * the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    class TreeEditableChangeListener extends Object implements ChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            tree.setEditable(((JCheckBox)e.getSource()).isSelected());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    } // End of class SampleTree.TreeEditableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    static public void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        new SampleTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
}