Statistics
| Revision:

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

History | View | Annotate | Download (9.55 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.util.ArrayList;
51

    
52
import javax.swing.JButton;
53
import javax.swing.JFileChooser;
54
import javax.swing.JLabel;
55
import javax.swing.JPanel;
56
import javax.swing.filechooser.FileFilter;
57

    
58
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
59
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
60
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
61

    
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.messages.NotificationManager;
64
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
65
import com.iver.cit.gvsig.gui.styling.AbstractTypeSymbolEditor;
66
import com.iver.cit.gvsig.gui.styling.EditorTool;
67
import com.iver.cit.gvsig.gui.styling.Mask;
68
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
69

    
70
/**
71
 * PictureMarker allows the user to store and modify the properties that define a
72
 * <b>picture marker symbol</b>.<p>
73
 * <p>
74
 * This functionality is carried out thanks to a tab (simple marker)which is
75
 * included in the panel to edit the properities of a symbol (SymbolEditor)how is
76
 * explained in AbstractTypeSymbolEditor.
77
 * <p>
78
 * First of all, in the above mentioned tab the user will have options to change
79
 * the files from where the pictures for the symbol are taken (one for the symbol
80
 * when it is not selected in the map and the other when it is done).<p>
81
 * <p>
82
 * Secondly, the user will have options to modify the pictures which had been
83
 * selected before  (width and offset) .
84
 *
85
 *@see AbstractTypeSymbolEditor
86
 *@author jaume dominguez faus - jaume.dominguez@iver.es
87
 */
88
public class PictureMarker extends AbstractTypeSymbolEditor implements
89
ActionListener {
90
        private static File lastDir;
91
        protected ArrayList<JPanel> tabs = new ArrayList<JPanel>();
92
        protected JIncrementalNumberField txtSize;
93
        protected JIncrementalNumberField txtX;
94
        protected JIncrementalNumberField txtY;
95
        protected Mask mask;
96
        protected JLabel lblFileName;
97
        protected JLabel lblSelFileName;
98
        private File picFile;
99
        private File selPicFile;
100
        protected JLabel lblSize = new JLabel(PluginServices.getText(this, "width")+":");
101
        protected JLabel lblX = new JLabel(PluginServices.getText(this, "x_offset")+":");
102
        protected JLabel lblY = new JLabel(PluginServices.getText(this, "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
                        boolean isSelection;
112
                        if (e.getSource().equals(btn)) {
113
                                targetLbl = lblFileName;
114
                                isSelection = false;
115
                        } else {
116
                                targetLbl = lblSelFileName;
117
                                isSelection = true;
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
                        JFileChooser jfc = new JFileChooser(lastDir);
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
                                File myFile = jfc.getSelectedFile();
147
                                lastDir = jfc.getCurrentDirectory();
148
                                if (myFile != null && myFile.exists()) {
149
                                        if (isSelection) {
150
                                                selPicFile = myFile;
151
                                        } else {
152
                                                picFile = myFile;
153
                                        }
154
                                        targetLbl.setText(myFile.getAbsolutePath());
155
                                        fireSymbolChangedEvent();
156
                                }
157
                        }
158

    
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(PluginServices.getText(this, "simple_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(PluginServices.getText(this, "picture_file")+":",
185
                                lblFileName);
186

    
187
                // button browse
188
                btn = new JButton(PluginServices.getText(this, "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(PluginServices.getText(this, "selection_picture_file")+":",
199
                                lblSelFileName);
200

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

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

    
215

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

    
222

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

    
230

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

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

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

    
244
        public ISymbol getLayer() {
245
                try {
246
                        PictureMarkerSymbol layer = null;
247

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

    
251
                        else {
252
                        layer = new PictureMarkerSymbol(new File(lblFileName.getText()), new File(lblSelFileName.getText()));
253
                        layer.setIsShapeVisible(true);
254
                        layer.setSize(txtSize.getDouble());
255
                        layer.setOffset(new Point2D.Double(
256
                                        txtX.getDouble(),
257
                                        txtY.getDouble()));
258
                        layer.setMask(mask.getMask());
259
                        }
260

    
261
                        return layer;
262
                } catch (IOException e) {
263
                        NotificationManager.addError(PluginServices.getText(this, "failed_accessing_file"), e);
264
                        return null;
265
                }
266

    
267
        }
268

    
269
        public String getName() {
270
                return PluginServices.getText(this, "picture_marker_symbol");
271

    
272
        }
273

    
274
        public JPanel[] getTabs() {
275
                return tabs.toArray(new JPanel[tabs.size()]);
276
        }
277

    
278
        public void refreshControls(ISymbol layer) {
279
                PictureMarkerSymbol sym;
280
                try {
281
                        double size, xOffset, yOffset;
282
                        String fileName, selectionFileName;
283
                        if (layer == null) {
284
                                // initialize defaults
285
                                System.err.println(getClass().getName()+":: should be unreachable code");
286

    
287
                                size = 1D;
288
                                xOffset = 0D;
289
                                yOffset = 0D;
290
                                fileName = "-";
291
                                selectionFileName = "-";
292
                        } else {
293
                                sym = (PictureMarkerSymbol) layer;
294

    
295
                                size = sym.getSize();
296
                                xOffset = sym.getOffset().getX();
297
                                yOffset = sym.getOffset().getY();
298

    
299
                                fileName = sym.getImagePath();
300
                                selectionFileName = sym.getSelImagePath();
301
                        }
302

    
303
                        setValues(size, xOffset, yOffset, fileName, selectionFileName);
304
                } catch (IndexOutOfBoundsException ioEx) {
305
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
306
                } catch (ClassCastException ccEx) {
307
                        NotificationManager.addWarning("Illegal casting from " +
308
                                        layer.getClassName() + " to " + getSymbolClass().
309
                                        getName() + ".", ccEx);
310

    
311
                }
312
        }
313

    
314
        protected void setValues(double size, double xOffset, double yOffset, String fileName, String selectionFileName) {
315
                txtSize.setDouble(size);
316
                txtX.setDouble(xOffset);
317
                txtY.setDouble(yOffset);
318
                lblFileName.setText(fileName);
319
                lblSelFileName.setText(selectionFileName);
320
        }
321

    
322
        public Class getSymbolClass() {
323
                return PictureMarkerSymbol.class;
324
        }
325

    
326
        public EditorTool[] getEditorTools() {
327
                return null;
328

    
329
        }
330

    
331
        public void actionPerformed(ActionEvent e) {
332
                fireSymbolChangedEvent();
333
        }
334

    
335
}