svn-gvsig-desktop / trunk / org.gvsig.desktop / 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 @ 43913
History | View | Annotate | Download (15.7 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 |
@Override
|
195 |
public void setData(Object data) { |
196 |
|
197 |
} |
198 |
|
199 |
public boolean whenAccept() { |
200 |
isAcceppted = true;
|
201 |
return whenApply();
|
202 |
} |
203 |
|
204 |
public boolean whenApply() { |
205 |
TableModel tm = getJTableSnapping().getModel();
|
206 |
ArrayList layersToSnap = new ArrayList(); |
207 |
for (int i = 0; i < tm.getRowCount(); i++) { |
208 |
String layerName = (String) tm.getValueAt(i, 1); |
209 |
FLyrVect lyr = (FLyrVect) layers.getLayer(layerName); |
210 |
Boolean bUseCache = (Boolean) tm.getValueAt(i, 0); |
211 |
Integer maxFeat = (Integer) tm.getValueAt(i, 2); |
212 |
|
213 |
// Decidimos si vamos a habilitar el spatialCache DESPUES, justo
|
214 |
// antes de renderizar.
|
215 |
// Necesitamos un m?todo que explore las capas en edici?n y mire las
|
216 |
// capas sobre las
|
217 |
// que se necestia el cache. Aqu? lo que hacemos es a?adir las
|
218 |
// seleccionadas a la
|
219 |
// lista de capas asociadas al snapping de los temas activos en
|
220 |
// edici?n.
|
221 |
// Lo del m?ximo de features en cach?, tiene que ser para cada capa
|
222 |
// distinto. Pero no
|
223 |
// puedes "chafar" el que ya hay, porque puedes fastidiar a otra
|
224 |
// capa en edici?n.
|
225 |
// Como m?ximo, lo que podemos hacer es que si es mayor al que hay,
|
226 |
// lo subimos. Si
|
227 |
// se solicita uno menor, lo dejamos como est?.
|
228 |
// Otra opci?n ser?a no hacer caso de esto para cada capa, y ponerlo
|
229 |
// de forma global.
|
230 |
// lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
|
231 |
lyr.setMaxFeaturesInEditionCache(maxFeat.intValue()); |
232 |
if (bUseCache.booleanValue())
|
233 |
layersToSnap.add(lyr); |
234 |
} |
235 |
SingleLayerIterator it = new SingleLayerIterator(layers);
|
236 |
|
237 |
while (it.hasNext()) {
|
238 |
FLayer aux = it.next(); |
239 |
if (aux instanceof FLyrVect) |
240 |
{ |
241 |
FLyrVect lyrVect = (FLyrVect) aux; |
242 |
// Inicializamos todas
|
243 |
lyrVect.getSpatialCache().setEnabled(false);
|
244 |
if (aux.isActive()) {
|
245 |
if (aux.isEditing()) {
|
246 |
// Sobre la capa en edici?n siempre se puede hacer snapping
|
247 |
// Esto no es correcto, el usuario puede preferir no hacer snapping sobre la capa en edici?n
|
248 |
// lyrVect.getSpatialCache().setEnabled(true);
|
249 |
lyrVect.getMapContext().setLayersToSnap(layersToSnap); |
250 |
|
251 |
} |
252 |
} |
253 |
} |
254 |
} // while
|
255 |
it.rewind(); |
256 |
/*
|
257 |
* Iteramos por las capas en edici?n y marcamos aquellas capas que
|
258 |
* necesitan trabajar con el cache habilitado
|
259 |
*/
|
260 |
while (it.hasNext()) {
|
261 |
FLayer aux = it.next(); |
262 |
if (aux.isEditing())
|
263 |
if (aux instanceof FLyrVect) { |
264 |
MapContext mx=aux.getMapContext(); |
265 |
for (int i=0; i<mx.getLayersToSnap().size(); i++) |
266 |
{ |
267 |
FLyrVect lyrVect = (FLyrVect) mx.getLayersToSnap().get(i); |
268 |
lyrVect.getSpatialCache().setEnabled(true);
|
269 |
} |
270 |
|
271 |
} |
272 |
|
273 |
} // while
|
274 |
|
275 |
try{
|
276 |
mapControlManager.setTolerance(Integer.parseInt(getJTxtTolerance().getText()));
|
277 |
|
278 |
}catch (Exception e) { |
279 |
logger.error(PluginServices.getText(this, "tolerancia_incorrecta"),e); |
280 |
|
281 |
// throw new StoreException(PluginServices.getText(this, "tolerancia_incorrecta"),e);
|
282 |
} |
283 |
|
284 |
/*
|
285 |
* I think "mapContext.invalidate()" does not work as expected.
|
286 |
* Some layers are not actually redrawn.
|
287 |
*/
|
288 |
forceRepaint(mapContext); |
289 |
return true; |
290 |
} |
291 |
|
292 |
/**
|
293 |
* Not very elegant method to force repaint of view.
|
294 |
* @param mco
|
295 |
*/
|
296 |
private void forceRepaint(MapContext mco) { |
297 |
|
298 |
ViewPort vp = mco.getViewPort(); |
299 |
Envelope env = vp.getAdjustedEnvelope(); |
300 |
if (env != null) { |
301 |
try {
|
302 |
env = (Envelope) env.clone(); |
303 |
} catch (CloneNotSupportedException e) { |
304 |
logger.error("Unable to clone envelope: ", e);
|
305 |
} |
306 |
Point uc = env.getUpperCorner();
|
307 |
double upperx = uc.getX();
|
308 |
double inc_len = 0.000001 * env.getLength(0); |
309 |
uc.setX(upperx + inc_len); |
310 |
vp.setEnvelope(env); |
311 |
} |
312 |
} |
313 |
|
314 |
public boolean whenCancel() { |
315 |
isAcceppted = false;
|
316 |
return true; |
317 |
} |
318 |
|
319 |
public String getTitle() { |
320 |
return PluginServices.getText(this, "Snapping"); |
321 |
} |
322 |
|
323 |
public int getPriority() { |
324 |
return 900; |
325 |
} |
326 |
|
327 |
public JComponent asJComponent() { |
328 |
return this; |
329 |
} |
330 |
|
331 |
private void initComponents() { |
332 |
BorderLayout layout = new BorderLayout(); |
333 |
layout.setHgap(20);
|
334 |
|
335 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
336 |
this.setLayout(layout);
|
337 |
|
338 |
jLabelCache = new JLabel(); |
339 |
jLabelCache.setText(PluginServices.getText(this, "capas_edition_cache")); |
340 |
jLabelCache.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); |
341 |
jLabelCache.setPreferredSize(new java.awt.Dimension(500,20)); |
342 |
jLabelCache.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); |
343 |
jLabel1 = new JLabel(); |
344 |
jLabel1.setText(i18nManager.getTranslation("pixels"));
|
345 |
// jLabel1.setBounds(new java.awt.Rectangle(195, 8, 207, 15));
|
346 |
// jLabel1.setPreferredSize(new java.awt.Dimension(28, 20));
|
347 |
jLabel1.setName("jLabel1");
|
348 |
jLabel = new JLabel(); |
349 |
jLabel.setText(i18nManager.getTranslation("snap_tolerance"));
|
350 |
jLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); |
351 |
jLabel.setName("jLabel");
|
352 |
// jLabel.setBounds(new java.awt.Rectangle(15, 8, 122, 15));
|
353 |
// jLabel.setPreferredSize(new java.awt.Dimension(28, 20));
|
354 |
jLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT); |
355 |
|
356 |
this.setSize(new java.awt.Dimension(502,288)); |
357 |
this.setPreferredSize(this.getSize()); |
358 |
this.add(getJPanelNord(), BorderLayout.NORTH); |
359 |
|
360 |
this.add(getJPanelCache(), BorderLayout.CENTER); |
361 |
|
362 |
} |
363 |
|
364 |
/**
|
365 |
* This method initializes jPanelNord
|
366 |
*
|
367 |
* @return javax.swing.JPanel
|
368 |
*/
|
369 |
private JPanel getJPanelNord() { |
370 |
if (jPanelNord == null) { |
371 |
JPanel panel = new JPanel(); |
372 |
panel.setLayout(new FlowLayout()); |
373 |
// panel.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
|
374 |
// panel.setPreferredSize(new java.awt.Dimension(30, 30));
|
375 |
panel.add(jLabel); |
376 |
panel.add(getJTxtTolerance()); |
377 |
panel.add(jLabel1); |
378 |
|
379 |
jPanelNord = new JPanel(); |
380 |
jPanelNord.setLayout(new BorderLayout()); |
381 |
jPanelNord.add(panel,BorderLayout.LINE_START);
|
382 |
ConfigurableActionsMamager cfgActionsManager = ToolsUtilLocator.getConfigurableActionsMamager(); |
383 |
JComponent c = cfgActionsManager.getConfigurableActionsComponent("SnappingPropertiesPage", this); |
384 |
jPanelNord.add(c, BorderLayout.LINE_END);
|
385 |
} |
386 |
return jPanelNord;
|
387 |
} |
388 |
|
389 |
/**
|
390 |
* This method initializes jPanelCache
|
391 |
*
|
392 |
* @return javax.swing.JPanel
|
393 |
*/
|
394 |
private JPanel getJPanelCache() { |
395 |
if (jPanelCache == null) { |
396 |
jPanelCache = new JPanel(); |
397 |
jPanelCache.setLayout(new BorderLayout()); |
398 |
jPanelCache.setBorder(new EmptyBorder(10, 10, 10, 10)); |
399 |
jPanelCache.add(jLabelCache, java.awt.BorderLayout.NORTH); |
400 |
jPanelCache.add(getJScrollPane(), java.awt.BorderLayout.CENTER); |
401 |
} |
402 |
return jPanelCache;
|
403 |
} |
404 |
|
405 |
/**
|
406 |
* This method initializes jTxtTolerance
|
407 |
*
|
408 |
* @return javax.swing.JTextField
|
409 |
*/
|
410 |
public JTextField getJTxtTolerance() { |
411 |
if (jTxtTolerance == null) { |
412 |
jTxtTolerance = new JTextField(); |
413 |
jTxtTolerance.setPreferredSize(new java.awt.Dimension(28, 20)); |
414 |
jTxtTolerance.setName("jTxtTolerance");
|
415 |
jTxtTolerance.setHorizontalAlignment(javax.swing.JTextField.RIGHT); |
416 |
jTxtTolerance.setText("4");
|
417 |
jTxtTolerance.setBounds(new java.awt.Rectangle(142, 8, 39, 15)); |
418 |
jTxtTolerance.addKeyListener(new KeyListener() { |
419 |
public void keyPressed(KeyEvent e) { changed = true; } |
420 |
public void keyReleased(KeyEvent e) { changed = true; } |
421 |
public void keyTyped(KeyEvent e){ changed = true; } |
422 |
}); |
423 |
} |
424 |
return jTxtTolerance;
|
425 |
} |
426 |
|
427 |
/**
|
428 |
* This method initializes jScrollPane
|
429 |
*
|
430 |
* @return javax.swing.JScrollPane
|
431 |
*/
|
432 |
private JScrollPane getJScrollPane() { |
433 |
if (jScrollPane == null) { |
434 |
jScrollPane = new JScrollPane(); |
435 |
jScrollPane.setBorder(new LineBorder(Color.GRAY)); |
436 |
jScrollPane.setPreferredSize(new java.awt.Dimension(500,419)); |
437 |
jScrollPane.setViewportView(getJTableSnapping()); |
438 |
} |
439 |
return jScrollPane;
|
440 |
} |
441 |
|
442 |
/**
|
443 |
* This method initializes jTableSnapping
|
444 |
*
|
445 |
* @return javax.swing.JTable
|
446 |
*/
|
447 |
public JTable getJTableSnapping() { |
448 |
if (jTableSnapping == null) { |
449 |
jTableSnapping = new JTable(); |
450 |
jTableSnapping.addKeyListener(new KeyListener() { |
451 |
public void keyPressed(KeyEvent e) { changed = true; } |
452 |
public void keyReleased(KeyEvent e) { changed = true; } |
453 |
public void keyTyped(KeyEvent e){ changed = true; } |
454 |
}); |
455 |
} |
456 |
return jTableSnapping;
|
457 |
} |
458 |
|
459 |
|
460 |
/**
|
461 |
* @see org.gvsig.andami.ui.mdiManager.SingletonWindow#getWindowModel()
|
462 |
*/
|
463 |
public Object getWindowModel() { |
464 |
return view;
|
465 |
} |
466 |
|
467 |
|
468 |
public boolean isAcceppted() { |
469 |
return isAcceppted;
|
470 |
} |
471 |
|
472 |
} |