-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathXMLTree.java
More file actions
193 lines (177 loc) · 5.81 KB
/
XMLTree.java
File metadata and controls
193 lines (177 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Enumeration;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
/**
* This is an XML tree in a scroller.
*
* @author Douglas Brown
*/
public class XMLTree {
// static fields
protected static Icon hiliteIcon;
// instance fields
protected XMLTreeNode root;
protected JTree tree;
protected JScrollPane scroller;
protected XMLControl control;
protected java.util.List<XMLProperty> selectedProps = new java.util.ArrayList<XMLProperty>();
protected Class<?> hilite = Object.class;
/**
* Constructs a tree view of an XMLControl
*
* @param control the XMLControl
*/
public XMLTree(XMLControl control) {
this.control = control;
createGUI();
}
/**
* Gets the tree.
*
* @return the tree
*/
public JTree getTree() {
return tree;
}
/**
* Gets the selected xml properties.
*
* @return a list of currently selected properties
*/
public java.util.List<XMLProperty> getSelectedProperties() {
selectedProps.clear();
TreePath[] paths = tree.getSelectionPaths();
if(paths!=null) {
for(int i = 0; i<paths.length; i++) {
XMLTreeNode node = (XMLTreeNode) paths[i].getLastPathComponent();
selectedProps.add(node.getProperty());
}
}
return selectedProps;
}
/**
* Gets the scroll pane with view of the tree
*
* @return the scroll pane
*/
public JScrollPane getScrollPane() {
return scroller;
}
/**
* Sets the highlighted class.
*
* @param type the class to highlight
*/
public void setHighlightedClass(Class<?> type) {
if(type!=null) {
hilite = type;
scroller.repaint();
}
}
/**
* Gets the highlighted class.
*
* @return the highlighted class
*/
public Class<?> getHighlightedClass() {
return hilite;
}
/**
* Selects the highlighted properties.
*/
public void selectHighlightedProperties() {
Enumeration<?> e = root.breadthFirstEnumeration();
while(e.hasMoreElements()) {
XMLTreeNode node = (XMLTreeNode) e.nextElement();
XMLProperty prop = node.getProperty();
Class<?> type = prop.getPropertyClass();
if((type!=null)&&hilite.isAssignableFrom(type)) {
TreePath path = new TreePath(node.getPath());
tree.addSelectionPath(path);
tree.scrollPathToVisible(path);
}
}
}
/**
* Shows the highlighted properties.
*/
public void showHighlightedProperties() {
Enumeration<?> e = root.breadthFirstEnumeration();
while(e.hasMoreElements()) {
XMLTreeNode node = (XMLTreeNode) e.nextElement();
XMLProperty prop = node.getProperty();
Class<?> type = prop.getPropertyClass();
if((type!=null)&&hilite.isAssignableFrom(type)) {
TreePath path = new TreePath(node.getPath());
tree.scrollPathToVisible(path);
}
}
}
/**
* Creates the GUI and listeners.
*/
protected void createGUI() {
// create icons
String imageFile = "/org/opensourcephysics/resources/controls/images/hilite.gif"; //$NON-NLS-1$
hiliteIcon = new ImageIcon(XMLTree.class.getResource(imageFile));
// create root and tree
root = new XMLTreeNode(control);
tree = new JTree(root);
tree.setCellRenderer(new HighlightRenderer());
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
// put tree in a scroller
scroller = new JScrollPane(tree);
scroller.setPreferredSize(new Dimension(200, 200));
}
/**
* A cell renderer to show launchable nodes.
*/
private class HighlightRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
XMLTreeNode node = (XMLTreeNode) value;
XMLProperty prop = node.getProperty();
Class<?> type = prop.getPropertyClass();
if((type!=null)&&hilite.isAssignableFrom(type)) {
setIcon(hiliteIcon);
}
return this;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/