File Source: TreeViewModel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewModel__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.framemanager.tree;
24
25 import com.dmdirc.FrameContainerComparator;
26 import com.dmdirc.GlobalWindow;
27 import com.dmdirc.config.IdentityManager;
28
29 import javax.swing.tree.DefaultTreeModel;
30
31 /**
32 * A simple sorted tree data model based on DefaultTreeModel.
33 */
34 public class TreeViewModel extends DefaultTreeModel {
35
36 /**
37 * A version number for this class. It should be changed whenever the class
38 * structure is changed (or anything else that would prevent serialized
39 * objects being unserialized with the new class).
40 */
41 private static final long serialVersionUID = 1;
42 /** Frame container comparator. */
43 private final FrameContainerComparator comparator;
44
45 /**
46 * Creates a tree in which any node can have children.
47 *
48 * @param root a TreeNode object that is the root of the tree.
49 */
50 public TreeViewModel(final TreeViewNode root) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewModel(TreeViewNode)
*
* Postconditions:
* this.comparator == &new FrameContainerComparator(TreeViewModel#1)
* new FrameContainerComparator(TreeViewModel#1) num objects == 1
*/
51 this(root, false);
52 }
53
54 /**
55 * Creates a tree specifying whether any node can have children,
56 * or whether only certain nodes can have children.
57 *
58 * @param asksAllowsChildren true = ask whether child can have chilren,
59 * false all nodes can have chilren.
60 * @param root a root TreeNode.
61 */
62 public TreeViewModel(final TreeViewNode root,
63 final boolean asksAllowsChildren) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewModel(TreeViewNode, bool)
*
* Postconditions:
* this.comparator == &new FrameContainerComparator(TreeViewModel#1)
* new FrameContainerComparator(TreeViewModel#1) num objects == 1
*/
64 super(root, asksAllowsChildren);
65
66 comparator = new FrameContainerComparator();
67 }
68
69 /**
70 * Inserts a new node into the tree and fires the appropriate events.
71 *
72 * @param newChild child to be added.
73 * @param parent parent child is to be added too.
74 */
75 public final void insertNodeInto(final TreeViewNode newChild,
76 final TreeViewNode parent) {
/*
P/P * Method: void insertNodeInto(TreeViewNode, TreeViewNode)
*
* Preconditions:
* newChild != null
* init'ed(newChild.frameContainer)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) parent != null
* (soft) this.comparator != null
* (soft) init'ed(this.root)
*
* Postconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(new ArrayList(getSources#1) num objects)
* init'ed(new ConfigManager(getGlobalConfig#1) num objects)
* possibly_updated(new ConfigManager(getGlobalConfig#1).channel)
* possibly_updated(new ConfigManager(getGlobalConfig#1).file)
* possibly_updated(new ConfigManager(getGlobalConfig#1).ircd)
* possibly_updated(new ConfigManager(getGlobalConfig#1).listeners)
* possibly_updated(new ConfigManager(getGlobalConfig#1).network)
* possibly_updated(new ConfigManager(getGlobalConfig#1).server)
* ...
*/
77 int index = 0;
78 index = getIndex(newChild, parent);
79 super.insertNodeInto(newChild, parent, index);
80 }
81
82 /**
83 * Compares the new child with the existing children or parent to decide
84 * where it needs to be inserted.
85 *
86 * @param newChild new node to be inserted.
87 * @param parent node the new node will be inserted into.
88 *
89 * @return index where new node is to be inserted.
90 */
91 private int getIndex(final TreeViewNode newChild, final TreeViewNode parent) {
/*
P/P * Method: int getIndex(TreeViewNode, TreeViewNode)
*
* Preconditions:
* newChild != null
* init'ed(newChild.frameContainer)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) parent != null
* (soft) this.comparator != null
* (soft) init'ed(this.root)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewNode:getChildAt(...)@102 != null
* com.dmdirc.addons.ui_swing.framemanager.tree.TreeViewNode:getUserObject(...)@105 != null
*
* Postconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(return_value)
* new ArrayList(getSources#1) num objects <= 1
* init'ed(new ArrayList(getSources#1) num objects)
* new ConfigManager(getGlobalConfig#1) num objects <= 1
* init'ed(new ConfigManager(getGlobalConfig#1) num objects)
* init'ed(new ConfigManager(getGlobalConfig#1).channel)
* init'ed(new ConfigManager(getGlobalConfig#1).file)
* init'ed(new ConfigManager(getGlobalConfig#1).ircd)
* ...
*
* Test Vectors:
* java.lang.Boolean:parseBoolean(...)@159: {1}, {0}
* java.lang.Boolean:parseBoolean(...)@159: {0}, {1}
* java.lang.Object:equals(...)@96: {0}, {1}
* java.lang.String:compareToIgnoreCase(...)@105: {0..232-1}, {-231..-1}
*/
92 if (newChild.getFrameContainer() instanceof GlobalWindow) {
93 return 0;
94 }
95
96 if (parent.equals(root) && !IdentityManager.getGlobalConfig().
97 getOptionBool("treeview", "sortservers")) {
98 return parent.getChildCount();
99 }
100
101 for (int i = 0; i < parent.getChildCount(); i++) {
102 final TreeViewNode child = (TreeViewNode) parent.getChildAt(i);
103 if (sortBefore(newChild, child)) {
104 return i;
105 } else if (!sortAfter(newChild, child) && IdentityManager.getGlobalConfig().
106 getOptionBool("treeview", "sortwindows") && newChild.getUserObject().
107 toString().compareToIgnoreCase(child.getUserObject().toString()) < 0) {
108 return i;
109 }
110 }
111
112 return parent.getChildCount();
113 }
114
115 /**
116 * Compares the types of the specified nodes' objects to see if the new
117 * node should be sorted before the other.
118 *
119 * @param newChild The new child to be tested
120 * @param child The existing child that it's being tested against
121 *
122 * @return True iff newChild should be sorted before child
123 */
124 private boolean sortBefore(final TreeViewNode newChild, final TreeViewNode child) {
/*
P/P * Method: bool sortBefore(TreeViewNode, TreeViewNode)
*
* Preconditions:
* child != null
* init'ed(child.frameContainer)
* newChild != null
* init'ed(newChild.frameContainer)
* this.comparator != null
*
* Postconditions:
* init'ed(return_value)
*/
125 return comparator.compare(newChild.getFrameContainer(), child.getFrameContainer()) <= -1;
126 }
127
128 /**
129 * Compares the types of the specified nodes' objects to see if the new
130 * node should be sorted after the other.
131 *
132 * @param newChild The new child to be tested
133 * @param child The existing child that it's being tested against
134 *
135 * @return True iff newChild should be sorted before child
136 */
137 private boolean sortAfter(final TreeViewNode newChild,final TreeViewNode child) {
/*
P/P * Method: bool sortAfter(TreeViewNode, TreeViewNode)
*
* Preconditions:
* child != null
* init'ed(child.frameContainer)
* newChild != null
* init'ed(newChild.frameContainer)
* this.comparator != null
*
* Postconditions:
* init'ed(return_value)
*/
138 return comparator.compare(newChild.getFrameContainer(), child.getFrameContainer()) >= 1;
139 }
140
141 /**
142 * Returns the root node for this model.
143 *
144 * @return Root node
145 */
146 public TreeViewNode getRootNode() {
/*
P/P * Method: TreeViewNode getRootNode()
*
* Postconditions:
* init'ed(return_value)
*/
147 return (TreeViewNode) getRoot();
148 }
149 }
SofCheck Inspector Build Version : 2.17854
| TreeViewModel.java |
2009-Jun-25 01:54:24 |
| TreeViewModel.class |
2009-Sep-02 17:04:14 |