svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.plugin / org.gvsig.snapping.app / org.gvsig.snapping.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / gui / ViewSnappingPropertiesPage.java @ 43888
History | View | Annotate | Download (15.6 KB)
1 |
package org.gvsig.app.project.documents.view.gui; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.awt.FlowLayout; |
6 |
import java.awt.event.KeyEvent; |
7 |
import java.awt.event.KeyListener; |
8 |
import java.util.ArrayList; |
9 |
|
10 |
import javax.swing.JComponent; |
11 |
import javax.swing.JLabel; |
12 |
import javax.swing.JPanel; |
13 |
import javax.swing.JScrollPane; |
14 |
import javax.swing.JSeparator; |
15 |
import javax.swing.JTable; |
16 |
import javax.swing.JTextField; |
17 |
import javax.swing.border.EmptyBorder; |
18 |
import javax.swing.border.LineBorder; |
19 |
import javax.swing.table.AbstractTableModel; |
20 |
import javax.swing.table.TableModel; |
21 |
|
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
import org.gvsig.andami.PluginServices; |
26 |
import org.gvsig.app.project.documents.view.ViewDocument; |
27 |
import org.gvsig.configurableactions.ConfigurableActionsMamager; |
28 |
import org.gvsig.fmap.geom.primitive.Envelope; |
29 |
import org.gvsig.fmap.geom.primitive.Point; |
30 |
import org.gvsig.fmap.mapcontext.MapContext; |
31 |
import org.gvsig.fmap.mapcontext.ViewPort; |
32 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
33 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
34 |
import org.gvsig.fmap.mapcontext.layers.SingleLayerIterator; |
35 |
import org.gvsig.fmap.mapcontext.layers.SpatialCache; |
36 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
37 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
38 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
39 |
import org.gvsig.propertypage.PropertiesPage; |
40 |
import org.gvsig.tools.ToolsLocator; |
41 |
import org.gvsig.tools.i18n.I18nManager; |
42 |
import org.gvsig.tools.util.ToolsUtilLocator; |
43 |
|
44 |
public class ViewSnappingPropertiesPage extends JPanel implements PropertiesPage { |
45 |
|
46 |
/**
|
47 |
*
|
48 |
*/
|
49 |
private static final long serialVersionUID = 3534295787643266933L; |
50 |
|
51 |
private static final Logger logger = |
52 |
LoggerFactory.getLogger(ViewSnappingPropertiesPage.class); |
53 |
|
54 |
|
55 |
private JLabel jLabel = null; |
56 |
|
57 |
private JTextField jTxtTolerance = null; |
58 |
|
59 |
private JLabel jLabel1 = null; |
60 |
|
61 |
private JSeparator jSeparator = null; |
62 |
|
63 |
private JScrollPane jScrollPane = null; |
64 |
|
65 |
private JTable jTableSnapping = null; |
66 |
|
67 |
private JLabel jLabelCache = null; |
68 |
|
69 |
private JPanel jPanelNord = null; |
70 |
|
71 |
private JPanel jPanelCache = null; |
72 |
private boolean changed = false; |
73 |
|
74 |
private FLayers layers;
|
75 |
|
76 |
private MapContext mapContext;
|
77 |
|
78 |
private ViewDocument view;
|
79 |
|
80 |
private boolean isAcceppted; |
81 |
|
82 |
private static MapControlManager mapControlManager = MapControlLocator.getMapControlManager(); |
83 |
|
84 |
private class MyRecord { |
85 |
public Boolean bSelec = new Boolean(false); |
86 |
|
87 |
public String layerName; |
88 |
|
89 |
public Integer maxFeat = new Integer(1000); |
90 |
} |
91 |
|
92 |
private class MyTableModel extends AbstractTableModel { |
93 |
private ArrayList records = new ArrayList(); |
94 |
|
95 |
public MyTableModel(FLayers layers) {
|
96 |
addLayer(layers); |
97 |
} |
98 |
|
99 |
private void addLayer(FLayer lyr) { |
100 |
if (lyr instanceof FLayers) { |
101 |
FLayers lyrGroup = (FLayers) lyr; |
102 |
for (int i = 0; i < lyrGroup.getLayersCount(); i++) { |
103 |
FLayer lyr2 = lyrGroup.getLayer(i); |
104 |
addLayer(lyr2); |
105 |
} |
106 |
} else {
|
107 |
if (lyr instanceof FLyrVect) { |
108 |
FLyrVect aux = (FLyrVect) lyr; |
109 |
MyRecord rec = new MyRecord();
|
110 |
rec.layerName = lyr.getName(); |
111 |
SpatialCache spatialCache = aux.getSpatialCache(); |
112 |
rec.bSelec = new Boolean(spatialCache.isEnabled()); |
113 |
rec.maxFeat = new Integer(spatialCache.getMaxFeatures()); |
114 |
records.add(rec); |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
public int getColumnCount() { |
120 |
return 3; |
121 |
} |
122 |
|
123 |
public int getRowCount() { |
124 |
return records.size();
|
125 |
} |
126 |
|
127 |
public Object getValueAt(int rowIndex, int columnIndex) { |
128 |
MyRecord rec = (MyRecord) records.get(rowIndex); |
129 |
if (columnIndex == 0) |
130 |
return rec.bSelec;
|
131 |
if (columnIndex == 1) |
132 |
return rec.layerName;
|
133 |
if (columnIndex == 2) |
134 |
return rec.maxFeat;
|
135 |
return null; |
136 |
|
137 |
} |
138 |
|
139 |
public Class getColumnClass(int c) { |
140 |
if (c == 0) |
141 |
return Boolean.class; |
142 |
if (c == 2) |
143 |
return Integer.class; |
144 |
return String.class; |
145 |
} |
146 |
|
147 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
148 |
MyRecord rec = (MyRecord) records.get(rowIndex); |
149 |
if (columnIndex == 0) |
150 |
rec.bSelec = (Boolean) aValue;
|
151 |
if (columnIndex == 2) { |
152 |
if (aValue != null) |
153 |
rec.maxFeat = (Integer) aValue;
|
154 |
else
|
155 |
rec.maxFeat = new Integer(0); |
156 |
} |
157 |
changed =true;
|
158 |
super.setValueAt(aValue, rowIndex, columnIndex);
|
159 |
} |
160 |
|
161 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
162 |
if (columnIndex == 0) |
163 |
return true; |
164 |
if (columnIndex == 2) |
165 |
return true; |
166 |
|
167 |
return false; |
168 |
} |
169 |
|
170 |
public String getColumnName(int column) { |
171 |
if (column == 0) |
172 |
return PluginServices.getText(this, "Selected"); |
173 |
if (column == 1) |
174 |
return PluginServices.getText(this, "LayerName"); |
175 |
if (column == 2) |
176 |
return PluginServices.getText(this, "MaxFeaturesEditionCache"); |
177 |
return "You shouldn't reach this point"; |
178 |
|
179 |
} |
180 |
} |
181 |
|
182 |
public ViewSnappingPropertiesPage(ViewDocument view) {
|
183 |
this.view = view;
|
184 |
initComponents(); |
185 |
|
186 |
// addLayer(layers);
|
187 |
this.mapContext = view.getMapContext();
|
188 |
this.layers = mapContext.getLayers();
|
189 |
MyTableModel tm = new MyTableModel(layers);
|
190 |
getJTableSnapping().setModel(tm); |
191 |
getJTxtTolerance().setText(String.valueOf(mapControlManager.getTolerance()));
|
192 |
} |
193 |
|
194 |
public boolean whenAccept() { |
195 |
isAcceppted = true;
|
196 |
return whenApply();
|
197 |
} |
198 |
|
199 |
public boolean whenApply() { |
200 |
TableModel tm = getJTableSnapping().getModel();
|
201 |
ArrayList layersToSnap = new ArrayList(); |
202 |
for (int i = 0; i < tm.getRowCount(); i++) { |
203 |
String layerName = (String) tm.getValueAt(i, 1); |
204 |
FLyrVect lyr = (FLyrVect) layers.getLayer(layerName); |
205 |
Boolean bUseCache = (Boolean) tm.getValueAt(i, 0); |
206 |
Integer maxFeat = (Integer) tm.getValueAt(i, 2); |
207 |
|
208 |
// Decidimos si vamos a habilitar el spatialCache DESPUES, justo
|
209 |
// antes de renderizar.
|
210 |
// Necesitamos un m?todo que explore las capas en edici?n y mire las
|
211 |
// capas sobre las
|
212 |
// que se necestia el cache. Aqu? lo que hacemos es a?adir las
|
213 |
// seleccionadas a la
|
214 |
// lista de capas asociadas al snapping de los temas activos en
|
215 |
// edici?n.
|
216 |
// Lo del m?ximo de features en cach?, tiene que ser para cada capa
|
217 |
// distinto. Pero no
|
218 |
// puedes "chafar" el que ya hay, porque puedes fastidiar a otra
|
219 |
// capa en edici?n.
|
220 |
// Como m?ximo, lo que podemos hacer es que si es mayor al que hay,
|
221 |
// lo subimos. Si
|
222 |
// se solicita uno menor, lo dejamos como est?.
|
223 |
// Otra opci?n ser?a no hacer caso de esto para cada capa, y ponerlo
|
224 |
// de forma global.
|
225 |
// lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
|
226 |
lyr.setMaxFeaturesInEditionCache(maxFeat.intValue()); |
227 |
if (bUseCache.booleanValue())
|
228 |
layersToSnap.add(lyr); |
229 |
} |
230 |
SingleLayerIterator it = new SingleLayerIterator(layers);
|
231 |
|
232 |
while (it.hasNext()) {
|
233 |
FLayer aux = it.next(); |
234 |
if (aux instanceof FLyrVect) |
235 |
{ |
236 |
FLyrVect lyrVect = (FLyrVect) aux; |
237 |
// Inicializamos todas
|
238 |
lyrVect.getSpatialCache().setEnabled(false);
|
239 |
if (aux.isActive()) {
|
240 |
if (aux.isEditing()) {
|
241 |
// Sobre la capa en edici?n siempre se puede hacer snapping
|
242 |
// Esto no es correcto, el usuario puede preferir no hacer snapping sobre la capa en edici?n
|
243 |
// lyrVect.getSpatialCache().setEnabled(true);
|
244 |
lyrVect.getMapContext().setLayersToSnap(layersToSnap); |
245 |
|
246 |
} |
247 |
} |
248 |
} |
249 |
} // while
|
250 |
it.rewind(); |
251 |
/*
|
252 |
* Iteramos por las capas en edici?n y marcamos aquellas capas que
|
253 |
* necesitan trabajar con el cache habilitado
|
254 |
*/
|
255 |
while (it.hasNext()) {
|
256 |
FLayer aux = it.next(); |
257 |
if (aux.isEditing())
|
258 |
if (aux instanceof FLyrVect) { |
259 |
MapContext mx=aux.getMapContext(); |
260 |
for (int i=0; i<mx.getLayersToSnap().size(); i++) |
261 |
{ |
262 |
FLyrVect lyrVect = (FLyrVect) mx.getLayersToSnap().get(i); |
263 |
lyrVect.getSpatialCache().setEnabled(true);
|
264 |
} |
265 |
|
266 |
} |
267 |
|
268 |
} // while
|
269 |
|
270 |
try{
|
271 |
mapControlManager.setTolerance(Integer.parseInt(getJTxtTolerance().getText()));
|
272 |
|
273 |
}catch (Exception e) { |
274 |
logger.error(PluginServices.getText(this, "tolerancia_incorrecta"),e); |
275 |
|
276 |
// throw new StoreException(PluginServices.getText(this, "tolerancia_incorrecta"),e);
|
277 |
} |
278 |
|
279 |
/*
|
280 |
* I think "mapContext.invalidate()" does not work as expected.
|
281 |
* Some layers are not actually redrawn.
|
282 |
*/
|
283 |
forceRepaint(mapContext); |
284 |
return true; |
285 |
} |
286 |
|
287 |
/**
|
288 |
* Not very elegant method to force repaint of view.
|
289 |
* @param mco
|
290 |
*/
|
291 |
private void forceRepaint(MapContext mco) { |
292 |
|
293 |
ViewPort vp = mco.getViewPort(); |
294 |
Envelope env = vp.getAdjustedEnvelope(); |
295 |
if (env != null) { |
296 |
try {
|
297 |
env = (Envelope) env.clone(); |
298 |
} catch (CloneNotSupportedException e) { |
299 |
logger.error("Unable to clone envelope: ", e);
|
300 |
} |
301 |
Point uc = env.getUpperCorner();
|
302 |
double upperx = uc.getX();
|
303 |
double inc_len = 0.000001 * env.getLength(0); |
304 |
uc.setX(upperx + inc_len); |
305 |
vp.setEnvelope(env); |
306 |
} |
307 |
} |
308 |
|
309 |
public boolean whenCancel() { |
310 |
isAcceppted = false;
|
311 |
return true; |
312 |
} |
313 |
|
314 |
public String getTitle() { |
315 |
return PluginServices.getText(this, "Snapping"); |
316 |
} |
317 |
|
318 |
public int getPriority() { |
319 |
return 900; |
320 |
} |
321 |
|
322 |
public JComponent asJComponent() { |
323 |
return this; |
324 |
} |
325 |
|
326 |
private void initComponents() { |
327 |
BorderLayout layout = new BorderLayout(); |
328 |
layout.setHgap(20);
|
329 |
|
330 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
331 |
this.setLayout(layout);
|
332 |
|
333 |
jLabelCache = new JLabel(); |
334 |
jLabelCache.setText(PluginServices.getText(this, "capas_edition_cache")); |
335 |
jLabelCache.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); |
336 |
jLabelCache.setPreferredSize(new java.awt.Dimension(500,20)); |
337 |
jLabelCache.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); |
338 |
jLabel1 = new JLabel(); |
339 |
jLabel1.setText(i18nManager.getTranslation("pixels"));
|
340 |
// jLabel1.setBounds(new java.awt.Rectangle(195, 8, 207, 15));
|
341 |
// jLabel1.setPreferredSize(new java.awt.Dimension(28, 20));
|
342 |
jLabel1.setName("jLabel1");
|
343 |
jLabel = new JLabel(); |
344 |
jLabel.setText(i18nManager.getTranslation("snap_tolerance"));
|
345 |
jLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); |
346 |
jLabel.setName("jLabel");
|
347 |
// jLabel.setBounds(new java.awt.Rectangle(15, 8, 122, 15));
|
348 |
// jLabel.setPreferredSize(new java.awt.Dimension(28, 20));
|
349 |
jLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); |
350 |
|
351 |
this.setSize(new java.awt.Dimension(502,288)); |
352 |
this.setPreferredSize(this.getSize()); |
353 |
this.add(getJPanelNord(), BorderLayout.NORTH); |
354 |
|
355 |
this.add(getJPanelCache(), BorderLayout.CENTER); |
356 |
|
357 |
} |
358 |
|
359 |
/**
|
360 |
* This method initializes jPanelNord
|
361 |
*
|
362 |
* @return javax.swing.JPanel
|
363 |
*/
|
364 |
private JPanel getJPanelNord() { |
365 |
if (jPanelNord == null) { |
366 |
JPanel panel = new JPanel(); |
367 |
panel.setLayout(new FlowLayout()); |
368 |
// panel.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
|
369 |
// panel.setPreferredSize(new java.awt.Dimension(30, 30));
|
370 |
panel.add(jLabel); |
371 |
panel.add(getJTxtTolerance()); |
372 |
panel.add(jLabel1); |
373 |
|
374 |
jPanelNord = new JPanel(); |
375 |
jPanelNord.setLayout(new BorderLayout()); |
376 |
jPanelNord.add(panel,BorderLayout.LINE_START);
|
377 |
ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
378 |
JComponent c = cfgActionsManager.getConfigurableActionsComponent("SnappingPropertiesPage", this); |
379 |
jPanelNord.add(c, BorderLayout.LINE_END);
|
380 |
} |
381 |
return jPanelNord;
|
382 |
} |
383 |
|
384 |
/**
|
385 |
* This method initializes jPanelCache
|
386 |
*
|
387 |
* @return javax.swing.JPanel
|
388 |
*/
|
389 |
private JPanel getJPanelCache() { |
390 |
if (jPanelCache == null) { |
391 |
jPanelCache = new JPanel(); |
392 |
jPanelCache.setLayout(new BorderLayout()); |
393 |
jPanelCache.setBorder(new EmptyBorder(10, 10, 10, 10)); |
394 |
jPanelCache.add(jLabelCache, java.awt.BorderLayout.NORTH); |
395 |
jPanelCache.add(getJScrollPane(), java.awt.BorderLayout.CENTER); |
396 |
} |
397 |
return jPanelCache;
|
398 |
} |
399 |
|
400 |
/**
|
401 |
* This method initializes jTxtTolerance
|
402 |
*
|
403 |
* @return javax.swing.JTextField
|
404 |
*/
|
405 |
public JTextField getJTxtTolerance() { |
406 |
if (jTxtTolerance == null) { |
407 |
jTxtTolerance = new JTextField(); |
408 |
jTxtTolerance.setPreferredSize(new java.awt.Dimension(28, 20)); |
409 |
jTxtTolerance.setName("jTxtTolerance");
|
410 |
jTxtTolerance.setHorizontalAlignment(javax.swing.JTextField.RIGHT); |
411 |
jTxtTolerance.setText("4");
|
412 |
jTxtTolerance.setBounds(new java.awt.Rectangle(142, 8, 39, 15)); |
413 |
jTxtTolerance.addKeyListener(new KeyListener() { |
414 |
public void keyPressed(KeyEvent e) { changed = true; } |
415 |
public void keyReleased(KeyEvent e) { changed = true; } |
416 |
public void keyTyped(KeyEvent e){ changed = true; } |
417 |
}); |
418 |
} |
419 |
return jTxtTolerance;
|
420 |
} |
421 |
|
422 |
/**
|
423 |
* This method initializes jScrollPane
|
424 |
*
|
425 |
* @return javax.swing.JScrollPane
|
426 |
*/
|
427 |
private JScrollPane getJScrollPane() { |
428 |
if (jScrollPane == null) { |
429 |
jScrollPane = new JScrollPane(); |
430 |
jScrollPane.setBorder(new LineBorder(Color.GRAY)); |
431 |
jScrollPane.setPreferredSize(new java.awt.Dimension(500,419)); |
432 |
jScrollPane.setViewportView(getJTableSnapping()); |
433 |
} |
434 |
return jScrollPane;
|
435 |
} |
436 |
|
437 |
/**
|
438 |
* This method initializes jTableSnapping
|
439 |
*
|
440 |
* @return javax.swing.JTable
|
441 |
*/
|
442 |
public JTable getJTableSnapping() { |
443 |
if (jTableSnapping == null) { |
444 |
jTableSnapping = new JTable(); |
445 |
jTableSnapping.addKeyListener(new KeyListener() { |
446 |
public void keyPressed(KeyEvent e) { changed = true; } |
447 |
public void keyReleased(KeyEvent e) { changed = true; } |
448 |
public void keyTyped(KeyEvent e){ changed = true; } |
449 |
}); |
450 |
} |
451 |
return jTableSnapping;
|
452 |
} |
453 |
|
454 |
|
455 |
/**
|
456 |
* @see org.gvsig.andami.ui.mdiManager.SingletonWindow#getWindowModel()
|
457 |
*/
|
458 |
public Object getWindowModel() { |
459 |
return view;
|
460 |
} |
461 |
|
462 |
|
463 |
public boolean isAcceppted() { |
464 |
return isAcceppted;
|
465 |
} |
466 |
|
467 |
} |