Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / PictureMarker.java @ 20905

History | View | Annotate | Download (10.4 KB)

1 20768 jdominguez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.symbology.gui.styling;
42
43
import java.awt.FlowLayout;
44
import java.awt.Font;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.awt.geom.Point2D;
48
import java.io.File;
49
import java.io.IOException;
50
import java.net.MalformedURLException;
51
import java.net.URL;
52
import java.util.ArrayList;
53
54
import javax.swing.JButton;
55
import javax.swing.JLabel;
56
import javax.swing.JPanel;
57
import javax.swing.filechooser.FileFilter;
58
59
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
60 20905 jdominguez
import org.gvsig.gui.beans.swing.JFileChooser;
61 20768 jdominguez
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
62
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
63
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.messages.NotificationManager;
66
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
67
import com.iver.cit.gvsig.gui.styling.AbstractTypeSymbolEditor;
68
import com.iver.cit.gvsig.gui.styling.EditorTool;
69
import com.iver.cit.gvsig.gui.styling.Mask;
70
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
71
72
/**
73
 * PictureMarker allows the user to store and modify the properties that define a
74
 * <b>picture marker symbol</b>.<p>
75
 * <p>
76
 * This functionality is carried out thanks to a tab (simple marker)which is
77
 * included in the panel to edit the properities of a symbol (SymbolEditor)how is
78
 * explained in AbstractTypeSymbolEditor.
79
 * <p>
80
 * First of all, in the above mentioned tab the user will have options to change
81
 * the files from where the pictures for the symbol are taken (one for the symbol
82
 * when it is not selected in the map and the other when it is done).<p>
83
 * <p>
84
 * Secondly, the user will have options to modify the pictures which had been
85
 * selected before  (width and offset) .
86
 *
87
 *@see AbstractTypeSymbolEditor
88
 *@author jaume dominguez faus - jaume.dominguez@iver.es
89
 */
90
public class PictureMarker extends AbstractTypeSymbolEditor implements
91
ActionListener {
92
        private static File lastDir;
93
        protected ArrayList<JPanel> tabs = new ArrayList<JPanel>();
94
        protected JIncrementalNumberField txtSize;
95
        protected JIncrementalNumberField txtX;
96
        protected JIncrementalNumberField txtY;
97
        protected Mask mask;
98
        protected JLabel lblFileName;
99
        protected JLabel lblSelFileName;
100
        private File picFile;
101
        private File selPicFile;
102
        protected JLabel lblSize = new JLabel(PluginServices.getText(this, "width")+":");
103
        protected JLabel lblX = new JLabel(PluginServices.getText(this, "x_offset")+":");
104
        protected JLabel lblY = new JLabel(PluginServices.getText(this, "y_offset")+":");
105
        private JButton btn;
106
        private JButton btnSel;
107
108
        private ActionListener chooseAction = new ActionListener() {
109
110
                public void actionPerformed(ActionEvent e) {
111
112
                        JLabel targetLbl;
113
                        boolean isSelection;
114
                        if (e.getSource().equals(btn)) {
115
                                targetLbl = lblFileName;
116
                                isSelection = false;
117
                        } else {
118
                                targetLbl = lblSelFileName;
119
                                isSelection = true;
120
                        }
121
                        FileFilter ff = new FileFilter() {
122
                                public boolean accept(File f) {
123
                                        if (f.isDirectory()) return true;
124
                                        String fName = f.getAbsolutePath();
125
                                        if (fName!=null) {
126
                                                fName = fName.toLowerCase();
127
                                                return fName.endsWith(".png")
128
                                                || fName.endsWith(".gif")
129
                                                || fName.endsWith(".jpg")
130
                                                || fName.endsWith(".jpeg")
131
                                                || fName.endsWith(".bmp")
132
                                                || fName.endsWith(".svg");
133
                                        }
134
                                        return false;
135
                                }
136
137
                                public String getDescription() {
138
                                        return PluginServices.getText(this, "bitmap_and_svg_image_files");
139
                                }
140
                        };
141 20905 jdominguez
                        JUrlFileChooser jfc = new JUrlFileChooser(getName(), null);
142 20768 jdominguez
                        jfc.setFileFilter(ff);
143
                        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
144
                        jfc.setSelectedFile(picFile);
145
                        jfc.setMultiSelectionEnabled(false);
146
                        int returnVal = jfc.showOpenDialog(PictureMarker.this.owner);
147
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
148 20905 jdominguez
149
                                URL url = jfc.getSelectedURL();
150
                                if (url == null) return;
151 20768 jdominguez
                                lastDir = jfc.getCurrentDirectory();
152 20905 jdominguez
                                targetLbl.setText(url.toString());
153
                                fireSymbolChangedEvent();
154 20768 jdominguez
                        }
155 20905 jdominguez
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
156
//                                File myFile = jfc.getSelectedFile();
157
//                                lastDir = jfc.getCurrentDirectory();
158
//                                if (myFile != null && myFile.exists()) {
159
//                                        if (isSelection) {
160
//                                                selPicFile = myFile;
161
//                                        } else {
162
//                                                picFile = myFile;
163
//                                        }
164
//                                        try {
165
//                                                targetLbl.setText(myFile.toURL().toString());
166
//                                        } catch (MalformedURLException e1) {
167
//                                                NotificationManager.addError(PluginServices.getText(this, "Error en la creaci?n" +
168
//                                                "de la URL"), e1);
169
//                                        }
170
//                                        fireSymbolChangedEvent();
171
//                                }
172
//                        }
173 20768 jdominguez
174
175
                }
176
177
        };
