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 @ 40560

History | View | Annotate | Download (13.2 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.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.PluginServices;
44
import org.gvsig.andami.messages.NotificationManager;
45
import org.gvsig.app.gui.panels.ColorChooserPanel;
46
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.mapcontext.MapContextLocator;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
51
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
52
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
53
import org.gvsig.gui.beans.swing.JBlank;
54
import org.gvsig.gui.beans.swing.JButton;
55
import org.gvsig.gui.beans.swing.JFileChooser;
56
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
57
import org.gvsig.i18n.Messages;
58
import org.gvsig.symbology.SymbologyLocator;
59
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IPictureFillSymbol;
60
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
61

    
62
/**
63
 * <b>PictureFill</b> allows to store and modify the properties that fills a
64
 * polygon with a padding and an outline<p>
65
 * <p>
66
 * This functionality is carried out thanks to two tabs (picture fill and MarkerFillProperties)
67
 * which are included in the panel to edit the properities of a symbol (SymbolEditor)
68
 * how is explained in AbstractTypeSymbolEditor.<p>
69
 * <p>
70
 * The first tab (picture fill)permits the user to select the picture for the padding and
71
 * differentes options to modify it such as the angle(<b>incrAngle</b>) and the scale
72
 * (<b>incrScaleX,incrScaleY</b>). Also, there is an option to select a color for the
73
 * fill (<b>jccFillColor</b>).
74
 * <p>
75
 * The second tab is implementes as a MarkerFillProperties class and offers the possibilities
76
 * to change the separtion and the offset.
77
 *
78
 *
79
 *@see MarkerFillProperties
80
 *@see AbstractTypeSymbolEditor
81
 *@author jaume dominguez faus - jaume.dominguez@iver.es
82
 */
83
public class PictureFill extends AbstractTypeSymbolEditor implements
84
ActionListener {
85

    
86
        private JLabel lblFileName;
87
        private JLabel lblSelFileName;
88
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
89
        private MarkerFillProperties fillProperties;
90
        private File picFile;
91
        private JIncrementalNumberField incrAngle;
92
        private JIncrementalNumberField incrScaleX;
93
        private JIncrementalNumberField incrScaleY;
94
        private ColorChooserPanel jccFillColor;
95
        private ILineSymbol outline;
96
        private JSymbolPreviewButton btnOutline;
97
        private JCheckBox useBorder;
98

    
99
        private JButton btnBrowseFile;
100
    private JButton btnBrowseFileSelected;
101

    
102
        private static final double DEGREE_TO_RADIANS = Math.PI/180D;
103

    
104
        private ActionListener chooseAction = new ActionListener() {
105
                public void actionPerformed(ActionEvent e) {
106
                        boolean isSelection;
107
                        JLabel targetLbl;
108

    
109
                        if (e.getSource().equals(btnBrowseFile)) {
110
                                targetLbl = lblFileName;
111
                                isSelection = false;
112
                        } else {
113
                                targetLbl = lblSelFileName;
114
                                isSelection = true;
115
                        }
116

    
117
                        FileFilter ff = new FileFilter() {
118
                                public boolean accept(File f) {
119
                                        if (f.isDirectory()) return true;
120
                                        String fName = f.getAbsolutePath();
121
                                        if (fName!=null) {
122
                                                fName = fName.toLowerCase();
123
                                                return fName.endsWith(".png")
124
                                                || fName.endsWith(".gif")
125
                                                || fName.endsWith(".jpg")
126
                                                || fName.endsWith(".jpeg")
127
                                                || fName.endsWith(".bmp")
128
                                                || fName.endsWith(".svg");
129
                                        }
130
                                        return false;
131
                                }
132

    
133
                                public String getDescription() {
134
                                        return Messages.getText("bitmap_and_svg_image_files");
135
                                }
136
                        };
137
                        JUrlFileChooser jfc = new JUrlFileChooser(getName(), null);
138
                        jfc.setFileFilter(ff);
139
                        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
140
                        jfc.setSelectedFile(picFile);
141
                        jfc.setMultiSelectionEnabled(false);
142
                        int returnVal = jfc.showOpenDialog(PictureFill.this.owner);
143
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
144
//                                File myFile = jfc.getSelectedFile();
145
//                                lastDir = jfc.getCurrentDirectory();
146
//                                if (myFile != null && myFile.exists()) {
147
//                                        if(!isSelection){
148
//                                                picFile = myFile;
149
//                                        }
150
//                                        else{
151
//                                                selPicFile = myFile;
152
//                                        }
153
//                                        try {
154
//                                                targetLbl.setText(myFile.toURL().toString());
155
//                                        } catch (MalformedURLException e1) {
156
//                                                NotificationManager.addError(PluginServices.getText(this, "invalid_url"), e1);
157
//                                        }
158
//                                        fireSymbolChangedEvent();
159
//                                }
160
//                        }
161
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
162

    
163
                                URL url = jfc.getSelectedURL();
164
                                if (url == null) return;
165
                                try {
166
                                        targetLbl.setText(url.toURI().getPath());
167
                                } catch (URISyntaxException e1) {
168
                                        NotificationManager.addWarning("URI Syntax error", e1);
169
                                }
170
                                fireSymbolChangedEvent();
171
                        }
172
                        boolean enabled = (lblFileName.getText()!="");
173
                        enableControls(lblFileName.getText()!="");
174

    
175
                }
176
        };
177

    
178
        /**
179
         * Constructor method
180
         * @param owner
181
         */
182
        public PictureFill(SymbolEditor owner) {
183
                super(owner);
184
                initialize();
185
        }
186

    
187
        /**
188
         * Initializes the parameters that allows the user to fill the padding of
189
         * a polygon with a picture style.To do it, two tabs are created (picture fill and
190
         * MarkerFillProperties)inside the SymbolEditor panel with default values for the
191
     * different attributes.
192
         */
193
        private void initialize() {
194
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
195
                myTab.setName(Messages.getText("picture_fill"));
196

    
197
                btnBrowseFile = new JButton(Messages.getText("browse"));
198
                btnBrowseFile.addActionListener(chooseAction);
199

    
200
                btnBrowseFileSelected = new JButton(Messages.getText("browse"));
201
                btnBrowseFileSelected.addActionListener(chooseAction);
202

    
203
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
204

    
205
                JPanel auxLabelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
206
                JLabel lblName = new JLabel();
207
                lblName.setFont(lblName.getFont().deriveFont(Font.BOLD));
208
                lblName.setText(Messages.getText("picture_file")+":");
209
                auxLabelPic.add(lblName);
210

    
211
                aux2.add(btnBrowseFile);
212
                aux2.add(lblFileName = new JLabel(""));
213

    
214
                JPanel auxLabelSelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
215
                JLabel lblSelName = new JLabel();
216
                lblSelName.setFont(lblSelName.getFont().deriveFont(Font.BOLD));
217
                lblSelName.setText(Messages.getText("selection_picture_file")+":");
218
                auxLabelSelPic.add(lblSelName);
219

    
220
                JPanel aux4 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
221
                aux4.add(btnBrowseFileSelected);
222
                aux4.add(lblSelFileName = new JLabel(""));
223

    
224
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
225
                aux.addComponent(new JBlank(5, 5));
226
                aux.addComponent(auxLabelPic);
227
                aux.addComponent(aux2);
228
                aux.addComponent(auxLabelSelPic);
229
                aux.addComponent(aux4);
230

    
231

    
232
                aux2 = new JPanel(new GridLayout(1, 2, 20, 5));
233
                GridBagLayoutPanel aux3;
234
                aux3 = new GridBagLayoutPanel();
235
                aux3.addComponent(Messages.getText("angle")+":",
236
                                incrAngle = new JIncrementalNumberField("0", 20));
237
                aux3.addComponent(Messages.getText("scale")+"X:",
238
                                incrScaleX = new JIncrementalNumberField(
239
                                                "1",
240
                                                20,
241
                                                0.01,
242
                                                Double.POSITIVE_INFINITY,
243
                                                0.1));
244
                incrScaleX.setDouble(1);
245
                aux3.addComponent(Messages.getText("scale")+"Y:",
246
                                incrScaleY = new JIncrementalNumberField(
247
                                                "1",
248
                                                20,
249
                                                0.01,
250
                                                Double.POSITIVE_INFINITY,
251
                                                0.1));
252
                incrScaleY.setDouble(1);
253
                aux2.add(aux3);
254

    
255
                aux3 = new GridBagLayoutPanel();
256
                aux3.addComponent(new JBlank(5,5));
257
                aux3.addComponent(new JLabel (Messages.getText("fill_color")+":"));
258
                aux3.addComponent(new JBlank(5,5));
259
                aux3.addComponent(jccFillColor = new ColorChooserPanel(true,true));
260
                jccFillColor.setAlpha(255);
261

    
262
                aux3.addComponent(new JBlank(5,5));
263
                aux3.addComponent(new JBlank(5,5));
264
                aux2.add(aux3);
265

    
266
                aux.addComponent(aux2);
267
                aux.addComponent(new JBlank(10, 10));
268
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
269
                aux2.add(btnOutline = new JSymbolPreviewButton(Geometry.TYPES.CURVE));
270
                useBorder = new JCheckBox(Messages.getText("use_outline"));
271
                aux.addComponent(useBorder);
272
                aux.addComponent(Messages.getText("outline")+":",
273
                                aux2);
274

    
275
                fillProperties = new MarkerFillProperties();
276
                myTab.add(aux);
277

    
278
                fillProperties.addActionListener(this);
279
                incrAngle.addActionListener(this);
280
                incrScaleX.addActionListener(this);
281
                incrScaleY.addActionListener(this);
282
                jccFillColor.addActionListener(this);
283
                btnOutline.addActionListener(this);
284
                useBorder.addActionListener(this);
285

    
286
                tabs.add(myTab);
287
                tabs.add(fillProperties);
288

    
289
                enableControls(false);
290

    
291
        }
292

    
293
        public EditorTool[] getEditorTools() {
294
                // TODO Auto-generated method stub
295
                throw new Error("Not yet implemented!");
296
        }
297

    
298
        public ISymbol getLayer() {
299

    
300
                IPictureFillSymbol sym=null;
301
                try {
302

    
303
                        if( lblFileName.getText().equals("") )
304
                                sym=null;
305

    
306
                        else {
307
                                sym =  SymbologyLocator.getSymbologyManager().createPictureFillSymbol(new File(lblFileName.getText()).toURI().toURL(),null);
308
                                if (!lblSelFileName.getText().equals("")){
309
                                        sym = SymbologyLocator.getSymbologyManager().createPictureFillSymbol(new File(lblFileName.getText()).toURI().toURL(),new File(lblSelFileName.getText()).toURI().toURL());
310
                                }
311
                                sym.setHasFill(jccFillColor.getUseColorisSelected());
312
                                Color c = jccFillColor.getColor();
313
                                if (c != null)
314
                                        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
315
                                sym.setFillColor(c);
316

    
317
                                sym.setHasOutline(useBorder.isSelected());
318
                                outline = (ILineSymbol) btnOutline.getSymbol();
319
                                sym.setOutline(outline);
320

    
321
                                sym.setAngle(incrAngle.getDouble()*DEGREE_TO_RADIANS);
322
                                sym.setXScale(incrScaleX.getDouble());
323
                                sym.setYScale(incrScaleY.getDouble());
324
                                sym.setMarkerFillProperties(fillProperties.getMarkerFillProperties());
325
                        }
326

    
327
                } catch (IOException e) {
328
                        IWarningSymbol warning =
329
                                (IWarningSymbol) MapContextLocator.getSymbolManager()
330
                                .getWarningSymbol(
331
                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
332
                                                Messages.getText("failed_acessing_files"),
333
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
334
                                                return warning;
335
                }
336

    
337

    
338
                return sym;
339

    
340

    
341
        }
342

    
343
        public String getName() {
344
                return Messages.getText("picture_fill_symbol");
345
        }
346

    
347
        public JPanel[] getTabs() {
348
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
349
        }
350

    
351
        public void refreshControls(ISymbol layer) {
352
                if (layer instanceof IPictureFillSymbol){
353
                        IPictureFillSymbol sym = (IPictureFillSymbol) layer;
354

    
355
                        URL source = sym.getSource();
356
                        if (source != null) {
357
                                try {
358
                                        lblFileName.setText(source.toURI().getPath());
359
                                } catch (URISyntaxException e) {
360
                                        NotificationManager.addWarning("URI Syntax error", e);                                }
361
                        } else {
362
                                lblFileName.setText("");
363
                        }
364
                        URL selSource = sym.getSelectedSource();
365
                        if (selSource != null) {
366
                                try {
367
                                        lblSelFileName.setText(selSource.toURI().getPath());
368
                                } catch (URISyntaxException e) {
369
                                        NotificationManager.addWarning("URI Syntax error", e);
370
                                }
371
                        } else {
372
                                lblSelFileName.setText("");
373
                        }
374
                        
375
                        jccFillColor.setUseColorIsSelected(sym.hasFill());
376
                        jccFillColor.setColor(sym.getFillColor());
377

    
378
                        outline=sym.getOutline();
379
                        btnOutline.setSymbol(outline);
380
                        useBorder.setSelected(sym.hasOutline());
381

    
382
                        incrAngle.setDouble(sym.getAngle()/DEGREE_TO_RADIANS);
383
                        incrScaleX.setDouble(sym.getXScale());
384
                        incrScaleY.setDouble(sym.getYScale());
385
                        fillProperties.setModel(sym.getMarkerFillProperties());
386

    
387
                        enableControls(lblFileName.getText()!="");
388
                }
389
        }
390

    
391
        private void enableControls(boolean enabled){
392
                btnBrowseFileSelected.setEnabled(enabled);
393
                incrAngle.setEnabled(enabled);
394
                incrScaleX.setEnabled(enabled);
395
                incrScaleY.setEnabled(enabled);
396
                incrAngle.setEnabled(enabled);
397
                incrScaleX.setEnabled(enabled);
398
                incrScaleY.setEnabled(enabled);
399
                jccFillColor.setEnabled(enabled);
400
                btnOutline.setEnabled(enabled);
401
                useBorder.setEnabled(enabled);
402

    
403
                fillProperties.setEnabled(enabled);
404
        }
405

    
406
        public void actionPerformed(ActionEvent e) {
407
                Object s = e.getSource();
408

    
409
                if(s.equals(jccFillColor)) {
410
                        jccFillColor.getColor().getAlpha();
411
                }
412
                outline = (ILineSymbol) btnOutline.getSymbol();
413
                fireSymbolChangedEvent();
414
        }
415

    
416
        @Override
417
        public boolean canManageSymbol(ISymbol symbol) {
418
                return symbol instanceof IPictureFillSymbol;
419
        }
420

    
421
}