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 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* 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 |
*
|
11 |
* 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 |
*
|
16 |
* 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 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.mapcontrol.swing.dynobject.impl; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.util.Iterator; |
29 |
import java.util.Map; |
30 |
|
31 |
import javax.swing.AbstractAction; |
32 |
import javax.swing.Action; |
33 |
import javax.swing.JComponent; |
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.JPanel; |
36 |
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 |
|
48 |
import org.gvsig.fmap.geom.DataTypes; |
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.mapcontext.MapContext; |
51 |
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
54 |
import org.gvsig.tools.dynform.AbortActionException; |
55 |
import org.gvsig.tools.dynform.DynFormLocator; |
56 |
import org.gvsig.tools.dynform.JDynFormField; |
57 |
import org.gvsig.tools.dynform.JDynFormSet; |
58 |
import org.gvsig.tools.dynform.JDynFormSet.JDynFormSetListener; |
59 |
import org.gvsig.tools.dynobject.DynObject; |
60 |
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 |
*
|
69 |
*/
|
70 |
public class DefaultLayersDynObjectSetComponent extends JPanel implements |
71 |
LayersDynObjectSetComponent, TreeSelectionListener, JDynFormSetListener {
|
72 |
|
73 |
private static final long serialVersionUID = 5864674721657215264L; |
74 |
|
75 |
private static final Logger LOG = LoggerFactory |
76 |
.getLogger(DefaultLayersDynObjectSetComponent.class); |
77 |
|
78 |
private final LayersDynObjectSetComponentModel model; |
79 |
|
80 |
private JDynFormSet component;
|
81 |
|
82 |
private JTree layersTree; |
83 |
private JPanel contentPanel; |
84 |
|
85 |
private final boolean writable; |
86 |
|
87 |
private DefaultMutableTreeNode topNode; |
88 |
private MutableTreeNode previousSelection = null; |
89 |
|
90 |
private MapContext mapContext = null; |
91 |
|
92 |
/**
|
93 |
* Creates a new {@link DefaultLayersDynObjectSetComponent} with the given
|
94 |
* information for a list of layers.
|
95 |
*/
|
96 |
public DefaultLayersDynObjectSetComponent(
|
97 |
Map<String, DynObjectSet> layerName2InfoByPoint) { |
98 |
this(layerName2InfoByPoint, true); |
99 |
} |
100 |
|
101 |
/**
|
102 |
* @param isDoubleBuffered
|
103 |
*/
|
104 |
public DefaultLayersDynObjectSetComponent(
|
105 |
Map<String, DynObjectSet> layerName2InfoByPoint, |
106 |
boolean writable) {
|
107 |
super(new BorderLayout()); |
108 |
this.writable = writable;
|
109 |
model = new LayersDynObjectSetComponentModel(layerName2InfoByPoint);
|
110 |
initializeUI(); |
111 |
} |
112 |
|
113 |
private void initializeUI() { |
114 |
addLayerTree(); |
115 |
} |
116 |
|
117 |
private void addLayerTree() { |
118 |
topNode = new DefaultMutableTreeNode(""); |
119 |
layersTree = new JTree(topNode); |
120 |
layersTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
121 |
|
122 |
//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 |
scroll, contentPanel); |
134 |
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 |
} |
152 |
|
153 |
private void createTreeModel() { |
154 |
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 |
} |
180 |
|
181 |
private String concatString(String cadena, int i) { |
182 |
if (cadena.length() > i && i > 3) { |
183 |
String newCadena = cadena.substring(0, i - 3); |
184 |
cadena = newCadena + "...";
|
185 |
} |
186 |
return cadena;
|
187 |
} |
188 |
|
189 |
private void setCurrentLayerInfoByPoint(String layerName) { |
190 |
JDynFormSet newComponent = null;
|
191 |
|
192 |
DynObjectSet dynObjectSet = model.getLayerInfoByPoint(layerName); |
193 |
try {
|
194 |
newComponent |
195 |
= DynFormLocator.getDynFormManager() |
196 |
.createJDynFormSet(dynObjectSet); |
197 |
newComponent.setReadOnly(!this.writable);
|
198 |
} catch (BaseException e) {
|
199 |
LOG.error("Error creating the JDynFormSet for "
|
200 |
+ "the DynObjectSet: " + dynObjectSet, e);
|
201 |
} |
202 |
|
203 |
if (newComponent != null) { |
204 |
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 |
removeCurrentDynObjectSetComponent(); |
213 |
component = newComponent; |
214 |
component.addListener(this);
|
215 |
JComponent comp = component.asJComponent();
|
216 |
contentPanel.add(comp, BorderLayout.CENTER);
|
217 |
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 |
// component.dispose();
|
235 |
contentPanel.removeAll(); |
236 |
contentPanel.repaint(); |
237 |
} |
238 |
} |
239 |
|
240 |
public void valueChanged(TreeSelectionEvent e) { |
241 |
TreePath newPath = e.getNewLeadSelectionPath();
|
242 |
TreePath oldPath = e.getOldLeadSelectionPath();
|
243 |
|
244 |
if (!newPath.equals(oldPath)) {
|
245 |
DefaultMutableTreeNode aux
|
246 |
= (DefaultMutableTreeNode) layersTree.getLastSelectedPathComponent();
|
247 |
|
248 |
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 |
|
266 |
public void message(String arg0) { |
267 |
// Do nothing
|
268 |
} |
269 |
|
270 |
public void formClose() { |
271 |
this.setVisible(false); |
272 |
} |
273 |
|
274 |
public void formMessage(String arg0) { |
275 |
// Do nothing
|
276 |
} |
277 |
|
278 |
public void formMovedTo(int arg0) { |
279 |
DefaultMutableTreeNode aux
|
280 |
= (DefaultMutableTreeNode) layersTree.getLastSelectedPathComponent();
|
281 |
TreeNode parent = aux.getParent();
|
282 |
TreeNode selectNode = parent.getChildAt(arg0);
|
283 |
|
284 |
TreePath path = new TreePath(((DefaultTreeModel) layersTree.getModel()).getPathToRoot(selectNode)); |
285 |
layersTree.setSelectionPath(path); |
286 |
} |
287 |
|
288 |
public void setMapContext(MapContext mapContext) { |
289 |
this.mapContext = mapContext;
|
290 |
} |
291 |
|
292 |
public void formBeforeSave(JDynFormSet jdfs) throws AbortActionException { |
293 |
} |
294 |
|
295 |
public void formBeforeNew(JDynFormSet jdfs) throws AbortActionException { |
296 |
} |
297 |
|
298 |
public void formBeforeDelete(JDynFormSet jdfs) throws AbortActionException { |
299 |
} |
300 |
|
301 |
public void formBeforeSearch(JDynFormSet jdfs) throws AbortActionException { |
302 |
} |
303 |
|
304 |
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 |
@Override
|
317 |
public void formBeforeCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
318 |
} |
319 |
|
320 |
@Override
|
321 |
public void formAfterCancelNew(JDynFormSet dynformSet) throws AbortActionException { |
322 |
} |
323 |
|
324 |
public class CenterGeometryInMapContext extends AbstractAction implements Action { |
325 |
|
326 |
private MapContext mapContext;
|
327 |
|
328 |
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 |
} |
356 |
|
357 |
} |