Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / PictureFill.java @ 34294

History | View | Annotate | Download (13.4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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.app.gui.styling;
42

    
43
import java.awt.Color;
44
import java.awt.FlowLayout;
45
import java.awt.Font;
46
import java.awt.GridLayout;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.io.File;
50
import java.io.IOException;
51
import java.net.URISyntaxException;
52
import java.net.URL;
53
import java.util.ArrayList;
54

    
55
import javax.swing.JCheckBox;
56
import javax.swing.JLabel;
57
import javax.swing.JPanel;
58
import javax.swing.filechooser.FileFilter;
59

    
60
import org.gvsig.andami.PluginServices;
61
import org.gvsig.andami.messages.NotificationManager;
62
import org.gvsig.app.gui.panels.ColorChooserPanel;
63
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton;
64
import org.gvsig.fmap.geom.Geometry;
65
import org.gvsig.fmap.mapcontext.MapContextLocator;
66
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
67
import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol;
68
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
69
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
70
import org.gvsig.gui.beans.swing.JBlank;
71
import org.gvsig.gui.beans.swing.JButton;
72
import org.gvsig.gui.beans.swing.JFileChooser;
73
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
74
import org.gvsig.i18n.Messages;
75
import org.gvsig.symbology.SymbologyLocator;
76
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IPictureFillSymbol;
77
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
78

    
79
/**
80
 * <b>PictureFill</b> allows to store and modify the properties that fills a
81
 * polygon with a padding and an outline<p>
82
 * <p>
83
 * This functionality is carried out thanks to two tabs (picture fill and MarkerFillProperties)
84
 * which are included in the panel to edit the properities of a symbol (SymbolEditor)
85
 * how is explained in AbstractTypeSymbolEditor.<p>
86
 * <p>
87
 * The first tab (picture fill)permits the user to select the picture for the padding and
88
 * differentes options to modify it such as the angle(<b>incrAngle</b>) and the scale
89
 * (<b>incrScaleX,incrScaleY</b>). Also, there is an option to select a color for the
90
 * fill (<b>jccFillColor</b>).
91
 * <p>
92
 * The second tab is implementes as a MarkerFillProperties class and offers the possibilities
93
 * to change the separtion and the offset.
94
 *
95
 *
96
 *@see MarkerFillProperties
97
 *@see AbstractTypeSymbolEditor
98
 *@author jaume dominguez faus - jaume.dominguez@iver.es
99
 */
100
public class PictureFill extends AbstractTypeSymbolEditor implements
101
ActionListener {
102

    
103
        private JLabel lblFileName;
104
        private JLabel lblSelFileName;
105
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
106
        private MarkerFillProperties fillProperties;
107
        private File picFile;
108
        private JIncrementalNumberField incrAngle;
109
        private JIncrementalNumberField incrScaleX;
110
        private JIncrementalNumberField incrScaleY;
111
        private ColorChooserPanel jccFillColor;
112
        private ILineSymbol outline;
113
        private JSymbolPreviewButton btnOutline;
114
        private JCheckBox useBorder;
115

    
116
        private JButton btnBrowseFile;
117
    private JButton btnBrowseFileSelected;
118

    
119
        private static final double DEGREE_TO_RADIANS = Math.PI/180D;
120

    
121
        private ActionListener chooseAction = new ActionListener() {
122
                public void actionPerformed(ActionEvent e) {
123
                        boolean isSelection;
124
                        JLabel targetLbl;
125

    
126
                        if (e.getSource().equals(btnBrowseFile)) {
127
                                targetLbl = lblFileName;
128
                                isSelection = false;
129
                        } else {
130
                                targetLbl = lblSelFileName;
131
                                isSelection = true;
132
                        }
133

    
134
                        FileFilter ff = new FileFilter() {
135
                                public boolean accept(File f) {
136
                                        if (f.isDirectory()) return true;
137
                                        String fName = f.getAbsolutePath();
138
                                        if (fName!=null) {
139
                                                fName = fName.toLowerCase();
140
                                                return fName.endsWith(".png")
141
                                                || fName.endsWith(".gif")
142
                                                || fName.endsWith(".jpg")
143
                                                || fName.endsWith(".jpeg")
144
                                                || fName.endsWith(".bmp")
145
                                                || fName.endsWith(".svg");
146
                                        }
147
                                        return false;
148
                                }
149

    
150
                                public String getDescription() {
151
                                        return Messages.getText("bitmap_and_svg_image_files");
152
                                }
153
                        };
154
                        JUrlFileChooser jfc = new JUrlFileChooser(getName(), null);
155
                        jfc.setFileFilter(ff);
156
                        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
157
                        jfc.setSelectedFile(picFile);
158
                        jfc.setMultiSelectionEnabled(false);
159
                        int returnVal = jfc.showOpenDialog(PictureFill.this.owner);
160
//                        if(returnVal == JFileChooser.APPROVE_OPTION) {
161
//                                File myFile = jfc.getSelectedFile();
162
//                                lastDir = jfc.getCurrentDirectory();
163
//                                if (myFile != null && myFile.exists()) {
164
//                                        if(!isSelection){
165
//                                                picFile = myFile;
166
//                                        }
167
//                                        else{
168
//                                                selPicFile = myFile;
169
//                                        }
170
//                                        try {
171
//                                                targetLbl.setText(myFile.toURL().toString());
172
//                                        } catch (MalformedURLException e1) {
173
//                                                NotificationManager.addError(PluginServices.getText(this, "invalid_url"), e1);
174
//                                        }
175
//                                        fireSymbolChangedEvent();
176
//                                }
177
//                        }
178
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
179

    
180
                                URL url = jfc.getSelectedURL();
181
                                if (url == null) return;
182
                                try {
183
                                        targetLbl.setText(url.toURI().getPath());
184
                                } catch (URISyntaxException e1) {
185
                                        NotificationManager.addWarning("URI Syntax error", e1);
186
                                }
187
                                fireSymbolChangedEvent();
188
                        }
189
                        boolean enabled = (lblFileName.getText()!="");
190
                        enableControls(lblFileName.getText()!="");
191

    
192
                }
193
        };
194

    
195
        /**
196
         * Constructor method
197
         * @param owner
198
         */
199
        public PictureFill(SymbolEditor owner) {
200
                super(owner);
201
                initialize();
202
        }
203

    
204
        /**
205
         * Initializes the parameters that allows the user to fill the padding of
206
         * a polygon with a picture style.To do it, two tabs are created (picture fill and
207
         * MarkerFillProperties)inside the SymbolEditor panel with default values for the
208
     * different attributes.
209
         */
210
        private void initialize() {
211
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
212
                myTab.setName(Messages.getText("picture_fill"));
213

    
214
                btnBrowseFile = new JButton(Messages.getText("browse"));
215
                btnBrowseFile.addActionListener(chooseAction);
216

    
217
                btnBrowseFileSelected = new JButton(Messages.getText("browse"));
218
                btnBrowseFileSelected.addActionListener(chooseAction);
219

    
220
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
221

    
222
                JPanel auxLabelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
223
                JLabel lblName = new JLabel();
224
                lblName.setFont(lblName.getFont().deriveFont(Font.BOLD));
225
                lblName.setText(Messages.getText("picture_file")+":");
226
                auxLabelPic.add(lblName);
227

    
228
                aux2.add(btnBrowseFile);
229
                aux2.add(lblFileName = new JLabel(""));
230

    
231
                JPanel auxLabelSelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
232
                JLabel lblSelName = new JLabel();
233
                lblSelName.setFont(lblSelName.getFont().deriveFont(Font.BOLD));
234
                lblSelName.setText(Messages.getText("selection_picture_file")+":");
235
                auxLabelSelPic.add(lblSelName);
236

    
237
                JPanel aux4 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
238
                aux4.add(btnBrowseFileSelected);
239
                aux4.add(lblSelFileName = new JLabel(""));
240

    
241
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
242
                aux.addComponent(new JBlank(5, 5));
243
                aux.addComponent(auxLabelPic);
244
                aux.addComponent(aux2);
245
                aux.addComponent(auxLabelSelPic);
246
                aux.addComponent(aux4);
247

    
248

    
249
                aux2 = new JPanel(new GridLayout(1, 2, 20, 5));
250
                GridBagLayoutPanel aux3;
251
                aux3 = new GridBagLayoutPanel();
252
                aux3.addComponent(Messages.getText("angle")+":",
253
                                incrAngle = new JIncrementalNumberField("0", 20));
254
                aux3.addComponent(Messages.getText("scale")+"X:",
255
                                incrScaleX = new JIncrementalNumberField(
256
                                                "1",
257
                                                20,
258
                                                0.01,
259
                                                Double.POSITIVE_INFINITY,
260
                                                0.1));
261
                incrScaleX.setDouble(1);
262
                aux3.addComponent(Messages.getText("scale")+"Y:",
263
                                incrScaleY = new JIncrementalNumberField(
264
                                                "1",
265
                                                20,
266
                                                0.01,
267
                                                Double.POSITIVE_INFINITY,
268
                                                0.1));
269
                incrScaleY.setDouble(1);
270
                aux2.add(aux3);
271

    
272
                aux3 = new GridBagLayoutPanel();
273
                aux3.addComponent(new JBlank(5,5));
274
                aux3.addComponent(new JLabel (Messages.getText("fill_color")+":"));
275
                aux3.addComponent(new JBlank(5,5));
276
                aux3.addComponent(jccFillColor = new ColorChooserPanel(true,true));
277
                jccFillColor.setAlpha(255);
278

    
279
                aux3.addComponent(new JBlank(5,5));
280
                aux3.addComponent(new JBlank(5,5));
281
                aux2.add(aux3);
282

    
283
                aux.addComponent(aux2);
284
                aux.addComponent(new JBlank(10, 10));
285
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
286
                aux2.add(btnOutline = new JSymbolPreviewButton(Geometry.TYPES.CURVE));
287
                useBorder = new JCheckBox(Messages.getText("use_outline"));
288
                aux.addComponent(useBorder);
289
                aux.addComponent(Messages.getText("outline")+":",
290
                                aux2);
291

    
292
                fillProperties = new MarkerFillProperties();
293
                myTab.add(aux);
294

    
295
                fillProperties.addActionListener(this);
296
                incrAngle.addActionListener(this);
297
                incrScaleX.addActionListener(this);
298
                incrScaleY.addActionListener(this);
299
                jccFillColor.addActionListener(this);
300
                btnOutline.addActionListener(this);
301
                useBorder.addActionListener(this);
302

    
303
                tabs.add(myTab);
304
                tabs.add(fillProperties);
305

    
306
                enableControls(false);
307

    
308
        }
309

    
310
        public EditorTool[] getEditorTools() {
311
                // TODO Auto-generated method stub
312
                throw new Error("Not yet implemented!");
313
        }
314

    
315
        public ISymbol getLayer() {
316

    
317
                IPictureFillSymbol sym=null;
318
                try {
319

    
320
                        if( lblFileName.getText().equals("") )
321
                                sym=null;
322

    
323
                        else {
324
                                sym =  SymbologyLocator.getSymbologyManager().createPictureFillSymbol(new File(lblFileName.getText()).toURI().toURL(),null);
325
                                if (!lblSelFileName.getText().equals("")){
326
                                        sym = SymbologyLocator.getSymbologyManager().createPictureFillSymbol(new File(lblFileName.getText()).toURI().toURL(),new File(lblSelFileName.getText()).toURI().toURL());
327
                                }
328
                                sym.setHasFill(jccFillColor.getUseColorisSelected());
329
                                Color c = jccFillColor.getColor();
330
                                if (c != null)
331
                                        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
332
                                sym.setFillColor(c);
333

    
334
                                sym.setHasOutline(useBorder.isSelected());
335
                                outline = (ILineSymbol) btnOutline.getSymbol();
336
                                sym.setOutline(outline);
337

    
338
                                sym.setAngle(incrAngle.getDouble()*DEGREE_TO_RADIANS);
339
                                sym.setXScale(incrScaleX.getDouble());
340
                                sym.setYScale(incrScaleY.getDouble());
341
                                sym.setMarkerFillProperties(fillProperties.getMarkerFillProperties());
342
                        }
343

    
344
                } catch (IOException e) {
345
                        IWarningSymbol warning =
346
                                (IWarningSymbol) MapContextLocator.getSymbolManager()
347
                                .getWarningSymbol(
348
                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
349
                                                Messages.getText("failed_acessing_files"),
350
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
351
                                                return warning;
352
                }
353

    
354

    
355
                return sym;
356

    
357

    
358
        }
359

    
360
        public String getName() {
361
                return Messages.getText("picture_fill_symbol");
362
        }
363

    
364
        public JPanel[] getTabs() {
365
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
366
        }
367

    
368
        public void refreshControls(ISymbol layer) {
369
                if (layer instanceof IPictureFillSymbol){
370
                        IPictureFillSymbol sym = (IPictureFillSymbol) layer;
371

    
372
                        URL source = sym.getSource();
373
                        if (source != null) {
374
                                try {
375
                                        lblFileName.setText(source.toURI().getPath());
376
                                } catch (URISyntaxException e) {
377
                                        NotificationManager.addWarning("URI Syntax error", e);                                }
378
                        } else {
379
                                lblFileName.setText("");
380
                        }
381
                        URL selSource = sym.getSelectedSource();
382
                        if (selSource != null) {
383
                                try {
384
                                        lblSelFileName.setText(selSource.toURI().getPath());
385
                                } catch (URISyntaxException e) {
386
                                        NotificationManager.addWarning("URI Syntax error", e);
387
                                }
388
                        } else {
389
                                lblSelFileName.setText("");
390
                        }
391
                        
392
                        jccFillColor.setUseColorIsSelected(sym.hasFill());
393
                        jccFillColor.setColor(sym.getFillColor());
394

    
395
                        outline=sym.getOutline();
396
                        btnOutline.setSymbol(outline);
397
                        useBorder.setSelected(sym.hasOutline());
398

    
399
                        incrAngle.setDouble(sym.getAngle()/DEGREE_TO_RADIANS);
400
                        incrScaleX.setDouble(sym.getXScale());
401
                        incrScaleY.setDouble(sym.getYScale());
402
                        fillProperties.setModel(sym.getMarkerFillProperties());
403

    
404
                        enableControls(lblFileName.getText()!="");
405
                }
406
        }
407

    
408
        private void enableControls(boolean enabled){
409
                btnBrowseFileSelected.setEnabled(enabled);
410
                incrAngle.setEnabled(enabled);
411
                incrScaleX.setEnabled(enabled);
412
                incrScaleY.setEnabled(enabled);
413
                incrAngle.setEnabled(enabled);
414
                incrScaleX.setEnabled(enabled);
415
                incrScaleY.setEnabled(enabled);
416
                jccFillColor.setEnabled(enabled);
417
                btnOutline.setEnabled(enabled);
418
                useBorder.setEnabled(enabled);
419

    
420
                fillProperties.setEnabled(enabled);
421
        }
422

    
423
        public void actionPerformed(ActionEvent e) {
424
                Object s = e.getSource();
425

    
426
                if(s.equals(jccFillColor)) {
427
                        jccFillColor.getColor().getAlpha();
428
                }
429
                outline = (ILineSymbol) btnOutline.getSymbol();
430
                fireSymbolChangedEvent();
431
        }
432

    
433
        @Override
434
        public boolean canManageSymbol(ISymbol symbol) {
435
                return symbol instanceof IPictureFillSymbol;
436
        }
437

    
438
}