Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / preferences / EditionPreferencePage.java @ 10254

History | View | Annotate | Download (15 KB)

1 5793 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.preferences;
42
43
import java.awt.BorderLayout;
44 6805 jaume
import java.awt.event.KeyEvent;
45
import java.awt.event.KeyListener;
46 5793 fjp
import java.util.ArrayList;
47
48
import javax.swing.ImageIcon;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52
import javax.swing.JSeparator;
53
import javax.swing.JTable;
54
import javax.swing.JTextField;
55
import javax.swing.table.AbstractTableModel;
56
import javax.swing.table.TableModel;
57
58
import com.iver.andami.PluginServices;
59 5813 fjp
import com.iver.andami.preferences.AbstractPreferencePage;
60 6660 caballero
import com.iver.andami.preferences.StoreException;
61 5813 fjp
import com.iver.cit.gvsig.CADExtension;
62
import com.iver.cit.gvsig.EditionManager;
63 8181 fjp
import com.iver.cit.gvsig.fmap.MapContext;
64 5793 fjp
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.fmap.layers.FLayers;
66
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
67 5813 fjp
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
68 5817 fjp
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
69 5813 fjp
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
70 5793 fjp
71
public class EditionPreferencePage extends AbstractPreferencePage {
72
        private JLabel jLabel = null;
73 5813 fjp
74 5793 fjp
        private JTextField jTxtTolerance = null;
75 5813 fjp
76 5793 fjp
        private JLabel jLabel1 = null;
77 5813 fjp
78 5793 fjp
        private JSeparator jSeparator = null;
79 5813 fjp
80 5793 fjp
        private JScrollPane jScrollPane = null;
81 5813 fjp
82 5793 fjp
        private JTable jTableSnapping = null;
83 5813 fjp
84 5793 fjp
        private JLabel jLabelCache = null;
85 5813 fjp
86 5793 fjp
        private JPanel jPanelNord = null;
87 5813 fjp
88 5793 fjp
        private JPanel jPanelCache = null;
89 6805 jaume
        private boolean changed = false;
90 5813 fjp
91 5793 fjp
        private FLayers layers;
92 5813 fjp
93 8181 fjp
        private MapContext mapContext;
94
95 5813 fjp
        private class MyRecord {
96 5793 fjp
                public Boolean bSelec = new Boolean(false);
97 5813 fjp
98 5793 fjp
                public String layerName;
99 5813 fjp
100 5793 fjp
                public Integer maxFeat = new Integer(1000);
101
        }
102 5813 fjp
103
        private class MyTableModel extends AbstractTableModel {
104 5793 fjp
                private ArrayList records = new ArrayList();
105 5813 fjp
106
                public MyTableModel(FLayers layers) {
107 5793 fjp
                        addLayer(layers);
108
                }
109
110 5813 fjp
                private void addLayer(FLayer lyr) {
111
                        if (lyr instanceof FLayers) {
112 5793 fjp
                                FLayers lyrGroup = (FLayers) lyr;
113 5813 fjp
                                for (int i = 0; i < lyrGroup.getLayersCount(); i++) {
114
                                        FLayer lyr2 = lyrGroup.getLayer(i);
115 5793 fjp
                                        addLayer(lyr2);
116
                                }
117 5813 fjp
                        } else {
118
                                if (lyr instanceof FLyrVect) {
119
                                        FLyrVect aux = (FLyrVect) lyr;
120 5793 fjp
                                        MyRecord rec = new MyRecord();
121
                                        rec.layerName = lyr.getName();
122 5813 fjp
                                        rec.bSelec = new Boolean(aux.isSpatialCacheEnabled());
123
                                        rec.maxFeat = new Integer(aux.getSpatialCache()
124
                                                        .getMaxFeatures());
125
                                        records.add(rec);
126 5793 fjp
                                }
127
                        }
128 5813 fjp
                }
129 5793 fjp
130
                public int getColumnCount() {
131
                        return 3;
132
                }
133
134
                public int getRowCount() {
135
                        return records.size();
136
                }
137
138
                public Object getValueAt(int rowIndex, int columnIndex) {
139
                        MyRecord rec = (MyRecord) records.get(rowIndex);
140
                        if (columnIndex == 0)
141
                                return rec.bSelec;
142
                        if (columnIndex == 1)
143
                                return rec.layerName;
144
                        if (columnIndex == 2)
145
                                return rec.maxFeat;
146
                        return null;
147 5813 fjp
148 5793 fjp
                }
149
150
                public Class getColumnClass(int c) {
151
                        if (c == 0)
152
                                return Boolean.class;
153
                        if (c == 2)
154
                                return Integer.class;
155 5813 fjp
                        return String.class;
156 5793 fjp
                }
157
158
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
159
                        MyRecord rec = (MyRecord) records.get(rowIndex);
160
                        if (columnIndex == 0)
161
                                rec.bSelec = (Boolean) aValue;
162 5813 fjp
                        if (columnIndex == 2) {
163 5793 fjp
                                if (aValue != null)
164
                                        rec.maxFeat = (Integer) aValue;
165
                                else
166
                                        rec.maxFeat = new Integer(0);
167
                        }
168 8330 fjp
                        changed  =true;
169 5793 fjp
                        super.setValueAt(aValue, rowIndex, columnIndex);
170
                }
171
172
                public boolean isCellEditable(int rowIndex, int columnIndex) {
173
                        if (columnIndex == 0)
174
                                return true;
175
                        if (columnIndex == 2)
176
                                return true;
177
178
                        return false;
179
                }
180
181
                public String getColumnName(int column) {
182
                        if (column == 0)
183
                                return PluginServices.getText(this, "Selected");
184
                        if (column == 1)
185
                                return PluginServices.getText(this, "LayerName");
186
                        if (column == 2)
187
                                return PluginServices.getText(this, "MaxFeaturesEditionCache");
188
                        return "You shouldn't reach this point";
189
190
                }
191 5813 fjp
192 5793 fjp
        }
