svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / LayersTreeModel.java @ 42651
History | View | Annotate | Download (2.38 KB)
1 | 42651 | jjdelcerro | package org.gvsig.fmap.mapcontrol; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.Iterator; |
||
5 | import java.util.List; |
||
6 | import javax.swing.event.TreeModelEvent; |
||
7 | import javax.swing.event.TreeModelListener; |
||
8 | import javax.swing.tree.TreeModel; |
||
9 | import javax.swing.tree.TreePath; |
||
10 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
11 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
12 | |||
13 | |||
14 | public class LayersTreeModel implements TreeModel { |
||
15 | |||
16 | protected final FLayers layers; |
||
17 | |||
18 | protected final List<TreeModelListener> listeners = new ArrayList(); |
||
19 | |||
20 | public LayersTreeModel(FLayers layers) {
|
||
21 | this.layers = layers;
|
||
22 | } |
||
23 | |||
24 | @Override
|
||
25 | public Object getRoot() { |
||
26 | return layers;
|
||
27 | } |
||
28 | |||
29 | @Override
|
||
30 | public Object getChild(Object parent, int index) { |
||
31 | if (!(parent instanceof FLayers)) { |
||
32 | return null; |
||
33 | } |
||
34 | return ((FLayers) parent).getLayer(index);
|
||
35 | } |
||
36 | |||
37 | @Override
|
||
38 | public int getChildCount(Object parent) { |
||
39 | if (!(parent instanceof FLayers)) { |
||
40 | return 0; |
||
41 | } |
||
42 | return ((FLayers) parent).getLayersCount();
|
||
43 | } |
||
44 | |||
45 | @Override
|
||
46 | public boolean isLeaf(Object node) { |
||
47 | return !(node instanceof FLayers); |
||
48 | } |
||
49 | |||
50 | @Override
|
||
51 | public int getIndexOfChild(Object parent, Object child) { |
||
52 | for( int i=0; i<((FLayers)parent).getLayersCount() ; i++ ) { |
||
53 | FLayer layer = ((FLayers)parent).getLayer(i); |
||
54 | if( child == layer ) {
|
||
55 | return i;
|
||
56 | } |
||
57 | } |
||
58 | return -1; |
||
59 | } |
||
60 | |||
61 | @Override
|
||
62 | public void valueForPathChanged(TreePath path, Object value) { |
||
63 | } |
||
64 | |||
65 | @Override
|
||
66 | public void addTreeModelListener(TreeModelListener listener) { |
||
67 | listeners.add(listener); |
||
68 | } |
||
69 | |||
70 | @Override
|
||
71 | public void removeTreeModelListener(TreeModelListener listener) { |
||
72 | listeners.remove(listener); |
||
73 | } |
||
74 | |||
75 | public void reload() { |
||
76 | TreeModelEvent event = new TreeModelEvent(this, new TreePath(this.layers)); |
||
77 | Iterator<TreeModelListener> iterator = listeners.iterator(); |
||
78 | TreeModelListener listener;
|
||
79 | while (iterator.hasNext()) {
|
||
80 | listener = iterator.next(); |
||
81 | listener.treeStructureChanged(event); |
||
82 | } |
||
83 | } |
||
84 | } |