Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_907 / extensions / extCAD / src / com / iver / cit / gvsig / gui / preferences / EditionPreferencePage.java @ 11015

History | View | Annotate | Download (15 KB)

1
/* 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
import java.awt.event.KeyEvent;
45
import java.awt.event.KeyListener;
46
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
import com.iver.andami.preferences.AbstractPreferencePage;
60
import com.iver.andami.preferences.StoreException;
61
import com.iver.cit.gvsig.CADExtension;
62
import com.iver.cit.gvsig.EditionManager;
63
import com.iver.cit.gvsig.fmap.MapContext;
64
import com.iver.cit.gvsig.fmap.MapControl;
65
import com.iver.cit.gvsig.fmap.layers.FLayer;
66
import com.iver.cit.gvsig.fmap.layers.FLayers;
67
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
68
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
69
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
70
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
71

    
72
public class EditionPreferencePage extends AbstractPreferencePage {
73
        private JLabel jLabel = null;
74

    
75
        private JTextField jTxtTolerance = null;
76

    
77
        private JLabel jLabel1 = null;
78

    
79
        private JSeparator jSeparator = null;
80

    
81
        private JScrollPane jScrollPane = null;
82

    
83
        private JTable jTableSnapping = null;
84

    
85
        private JLabel jLabelCache = null;
86

    
87
        private JPanel jPanelNord = null;
88

    
89
        private JPanel jPanelCache = null;
90
        private boolean changed = false;
91

    
92
        private FLayers layers;
93

    
94
        private MapContext mapContext;
95

    
96
        private class MyRecord {
97
                public Boolean bSelec = new Boolean(false);
98

    
99
                public String layerName;
100

    
101
                public Integer maxFeat = new Integer(1000);
102
        }
103

    
104
        private class MyTableModel extends AbstractTableModel {
105
                private ArrayList records = new ArrayList();
106

    
107
                public MyTableModel(FLayers layers) {
108
                        addLayer(layers);
109
                }
110

    
111
                private void addLayer(FLayer lyr) {
112
                        if (lyr instanceof FLayers) {
113
                                FLayers lyrGroup = (FLayers) lyr;
114
                                for (int i = 0; i < lyrGroup.getLayersCount(); i++) {
115
                                        FLayer lyr2 = lyrGroup.getLayer(i);
116
                                        addLayer(lyr2);
117
                                }
118
                        } else {
119
                                if (lyr instanceof FLyrVect) {
120
                                        FLyrVect aux = (FLyrVect) lyr;
121
                                        MyRecord rec = new MyRecord();
122
                                        rec.layerName = lyr.getName();
123
                                        rec.bSelec = new Boolean(aux.isSpatialCacheEnabled());
124
                                        rec.maxFeat = new Integer(aux.getSpatialCache()
125
                                                        .getMaxFeatures());
126
                                        records.add(rec);
127
                                }
128
                        }
129
                }
130

    
131
                public int getColumnCount() {
132
                        return 3;
133
                }
134

    
135
                public int getRowCount() {
136
                        return records.size();
137
                }
138

    
139
                public Object getValueAt(int rowIndex, int columnIndex) {
140
                        MyRecord rec = (MyRecord) records.get(rowIndex);
141
                        if (columnIndex == 0)
142
                                return rec.bSelec;
143
                        if (columnIndex == 1)
144
                                return rec.layerName;
145
                        if (columnIndex == 2)
146
                                return rec.maxFeat;
147
                        return null;
148

    
149
                }
150

    
151
                public Class getColumnClass(int c) {
152
                        if (c == 0)
153
                                return Boolean.class;
154
                        if (c == 2)
155
                                return Integer.class;
156
                        return String.class;
157
                }
158

    
159
                public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
160
                        MyRecord rec = (MyRecord) records.get(rowIndex);
161
                        if (columnIndex == 0)
162
                                rec.bSelec = (Boolean) aValue;
163
                        if (columnIndex == 2) {
164
                                if (aValue != null)
165
                                        rec.maxFeat = (Integer) aValue;
166
                                else
167
                                        rec.maxFeat = new Integer(0);
168
                        }
169
                        changed  =true;
170
                        super.setValueAt(aValue, rowIndex, columnIndex);
171
                }
172

    
173
                public boolean isCellEditable(int rowIndex, int columnIndex) {
174
                        if (columnIndex == 0)
175
                                return true;
176
                        if (columnIndex == 2)
177
                                return true;
178

    
179
                        return false;
180
                }
181

    
182
                public String getColumnName(int column) {
183
                        if (column == 0)
184
                                return PluginServices.getText(this, "Selected");
185
                        if (column == 1)
186
                                return PluginServices.getText(this, "LayerName");
187
                        if (column == 2)
188
                                return PluginServices.getText(this, "MaxFeaturesEditionCache");
189
                        return "You shouldn't reach this point";
190

    
191
                }
192

    
193
        }
194

    
195
        /**
196
         * This method initializes
197
         *
198
         */
