Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / PictureMarker.java @ 34294

History | View | Annotate | Download (10.8 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.app.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.URISyntaxException;
51
import java.net.URL;
52
import java.util.ArrayList;
53

    
54
import javax.swing.JButton;
55
import javax.swing.JFileChooser;
56
import javax.swing.JLabel;
57
import javax.swing.JPanel;
58
import javax.swing.filechooser.FileFilter;
59

    
60
import org.gvsig.andami.messages.NotificationManager;
61
import org.gvsig.fmap.mapcontext.MapContextLocator;
62
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
63
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
64
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
65
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
66
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
67
import org.gvsig.i18n.Messages;
68
import org.gvsig.symbology.SymbologyLocator;
69
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
70

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

    
106
        private ActionListener chooseAction = new ActionListener() {
107

    
108
                public void actionPerformed(ActionEvent e) {
109

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

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

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

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

    
174
                }
175

    
176
        };
177

    
178

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

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

    
190
        private void initialize() {
191
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
192
                myTab.setName(Messages.getText("picture_marker"));
193
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
194

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

    
201
                // button browse
202
                btn = new JButton(Messages.getText("browse"));
203
                btn.addActionListener(chooseAction);
204

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

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

    
215
                // button browse
216
                btnSel = new JButton(Messages.getText("browse"));
217
                btnSel.addActionListener(chooseAction);
218
                btnSel.setEnabled(false);
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
                        IPictureMarkerSymbol layer = null;
262

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

    
266
                        else {
267
                                if (lblSelFileName.getText().equals("")){
268
                                        layer =  SymbologyLocator.getSymbologyManager().createPictureMarkerSymbol(new File(lblFileName.getText()).toURI().toURL(),null);
269
                                }else {
270
                                        layer = SymbologyLocator.getSymbologyManager().createPictureMarkerSymbol(new File(lblFileName.getText()).toURI().toURL(),new File(lblSelFileName.getText()).toURI().toURL());
271
                                }
272
//                                layer.setIsShapeVisible(true); //True is the default value of this property
273
                                layer.setSize(txtSize.getDouble());
274
                                layer.setOffset(new Point2D.Double(
275
                                        txtX.getDouble(),
276
                                        txtY.getDouble()));
277
//                                layer.setMask(mask.getMask());
278
                        }
279

    
280
                        return layer;
281
                } catch (IOException e) {
282
                        IWarningSymbol warning =
283
                                (IWarningSymbol) MapContextLocator.getSymbolManager()
284
                                .getWarningSymbol(
285
                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
286
                                                Messages.getText("failed_acessing_files"),
287
                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
288
                        return warning;
289

    
290
                }
291

    
292

    
293
        }
294

    
295
        public String getName() {
296
                return Messages.getText("picture_marker_symbol");
297

    
298
        }
299

    
300
        public JPanel[] getTabs() {
301
                return tabs.toArray(new JPanel[tabs.size()]);
302
        }
303

    
304
        public void refreshControls(ISymbol layer) {
305
                IPictureMarkerSymbol sym;
306
                try {
307
                        double size, xOffset, yOffset;
308
                        String fileName = null, selectionFileName = null;
309
                        if (layer == null) {
310
                                // initialize defaults
311
                                System.err.println(getClass().getName()+":: should be unreachable code");
312

    
313
                                size = 1D;
314
                                xOffset = 0D;
315
                                yOffset = 0D;
316
                                fileName = "-";
317
                                selectionFileName = "-";
318
                        } else {
319
                                sym = (IPictureMarkerSymbol) layer;
320

    
321
                                size = sym.getSize();
322
                                xOffset = sym.getOffset().getX();
323
                                yOffset = sym.getOffset().getY();
324

    
325
                                fileName = sym.getSource().toURI().getPath(); //.toString();
326
                                selectionFileName = sym.getSelectedSource().toURI().getPath(); //.toString();
327
                        }
328

    
329
                        setValues(size, xOffset, yOffset, fileName, selectionFileName);
330
                } catch (IndexOutOfBoundsException ioEx) {
331
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
332
                } catch (ClassCastException ccEx) {
333
                        NotificationManager.addWarning("Illegal casting from " +
334
                                        layer.getClass().getName() + " to IPictureMarkerSymbol.", ccEx);
335
                } catch (URISyntaxException e) {
336
                        NotificationManager.addWarning("URI Syntax error", e);
337
                }
338
        }
339

    
340
        protected void setValues(double size, double xOffset, double yOffset, String fileName, String selectionFileName) {
341
                txtSize.setDouble(size);
342
                txtX.setDouble(xOffset);
343
                txtY.setDouble(yOffset);
344
                lblFileName.setText(fileName);
345
                lblSelFileName.setText(selectionFileName);
346
                btnSel.setEnabled(lblFileName.getText()!="");
347
        }
348

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

    
352
        }
353

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

    
358
        public boolean canManageSymbol(ISymbol symbol) {
359
                return symbol instanceof IPictureMarkerSymbol;
360
        }
361

    
362
}