193
194
        /**
195 5813 fjp
         * This method initializes
196 6606 caballero
         *
197 5793 fjp
         */
198
        public EditionPreferencePage() {
199
                super();
200
                initialize();
201
        }
202
203 5813 fjp
        /*
204
         * private void addLayer(FLayer lyr) { if (lyr instanceof FLayers) { FLayers
205
         * lyrGroup = (FLayers) lyr; for (int i=0; i < lyrGroup.getLayersCount();
206
         * i++) { FLayer lyr2 = lyrGroup.getLayer(i); addLayer(lyr2); } } else { if
207
         * (lyr instanceof FLyrVect) { layers.add(lyr); } } }
208
         */
209
210 5793 fjp
        /**
211
         * This method initializes this
212 6606 caballero
         *
213 5793 fjp
         */
214
        private void initialize() {
215
                BorderLayout layout = new BorderLayout();
216
                layout.setHgap(20);
217
218 5813 fjp
                this.setLayout(layout);
219
220
                jLabelCache = new JLabel();
221
                jLabelCache
222
                                .setText(PluginServices.getText(this, "capas_edition_cache"));
223
                jLabelCache.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
224 7768 caballero
                jLabelCache.setPreferredSize(new java.awt.Dimension(500,20));
225 5813 fjp
                jLabelCache.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
226
                jLabel1 = new JLabel();
227
                jLabel1.setText("pixels");
228
                jLabel1.setBounds(new java.awt.Rectangle(195, 8, 207, 15));
229
                jLabel1.setPreferredSize(new java.awt.Dimension(28, 20));
230
                jLabel1.setName("jLabel1");
231
                jLabel = new JLabel();
232
                jLabel.setText("Snap Tolerance:");
233
                jLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
234
                jLabel.setName("jLabel");
235
                jLabel.setBounds(new java.awt.Rectangle(15, 8, 122, 15));
236
                jLabel.setPreferredSize(new java.awt.Dimension(28, 20));
237
                jLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
238
239 7768 caballero
                this.setSize(new java.awt.Dimension(502,288));
240 5813 fjp
                this.setPreferredSize(this.getSize());
241
                this.add(getJPanelNord(), BorderLayout.NORTH);
242
243
                this.add(getJSeparator(), BorderLayout.CENTER);
244
245
                this.add(getJPanelCache(), BorderLayout.CENTER);
246
247 5793 fjp
        }
