svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / TOC.java @ 403
History | View | Annotate | Download (9.67 KB)
1 |
/*
|
---|---|
2 |
* Created on 13-may-2004
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
package com.iver.cit.gvsig.gui; |
8 |
|
9 |
|
10 |
import java.awt.BorderLayout; |
11 |
import java.awt.Dimension; |
12 |
import java.awt.Point; |
13 |
import java.awt.Rectangle; |
14 |
import java.awt.event.ActionEvent; |
15 |
import java.awt.event.ActionListener; |
16 |
import java.awt.event.InputEvent; |
17 |
import java.awt.event.MouseAdapter; |
18 |
import java.awt.event.MouseEvent; |
19 |
import java.util.ArrayList; |
20 |
|
21 |
import javax.swing.BorderFactory; |
22 |
import javax.swing.JColorChooser; |
23 |
import javax.swing.JComponent; |
24 |
import javax.swing.JDialog; |
25 |
import javax.swing.JScrollPane; |
26 |
import javax.swing.JTree; |
27 |
import javax.swing.UIManager; |
28 |
import javax.swing.tree.DefaultMutableTreeNode; |
29 |
import javax.swing.tree.DefaultTreeModel; |
30 |
import javax.swing.tree.TreePath; |
31 |
import javax.swing.tree.TreeSelectionModel; |
32 |
|
33 |
import com.iver.cit.gvsig.fmap.FMap; |
34 |
import com.iver.cit.gvsig.fmap.NewMapControl; |
35 |
import com.iver.cit.gvsig.fmap.layers.CancelationException; |
36 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
37 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
38 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
39 |
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend; |
40 |
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend; |
41 |
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D; |
42 |
import com.iver.cit.gvsig.gui.toc.DnDJTree; |
43 |
import com.iver.cit.gvsig.gui.toc.FPopupMenu; |
44 |
import com.iver.cit.gvsig.gui.toc.FTheme; |
45 |
import com.iver.cit.gvsig.gui.toc.ITocOrderListener; |
46 |
import com.iver.cit.gvsig.gui.toc.TOCRenderer; |
47 |
|
48 |
/**
|
49 |
* @author fjp
|
50 |
*
|
51 |
* To change the template for this generated type comment go to
|
52 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
53 |
*/
|
54 |
public class TOC extends JComponent implements ITocOrderListener { |
55 |
private NewMapControl mapCtrl;
|
56 |
private DnDJTree m_Tree;
|
57 |
private DefaultTreeModel m_TreeModel; |
58 |
private DefaultMutableTreeNode m_Root; |
59 |
private TOCRenderer m_TocRenderer;
|
60 |
private JScrollPane m_Scroller; |
61 |
private ArrayList m_Listeners; |
62 |
|
63 |
/**
|
64 |
* Clase Listener que reacciona al pulsar sobre el checkbox de un nodo y
|
65 |
* crea un popupmenu al pulsar el bot?n derecho.
|
66 |
*/
|
67 |
class NodeSelectionListener extends MouseAdapter implements ActionListener { |
68 |
JTree tree;
|
69 |
JDialog dlg;
|
70 |
JColorChooser colorChooser;
|
71 |
FPopupMenu popmenu = null;
|
72 |
|
73 |
NodeSelectionListener(JTree tree) {
|
74 |
this.tree = tree;
|
75 |
} |
76 |
|
77 |
public void mouseClicked(MouseEvent e) { |
78 |
int x = e.getX();
|
79 |
int y = e.getY();
|
80 |
int row = tree.getRowForLocation(x, y);
|
81 |
TreePath path = tree.getPathForRow(row);
|
82 |
FLayers layers = mapCtrl.getMapContext().getLayers(); |
83 |
// System.out.println(e.getSource());
|
84 |
// Si tenemos may?sculas apretadas es que queremos activar m?s temas.
|
85 |
// Si no, primero desactivamos temas
|
86 |
if (!((e.getModifiers() & InputEvent.SHIFT_MASK) != 0)) |
87 |
{ |
88 |
layers.setActive(false);
|
89 |
} |
90 |
|
91 |
if (path != null) { |
92 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); |
93 |
// System.out.println("Evento de rat?n originado por " + e.getSource().getClass().toString());
|
94 |
|
95 |
if (node.getUserObject() instanceof FTheme) { |
96 |
FTheme elTema = (FTheme) node.getUserObject(); |
97 |
FLayer lyr = elTema.getLayer(); |
98 |
lyr.setActive(true);
|
99 |
// Si pertenece a un grupo, lo ponemos activo tambi?n.
|
100 |
FLayer parentLayer = lyr.getParentLayer(); |
101 |
if (parentLayer != null){ |
102 |
parentLayer.setActive(true);
|
103 |
} |
104 |
|
105 |
Point layerNodeLocation = tree.getUI()
|
106 |
.getPathBounds(tree, path) |
107 |
.getLocation(); |
108 |
|
109 |
//Rect?ngulo que representa el checkbox
|
110 |
Rectangle checkBoxBounds = m_TocRenderer.getCheckBoxBounds();
|
111 |
checkBoxBounds.translate((int) layerNodeLocation.getX(),
|
112 |
(int) layerNodeLocation.getY());
|
113 |
|
114 |
if (checkBoxBounds.contains(e.getPoint())) {
|
115 |
lyr.setVisible(!lyr.isVisible()); |
116 |
mapCtrl.drawMap(); |
117 |
} |
118 |
|
119 |
//Boton derecho sobre un nodo del arbol
|
120 |
if ((e.getModifiers() & InputEvent.META_MASK) != 0) { |
121 |
//TODO quitar el siguiente comentario
|
122 |
/* popmenu = new FPopupMenu(m_View, node);
|
123 |
tree.add(popmenu);
|
124 |
|
125 |
//System.out.println("boton derecho");
|
126 |
popmenu.show(e.getComponent(), e.getX(), e.getY());
|
127 |
*/ }
|
128 |
|
129 |
} |
130 |
|
131 |
//TODO quitar el siguiente comentario
|
132 |
/*
|
133 |
if (node.getUserObject() instanceof FSymbol)
|
134 |
{
|
135 |
//Boton derecho sobre un Simbolo
|
136 |
if ((e.getModifiers() & InputEvent.META_MASK) != 0) {
|
137 |
popmenu = new FPopupMenu(m_View, node);
|
138 |
tree.add(popmenu);
|
139 |
popmenu.show(e.getComponent(), e.getX(), e.getY());
|
140 |
}
|
141 |
}
|
142 |
*/
|
143 |
((DefaultTreeModel) tree.getModel()).nodeChanged(node);
|
144 |
|
145 |
if (row == 0) { |
146 |
tree.revalidate(); |
147 |
tree.repaint(); |
148 |
} |
149 |
|
150 |
} |
151 |
} |
152 |
|
153 |
public void actionPerformed(ActionEvent arg0) { |
154 |
} |
155 |
} |
156 |
|
157 |
|
158 |
public TOC(NewMapControl associatedMapControl) {
|
159 |
|
160 |
this.mapCtrl = associatedMapControl;
|
161 |
|
162 |
this.setLayout(new BorderLayout()); |
163 |
this.setMinimumSize(new Dimension(80,80)); |
164 |
// this.setPreferredSize(new Dimension(80,80));
|
165 |
|
166 |
//Construct the tree.
|
167 |
m_Root = new DefaultMutableTreeNode(java.lang.Object.class); |
168 |
m_TreeModel = new DefaultTreeModel(m_Root); |
169 |
m_Tree = new DnDJTree(m_TreeModel);
|
170 |
|
171 |
m_TocRenderer = new TOCRenderer();
|
172 |
m_Tree.setCellRenderer(m_TocRenderer); |
173 |
|
174 |
m_Tree.setRootVisible(false);
|
175 |
// m_Tree.setExpandsSelectedPaths(true);
|
176 |
// m_Tree.setAutoscrolls(true);
|
177 |
m_Tree.setShowsRootHandles(true);
|
178 |
//Posibilidad de seleccionar de forma aleatoria nodos de la leyenda.
|
179 |
m_Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
|
180 |
m_Tree.addMouseListener(new NodeSelectionListener(m_Tree));
|
181 |
m_Tree.setBackground(UIManager.getColor("Button.background")); |
182 |
m_Tree.setBorder(BorderFactory.createEtchedBorder());
|
183 |
|
184 |
m_Tree.addOrderListener(this);
|
185 |
|
186 |
m_Tree.setRowHeight(0); // Para que lo determine el renderer |
187 |
|
188 |
|
189 |
m_Scroller = new JScrollPane(m_Tree); |
190 |
m_Scroller.setBorder(BorderFactory.createEmptyBorder());
|
191 |
|
192 |
// scrollPane.setPreferredSize(new Dimension(80,80));
|
193 |
|
194 |
//Add everything to this panel.
|
195 |
/* GridBagLayout gridbag = new GridBagLayout();
|
196 |
GridBagConstraints c = new GridBagConstraints();
|
197 |
|
198 |
setLayout(gridbag);
|
199 |
|
200 |
c.fill = GridBagConstraints.BOTH;
|
201 |
c.weightx = 1.0;
|
202 |
gridbag.setConstraints(check,c); */
|
203 |
|
204 |
add(m_Scroller); //, BorderLayout.WEST);
|
205 |
|
206 |
// refresh();
|
207 |
|
208 |
} |
209 |
|
210 |
|
211 |
|
212 |
/* (non-Javadoc)
|
213 |
* @see com.iver.cit.opensig.gui.IToc#getMap()
|
214 |
*/
|
215 |
public FMap getMapContext() {
|
216 |
return mapCtrl.getMapContext();
|
217 |
} |
218 |
|
219 |
/* (non-Javadoc)
|
220 |
* @see com.iver.cit.opensig.gui.IToc#refresh()
|
221 |
*/
|
222 |
public void refresh() { |
223 |
FLayers theLayers = mapCtrl.getMapContext().getLayers(); |
224 |
m_Root.removeAllChildren(); |
225 |
m_Root.setAllowsChildren(true);
|
226 |
|
227 |
for (int i=theLayers.getLayersCount()-1;i>=0; i--) |
228 |
{ |
229 |
FLayer lyr = theLayers.getLayer(i); |
230 |
FTheme elTema = new FTheme(lyr);
|
231 |
DefaultMutableTreeNode nodeLayer = new DefaultMutableTreeNode(elTema); |
232 |
|
233 |
m_TreeModel.insertNodeInto(nodeLayer, m_Root, m_Root.getChildCount()); |
234 |
TreePath path = new TreePath(m_TreeModel.getPathToRoot(nodeLayer)); |
235 |
m_Tree.makeVisible(path); |
236 |
|
237 |
if (lyr instanceof FLyrVect) |
238 |
{ |
239 |
FLyrVect lyrVect = (FLyrVect) lyr; |
240 |
// CommonOperations aux = (CommonOperations) lyr;
|
241 |
VectorialLegend legend = lyrVect.getLegend(); |
242 |
FStyle2D theSymbol = null;
|
243 |
if (legend instanceof ClassifiedLegend){ |
244 |
ClassifiedLegend cl = (ClassifiedLegend) legend; |
245 |
String[] descriptions = cl.getDescriptions(); |
246 |
FStyle2D[] symbols = cl.getSymbols();
|
247 |
for (int j=0; j<descriptions.length;j++) |
248 |
{ |
249 |
theSymbol = symbols[j]; |
250 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(theSymbol); |
251 |
m_TreeModel.insertNodeInto(nodeValue, nodeLayer, nodeLayer.getChildCount()); |
252 |
TreePath pathSymbol = new TreePath(m_TreeModel.getPathToRoot(nodeValue)); |
253 |
m_Tree.makeVisible(pathSymbol); |
254 |
|
255 |
} |
256 |
} |
257 |
|
258 |
} |
259 |
if (lyr instanceof FLayers) |
260 |
{ |
261 |
FLayers group = (FLayers) lyr; |
262 |
for (int k=0; k < group.getLayersCount(); k++) |
263 |
{ |
264 |
FLayer subLayer = group.getLayer(k); |
265 |
FTheme elSubTema = new FTheme(subLayer);
|
266 |
DefaultMutableTreeNode nodeSubLayer = new DefaultMutableTreeNode(elSubTema); |
267 |
m_TreeModel.insertNodeInto(nodeSubLayer,nodeLayer, nodeLayer.getChildCount()); |
268 |
|
269 |
if (subLayer instanceof FLyrVect) |
270 |
{ |
271 |
FLyrVect lyrVect = (FLyrVect) subLayer; |
272 |
VectorialLegend legend = lyrVect.getLegend(); |
273 |
FStyle2D theSymbol = null;
|
274 |
|
275 |
if (legend instanceof ClassifiedLegend){ |
276 |
ClassifiedLegend cl = (ClassifiedLegend) legend; |
277 |
String[] descriptions = cl.getDescriptions(); |
278 |
FStyle2D[] symbols = cl.getSymbols();
|
279 |
for (int j=0; j< descriptions.length ;j++) |
280 |
{ |
281 |
theSymbol = symbols[j]; |
282 |
DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(theSymbol); |
283 |
m_TreeModel.insertNodeInto(nodeValue, nodeSubLayer, nodeSubLayer.getChildCount()); |
284 |
TreePath pathSymbol = new TreePath(m_TreeModel.getPathToRoot(nodeValue)); |
285 |
m_Tree.makeVisible(pathSymbol); |
286 |
|
287 |
} |
288 |
} // switch
|
289 |
} // if subLayer
|
290 |
} // for k
|
291 |
} // if lyrGroup
|
292 |
|
293 |
} |
294 |
m_TreeModel.reload(); |
295 |
} |
296 |
|
297 |
/**
|
298 |
* @see com.iver.cit.opensig.gui.toc.ITocOrderListener#orderChanged(int, int)
|
299 |
*/
|
300 |
public void orderChanged(int oldPos, int newPos) { |
301 |
FLayers layers = mapCtrl.getMapContext().getLayers(); |
302 |
// El orden es el contrario, hay que traducir.
|
303 |
// El orden es el contrario, hay que traducir.
|
304 |
oldPos = layers.getLayersCount() -1 - oldPos;
|
305 |
newPos = layers.getLayersCount() -1 - newPos;
|
306 |
try {
|
307 |
layers.moveTo(oldPos, newPos); |
308 |
} catch (CancelationException e) {
|
309 |
// TODO Auto-generated catch block
|
310 |
e.printStackTrace(); |
311 |
} |
312 |
refresh(); |
313 |
mapCtrl.drawMap(); |
314 |
} |
315 |
|
316 |
|
317 |
} |