Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / preferences / EditionPreferencePage.java @ 30149

History | View | Annotate | Download (14.8 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 org.gvsig.editing.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.andami.PluginServices;
59
import org.gvsig.andami.preferences.AbstractPreferencePage;
60
import org.gvsig.andami.preferences.StoreException;
61
import org.gvsig.editing.CADExtension;
62
import org.gvsig.editing.EditionManager;
63
import org.gvsig.editing.layers.VectorialLayerEdited;
64
import org.gvsig.fmap.mapcontext.MapContext;
65
import org.gvsig.fmap.mapcontext.layers.FLayer;
66
import org.gvsig.fmap.mapcontext.layers.FLayers;
67
import org.gvsig.fmap.mapcontext.layers.SingleLayerIterator;
68
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
69
import org.gvsig.fmap.mapcontrol.MapControl;
70

    
71

    
72

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

    
76
        private JTextField jTxtTolerance = null;
77

    
78
        private JLabel jLabel1 = null;
79

    
80
        private JSeparator jSeparator = null;
81

    
82
        private JScrollPane jScrollPane = null;
83

    
84
        private JTable jTableSnapping = null;
85

    
86
        private JLabel jLabelCache = null;
87

    
88
        private JPanel jPanelNord = null;
89

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

    
93
        private FLayers layers;
94

    
95
        private MapContext mapContext;
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
                        changed  =true;
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(500,20));
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(502,288));
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
                TableModel tm = getJTableSnapping().getModel();
270
                EditionManager edManager = CADExtension.getEditionManager();
271
                FLayer layerActive=layers.getActives()[0];
272
                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
273
                        .getLayerEdited(layerActive);
274
                ArrayList layersToSnap = layerActive.getMapContext().getLayersToSnap();
275
                ArrayList layersVect=new ArrayList();
276
                for (int i = 0; i < layers.getLayersCount(); i++) {
277
                        FLayer layer=layers.getLayer(i);
278
                        if (layer instanceof FLyrVect) {
279
                                layersVect.add(layer);
280
                        }
281
                }
282
                for (int i = 0; i < layersVect.size(); i++) {
283
                        FLyrVect lv=(FLyrVect)layersVect.get(i);
284
                        tm.setValueAt(lv.getName(), i, 1);
285
                        tm.setValueAt(layersToSnap.contains(lv), i, 0);
286
                        tm.setValueAt(lv.getSpatialCache().getMaxFeatures(), i, 2);
287
                }
288
        }
