svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / panelGroup / treePanel / TreePanel.java @ 40561
History | View | Annotate | Download (23.4 KB)
1 | 40561 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40561 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40561 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40561 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.gui.beans.panelGroup.treePanel; |
||
25 | |||
26 | import java.awt.BorderLayout; |
||
27 | import java.awt.Component; |
||
28 | import java.awt.Dimension; |
||
29 | import java.awt.event.ContainerEvent; |
||
30 | import java.io.Serializable; |
||
31 | import java.util.Enumeration; |
||
32 | |||
33 | import javax.swing.ImageIcon; |
||
34 | import javax.swing.JPanel; |
||
35 | import javax.swing.JScrollPane; |
||
36 | import javax.swing.JSplitPane; |
||
37 | import javax.swing.JTree; |
||
38 | import javax.swing.event.ChangeEvent; |
||
39 | import javax.swing.event.TreeModelEvent; |
||
40 | import javax.swing.event.TreeModelListener; |
||
41 | import javax.swing.event.TreeSelectionEvent; |
||
42 | import javax.swing.event.TreeSelectionListener; |
||
43 | import javax.swing.tree.DefaultMutableTreeNode; |
||
44 | import javax.swing.tree.DefaultTreeCellRenderer; |
||
45 | import javax.swing.tree.DefaultTreeModel; |
||
46 | import javax.swing.tree.TreePath; |
||
47 | import javax.swing.tree.TreeSelectionModel; |
||
48 | |||
49 | import org.gvsig.gui.beans.panelGroup.AbstractPanelGroup; |
||
50 | import org.gvsig.gui.beans.panelGroup.PanelGroupManager; |
||
51 | import org.gvsig.gui.beans.panelGroup.panels.AbstractPanel; |
||
52 | import org.gvsig.gui.beans.panelGroup.panels.IPanel; |
||
53 | import org.gvsig.gui.beans.panelGroup.tabbedPanel.TabbedPanel; |
||
54 | |||
55 | |||
56 | /**
|
||
57 | * <p>Graphical interface that's a {@link JPanel JPanel} with an inner {@link JSplitPane JSplitPane} that
|
||
58 | * contains the {@link IPanel IPanel}'s grouped in a {@link JTree JTree}.</p>
|
||
59 | * <p>When user selects one of them, at the tree, that's displayed at the right pane of the split pane, and
|
||
60 | * user can work with it.</p>
|
||
61 | *
|
||
62 | * @see AbstractPanelGroup
|
||
63 | *
|
||
64 | * @version 15/10/2007
|
||
65 | * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
||
66 | */
|
||
67 | public class TreePanel extends AbstractPanelGroup implements Serializable { |
||
68 | private static final long serialVersionUID = 2350636078676872309L; |
||
69 | |||
70 | /**
|
||
71 | * <p>Graphical interface where the tree and the selected panel will be drawn.</p>
|
||
72 | *
|
||
73 | * @see #getJSplitPane()
|
||
74 | */
|
||
75 | private JSplitPane jSplitPane = null; |
||
76 | |||
77 | /**
|
||
78 | * <p>Graphical interface that represents a tree where panels will be listed and grouped together.</p>
|
||
79 | *
|
||
80 | * @see #getJTree()
|
||
81 | */
|
||
82 | private JTree jTree = null; |
||
83 | |||
84 | /**
|
||
85 | * <p>Graphical interface where is allocated the tree.</p>
|
||
86 | *
|
||
87 | * @see #getJSplitPane()
|
||
88 | */
|
||
89 | private JScrollPane treeJScrollPane = null; |
||
90 | |||
91 | /**
|
||
92 | * <p>Reference to the root node of the tree.</p>
|
||
93 | *
|
||
94 | * #see #getRootNode()
|
||
95 | */
|
||
96 | private DefaultMutableTreeNode rootNode = null; |
||
97 | |||
98 | /**
|
||
99 | * <p>Reference to the tree model.</p>
|
||
100 | *
|
||
101 | * @see #getTreeModel()
|
||
102 | */
|
||
103 | private DefaultTreeModel treeModel = null; |
||
104 | |||
105 | /**
|
||
106 | * <p>A panel used in the initialization of the split pane.</p>
|
||
107 | *
|
||
108 | * @see #getDefaultPanel()
|
||
109 | */
|
||
110 | private AbstractPanel defaultPanel = null; |
||
111 | |||
112 | /**
|
||
113 | * <p>Name of the tree root node.</p>
|
||
114 | */
|
||
115 | private final String rootNodeName = ""; |
||
116 | |||
117 | /**
|
||
118 | * <p>Default (horizontal) divider width.</p>
|
||
119 | */
|
||
120 | private final short DEFAULT_DIVIDER_WIDTH = 5; |
||
121 | |||
122 | /**
|
||
123 | * <p>Reference to the last node inserted or removed.</p>
|
||
124 | */
|
||
125 | private DefaultMutableTreeNode lastNode = null; |
||
126 | |||
127 | /**
|
||
128 | * <p>Attribute used to hold the position of the divider.</p>
|
||
129 | */
|
||
130 | private int currentDividerLocation; |
||
131 | |||
132 | /**
|
||
133 | * <p>Number of the registered panels of this group that are in the GUI.</p>
|
||
134 | */
|
||
135 | private int panelsInGUICount; |
||
136 | |||
137 | static {
|
||
138 | // Registers this class as a "PanelGroup" type
|
||
139 | PanelGroupManager.getManager().registerPanelGroup(TabbedPanel.class); |
||
140 | } |
||
141 | |||
142 | /**
|
||
143 | * <p>Default constructor.</p>
|
||
144 | *
|
||
145 | * @param reference object that is ''semantically' or 'contextually' related to the group of panels
|
||
146 | */
|
||
147 | public TreePanel(Object reference) { |
||
148 | super(reference);
|
||
149 | |||
150 | initialize(); |
||
151 | } |
||
152 | |||
153 | /*
|
||
154 | * (non-Javadoc)
|
||
155 | * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#initialize()
|
||
156 | */
|
||
157 | protected void initialize() { |
||
158 | super.initialize();
|
||
159 | |||
160 | currentDividerLocation = -1;
|
||
161 | |||
162 | panelsInGUICount = 0;
|
||
163 | this.setLayout(new BorderLayout()); |
||
164 | this.add(getJSplitPane(), BorderLayout.CENTER); |
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * <p>This method initializes <code>jSplitPane</code>.</p>
|
||
169 | *
|
||
170 | * @return JSplitPane
|
||
171 | */
|
||
172 | protected JSplitPane getJSplitPane() { |
||
173 | if (jSplitPane == null) { |
||
174 | jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); |
||
175 | jSplitPane.setDividerSize(DEFAULT_DIVIDER_WIDTH); |
||
176 | jSplitPane.setLeftComponent(getTreeJScrollPane()); |
||
177 | jSplitPane.setRightComponent(getDefaultPanel()); |
||
178 | jSplitPane.setOneTouchExpandable(true);
|
||
179 | jSplitPane.setDividerLocation(180);
|
||
180 | } |
||
181 | |||
182 | return jSplitPane;
|
||
183 | } |
||
184 | |||
185 | /**
|
||
186 | * <p>This method initializes <code>defaultPanel</code>.</p>
|
||
187 | *
|
||
188 | * @return AbstractPanel inner panel that is stored as a right panel of the <code>JSplitPane</code> object
|
||
189 | * when this object is created
|
||
190 | */
|
||
191 | protected AbstractPanel getDefaultPanel() {
|
||
192 | if (defaultPanel == null) { |
||
193 | defaultPanel = new DefaultPanel();
|
||
194 | } |
||
195 | |||
196 | return defaultPanel;
|
||
197 | } |
||
198 | |||
199 | /**
|
||
200 | * <p>This method initializes <code>jTree</code>.</p>
|
||
201 | *
|
||
202 | * @return JTree
|
||
203 | */
|
||
204 | protected JTree getJTree() { |
||
205 | if (jTree == null) { |
||
206 | jTree = new JTree(getTreeModel()); |
||
207 | |||
208 | // Single Selection:
|
||
209 | jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||
210 | |||
211 | // Remove icons:
|
||
212 | DefaultTreeCellRenderer defaultTreeCellRenderer = new DefaultTreeCellRenderer(); |
||
213 | defaultTreeCellRenderer.setOpenIcon(null);
|
||
214 | defaultTreeCellRenderer.setClosedIcon(null);
|
||
215 | defaultTreeCellRenderer.setLeafIcon(null);
|
||
216 | |||
217 | // Add new icons for the node-branches that can be expanded or collapsed
|
||
218 | ImageIcon treeCellRendererIcon = new ImageIcon(TreePanel.class.getResource("images/treenodecollapsibleicon.png"), null); |
||
219 | defaultTreeCellRenderer.setOpenIcon(treeCellRendererIcon); |
||
220 | treeCellRendererIcon = new ImageIcon(TreePanel.class.getResource("images/treenodeexpandableicon.png"), null); |
||
221 | defaultTreeCellRenderer.setClosedIcon(treeCellRendererIcon); |
||
222 | |||
223 | // Root not visible
|
||
224 | jTree.setRootVisible(false);
|
||
225 | jTree.setCellRenderer(defaultTreeCellRenderer); |
||
226 | |||
227 | // Expand only the root node
|
||
228 | // Adds support for notification that a node has been adder or removed using a ''ContainerEvent''
|
||
229 | // which is used in other AbstractPanelGruop implementations:
|
||
230 | jTree.getModel().addTreeModelListener(new TreeModelListener() { |
||
231 | /*
|
||
232 | * (non-Javadoc)
|
||
233 | * @see org.apache.log4j.lf5.viewer.categoryexplorer.TreeModelAdapter#treeNodesInserted(javax.swing.event.TreeModelEvent)
|
||
234 | */
|
||
235 | public void treeNodesInserted(TreeModelEvent e) { |
||
236 | // Expand only the root node
|
||
237 | TreePath rootNodePath = new TreePath(getRootNode()); |
||
238 | |||
239 | if (jTree.isCollapsed(rootNodePath))
|
||
240 | jTree.expandPath(rootNodePath); |
||
241 | |||
242 | // Only notify as ContainerEvent the insertion of a IPanel
|
||
243 | if (((DefaultMutableTreeNode)lastNode).getUserObject() instanceof IPanel) |
||
244 | dispatchEvent(new ContainerEvent(jTree, ContainerEvent.COMPONENT_ADDED, (Component)((DefaultMutableTreeNode)lastNode).getUserObject())); |
||
245 | |||
246 | lastNode = null;
|
||
247 | } |
||
248 | |||
249 | /*
|
||
250 | * (non-Javadoc)
|
||
251 | * @see org.apache.log4j.lf5.viewer.categoryexplorer.TreeModelAdapter#treeNodesRemoved(javax.swing.event.TreeModelEvent)
|
||
252 | */
|
||
253 | public void treeNodesRemoved(TreeModelEvent e) { |
||
254 | // Expand only the root node
|
||
255 | TreePath rootNodePath = new TreePath(getRootNode()); |
||
256 | |||
257 | if (jTree.isCollapsed(rootNodePath))
|
||
258 | jTree.expandPath(rootNodePath); |
||
259 | |||
260 | // Only notify as ContainerEvent the insertion of a IPanel
|
||
261 | if (((DefaultMutableTreeNode)lastNode).getUserObject() instanceof IPanel) |
||
262 | dispatchEvent(new ContainerEvent(jTree, ContainerEvent.COMPONENT_REMOVED, (Component)((DefaultMutableTreeNode)lastNode).getUserObject())); |
||
263 | |||
264 | lastNode = null;
|
||
265 | } |
||
266 | |||
267 | public void treeNodesChanged(TreeModelEvent e) { |
||
268 | // TODO Auto-generated method stub
|
||
269 | |||
270 | } |
||
271 | |||
272 | public void treeStructureChanged(TreeModelEvent e) { |
||
273 | // TODO Auto-generated method stub
|
||
274 | |||
275 | } |
||
276 | }); |
||
277 | |||
278 | // Set to the right panel of the JSplitPane, the AbstractPanel selected
|
||
279 | // Adds support for notification that a node has been selected -> set the panel associated to
|
||
280 | // the right panel of the jSplitPane
|
||
281 | jTree.addTreeSelectionListener(new TreeSelectionListener() { |
||
282 | /*
|
||
283 | * (non-Javadoc)
|
||
284 | * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
|
||
285 | */
|
||
286 | public void valueChanged(TreeSelectionEvent e) { |
||
287 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent(); |
||
288 | |||
289 | /* If nothing is selected */
|
||
290 | if (node == null) |
||
291 | return;
|
||
292 | |||
293 | /* Retrieve the node that was selected and set it to the right panel of the JSPlitPane */
|
||
294 | Object object = node.getUserObject();
|
||
295 | |||
296 | if ((object != null) && (object instanceof AbstractPanel)) { |
||
297 | getJSplitPane().setRightComponent((Component) object);
|
||
298 | |||
299 | // Notifies the new panel selected
|
||
300 | stateChanged(new ChangeEvent(object)); |
||
301 | |||
302 | getJSplitPane().setDividerLocation(currentDividerLocation); |
||
303 | } |
||
304 | } |
||
305 | }); |
||
306 | } |
||
307 | |||
308 | return jTree;
|
||
309 | } |
||
310 | |||
311 | /**
|
||
312 | * <p>This method initializes <code>treeModel</code>.</p>
|
||
313 | *
|
||
314 | * @return DefaultTreeModel
|
||
315 | */
|
||
316 | protected DefaultTreeModel getTreeModel() { |
||
317 | if (treeModel == null) { |
||
318 | treeModel = new DefaultTreeModel(getRootNode()); |
||
319 | } |
||
320 | |||
321 | return treeModel;
|
||
322 | } |
||
323 | |||
324 | /**
|
||
325 | * <p>This method initializes <code>rootNode</code>.</p>
|
||
326 | *
|
||
327 | * @return DefaultMutableTreeNode
|
||
328 | */
|
||
329 | protected DefaultMutableTreeNode getRootNode() { |
||
330 | if (rootNode == null) |
||
331 | rootNode = new DefaultMutableTreeNode(rootNodeName); |
||
332 | |||
333 | return rootNode;
|
||
334 | } |
||
335 | |||
336 | /**
|
||
337 | * <p>This method initialiazes <code>jScrollPane</code>.</p>
|
||
338 | *
|
||
339 | * @return JScrollPane
|
||
340 | */
|
||
341 | protected JScrollPane getTreeJScrollPane() { |
||
342 | if (treeJScrollPane == null) { |
||
343 | treeJScrollPane = new JScrollPane(); |
||
344 | treeJScrollPane.setViewportView(getJTree()); |
||
345 | } |
||
346 | |||
347 | return treeJScrollPane;
|
||
348 | } |
||
349 | |||
350 | /*
|
||
351 | * (non-Javadoc)
|
||
352 | * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#loadPanel(org.gvsig.gui.beans.panelGroup.panels.IPanel)
|
||
353 | */
|
||
354 | protected void loadPanel(IPanel panel) { |
||
355 | super.loadPanel(panel);
|
||
356 | |||
357 | AbstractPanel panel_obj = (AbstractPanel)panel; |
||
358 | |||
359 | if (panel_obj.isVisible()) {
|
||
360 | addPanelImpl(panel); |
||
361 | |||
362 | // Recalculates preferred sizes (of the right component of the JSPlitPane), and then resizes also this one if it's necessary
|
||
363 | Dimension panelPreferredSize = panel_obj.getPreferredSize();
|
||
364 | Dimension rightPanelPreferredSize = getJSplitPane().getRightComponent().getPreferredSize();
|
||
365 | |||
366 | if ((panelPreferredSize.width > rightPanelPreferredSize.width) || (panelPreferredSize.height > rightPanelPreferredSize.height)) {
|
||
367 | getJSplitPane().getRightComponent().setPreferredSize(new Dimension(Math.max(panelPreferredSize.width, rightPanelPreferredSize.width), Math.max(panelPreferredSize.height, rightPanelPreferredSize.height))); |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | |||
372 | /**
|
||
373 | * <p>Has the algorithms that allows adding a panel to the JTree component.</p>
|
||
374 | *
|
||
375 | * @param panel the panel to add
|
||
376 | *
|
||
377 | * @see #addPanel(IPanel)
|
||
378 | */
|
||
379 | protected void addPanelImpl(IPanel panel) { |
||
380 | if (belongsThisGroup(panel)) {
|
||
381 | |||
382 | // Don't add the panel if hasn't defined the 'label' attribute
|
||
383 | if (panel.getLabel() == null) |
||
384 | return;
|
||
385 | |||
386 | DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(panel); |
||
387 | lastNode = new_node; |
||
388 | |||
389 | // Case 1 -> the attribute 'labelGroup' is undefined
|
||
390 | if (panel.getLabelGroup() == null) { |
||
391 | getTreeModel().insertNodeInto(new_node, getRootNode(), getRootNode().getChildCount()); |
||
392 | panelsInGUICount ++; |
||
393 | return;
|
||
394 | } |
||
395 | |||
396 | // Case 2 -> Check if already exists a node with the same 'labelGroup' attribute than the panel
|
||
397 | DefaultMutableTreeNode node;
|
||
398 | |||
399 | for (short i = 0; i < getRootNode().getChildCount(); i ++) { |
||
400 | node = (DefaultMutableTreeNode) getRootNode().getChildAt(i);
|
||
401 | |||
402 | if (node.getUserObject().toString().equals(panel.getLabelGroup())) {
|
||
403 | getTreeModel().insertNodeInto(new_node, node, node.getChildCount()); |
||
404 | panelsInGUICount ++; |
||
405 | return;
|
||
406 | } |
||
407 | } |
||
408 | |||
409 | // Case 3 -> Normal insertion
|
||
410 | DefaultMutableTreeNode parent = new DefaultMutableTreeNode(panel.getLabelGroup()); |
||
411 | lastNode = parent; |
||
412 | getTreeModel().insertNodeInto(parent, getRootNode(), getRootNode().getChildCount()); |
||
413 | |||
414 | lastNode = new_node; |
||
415 | getTreeModel().insertNodeInto(new_node, parent, 0);
|
||
416 | panelsInGUICount ++; |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /**
|
||
421 | * <p>Has the algorithms that allows adding a panel to the JTree component, considering the position
|
||
422 | * of that panel in the group, and the group position in the JTree.</p>
|
||
423 | *
|
||
424 | * @param panel the panel to add
|
||
425 | *
|
||
426 | * @see #addPanel(IPanel)
|
||
427 | */
|
||
428 | protected void addPanelSortOrdered(IPanel panel) { |
||
429 | if (belongsThisGroup(panel)) {
|
||
430 | |||
431 | // Don't add the panel if hasn't defined the 'label' attribute
|
||
432 | if (panel.getLabel() == null) |
||
433 | return;
|
||
434 | |||
435 | short p_index = (short)registeredPanels.indexOf(panel); |
||
436 | |||
437 | DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(panel); |
||
438 | lastNode = new_node; |
||
439 | |||
440 | // String label;
|
||
441 | short l_index = 0; |
||
442 | short i;
|
||
443 | |||
444 | // Case 1 -> the attribute 'labelGroup' is undefined (null)
|
||
445 | if (panel.getLabelGroup() == null) { |
||
446 | Enumeration labelsAndGroups = rootNode.children();
|
||
447 | |||
448 | for (i = 0; i < p_index; i++) { |
||
449 | if (registeredPanels.get(i).getLabelGroup() == null) { |
||
450 | if (labelsAndGroups.hasMoreElements()) {
|
||
451 | if (((DefaultMutableTreeNode)labelsAndGroups.nextElement()).getUserObject().equals(registeredPanels.get(i))) |
||
452 | l_index ++; |
||
453 | } |
||
454 | else {
|
||
455 | break;
|
||
456 | } |
||
457 | } |
||
458 | else {
|
||
459 | if (labelsAndGroups.hasMoreElements()) {
|
||
460 | if (((DefaultMutableTreeNode)labelsAndGroups.nextElement()).getUserObject().equals(registeredPanels.get(i).getLabelGroup())) |
||
461 | l_index ++; |
||
462 | } |
||
463 | else {
|
||
464 | break;
|
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | |||
469 | // Insert the node
|
||
470 | getTreeModel().insertNodeInto(new_node, getRootNode(), l_index); |
||
471 | panelsInGUICount ++; |
||
472 | return;
|
||
473 | } |
||
474 | |||
475 | // Case 2 -> Check if already exists a node with the same 'labelGroup' attribute than the panel
|
||
476 | DefaultMutableTreeNode node = null; |
||
477 | Enumeration groups = rootNode.children();
|
||
478 | Enumeration labels;
|
||
479 | |||
480 | // Find the node with the label group
|
||
481 | while (groups.hasMoreElements()) {
|
||
482 | node = (DefaultMutableTreeNode) groups.nextElement();
|
||
483 | |||
484 | if ((node.getUserObject()).equals(panel.getLabelGroup())) {
|
||
485 | break;
|
||
486 | } |
||
487 | else {
|
||
488 | node = null;
|
||
489 | } |
||
490 | } |
||
491 | |||
492 | if (node != null) { |
||
493 | labels = node.children(); |
||
494 | l_index = 0;
|
||
495 | |||
496 | // Avoid adding two times the same node
|
||
497 | while (labels.hasMoreElements()) {
|
||
498 | if (((DefaultMutableTreeNode)labels.nextElement()).getUserObject().equals(panel)) |
||
499 | return;
|
||
500 | } |
||
501 | |||
502 | labels = node.children(); |
||
503 | Object leaf_panel = ((DefaultMutableTreeNode) labels.nextElement()).getUserObject(); |
||
504 | |||
505 | for (i = 0; i < p_index; i++) { |
||
506 | if (registeredPanels.get(i).getLabelGroup() == panel.getLabelGroup()) {
|
||
507 | if (registeredPanels.get(i).equals(leaf_panel)) {
|
||
508 | l_index ++; |
||
509 | |||
510 | if (! labels.hasMoreElements())
|
||
511 | break;
|
||
512 | |||
513 | leaf_panel = ((DefaultMutableTreeNode) labels.nextElement()).getUserObject();
|
||
514 | } |
||
515 | } |
||
516 | } |
||
517 | |||
518 | // Insert the node
|
||
519 | getTreeModel().insertNodeInto(new_node, node, l_index); |
||
520 | panelsInGUICount ++; |
||
521 | return;
|
||
522 | } |
||
523 | |||
524 | // Case 3 -> Normal insertion
|
||
525 | |||
526 | // Check the position of the 'labelGroup' node:
|
||
527 | short g_index = 0; |
||
528 | |||
529 | groups = rootNode.children(); |
||
530 | String c_labelGroup = (String)((DefaultMutableTreeNode)groups.nextElement()).getUserObject(); |
||
531 | String labelGroup;
|
||
532 | |||
533 | for (i = 0; i < p_index; i++) { |
||
534 | labelGroup = registeredPanels.get(i).getLabelGroup(); |
||
535 | |||
536 | if (c_labelGroup == null) { |
||
537 | if (labelGroup == null) { |
||
538 | c_labelGroup = (String)((DefaultMutableTreeNode)groups.nextElement()).getUserObject(); |
||
539 | g_index ++; |
||
540 | } |
||
541 | } |
||
542 | else {
|
||
543 | if (c_labelGroup.equals(labelGroup))
|
||
544 | { |
||
545 | DefaultMutableTreeNode dmtnode = ((DefaultMutableTreeNode)groups.nextElement()); |
||
546 | c_labelGroup = (String)dmtnode.getUserObject().toString();
|
||
547 | g_index ++; |
||
548 | } |
||
549 | } |
||
550 | } |
||
551 | |||
552 | // Insert the node
|
||
553 | DefaultMutableTreeNode parent = new DefaultMutableTreeNode(panel.getLabelGroup()); |
||
554 | lastNode = parent; |
||
555 | getTreeModel().insertNodeInto(parent, getRootNode(), g_index); |
||
556 | |||
557 | lastNode = new_node; |
||
558 | getTreeModel().insertNodeInto(new_node, parent, 0);
|
||
559 | panelsInGUICount ++; |
||
560 | } |
||
561 | } |
||
562 | |||
563 | /*
|
||
564 | * (non-Javadoc)
|
||
565 | * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#unLoadPanel(org.gvsig.gui.beans.panelGroup.panels.IPanel)
|
||
566 | */
|
||
567 | protected void unLoadPanel(IPanel panel) { |
||
568 | super.unLoadPanel(panel);
|
||
569 | |||
570 | removePanelImpl(panel); |
||
571 | } |
||
572 | |||
573 | /**
|
||
574 | * <p>Has the algorithms that allows removing a panel from the JTree component.</p>
|
||
575 | *
|
||
576 | * @param panel the panel to remove
|
||
577 | *
|
||
578 | * @see #removePanel(IPanel)
|
||
579 | */
|
||
580 | protected void removePanelImpl(IPanel panel) { |
||
581 | if (belongsThisGroup(panel)) {
|
||
582 | |||
583 | // Don't add the panel if hasn't defined the 'label' attribute
|
||
584 | if (panel.getLabel() == null) |
||
585 | return;
|
||
586 | |||
587 | DefaultMutableTreeNode node;
|
||
588 | |||
589 | // Case 1 -> the attribute 'labelGroup' is undefined
|
||
590 | if (panel.getLabelGroup() == null) { |
||
591 | for (short i = 0; i < getRootNode().getChildCount(); i ++) { |
||
592 | node = (DefaultMutableTreeNode) getRootNode().getChildAt(i);
|
||
593 | |||
594 | if ((node.getUserObject().toString().equals(panel.getLabel())) && (node.getChildCount() == 0)) { |
||
595 | lastNode = node; |
||
596 | getTreeModel().removeNodeFromParent(node); |
||
597 | panelsInGUICount --; |
||
598 | return;
|
||
599 | } |
||
600 | } |
||
601 | |||
602 | return;
|
||
603 | } |
||
604 | |||
605 | DefaultMutableTreeNode parent;
|
||
606 | |||
607 | // Case 2 -> Check if already exists a node with the same 'labelGroup' and 'label' attributes than the panel
|
||
608 | for (short i = 0; i < getRootNode().getChildCount(); i ++) { |
||
609 | parent = (DefaultMutableTreeNode) getRootNode().getChildAt(i);
|
||
610 | |||
611 | if ((parent.getChildCount() > 0) && (parent.getUserObject().toString().equals(panel.getLabelGroup()))) { |
||
612 | for (short j = 0; j < parent.getChildCount(); j ++) { |
||
613 | node = (DefaultMutableTreeNode)parent.getChildAt(j);
|
||
614 | |||
615 | if (node.getUserObject().toString().equals(panel.getLabel())) {
|
||
616 | if (parent.getChildCount() == 1) { // Case 3 -> If there is only one panel in that 'labelGroup' |
||
617 | lastNode = parent; |
||
618 | getTreeModel().removeNodeFromParent(parent); |
||
619 | panelsInGUICount --; |
||
620 | } |
||
621 | else {
|
||
622 | lastNode = node; |
||
623 | getTreeModel().removeNodeFromParent(node); |
||
624 | panelsInGUICount --; |
||
625 | } |
||
626 | } |
||
627 | } |
||
628 | return;
|
||
629 | } |
||
630 | } |
||
631 | } |
||
632 | } |
||
633 | |||
634 | /**
|
||
635 | * @see JTree#getSelectionPath()
|
||
636 | */
|
||
637 | public TreePath getSelectionPath() { |
||
638 | return getJTree().getSelectionPath();
|
||
639 | } |
||
640 | |||
641 | /*
|
||
642 | * (non-Javadoc)
|
||
643 | * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#getActivePanel()
|
||
644 | */
|
||
645 | public IPanel getActivePanel() {
|
||
646 | if (registeredPanels.size() == 0) |
||
647 | return null; |
||
648 | |||
649 | Object node = ((DefaultMutableTreeNode)getJTree().getLastSelectedPathComponent()).getUserObject(); |
||
650 | |||
651 | if (node instanceof IPanel) |
||
652 | return (IPanel)node;
|
||
653 | else
|
||
654 | return null; |
||
655 | } |
||
656 | |||
657 | /*
|
||
658 | * (non-Javadoc)
|
||
659 | * @see org.gvsig.gui.beans.panelGroup.AbstractPanelGroup#stateChanged(javax.swing.event.ChangeEvent)
|
||
660 | */
|
||
661 | public void stateChanged(ChangeEvent e) { |
||
662 | if (registeredPanels.size() == 0) |
||
663 | return;
|
||
664 | |||
665 | Object object = e.getSource();
|
||
666 | |||
667 | if ((object != null) && (object instanceof IPanel)) { |
||
668 | ((IPanel) object).selected(); |
||
669 | |||
670 | // Get the current divider location to hold it after the selected panel will be set to the interface of the inner JTreePanel
|
||
671 | currentDividerLocation = getJSplitPane().getDividerLocation(); |
||
672 | } |
||
673 | } |
||
674 | |||
675 | /*
|
||
676 | * (non-Javadoc)
|
||
677 | * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#updatePanelVisibility(org.gvsig.gui.beans.panelGroup.panels.IPanel, boolean)
|
||
678 | */
|
||
679 | public synchronized void setPanelInGUI(IPanel panel, boolean b) { |
||
680 | if (registeredPanels.size() == 0) |
||
681 | return;
|
||
682 | |||
683 | if (registeredPanels.indexOf(panel) == -1) |
||
684 | return;
|
||
685 | |||
686 | if (b == true) { |
||
687 | addPanelSortOrdered((AbstractPanel)panel); |
||
688 | } |
||
689 | else {
|
||
690 | removePanelImpl((AbstractPanel)panel); |
||
691 | } |
||
692 | |||
693 | repaint(); |
||
694 | } |
||
695 | |||
696 | /*
|
||
697 | * (non-Javadoc)
|
||
698 | * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#isPanelInGUI(org.gvsig.gui.beans.panelGroup.panels.IPanel)
|
||
699 | */
|
||
700 | public synchronized boolean isPanelInGUI(IPanel panel) { |
||
701 | if (!registeredPanels.contains(panel))
|
||
702 | return false; |
||
703 | |||
704 | DefaultMutableTreeNode node;
|
||
705 | |||
706 | for (int i = 0; i < getRootNode().getChildCount(); i++) { |
||
707 | node = (DefaultMutableTreeNode) getRootNode().getChildAt(i);
|
||
708 | |||
709 | if (node.getChildCount() == 0) { |
||
710 | if (node.getUserObject().equals(panel))
|
||
711 | return true; |
||
712 | } |
||
713 | else {
|
||
714 | if (isPanelAtBranch(panel, node))
|
||
715 | return true; |
||
716 | } |
||
717 | } |
||
718 | |||
719 | return false; |
||
720 | } |
||
721 | |||
722 | /**
|
||
723 | * <p>Returns <code>true</code> if the panel is a child of the tree node as parameter.</p>
|
||
724 | */
|
||
725 | protected boolean isPanelAtBranch(IPanel panel, DefaultMutableTreeNode node) { |
||
726 | DefaultMutableTreeNode child;
|
||
727 | |||
728 | for (int i = 0; i < node.getChildCount(); i++) { |
||
729 | child = (DefaultMutableTreeNode) node.getChildAt(i);
|
||
730 | |||
731 | if (child.getUserObject().equals(panel))
|
||
732 | return true; |
||
733 | } |
||
734 | |||
735 | return false; |
||
736 | } |
||
737 | |||
738 | /**
|
||
739 | * @see JTree#addTreeSelectionListener(TreeSelectionListener)
|
||
740 | */
|
||
741 | public void addTreeSelectionListener(TreeSelectionListener tsl) { |
||
742 | getJTree().addTreeSelectionListener(tsl); |
||
743 | } |
||
744 | |||
745 | /**
|
||
746 | * @see JTree#removeTreeSelectionListener(TreeSelectionListener)
|
||
747 | */
|
||
748 | public void removeTreeSelectionListener(TreeSelectionListener tsl) { |
||
749 | getJTree().removeTreeSelectionListener(tsl); |
||
750 | } |
||
751 | |||
752 | /**
|
||
753 | * @see JTree#getTreeSelectionListeners()
|
||
754 | */
|
||
755 | public TreeSelectionListener[] getTreeSelectionListeners() { |
||
756 | return getJTree().getTreeSelectionListeners();
|
||
757 | } |
||
758 | |||
759 | /*
|
||
760 | * (non-Javadoc)
|
||
761 | * @see org.gvsig.gui.beans.panelGroup.IPanelGroup#getPanelInGUICount()
|
||
762 | */
|
||
763 | public int getPanelInGUICount() { |
||
764 | return panelsInGUICount;
|
||
765 | } |
||
766 | |||
767 | /**
|
||
768 | * @see JSplitPane#getDividerLocation()
|
||
769 | */
|
||
770 | public int getDividerLocation() { |
||
771 | return getJSplitPane().getDividerLocation();
|
||
772 | } |
||
773 | |||
774 | /**
|
||
775 | * @see JSplitPane#setDividerLocation(int)
|
||
776 | */
|
||
777 | public void setDividerLocation(int location) { |
||
778 | getJSplitPane().setDividerLocation(location); |
||
779 | } |
||
780 | |||
781 | /**
|
||
782 | * @see JSplitPane#getDividerSize()
|
||
783 | */
|
||
784 | public int getDividerSize() { |
||
785 | return getJSplitPane().getDividerSize();
|
||
786 | } |
||
787 | |||
788 | /**
|
||
789 | * @see JSplitPane#setDividerSize()
|
||
790 | */
|
||
791 | public void setDividerSize(int newSize) { |
||
792 | getJSplitPane().setDividerSize(newSize); |
||
793 | } |
||
794 | |||
795 | /**
|
||
796 | * <p>A trivial implementation of {@link AbstractPanel AbstractPanel} used in the
|
||
797 | * initialization of the <code>jSplitPane</code> attribute.</p>
|
||
798 | *
|
||
799 | * @version 15/10/2007
|
||
800 | * @author Pablo Piqueras Bartolom? (pablo.piqueras@iver.es)
|
||
801 | */
|
||
802 | protected class DefaultPanel extends AbstractPanel { |
||
803 | /**
|
||
804 | * @see AbstractPanel#AbstractPanel()
|
||
805 | */
|
||
806 | public DefaultPanel() {
|
||
807 | super();
|
||
808 | } |
||
809 | |||
810 | /**
|
||
811 | * @see AbstractPanel#AbstractPanel(String, String, String)
|
||
812 | */
|
||
813 | public DefaultPanel(String id, String label, String labelGroup) { |
||
814 | super(id, label, labelGroup);
|
||
815 | } |
||
816 | |||
817 | /*
|
||
818 | * (non-Javadoc)
|
||
819 | * @see org.gvsig.gui.beans.panelGroup.panels.AbstractPanel#initialize()
|
||
820 | */
|
||
821 | protected void initialize() { |
||
822 | } |
||
823 | |||
824 | /*
|
||
825 | * (non-Javadoc)
|
||
826 | * @see org.gvsig.gui.beans.panelGroup.panels.IPanel#accept()
|
||
827 | */
|
||
828 | public void accept() { |
||
829 | } |
||
830 | |||
831 | /*
|
||
832 | * (non-Javadoc)
|
||
833 | * @see org.gvsig.gui.beans.panelGroup.panels.IPanel#apply()
|
||
834 | */
|
||
835 | public void apply() { |
||
836 | } |
||
837 | |||
838 | public void cancel() { |
||
839 | } |
||
840 | |||
841 | public void selected() { |
||
842 | } |
||
843 | } |
||
844 | } |