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