Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / PictureMarker.java @ 26824

History | View | Annotate | Download (10.6 KB)

1
/* 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
import org.gvsig.gui.beans.swing.JFileChooser;
61
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.SymbologyFactory;
67
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
68
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
69
import com.iver.cit.gvsig.gui.styling.AbstractTypeSymbolEditor;
70
import com.iver.cit.gvsig.gui.styling.EditorTool;
71
import com.iver.cit.gvsig.gui.styling.Mask;
72
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
73

    
74
/**
75
 * PictureMarker allows the user to store and modify the properties that define a
76
 * <b>picture marker symbol</b>.<p>
77
 * <p>
78
 * This functionality is carried out thanks to a tab (simple marker)which is
79
 * included in the panel to edit the properities of a symbol (SymbolEditor)how is
80
 * explained in AbstractTypeSymbolEditor.
81
 * <p>
82
 * First of all, in the above mentioned tab the user will have options to change
83
 * the files from where the pictures for the symbol are taken (one for the symbol
84
 * when it is not selected in the map and the other when it is done).<p>
85
 * <p>
86
 * Secondly, the user will have options to modify the pictures which had been
87
 * selected before  (width and offset) .
88
 *
89
 *@see AbstractTypeSymbolEditor
90
 *@author jaume dominguez faus - jaume.dominguez@iver.es
91
 */
92
public class PictureMarker extends AbstractTypeSymbolEditor implements
93
ActionListener {
94
        protected ArrayList<JPanel> tabs = new ArrayList<JPanel>();
95
        protected JIncrementalNumberField txtSize;
96
        protected JIncrementalNumberField txtX;
97
        protected JIncrementalNumberField txtY;
98
        //TODO: Comentarizado hasta que mask est? acabado
99
//        protected Mask mask;
100
        protected JLabel lblFileName;
101
        protected JLabel lblSelFileName;
102
        private File picFile;
103
        protected JLabel lblSize = new JLabel(PluginServices.getText(this, "width")+":");
104
        protected JLabel lblX = new JLabel(PluginServices.getText(this, "x_offset")+":");
105
        protected JLabel lblY = new JLabel(PluginServices.getText(this, "y_offset")+":");
106
        private JButton btn;
107
        private JButton btnSel;
108

    
109
        private ActionListener chooseAction = new ActionListener() {
110

    
111
                public void actionPerformed(ActionEvent e) {
112

    
113
                        JLabel targetLbl;
114
                        if (e.getSource().equals(btn)) {
115
                                targetLbl = lblFileName;
116
                        } else {
117
                                targetLbl = lblSelFileName;
118
                        }
119
                        FileFilter ff = new FileFilter() {
120
                                public boolean accept(File f) {
121
                                        if (f.isDirectory()) return true;
122
                                        String fName = f.getAbsolutePath();
123
                                        if (fName!=null) {
124
                                                fName = fName.toLowerCase();
125
                                                return fName.endsWith(".png")
126
                                                || fName.endsWith(".gif")
127
                                                || fName.endsWith(".jpg")
128
                                                || fName.endsWith(".jpeg")
129
                                                || fName.endsWith(".bmp")
130
                                                || fName.endsWith(".svg");
131
                                        }
132
                                        return false;
133
                                }
134

    
135
                                public String getDescription() {
136
                                        return PluginServices.getText(this, "bitmap_and_svg_image_files");
137
                                }
138
                        };
139
                        JUrlFileChooser jfc = new JUrlFileChooser(getName(), null);
140
                        jfc.setFileFilter(ff);
141
                        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
142
                        jfc.setSelectedFile(picFile);
143
                        jfc.setMultiSelectionEnabled(false);
144
                        int returnVal = jfc.showOpenDialog(PictureMarker.this.owner);
145
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
146

    
147
                                URL url = jfc.getSelectedURL();
148
                                if (url == null) return;
149
                                targetLbl.setText(url.toString());
150
                                fireSymbolChangedEvent();
151
                        }
152
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
153
//                                File myFile = jfc.getSelectedFile();
154
//                                lastDir = jfc.getCurrentDirectory();
155
//                                if (myFile != null && myFile.exists()) {
156
//                                        if (isSelection) {
157
//                                                selPicFile = myFile;
158
//                                        } else {
159
//                                                picFile = myFile;
160
//                                        }
161
//                                        try {
162
//                                                targetLbl.setText(myFile.toURL().toString());
163
//                                        } catch (MalformedURLException e1) {
164
//                                                NotificationManager.addError(PluginServices.getText(this, "Error en la creaci?n" +
165
//                                                "de la URL"), e1);
166
//                                        }
167
//                                        fireSymbolChangedEvent();
168
//                                }
169
//                        }
170

    
171
                        btnSel.setEnabled(lblFileName.getText()!="");
172

    
173
                }
174

    
175
        };