289

    
290
        public void storeValues() throws StoreException {
291
                TableModel tm = getJTableSnapping().getModel();
292
                ArrayList layersToSnap = new ArrayList();
293
                for (int i = 0; i < tm.getRowCount(); i++) {
294
                        String layerName = (String) tm.getValueAt(i, 1);
295
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
296
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
297
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
298

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

    
324
                while (it.hasNext()) {
325
                        FLayer aux = it.next();
326
                        if (aux instanceof FLyrVect)
327
                        {
328
                                FLyrVect lyrVect = (FLyrVect) aux;
329
                                // Inicializamos todas
330
                                lyrVect.setSpatialCacheEnabled(false);
331
                                if (aux.isActive())
332
                                        if (aux.isEditing()) {
333
                                                // Sobre la capa en edici?n siempre se puede hacer snapping
334
                                                lyrVect.setSpatialCacheEnabled(true);
335
                                                lyrVect.getMapContext().setLayersToSnap(layersToSnap);
336
//                                                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
337
//                                                                .getLayerEdited(aux);
338
//                                                lyrEd.setLayersToSnap(layersToSnap);
339

    
340
                                        }
341
                        }
342
                } // while
343
                it.rewind();
344
                /*
345
                 * Iteramos por las capas en edici?n y marcamos aquellas capas que
346
                 * necesitan trabajar con el cache habilitado
347
                 */
348
                while (it.hasNext()) {
349
                        FLayer aux = it.next();
350
                        if (aux.isEditing())
351
                                if (aux instanceof FLyrVect) {
352
                                        MapContext mx=aux.getMapContext();
353
//                                        VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
354
//                                                                .getLayerEdited(aux);
355
                                                for (int i=0; i<mx.getLayersToSnap().size(); i++)
356
                                                {
357
                                                        FLyrVect lyrVect = (FLyrVect) mx.getLayersToSnap().get(i);
358
                                                        lyrVect.setSpatialCacheEnabled(true);
359
                                                }
360

    
361
                                }
362

    
363
                } // while
364
                mapContext.invalidate();
365
                try{
366
                        MapControl.tolerance = Integer.parseInt(getJTxtTolerance().getText());
367

    
368
                }catch (Exception e) {
369
                        throw new StoreException(PluginServices.getText(this, "tolerancia_incorrecta"),e);
370
                }
371
        }
372

    
373
        public void initializeDefaults() {
374
                getJTxtTolerance().setText("4");
375
                TableModel tm = getJTableSnapping().getModel();
376
                for (int i = 0; i < tm.getRowCount(); i++) {
377
                        String layerName = (String) tm.getValueAt(i, 1);
378
                        FLyrVect lyr = (FLyrVect) layers.getLayer(layerName);
379
                        Boolean bUseCache = (Boolean) tm.getValueAt(i, 0);
380
                        Integer maxFeat = (Integer) tm.getValueAt(i, 2);
381
                        lyr.setSpatialCacheEnabled(bUseCache.booleanValue());
382
                        lyr.setMaxFeaturesInEditionCache(maxFeat.intValue());
383
                }
384

    
385
        }
386

    
387
        public ImageIcon getIcon() {
388
                return null;
389
        }
390

    
391
        public void setMapContext(MapContext mc) {
392
                // addLayer(layers);
393
                this.mapContext = mc;
394
                this.layers = mc.getLayers();
395
                MyTableModel tm = new MyTableModel(layers);
396
                getJTableSnapping().setModel(tm);
397
                getJTxtTolerance().setText(String.valueOf(MapControl.tolerance));
398
        }
399

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

    
422
        /**
423
         * This method initializes jSeparator
424
         *
425
         * @return javax.swing.JSeparator
426
         */
427
        private JSeparator getJSeparator() {
428
                if (jSeparator == null) {
429
                        jSeparator = new JSeparator();
430
                        jSeparator.setPreferredSize(new java.awt.Dimension(200,2));
431
                }
432
                return jSeparator;
433
        }
434

    
435
        /**
436
         * This method initializes jScrollPane
437
         *
438
         * @return javax.swing.JScrollPane
439
         */
440
        private JScrollPane getJScrollPane() {
441
                if (jScrollPane == null) {
442
                        jScrollPane = new JScrollPane();
443
                        jScrollPane.setPreferredSize(new java.awt.Dimension(500,419));
444
                        jScrollPane.setViewportView(getJTableSnapping());
445
                }
446
                return jScrollPane;
447
        }
448

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

    
483
        /**
484
         * This method initializes jPanelNord
485
         *
486
         * @return javax.swing.JPanel
487
         */
488
        private JPanel getJPanelNord() {
489
                if (jPanelNord == null) {
490
                        jPanelNord = new JPanel();
491
                        jPanelNord.setLayout(null);
492
                        jPanelNord
493
                                        .setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
494
                        jPanelNord.setPreferredSize(new java.awt.Dimension(30, 30));
495
                        jPanelNord.add(jLabel, null);
496
                        jPanelNord.add(getJTxtTolerance(), null);
497
                        jPanelNord.add(jLabel1, null);
498

    
499
                }
500
                return jPanelNord;
501
        }
502

    
503
        /**
504
         * This method initializes jPanelCache
505
         *
506
         * @return javax.swing.JPanel
507
         */
508
        private JPanel getJPanelCache() {
509
                if (jPanelCache == null) {
510
                        jPanelCache = new JPanel();
511
                        jPanelCache.setLayout(new BorderLayout());
512
                        jPanelCache.add(jLabelCache, java.awt.BorderLayout.NORTH);
513
                        jPanelCache.add(getJScrollPane(), java.awt.BorderLayout.EAST);
514
                }
515
                return jPanelCache;
516
        }
517

    
518
        public boolean isValueChanged() {
519
                return changed;
520
        }
521

    
522
        public void setChangesApplied() {
523
                changed = false;
524
        }
525

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