Statistics
| Revision:

root / trunk / extensions / extAnnotations / src / org / gvsig / tools / annotations / labeling / gui / ConfigTargetLayer.java @ 25449

History | View | Annotate | Download (11.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2008 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
 *   Lerida 20
35
 *   46009 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.tools.annotations.labeling.gui;
42

    
43
import java.awt.Font;
44
import java.awt.GridBagConstraints;
45
import java.awt.GridBagLayout;
46
import java.awt.Insets;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.io.File;
50

    
51
import javax.swing.ButtonGroup;
52
import javax.swing.JComboBox;
53
import javax.swing.JLabel;
54
import javax.swing.JPanel;
55
import javax.swing.JRadioButton;
56

    
57
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
58
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
59
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
60
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
61
import org.gvsig.gui.beans.openfile.FileFilter;
62
import org.gvsig.gui.beans.openfile.FileTextField;
63

    
64
import com.hardcode.driverManager.DriverLoadException;
65
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.messages.NotificationManager;
68
import com.iver.andami.ui.mdiManager.IWindow;
69
import com.iver.andami.ui.mdiManager.WindowInfo;
70
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
71
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
72
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException;
73
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
74
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
75
import com.iver.cit.gvsig.fmap.layers.Annotation_LayerFactory;
76
import com.iver.cit.gvsig.fmap.layers.FLayers;
77
import com.iver.cit.gvsig.project.documents.view.gui.BaseView;
78

    
79
/**
80
 *
81
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> 12:00:04
82
 *
83
 */
84
public class ConfigTargetLayer extends DefaultButtonsPanel
85
        implements IWindow, ActionListener, ButtonsPanelListener {
86
        private WindowInfo wInfo = null;
87
        private static final long serialVersionUID = 1L;
88
        private BaseView sourceView;
89
        private JComboBox jcb_layers = null;
90
        private JRadioButton jrb_file = null;
91
        private JRadioButton jrb_layer = null;
92
        private FileTextField ftf_fileName = null;
93
        private SingleLabelingToolUI toolUI;
94

    
95
        public ConfigTargetLayer(BaseView sourceView, SingleLabelingToolUI toolUI) {
96
                super(ButtonsPanel.BUTTONS_ACCEPTCANCEL);
97
                this.sourceView = sourceView;
98
                this.toolUI = toolUI;
99
                initialize();
100
        }
101

    
102
        private void initialize() {
103
                this.addButtonPressedListener(this);
104
                getContent().setLayout(new GridBagLayout());
105
                JLabel lbl_header = new JLabel(PluginServices.getText(this, "Select_the_annotation_layer_to_store_the_labels_"));
106
                Font font = lbl_header.getFont();
107
                lbl_header.setFont(font.deriveFont(Font.BOLD));
108
                GridBagConstraints constraints = new GridBagConstraints();
109
                constraints.gridx = 0;
110
                constraints.gridy = 0;
111
                constraints.gridwidth = 2;
112
                constraints.anchor = GridBagConstraints.NORTH;
113
                constraints.fill = GridBagConstraints.HORIZONTAL;
114
                constraints.weightx = 1.0;
115
                constraints.weighty = 0.0;
116
                constraints.insets = new Insets(4,10,8,4);
117
                getContent().add(lbl_header, constraints);
118

    
119
                ButtonGroup group = new ButtonGroup();
120
                group.add(getFileButton());
121
                group.add(getLayerButton());
122
                getFileButton().addActionListener(this);
123
                getLayerButton().addActionListener(this);
124
                constraints.gridx = 0;
125
                constraints.gridy = 1;
126
                constraints.gridwidth = 1;
127
                constraints.anchor = GridBagConstraints.NORTHWEST;
128
                constraints.fill = GridBagConstraints.NONE;
129
                constraints.weightx = 0.0;
130
                constraints.weighty = 0.0;
131
                constraints.insets = new Insets(4,10,4,6);
132
                getContent().add(getFileButton(), constraints);
133

    
134
                constraints.gridx = 1;
135
                constraints.gridy = 1;
136
                constraints.gridwidth = 1;
137
                constraints.anchor = GridBagConstraints.NORTHWEST;
138
                constraints.fill = GridBagConstraints.HORIZONTAL;
139
                constraints.weightx = 0.5;
140
                constraints.weighty = 0.0;
141
                getContent().add(getFileNameField(), constraints);
142

    
143
                JPanel detailLine = new JPanel(new GridBagLayout());
144

    
145
                constraints.gridx = 0;
146
                constraints.gridy = 2;
147
                constraints.gridwidth = 2;
148
                constraints.anchor = GridBagConstraints.NORTHWEST;
149
                constraints.fill = GridBagConstraints.HORIZONTAL;
150
                constraints.weightx = 0.5;
151
                constraints.weighty = 0.0;
152
                constraints.insets = new Insets(3,20,10,6);
153
                getContent().add(detailLine, constraints);
154

    
155
                constraints.gridx = 0;
156
                constraints.gridy = 3;
157
                constraints.gridwidth = 1;
158
                constraints.anchor = GridBagConstraints.NORTHWEST;
159
                constraints.fill = GridBagConstraints.NONE;
160
                constraints.weightx = 0.0;
161
                constraints.weighty = 0.0;
162
                constraints.insets = new Insets(4,10,4,6);
163
                getContent().add(getLayerButton(), constraints);
164

    
165
                constraints.gridx = 1;
166
                constraints.gridy = 3;
167
                constraints.fill = GridBagConstraints.HORIZONTAL;
168
                constraints.anchor = GridBagConstraints.NORTHWEST;
169
                constraints.weightx = 0.1;
170
                constraints.weighty = 0.0;
171
                getContent().add(getLayersCombo(), constraints);
172

    
173

    
174
                constraints.gridx = 0;
175
                constraints.gridy = 4;
176
                constraints.gridwidth = 3;
177
                constraints.fill = GridBagConstraints.BOTH;
178
                constraints.anchor = GridBagConstraints.SOUTH;
179
                constraints.weightx = 1.0;
180
                constraints.weighty = 0.2;
181
                getContent().add(new JPanel(), constraints); // empty panel
182

    
183
                Annotation_Layer lyr = toolUI.getTargetLayer();
184
                if (lyr!=null) {
185
                        getLayersCombo().setSelectedItem(
186
                                        new ComboItem(lyr));
187
                        selectExistingLayerMode();
188
                }
189
                else {
190
                        selectFileMode();
191
                }
192
        }
193

    
194
        private void selectFileMode() {
195
                getFileButton().setSelected(true);
196
                getFileNameField().setEnabled(true);
197
                getLayersCombo().setEnabled(false);
198

    
199
        }
200

    
201
        private void selectExistingLayerMode() {
202
                getLayerButton().setSelected(true);
203
                getFileNameField().setEnabled(false);
204
                getLayersCombo().setEnabled(true);
205
        }
206

    
207
        private JRadioButton getFileButton() {
208
                if (jrb_file==null) {
209
                        jrb_file = new JRadioButton(
210
                                        PluginServices.getText(this, "Create_Open_layer"));
211
                }
212
                return jrb_file;
213
        }
214

    
215
        private JRadioButton getLayerButton(){
216
                if (jrb_layer == null) {
217
                        jrb_layer = new JRadioButton(
218
                                        PluginServices.getText(this, "Layer_from_active_view"));
219
                }
220
                return jrb_layer;
221
        }
222

    
223
        private FileTextField getFileNameField() {
224
                if (ftf_fileName==null) {
225
                        ftf_fileName = new FileTextField(this.getClass().getName());
226
                        ftf_fileName.addChoosableFileFilter(new FileFilter() {
227

    
228
                                public boolean accept(File f) {
229
                                        if (f.isDirectory()
230
                                                        || f.getName().toLowerCase().endsWith("gva")) {
231
                                                return true;
232
                                        }
233
                                        else {
234
                                                return false;
235
                                        }
236
                                }
237

    
238
                                public String getDescription() {
239
                                        return PluginServices.getText(this, "Annotation_layers")+" (*.gva)";
240
                                }
241

    
242
                                public String getDefaultExtension() {
243
                                        return "gva";
244
                                }
245
                        });
246
                }
247
                return ftf_fileName;
248
        }
249

    
250
        public JComboBox getLayersCombo() {
251
                if (jcb_layers==null) {
252
                        jcb_layers = new JComboBox();
253
                        FLayers layers = sourceView.getMapControl().getMapContext().getLayers();
254
                        for (int i=0; i<layers.getLayersCount(); i++) {
255
                                if (layers.getLayer(i) instanceof Annotation_Layer) {
256
                                        jcb_layers.addItem(new ComboItem((Annotation_Layer)layers.getLayer(i)));
257
                                }
258
                        }
259
                }
260
                return jcb_layers;
261
        }
262

    
263
        public void actionPerformed(ActionEvent e) {
264
                if (e.getSource()==getLayerButton()) {
265
                        selectExistingLayerMode();
266
                }
267
                else if (e.getSource()==getFileButton()) {
268
                        selectFileMode();
269
                }
270
        }
271

    
272
        private class ComboItem {
273
                private Annotation_Layer layer;
274
                public ComboItem(Annotation_Layer layer) {
275
                        this.layer = layer;
276
                }
277

    
278
                public String toString() {
279
                        return layer.getName();
280
                }
281

    
282
                public Annotation_Layer getLayer() {
283
                        return layer;
284
                }
285

    
286
                @Override
287
                public boolean equals(Object obj) {
288
                        if (obj instanceof ComboItem) {
289
                                return this.layer==((ComboItem)obj).getLayer();
290
                        }
291
                        return super.equals(obj);
292
                }
293
        }
294

    
295
        public WindowInfo getWindowInfo() {
296
                if (wInfo==null) {
297
                        wInfo = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
298
                        wInfo.setWidth(500);
299
                        wInfo.setHeight(155);
300
                        wInfo.setTitle(PluginServices.getText(this, "Set_target_layer"));
301
                }
302
                return wInfo;
303
        }
304

    
305
        private boolean apply() {
306
                if (getLayerButton().isSelected()) {
307
                        ComboItem item = (ComboItem) getLayersCombo().getSelectedItem();
308
                        if (item!=null) {
309
                                toolUI.setTargetLayer(item.getLayer());
310
                                return true;
311
                        }
312
                        else {
313
                                NotificationManager.showMessageError(
314
                                                PluginServices.getText(this, "Annotation_layer_not_valid"),
315
                                                null);
316
                        }
317
                }
318
                else {
319
                        File file = getFileNameField().getSelectedFile();
320
                        if (file!=null) {
321
                                file = gvaToShpExtension(file);
322
                                if (!file.exists()) {
323
                                        try {
324
                                                // create new layer
325
                                                Annotation_LayerFactory.createEmptyLayer(file, sourceView.getProjection());
326
                                        } catch (StartWriterVisitorException e) {
327
                                                NotificationManager.showMessageError(
328
                                                                PluginServices.getText(this, "Error_creating_annotation_layer"),
329
                                                                e);
330
                                                return false;
331
                                        } catch (StopWriterVisitorException e) {
332
                                                NotificationManager.showMessageError(
333
                                                                PluginServices.getText(this, "Error_creating_annotation_layer"),
334
                                                                e);
335
                                                return false;
336
                                        } catch (DriverLoadException e) {
337
                                                NotificationManager.showMessageError(
338
                                                                PluginServices.getText(this, "Error_opening_annotation_layer"),
339
                                                                e);
340
                                                return false;
341
                                        } catch (InitializeWriterException e) {
342
                                                NotificationManager.showMessageError(
343
                                                                PluginServices.getText(this, "Error_creating_annotation_layer"),
344
                                                                e);
345
                                                return false;
346
                                        } catch (LoadLayerException e) {
347
                                                NotificationManager.showMessageError(
348
                                                                PluginServices.getText(this, "Error_opening_annotation_layer"),
349
                                                                e);
350
                                                return false;
351
                                        }
352
                                }
353
                                // open the layer
354
                                Annotation_Layer layer = Annotation_LayerFactory.createLayer(file.getName(), file, sourceView.getProjection(), CartographicSupportToolkit.DefaultMeasureUnit);
355
                                sourceView.getMapControl().getMapContext().getLayers().addLayer(layer);
356
                                toolUI.setTargetLayer(layer);
357
                                return true;
358
                        }
359
                }
360
                NotificationManager.showMessageError(
361
                                PluginServices.getText(this, "Error_opening_annotation_layer"),
362
                                null);
363
                return false;
364
        }
365

    
366
        private File gvaToShpExtension(File file) {
367
                String path = file.getPath();
368
                int pos = path.toLowerCase().lastIndexOf(".gva");
369
                if (pos!=-1) {
370
                        file = new File(path.substring(0, pos)+".shp");
371
                }
372
                return file;
373
        }
374

    
375
        public void actionButtonPressed(ButtonsPanelEvent e) {
376
                switch (e.getButton()) {
377
                        case ButtonsPanel.BUTTON_ACCEPT:
378
                                if (apply()) {
379
                                        PluginServices.getMDIManager().closeWindow(this);
380
                                        getLayersCombo().removeAllItems(); // forget the available layers
381
                                }
382
                                break;
383
                        case ButtonsPanel.BUTTON_APPLY:
384
                                apply();
385
                                break;
386
                        case ButtonsPanel.BUTTON_CANCEL:
387
                                PluginServices.getMDIManager().closeWindow(this);
388
                                getLayersCombo().removeAllItems(); // forget the available layers
389
                                break;
390

    
391
                }
392
        }
393

    
394
        public Object getWindowProfile() {
395
                return WindowInfo.TOOL_PROFILE;
396
        }
397

    
398
}