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 / PictureFill.java @ 43510

History | View | Annotate | Download (13.1 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40560 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40560 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40435 jjdelcerro
 *
21 40560 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
24
package org.gvsig.app.gui.styling;
25
26
import java.awt.Color;
27
import java.awt.FlowLayout;
28
import java.awt.Font;
29
import java.awt.GridLayout;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.io.File;
33
import java.io.IOException;
34
import java.net.URISyntaxException;
35
import java.net.URL;
36
import java.util.ArrayList;
37
38
import javax.swing.JCheckBox;
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.app.gui.panels.ColorChooserPanel;
45
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton;
46
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.mapcontext.MapContextLocator;
48
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
51
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
52
import org.gvsig.gui.beans.swing.JBlank;
53
import org.gvsig.gui.beans.swing.JButton;
54
import org.gvsig.gui.beans.swing.JFileChooser;
55 42026 jjdelcerro
import org.gvsig.gui.beans.swing.JNumberSpinner;
56 40435 jjdelcerro
import org.gvsig.i18n.Messages;
57
import org.gvsig.symbology.SymbologyLocator;
58
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IPictureFillSymbol;
59
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
60
61
/**
62
 * <b>PictureFill</b> allows to store and modify the properties that fills a
63
 * polygon with a padding and an outline<p>
64
 * <p>
65
 * This functionality is carried out thanks to two tabs (picture fill and MarkerFillProperties)
66
 * which are included in the panel to edit the properities of a symbol (SymbolEditor)
67
 * how is explained in AbstractTypeSymbolEditor.<p>
68
 * <p>
69
 * The first tab (picture fill)permits the user to select the picture for the padding and
70
 * differentes options to modify it such as the angle(<b>incrAngle</b>) and the scale
71
 * (<b>incrScaleX,incrScaleY</b>). Also, there is an option to select a color for the
72
 * fill (<b>jccFillColor</b>).
73
 * <p>
74
 * The second tab is implementes as a MarkerFillProperties class and offers the possibilities
75
 * to change the separtion and the offset.
76
 *
77
 *
78
 *@see MarkerFillProperties
79
 *@see AbstractTypeSymbolEditor
80
 *@author jaume dominguez faus - jaume.dominguez@iver.es
81
 */
82
public class PictureFill extends AbstractTypeSymbolEditor implements
83
ActionListener {
84
85
        private JLabel lblFileName;
86
        private JLabel lblSelFileName;
87
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
88
        private MarkerFillProperties fillProperties;
89
        private File picFile;
90 42026 jjdelcerro
        private JNumberSpinner incrAngle;
91
        private JNumberSpinner incrScaleX;
92
        private JNumberSpinner incrScaleY;
93 40435 jjdelcerro
        private ColorChooserPanel jccFillColor;
94
        private ILineSymbol outline;
95
        private JSymbolPreviewButton btnOutline;
96
        private JCheckBox useBorder;
97
98
        private JButton btnBrowseFile;
99
    private JButton btnBrowseFileSelected;
100
101
        private static final double DEGREE_TO_RADIANS = Math.PI/180D;
102
103
        private ActionListener chooseAction = new ActionListener() {
104
                public void actionPerformed(ActionEvent e) {
105
                        boolean isSelection;
106
                        JLabel targetLbl;
107
108
                        if (e.getSource().equals(btnBrowseFile)) {
109
                                targetLbl = lblFileName;
110
                                isSelection = false;
111
                        } else {
112
                                targetLbl = lblSelFileName;
113
                                isSelection = true;
114
                        }
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(PictureFill.this.owner);
142
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
143
//                                File myFile = jfc.getSelectedFile();
144
//                                lastDir = jfc.getCurrentDirectory();
145
//                                if (myFile != null && myFile.exists()) {
146
//                                        if(!isSelection){
147
//                                                picFile = myFile;
148
//                                        }
149
//                                        else{
150
//                                                selPicFile = myFile;
151
//                                        }
152
//                                        try {
153
//                                                targetLbl.setText(myFile.toURL().toString());
154
//                                        } catch (MalformedURLException e1) {
155
//                                                NotificationManager.addError(PluginServices.getText(this, "invalid_url"), e1);
156
//                                        }
157
//                                        fireSymbolChangedEvent();
158
//                                }
159
//                        }
160
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
161
162
                                URL url = jfc.getSelectedURL();
163
                                if (url == null) return;
164
                                try {
165
                                        targetLbl.setText(url.toURI().getPath());
166
                                } catch (URISyntaxException e1) {
167
                                        NotificationManager.addWarning("URI Syntax error", e1);
168
                                }
169
                                fireSymbolChangedEvent();
170
                        }
171
                        boolean enabled = (lblFileName.getText()!="");
172
                        enableControls(lblFileName.getText()!="");
173
174
                }
175
        };
176
177
        /**
178
         * Constructor method
179
         * @param owner
180
         */
181
        public PictureFill(SymbolEditor owner) {
182
                super(owner);
183
                initialize();
184
        }
185
186
        /**
187
         * Initializes the parameters that allows the user to fill the padding of
188
         * a polygon with a picture style.To do it, two tabs are created (picture fill and
189
         * MarkerFillProperties)inside the SymbolEditor panel with default values for the
190
     * different attributes.
191
         */
192
        private void initialize() {
193
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
194
                myTab.setName(Messages.getText("picture_fill"));
195
196
                btnBrowseFile = new JButton(Messages.getText("browse"));
197
                btnBrowseFile.addActionListener(chooseAction);
198
199
                btnBrowseFileSelected = new JButton(Messages.getText("browse"));
200
                btnBrowseFileSelected.addActionListener(chooseAction);
201
202
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
203
204
                JPanel auxLabelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
205
                JLabel lblName = new JLabel();
206
                lblName.setFont(lblName.getFont().deriveFont(Font.BOLD));
207
                lblName.setText(Messages.getText("picture_file")+":");
208
                auxLabelPic.add(lblName);
209
210
                aux2.add(btnBrowseFile);
211
                aux2.add(lblFileName = new JLabel(""));
212
213
                JPanel auxLabelSelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
214
                JLabel lblSelName = new JLabel();
215
                lblSelName.setFont(lblSelName.getFont().deriveFont(Font.BOLD));
216
                lblSelName.setText(Messages.getText("selection_picture_file")+":");
217
                auxLabelSelPic.add(lblSelName);
218
219
                JPanel aux4 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
220
                aux4.add(btnBrowseFileSelected);
221
                aux4.add(lblSelFileName = new JLabel(""));
222
223
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
224
                aux.addComponent(new JBlank(5, 5));
225
                aux.addComponent(auxLabelPic);
226
                aux.addComponent(aux2);
227
                aux.addComponent(auxLabelSelPic);
228
                aux.addComponent(aux4);
229
230
231
                aux2 = new JPanel(new GridLayout(1, 2, 20, 5));
232
                GridBagLayoutPanel aux3;
233
                aux3 = new GridBagLayoutPanel();
234
                aux3.addComponent(Messages.getText("angle")+":",
235 42026 jjdelcerro
                                incrAngle = new JNumberSpinner(0, 20));
236 40435 jjdelcerro
                aux3.addComponent(Messages.getText("scale")+"X:",
237 42026 jjdelcerro
                                incrScaleX = new JNumberSpinner(
238
                                                1.0,
239
                                                20,
240 40435 jjdelcerro
                                                0.01,
241
                                                Double.POSITIVE_INFINITY,
242
                                                0.1));
243
                incrScaleX.setDouble(1);
244
                aux3.addComponent(Messages.getText("scale")+"Y:",
245 42026 jjdelcerro
                                incrScaleY = new JNumberSpinner(
246
                                                1.0,
247
                                                20,
248 40435 jjdelcerro
                                                0.01,
249
                                                Double.POSITIVE_INFINITY,
250
                                                0.1));
251
                incrScaleY.setDouble(1);
252
                aux2.add(aux3);
253
254
                aux3 = new GridBagLayoutPanel();
255
                aux3.addComponent(new JBlank(5,5));
256
                aux3.addComponent(new JLabel (Messages.getText("fill_color")+":"));
257
                aux3.addComponent(new JBlank(5,5));
258
                aux3.addComponent(jccFillColor = new ColorChooserPanel(true,true));
259
                jccFillColor.setAlpha(255);
260
261
                aux3.addComponent(new JBlank(5,5));
262
                aux3.addComponent(new JBlank(5,5));
263
                aux2.add(aux3);
264
265
                aux.addComponent(aux2);
266
                aux.addComponent(new JBlank(10, 10));
267
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
268
                aux2.add(btnOutline = new JSymbolPreviewButton(Geometry.TYPES.CURVE));
269
                useBorder = new JCheckBox(Messages.getText("use_outline"));
270
                aux.addComponent(useBorder);
271
                aux.addComponent(Messages.getText("outline")+":",
272
                                aux2);
273
274
                fillProperties = new MarkerFillProperties();
275
                myTab.add(aux);
276
277
                fillProperties.addActionListener(this);
278
                incrAngle.addActionListener(this);
279
                incrScaleX.addActionListener(this);
280
                incrScaleY.addActionListener(this);
281
                jccFillColor.addActionListener(this);
282
                btnOutline.addActionListener(this);
283
                useBorder.addActionListener(this);
284
285
                tabs.add(myTab);
286
                tabs.add(fillProperties);
287
288
                enableControls(false);
289
290
        }
291
292
        public EditorTool[] getEditorTools() {
293
                // TODO Auto-generated method stub
294
                throw new Error("Not yet implemented!");
295
        }
296
297
        public ISymbol getLayer() {
298
299
                IPictureFillSymbol sym=null;
300
                try {
301
302
                        if( lblFileName.getText().equals("") )
303
                                sym=null;
304
305
                        else {
306
                                sym =  SymbologyLocator.getSymbologyManager().createPictureFillSymbol(new File(lblFileName.getText()).toURI().toURL(),null);
307
                                if (!lblSelFileName.getText().equals("")){
308
                                        sym = SymbologyLocator.getSymbologyManager().createPictureFillSymbol(new File(lblFileName.getText()).toURI().toURL(),new File(lblSelFileName.getText()).toURI().toURL());
309
                                }
310
                                sym.setHasFill(jccFillColor.getUseColorisSelected());
311
                                Color c = jccFillColor.getColor();
312
                                if (c != null)
313
                                        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
314
                                sym.setFillColor(c);
315
316
                                sym.setHasOutline(useBorder.isSelected());
317
                                outline = (ILineSymbol) btnOutline.getSymbol();
318
                                sym.setOutline(outline);
319
320
                                sym.setAngle(incrAngle.getDouble()*DEGREE_TO_RADIANS);
321
                                sym.setXScale(incrScaleX.getDouble());
322
                                sym.setYScale(incrScaleY.getDouble());
323
                                sym.setMarkerFillProperties(fillProperties.getMarkerFillProperties());
324
                        }
325
326
                } catch (IOException e) {
327
                        IWarningSymbol warning =
328
                                (IWarningSymbol) MapContextLocator.getSymbolManager()
329
                                .getWarningSymbol(
330
                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
331
                                                Messages.getText("failed_acessing_files"),
332
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
333
                                                return warning;
334
                }
335
336
337
                return sym;
338
339
340
        }
341
342
        public String getName() {
343
                return Messages.getText("picture_fill_symbol");
344
        }
345
346
        public JPanel[] getTabs() {
347
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
348
        }
349
350
        public void refreshControls(ISymbol layer) {
351
                if (layer instanceof IPictureFillSymbol){
352
                        IPictureFillSymbol sym = (IPictureFillSymbol) layer;
353
354
                        URL source = sym.getSource();
355
                        if (source != null) {
356
                                try {
357
                                        lblFileName.setText(source.toURI().getPath());
358
                                } catch (URISyntaxException e) {
359
                                        NotificationManager.addWarning("URI Syntax error", e);                                }
360
                        } else {
361
                                lblFileName.setText("");
362
                        }
363
                        URL selSource = sym.getSelectedSource();
364
                        if (selSource != null) {
365
                                try {
366
                                        lblSelFileName.setText(selSource.toURI().getPath());
367
                                } catch (URISyntaxException e) {
368
                                        NotificationManager.addWarning("URI Syntax error", e);
369
                                }
370
                        } else {
371
                                lblSelFileName.setText("");
372
                        }
373 42001 fdiaz
374 40435 jjdelcerro
                        jccFillColor.setUseColorIsSelected(sym.hasFill());
375
                        jccFillColor.setColor(sym.getFillColor());
376
377
                        outline=sym.getOutline();
378
                        btnOutline.setSymbol(outline);
379
                        useBorder.setSelected(sym.hasOutline());
380
381
                        incrAngle.setDouble(sym.getAngle()/DEGREE_TO_RADIANS);
382
                        incrScaleX.setDouble(sym.getXScale());
383
                        incrScaleY.setDouble(sym.getYScale());
384
                        fillProperties.setModel(sym.getMarkerFillProperties());
385
386
                        enableControls(lblFileName.getText()!="");
387
                }
388
        }
389
390
        private void enableControls(boolean enabled){
391
                btnBrowseFileSelected.setEnabled(enabled);
392
                incrAngle.setEnabled(enabled);
393
                incrScaleX.setEnabled(enabled);
394
                incrScaleY.setEnabled(enabled);
395
                incrAngle.setEnabled(enabled);
396
                incrScaleX.setEnabled(enabled);
397
                incrScaleY.setEnabled(enabled);
398
                jccFillColor.setEnabled(enabled);
399
                btnOutline.setEnabled(enabled);
400
                useBorder.setEnabled(enabled);
401
402
                fillProperties.setEnabled(enabled);
403
        }
404
405
        public void actionPerformed(ActionEvent e) {
406
                Object s = e.getSource();
407
408
                if(s.equals(jccFillColor)) {
409
                        jccFillColor.getColor().getAlpha();
410
                }
411
                outline = (ILineSymbol) btnOutline.getSymbol();
412
                fireSymbolChangedEvent();
413
        }
414
415
        public boolean canManageSymbol(ISymbol symbol) {
416
                return symbol instanceof IPictureFillSymbol;
417
        }
418
419
}