KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > outline > TestOutlineDynamic


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.outline;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import javax.swing.BoxLayout JavaDoc;
36 import javax.swing.DefaultListSelectionModel JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JScrollPane JavaDoc;
42 import javax.swing.ListSelectionModel JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import javax.swing.event.ListSelectionEvent JavaDoc;
45 import javax.swing.event.ListSelectionListener JavaDoc;
46 import javax.swing.event.TreeModelEvent JavaDoc;
47 import javax.swing.event.TreeModelListener JavaDoc;
48 import javax.swing.table.TableModel JavaDoc;
49 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
50 import javax.swing.tree.DefaultTreeModel JavaDoc;
51 import javax.swing.tree.TreeModel JavaDoc;
52 import javax.swing.tree.TreeNode JavaDoc;
53 import javax.swing.tree.TreePath JavaDoc;
54 import org.openide.util.Utilities;
55
56 /** Another Outline test app - this one allows dynamic adding and removal of
57  * nodes and provides an editable column called "comment".
58  *
59  * @author Tim Boudreau
60  */

61 public class TestOutlineDynamic extends JFrame JavaDoc implements ActionListener JavaDoc {
62     private Outline outline;
63     private TreeModel JavaDoc treeMdl;
64     static int nodeCount = 0;
65     
66     /** Creates a new instance of Test */
67     public TestOutlineDynamic() {
68         setDefaultCloseOperation (EXIT_ON_CLOSE);
69         getContentPane().setLayout (new BorderLayout JavaDoc());
70   
71         treeMdl = createModel();
72         
73         OutlineModel mdl = DefaultOutlineModel.createOutlineModel(treeMdl,
74             new NodeRowModel(), true);
75         
76         outline = new Outline();
77         
78        // outline.setRenderDataProvider(new RenderData());
79

80         outline.setRootVisible (true);
81         
82         outline.setModel (mdl);
83         
84         JPanel JavaDoc buttons = new JPanel JavaDoc();
85         
86         JLabel JavaDoc jl = new JLabel JavaDoc("Read the button tooltips");
87         
88         buttons.setLayout(new BoxLayout JavaDoc(buttons, BoxLayout.Y_AXIS));
89         buttons.add(jl);
90         
91         final JButton JavaDoc add = new JButton JavaDoc ("Add child");
92         final JButton JavaDoc remove = new JButton JavaDoc ("Delete child");
93         final JButton JavaDoc clear = new JButton JavaDoc("Clear");
94         final JButton JavaDoc addDis = new JButton JavaDoc ("Add discontiguous");
95         final JButton JavaDoc removeDis = new JButton JavaDoc ("Delete discontiguous");
96         
97         addDis.setEnabled(false);
98         removeDis.setEnabled(false);
99         removeDis.setToolTipText("To enable, select more than one immediate child node of the same parent node");
100         addDis.setToolTipText("To enable, select a node with more than one child");
101         add.setToolTipText("Add a child to the selected node");
102         remove.setToolTipText("Delete the selected node");
103         clear.setToolTipText("Clear the model, leaving only the root node");
104         clear.setEnabled(false);
105         
106         add.addActionListener (this);
107         remove.addActionListener(this);
108         clear.addActionListener(this);
109         addDis.addActionListener(this);
110         removeDis.addActionListener(this);
111         add.setName("add");
112         remove.setName("remove");
113         clear.setName("clear");
114         addDis.setName("addDis");
115         removeDis.setName("removeDis");
116         buttons.add (add);
117         buttons.add(remove);
118         buttons.add(clear);
119         buttons.add(addDis);
120         buttons.add(removeDis);
121         
122         add.setEnabled(false);
123         remove.setEnabled(false);
124         
125         outline.getSelectionModel().addListSelectionListener(
126             new ListSelectionListener JavaDoc() {
127                 public void valueChanged(ListSelectionEvent JavaDoc e) {
128                     boolean en = outline.getSelectedRow() != -1;
129                     add.setEnabled(en);
130                     remove.setEnabled(en && outline.getSelectedRow() != 0);
131                     clear.setEnabled(outline.getRowCount() > 1);
132                     
133                     ListSelectionModel JavaDoc m =
134                         outline.getSelectionModel();
135                     //en = (m.getMinSelectionIndex() != m.getMaxSelectionIndex());
136
en = getSelectedNode() != null;
137                     if (en) {
138                         DefaultMutableTreeNode JavaDoc nd = getSelectedNode();
139                         en = nd.getChildCount() > 1;
140                     }
141                     addDis.setEnabled(en);
142                     
143                     en = getSelectedNode() != null;
144                     if (en) {
145                         int[] sels = getSelectedIndices();
146                         en = sels.length > 1;
147                         if (sels.length > outline.getRowCount()) {
148                             en = false;
149                         }
150                         if (en) {
151                             DefaultMutableTreeNode JavaDoc lastParent = null;
152                             for (int i=0; i < sels.length; i++) {
153                                 DefaultMutableTreeNode JavaDoc nd = (DefaultMutableTreeNode JavaDoc)
154                                     outline.getValueAt(sels[i], 0);
155                                 if (nd == null) {
156                                     en = false;
157                                     break;
158                                 }
159                                 if (lastParent != null) {
160                                     en &= nd.getParent() == lastParent;
161                                     if (!en) {
162                                         break;
163                                     }
164                                 } else {
165                                     lastParent = (DefaultMutableTreeNode JavaDoc) nd.getParent();
166                                 }
167                             }
168                         }
169                         
170                     }
171                     
172                     removeDis.setEnabled(en);
173                 }
174         });
175                 
176         getContentPane().add(new JScrollPane JavaDoc(outline), BorderLayout.CENTER);
177         getContentPane().add(buttons, BorderLayout.EAST);
178         
179         setBounds (20, 20, 700, 400);
180     }
181
182     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
183         JButton JavaDoc b = (JButton JavaDoc) e.getSource();
184         DefaultMutableTreeNode JavaDoc n = getSelectedNode();
185         DefaultTreeModel JavaDoc mdl = (DefaultTreeModel JavaDoc) treeMdl;
186         
187         if ("add".equals(b.getName())) {
188             Node JavaDoc newNode = new Node JavaDoc();
189             DefaultMutableTreeNode JavaDoc nd = new DefaultMutableTreeNode JavaDoc(newNode, true);
190             n.add(nd);
191             
192             mdl.nodesWereInserted(n, new int[] {n.getChildCount()-1});
193             
194 // mdl.insertNodeInto(new DefaultMutableTreeNode(newNode, true), n, n.getChildCount());
195

196         } else if ("remove".equals(b.getName())) {
197             mdl.removeNodeFromParent(n);
198         } else if ("clear".equals(b.getName())) {
199             DefaultMutableTreeNode JavaDoc root = (DefaultMutableTreeNode JavaDoc) mdl.getRoot();
200             root.removeAllChildren();
201             nodeCount = 1;
202             mdl.reload(root);
203         } else if ("addDis".equals(b.getName())) {
204             DefaultMutableTreeNode JavaDoc nd = getSelectedNode();
205             int ch = nd.getChildCount();
206             
207             DefaultMutableTreeNode JavaDoc atStart = new DefaultMutableTreeNode JavaDoc(new Node JavaDoc(), true);
208             DefaultMutableTreeNode JavaDoc atEnd = new DefaultMutableTreeNode JavaDoc(new Node JavaDoc(), true);
209             
210             nd.insert(atEnd, ch);
211             nd.insert(atStart, 0);
212             
213             mdl.nodesWereInserted(nd, new int[] {0, nd.getChildCount()-1});
214             
215             
216         } else if ("removeDis".equals(b.getName())) {
217             int[] sels = getSelectedIndices();
218             
219             
220             //they all have the same parent if the button is enabled
221
DefaultMutableTreeNode JavaDoc aNode = (DefaultMutableTreeNode JavaDoc)
222                 outline.getValueAt(sels[0], 0);
223             
224             DefaultMutableTreeNode JavaDoc parent = (DefaultMutableTreeNode JavaDoc) aNode.getParent();
225             
226             //reverse sort the selections, so we remove nodes from bottom to top
227
for (int i=0; i < sels.length; i++) {
228                 sels[i] *= -1;
229             }
230             Arrays.sort(sels);
231             for (int i=0; i < sels.length; i++) {
232                 sels[i] *= -1;
233             }
234
235             System.err.println("Going to remove " + Arrays.asList(Utilities.toObjectArray(sels)));
236             
237             ArrayList JavaDoc nodes = new ArrayList JavaDoc();
238             int[] indices = new int[sels.length];
239             //Build the list of nodes we'll play with before we start
240
//modifying the model - we can't do it while we're going
241
for (int i=0; i < sels.length; i++) {
242                 aNode = (DefaultMutableTreeNode JavaDoc) outline.getValueAt(sels[i], 0);
243                 System.err.println("To delete user object class " + aNode.getUserObject().getClass() + " = " + aNode.getUserObject());
244                 nodes.add (aNode);
245                 indices[i] = parent.getIndex(aNode);
246             }
247             
248             System.err.println("Will really remove indices " + Arrays.asList(Utilities.toObjectArray(indices)));
249             
250             for (int i=0; i < nodes.size(); i++) {
251                 aNode = (DefaultMutableTreeNode JavaDoc) nodes.get(i);
252                 if (aNode.getParent() != parent) {
253                     System.err.println(aNode + " not child of " + parent + " but of " + aNode.getParent());
254                 } else {
255                     System.err.println("REMOVING " + aNode + " from parent");
256                     parent.remove(aNode);
257                     nodes.add(aNode);
258                 }
259             }
260             
261             mdl.nodesWereRemoved(parent, indices, nodes.toArray());
262             
263         }
264     }
265     
266     private int[] getSelectedIndices() {
267         ListSelectionModel JavaDoc lsm = outline.getSelectionModel();
268         int min = lsm.getMinSelectionIndex();
269         int max = lsm.getMaxSelectionIndex();
270         if (min == max) {
271             return new int[] {min};
272         }
273         ArrayList JavaDoc al = new ArrayList JavaDoc();
274         for (int i=min; i <= max; i++) {
275             if (lsm.isSelectedIndex(i)) {
276                 al.add (new Integer JavaDoc(i));
277             }
278         }
279         Integer JavaDoc[] ints = (Integer JavaDoc[]) al.toArray(new Integer JavaDoc[0]);
280         return (int[]) Utilities.toPrimitiveArray(ints);
281     }
282     
283     public DefaultMutableTreeNode JavaDoc getSelectedNode() {
284         return ((DefaultMutableTreeNode JavaDoc) outline.getValueAt(
285             outline.getSelectedRow(), 0));
286     }
287     
288     /** A handy method to create a model to install into a JTree to compare
289      * behavior of a real JTree's layout cache and ours */

