1 19 20 package org.netbeans.swing.outline; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.FlowLayout ; 25 import java.awt.event.ActionListener ; 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Date ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import javax.swing.BoxLayout ; 36 import javax.swing.DefaultListSelectionModel ; 37 import javax.swing.JButton ; 38 import javax.swing.JFrame ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JScrollPane ; 42 import javax.swing.ListSelectionModel ; 43 import javax.swing.UIManager ; 44 import javax.swing.event.ListSelectionEvent ; 45 import javax.swing.event.ListSelectionListener ; 46 import javax.swing.event.TreeModelEvent ; 47 import javax.swing.event.TreeModelListener ; 48 import javax.swing.table.TableModel ; 49 import javax.swing.tree.DefaultMutableTreeNode ; 50 import javax.swing.tree.DefaultTreeModel ; 51 import javax.swing.tree.TreeModel ; 52 import javax.swing.tree.TreeNode ; 53 import javax.swing.tree.TreePath ; 54 import org.openide.util.Utilities; 55 56 61 public class TestOutlineDynamic extends JFrame implements ActionListener { 62 private Outline outline; 63 private TreeModel treeMdl; 64 static int nodeCount = 0; 65 66 67 public TestOutlineDynamic() { 68 setDefaultCloseOperation (EXIT_ON_CLOSE); 69 getContentPane().setLayout (new BorderLayout ()); 70 71 treeMdl = createModel(); 72 73 OutlineModel mdl = DefaultOutlineModel.createOutlineModel(treeMdl, 74 new NodeRowModel(), true); 75 76 outline = new Outline(); 77 78 80 outline.setRootVisible (true); 81 82 outline.setModel (mdl); 83 84 JPanel buttons = new JPanel (); 85 86 JLabel jl = new JLabel ("Read the button tooltips"); 87 88 buttons.setLayout(new BoxLayout (buttons, BoxLayout.Y_AXIS)); 89 buttons.add(jl); 90 91 final JButton add = new JButton ("Add child"); 92 final JButton remove = new JButton ("Delete child"); 93 final JButton clear = new JButton ("Clear"); 94 final JButton addDis = new JButton ("Add discontiguous"); 95 final JButton removeDis = new JButton ("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 () { 127 public void valueChanged(ListSelectionEvent 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 m = 134 outline.getSelectionModel(); 135 en = getSelectedNode() != null; 137 if (en) { 138 DefaultMutableTreeNode 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 lastParent = null; 152 for (int i=0; i < sels.length; i++) { 153 DefaultMutableTreeNode nd = (DefaultMutableTreeNode ) 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 ) nd.getParent(); 166 } 167 } 168 } 169 170 } 171 172 removeDis.setEnabled(en); 173 } 174 }); 175 176 getContentPane().add(new JScrollPane (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 e) { 183 JButton b = (JButton ) e.getSource(); 184 DefaultMutableTreeNode n = getSelectedNode(); 185 DefaultTreeModel mdl = (DefaultTreeModel ) treeMdl; 186 187 if ("add".equals(b.getName())) { 188 Node newNode = new Node (); 189 DefaultMutableTreeNode nd = new DefaultMutableTreeNode (newNode, true); 190 n.add(nd); 191 192 mdl.nodesWereInserted(n, new int[] {n.getChildCount()-1}); 193 194 196 } else if ("remove".equals(b.getName())) { 197 mdl.removeNodeFromParent(n); 198 } else if ("clear".equals(b.getName())) { 199 DefaultMutableTreeNode root = (DefaultMutableTreeNode ) mdl.getRoot(); 200 root.removeAllChildren(); 201 nodeCount = 1; 202 mdl.reload(root); 203 } else if ("addDis".equals(b.getName())) { 204 DefaultMutableTreeNode nd = getSelectedNode(); 205 int ch = nd.getChildCount(); 206 207 DefaultMutableTreeNode atStart = new DefaultMutableTreeNode (new Node (), true); 208 DefaultMutableTreeNode atEnd = new DefaultMutableTreeNode (new Node (), 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 DefaultMutableTreeNode aNode = (DefaultMutableTreeNode ) 222 outline.getValueAt(sels[0], 0); 223 224 DefaultMutableTreeNode parent = (DefaultMutableTreeNode ) aNode.getParent(); 225 226 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 nodes = new ArrayList (); 238 int[] indices = new int[sels.length]; 239 for (int i=0; i < sels.length; i++) { 242 aNode = (DefaultMutableTreeNode ) 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 ) 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 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 al = new ArrayList (); 274 for (int i=min; i <= max; i++) { 275 if (lsm.isSelectedIndex(i)) { 276 al.add (new Integer (i)); 277 } 278 } 279 Integer [] ints = (Integer []) al.toArray(new Integer [0]); 280 return (int[]) Utilities.toPrimitiveArray(ints); 281 } 282 283 public DefaultMutableTreeNode getSelectedNode() { 284 return ((DefaultMutableTreeNode ) outline.getValueAt( 285 outline.getSelectedRow(), 0)); 286 } 287 288 290 public static TreeModel createModel() { 291 DefaultMutableTreeNode root = new DefaultMutableTreeNode (new Node ()); 292 TreeModel treeMdl = new DefaultTreeModel (root, false); 293 return treeMdl; 294 } 295 296 public static void main(String [] ignored) { 297 try { 298 } catch (Exception e) {} 300 301 new TestOutlineDynamic().show(); 302 } 303 304 305 private class NodeRowModel implements RowModel { 306 307 public Class getColumnClass(int column) { 308 switch (column) { 309 case 0 : return Integer .class; 310 case 1 : return String .class; 311 default : assert false; 312 } 313 return null; 314 } 315 316 public int getColumnCount() { 317 return 2; 318 } 319 320 public String getColumnName(int column) { 321 return column == 0 ? "Hash code" : "Comment"; 322 } 323 324 public Object getValueFor(Object node, int column) { 325 Node n = (Node ) ((DefaultMutableTreeNode ) node).getUserObject(); 326 switch (column) { 327 case 0 : return new Integer (node.hashCode()); 328 case 1 : return n.getComment(); 329 default : assert false; 330 } 331 return null; 332 } 333 334 public boolean isCellEditable(Object node, int column) { 335 return column == 1; 336 } 337 338 public void setValueFor(Object node, int column, Object value) { 339 if (column == 1) { 340 ((Node ) ((DefaultMutableTreeNode ) node).getUserObject()) 341 .setComment(value.toString()); 342 } 343 } 344 } 345 346 private static class Node { 347 int idx; 348 private String comment = "no comment"; 349 public Node() { 350 idx = nodeCount++; 351 } 352 353 public String getComment() { 354 return comment; 355 } 356 357 public void setComment(String s) { 358 comment = s; 359 } 360 361 public String toString() { 362 return "Node " + idx; 363 } 364 } 365 } 366 | Popular Tags |