199
        public EditionPreferencePage() {
200
                super();
201
                initialize();
202
        }
203

    
204
        /*
205
         * private void addLayer(FLayer lyr) { if (lyr instanceof FLayers) { FLayers
206
         * lyrGroup = (FLayers) lyr; for (int i=0; i < lyrGroup.getLayersCount();
207
         * i++) { FLayer lyr2 = lyrGroup.getLayer(i); addLayer(lyr2); } } else { if
208
         * (lyr instanceof FLyrVect) { layers.add(lyr); } } }
209
         */
210

    
211
        /**
212
         * This method initializes this
213
         *
214
         */
215
        private void initialize() {
216
                BorderLayout layout = new BorderLayout();
217
                layout.setHgap(20);
218

    
219
                this.setLayout(layout);
220

    
221
                jLabelCache = new JLabel();
222
                jLabelCache
223
                                .setText(PluginServices.getText(this, "capas_edition_cache"));
224
                jLabelCache.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
225
                jLabelCache.setPreferredSize(new java.awt.Dimension(500,20));
226
                jLabelCache.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
227
                jLabel1 = new JLabel();
228
                jLabel1.setText("pixels");
229
                jLabel1.setBounds(new java.awt.Rectangle(195, 8, 207, 15));
230
                jLabel1.setPreferredSize(new java.awt.Dimension(28, 20));
231
                jLabel1.setName("jLabel1");
232
                jLabel = new JLabel();
233
                jLabel.setText("Snap Tolerance:");
234
                jLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
235
                jLabel.setName("jLabel");
236
                jLabel.setBounds(new java.awt.Rectangle(15, 8, 122, 15));
237
                jLabel.setPreferredSize(new java.awt.Dimension(28, 20));
238
                jLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
239

    
240
                this.setSize(new java.awt.Dimension(502,288));
241
                this.setPreferredSize(this.getSize());
242
                this.add(getJPanelNord(), BorderLayout.NORTH);
243

    
244
                this.add(getJSeparator(), BorderLayout.CENTER);
245

    
246
                this.add(getJPanelCache(), BorderLayout.CENTER);
247

    
248
        }
249

    
250
        public String getID() {
251
                return this.getClass().getName();
252
        }
253

    
254
        public String getTitle() {
255
                return PluginServices.getText(this, "Edition");
256
        }
257

    
258
        public JPanel getPanel() {
259
                return this;
260
        }
261

    
262
        /*
263
         * (non-Javadoc)
264
         *
265
         * @see com.iver.cit.gvsig.gui.preferences.IPreference#initializeValues()
266
         */
267
        public void initializeValues() {
268
                // /* Vamos a usar esto por ahora as?:
269
                // * Al abrir el dialogo, miramos las capas que hay
270
                // * en edici?n y las capas activas.
271
                // * Las capas en edici?n nos las guardamos para
272
                // * fijarles las propiedades, y las que est?n activas
273
                // * las metemos en la tabla de configuraci?n de
274
                // * snapping.
275
                // */
276
                // FLyrVect firstLyrVect = null;
277
                // for (int i=0; i<layers.getLayersCount(); i++)
278
                // {
279
                // FLayer aux = layers.getLayer(i);
280
                // if (aux.isActive())
281
                // if (aux instanceof FLyrVect)
282
                // {
283
                // firstLyrVect = (FLyrVect) aux;
284
                // }
285
                // }
286
                //
287
                // TableModel tm = getJTableSnapping().getModel();
288
                // for (int i=0; i < tm.getRowCount(); i++)
289
                // {
290
                // String layerName = (String) tm.getValueAt(i, 1);
291
                // FLayer layer = layers.getLayer(layerName);
292
                // FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
293
                // Boolean bUseCache = (Boolean) tm.getValueAt(i,0);
294
                // Integer maxFeat = (Integer) tm.getValueAt(i,2);
295
                // lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
296
                // lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
297
                // }
298
                //
299

    
300
        }
