Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / PictureMarker.java @ 12867

History | View | Annotate | Download (8.27 KB)

1 11877 jaume
/* 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 com.iver.cit.gvsig.gui.styling;
42
43
import java.awt.FlowLayout;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
46
import java.awt.geom.Point2D;
47
import java.io.File;
48
import java.io.IOException;
49
import java.util.ArrayList;
50
51
import javax.swing.JButton;
52
import javax.swing.JFileChooser;
53
import javax.swing.JLabel;
54
import javax.swing.JPanel;
55
import javax.swing.filechooser.FileFilter;
56
57
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
58
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
59
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
63
import com.iver.cit.gvsig.fmap.core.symbols.PictureMarkerSymbol;
64
import com.lowagie.text.Font;
65
66
/**
67
 * @author jaume dominguez faus - jaume.dominguez@iver.es
68
 */
69
public class PictureMarker extends AbstractTypeSymbolEditorPanel implements
70
                ActionListener {
71
        private static File lastDir;
72 12427 jaume
        protected ArrayList tabs = new ArrayList();
73
        protected JIncrementalNumberField txtSize;
74
        protected JIncrementalNumberField txtX;
75
        protected JIncrementalNumberField txtY;
76
        protected Mask mask;
77
        protected JLabel lblFileName;
78
        protected JLabel lblSelFileName;
79 11877 jaume
        private File picFile;
80 12427 jaume
        private File selPicFile;
81
        protected JLabel lblSize = new JLabel(PluginServices.getText(this, "width")+":");
82
        protected JLabel lblX = new JLabel(PluginServices.getText(this, "x_offset")+":");
83
        protected JLabel lblY = new JLabel(PluginServices.getText(this, "y_offset")+":");
84
        private JButton btn;
85
        private JButton btnSel;
86 11877 jaume
87 12427 jaume
        private ActionListener chooseAction = new ActionListener() {
88 11877 jaume
89 12427 jaume
                public void actionPerformed(ActionEvent e) {
90
                        JLabel targetLbl;
91
                        boolean isSelection;
92
                        if (e.getSource().equals(btn)) {
93
                                targetLbl = lblFileName;
94
                                isSelection = false;
95
                        } else {
96
                                targetLbl = lblSelFileName;
97
                                isSelection = true;
98
                        }
99
                        FileFilter ff = new FileFilter() {
100
                                public boolean accept(File f) {
101
                                        if (f.isDirectory()) return true;
102
                                        String fName = f.getAbsolutePath();
103
                                        if (fName!=null) {
104
                                                fName = fName.toLowerCase();
105
                                                return fName.endsWith(".png")
106
                                                || fName.endsWith(".gif")
107
                                                || fName.endsWith(".jpg")
108
                                                || fName.endsWith(".jpeg")
109
                                                || fName.endsWith(".bmp")
110
                                                || fName.endsWith(".svg");
111
                                        }
112
                                        return false;
113
                                }
114
115
                                public String getDescription() {
116
                                        return PluginServices.getText(this, "bitmap_and_svg_image_files");
117
                                }
118
                        };
119
                        JFileChooser jfc = new JFileChooser(lastDir);
120
                        jfc.setFileFilter(ff);
121
                        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
122
                        jfc.setSelectedFile(picFile);
123
                        jfc.setMultiSelectionEnabled(false);
124
                        int returnVal = jfc.showOpenDialog(PictureMarker.this.owner);
125
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
126
                                File myFile = jfc.getSelectedFile();
127
                                lastDir = jfc.getCurrentDirectory();
128
                                if (myFile != null && myFile.exists()) {
129
                                        if (isSelection) {
130
                                                selPicFile = myFile;
131
                                        } else {
132
                                                picFile = myFile;
133
                                        }
134
                                        targetLbl.setText(myFile.getAbsolutePath());
135
                                        fireSymbolChangedEvent();
136
                                }
137
                        }
138
                }
139
140
        };
141
142
143 11877 jaume
        public PictureMarker(SymbolEditor owner) {
144
                super(owner);
145
                initialize();
146
        }
