Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.daltransform.app.mainplugin / src / main / java / org / gvsig / daltransform / swing / impl / DefaultJDialogDataTransformList.java @ 37817

History | View | Annotate | Download (13 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.daltransform.swing.impl;
23

    
24
import java.awt.Dimension;
25
import java.awt.FlowLayout;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.beans.PropertyChangeListener;
29
import java.util.ArrayList;
30
import java.util.List;
31

    
32
import javax.swing.Action;
33
import javax.swing.ComboBoxModel;
34
import javax.swing.JButton;
35
import javax.swing.JComboBox;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.JScrollPane;
39
import javax.swing.border.EmptyBorder;
40
import javax.swing.event.ListDataListener;
41
import javax.swing.event.ListSelectionEvent;
42
import javax.swing.event.ListSelectionListener;
43

    
44
import org.gvsig.app.ApplicationLocator;
45
import org.gvsig.app.ApplicationManager;
46
import org.gvsig.app.project.Project;
47
import org.gvsig.app.project.documents.Document;
48
import org.gvsig.app.project.documents.table.TableDocument;
49
import org.gvsig.app.project.documents.table.TableManager;
50
import org.gvsig.app.project.documents.view.ViewDocument;
51
import org.gvsig.app.project.documents.view.ViewManager;
52
import org.gvsig.daltransform.DataTransformLocator;
53
import org.gvsig.daltransform.swing.JDataTransformList;
54
import org.gvsig.daltransform.swing.JDialogDataTransformList;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.feature.FeatureStoreTransforms;
57
import org.gvsig.fmap.mapcontext.layers.FLayer;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
59
import org.gvsig.i18n.Messages;
60
import org.gvsig.tools.swing.api.ToolsSwingLocator;
61
import org.gvsig.tools.swing.api.usability.UsabilitySwingManager;
62

    
63
/**
64
 * @author gvSIG Team
65
 * @version $Id$
66
 * 
67
 */
68
public class DefaultJDialogDataTransformList extends JDialogDataTransformList {
69

    
70
    private static final long serialVersionUID = 8666781163497235054L;
71
    private JDataTransformList transforms = null;
72

    
73
    private JButton botonEliminar;
74
    private JButton botonCerrar;
75
    private JComboBox comboBox;
76

    
77
    public DefaultJDialogDataTransformList() {
78
        super();
79
        initComponents();
80
        createGUI();
81
    }
82

    
83
    private class MyComboBoxItem {
84

    
85
        private String label;
86
        private FeatureStore store;
87

    
88
        MyComboBoxItem(String label, FeatureStore store) {
89
            this.label = label;
90
            this.store = store;
91
        }
92

    
93
        @Override
94
        public String toString() {
95
            return label;
96
        }
97

    
98
        public FeatureStore getStore() {
99
            return this.store;
100
        }
101
    }
102

    
103
    private class MyComboBoxModel implements ComboBoxModel {
104

    
105
        private List<MyComboBoxItem> items = new ArrayList<MyComboBoxItem>();
106
        private MyComboBoxItem selected = null;
107

    
108
        public MyComboBoxModel(List<MyComboBoxItem> items) {
109
            this.items.addAll(items);
110
        }
111

    
112
        public Object getSelectedItem() {
113
            return this.selected;
114
        }
115

    
116
        public MyComboBoxItem getSelected() {
117
            return this.selected;
118
        }
119

    
120
        public void setSelectedItem(Object arg0) {
121
            this.selected = (MyComboBoxItem) arg0;
122
        }
123

    
124
        public Object getElementAt(int arg0) {
125
            return this.items.get(arg0);
126
        }
127

    
128
        public MyComboBoxItem get(int arg0) {
129
            return this.items.get(arg0);
130
        }
131

    
132
        public int getSize() {
133
            return this.items.size();
134
        }
135

    
136
        public void removeListDataListener(ListDataListener arg0) {
137
            // TODO Auto-generated method stub
138

    
139
        }
140

    
141
        public void addListDataListener(ListDataListener arg0) {
142
            // TODO Auto-generated method stub
143

    
144
        }
145

    
146
    }
147

    
148
    private void initComponents() {
149
        MyComboBoxModel comboModel = new MyComboBoxModel(getAllStores());
150

    
151
        comboBox = new JComboBox(comboModel);
152

    
153
        comboBox.addActionListener(new ActionListener() {
154

    
155
            public void actionPerformed(ActionEvent e) {
156
                JComboBox jcmbType = (JComboBox) e.getSource();
157
                MyComboBoxModel model = (MyComboBoxModel) jcmbType.getModel();
158
                updatePanel(model.getSelected().getStore());
159
            }
160
        });
161

    
162
        if (comboModel.getSize() > 0) {
163
            transforms =
164
                DataTransformLocator.getDataTransformManager()
165
                    .createJDataTransformList(comboModel.get(0).getStore());
166
            comboModel.setSelectedItem(comboModel.get(0));
167
        }
168
        if (transforms != null) {
169
            transforms.addListSelectionListener(new ListSelectionHandler());
170
        }
171
    }
172

    
173
    private List<MyComboBoxItem> getAllStores() {
174
        List<MyComboBoxItem> stores = new ArrayList<MyComboBoxItem>();
175
        ApplicationManager application = ApplicationLocator.getManager();
176
        Project project = application.getCurrentProject();
177

    
178
        List<Document> views = project.getDocuments(ViewManager.TYPENAME);
179
        for (Document doc : views) {
180
            ViewDocument viewDocument = (ViewDocument) doc;
181

    
182
            FLayer[] layers =
183
                viewDocument.getMapContext().getLayers().getVisibles();
184
            if (layers.length > 1) {
185
                for (int i = 1; i < layers.length; i++) {
186
                    if (layers[i] instanceof FLyrVect) {
187
                        FLyrVect layer = (FLyrVect) layers[i];
188
                        stores.add(new MyComboBoxItem(layer.getName(), layer
189
                            .getFeatureStore()));
190
                    }
191
                }
192
            }
193
        }
194
        List<Document> tables = project.getDocuments(TableManager.TYPENAME);
195
        for (Document doc : tables) {
196
            TableDocument tableDocument = (TableDocument) doc;
197
            stores.add(new MyComboBoxItem(doc.getName(), tableDocument
198
                .getStore()));
199
        }
200

    
201
        return stores;
202
    }
203

    
204
    private void createGUI() {
205

    
206
        java.awt.GridBagConstraints gridBagConstraints;
207
        this.setLayout(new java.awt.GridBagLayout());
208

    
209
        // Add an empty margin
210
        this.setBorder(new EmptyBorder(15, 15, 15, 15));
211

    
212
        UsabilitySwingManager usabilitymanager =
213
            ToolsSwingLocator.getUsabilitySwingManager();
214

    
215
        JLabel layerText = new JLabel(Messages.getText("select_layer"));
216
        JLabel transformText = new JLabel(Messages.getText("select_transform"));
217
        JPanel botones = new JPanel();
218

    
219
        gridBagConstraints = new java.awt.GridBagConstraints();
220
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
221
        gridBagConstraints.gridx = 0;
222
        gridBagConstraints.gridy = 0;
223
        gridBagConstraints.weightx = 0.0;
224
        gridBagConstraints.weighty = 0.0;
225
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
226
        this.add(layerText, gridBagConstraints);
227

    
228
        gridBagConstraints = new java.awt.GridBagConstraints();
229
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
230
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
231
        gridBagConstraints.gridx = 0;
232
        gridBagConstraints.gridy = 1;
233
        gridBagConstraints.weightx = 1.0;
234
        gridBagConstraints.weighty = 0.0;
235
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
236
        this.add(comboBox, gridBagConstraints);
237

    
238
        gridBagConstraints = new java.awt.GridBagConstraints();
239
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
240
        gridBagConstraints.gridx = 0;
241
        gridBagConstraints.gridy = 2;
242
        gridBagConstraints.weightx = 0.0;
243
        gridBagConstraints.weighty = 0.0;
244
        gridBagConstraints.insets = new java.awt.Insets(10, 2, 5, 2);
245
        this.add(transformText, gridBagConstraints);
246

    
247
        gridBagConstraints = new java.awt.GridBagConstraints();
248
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
249
        gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
250
        gridBagConstraints.gridx = 0;
251
        gridBagConstraints.gridy = 3;
252
        gridBagConstraints.weightx = 1.0;
253
        gridBagConstraints.weighty = 1.0;
254
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
255
        if (transforms != null) {
256
            JScrollPane scroller = new JScrollPane(transforms);
257
            this.add(scroller, gridBagConstraints);
258
        } else {
259
            JPanel emptyPanel = new JPanel();
260
            this.add(emptyPanel, gridBagConstraints);
261
        }
262

    
263
        botones.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 0));
264
        botonCerrar = usabilitymanager.createJButton(new ActionClose(this));
265
        botonCerrar.setText(Messages.getText("close"));
266
        botonEliminar = usabilitymanager.createJButton(new ActionRemove(this));
267
        botonEliminar.setText(Messages.getText("remove"));
268
        botones.add(botonEliminar);
269
        botones.add(botonCerrar);
270
        botonEliminar.setEnabled(false);
271

    
272
        gridBagConstraints = new java.awt.GridBagConstraints();
273
        gridBagConstraints.gridx = 0;
274
        gridBagConstraints.gridy = 4;
275
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
276
        gridBagConstraints.weightx = 0.0;
277
        gridBagConstraints.weighty = 0.0;
278
        gridBagConstraints.insets = new java.awt.Insets(10, 2, 2, 2);
279
        this.add(botones, gridBagConstraints);
280

    
281
        botonEliminar.setEnabled(false);
282

    
283
        updatePanel(null);
284
    }