178
179
180
        public PictureMarker(SymbolEditor owner) {
181
                super(owner);
182
                initialize();
183
        }
184
185
        /**
186
         * Initializes the parameters that define a picturmarker.To do it,
187
         * a tab is created inside the SymbolEditor panel with default values
188
         *  for the different attributes of the picture marker.
189
         */
190
191
        private void initialize() {
192
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
193
                myTab.setName(PluginServices.getText(this, "simple_marker"));
194
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
195
196
                // picture file label
197
                lblFileName = new JLabel();
198
                lblFileName.setFont(lblFileName.getFont().deriveFont(Font.BOLD));
199
                aux.addComponent(PluginServices.getText(this, "picture_file")+":",
200
                                lblFileName);
201
202
                // button browse
203
                btn = new JButton(PluginServices.getText(this, "browse"));
204
                btn.addActionListener(chooseAction);
205
206
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
207
                aux2.add(btn);
208
                aux.addComponent("", aux2);
209
210
                // selection picture file
211
                lblSelFileName = new JLabel();
212
                lblSelFileName.setFont(lblSelFileName.getFont().deriveFont(Font.BOLD));
213
                aux.addComponent(PluginServices.getText(this, "selection_picture_file")+":",
214
                                lblSelFileName);
215
216
                // button browse
217
                btnSel = new JButton(PluginServices.getText(this, "browse"));
218
                btnSel.addActionListener(chooseAction);
219
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
220
                aux2.add(btnSel);
221
                aux.addComponent("", aux2);
222
223
                // picture width
224
                txtSize = new JIncrementalNumberField("5", 25, 0, Double.POSITIVE_INFINITY, 0.5);
225
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
226
                aux2.add(txtSize);
227
                aux.addComponent(lblSize, aux2 );
228
                txtSize.setDouble(5);
229
230
231
                // picture xOffset
232
                txtX = new JIncrementalNumberField("0", 25);
233
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
234
                aux2.add(txtX);
235
                aux.addComponent(lblX, aux2);
236
237
238
                // picture width
239
                txtY = new JIncrementalNumberField("0", 25);
240
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
241
                aux2.add(txtY);
242
                aux.addComponent(lblY,
243
                                aux2 );
244
245
246
                // initialize defaults
247
                txtSize.addActionListener(this);
248
                txtX.addActionListener(this);
249
                txtY.addActionListener(this);
250
                // buttons have their own listener!!!!
251
252
                myTab.add(aux);
253
                tabs.add(myTab);
254
255
                mask = new Mask(this);
256
                tabs.add(mask);
257
        }
