svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / swing / dynobject / impl / DefaultLayersDynObjectSetComponent.java @ 42775
History | View | Annotate | Download (12.1 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 41426 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40435 | jjdelcerro | *
|
11 | 41426 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40435 | jjdelcerro | *
|
16 | 41426 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40435 | jjdelcerro | *
|
20 | 41426 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.fmap.mapcontrol.swing.dynobject.impl; |
||
24 | |||
25 | import java.awt.BorderLayout; |
||
26 | 40866 | jbadia | import java.awt.Dimension; |
27 | 40954 | jjdelcerro | import java.awt.event.ActionEvent; |
28 | 40866 | jbadia | import java.util.Iterator; |
29 | 40435 | jjdelcerro | import java.util.Map; |
30 | |||
31 | 40954 | jjdelcerro | import javax.swing.AbstractAction; |
32 | import javax.swing.Action; |
||
33 | 40435 | jjdelcerro | import javax.swing.JComponent; |
34 | 40956 | jjdelcerro | import javax.swing.JOptionPane; |
35 | 40435 | jjdelcerro | import javax.swing.JPanel; |
36 | 40866 | jbadia | import javax.swing.JScrollPane; |
37 | import javax.swing.JSplitPane; |
||
38 | import javax.swing.JTree; |
||
39 | import javax.swing.event.TreeSelectionEvent; |
||
40 | import javax.swing.event.TreeSelectionListener; |
||
41 | import javax.swing.tree.DefaultMutableTreeNode; |
||
42 | import javax.swing.tree.DefaultTreeModel; |
||
43 | import javax.swing.tree.MutableTreeNode; |
||
44 | import javax.swing.tree.TreeNode; |
||
45 | import javax.swing.tree.TreePath; |
||
46 | import javax.swing.tree.TreeSelectionModel; |
||
47 | 40435 | jjdelcerro | |
48 | 40954 | jjdelcerro | import org.gvsig.fmap.geom.DataTypes; |
49 | import org.gvsig.fmap.geom.Geometry; |
||
50 | import org.gvsig.fmap.mapcontext.MapContext; |
||
51 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent; |
52 | 40954 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
53 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
54 | 40866 | jbadia | import org.gvsig.tools.dynform.AbortActionException; |
55 | import org.gvsig.tools.dynform.DynFormLocator; |
||
56 | 40954 | jjdelcerro | import org.gvsig.tools.dynform.JDynFormField; |
57 | 40866 | jbadia | import org.gvsig.tools.dynform.JDynFormSet; |
58 | import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener; |
||
59 | 40869 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
60 | 40435 | jjdelcerro | import org.gvsig.tools.dynobject.DynObjectSet; |
61 | import org.gvsig.tools.exception.BaseException; |
||
62 | import org.slf4j.Logger; |
||
63 | import org.slf4j.LoggerFactory; |
||
64 | |||
65 | /**
|
||
66 | * @author gvSIG Team
|
||
67 | * @version $Id$
|
||
68 | 41426 | jjdelcerro | *
|
69 | 40435 | jjdelcerro | */
|
70 | public class DefaultLayersDynObjectSetComponent extends JPanel implements |
||
71 | 41426 | jjdelcerro | LayersDynObjectSetComponent, TreeSelectionListener, JDynFormSetListener {
|
72 | 40435 | jjdelcerro | |
73 | private static final long serialVersionUID = 5864674721657215264L; |
||
74 | |||
75 | private static final Logger LOG = LoggerFactory |
||
76 | 41426 | jjdelcerro | .getLogger(DefaultLayersDynObjectSetComponent.class); |
77 | 40435 | jjdelcerro | |
78 | private final LayersDynObjectSetComponentModel model; |
||
79 | |||
80 | 40866 | jbadia | private JDynFormSet component;
|
81 | 40435 | jjdelcerro | |
82 | 40866 | jbadia | private JTree layersTree; |
83 | private JPanel contentPanel; |
||
84 | 40435 | jjdelcerro | |
85 | private final boolean writable; |
||
86 | |||
87 | 41426 | jjdelcerro | private DefaultMutableTreeNode topNode; |
88 | private MutableTreeNode previousSelection = null; |
||
89 | 40866 | jbadia | |
90 | 41426 | jjdelcerro | private MapContext mapContext = null; |
91 | |||
92 | 40435 | jjdelcerro | /**
|
93 | * Creates a new {@link DefaultLayersDynObjectSetComponent} with the given
|
||
94 | * information for a list of layers.
|
||
95 | */
|
||
96 | public DefaultLayersDynObjectSetComponent(
|
||
97 | 41426 | jjdelcerro | Map<String, DynObjectSet> layerName2InfoByPoint) { |
98 | 40435 | jjdelcerro | this(layerName2InfoByPoint, true); |
99 | } |
||
100 | |||
101 | /**
|
||
102 | * @param isDoubleBuffered
|
||
103 | */
|
||
104 | public DefaultLayersDynObjectSetComponent(
|
||
105 | 41426 | jjdelcerro | Map<String, DynObjectSet> layerName2InfoByPoint, |
106 | boolean writable) {
|
||
107 | 40435 | jjdelcerro | super(new BorderLayout()); |
108 | this.writable = writable;
|
||
109 | model = new LayersDynObjectSetComponentModel(layerName2InfoByPoint);
|
||
110 | initializeUI(); |
||
111 | } |
||
112 | |||
113 | private void initializeUI() { |
||
114 | 40866 | jbadia | addLayerTree(); |
115 | 40435 | jjdelcerro | } |
116 | |||
117 | 41426 | jjdelcerro | private void addLayerTree() { |
118 | topNode = new DefaultMutableTreeNode(""); |
||
119 | layersTree = new JTree(topNode); |
||
120 | layersTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||
121 | 40435 | jjdelcerro | |
122 | 41426 | jjdelcerro | //layersTree.setShowsRootHandles(true);
|
123 | layersTree.addTreeSelectionListener(this);
|
||
124 | createTreeModel(); |
||
125 | |||
126 | JScrollPane scroll = new JScrollPane(layersTree); |
||
127 | scroll.setPreferredSize(new Dimension(200, 200)); |
||
128 | |||
129 | contentPanel = new JPanel(); |
||
130 | contentPanel.setLayout(new BorderLayout()); |
||
131 | |||
132 | JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, |
||
133 | 40866 | jbadia | scroll, contentPanel); |
134 | 41426 | jjdelcerro | splitPane.setOneTouchExpandable(true);
|
135 | splitPane.setDividerLocation(170);
|
||
136 | |||
137 | //Provide minimum sizes for the two components in the split pane
|
||
138 | Dimension minimumSize = new Dimension(100, 150); |
||
139 | scroll.setMinimumSize(minimumSize); |
||
140 | |||
141 | add(splitPane, BorderLayout.CENTER);
|
||
142 | |||
143 | for (int i = 0; i < layersTree.getRowCount(); i++) { |
||
144 | layersTree.expandRow(i); |
||
145 | } |
||
146 | layersTree.setRootVisible(false);
|
||
147 | |||
148 | if (topNode.getChildCount() > 0 && topNode.getFirstChild().getChildCount() > 0) { |
||
149 | layersTree.setSelectionPath(new TreePath(((DefaultTreeModel) layersTree.getModel()).getPathToRoot(topNode.getFirstChild().getChildAt(0)))); |
||
150 | } |
||
151 | 40435 | jjdelcerro | } |
152 | 41426 | jjdelcerro | |
153 | 40866 | jbadia | private void createTreeModel() { |
154 | 41426 | jjdelcerro | for (int i = 0; i < model.getSize(); i++) { |
155 | MutableTreeNode newLayer = new DefaultMutableTreeNode( |
||
156 | model.getElementAt(i), |
||
157 | true);
|
||
158 | |||
159 | DynObjectSet dynObjects = model.getLayerInfoByPoint((String) model.getElementAt(i));
|
||
160 | Iterator it;
|
||
161 | try {
|
||
162 | it = dynObjects.iterator(); |
||
163 | int j = 0; |
||
164 | while (it.hasNext()) {
|
||
165 | DynObject dynObject = (DynObject) it.next(); |
||
166 | MutableTreeNode newDynObject = new DefaultMutableTreeNode( |
||
167 | concatString(dynObject.toString(), 20),
|
||
168 | false);
|
||
169 | newLayer.insert(newDynObject, j); |
||
170 | j++; |
||
171 | } |
||
172 | } catch (BaseException e) {
|
||
173 | LOG.error("Error getting the DynObjects of "
|
||
174 | + "the DynObjectSet: " + dynObjects, e);
|
||
175 | } |
||
176 | topNode.add(newLayer); |
||
177 | |||
178 | } |
||
179 | 40435 | jjdelcerro | } |
180 | 41426 | jjdelcerro | |
181 | 40867 | jbadia | private String concatString(String cadena, int i) { |
182 | 41426 | jjdelcerro | if (cadena.length() > i && i > 3) { |
183 | String newCadena = cadena.substring(0, i - 3); |
||
184 | cadena = newCadena + "...";
|
||
185 | } |
||
186 | return cadena;
|
||
187 | } |
||
188 | 40435 | jjdelcerro | |
189 | private void setCurrentLayerInfoByPoint(String layerName) { |
||
190 | 40866 | jbadia | JDynFormSet newComponent = null;
|
191 | 40435 | jjdelcerro | |
192 | DynObjectSet dynObjectSet = model.getLayerInfoByPoint(layerName); |
||
193 | try {
|
||
194 | 41426 | jjdelcerro | newComponent |
195 | = DynFormLocator.getDynFormManager() |
||
196 | .createJDynFormSet(dynObjectSet); |
||
197 | 40866 | jbadia | newComponent.setReadOnly(!this.writable);
|
198 | 40435 | jjdelcerro | } catch (BaseException e) {
|
199 | 40866 | jbadia | LOG.error("Error creating the JDynFormSet for "
|
200 | 41426 | jjdelcerro | + "the DynObjectSet: " + dynObjectSet, e);
|
201 | 40435 | jjdelcerro | } |
202 | |||
203 | if (newComponent != null) { |
||
204 | 41426 | jjdelcerro | newComponent.setAllowDelete(false);
|
205 | newComponent.setAllowNew(false);
|
||
206 | newComponent.setAllowSearch(false);
|
||
207 | newComponent.setAllowUpdate(false);
|
||
208 | if (this.mapContext != null) { |
||
209 | DataTypesManager dataManager = ToolsLocator.getDataTypesManager(); |
||
210 | newComponent.addActionToPopupMenu(dataManager.get(DataTypes.GEOMETRY), "Center in current view", new CenterGeometryInMapContext(this.mapContext)); |
||
211 | } |
||
212 | 40435 | jjdelcerro | removeCurrentDynObjectSetComponent(); |
213 | component = newComponent; |
||
214 | 40866 | jbadia | component.addListener(this);
|
215 | JComponent comp = component.asJComponent();
|
||
216 | contentPanel.add(comp, BorderLayout.CENTER);
|
||
217 | 40435 | jjdelcerro | revalidate(); |
218 | repaint(); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | public JComponent asJComponent() { |
||
223 | return this; |
||
224 | } |
||
225 | |||
226 | public void dispose() { |
||
227 | removeCurrentDynObjectSetComponent(); |
||
228 | model.dispose(); |
||
229 | } |
||
230 | |||
231 | private void removeCurrentDynObjectSetComponent() { |
||
232 | if (component != null) { |
||
233 | remove(component.asJComponent()); |
||
234 | 41426 | jjdelcerro | // component.dispose();
|
235 | 40866 | jbadia | contentPanel.removeAll(); |
236 | contentPanel.repaint(); |
||
237 | 40435 | jjdelcerro | } |
238 | } |
||
239 | |||
240 | 41426 | jjdelcerro | public void valueChanged(TreeSelectionEvent e) { |
241 | TreePath newPath = e.getNewLeadSelectionPath();
|
||
242 | TreePath oldPath = e.getOldLeadSelectionPath();
|
||
243 | 40866 | jbadia | |
244 | 41426 | jjdelcerro | if (!newPath.equals(oldPath)) {
|
245 | DefaultMutableTreeNode aux
|
||
246 | = (DefaultMutableTreeNode) layersTree.getLastSelectedPathComponent();
|
||
247 | 40866 | jbadia | |
248 | 41426 | jjdelcerro | if (layersTree.getSelectionPath() != null) { |
249 | if (aux.isLeaf()) { // A Feature node is selected |
||
250 | String layerName = ""; |
||
251 | if (previousSelection == null |
||
252 | || !aux.getParent().equals(previousSelection.getParent())) { |
||
253 | layerName = aux.getParent().toString(); |
||
254 | setCurrentLayerInfoByPoint(layerName); |
||
255 | } |
||
256 | int childIndex = aux.getParent().getIndex(aux);
|
||
257 | if (childIndex != -1) { |
||
258 | component.setCurrentIndex(childIndex); |
||
259 | } |
||
260 | previousSelection = aux; |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | 40866 | jbadia | |
266 | 41426 | jjdelcerro | public void message(String arg0) { |
267 | // Do nothing
|
||
268 | } |
||
269 | 40866 | jbadia | |
270 | 41426 | jjdelcerro | public void formClose() { |
271 | this.setVisible(false); |
||
272 | } |
||
273 | 40866 | jbadia | |
274 | 41426 | jjdelcerro | public void formMessage(String arg0) { |
275 | // Do nothing
|
||
276 | } |
||
277 | 40866 | jbadia | |
278 | 41426 | jjdelcerro | public void formMovedTo(int arg0) { |
279 | DefaultMutableTreeNode aux
|
||
280 | = (DefaultMutableTreeNode) layersTree.getLastSelectedPathComponent();
|
||
281 | TreeNode parent = aux.getParent();
|
||
282 | TreeNode selectNode = parent.getChildAt(arg0);
|
||
283 | 40866 | jbadia | |
284 | 41426 | jjdelcerro | TreePath path = new TreePath(((DefaultTreeModel) layersTree.getModel()).getPathToRoot(selectNode)); |
285 | layersTree.setSelectionPath(path); |
||
286 | } |
||
287 | 40866 | jbadia | |
288 | 41426 | jjdelcerro | public void setMapContext(MapContext mapContext) { |
289 | this.mapContext = mapContext;
|
||
290 | } |
||
291 | 40866 | jbadia | |
292 | 41426 | jjdelcerro | public void formBeforeSave(JDynFormSet jdfs) throws AbortActionException { |
293 | } |
||
294 | 40866 | jbadia | |
295 | 41426 | jjdelcerro | public void formBeforeNew(JDynFormSet jdfs) throws AbortActionException { |
296 | } |
||
297 | 40866 | jbadia | |
298 | 41426 | jjdelcerro | public void formBeforeDelete(JDynFormSet jdfs) throws AbortActionException { |
299 | } |
||
300 | 40866 | jbadia | |
301 | 41426 | jjdelcerro | public void formBeforeSearch(JDynFormSet jdfs) throws AbortActionException { |
302 | } |
||
303 | 40866 | jbadia | |
304 | 41426 | jjdelcerro | public void formAfterSave(JDynFormSet jdfs) { |
305 | } |
||
306 | |||
307 | public void formAfterNew(JDynFormSet jdfs) { |
||
308 | } |
||
309 | |||
310 | public void formAfterDelete(JDynFormSet jdfs) { |
||
311 | } |
||
312 | |||
313 | public void formAfterSearch(JDynFormSet jdfs) { |
||
314 | } |
||
315 | |||
316 | 42775 | jjdelcerro | @Override
|
317 | public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
||
318 | } |
||
319 | |||
320 | @Override
|
||
321 | public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
||
322 | } |
||
323 | |||
324 | 40954 | jjdelcerro | public class CenterGeometryInMapContext extends AbstractAction implements Action { |
325 | |||
326 | 41426 | jjdelcerro | private MapContext mapContext;
|
327 | 40954 | jjdelcerro | |
328 | 41426 | jjdelcerro | public CenterGeometryInMapContext(MapContext mapContext) {
|
329 | super("Center in the current view"); |
||
330 | this.mapContext = mapContext;
|
||
331 | this.putValue(Action.SHORT_DESCRIPTION, "Center geometry in the current view"); |
||
332 | this.putValue(Action.LONG_DESCRIPTION, "Center geometry in the current view"); |
||
333 | } |
||
334 | |||
335 | public void actionPerformed(ActionEvent arg0) { |
||
336 | if (mapContext == null) { |
||
337 | return;
|
||
338 | } |
||
339 | Object source = null; |
||
340 | try {
|
||
341 | source = arg0.getSource(); |
||
342 | JDynFormField field = (JDynFormField) source; |
||
343 | Geometry geom = (Geometry) field.getValue(); |
||
344 | if (geom == null) { |
||
345 | JOptionPane.showMessageDialog(component.asJComponent(), "The geometri is not valid. Can't center the view in it.", "Warning", JOptionPane.INFORMATION_MESSAGE); |
||
346 | return;
|
||
347 | } |
||
348 | mapContext.getViewPort().setEnvelope(geom.getEnvelope()); |
||
349 | mapContext.invalidate(); |
||
350 | } catch (Exception ex) { |
||
351 | LOG.warn("Can't center view in the geometry (source=" + source.toString() + ").", ex); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | 40954 | jjdelcerro | } |
356 | |||
357 | 40435 | jjdelcerro | } |