301

    
302
        public void storeValues() throws StoreException {
303
                TableModel tm = getJTableSnapping().getModel();
304
                ArrayList layersToSnap = new ArrayList();
305
                for (int i = 0; i < tm.getRowCount(); i++) {
306
                        String layerName = (String) tm.getValueAt(i, 1);
307
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
308
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
309
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
310

    
311
                        // Decidimos si vamos a habilitar el spatialCache DESPUES, justo
312
                        // antes de renderizar.
313
                        // Necesitamos un m?todo que explore las capas en edici?n y mire las
314
                        // capas sobre las
315
                        // que se necestia el cache. Aqu? lo que hacemos es a?adir las
316
                        // seleccionadas a la
317
                        // lista de capas asociadas al snapping de los temas activos en
318
                        // edici?n.
319
                        // Lo del m?ximo de features en cach?, tiene que ser para cada capa
320
                        // distinto. Pero no
321
                        // puedes "chafar" el que ya hay, porque puedes fastidiar a otra
322
                        // capa en edici?n.
323
                        // Como m?ximo, lo que podemos hacer es que si es mayor al que hay,
324
                        // lo subimos. Si
325
                        // se solicita uno menor, lo dejamos como est?.
326
                        // Otra opci?n ser?a no hacer caso de esto para cada capa, y ponerlo
327
                        // de forma global.
328
                        // lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
329
                        lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
330
                        if (bUseCache.booleanValue())
331
                                layersToSnap.add(lyr);
332
                }
333
                SingleLayerIterator it = new SingleLayerIterator(layers);
334
                EditionManager edManager = CADExtension.getEditionManager();
335

    
336
                while (it.hasNext()) {
337
                        FLayer aux = it.next();
338
                        if (aux instanceof FLyrVect)
339
                        {
340
                                FLyrVect lyrVect = (FLyrVect) aux;
341
                                // Inicializamos todas
342
                                lyrVect.setSpatialCacheEnabled(false);
343
                                if (aux.isActive())
344
                                        if (aux.isEditing()) {
345
                                                // Sobre la capa en edici?n siempre se puede hacer snapping
346
                                                lyrVect.setSpatialCacheEnabled(true);
347
                                                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
348
                                                                .getLayerEdited(aux);
349
                                                lyrEd.setLayersToSnap(layersToSnap);
350

    
351
                                        }
352
                        }
353
                } // while
354
                it.rewind();
355
                /*
356
                 * Iteramos por las capas en edici?n y marcamos aquellas capas que
357
                 * necesitan trabajar con el cache habilitado
358
                 */
359
                while (it.hasNext()) {
360
                        FLayer aux = it.next();
361
                        if (aux.isEditing())
362
                                if (aux instanceof FLyrVect) {
363
                                                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
364
                                                                .getLayerEdited(aux);
365
                                                for (int i=0; i<lyrEd.getLayersToSnap().size(); i++)
366
                                                {
367
                                                        FLyrVect lyrVect = (FLyrVect) lyrEd.getLayersToSnap().get(i);
368
                                                        lyrVect.setSpatialCacheEnabled(true);
369
                                                }
370

    
371
                                }
372

    
373
                } // while
374
                mapContext.redraw();
375
                try{
376
                        SelectionCADTool.tolerance = Integer.parseInt(getJTxtTolerance().getText());
377

    
378
                }catch (Exception e) {
379
                        throw new StoreException(PluginServices.getText(this, "tolerancia_incorrecta"),e);
380
                }
381
        }
382

    
383
        public void initializeDefaults() {
384
                getJTxtTolerance().setText("4");
385
                TableModel tm = getJTableSnapping().getModel();
386
                for (int i = 0; i < tm.getRowCount(); i++) {
387
                        String layerName = (String) tm.getValueAt(i, 1);
388
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
389
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
390
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
391
                        lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
392
                        lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
393
                }
394

    
395
        }
396

    
397
        public ImageIcon getIcon() {
398
                return null;
399
        }
400

    
401
        public void setMapContext(MapContext mc) {
402
                // addLayer(layers);
403
                this.mapContext = mc;
404
                this.layers = mc.getLayers();
405
                MyTableModel tm = new MyTableModel(layers);
406
                getJTableSnapping().setModel(tm);
407
                getJTxtTolerance().setText(String.valueOf(SelectionCADTool.tolerance));
408
        }
409

    
410
        /**
411
         * This method initializes jTxtTolerance
412
         *
413
         * @return javax.swing.JTextField
414
         */