248
249
        public String getID() {
250
                return this.getClass().getName();
251
        }
252
253
        public String getTitle() {
254 5813 fjp
                return PluginServices.getText(this, "Edition");
255 5793 fjp
        }
256
257
        public JPanel getPanel() {
258
                return this;
259
        }
260
261 5813 fjp
        /*
262
         * (non-Javadoc)
263 6606 caballero
         *
264 5793 fjp
         * @see com.iver.cit.gvsig.gui.preferences.IPreference#initializeValues()
265
         */
266
        public void initializeValues() {
267 5813 fjp
                // /* Vamos a usar esto por ahora as?:
268
                // * Al abrir el dialogo, miramos las capas que hay
269
                // * en edici?n y las capas activas.
270
                // * Las capas en edici?n nos las guardamos para
271
                // * fijarles las propiedades, y las que est?n activas
272
                // * las metemos en la tabla de configuraci?n de
273
                // * snapping.
274
                // */
275
                // FLyrVect firstLyrVect = null;
276
                // for (int i=0; i<layers.getLayersCount(); i++)
277
                // {
278
                // FLayer aux = layers.getLayer(i);
279
                // if (aux.isActive())
280
                // if (aux instanceof FLyrVect)
281
                // {
282
                // firstLyrVect = (FLyrVect) aux;
283
                // }
284
                // }
285 6606 caballero
                //
286 5813 fjp
                // TableModel tm = getJTableSnapping().getModel();
287
                // for (int i=0; i < tm.getRowCount(); i++)
288
                // {
289
                // String layerName = (String) tm.getValueAt(i, 1);
290
                // FLayer layer = layers.getLayer(layerName);
291
                // FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
292
                // Boolean bUseCache = (Boolean) tm.getValueAt(i,0);
293
                // Integer maxFeat = (Integer) tm.getValueAt(i,2);
294
                // lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
295
                // lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
296
                // }
297
                //
298 5793 fjp
299
        }
300
301 6660 caballero
        public void storeValues() throws StoreException {
302 5793 fjp
                TableModel tm = getJTableSnapping().getModel();
303 5813 fjp
                ArrayList layersToSnap = new ArrayList();
304
                for (int i = 0; i < tm.getRowCount(); i++) {
305 5793 fjp
                        String layerName = (String) tm.getValueAt(i, 1);
306
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
307 5813 fjp
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
308
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
309
310
                        // Decidimos si vamos a habilitar el spatialCache DESPUES, justo
311
                        // antes de renderizar.
312
                        // Necesitamos un m?todo que explore las capas en edici?n y mire las
313
                        // capas sobre las
314
                        // que se necestia el cache. Aqu? lo que hacemos es a?adir las
315
                        // seleccionadas a la
316
                        // lista de capas asociadas al snapping de los temas activos en
317
                        // edici?n.
318
                        // Lo del m?ximo de features en cach?, tiene que ser para cada capa
319
                        // distinto. Pero no
320
                        // puedes "chafar" el que ya hay, porque puedes fastidiar a otra
321
                        // capa en edici?n.
322
                        // Como m?ximo, lo que podemos hacer es que si es mayor al que hay,
323
                        // lo subimos. Si
324
                        // se solicita uno menor, lo dejamos como est?.
325
                        // Otra opci?n ser?a no hacer caso de esto para cada capa, y ponerlo
326
                        // de forma global.
327
                        // lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
328 5793 fjp
                        lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
329 5813 fjp
                        if (bUseCache.booleanValue())
330
                                layersToSnap.add(lyr);
331 5793 fjp
                }
332 5813 fjp
                SingleLayerIterator it = new SingleLayerIterator(layers);
333
                EditionManager edManager = CADExtension.getEditionManager();
334 5793 fjp
335 5813 fjp
                while (it.hasNext()) {
336
                        FLayer aux = it.next();
337
                        if (aux instanceof FLyrVect)
338
                        {
339
                                FLyrVect lyrVect = (FLyrVect) aux;
340
                                // Inicializamos todas
341
                                lyrVect.setSpatialCacheEnabled(false);
342
                                if (aux.isActive())
343
                                        if (aux.isEditing()) {
344
                                                // Sobre la capa en edici?n siempre se puede hacer snapping
345 6606 caballero
                                                lyrVect.setSpatialCacheEnabled(true);
346 5813 fjp
                                                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
347
                                                                .getLayerEdited(aux);
348
                                                lyrEd.setLayersToSnap(layersToSnap);
349
350
                                        }
351
                        }
352
                } // while
353
                it.rewind();
354
                /*
355
                 * Iteramos por las capas en edici?n y marcamos aquellas capas que
356
                 * necesitan trabajar con el cache habilitado
357
                 */
358
                while (it.hasNext()) {
359
                        FLayer aux = it.next();
360
                        if (aux.isEditing())
361
                                if (aux instanceof FLyrVect) {
362
                                                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
363
                                                                .getLayerEdited(aux);
364
                                                for (int i=0; i<lyrEd.getLayersToSnap().size(); i++)
365
                                                {
366
                                                        FLyrVect lyrVect = (FLyrVect) lyrEd.getLayersToSnap().get(i);
367
                                                        lyrVect.setSpatialCacheEnabled(true);
368
                                                }
369
370
                                }
371
372
                } // while
373 8181 fjp
                mapContext.redraw();
374 6660 caballero
                try{
375
                        SelectionCADTool.tolerance = Integer.parseInt(getJTxtTolerance().getText());
376 6811 jaume
377 6660 caballero
                }catch (Exception e) {
378
                        throw new StoreException(PluginServices.getText(this, "tolerancia_incorrecta"),e);
379
                }
380 5793 fjp
        }