147
148
        private void initialize() {
149
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
150
                myTab.setName(PluginServices.getText(this, "simple_marker"));
151
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
152
153
                // picture file label
154
                lblFileName = new JLabel();
155
                lblFileName.setFont(lblFileName.getFont().deriveFont(Font.BOLD));
156
                aux.addComponent(PluginServices.getText(this, "picture_file")+":",
157
                                lblFileName);
158
159
                // button browse
160 12427 jaume
                btn = new JButton(PluginServices.getText(this, "browse"));
161
                btn.addActionListener(chooseAction);
162 11877 jaume
163
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
164
                aux2.add(btn);
165
                aux.addComponent("", aux2);
166
167 12427 jaume
                // selection picture file
168
                lblSelFileName = new JLabel();
169
                lblSelFileName.setFont(lblSelFileName.getFont().deriveFont(Font.BOLD));
170
                aux.addComponent(PluginServices.getText(this, "selection_picture_file")+":",
171
                                lblSelFileName);
172
173
                // button browse
174
                btnSel = new JButton(PluginServices.getText(this, "browse"));
175
                btnSel.addActionListener(chooseAction);
176
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
177
                aux2.add(btnSel);
178
                aux.addComponent("", aux2);
179
180 11877 jaume
                // picture width
181 12427 jaume
                txtSize = new JIncrementalNumberField("5", 25, 0, Double.POSITIVE_INFINITY, 0.5);
182 11877 jaume
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
183
                aux2.add(txtSize);
184 12427 jaume
                aux.addComponent(lblSize, aux2 );
185 11877 jaume
                txtSize.setDouble(5);
186
187
188
                // picture xOffset
189 12427 jaume
                txtX = new JIncrementalNumberField("0", 25);
190 11877 jaume
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
191 12427 jaume
                aux2.add(txtX);
192
                aux.addComponent(lblX, aux2);
193 11877 jaume
194
195
                // picture width
196 12427 jaume
                txtY = new JIncrementalNumberField("0", 25);
197 11877 jaume
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
198 12427 jaume
                aux2.add(txtY);
199
                aux.addComponent(lblY,
200 11877 jaume
                                aux2 );
201
202
203
                // initialize defaults
204
                txtSize.addActionListener(this);
205 12427 jaume
                txtX.addActionListener(this);
206
                txtY.addActionListener(this);
207 11877 jaume
208
                myTab.add(aux);
209
                tabs.add(myTab);
210
211
                mask = new Mask(this);
212
                tabs.add(mask);
213
        }
214
215
        public ISymbol getLayer() {
216
                try {
217 12561 jaume
                        PictureMarkerSymbol layer;
218 11877 jaume
219 12561 jaume
                        layer = new PictureMarkerSymbol(new File(lblFileName.getText()), new File(lblSelFileName.getText()));
220
                        layer.setIsShapeVisible(true);
221
                        layer.setSize(txtSize.getDouble());
222
                        layer.setOffset(new Point2D.Double(
223 12427 jaume
                                        txtX.getDouble(),
224
                                        txtY.getDouble()));
225 12561 jaume
                        layer.setMask(mask.getMask());
226
                        return layer;
227 11877 jaume
                } catch (IOException e) {
228
                        NotificationManager.addError(PluginServices.getText(this, "failed_accessing_file"), e);
229
                        return null;
230
                }
231
232
        }
233
234
        public String getName() {
235
                return PluginServices.getText(this, "picture_marker_symbol");
236
237
        }
238
239
        public JPanel[] getTabs() {
240
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
241
        }
242
243
        public void refreshControls(ISymbol layer) {
244
                PictureMarkerSymbol sym;
245
                try {
246 12427 jaume
                        double size, xOffset, yOffset;
247
                        String fileName, selectionFileName;
248 11877 jaume
                        if (layer == null) {
249
                                // initialize defaults
250 12427 jaume
                                System.err.println(getClass().getName()+":: should be unreachable code");
251 11877 jaume
252 12427 jaume
                                size = 1D;
253
                                xOffset = 0D;
254
                                yOffset = 0D;
255
                                fileName = "-";
256
                                selectionFileName = "-";
257 11877 jaume
                        } else {
258
                                sym = (PictureMarkerSymbol) layer;
259
260 12427 jaume
                                size = sym.getSize();
261
                                xOffset = sym.getOffset().getX();
262
                                yOffset = sym.getOffset().getY();
263
                                fileName = sym.getImagePath();
264
                                selectionFileName = sym.getSelImagePath();
265 11877 jaume
                        }
266 12427 jaume
267
                        setValues(size, xOffset, yOffset, fileName, selectionFileName);
268 11877 jaume
                } catch (IndexOutOfBoundsException ioEx) {
269
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
270
                } catch (ClassCastException ccEx) {
271
                        NotificationManager.addWarning("Illegal casting from " +
272
                                        layer.getClassName() + " to " + getSymbolClass().
273
                                        getName() + ".", ccEx);
274
275
                }
276
        }
277
278 12427 jaume
        protected void setValues(double size, double xOffset, double yOffset, String fileName, String selectionFileName) {
279
                txtSize.setDouble(size);
280
                txtX.setDouble(xOffset);
281
                txtY.setDouble(yOffset);
282
                lblFileName.setText(fileName);
283
        }
284
285 11877 jaume
        public Class getSymbolClass() {
286
                return PictureMarkerSymbol.class;
287
        }
288
289
        public EditorTool getEditorTool() {
290
                return null;
291
292
        }
293
294
        public void actionPerformed(ActionEvent e) {
295
                fireSymbolChangedEvent();
296
        }
297
298
}