415
        private JTextField getJTxtTolerance() {
416
                if (jTxtTolerance == null) {
417
                        jTxtTolerance = new JTextField();
418
                        jTxtTolerance.setPreferredSize(new java.awt.Dimension(28, 20));
419
                        jTxtTolerance.setName("jTxtTolerance");
420
                        jTxtTolerance.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
421
                        jTxtTolerance.setText("4");
422
                        jTxtTolerance.setBounds(new java.awt.Rectangle(142, 8, 39, 15));
423
                        jTxtTolerance.addKeyListener(new KeyListener() {
424
                       public void keyPressed(KeyEvent e) { changed = true; }
425
                                public void keyReleased(KeyEvent e) { changed = true; }
426
                                public void keyTyped(KeyEvent e){ changed = true; }
427
                        });
428
                }
429
                return jTxtTolerance;
430
        }
431

    
432
        /**
433
         * This method initializes jSeparator
434
         *
435
         * @return javax.swing.JSeparator
436
         */
437
        private JSeparator getJSeparator() {
438
                if (jSeparator == null) {
439
                        jSeparator = new JSeparator();
440
                        jSeparator.setPreferredSize(new java.awt.Dimension(200,2));
441
                }
442
                return jSeparator;
443
        }
444

    
445
        /**
446
         * This method initializes jScrollPane
447
         *
448
         * @return javax.swing.JScrollPane
449
         */
450
        private JScrollPane getJScrollPane() {
451
                if (jScrollPane == null) {
452
                        jScrollPane = new JScrollPane();
453
                        jScrollPane.setPreferredSize(new java.awt.Dimension(500,419));
454
                        jScrollPane.setViewportView(getJTableSnapping());
455
                }
456
                return jScrollPane;
457
        }
458

    
459
        /**
460
         * This method initializes jTableSnapping
461
         *
462
         * @return javax.swing.JTable
463
         */
464
        private JTable getJTableSnapping() {
465
                if (jTableSnapping == null) {
466
                        jTableSnapping = new JTable();
467
                        // TableColumnModel cm = new DefaultTableColumnModel();
468
                        // TableColumn checkCol = new TableColumn(0, 50);
469
                        // cm.addColumn(checkCol);
470
                        //
471
                        // TableColumn layerCol = new TableColumn(1, 250);
472
                        // cm.addColumn(layerCol);
473
                        //
474
                        // TableColumn maxFeatCol = new TableColumn(2, 50);
475
                        // cm.addColumn(maxFeatCol);
476
                        //
477
                        // JTableHeader head = new JTableHeader(cm);
478
                        // head.setVisible(true);
479
                        //
480
                        //
481
                        // TableModel tm = new DefaultTableModel(4,3);
482
                        // jTableSnapping.setModel(tm);
483
                        // jTableSnapping.setTableHeader(head);
484
                        jTableSnapping.addKeyListener(new KeyListener() {
485
                       public void keyPressed(KeyEvent e) { changed = true; }
486
                                public void keyReleased(KeyEvent e) { changed = true; }
487
                                public void keyTyped(KeyEvent e){ changed = true; }
488
                        });
489
                }
490
                return jTableSnapping;
491
        }
492

    
493
        /**
494
         * This method initializes jPanelNord
495
         *
496
         * @return javax.swing.JPanel
497
         */
498
        private JPanel getJPanelNord() {
499
                if (jPanelNord == null) {
500
                        jPanelNord = new JPanel();
501
                        jPanelNord.setLayout(null);
502
                        jPanelNord
503
                                        .setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
504
                        jPanelNord.setPreferredSize(new java.awt.Dimension(30, 30));
505
                        jPanelNord.add(jLabel, null);
506
                        jPanelNord.add(getJTxtTolerance(), null);
507
                        jPanelNord.add(jLabel1, null);
508

    
509
                }
510
                return jPanelNord;
511
        }
512

    
513
        /**
514
         * This method initializes jPanelCache
515
         *
516
         * @return javax.swing.JPanel
517
         */
518
        private JPanel getJPanelCache() {
519
                if (jPanelCache == null) {
520
                        jPanelCache = new JPanel();
521
                        jPanelCache.setLayout(new BorderLayout());
522
                        jPanelCache.add(jLabelCache, java.awt.BorderLayout.NORTH);
523
                        jPanelCache.add(getJScrollPane(), java.awt.BorderLayout.EAST);
524
                }
525
                return jPanelCache;
526
        }
527

    
528
        public boolean isValueChanged() {
529
                return changed;
530
        }
531

    
532
        public void setChangesApplied() {
533
                changed = false;
534
        }
535

    
536
}  //  @jve:decl-index=0:visual-constraint="14,10"
537