Statistics
| Revision:

svn-gvsig-desktop / 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 @ 38886

History | View | Annotate | Download (10.9 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
    
92
    public static final double MARKER_IMAGE_DEFAULT_WIDTH = 18;
93
    
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(Messages.getText("width")+":");
104
        protected JLabel lblX = new JLabel(Messages.getText("x_offset")+":");
105
        protected JLabel lblY = new JLabel(Messages.getText("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 Messages.getText("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
                                try {
150
                                        targetLbl.setText(url.toURI().getPath());
151
                                } catch (URISyntaxException e1) {
152
                                        NotificationManager.addWarning("URI Syntax error", e1);
153
                                } //.toString());
154
                                fireSymbolChangedEvent();
155
                        }
156
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
157
//                                File myFile = jfc.getSelectedFile();
158
//                                lastDir = jfc.getCurrentDirectory();
159
//                                if (myFile != null && myFile.exists()) {
160
//                                        if (isSelection) {
161
//                                                selPicFile = myFile;
162
//                                        } else {
163
//                                                picFile = myFile;
164
//                                        }
165
//                                        try {
166
//                                                targetLbl.setText(myFile.toURL().toString());
167
//                                        } catch (MalformedURLException e1) {
168
//                                                NotificationManager.addError(PluginServices.getText(this, "Error en la creaci?n" +
169
//                                                "de la URL"), e1);
170
//                                        }
171
//                                        fireSymbolChangedEvent();
172
//                                }
173
//                        }
174

    
175
                        btnSel.setEnabled(lblFileName.getText()!="");
176

    
177
                }
178

    
179
        };
180

    
181

    
182
        public PictureMarker(SymbolEditor owner) {
183
                super(owner);
184
                initialize();
185
        }
186

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

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

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

    
204
                // button browse
205
                btn = new JButton(Messages.getText("browse"));
206
                btn.addActionListener(chooseAction);
207

    
208
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
209
                aux2.add(btn);
210
                aux.addComponent("", aux2);
211

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

    
218
                // button browse
219
                btnSel = new JButton(Messages.getText("browse"));
220
                btnSel.addActionListener(chooseAction);
221
                btnSel.setEnabled(false);
222
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
223
                aux2.add(btnSel);
224
                aux.addComponent("", aux2);
225

    
226
                // picture width
227
                txtSize = new JIncrementalNumberField("5", 25, 0, Double.POSITIVE_INFINITY, 0.5);
228
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
229
                aux2.add(txtSize);
230
                aux.addComponent(lblSize, aux2 );
231
                txtSize.setDouble(MARKER_IMAGE_DEFAULT_WIDTH);
232

    
233

    
234
                // picture xOffset
235
                txtX = new JIncrementalNumberField("0", 25);
236
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
237
                aux2.add(txtX);
238
                aux.addComponent(lblX, aux2);
239

    
240

    
241
                // picture width
242
                txtY = new JIncrementalNumberField("0", 25);
243
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
244
                aux2.add(txtY);
245
                aux.addComponent(lblY,
246
                                aux2 );
247

    
248

    
249
                // initialize defaults
250
                txtSize.addActionListener(this);
251
                txtX.addActionListener(this);
252
                txtY.addActionListener(this);
253
                // buttons have their own listener!!!!
254

    
255
                myTab.add(aux);
256
                tabs.add(myTab);
257

    
258
//                mask = new Mask(this);
259
//                tabs.add(mask);
260
        }
261

    
262
        public ISymbol getLayer() {
263
                try {
264
                        IPictureMarkerSymbol layer = null;
265

    
266
                        if( lblFileName.getText().equals(""))
267
                                layer=null;
268

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

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

    
293
                }
294

    
295

    
296
        }
297

    
298
        public String getName() {
299
                return Messages.getText("picture_marker_symbol");
300

    
301
        }
302

    
303
        public JPanel[] getTabs() {
304
                return tabs.toArray(new JPanel[tabs.size()]);
305
        }
306

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

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

    
324
                                size = sym.getSize();
325
                                xOffset = sym.getOffset().getX();
326
                                yOffset = sym.getOffset().getY();
327

    
328
                                fileName = sym.getSource().toURI().getPath(); //.toString();
329
                                selectionFileName = sym.getSelectedSource().toURI().getPath(); //.toString();
330
                        }
331

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

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

    
352
        public EditorTool[] getEditorTools() {
353
                return null;
354

    
355
        }
356

    
357
        public void actionPerformed(ActionEvent e) {
358
                fireSymbolChangedEvent();
359
        }
360

    
361
        public boolean canManageSymbol(ISymbol symbol) {
362
                return symbol instanceof IPictureMarkerSymbol;
363
        }
364

    
365
}