Source Code Cross Referenced for BugTreeModel.java in  » Code-Analyzer » findbugs » edu » umd » cs » findbugs » gui2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Code Analyzer » findbugs » edu.umd.cs.findbugs.gui2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * FindBugs - Find Bugs in Java programs
003:         * Copyright (C) 2006, University of Maryland
004:         * 
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         * 
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013:         * Lesser General Public License for more details.
014:         * 
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
018:         */
019:
020:        package edu.umd.cs.findbugs.gui2;
021:
022:        import java.util.ArrayList;
023:        import java.util.List;
024:        import java.util.Vector;
025:
026:        import javax.swing.JTree;
027:        import javax.swing.SwingUtilities;
028:        import javax.swing.event.ChangeEvent;
029:        import javax.swing.event.ListSelectionEvent;
030:        import javax.swing.event.TableColumnModelEvent;
031:        import javax.swing.event.TableColumnModelListener;
032:        import javax.swing.event.TreeExpansionEvent;
033:        import javax.swing.event.TreeExpansionListener;
034:        import javax.swing.event.TreeModelEvent;
035:        import javax.swing.event.TreeModelListener;
036:        import javax.swing.tree.TreeModel;
037:        import javax.swing.tree.TreePath;
038:
039:        import edu.umd.cs.findbugs.BugInstance;
040:        import edu.umd.cs.findbugs.gui2.BugAspects.SortableValue;
041:
042:        /*
043:         * Our TreeModel.  Once upon a time it was a simple model, that queried data, its BugSet, for what to show under each branch
044:         * Then it got more and more complicated, no one knows why it still seems to work... or why it doesn't if it in fact doesn't.  
045:         * 
046:         * Here's a tip, Dont even attempt to deal with suppressions or filtering and their related tree model events without the API
047:         * for TreeModelEvents open.  And read it three times first.  Ignore the fact that its inconsistent for sending events about the root, just pick one of the things it says and go with it
048:         * 
049:         * Heres the order things MUST be done when dealing with suppressions, filtering, unsuppressions... unfiltering... all that fun stuff
050:         * 
051:         * Inserts:
052:         * Update model
053:         * Get Path
054:         * Make Event
055:         * Send Event
056:         * ResetData
057:         * 
058:         * Removes:
059:         * Get Path
060:         * Make Event
061:         * Update Model
062:         * Send Event
063:         * ResetData
064:         * 
065:         * Restructure:
066:         * Update Model
067:         * Get Path
068:         * Make Event
069:         * Send Event
070:         * resetData? hmmm
071:         * 
072:         * These may or may not be the orders of events used in suppressBug, unsuppressBug, branchOperations and so forth
073:         * if they seem to work anyway, I wouldn't touch them.  
074:         * 
075:         * changeSet() is what to do when the data set is completely different (loaded a new collection, reran the analysis what have you)
076:         * changeSet calls rebuild(), which does a very tricky thing, where it makes a new model, and a new JTree, and swaps them in in place of this one, as well as 
077:         * turning off user input in hopefully every place it needs to be turned off
078:         * 
079:         */
080:
081:        /**
082:         * The treeModel for our JTree
083:         */
084:        public class BugTreeModel implements  TreeModel,
085:                TableColumnModelListener, TreeExpansionListener {
086:            private BugAspects root = new BugAspects();
087:            private SorterTableColumnModel st;
088:            private BugSet data;
089:            private ArrayList<TreeModelListener> listeners = new ArrayList<TreeModelListener>();
090:            private JTree tree;
091:            static Vector<BugLeafNode> selectedBugLeafNodes = new Vector<BugLeafNode>();
092:
093:            private static final boolean DEBUG = false;
094:
095:            private volatile Thread rebuildingThread;
096:            private boolean sortOrderChanged;
097:            private boolean sortsAddedOrRemoved;
098:
099:            public BugTreeModel(JTree tree, SorterTableColumnModel st,
100:                    BugSet data) {
101:                st.addColumnModelListener(this );
102:                this .tree = tree;
103:                this .st = st;
104:                this .data = data;
105:                BugSet.setAsRootAndCache(this .data);
106:                root.setCount(data.size());
107:                FilterActivity.addFilterListener(bugTreeFilterListener);
108:                if (DEBUG)
109:                    this .addTreeModelListener(new TreeModelListener() {
110:
111:                        public void treeNodesChanged(TreeModelEvent arg0) {
112:                            System.out.println("Tree nodes changed");
113:                            System.out.println("  " + arg0.getTreePath());
114:
115:                        }
116:
117:                        public void treeNodesInserted(TreeModelEvent arg0) {
118:                            System.out.println("Tree nodes inserted");
119:                            System.out.println("  " + arg0.getTreePath());
120:
121:                        }
122:
123:                        public void treeNodesRemoved(TreeModelEvent arg0) {
124:                            System.out.println("Tree nodes removed");
125:                            System.out.println("  " + arg0.getTreePath());
126:
127:                        }
128:
129:                        public void treeStructureChanged(TreeModelEvent arg0) {
130:                            System.out.println("Tree structure changed");
131:                            System.out.println("  " + arg0.getTreePath());
132:
133:                        }
134:                    });
135:            }
136:
137:            public BugTreeModel(BugTreeModel other) {
138:                this .root = new BugAspects(other.root);
139:                this .st = other.st;
140:                this .data = new BugSet(other.data);
141:                //this.listeners = other.listeners;
142:                this .tree = other.tree;
143:            }
144:
145:            public void getOffListenerList() {
146:                FilterActivity.removeFilterListener(bugTreeFilterListener);
147:                st.removeColumnModelListener(this );
148:                tree.removeTreeExpansionListener(this );
149:            }
150:
151:            public Object getRoot() {
152:                return root;
153:            }
154:
155:            public Object getChild(Object o, int index) {
156:                Object result = getChild0(o, index);
157:                assert o != null : "child " + index + " of " + this 
158:                        + " is null";
159:                return result;
160:            }
161:
162:            public Object getChild0(Object o, int index) {
163:                BugAspects a = (BugAspects) o;
164:                if (st.getOrderBeforeDivider().size() == 0 && a.size() == 0)//Root without any sortables
165:                    return data.get(index);
166:
167:                try {
168:                    if ((a.size() == 0)
169:                            || (a.last().key != st.getOrderBeforeDivider().get(
170:                                    st.getOrderBeforeDivider().size() - 1))) {
171:                        BugAspects child = a.addToNew(enumsThatExist(a).get(
172:                                index));
173:                        child.setCount(data.query(child).size());
174:                        return child;
175:                    } else
176:                        return data.query(a).get(index);
177:                } catch (IndexOutOfBoundsException e) {
178:                    Debug
179:                            .println("IndexOutOfBounds caught: I am treemodel #"
180:                                    + this 
181:                                    + "I am no longer the current treemodel,"
182:                                    + " my data is cached and I return bad values for getChild.  Something is wrong with rebuild,"
183:                                    + " since the tree is asking both of us for children");
184:                    return null;
185:                }
186:
187:            }
188:
189:            public int getChildCount(Object o) {
190:                //			long start = bean.getCurrentThreadCpuTime();
191:                //			try
192:                //			{
193:                //			System.out.println("# getChildCount [o = " + o + "]");
194:                //			System.out.println("getChildCount: " + Thread.currentThread().toString());
195:                if (!(o instanceof  BugAspects))
196:                    return 0;
197:
198:                BugAspects a = (BugAspects) o;
199:
200:                if (st.getOrderBeforeDivider().size() == 0 && a.size() == 0)//If its the root and we aren't sorting by anything
201:                    return data.size();
202:
203:                if ((a.size() == 0)
204:                        || (a.last().key != st.getOrderBeforeDivider().get(
205:                                st.getOrderBeforeDivider().size() - 1)))
206:                    //			{
207:                    //			System.out.println("#  before enumsThatExist: " + (bean.getCurrentThreadCpuTime() - start));
208:                    return enumsThatExist(a).size();
209:                //			}
210:                else
211:                    //			{
212:                    //			System.out.println("#  before query: " + (bean.getCurrentThreadCpuTime() - start));
213:                    return data.query(a).size();
214:                //			}
215:                //			}
216:                //			finally
217:                //			{
218:                //			System.out.println("#  finished: " + (bean.getCurrentThreadCpuTime() - start));
219:                //			}
220:            }
221:
222:            /*This contract has been changed to return a HashList of Stringpair, our own data structure in which finding the index of an object in the list is very fast*/
223:
224:            private HashList<SortableValue> enumsThatExist(BugAspects a) {
225:                //			long start = bean.getCurrentThreadCpuTime();
226:                //			System.out.println(" ## enumsThatExist [a = " + a + "]");
227:                //			try
228:                //			{					
229:                //StringPair[] toCheck = null;
230:                if (st.getOrderBeforeDivider().size() == 0)
231:                    return null;
232:
233:                //					if (a.size() == 0) // root
234:                //						toCheck = getValues(st.getOrder().get(0));
235:                //					else if (st.getOrder().indexOf(a.get(a.size() - 1).key) == st.getOrder().size() - 1) // last branch
236:                //						return null;
237:                //					else // somewhere in between
238:                //						toCheck = getValues(st.getOrder().get(st.getOrder().indexOf(a.get(a.size() - 1).key) + 1));
239:                //					BugSet set = data.query(a);
240:                //			System.out.println(" ## after query: " + (bean.getCurrentThreadCpuTime() - start));
241:                //					for (StringPair sp : toCheck)
242:                //						if (set.contains(sp))
243:                //							result.add(sp);
244:                //			System.out.println(" ## after loop (" + toCheck.length + " elements): " + (bean.getCurrentThreadCpuTime() - start));
245:                //					return result;
246:
247:                Sortables key = (a.size() == 0 ? st.getOrderBeforeDivider()
248:                        .get(0) : st.getOrderBeforeDivider().get(
249:                        st.getOrderBeforeDivider().indexOf(a.last().key) + 1));
250:
251:                String[] all = key.getAll(data.query(a));
252:                ArrayList<SortableValue> result = new ArrayList<SortableValue>();
253:                for (String i : all)
254:                    result.add(new SortableValue(key, i));
255:                //			System.out.println(" ## before sort: " + (bean.getCurrentThreadCpuTime() - start));
256:                //					Collections.sort(result, key);
257:                //			System.out.println(" ## after sort: " + (bean.getCurrentThreadCpuTime() - start));
258:                return new HashList<SortableValue>(result);
259:                //			}
260:                //			finally
261:                //			{
262:                //			System.out.println(" ## finished: " + (bean.getCurrentThreadCpuTime() - start));
263:                //			}
264:            }
265:
266:            public boolean isLeaf(Object o) {
267:                return (o instanceof  BugLeafNode);
268:            }
269:
270:            public void valueForPathChanged(TreePath arg0, Object arg1) {
271:            }
272:
273:            public int getIndexOfChild(Object parent, Object child) {
274:                if (parent == null || child == null || isLeaf(parent))
275:                    return -1;
276:
277:                if (isLeaf(child)) {
278:                    return data.query((BugAspects) parent).indexOf(
279:                            (BugLeafNode) child);
280:                } else {
281:                    HashList<SortableValue> stringPairs = enumsThatExist((BugAspects) parent);
282:                    if (stringPairs == null) {
283:                        //XXX-Threading difficulties-stringpairs is null somehow
284:                        Debug
285:                                .println("Stringpairs is null on getIndexOfChild!  Error!");
286:                        assert (false);
287:                        return -1;
288:                    }
289:
290:                    return stringPairs.indexOf(((BugAspects) child).last());
291:                    //				for (int i = 0; i < stringPairs.size(); i++)
292:                    //					if (stringPairs.get(i).equals(((BugAspects)child).get(((BugAspects)child).size() -1)))
293:                    //						return i;
294:                    //					
295:                    //					if (stringPairArray[i] == ((BugAspects)child).get(((BugAspects)child).size() - 1))
296:                    //						return i;
297:                    //				return -1;
298:                }
299:            }
300:
301:            public void addTreeModelListener(TreeModelListener listener) {
302:                listeners.add(listener);
303:            }
304:
305:            public void removeTreeModelListener(TreeModelListener listener) {
306:                listeners.remove(listener);
307:            }
308:
309:            private static SortableValue[] getValues(Sortables key) {
310:                String[] values = key.getAllSorted();
311:                SortableValue[] result = new SortableValue[values.length];
312:                for (int i = 0; i < values.length; i++) {
313:                    result[i] = new SortableValue(key, values[i]);
314:                }
315:                return result;
316:
317:            }
318:
319:            public void columnAdded(TableColumnModelEvent e) {
320:                sortsAddedOrRemoved = true;
321:                //rebuild();	
322:            }
323:
324:            public void columnRemoved(TableColumnModelEvent e) {
325:                sortsAddedOrRemoved = true;
326:                //rebuild();
327:            }
328:
329:            public void columnMoved(final TableColumnModelEvent evt) {
330:                if (evt.getFromIndex() == evt.getToIndex())
331:                    return;
332:                sortOrderChanged = true;
333:                //rebuild();
334:            }
335:
336:            void changeSet(BugSet set) {
337:                BugSet.setAsRootAndCache(set);
338:                data = new BugSet(set);
339:                root.setCount(data.size());
340:                rebuild();
341:            }
342:
343:            /**
344:             * Swaps in a new BugTreeModel and a new JTree
345:             *
346:             */
347:            private void rebuild() {
348:                if (TRACE)
349:                    System.out.println("rebuilding bug tree model");
350:                PreferencesFrame.getInstance().freeze();
351:                st.freezeOrder();
352:                MainFrame.getInstance().setRebuilding(true);
353:                NewFilterFromBug.closeAll();
354:
355:                //If this thread is not interrupting a previous thread, set the paths to be opened when the new tree is complete
356:                //If the thread is interrupting another thread, dont do this, because you dont have the tree with the correct paths selected
357:
358:                //As of now, it should be impossible to interrupt a rebuilding thread, in another version this may change, so this if statement check is left in, even though it should always be true.
359:                if (rebuildingThread == null)
360:                    setOldSelectedBugs();
361:
362:                Debug
363:                        .println("Please Wait called right before starting rebuild thread");
364:                pleaseWait();
365:                rebuildingThread = new Thread() {
366:                    BugTreeModel newModel;
367:
368:                    @Override
369:                    public void run() {
370:                        try {
371:                            newModel = new BugTreeModel(BugTreeModel.this );
372:                            newModel.listeners = listeners;
373:                            newModel.resetData();
374:                            newModel.data.sortList();
375:                        } finally {
376:                            rebuildingThread = null;
377:                            SwingUtilities.invokeLater(new Runnable() {
378:                                public void run() {
379:                                    if (newModel != null) {
380:                                        JTree newTree = new JTree(newModel);
381:                                        newModel.tree = newTree;
382:                                        MainFrame.getInstance().newTree(
383:                                                newTree, newModel);
384:                                    }
385:                                    getOffListenerList();
386:                                    MainFrame.getInstance()
387:                                            .setRebuilding(false);
388:                                    PreferencesFrame.getInstance().thaw();
389:                                    //st.thawOrder should be the last thing that happens, otherwise a very determined user could slip a new order in before we allow him to rebuild the tree, things get out of sync, nothing bad happens, it just looks wrong until he resorts.
390:                                    st.thawOrder();
391:                                }
392:                            });
393:                        }
394:                    }
395:                };
396:                rebuildingThread.start();
397:            }
398:
399:            public void crawl(final ArrayList<BugAspects> path, final int depth) {
400:                for (int i = 0; i < getChildCount(path.get(path.size() - 1)); i++)
401:                    if (depth > 0) {
402:                        ArrayList<BugAspects> newPath = new ArrayList<BugAspects>(
403:                                path);
404:                        newPath.add((BugAspects) getChild(path
405:                                .get(path.size() - 1), i));
406:                        crawl(newPath, depth - 1);
407:                    } else {
408:                        for (TreeModelListener l : listeners)
409:                            l.treeStructureChanged(new TreeModelEvent(this ,
410:                                    path.toArray()));
411:                    }
412:            }
413:
414:            void openPreviouslySelected(List<BugLeafNode> selected) {
415:                BugInstance bug = null;
416:                TreePath path = null;
417:                Debug.println("Starting Open Previously Selected");
418:                for (BugLeafNode b : selected) {
419:                    try {
420:                        bug = b.getBug();
421:                        path = getPathToBug(bug);
422:                        MainFrame.getInstance().tree.expandPath(path
423:                                .getParentPath());
424:                        MainFrame.getInstance().tree.addSelectionPath(path);
425:                    } catch (NullPointerException e) {
426:                        //Try to recover!
427:                        if (MainFrame.DEBUG)
428:                            System.err
429:                                    .println("Failure opening a selected node, node will not be opened in new tree");
430:                        //						System.err.println(b);  This will be accurate
431:                        //						System.err.println(bug);  This will be accurate
432:                        //System.err.println(path);  
433:                        //						e.printStackTrace();					
434:                        continue;
435:                    } catch (ArrayIndexOutOfBoundsException e) {
436:                        //System.err.println("Failure opening a selected node");
437:                        //System.err.println(b);
438:                        //System.err.println(bug);
439:                        //System.err.println(path);
440:                        if (MainFrame.DEBUG)
441:                            System.err
442:                                    .println("Failure opening a selected node, node will not be opened in new tree");
443:                        //						e.printStackTrace();
444:                        continue;
445:                    }
446:                }
447:
448:            }
449:
450:            /* Recursively traverses the tree, opens all nodes matching any bug 
451:             * in the list, then creates the full paths to the bugs that are selected
452:             * This keeps whatever bugs were selected selected when sorting
453:             *	DEPRECATED--Too Slow, use openPreviouslySelected
454:             */
455:
456:            public void crawlToOpen(TreePath path,
457:                    ArrayList<BugLeafNode> bugLeafNodes,
458:                    ArrayList<TreePath> treePaths) {
459:                for (int i = 0; i < getChildCount(path.getLastPathComponent()); i++) {
460:                    if (!isLeaf(getChild(path.getLastPathComponent(), i)))
461:                        for (BugLeafNode p : bugLeafNodes) {
462:                            if (p.matches((BugAspects) getChild(path
463:                                    .getLastPathComponent(), i))) {
464:                                tree.expandPath(path);
465:                                crawlToOpen(path.pathByAddingChild(getChild(
466:                                        path.getLastPathComponent(), i)),
467:                                        bugLeafNodes, treePaths);
468:                                break;
469:                            }
470:                        }
471:                    else {
472:                        for (BugLeafNode b : bugLeafNodes) {
473:                            if (getChild(path.getLastPathComponent(), i)
474:                                    .equals(b)) {
475:                                tree.expandPath(path);
476:                                treePaths.add(path.pathByAddingChild(getChild(
477:                                        path.getLastPathComponent(), i)));
478:                            }
479:                        }
480:                    }
481:                }
482:            }
483:
484:            public static boolean TRACE = false;
485:
486:            public void resetData()//FIXME:  Does this need a setAsRootAndCache() on the new BugSet?
487:            {
488:                if (TRACE)
489:                    System.out.println("Reseting data in bug tree model");
490:                data = new BugSet(data);
491:            }
492:
493:            FilterListener bugTreeFilterListener = new MyFilterListener();
494:
495:            class MyFilterListener implements  FilterListener {
496:                public void clearCache() {
497:                    if (TRACE)
498:                        System.out.println("clearing cache in bug tree model");
499:                    resetData();
500:                    BugSet.setAsRootAndCache(data);//FIXME:  Should this be in resetData?  Does this allow our main list to not be the same as the data in our tree?
501:                    root.setCount(data.size());
502:
503:                    rebuild();
504:                }
505:
506:                public void unsuppressBug(TreePath path) {
507:                    if (TRACE)
508:                        System.out.println("unsuppressing bug");
509:                    if (path == null)
510:                        return;
511:                    TreePath pathToFirstDeleted = null;
512:                    Object[] objPath = path.getParentPath().getPath();
513:                    ArrayList<Object> reconstruct = new ArrayList<Object>();
514:                    boolean earlyStop = false;
515:                    for (int x = 0; x < objPath.length; x++) {
516:                        Object o = objPath[x];
517:                        reconstruct.add(o);
518:                        if (o instanceof  BugAspects) {
519:                            pathToFirstDeleted = new TreePath(reconstruct
520:                                    .toArray());
521:                            ((BugAspects) o).setCount(((BugAspects) o)
522:                                    .getCount() + 1);
523:
524:                            if (((BugAspects) o).getCount() == 2
525:                                    && reconstruct.size() > 1) {
526:                                earlyStop = true;
527:                                break;
528:                            }
529:
530:                            for (TreeModelListener l : listeners) {
531:                                if (pathToFirstDeleted.getParentPath() != null)
532:                                    l
533:                                            .treeNodesChanged(new TreeModelEvent(
534:                                                    this ,
535:                                                    pathToFirstDeleted
536:                                                            .getParentPath(),
537:                                                    new int[] { getIndexOfChild(
538:                                                            pathToFirstDeleted
539:                                                                    .getParentPath()
540:                                                                    .getLastPathComponent(),
541:                                                            pathToFirstDeleted
542:                                                                    .getLastPathComponent()) },
543:                                                    new Object[] { pathToFirstDeleted
544:                                                            .getLastPathComponent() }));
545:                            }
546:                        }
547:                    }
548:
549:                    if (path.getParentPath() == null)//They are unsuppressing from the root, but we don't allow root to be suppressed, Dont know what to do here
550:                    {
551:                        throw new RuntimeException();
552:                    }
553:
554:                    if (pathToFirstDeleted == null) {
555:                        pathToFirstDeleted = path;
556:                    }
557:
558:                    if (earlyStop == false) {
559:                        pathToFirstDeleted = pathToFirstDeleted
560:                                .pathByAddingChild(path.getLastPathComponent());
561:                    }
562:
563:                    Object parent = pathToFirstDeleted.getParentPath()
564:                            .getLastPathComponent();
565:                    Object child = pathToFirstDeleted.getLastPathComponent();
566:
567:                    TreeModelEvent insertionEvent = new TreeModelEvent(this ,
568:                            pathToFirstDeleted.getParentPath(),
569:                            new int[] { getIndexOfChild(parent, child) },
570:                            new Object[] { child });
571:                    for (TreeModelListener l : listeners) {
572:                        l.treeNodesInserted(insertionEvent);
573:                    }
574:                    if (!isLeaf(child)) {
575:                        TreeModelEvent structureEvent = new TreeModelEvent(
576:                                this , pathToFirstDeleted, new int[0],
577:                                new Object[0]);
578:                        for (TreeModelListener l : listeners) {
579:                            l.treeStructureChanged(structureEvent);
580:                        }
581:                    }
582:                }
583:
584:                public void suppressBug(TreePath path) {
585:                    if (TRACE)
586:                        System.out.println("unsuppressing bug");
587:                    Debug.println(path);
588:                    Object[] objPath = path.getParentPath().getPath();
589:                    ArrayList<Object> reconstruct = new ArrayList<Object>();
590:                    for (int x = 0; x < objPath.length; x++) {
591:                        Object o = objPath[x];
592:                        ((BugAspects) o)
593:                                .setCount(((BugAspects) o).getCount() - 1);
594:                    }
595:
596:                    for (int x = 0; x < objPath.length; x++) {
597:                        Object o = objPath[x];
598:                        reconstruct.add(o);
599:                        if (o instanceof  BugAspects) {
600:                            if (((BugAspects) o).getCount() == 0) {
601:                                path = new TreePath(reconstruct.toArray());
602:                                break;
603:                            }
604:                        }
605:                    }
606:
607:                    TreeModelEvent event;
608:
609:                    if (path.getParentPath() == null)//They are suppressing the last bug in the tree
610:                    {
611:                        event = new TreeModelEvent(this , path, new int[] { 0 },
612:                                new Object[] { BugTreeModel.this .getChild(root,
613:                                        0) });
614:                        root.setCount(0);
615:                    } else {
616:                        Object parent = path.getParentPath()
617:                                .getLastPathComponent();
618:                        Object child = path.getLastPathComponent();
619:                        int indexOfChild = getIndexOfChild(parent, child);
620:                        if (indexOfChild != -1) {
621:                            event = new TreeModelEvent(this , path
622:                                    .getParentPath(),
623:                                    new int[] { indexOfChild },
624:                                    new Object[] { child });
625:                            resetData();
626:                        } else//They are suppressing something that has already been filtered out by setting a designation of a bug to a type that has been filtered out.
627:                        {
628:                            resetData();
629:                            for (TreeModelListener l : listeners) {
630:                                l.treeStructureChanged(new TreeModelEvent(this ,
631:                                        path.getParentPath()));
632:                            }
633:                            return;
634:                        }
635:                    }
636:
637:                    for (TreeModelListener l : listeners) {
638:                        l.treeNodesRemoved(event);
639:                    }
640:                }
641:            }
642:
643:            void treeNodeChanged(TreePath path) {
644:                Debug.println("Tree Node Changed: " + path);
645:                if (path.getParentPath() == null) {
646:                    TreeModelEvent event = new TreeModelEvent(this , path, null,
647:                            null);
648:                    for (TreeModelListener l : listeners) {
649:                        l.treeNodesChanged(event);
650:                    }
651:                    return;
652:                }
653:
654:                TreeModelEvent event = new TreeModelEvent(this , path
655:                        .getParentPath(), new int[] { getIndexOfChild(path
656:                        .getParentPath().getLastPathComponent(), path
657:                        .getLastPathComponent()) }, new Object[] { path
658:                        .getLastPathComponent() });
659:                for (TreeModelListener l : listeners) {
660:                    l.treeNodesChanged(event);
661:                }
662:            }
663:
664:            public TreePath getPathToBug(BugInstance b) {
665:                //ArrayList<Sortables> order=MainFrame.getInstance().getSorter().getOrder();
666:                List<Sortables> order = st.getOrderBeforeDivider();
667:                //Create an array of BugAspects of lengths from one to the full BugAspect list of the bugInstance	
668:                BugAspects[] toBug = new BugAspects[order.size()];
669:                for (int i = 0; i < order.size(); i++)
670:                    toBug[i] = new BugAspects();
671:
672:                for (int x = 0; x < order.size(); x++) {
673:                    for (int y = 0; y <= x; y++) {
674:                        Sortables s = order.get(y);
675:                        toBug[x].add(new SortableValue(s, s.getFrom(b)));
676:                    }
677:                }
678:                //Add this array as elements of the path
679:                TreePath pathToBug = new TreePath(root);
680:                for (int x = 0; x < order.size(); x++) {
681:                    int index = getIndexOfChild(pathToBug
682:                            .getLastPathComponent(), toBug[x]);
683:
684:                    if (index == -1) {
685:                        if (MainFrame.DEBUG)
686:                            System.err
687:                                    .println("Node does not exist in the tree");//For example, not a bug bugs are filtered, they set a bug to be not a bug it filters out
688:                        return null;
689:                    }
690:
691:                    pathToBug = pathToBug.pathByAddingChild(getChild(pathToBug
692:                            .getLastPathComponent(), index));
693:                }
694:                //Using a hashlist to store bugs in BugSet will make getIndexOfChild Waaaaaay faster, thus making this O(1) (avg case)
695:                int index = getIndexOfChild(pathToBug.getLastPathComponent(),
696:                        new BugLeafNode(b));
697:                if (index == -1)
698:                    return null;
699:                pathToBug = pathToBug.pathByAddingChild(getChild(pathToBug
700:                        .getLastPathComponent(), index));
701:                return pathToBug;
702:
703:            }
704:
705:            public TreePath getPathToNewlyUnsuppressedBug(BugInstance b) {
706:                resetData();
707:                return getPathToBug(b);
708:            }
709:
710:            @Override
711:            protected void finalize() throws Throwable {
712:                super .finalize();
713:
714:                //this will inform us when the garbage collector finds our old bug tree models and deletes them, thus preventing obnoxiously hard to find bugs from not remembering to remove the model from our listeners
715:                Debug
716:                        .println("The BugTreeModel has been DELETED!  This means there are no more references to it, and its finally off all of the stupid listener lists");
717:            }
718:
719:            public void columnMarginChanged(ChangeEvent arg0) {
720:            }
721:
722:            public void columnSelectionChanged(ListSelectionEvent arg0) {
723:            }
724:
725:            public void treeExpanded(TreeExpansionEvent event) {
726:            }
727:
728:            public void treeCollapsed(TreeExpansionEvent event) {
729:            }
730:
731:            private void setOldSelectedBugs() {
732:                selectedBugLeafNodes.clear();
733:                if (tree.getSelectionPaths() != null) // Who the cussword wrote this API anyway?
734:                    for (TreePath path : tree.getSelectionPaths())
735:                        if (isLeaf(path.getLastPathComponent()))
736:                            selectedBugLeafNodes.add((BugLeafNode) path
737:                                    .getLastPathComponent());
738:            }
739:
740:            Vector<BugLeafNode> getOldSelectedBugs() {
741:                return selectedBugLeafNodes;
742:            }
743:
744:            void checkSorter() {
745:                if (sortOrderChanged == true || sortsAddedOrRemoved == true) {
746:                    sortOrderChanged = false;
747:                    sortsAddedOrRemoved = false;
748:                    rebuild();
749:                }
750:                //			This old version isn't wrong... it just worries me, as it looks like we could rebuild twice, although
751:                //			we never do.  Above version should be safer.
752:                //			if (sortOrderChanged==true)
753:                //			{
754:                //				sortOrderChanged=false;
755:                //				rebuild();
756:                //			}
757:                //			if (sortsAddedOrRemoved==true)
758:                //			{
759:                //				sortsAddedOrRemoved=false;
760:                //				rebuild();
761:                //			}
762:            }
763:
764:            static void pleaseWait() {
765:                pleaseWait(null);
766:            }
767:
768:            static void pleaseWait(final String message) {
769:                MainFrame.getInstance().showWaitCard();
770:            }
771:
772:            public TreeModelEvent restructureBranch(
773:                    ArrayList<String> stringsToBranch, boolean removing)
774:                    throws BranchOperationException {
775:                if (removing)
776:                    return branchOperations(stringsToBranch,
777:                            TreeModification.REMOVERESTRUCTURE);
778:                else
779:                    return branchOperations(stringsToBranch,
780:                            TreeModification.INSERTRESTRUCTURE);
781:            }
782:
783:            public TreeModelEvent insertBranch(ArrayList<String> stringsToBranch)
784:                    throws BranchOperationException {
785:                return branchOperations(stringsToBranch,
786:                        TreeModification.INSERT);
787:            }
788:
789:            public TreeModelEvent removeBranch(ArrayList<String> stringsToBranch)
790:                    throws BranchOperationException {
791:                return branchOperations(stringsToBranch,
792:                        TreeModification.REMOVE);
793:            }
794:
795:            public void sortBranch(TreePath pathToBranch) {
796:                BugSet bs = data.query((BugAspects) pathToBranch
797:                        .getLastPathComponent());
798:                bs.sortList();
799:                Debug.println("Data in sorted branch: "
800:                        + pathToBranch.getLastPathComponent());
801:                for (BugLeafNode b : bs) {
802:                    Debug.println(b);
803:                }
804:
805:                Object[] children = new Object[getChildCount(pathToBranch
806:                        .getLastPathComponent())];
807:                int[] childIndices = new int[children.length];
808:                for (int x = 0; x < children.length; x++) {
809:                    children[x] = getChild(pathToBranch.getLastPathComponent(),
810:                            x);
811:                    childIndices[x] = x;
812:                }
813:                for (TreeModelListener l : listeners) {
814:                    TreeModelEvent event = new TreeModelEvent(this ,
815:                            pathToBranch, childIndices, children);
816:                    l.treeNodesChanged(event);
817:                }
818:
819:            }
820:
821:            @SuppressWarnings("serial")
822:            static class BranchOperationException extends Exception {
823:                public BranchOperationException(String s) {
824:                    super (s);
825:                }
826:            }
827:
828:            enum TreeModification {
829:                REMOVE, INSERT, REMOVERESTRUCTURE, INSERTRESTRUCTURE
830:            };
831:
832:            private TreeModelEvent branchOperations(
833:                    ArrayList<String> stringsToBranch, TreeModification whatToDo)
834:                    throws BranchOperationException {
835:                TreeModelEvent event = null;
836:
837:                if (whatToDo == TreeModification.REMOVE)
838:                    Debug.println("Removing a branch......");
839:                else if (whatToDo == TreeModification.INSERT)
840:                    Debug.println("Inserting a branch......");
841:                else if (whatToDo == TreeModification.REMOVERESTRUCTURE)
842:                    Debug.println("Restructuring from branch to remove......");
843:                else if (whatToDo == TreeModification.INSERTRESTRUCTURE)
844:                    Debug.println("Restructuring from branch to insert......");
845:                Debug.println(stringsToBranch);
846:
847:                if (whatToDo == TreeModification.INSERT
848:                        || whatToDo == TreeModification.INSERTRESTRUCTURE) {
849:                    resetData();
850:                }
851:                //ArrayList<Sortables> order=MainFrame.getInstance().getSorter().getOrder();
852:                List<Sortables> order = st.getOrderBeforeDivider();
853:                //Create an array of BugAspects of lengths from one to the full BugAspect list of the bugInstance	
854:                BugAspects[] toBug = new BugAspects[stringsToBranch.size()];
855:                for (int x = 0; x < stringsToBranch.size(); x++) {
856:                    toBug[x] = new BugAspects();
857:
858:                    for (int y = 0; y <= x; y++) {
859:                        Sortables s = order.get(y);
860:                        toBug[x].add(new SortableValue(s, stringsToBranch
861:                                .get(y)));
862:                    }
863:                }
864:
865:                //Add this array as elements of the path
866:                TreePath pathToBranch = new TreePath(root);
867:                for (int x = 0; x < stringsToBranch.size(); x++) {
868:                    BugAspects child = toBug[x];
869:                    BugAspects parent = (BugAspects) pathToBranch
870:                            .getLastPathComponent();
871:                    if (getIndexOfChild(parent, child) != -1) {
872:                        pathToBranch = pathToBranch.pathByAddingChild(child);
873:                    } else {
874:                        Debug.println(parent + " does not contain " + child);
875:                        throw new BranchOperationException(
876:                                "Branch has been filtered out by another filter.");
877:                        //					break;
878:                    }
879:                }
880:                if (pathToBranch.getParentPath() != null)
881:                    while (getChildCount(pathToBranch.getParentPath()
882:                            .getLastPathComponent()) == 1) {
883:                        if (pathToBranch.getParentPath().getLastPathComponent()
884:                                .equals(root))
885:                            break;
886:                        pathToBranch = pathToBranch.getParentPath();
887:                    }
888:                Debug.println(pathToBranch);
889:
890:                if (whatToDo == TreeModification.INSERT) {
891:                    event = new TreeModelEvent(
892:                            this ,
893:                            pathToBranch.getParentPath(),
894:                            new int[] { getIndexOfChild(pathToBranch
895:                                    .getParentPath().getLastPathComponent(),
896:                                    pathToBranch.getLastPathComponent()) },
897:                            new Object[] { pathToBranch.getLastPathComponent() });
898:                } else if (whatToDo == TreeModification.INSERTRESTRUCTURE) {
899:                    event = new TreeModelEvent(this , pathToBranch);
900:                }
901:
902:                if (whatToDo == TreeModification.REMOVE) {
903:                    event = new TreeModelEvent(
904:                            this ,
905:                            pathToBranch.getParentPath(),
906:                            new int[] { getIndexOfChild(pathToBranch
907:                                    .getParentPath().getLastPathComponent(),
908:                                    pathToBranch.getLastPathComponent()) },
909:                            new Object[] { pathToBranch.getLastPathComponent() });
910:
911:                } else if (whatToDo == TreeModification.REMOVERESTRUCTURE) {
912:                    event = new TreeModelEvent(this , pathToBranch);
913:                }
914:
915:                if (whatToDo == TreeModification.REMOVE
916:                        || whatToDo == TreeModification.REMOVERESTRUCTURE)
917:                    resetData();
918:
919:                return event;
920:            }
921:
922:            void sendEvent(TreeModelEvent event, TreeModification whatToDo) {
923:                Debug.println("Sending An Event!");
924:                if (event == null) {
925:                    throw new IllegalStateException("Dont throw null events.");
926:                }
927:                resetData();
928:                for (TreeModelListener l : listeners) {
929:                    if (whatToDo == TreeModification.REMOVE)
930:                        l.treeNodesRemoved(event);
931:                    else if (whatToDo == TreeModification.INSERT) {
932:                        l.treeNodesInserted(event);
933:                        l
934:                                .treeStructureChanged(new TreeModelEvent(this ,
935:                                        new TreePath(event.getPath())
936:                                                .pathByAddingChild(event
937:                                                        .getChildren()[0])));
938:                    } else if (whatToDo == TreeModification.INSERTRESTRUCTURE
939:                            || whatToDo == TreeModification.REMOVERESTRUCTURE) {
940:                        l.treeStructureChanged(event);
941:                    }
942:                }
943:
944:                root.setCount(data.size());
945:                TreePath changedPath = new TreePath(root);
946:                treeNodeChanged(changedPath);
947:                changedPath = new TreePath(event.getPath());
948:                while (changedPath.getParentPath() != null) {
949:                    treeNodeChanged(changedPath);
950:                    changedPath = changedPath.getParentPath();
951:                }
952:            }
953:
954:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.