Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / PictureFill.java @ 20905

History | View | Annotate | Download (12.8 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.symbology.gui.styling;
42

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

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

    
61
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
62
import org.gvsig.gui.beans.swing.JBlank;
63
import org.gvsig.gui.beans.swing.JButton;
64
import org.gvsig.gui.beans.swing.JFileChooser;
65
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
66
import org.gvsig.symbology.fmap.symbols.PictureFillSymbol;
67

    
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.cit.gvsig.fmap.core.FShape;
71
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
72
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
73
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
74
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
75
import com.iver.cit.gvsig.gui.styling.AbstractTypeSymbolEditor;
76
import com.iver.cit.gvsig.gui.styling.EditorTool;
77
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
78
import com.iver.cit.gvsig.project.documents.view.legend.gui.JSymbolPreviewButton;
79

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

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

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

    
124
                        if (e.getSource().equals(btnBrowseFile)) {
125
                                targetLbl = lblFileName;
126
                                isSelection = false;
127
                        } else {
128
                                targetLbl = lblSelFileName;
129
                                isSelection = true;
130
                        }
131
                        
132
                        FileFilter ff = new FileFilter() {
133
                                public boolean accept(File f) {
134
                                        if (f.isDirectory()) return true;
135
                                        String fName = f.getAbsolutePath();
136
                                        if (fName!=null) {
137
                                                fName = fName.toLowerCase();
138
                                                return fName.endsWith(".png")
139
                                                || fName.endsWith(".gif")
140
                                                || fName.endsWith(".jpg")
141
                                                || fName.endsWith(".jpeg")
142
                                                || fName.endsWith(".bmp")
143
                                                || fName.endsWith(".svg");
144
                                        }
145
                                        return false;
146
                                }
147

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

    
178
                                URL url = jfc.getSelectedURL();
179
                                if (url == null) return; 
180
                                lastDir = jfc.getCurrentDirectory();
181
                                targetLbl.setText(url.toString());
182
                                fireSymbolChangedEvent();
183
                        }
184
                }
185

    
186
        };
187

    
188
        private JButton btnBrowseFile;
189
    private JButton btnBrowseFileSelected; 
190
    
191
        /**
192
         * Constructor method
193
         * @param owner
194
         */
195
        public PictureFill(SymbolEditor owner) {
196
                super(owner);
197
                initialize();
198
        }
199

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

    
210
                btnBrowseFile = new JButton(PluginServices.getText(this, "browse"));
211
                btnBrowseFile.addActionListener(chooseAction);
212

    
213
                btnBrowseFileSelected = new JButton(PluginServices.getText(this,"browse")); 
214
                btnBrowseFileSelected.addActionListener(chooseAction);
215

    
216
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
217

    
218
                JPanel auxLabelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
219
                JLabel lblName = new JLabel();
220
                lblName.setFont(lblName.getFont().deriveFont(Font.BOLD));
221
                lblName.setText(PluginServices.getText(this, "picture_file")+":");
222
                auxLabelPic.add(lblName);
223

    
224
                aux2.add(btnBrowseFile);
225
                aux2.add(lblFileName = new JLabel(""));
226

    
227
                JPanel auxLabelSelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
228
                JLabel lblSelName = new JLabel();
229
                lblSelName.setFont(lblSelName.getFont().deriveFont(Font.BOLD));
230
                lblSelName.setText(PluginServices.getText(this, "selection_picture_file")+":");
231
                auxLabelSelPic.add(lblSelName);
232

    
233
                JPanel aux4 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
234
                aux4.add(btnBrowseFileSelected);
235
                aux4.add(lblSelFileName = new JLabel(""));
236

    
237
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
238
                aux.addComponent(new JBlank(5, 5));
239
                aux.addComponent(auxLabelPic);
240
                aux.addComponent(aux2);
241
                aux.addComponent(auxLabelSelPic);
242
                aux.addComponent(aux4);
243

    
244

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

    
268
                aux3 = new GridBagLayoutPanel();
269
                aux3.addComponent(new JBlank(5,5));
270
                aux3.addComponent(new JLabel (PluginServices.getText(this, "fill_color")+":"));
271
                aux3.addComponent(new JBlank(5,5));
272
                aux3.addComponent(jccFillColor = new ColorChooserPanel(true,true));
273
                jccFillColor.setAlpha(255);
274

    
275
                aux3.addComponent(new JBlank(5,5));
276
                aux3.addComponent(new JBlank(5,5));
277
                aux2.add(aux3);
278

    
279
                aux.addComponent(aux2);
280
                aux.addComponent(new JBlank(10, 10));
281
                aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
282
                aux2.add(btnOutline = new JSymbolPreviewButton(FShape.LINE));
283
                useBorder = new JCheckBox(PluginServices.getText(this, "use_outline"));
284
                aux.addComponent(useBorder);
285
                aux.addComponent(PluginServices.getText(this, "outline")+":",
286
                                aux2);