381
382
        public void initializeDefaults() {
383 7851 jmvivo
                getJTxtTolerance().setText("4");
384 5793 fjp
                TableModel tm = getJTableSnapping().getModel();
385 5813 fjp
                for (int i = 0; i < tm.getRowCount(); i++) {
386 5793 fjp
                        String layerName = (String) tm.getValueAt(i, 1);
387
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
388 5813 fjp
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
389
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
390 5793 fjp
                        lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
391
                        lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
392
                }
393 5813 fjp
394 5793 fjp
        }
395
396
        public ImageIcon getIcon() {
397
                return null;
398
        }
399 5813 fjp
400 8181 fjp
        public void setMapContext(MapContext mc) {
401 5813 fjp
                // addLayer(layers);
402 8181 fjp
                this.mapContext = mc;
403
                this.layers = mc.getLayers();
404 5793 fjp
                MyTableModel tm = new MyTableModel(layers);
405
                getJTableSnapping().setModel(tm);
406 5817 fjp
                getJTxtTolerance().setText(String.valueOf(SelectionCADTool.tolerance));
407 5793 fjp
        }
408
409
        /**
410 5813 fjp
         * This method initializes jTxtTolerance
411 6606 caballero
         *
412 5813 fjp
         * @return javax.swing.JTextField
413 5793 fjp
         */
414
        private JTextField getJTxtTolerance() {
415
                if (jTxtTolerance == null) {
416
                        jTxtTolerance = new JTextField();
417 5813 fjp
                        jTxtTolerance.setPreferredSize(new java.awt.Dimension(28, 20));
418 5793 fjp
                        jTxtTolerance.setName("jTxtTolerance");
419 5817 fjp
                        jTxtTolerance.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
420
                        jTxtTolerance.setText("4");
421 5813 fjp
                        jTxtTolerance.setBounds(new java.awt.Rectangle(142, 8, 39, 15));
422 6805 jaume
                        jTxtTolerance.addKeyListener(new KeyListener() {
423
                       public void keyPressed(KeyEvent e) { changed = true; }
424
                                public void keyReleased(KeyEvent e) { changed = true; }
425
                                public void keyTyped(KeyEvent e){ changed = true; }
426
                        });
427 5793 fjp
                }
428
                return jTxtTolerance;
429
        }
430
431
        /**
432 5813 fjp
         * This method initializes jSeparator
433 6606 caballero
         *
434 5813 fjp
         * @return javax.swing.JSeparator
435 5793 fjp
         */
