Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / PictureMarker.java @ 40560

History | View | Annotate | Download (10.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui.styling;
25

    
26
import java.awt.FlowLayout;
27
import java.awt.Font;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.geom.Point2D;
31
import java.io.File;
32
import java.io.IOException;
33
import java.net.URISyntaxException;
34
import java.net.URL;
35
import java.util.ArrayList;
36

    
37
import javax.swing.JButton;
38
import javax.swing.JFileChooser;
39
import javax.swing.JLabel;
40
import javax.swing.JPanel;
41
import javax.swing.filechooser.FileFilter;
42

    
43
import org.gvsig.andami.messages.NotificationManager;
44
import org.gvsig.fmap.mapcontext.MapContextLocator;
45
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
46
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
47
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
48
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
49
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
50
import org.gvsig.i18n.Messages;
51
import org.gvsig.symbology.SymbologyLocator;
52
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
53

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

    
92
        private ActionListener chooseAction = new ActionListener() {
93

    
94
                public void actionPerformed(ActionEvent e) {
95

    
96
                        JLabel targetLbl;
97
                        if (e.getSource().equals(btn)) {
98
                                targetLbl = lblFileName;
99
                        } else {
100
                                targetLbl = lblSelFileName;
101
                        }
102
                        FileFilter ff = new FileFilter() {
103
                                public boolean accept(File f) {
104
                                        if (f.isDirectory()) return true;
105
                                        String fName = f.getAbsolutePath();
106
                                        if (fName!=null) {
107
                                                fName = fName.toLowerCase();
108
                                                return fName.endsWith(".png")
109
                                                || fName.endsWith(".gif")
110
                                                || fName.endsWith(".jpg")
111
                                                || fName.endsWith(".jpeg")
112
                                                || fName.endsWith(".bmp")
113
                                                || fName.endsWith(".svg");
114
                                        }
115
                                        return false;
116
                                }
117

    
118
                                public String getDescription() {
119
                                        return Messages.getText("bitmap_and_svg_image_files");
120
                                }
121
                        };
122
                        JUrlFileChooser jfc = new JUrlFileChooser(getName(), null);
123
                        jfc.setFileFilter(ff);
124
                        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
125
                        jfc.setSelectedFile(picFile);
126
                        jfc.setMultiSelectionEnabled(false);
127
                        int returnVal = jfc.showOpenDialog(PictureMarker.this.owner);
128
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
129

    
130
                                URL url = jfc.getSelectedURL();
131
                                if (url == null) return;
132
                                try {
133
                                        targetLbl.setText(url.toURI().getPath());
134
                                } catch (URISyntaxException e1) {
135
                                        NotificationManager.addWarning("URI Syntax error", e1);
136
                                } //.toString());
137
                                fireSymbolChangedEvent();
138
                        }
139
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
140
//                                File myFile = jfc.getSelectedFile();
141
//                                lastDir = jfc.getCurrentDirectory();
142
//                                if (myFile != null && myFile.exists()) {
143
//                                        if (isSelection) {
144
//                                                selPicFile = myFile;
145
//                                        } else {
146
//                                                picFile = myFile;
147
//                                        }
148
//                                        try {
149
//                                                targetLbl.setText(myFile.toURL().toString());
150
//                                        } catch (MalformedURLException e1) {
151
//                                                NotificationManager.addError(PluginServices.getText(this, "Error en la creaci?n" +
152
//                                                "de la URL"), e1);
153
//                                        }
154
//                                        fireSymbolChangedEvent();
155
//                                }
156
//                        }
157

    
158
                        btnSel.setEnabled(lblFileName.getText()!="");
159

    
160
                }
161

    
162
        };
163

    
164

    
165
        public PictureMarker(SymbolEditor owner) {
166
                super(owner);
167
                initialize();
168
        }
169

    
170
        /**
171
         * Initializes the parameters that define a picturmarker.To do it,
172
         * a tab is created inside the SymbolEditor panel with default values
173
         *  for the different attributes of the picture marker.
174
         */
