Statistics
| Revision:

root / branches / v10 / extensions / extCAD / src / com / iver / cit / gvsig / gui / preferences / EditionPreferencePage.java @ 7777

History | View | Annotate | Download (15.6 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.GridBagConstraints;
45
import java.awt.GridBagLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.KeyEvent;
49
import java.awt.event.KeyListener;
50
import java.util.ArrayList;
51

    
52
import javax.swing.ImageIcon;
53
import javax.swing.JLabel;
54
import javax.swing.JPanel;
55
import javax.swing.JScrollPane;
56
import javax.swing.JSeparator;
57
import javax.swing.JTable;
58
import javax.swing.JTextField;
59
import javax.swing.table.AbstractTableModel;
60
import javax.swing.table.TableModel;
61

    
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.preferences.AbstractPreferencePage;
64
import com.iver.andami.preferences.StoreException;
65
import com.iver.cit.gvsig.CADExtension;
66
import com.iver.cit.gvsig.EditionManager;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.FLayers;
69
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
70
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
71
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
72
import com.iver.cit.gvsig.layers.ILayerEdited;
73
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
74

    
75
public class EditionPreferencePage extends AbstractPreferencePage {
76
        private JLabel jLabel = null;
77

    
78
        private JTextField jTxtTolerance = null;
79

    
80
        private JLabel jLabel1 = null;
81

    
82
        private JSeparator jSeparator = null;
83

    
84
        private JScrollPane jScrollPane = null;
85

    
86
        private JTable jTableSnapping = null;
87

    
88
        private JLabel jLabelCache = null;
89

    
90
        private JPanel jPanelNord = null;
91

    
92
        private JPanel jPanelCache = null;
93
        private boolean changed = false;
94

    
95
        private FLayers layers;
96

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

    
100
                public String layerName;
101

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

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

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

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

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

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

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

    
150
                }
151

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

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

    
171
                        super.setValueAt(aValue, rowIndex, columnIndex);
172
                }
173

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

    
180
                        return false;
181
                }
182

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

    
192
                }
193

    
194
        }
195

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

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

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

    
220
                this.setLayout(layout);
221

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

    
241
                this.setSize(new java.awt.Dimension(426, 239));
242
                this.setPreferredSize(this.getSize());
243
                this.add(getJPanelNord(), BorderLayout.NORTH);
244

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

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

    
249
        }
250

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

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

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

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

    
301
        }
302

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

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

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

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

    
372
                                }
373

    
374
                } // while
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
                TableModel tm = getJTableSnapping().getModel();
385
                for (int i = 0; i < tm.getRowCount(); i++) {
386
                        String layerName = (String) tm.getValueAt(i, 1);
387
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
388
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
389
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
390
                        lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
391
                        lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
392
                }
393

    
394
        }
395

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

    
400
        public void setLayers(FLayers layers) {
401
                // addLayer(layers);
402
                this.layers = layers;
403
                MyTableModel tm = new MyTableModel(layers);
404
                getJTableSnapping().setModel(tm);
405
                getJTxtTolerance().setText(String.valueOf(SelectionCADTool.tolerance));
406
        }
407

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

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

    
443
        /**
444
         * This method initializes jScrollPane
445
         *
446
         * @return javax.swing.JScrollPane
447
         */
448
        private JScrollPane getJScrollPane() {
449
                if (jScrollPane == null) {
450
                        jScrollPane = new JScrollPane();
451
                        jScrollPane.setViewportView(getJTableSnapping());
452
                }
453
                return jScrollPane;
454
        }
455

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

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

    
506
                }
507
                return jPanelNord;
508
        }
509

    
510
        /**
511
         * This method initializes jPanelCache
512
         *
513
         * @return javax.swing.JPanel
514
         */
515
        private JPanel getJPanelCache() {
516
                if (jPanelCache == null) {
517
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
518
                        gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
519
                        gridBagConstraints1.gridx = 0;
520
                        gridBagConstraints1.weighty = 1.0;
521
                        gridBagConstraints1.gridy = 1;
522
                        gridBagConstraints1.weightx = 1.0;
523
                        gridBagConstraints1.insets = new java.awt.Insets(5, 10, 5, 10);
524
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
525
                        gridBagConstraints.insets = new java.awt.Insets(5, 10, 2, 10);
526
                        gridBagConstraints.gridy = 0;
527
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
528
                        gridBagConstraints.ipadx = 14;
529
                        gridBagConstraints.gridwidth = 3;
530
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
531
                        gridBagConstraints.gridx = 0;
532
                        jPanelCache = new JPanel();
533
                        jPanelCache.setLayout(new GridBagLayout());
534
                        jPanelCache.add(jLabelCache, gridBagConstraints);
535
                        jPanelCache.add(getJScrollPane(), gridBagConstraints1);
536
                }
537
                return jPanelCache;
538
        }
539

    
540
        public boolean isValueChanged() {
541
                return changed;
542
        }
543

    
544
        public void setChangesApplied() {
545
                changed = false;
546
        }
547

    
548
} // @jve:decl-index=0:visual-constraint="10,10"
549