Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / preferences / EditionPreferencePage.java @ 30335

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 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
import org.gvsig.fmap.mapcontrol.MapControlLocator;
71
import org.gvsig.fmap.mapcontrol.MapControlManager;
72

    
73

    
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 MapContext mapContext;
98
        
99
        private static MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
100

    
101
        private class MyRecord {
102
                public Boolean bSelec = new Boolean(false);
103

    
104
                public String layerName;
105

    
106
                public Integer maxFeat = new Integer(1000);
107
        }
108

    
109
        private class MyTableModel extends AbstractTableModel {
110
                private ArrayList records = new ArrayList();
111

    
112
                public MyTableModel(FLayers layers) {
113
                        addLayer(layers);
114
                }
115

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

    
136
                public int getColumnCount() {
137
                        return 3;
138
                }
139

    
140
                public int getRowCount() {
141
                        return records.size();
142
                }
143

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

    
154
                }
155

    
156
                public Class getColumnClass(int c) {
157
                        if (c == 0)
158
                                return Boolean.class;
159
                        if (c == 2)
160
                                return Integer.class;
161
                        return String.class;
162
                }
163

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

    
178
                public boolean isCellEditable(int rowIndex, int columnIndex) {
179
                        if (columnIndex == 0)
180
                                return true;
181
                        if (columnIndex == 2)
182
                                return true;
183

    
184
                        return false;
185
                }
186

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

    
196
                }
197

    
198
        }
199

    
200
        /**
201
         * This method initializes
202
         *
203
         */
204
        public EditionPreferencePage() {
205
                super();
206
                initialize();
207
        }
208

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

    
216
        /**
217
         * This method initializes this
218
         *
219
         */
220
        private void initialize() {
221
                BorderLayout layout = new BorderLayout();
222
                layout.setHgap(20);
223

    
224
                this.setLayout(layout);
225

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

    
245
                this.setSize(new java.awt.Dimension(502,288));
246
                this.setPreferredSize(this.getSize());
247
                this.add(getJPanelNord(), BorderLayout.NORTH);
248

    
249
                this.add(getJSeparator(), BorderLayout.CENTER);
250

    
251
                this.add(getJPanelCache(), BorderLayout.CENTER);
252

    
253
        }
254

    
255
        public String getID() {
256
                return this.getClass().getName();
257
        }
258

    
259
        public String getTitle() {
260
                return PluginServices.getText(this, "Edition");
261
        }
262

    
263
        public JPanel getPanel() {
264
                return this;
265
        }
266

    
267
        /*
268
         * (non-Javadoc)
269
         *
270
         * @see com.iver.cit.gvsig.gui.preferences.IPreference#initializeValues()
271
         */
272
        public void initializeValues() {
273
                TableModel tm = getJTableSnapping().getModel();
274
                EditionManager edManager = CADExtension.getEditionManager();
275
                FLayer layerActive=layers.getActives()[0];
276
                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edManager
277
                        .getLayerEdited(layerActive);
278
                ArrayList layersToSnap = layerActive.getMapContext().getLayersToSnap();
279
                ArrayList layersVect=new ArrayList();
280
                for (int i = 0; i < layers.getLayersCount(); i++) {
281
                        FLayer layer=layers.getLayer(i);
282
                        if (layer instanceof FLyrVect) {
283
                                layersVect.add(layer);
284
                        }
285
                }
286
                for (int i = 0; i < layersVect.size(); i++) {
287
                        FLyrVect lv=(FLyrVect)layersVect.get(i);
288
                        tm.setValueAt(lv.getName(), i, 1);
289
                        tm.setValueAt(layersToSnap.contains(lv), i, 0);
290
                        tm.setValueAt(lv.getSpatialCache().getMaxFeatures(), i, 2);
291
                }
292
        }
293

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

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

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

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

    
365
                                }
366

    
367
                } // while
368
                mapContext.invalidate();
369
                try{
370
                        mapControlManager.setTolerance(Integer.parseInt(getJTxtTolerance().getText()));
371

    
372
                }catch (Exception e) {
373
                        throw new StoreException(PluginServices.getText(this, "tolerancia_incorrecta"),e);
374
                }
375
        }
376

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

    
389
        }
390

    
391
        public ImageIcon getIcon() {
392
                return null;
393
        }
394

    
395
        public void setMapContext(MapContext mc) {
396
                // addLayer(layers);
397
                this.mapContext = mc;
398
                this.layers = mc.getLayers();
399
                MyTableModel tm = new MyTableModel(layers);
400
                getJTableSnapping().setModel(tm);
401
                getJTxtTolerance().setText(String.valueOf(mapControlManager.getTolerance()));
402
        }
403

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

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

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

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

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

    
503
                }
504
                return jPanelNord;
505
        }
506

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

    
522
        public boolean isValueChanged() {
523
                return changed;
524
        }
525

    
526
        public void setChangesApplied() {
527
                changed = false;
528
        }
529

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