1 16 package org.apache.log4j.lf5.viewer.categoryexplorer; 17 18 import javax.swing.*; 19 import javax.swing.tree.TreePath ; 20 import java.awt.*; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.MouseAdapter ; 24 import java.awt.event.MouseEvent ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 28 34 35 37 public class CategoryNodeEditor extends CategoryAbstractCellEditor { 38 42 protected CategoryNodeEditorRenderer _renderer; 46 protected CategoryNode _lastEditedNode; 47 protected JCheckBox _checkBox; 48 protected CategoryExplorerModel _categoryModel; 49 protected JTree _tree; 50 51 55 59 public CategoryNodeEditor(CategoryExplorerModel model) { 60 _renderer = new CategoryNodeEditorRenderer(); 61 _checkBox = _renderer.getCheckBox(); 62 _categoryModel = model; 63 64 _checkBox.addActionListener(new ActionListener () { 65 public void actionPerformed(ActionEvent e) { 66 _categoryModel.update(_lastEditedNode, _checkBox.isSelected()); 67 stopCellEditing(); 68 } 69 }); 70 71 _renderer.addMouseListener(new MouseAdapter () { 72 public void mousePressed(MouseEvent e) { 73 if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) { 74 showPopup(_lastEditedNode, e.getX(), e.getY()); 75 } 76 stopCellEditing(); 77 } 78 }); 79 } 80 81 85 public Component getTreeCellEditorComponent(JTree tree, Object value, 86 boolean selected, boolean expanded, 87 boolean leaf, int row) { 88 _lastEditedNode = (CategoryNode) value; 89 _tree = tree; 90 91 return _renderer.getTreeCellRendererComponent(tree, 92 value, selected, expanded, 93 leaf, row, true); 94 } 96 97 public Object getCellEditorValue() { 98 return _lastEditedNode.getUserObject(); 99 } 100 104 protected JMenuItem createPropertiesMenuItem(final CategoryNode node) { 105 JMenuItem result = new JMenuItem("Properties"); 106 result.addActionListener(new ActionListener () { 107 public void actionPerformed(ActionEvent e) { 108 showPropertiesDialog(node); 109 } 110 }); 111 return result; 112 } 113 114 protected void showPropertiesDialog(CategoryNode node) { 115 JOptionPane.showMessageDialog( 116 _tree, 117 getDisplayedProperties(node), 118 "Category Properties: " + node.getTitle(), 119 JOptionPane.PLAIN_MESSAGE 120 ); 121 } 122 123 protected Object getDisplayedProperties(CategoryNode node) { 124 ArrayList result = new ArrayList (); 125 result.add("Category: " + node.getTitle()); 126 if (node.hasFatalRecords()) { 127 result.add("Contains at least one fatal LogRecord."); 128 } 129 if (node.hasFatalChildren()) { 130 result.add("Contains descendants with a fatal LogRecord."); 131 } 132 result.add("LogRecords in this category alone: " + 133 node.getNumberOfContainedRecords()); 134 result.add("LogRecords in descendant categories: " + 135 node.getNumberOfRecordsFromChildren()); 136 result.add("LogRecords in this category including descendants: " + 137 node.getTotalNumberOfRecords()); 138 return result.toArray(); 139 } 140 141 protected void showPopup(CategoryNode node, int x, int y) { 142 JPopupMenu popup = new JPopupMenu(); 143 popup.setSize(150, 400); 144 if (node.getParent() == null) { 148 popup.add(createRemoveMenuItem()); 149 popup.addSeparator(); 150 } 151 popup.add(createSelectDescendantsMenuItem(node)); 152 popup.add(createUnselectDescendantsMenuItem(node)); 153 popup.addSeparator(); 154 popup.add(createExpandMenuItem(node)); 155 popup.add(createCollapseMenuItem(node)); 156 popup.addSeparator(); 157 popup.add(createPropertiesMenuItem(node)); 158 popup.show(_renderer, x, y); 159 } 160 161 protected JMenuItem createSelectDescendantsMenuItem(final CategoryNode node) { 162 JMenuItem selectDescendants = 163 new JMenuItem("Select All Descendant Categories"); 164 selectDescendants.addActionListener( 165 new ActionListener () { 166 public void actionPerformed(ActionEvent e) { 167 _categoryModel.setDescendantSelection(node, true); 168 } 169 } 170 ); 171 return selectDescendants; 172 } 173 174 protected JMenuItem createUnselectDescendantsMenuItem(final CategoryNode node) { 175 JMenuItem unselectDescendants = 176 new JMenuItem("Deselect All Descendant Categories"); 177 unselectDescendants.addActionListener( 178 179 new ActionListener () { 180 public void actionPerformed(ActionEvent e) { 181 _categoryModel.setDescendantSelection(node, false); 182 } 183 } 184 185 ); 186 return unselectDescendants; 187 } 188 189 protected JMenuItem createExpandMenuItem(final CategoryNode node) { 190 JMenuItem result = new JMenuItem("Expand All Descendant Categories"); 191 result.addActionListener(new ActionListener () { 192 public void actionPerformed(ActionEvent e) { 193 expandDescendants(node); 194 } 195 }); 196 return result; 197 } 198 199 protected JMenuItem createCollapseMenuItem(final CategoryNode node) { 200 JMenuItem result = new JMenuItem("Collapse All Descendant Categories"); 201 result.addActionListener(new ActionListener () { 202 public void actionPerformed(ActionEvent e) { 203 collapseDescendants(node); 204 } 205 }); 206 return result; 207 } 208 209 218 protected JMenuItem createRemoveMenuItem() { 219 JMenuItem result = new JMenuItem("Remove All Empty Categories"); 220 result.addActionListener(new ActionListener () { 221 public void actionPerformed(ActionEvent e) { 222 while (removeUnusedNodes() > 0) ; 223 } 224 }); 225 return result; 226 } 227 228 protected void expandDescendants(CategoryNode node) { 229 Enumeration descendants = node.depthFirstEnumeration(); 230 CategoryNode current; 231 while (descendants.hasMoreElements()) { 232 current = (CategoryNode) descendants.nextElement(); 233 expand(current); 234 } 235 } 236 237 protected void collapseDescendants(CategoryNode node) { 238 Enumeration descendants = node.depthFirstEnumeration(); 239 CategoryNode current; 240 while (descendants.hasMoreElements()) { 241 current = (CategoryNode) descendants.nextElement(); 242 collapse(current); 243 } 244 } 245 246 249 protected int removeUnusedNodes() { 250 int count = 0; 251 CategoryNode root = _categoryModel.getRootCategoryNode(); 252 Enumeration enumeration = root.depthFirstEnumeration(); 253 while (enumeration.hasMoreElements()) { 254 CategoryNode node = (CategoryNode) enumeration.nextElement(); 255 if (node.isLeaf() && node.getNumberOfContainedRecords() == 0 256 && node.getParent() != null) { 257 _categoryModel.removeNodeFromParent(node); 258 count++; 259 } 260 } 261 262 return count; 263 } 264 265 protected void expand(CategoryNode node) { 266 _tree.expandPath(getTreePath(node)); 267 } 268 269 protected TreePath getTreePath(CategoryNode node) { 270 return new TreePath (node.getPath()); 271 } 272 273 protected void collapse(CategoryNode node) { 274 _tree.collapsePath(getTreePath(node)); 275 } 276 277 281 285 } 286 | Popular Tags |