KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.lf5.LogRecord;
19
20 import javax.swing.*;
21 import javax.swing.tree.DefaultTreeModel JavaDoc;
22 import javax.swing.tree.TreeNode JavaDoc;
23 import javax.swing.tree.TreePath JavaDoc;
24 import java.awt.*;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 /**
30  * CategoryExplorerModel
31  *
32  * @author Michael J. Sikorsky
33  * @author Robert Shaw
34  * @author Brent Sprecher
35  * @author Richard Hurst
36  */

37
38 // Contributed by ThoughtWorks Inc.
39

40 public class CategoryExplorerModel extends DefaultTreeModel JavaDoc {
41   //--------------------------------------------------------------------------
42
// Constants:
43
//--------------------------------------------------------------------------
44

45   //--------------------------------------------------------------------------
46
// Protected Variables:
47
//--------------------------------------------------------------------------
48

49   protected boolean _renderFatal = true;
50   protected ActionListener JavaDoc _listener = null;
51   protected ActionEvent JavaDoc _event = new ActionEvent JavaDoc(this,
52       ActionEvent.ACTION_PERFORMED,
53       "Nodes Selection changed");
54
55   //--------------------------------------------------------------------------
56
// Private Variables:
57
//--------------------------------------------------------------------------
58

59   //--------------------------------------------------------------------------
60
// Constructors:
61
//--------------------------------------------------------------------------
62

63   public CategoryExplorerModel(CategoryNode node) {
64     super(node);
65   }
66   //--------------------------------------------------------------------------
67
// Public Methods:
68
//--------------------------------------------------------------------------
69

70   public void addLogRecord(LogRecord lr) {
71     CategoryPath path = new CategoryPath(lr.getCategory());
72     addCategory(path); // create category path if it is new
73
CategoryNode node = getCategoryNode(path);
74     node.addRecord(); // update category node
75
if (_renderFatal && lr.isFatal()) {
76       TreeNode JavaDoc[] nodes = getPathToRoot(node);
77       int len = nodes.length;
78       CategoryNode parent;
79
80       // i = 0 gives root node
81
// skip node and root, loop through "parents" in between
82
for (int i = 1; i < len - 1; i++) {
83         parent = (CategoryNode) nodes[i];
84         parent.setHasFatalChildren(true);
85         nodeChanged(parent);
86       }
87       node.setHasFatalRecords(true);
88       nodeChanged(node);
89     }
90   }
91
92   public CategoryNode getRootCategoryNode() {
93     return (CategoryNode) getRoot();
94   }
95
96   public CategoryNode getCategoryNode(String JavaDoc category) {
97     CategoryPath path = new CategoryPath(category);
98     return (getCategoryNode(path));
99   }
100
101   /**
102    * returns null if no CategoryNode exists.
103    */

104   public CategoryNode getCategoryNode(CategoryPath path) {
105     CategoryNode root = (CategoryNode) getRoot();
106     CategoryNode parent = root; // Start condition.
107

108     for (int i = 0; i < path.size(); i++) {
109       CategoryElement element = path.categoryElementAt(i);
110
111       // If the two nodes have matching titles they are considered equal.
112
Enumeration JavaDoc children = parent.children();
113
114       boolean categoryAlreadyExists = false;
115       while (children.hasMoreElements()) {
116         CategoryNode node = (CategoryNode) children.nextElement();
117         String JavaDoc title = node.getTitle().toLowerCase();
118
119         String JavaDoc pathLC = element.getTitle().toLowerCase();
120         if (title.equals(pathLC)) {
121           categoryAlreadyExists = true;
122           // This is now the new parent node.
123
parent = node;
124           break; // out of the while, and back to the for().
125
}
126       }
127
128       if (categoryAlreadyExists == false) {
129         return null; // Didn't find the Node.
130
}
131     }
132
133     return (parent);
134   }
135
136   /**
137    * @return true if all the nodes in the specified CategoryPath are
138    * selected.
139    */

140   public boolean isCategoryPathActive(CategoryPath path) {
141     CategoryNode root = (CategoryNode) getRoot();
142     CategoryNode parent = root; // Start condition.
143
boolean active = false;
144
145     for (int i = 0; i < path.size(); i++) {
146       CategoryElement element = path.categoryElementAt(i);
147
148       // If the two nodes have matching titles they are considered equal.
149
Enumeration JavaDoc children = parent.children();
150
151       boolean categoryAlreadyExists = false;
152       active = false;
153
154       while (children.hasMoreElements()) {
155         CategoryNode node = (CategoryNode) children.nextElement();
156         String JavaDoc title = node.getTitle().toLowerCase();
157
158         String JavaDoc pathLC = element.getTitle().toLowerCase();
159         if (title.equals(pathLC)) {
160           categoryAlreadyExists = true;
161           // This is now the new parent node.
162
parent = node;
163
164           if (parent.isSelected()) {
165             active = true;
166           }
167
168           break; // out of the while, and back to the for().
169
}
170       }
171
172       if (active == false || categoryAlreadyExists == false) {
173         return false;
174       }
175     }
176
177     return (active);
178   }
179
180
181   /**
182    * <p>Method altered by Richard Hurst such that it returns the CategoryNode
183    * corresponding to the CategoryPath</p>
184    *
185    * @param CategoryPath
186    * @returns CategoryNode
187    */

188   public CategoryNode addCategory(CategoryPath path) {
189     CategoryNode root = (CategoryNode) getRoot();
190     CategoryNode parent = root; // Start condition.
191

192     for (int i = 0; i < path.size(); i++) {
193       CategoryElement element = path.categoryElementAt(i);
194
195       // If the two nodes have matching titles they are considered equal.
196
Enumeration JavaDoc children = parent.children();
197
198       boolean categoryAlreadyExists = false;
199       while (children.hasMoreElements()) {
200         CategoryNode node = (CategoryNode) children.nextElement();
201         String JavaDoc title = node.getTitle().toLowerCase();
202
203         String JavaDoc pathLC = element.getTitle().toLowerCase();
204         if (title.equals(pathLC)) {
205           categoryAlreadyExists = true;
206           // This is now the new parent node.
207
parent = node;
208           break;
209         }
210       }
211
212       if (categoryAlreadyExists == false) {
213         // We need to add the node.
214
CategoryNode newNode = new CategoryNode(element.getTitle());
215
216         //This method of adding a new node cause parent roots to be
217
// collapsed.
218
//parent.add( newNode );
219
//reload(parent);
220

221         // This doesn't force the nodes to collapse.
222
insertNodeInto(newNode, parent, parent.getChildCount());
223         refresh(newNode);
224
225         // The newly added node is now the parent.
226
parent = newNode;
227
228       }
229     }
230
231     return parent;
232   }
233
234   public void update(CategoryNode node, boolean selected) {
235     if (node.isSelected() == selected) {
236       return; // nothing was changed, nothing to do
237
}
238     // select parents or deselect children
239
if (selected) {
240       setParentSelection(node, true);
241     } else {
242       setDescendantSelection(node, false);
243     }
244   }
245
246   public void setDescendantSelection(CategoryNode node, boolean selected) {
247     Enumeration JavaDoc descendants = node.depthFirstEnumeration();
248     CategoryNode current;
249     while (descendants.hasMoreElements()) {
250       current = (CategoryNode) descendants.nextElement();
251       // does the current node need to be changed?
252
if (current.isSelected() != selected) {
253         current.setSelected(selected);
254         nodeChanged(current);
255       }
256     }
257     notifyActionListeners();
258   }
259
260   public void setParentSelection(CategoryNode node, boolean selected) {
261     TreeNode JavaDoc[] nodes = getPathToRoot(node);
262     int len = nodes.length;
263     CategoryNode parent;
264
265     // i = 0 gives root node, i=len-1 gives this node
266
// skip the root node
267
for (int i = 1; i < len; i++) {
268       parent = (CategoryNode) nodes[i];
269       if (parent.isSelected() != selected) {
270         parent.setSelected(selected);
271         nodeChanged(parent);
272       }
273     }
274     notifyActionListeners();
275   }
276
277
278   public synchronized void addActionListener(ActionListener JavaDoc l) {
279     _listener = AWTEventMulticaster.add(_listener, l);
280   }
281
282   public synchronized void removeActionListener(ActionListener JavaDoc l) {
283     _listener = AWTEventMulticaster.remove(_listener, l);
284   }
285
286   public void resetAllNodeCounts() {
287     Enumeration JavaDoc nodes = getRootCategoryNode().depthFirstEnumeration();
288     CategoryNode current;
289     while (nodes.hasMoreElements()) {
290       current = (CategoryNode) nodes.nextElement();
291       current.resetNumberOfContainedRecords();
292       nodeChanged(current);
293     }
294   }
295
296   /**
297    * <p>Returns the CategoryPath to the specified CategoryNode</p>
298    *
299    * @param CategoryNode The target CategoryNode
300    * @returns CategoryPath
301    */

302   public TreePath JavaDoc getTreePathToRoot(CategoryNode node) {
303     if (node == null) {
304       return null;
305     }
306     return (new TreePath JavaDoc(getPathToRoot(node)));
307   }
308
309   //--------------------------------------------------------------------------
310
// Protected Methods:
311
//--------------------------------------------------------------------------
312
protected void notifyActionListeners() {
313     if (_listener != null) {
314       _listener.actionPerformed(_event);
315     }
316   }
317
318   /**
319    * Fires a nodechanged event on the SwingThread.
320    */

321   protected void refresh(final CategoryNode node) {
322     SwingUtilities.invokeLater(new Runnable JavaDoc() {
323       public void run() {
324         nodeChanged(node); // remind the tree to render the new node
325
}
326     });
327   }
328
329   //--------------------------------------------------------------------------
330
// Private Methods:
331
//--------------------------------------------------------------------------
332

333   //--------------------------------------------------------------------------
334
// Nested Top-Level Classes or Interfaces:
335
//--------------------------------------------------------------------------
336

337 }
338
339
340
341
342
343
344
Popular Tags