290     public static TreeModel JavaDoc createModel() {
291         DefaultMutableTreeNode JavaDoc root = new DefaultMutableTreeNode JavaDoc(new Node JavaDoc());
292         TreeModel JavaDoc treeMdl = new DefaultTreeModel JavaDoc (root, false);
293         return treeMdl;
294     }
295     
296     public static void main(String JavaDoc[] ignored) {
297         try {
298            //UIManager.setLookAndFeel (new javax.swing.plaf.metal.MetalLookAndFeel());
299
} catch (Exception JavaDoc e) {}
300         
301         new TestOutlineDynamic().show();
302     }
303     
304     
305     private class NodeRowModel implements RowModel {
306         
307         public Class JavaDoc getColumnClass(int column) {
308             switch (column) {
309                 case 0 : return Integer JavaDoc.class;
310                 case 1 : return String JavaDoc.class;
311                 default : assert false;
312             }
313             return null;
314         }
315         
316         public int getColumnCount() {
317             return 2;
318         }
319         
320         public String JavaDoc getColumnName(int column) {
321             return column == 0 ? "Hash code" : "Comment";
322         }
323         
324         public Object JavaDoc getValueFor(Object JavaDoc node, int column) {
325             Node JavaDoc n = (Node JavaDoc) ((DefaultMutableTreeNode JavaDoc) node).getUserObject();
326             switch (column) {
327                 case 0 : return new Integer JavaDoc(node.hashCode());
328                 case 1 : return n.getComment();
329                 default : assert false;
330             }
331             return null;
332         }
333         
334         public boolean isCellEditable(Object JavaDoc node, int column) {
335             return column == 1;
336         }
337         
338         public void setValueFor(Object JavaDoc node, int column, Object JavaDoc value) {
339             if (column == 1) {
340                 ((Node JavaDoc) ((DefaultMutableTreeNode JavaDoc) node).getUserObject())
341                     .setComment(value.toString());
342             }
343         }
344     }
345     
346     private static class Node {
347         int idx;
348         private String JavaDoc comment = "no comment";
349         public Node() {
350             idx = nodeCount++;
351         }
352         
353         public String JavaDoc getComment() {
354             return comment;
355         }
356         
357         public void setComment(String JavaDoc s) {
358             comment = s;
359         }
360         
361         public String JavaDoc toString() {
362             return "Node " + idx;
363         }
364     }
365 }
366
Popular Tags