176

    
177

    
178
        public PictureMarker(SymbolEditor owner) {
179
                super(owner);
180
                initialize();
181
        }
182

    
183
        /**
184
         * Initializes the parameters that define a picturmarker.To do it,
185
         * a tab is created inside the SymbolEditor panel with default values
186
         *  for the different attributes of the picture marker.
187
         */
188

    
189
        private void initialize() {
190
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
191
                myTab.setName(PluginServices.getText(this, "simple_marker"));
192
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
193

    
194
                // picture file label
195
                lblFileName = new JLabel();
196
                lblFileName.setFont(lblFileName.getFont().deriveFont(Font.BOLD));
197
                aux.addComponent(PluginServices.getText(this, "picture_file")+":",
198
                                lblFileName);
199

    
200
                // button browse
201
                btn = new JButton(PluginServices.getText(this, "browse"));
202
                btn.addActionListener(chooseAction);
203

    
204
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
205
                aux2.add(btn);
206
                aux.addComponent("", aux2);
207

    
208
                // selection picture file
209
                lblSelFileName = new JLabel();
210
                lblSelFileName.setFont(lblSelFileName.getFont().deriveFont(Font.BOLD));
211
                aux.addComponent(PluginServices.getText(this, "selection_picture_file")+":",
212
                                lblSelFileName);
213

    
214
                // button browse
215
                btnSel = new JButton(PluginServices.getText(this, "browse"));
216
                btnSel.addActionListener(chooseAction);
217
                btnSel.setEnabled(false);
218
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
219
                aux2.add(btnSel);
220
                aux.addComponent("", aux2);
221

    
222
                // picture width
223
                txtSize = new JIncrementalNumberField("5", 25, 0, Double.POSITIVE_INFINITY, 0.5);
224
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
225
                aux2.add(txtSize);
226
                aux.addComponent(lblSize, aux2 );
227
                txtSize.setDouble(5);
228

    
229

    
230
                // picture xOffset
231
                txtX = new JIncrementalNumberField("0", 25);
232
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
233
                aux2.add(txtX);
234
                aux.addComponent(lblX, aux2);
235

    
236

    
237
                // picture width
238
                txtY = new JIncrementalNumberField("0", 25);
239
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
240
                aux2.add(txtY);
241
                aux.addComponent(lblY,
242
                                aux2 );
243

    
244

    
245
                // initialize defaults
246
                txtSize.addActionListener(this);
247
                txtX.addActionListener(this);
248
                txtY.addActionListener(this);
249
                // buttons have their own listener!!!!
250

    
251
                myTab.add(aux);
252
                tabs.add(myTab);
253

    
254
//                mask = new Mask(this);
255
//                tabs.add(mask);
256
        }
257

    
258
        public ISymbol getLayer() {
259
                try {
260
                        PictureMarkerSymbol layer = null;
261

    
262
                        if( lblFileName.getText().equals(""))
263
                                layer=null;
264

    
265
                        else {
266
                                layer =  new PictureMarkerSymbol(new URL(lblFileName.getText()),null);
267
                                if (!lblSelFileName.getText().equals(""))
268
                                        layer = new PictureMarkerSymbol(new URL(lblFileName.getText()),new URL(lblSelFileName.getText()));
269
                                layer.setIsShapeVisible(true);
270
                                layer.setSize(txtSize.getDouble());
271
                                layer.setOffset(new Point2D.Double(
272
                                        txtX.getDouble(),
273
                                        txtY.getDouble()));
274
//                                layer.setMask(mask.getMask());
275
                        }
276

    
277
                        return layer;
278
                } catch (IOException e) {
279
                        return SymbologyFactory.getWarningSymbol
280
                                (PluginServices.getText(this, "failed_acessing_files"), null, SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
281
                }
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
                btnSel.setEnabled(lblFileName.getText()!="");
342
        }
343

    
344
        public Class getSymbolClass() {
345
                return PictureMarkerSymbol.class;
346
        }
347

    
348
        public EditorTool[] getEditorTools() {
349
                return null;
350

    
351
        }
352

    
353
        public void actionPerformed(ActionEvent e) {
354
                fireSymbolChangedEvent();
355
        }
356

    
357
}