436
        private JSeparator getJSeparator() {
437
                if (jSeparator == null) {
438
                        jSeparator = new JSeparator();
439 5817 fjp
                        jSeparator.setPreferredSize(new java.awt.Dimension(200,2));
440 5793 fjp
                }
441
                return jSeparator;
442
        }
443
444
        /**
445 5813 fjp
         * This method initializes jScrollPane
446 6606 caballero
         *
447 5813 fjp
         * @return javax.swing.JScrollPane
448 5793 fjp
         */
449
        private JScrollPane getJScrollPane() {
450
                if (jScrollPane == null) {
451
                        jScrollPane = new JScrollPane();
452 7768 caballero
                        jScrollPane.setPreferredSize(new java.awt.Dimension(500,419));
453 5793 fjp
                        jScrollPane.setViewportView(getJTableSnapping());
454
                }
455
                return jScrollPane;
456
        }
457
458
        /**
459 5813 fjp
         * This method initializes jTableSnapping
460 6606 caballero
         *
461 5813 fjp
         * @return javax.swing.JTable
462 5793 fjp
         */
463
        private JTable getJTableSnapping() {
464
                if (jTableSnapping == null) {
465
                        jTableSnapping = new JTable();
466 5813 fjp
                        // TableColumnModel cm = new DefaultTableColumnModel();
467
                        // TableColumn checkCol = new TableColumn(0, 50);
468
                        // cm.addColumn(checkCol);
469 6606 caballero
                        //
470 5813 fjp
                        // TableColumn layerCol = new TableColumn(1, 250);
471
                        // cm.addColumn(layerCol);
472 6606 caballero
                        //
473 5813 fjp
                        // TableColumn maxFeatCol = new TableColumn(2, 50);
474
                        // cm.addColumn(maxFeatCol);
475
                        //
476
                        // JTableHeader head = new JTableHeader(cm);
477
                        // head.setVisible(true);
478 6606 caballero
                        //
479
                        //
480 5813 fjp
                        // TableModel tm = new DefaultTableModel(4,3);
481
                        // jTableSnapping.setModel(tm);
482
                        // jTableSnapping.setTableHeader(head);
483 6805 jaume
                        jTableSnapping.addKeyListener(new KeyListener() {
484
                       public void keyPressed(KeyEvent e) { changed = true; }
485
                                public void keyReleased(KeyEvent e) { changed = true; }
486
                                public void keyTyped(KeyEvent e){ changed = true; }
487
                        });
488 5793 fjp
                }
489
                return jTableSnapping;
490
        }
491
492
        /**
493 5813 fjp
         * This method initializes jPanelNord
494 6606 caballero
         *
495 5813 fjp
         * @return javax.swing.JPanel
496 5793 fjp
         */
497
        private JPanel getJPanelNord() {
498
                if (jPanelNord == null) {
499
                        jPanelNord = new JPanel();
500
                        jPanelNord.setLayout(null);
501 5813 fjp
                        jPanelNord
502
                                        .setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
503
                        jPanelNord.setPreferredSize(new java.awt.Dimension(30, 30));
504 5793 fjp
                        jPanelNord.add(jLabel, null);
505
                        jPanelNord.add(getJTxtTolerance(), null);
506
                        jPanelNord.add(jLabel1, null);
507
508
                }
509
                return jPanelNord;
510
        }
511
512
        /**
513 5813 fjp
         * This method initializes jPanelCache
514 6606 caballero
         *
515 5813 fjp
         * @return javax.swing.JPanel
516 5793 fjp
         */
517
        private JPanel getJPanelCache() {
518
                if (jPanelCache == null) {
519
                        jPanelCache = new JPanel();
520 7768 caballero
                        jPanelCache.setLayout(new BorderLayout());
521
                        jPanelCache.add(jLabelCache, java.awt.BorderLayout.NORTH);
522
                        jPanelCache.add(getJScrollPane(), java.awt.BorderLayout.EAST);
523 5793 fjp
                }
524
                return jPanelCache;
525
        }
526
527 6805 jaume
        public boolean isValueChanged() {
528
                return changed;
529 6606 caballero
        }
530
531 6811 jaume
        public void setChangesApplied() {
532
                changed = false;
533
        }
534
535 7768 caballero
}  //  @jve:decl-index=0:visual-constraint="14,10"