285

    
286
    class ListSelectionHandler implements ListSelectionListener {
287

    
288
        public void valueChanged(ListSelectionEvent e) {
289

    
290
            if ((transforms != null) && (transforms.getSelected() != null)) {
291
                if (transforms.isLastTransform(transforms.getSelected())) {
292
                    botonEliminar.setEnabled(true);
293
                    return;
294
                }
295
            }
296
            botonEliminar.setEnabled(false);
297
        }
298

    
299
    }
300

    
301
    @Override
302
    public Dimension getPreferredSize() {
303
        return new Dimension(250, 300);
304
    }
305

    
306
    private class ActionRemove implements Action {
307

    
308
        private DefaultJDialogDataTransformList parent;
309

    
310
        public ActionRemove(DefaultJDialogDataTransformList parent) {
311
            this.parent = parent;
312
        }
313

    
314
        public void addPropertyChangeListener(PropertyChangeListener listener) {
315
        }
316

    
317
        public Object getValue(String key) {
318
            return null;
319
        }
320

    
321
        public boolean isEnabled() {
322
            return true;
323
        }
324

    
325
        public void putValue(String key, Object value) {
326
        }
327

    
328
        public void removePropertyChangeListener(PropertyChangeListener listener) {
329
        }
330

    
331
        public void setEnabled(boolean b) {
332
        }
333

    
334
        public void actionPerformed(ActionEvent e) {
335
            if (this.parent.transforms.getSelected() != null) {
336
                this.parent.transforms.remove(this.parent.transforms
337
                    .getSelected());
338
            }
339
            updatePanel(null);
340
        }
341

    
342
    }