175

    
176
        private void initialize() {
177
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
178
                myTab.setName(Messages.getText("picture_marker"));
179
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
180

    
181
                // picture file label
182
                lblFileName = new JLabel();
183
                lblFileName.setFont(lblFileName.getFont().deriveFont(Font.BOLD));
184
                aux.addComponent(Messages.getText("picture_file")+":",
185
                                lblFileName);
186

    
187
                // button browse
188
                btn = new JButton(Messages.getText("browse"));
189
                btn.addActionListener(chooseAction);
190

    
191
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
192
                aux2.add(btn);
193
                aux.addComponent("", aux2);
194

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

    
201
                // button browse
202
                btnSel = new JButton(Messages.getText("browse"));
203
                btnSel.addActionListener(chooseAction);
204
                btnSel.setEnabled(false);
205
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
206
                aux2.add(btnSel);
207
                aux.addComponent("", aux2);
208

    
209
                // picture width
210
                txtSize = new JIncrementalNumberField("5", 25, 0, Double.POSITIVE_INFINITY, 0.5);
211
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
212
                aux2.add(txtSize);
213
                aux.addComponent(lblSize, aux2 );
214
                txtSize.setDouble(MARKER_IMAGE_DEFAULT_WIDTH);
215

    
216

    
217
                // picture xOffset
218
                txtX = new JIncrementalNumberField("0", 25);
219
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
220
                aux2.add(txtX);
221
                aux.addComponent(lblX, aux2);
222

    
223

    
224
                // picture width
225
                txtY = new JIncrementalNumberField("0", 25);
226
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
227
                aux2.add(txtY);
228
                aux.addComponent(lblY,
229
                                aux2 );
230

    
231

    
232
                // initialize defaults
233
                txtSize.addActionListener(this);
234
                txtX.addActionListener(this);
235
                txtY.addActionListener(this);
236
                // buttons have their own listener!!!!
237

    
238
                myTab.add(aux);
239
                tabs.add(myTab);
240

    
241
//                mask = new Mask(this);
242
//                tabs.add(mask);
243
        }
244

    
245
        public ISymbol getLayer() {
246
                try {
247
                        IPictureMarkerSymbol layer = null;
248

    
249
                        if( lblFileName.getText().equals(""))
250
                                layer=null;
251

    
252
                        else {
253
                                if (lblSelFileName.getText().equals("")){
254
                                        layer =  SymbologyLocator.getSymbologyManager().createPictureMarkerSymbol(new File(lblFileName.getText()).toURI().toURL(),null);
255
                                }else {
256
                                        layer = SymbologyLocator.getSymbologyManager().createPictureMarkerSymbol(new File(lblFileName.getText()).toURI().toURL(),new File(lblSelFileName.getText()).toURI().toURL());
257
                                }
258
//                                layer.setIsShapeVisible(true); //True is the default value of this property
259
                                layer.setSize(txtSize.getDouble());
260
                                layer.setOffset(new Point2D.Double(
261
                                        txtX.getDouble(),
262
                                        txtY.getDouble()));
263
//                                layer.setMask(mask.getMask());
264
                        }
265

    
266
                        return layer;
267
                } catch (IOException e) {
268
                        IWarningSymbol warning =
269
                                (IWarningSymbol) MapContextLocator.getSymbolManager()
270
                                .getWarningSymbol(
271
                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
272
                                                Messages.getText("failed_acessing_files"),
273
                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
274
                        return warning;
275

    
276
                }
277

    
278

    
279
        }
280

    
281
        public String getName() {
282
                return Messages.getText("picture_marker_symbol");
283

    
284
        }
285

    
286
        public JPanel[] getTabs() {
287
                return tabs.toArray(new JPanel[tabs.size()]);
288
        }
289

    
290
        public void refreshControls(ISymbol layer) {
291
                IPictureMarkerSymbol sym;
292
                try {
293
                        double size, xOffset, yOffset;
294
                        String fileName = null, selectionFileName = null;
295
                        if (layer == null) {
296
                                // initialize defaults
297
                                System.err.println(getClass().getName()+":: should be unreachable code");
298

    
299
                                size = 1D;
300
                                xOffset = 0D;
301
                                yOffset = 0D;
302
                                fileName = "-";
303
                                selectionFileName = "-";
304
                        } else {
305
                                sym = (IPictureMarkerSymbol) layer;
306

    
307
                                size = sym.getSize();
308
                                xOffset = sym.getOffset().getX();
309
                                yOffset = sym.getOffset().getY();
310

    
311
                                fileName = sym.getSource().toURI().getPath(); //.toString();
312
                                selectionFileName = sym.getSelectedSource().toURI().getPath(); //.toString();
313
                        }
314

    
315
                        setValues(size, xOffset, yOffset, fileName, selectionFileName);
316
                } catch (IndexOutOfBoundsException ioEx) {
317
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
318
                } catch (ClassCastException ccEx) {
319
                        NotificationManager.addWarning("Illegal casting from " +
320
                                        layer.getClass().getName() + " to IPictureMarkerSymbol.", ccEx);
321
                } catch (URISyntaxException e) {
322
                        NotificationManager.addWarning("URI Syntax error", e);
323
                }
324
        }
325

    
326
        protected void setValues(double size, double xOffset, double yOffset, String fileName, String selectionFileName) {
327
                txtSize.setDouble(size);
328
                txtX.setDouble(xOffset);
329
                txtY.setDouble(yOffset);
330
                lblFileName.setText(fileName);
331
                lblSelFileName.setText(selectionFileName);
332
                btnSel.setEnabled(lblFileName.getText()!="");
333
        }
334

    
335
        public EditorTool[] getEditorTools() {
336
                return null;
337

    
338
        }
339

    
340
        public void actionPerformed(ActionEvent e) {
341
                fireSymbolChangedEvent();
342
        }
343

    
344
        public boolean canManageSymbol(ISymbol symbol) {
345
                return symbol instanceof IPictureMarkerSymbol;
346
        }
347

    
348
}