KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > viewer > categoryexplorer > CategoryNodeEditor


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5.viewer.categoryexplorer;
17
18 import javax.swing.*;
19 import javax.swing.tree.TreePath JavaDoc;
20 import java.awt.*;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 /**
29  * CategoryNodeEditor
30  *
31  * @author Michael J. Sikorsky
32  * @author Robert Shaw
33  */

34
35 // Contributed by ThoughtWorks Inc.
36

37 public class CategoryNodeEditor extends CategoryAbstractCellEditor {
38   //--------------------------------------------------------------------------
39
// Constants:
40
//--------------------------------------------------------------------------
41

42   //--------------------------------------------------------------------------
43
// Protected Variables:
44
//--------------------------------------------------------------------------
45
protected CategoryNodeEditorRenderer _renderer;
46   protected CategoryNode _lastEditedNode;
47   protected JCheckBox _checkBox;
48   protected CategoryExplorerModel _categoryModel;
49   protected JTree _tree;
50
51   //--------------------------------------------------------------------------
52
// Private Variables:
53
//--------------------------------------------------------------------------
54

55   //--------------------------------------------------------------------------
56
// Constructors:
57
//--------------------------------------------------------------------------
58

59   public CategoryNodeEditor(CategoryExplorerModel model) {
60     _renderer = new CategoryNodeEditorRenderer();
61     _checkBox = _renderer.getCheckBox();
62     _categoryModel = model;
63
64     _checkBox.addActionListener(new ActionListener JavaDoc() {
65       public void actionPerformed(ActionEvent JavaDoc e) {
66         _categoryModel.update(_lastEditedNode, _checkBox.isSelected());
67         stopCellEditing();
68       }
69     });
70
71     _renderer.addMouseListener(new MouseAdapter JavaDoc() {
72       public void mousePressed(MouseEvent JavaDoc e) {
73         if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
74           showPopup(_lastEditedNode, e.getX(), e.getY());
75         }
76         stopCellEditing();
77       }
78     });
79   }
80
81   //--------------------------------------------------------------------------
82
// Public Methods:
83
//--------------------------------------------------------------------------
84

85   public Component getTreeCellEditorComponent(JTree tree, Object JavaDoc 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     // hasFocus ignored
95
}
96
97   public Object JavaDoc getCellEditorValue() {
98     return _lastEditedNode.getUserObject();
99   }
100   //--------------------------------------------------------------------------
101
// Protected Methods:
102
//--------------------------------------------------------------------------
103

104   protected JMenuItem createPropertiesMenuItem(final CategoryNode node) {
105     JMenuItem result = new JMenuItem("Properties");
106     result.addActionListener(new ActionListener JavaDoc() {
107       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc getDisplayedProperties(CategoryNode node) {
124     ArrayList JavaDoc result = new ArrayList JavaDoc();
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     //
145
// Configure the Popup
146
//
147
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 JavaDoc() {
166           public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc() {
180           public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc() {
192       public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc() {
202       public void actionPerformed(ActionEvent JavaDoc e) {
203         collapseDescendants(node);
204       }
205     });
206     return result;
207   }
208
209   /**
210    * This featured was moved from the LogBrokerMonitor class
211    * to the CategoryNodeExplorer so that the Category tree
212    * could be pruned from the Category Explorer popup menu.
213    * This menu option only appears when a user right clicks on
214    * the Category parent node.
215    *
216    * See removeUnusedNodes()
217    */

218   protected JMenuItem createRemoveMenuItem() {
219     JMenuItem result = new JMenuItem("Remove All Empty Categories");
220     result.addActionListener(new ActionListener JavaDoc() {
221       public void actionPerformed(ActionEvent JavaDoc e) {
222         while (removeUnusedNodes() > 0) ;
223       }
224     });
225     return result;
226   }
227
228   protected void expandDescendants(CategoryNode node) {
229     Enumeration JavaDoc 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 JavaDoc descendants = node.depthFirstEnumeration();
239     CategoryNode current;
240     while (descendants.hasMoreElements()) {
241       current = (CategoryNode) descendants.nextElement();
242       collapse(current);
243     }
244   }
245
246   /**
247    * Removes any inactive nodes from the Category tree.
248    */

249   protected int removeUnusedNodes() {
250     int count = 0;
251     CategoryNode root = _categoryModel.getRootCategoryNode();
252     Enumeration JavaDoc 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 JavaDoc getTreePath(CategoryNode node) {
270     return new TreePath JavaDoc(node.getPath());
271   }
272
273   protected void collapse(CategoryNode node) {
274     _tree.collapsePath(getTreePath(node));
275   }
276
277   //-----------------------------------------------------------------------
278
// Private Methods:
279
//--------------------------------------------------------------------------
280

281   //--------------------------------------------------------------------------
282
// Nested Top-Level Classes or Interfaces:
283
//--------------------------------------------------------------------------
284

285 }
286
Popular Tags