343

    
344
    private class ActionClose implements Action {
345

    
346
        private DefaultJDialogDataTransformList parent;
347

    
348
        public ActionClose(DefaultJDialogDataTransformList parent) {
349
            this.parent = parent;
350
        }
351

    
352
        public void addPropertyChangeListener(PropertyChangeListener listener) {
353
        }
354

    
355
        public Object getValue(String key) {
356
            return null;
357
        }
358

    
359
        public boolean isEnabled() {
360
            return true;
361
        }
362

    
363
        public void putValue(String key, Object value) {
364
        }
365

    
366
        public void removePropertyChangeListener(PropertyChangeListener listener) {
367
        }
368

    
369
        public void setEnabled(boolean b) {
370
        }
371

    
372
        public void actionPerformed(ActionEvent e) {
373
            this.parent.setVisible(false);
374
        }
375

    
376
    }
377

    
378
    private void updatePanel(FeatureStore store) {
379

    
380
        if (transforms != null) {
381

    
382
            if (store != null) {
383
                FeatureStoreTransforms layerTransforms = store.getTransforms();
384

    
385
                transforms.changeStore(store);
386
                transforms.removeAllTransforms();
387
                transforms.addTransforms(layerTransforms);
388
            }
389

    
390
            // disable the remove button if the transforms list is empty
391
            if (transforms.getSelected() != null) {
392
                botonEliminar.setEnabled(transforms.getTransformsCount() > 0);
393
            }
394

    
395
        } else {
396
            botonEliminar.setEnabled(false);
397
        }
398

    
399
    }
400

    
401
    @Override
402
    public void setDeleteButton(boolean active) {
403
        botonEliminar.setEnabled(active);
404
    }
405

    
406
}