287

    
288
                fillProperties = new MarkerFillProperties();
289
                myTab.add(aux);
290

    
291
                fillProperties.addActionListener(this);
292
                incrAngle.addActionListener(this);
293
                incrScaleX.addActionListener(this);
294
                incrScaleY.addActionListener(this);
295
                jccFillColor.addActionListener(this);
296
                btnOutline.addActionListener(this);
297
                useBorder.addActionListener(this);
298
                
299
                tabs.add(myTab);
300
                tabs.add(fillProperties);
301
                
302
        }
303

    
304
        public EditorTool[] getEditorTools() {
305
                // TODO Auto-generated method stub
306
                throw new Error("Not yet implemented!");
307
        }
308

    
309
        public ISymbol getLayer() {
310
                File imageFile = new File(lblFileName.getText());
311
                File imageSelFile =  new File(lblSelFileName.getText());
312

    
313

    
314

    
315
                PictureFillSymbol sym=null;
316
                try {
317

    
318
                        if( lblFileName.getText().equals("") )
319
                                sym=null;
320

    
321
                        else {
322
                                sym =  new PictureFillSymbol(new URL(lblFileName.getText()),null);
323
                                if (!lblSelFileName.getText().equals(""))
324
                                        sym = new PictureFillSymbol(new URL(lblFileName.getText()),new URL(lblSelFileName.getText()));
325

    
326
                                sym.setHasFill(jccFillColor.getUseColorisSelected());
327
                                Color c = jccFillColor.getColor();
328
                                if (c != null)
329
                                        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
330
                                sym.setFillColor(c);
331

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

    
336
                                sym.setAngle(incrAngle.getDouble()*FConstant.DEGREE_TO_RADIANS);
337
                                sym.setXScale(incrScaleX.getDouble());
338
                                sym.setYScale(incrScaleY.getDouble());
339
                                sym.setMarkerFillProperties(fillProperties.getMarkerFillProperties());
340
                        }
341
                        
342
                } catch (IOException e) {
343
                        // TODO Auto-generated catch block
344
                        NotificationManager.addError(PluginServices.getText(this, "could_not_open_picture_file"), e);
345
                        return null;
346
                }
347

    
348
                return sym;
349

    
350

    
351
        }
352

    
353
        private void setControlsEnabled(boolean enabled) {
354
                for (int i = 0; i < tabs.size(); i++) {
355
                        setOptionsEnabled(tabs.get(i), enabled);
356
                }
357
        }
358
        
359
        
360

    
361
        private void setOptionsEnabled(Component c, boolean b) {
362
                if (!(c.equals(btnBrowseFile) || tabs.contains(c)))
363
                        c.setEnabled(b);
364
                
365
                System.out.println(c.getClass().getName()+", "+c.getName());
366
                if (c instanceof Container) {
367
                        Container cont = (Container) c;
368
                        for(int j = 0; j < cont.getComponentCount(); j++) {
369
                                setOptionsEnabled(cont.getComponent(j), b);
370
                        }        
371
                }
372
                
373
        }
374
        
375
        
376

    
377
        public String getName() {
378
                return PluginServices.getText(this, "picture_fill_symbol");
379
        }
380

    
381
        public Class getSymbolClass() {
382
                return PictureFillSymbol.class;
383
        }
384

    
385
        public JPanel[] getTabs() {
386
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
387
        }
388

    
389
        public void refreshControls(ISymbol layer) {
390
                PictureFillSymbol sym = (PictureFillSymbol) layer;
391
                
392
                File imageFile = new File(sym.getImagePath());
393
                File selImageFile =  new File (sym.getSelImagePath());
394

    
395
//                boolean enabled = imageFile.exists();
396
//                setControlsEnabled(enabled);
397

    
398
                lblFileName.setText(sym.getImagePath());
399
                lblSelFileName.setText(sym.getSelImagePath());
400
                jccFillColor.setUseColorIsSelected(sym.hasFill());
401
                jccFillColor.setColor(sym.getFillColor());
402

    
403
                outline=sym.getOutline();
404
                btnOutline.setSymbol(outline);
405
                useBorder.setSelected(sym.hasOutline());
406

    
407
                incrAngle.setDouble(sym.getAngle()/FConstant.DEGREE_TO_RADIANS);
408
                incrScaleX.setDouble(sym.getXScale());
409
                incrScaleY.setDouble(sym.getYScale());
410
                fillProperties.setModel(sym.getMarkerFillProperties());
411

    
412
                
413
                
414
        }
415

    
416
        public void actionPerformed(ActionEvent e) {
417
                Object s = e.getSource();
418

    
419
                if(s.equals(jccFillColor)) {
420
                        jccFillColor.getColor().getAlpha(); 
421
                }
422
                outline = (ILineSymbol) btnOutline.getSymbol();
423
                fireSymbolChangedEvent();
424
        }
425

    
426
}