Statistics
| Revision:

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

History | View | Annotate | Download (15.1 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 org.gvsig.fmap.mapcontext.MapContext;
59
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.gvsig.fmap.mapcontext.layers.FLayers;
61
import org.gvsig.fmap.mapcontext.layers.SingleLayerIterator;
62
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.preferences.AbstractPreferencePage;
66
import com.iver.andami.preferences.StoreException;
67
import com.iver.cit.gvsig.CADExtension;
68
import com.iver.cit.gvsig.EditionManager;
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
                                                lyrVect.getMapContext().setLayersToSnap(layersToSnap);
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
                                        MapContext mx=aux.getMapContext();
365
//                                        VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
366
//                                                                .getLayerEdited(aux);
367
                                                for (int i=0; i<mx.getLayersToSnap().size(); i++)
368
                                                {
369
                                                        FLyrVect lyrVect = (FLyrVect) mx.getLayersToSnap().get(i);
370
                                                        lyrVect.setSpatialCacheEnabled(true);
371
                                                }
372

    
373
                                }
374

    
375
                } // while
376
                mapContext.invalidate();
377
                try{
378
                        SelectionCADTool.tolerance = Integer.parseInt(getJTxtTolerance().getText());
379

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

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

    
397
        }
398

    
399
        public ImageIcon getIcon() {
400
                return null;
401
        }
402

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

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

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

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

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

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

    
511
                }
512
                return jPanelNord;
513
        }
514

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

    
530
        public boolean isValueChanged() {
531
                return changed;
532
        }
533

    
534
        public void setChangesApplied() {
535
                changed = false;
536
        }
537

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