258
259
        public ISymbol getLayer() {
260
                try {
261
                        PictureMarkerSymbol layer = null;
262
263
                        if( lblFileName.getText().equals(""))
264
                                layer=null;
265
266
                        else {
267
                                layer =  new PictureMarkerSymbol(new URL(lblFileName.getText()),null);
268
                                if (!lblSelFileName.getText().equals(""))
269
                                        layer = new PictureMarkerSymbol(new URL(lblFileName.getText()),new URL(lblSelFileName.getText()));
270
                                layer.setIsShapeVisible(true);
271
                                layer.setSize(txtSize.getDouble());
272
                                layer.setOffset(new Point2D.Double(
273
                                        txtX.getDouble(),
274
                                        txtY.getDouble()));
275
                                layer.setMask(mask.getMask());
276
                        }
277
278
                        return layer;
279
                } catch (IOException e) {
280
                        NotificationManager.addError(PluginServices.getText(this, "failed_accessing_file"), e);
281
                        return null;
282
                }
283
284
        }
285
286
        public String getName() {
287
                return PluginServices.getText(this, "picture_marker_symbol");
288
289
        }
290
291
        public JPanel[] getTabs() {
292
                return tabs.toArray(new JPanel[tabs.size()]);
293
        }
294
295
        public void refreshControls(ISymbol layer) {
296
                PictureMarkerSymbol sym;
297
                try {
298
                        double size, xOffset, yOffset;
299
                        String fileName = null, selectionFileName = null;
300
                        if (layer == null) {
301
                                // initialize defaults
302
                                System.err.println(getClass().getName()+":: should be unreachable code");
303
304
                                size = 1D;
305
                                xOffset = 0D;
306
                                yOffset = 0D;
307
                                fileName = "-";
308
                                selectionFileName = "-";
309
                        } else {
310
                                sym = (PictureMarkerSymbol) layer;
311
312
                                size = sym.getSize();
313
                                xOffset = sym.getOffset().getX();
314
                                yOffset = sym.getOffset().getY();
315
316
                                try {
317
                                        fileName = new URL(sym.getImagePath()).toString();
318
                                        selectionFileName = new URL(sym.getSelImagePath()).toString();
319
                                } catch (MalformedURLException e) {
320
                                        NotificationManager.addError(PluginServices.getText(this, "invalid_url"), e);
321
                                }
322
                        }
323
324
                        setValues(size, xOffset, yOffset, fileName, selectionFileName);
325
                } catch (IndexOutOfBoundsException ioEx) {
326
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
327
                } catch (ClassCastException ccEx) {
328
                        NotificationManager.addWarning("Illegal casting from " +
329
                                        layer.getClassName() + " to " + getSymbolClass().
330
                                        getName() + ".", ccEx);
331
332
                }
333
        }
334
335
        protected void setValues(double size, double xOffset, double yOffset, String fileName, String selectionFileName) {
336
                txtSize.setDouble(size);
337
                txtX.setDouble(xOffset);
338
                txtY.setDouble(yOffset);
339
                lblFileName.setText(fileName);
340
                lblSelFileName.setText(selectionFileName);
341
        }
342
343
        public Class getSymbolClass() {
344
                return PictureMarkerSymbol.class;
345
        }
346
347
        public EditorTool[] getEditorTools() {
348
                return null;
349
350
        }
351
352
        public void actionPerformed(ActionEvent e) {
353
                fireSymbolChangedEvent();
354
        }
355
356
}