Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / gui / styling / PictureFill.java @ 40665

History | View | Annotate | Download (13.5 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.labeling.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.SymbologyFactory;
72
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
73
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
74
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
75
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
76
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
77
import com.iver.cit.gvsig.gui.styling.AbstractTypeSymbolEditor;
78
import com.iver.cit.gvsig.gui.styling.EditorTool;
79
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
80
import com.iver.cit.gvsig.project.documents.view.legend.gui.JSymbolPreviewButton;
81

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

    
106
        private JLabel lblFileName;
107
        private JLabel lblSelFileName;
108
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
109
        private MarkerFillProperties fillProperties;
110
        private File picFile;
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 JButton btnBrowseFile;
120
    private JButton btnBrowseFileSelected;
121

    
122

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

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

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

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

    
182
                                URL url = jfc.getSelectedURL();
183
                                if (url == null) return;
184
                                targetLbl.setText(url.toString());
185
                                fireSymbolChangedEvent();
186
                        }
187
                        boolean enabled = (lblFileName.getText()!="");
188
                        enableControls(lblFileName.getText()!="");
189

    
190
                }
191
        };
192

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

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

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

    
215
                btnBrowseFileSelected = new JButton(PluginServices.getText(this,"browse"));
216
                btnBrowseFileSelected.addActionListener(chooseAction);
217
//                btnBrowseFileSelected.setEnabled(false);
218

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

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

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

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

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

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

    
247

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

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

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

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

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

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

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

    
305
                enableControls(false);
306

    
307
        }
308

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

    
314
        public ISymbol getLayer() {
315
                File imageFile = new File(lblFileName.getText());
316
                File imageSelFile =  new File(lblSelFileName.getText());
317

    
318

    
319

    
320
                PictureFillSymbol sym=null;
321
                try {
322

    
323
                        if( lblFileName.getText().equals("") )
324
                                sym=null;
325

    
326
                        else {
327
                                sym =  new PictureFillSymbol(new URL(lblFileName.getText()),null);
328
                                if (!lblSelFileName.getText().equals(""))
329
                                        sym = new PictureFillSymbol(new URL(lblFileName.getText()),new URL(lblSelFileName.getText()));
330

    
331
                                sym.setHasFill(jccFillColor.getUseColorisSelected());
332
                                Color c = jccFillColor.getColor();
333
                                if (c != null)
334
                                        c = new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
335
                                sym.setFillColor(c);
336

    
337
                                sym.setHasOutline(useBorder.isSelected());
338
                                outline = (ILineSymbol) btnOutline.getSymbol();
339
                                sym.setOutline(outline);
340

    
341
                                sym.setAngle(incrAngle.getDouble()*FConstant.DEGREE_TO_RADIANS);
342
                                sym.setXScale(incrScaleX.getDouble());
343
                                sym.setYScale(incrScaleY.getDouble());
344
                                sym.setMarkerFillProperties(fillProperties.getMarkerFillProperties());
345
                        }
346

    
347
                } catch (IOException e) {
348
                        return SymbologyFactory.getWarningSymbol
349
                                (PluginServices.getText(this, "failed_acessing_files"), null, SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
350
                }
351

    
352

    
353
                return sym;
354

    
355

    
356
        }
357

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

    
380

    
381

    
382
        public String getName() {
383
                return PluginServices.getText(this, "picture_fill_symbol");
384
        }
385

    
386
        public Class getSymbolClass() {
387
                return PictureFillSymbol.class;
388
        }
389

    
390
        public JPanel[] getTabs() {
391
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
392
        }
393

    
394
        public void refreshControls(ISymbol layer) {
395
                PictureFillSymbol sym = (PictureFillSymbol) layer;
396

    
397
                File imageFile = new File(sym.getImagePath());
398
                File selImageFile =  new File (sym.getSelImagePath());
399

    
400
//                boolean enabled = imageFile.exists();
401
//                setControlsEnabled(enabled);
402

    
403
                lblFileName.setText(sym.getImagePath());
404
                lblSelFileName.setText(sym.getSelImagePath());
405
                jccFillColor.setUseColorIsSelected(sym.hasFill());
406
                jccFillColor.setColor(sym.getFillColor());
407

    
408
                outline=sym.getOutline();
409
                btnOutline.setSymbol(outline);
410
                useBorder.setSelected(sym.hasOutline());
411

    
412
                incrAngle.setDouble(sym.getAngle()/FConstant.DEGREE_TO_RADIANS);
413
                incrScaleX.setDouble(sym.getXScale());
414
                incrScaleY.setDouble(sym.getYScale());
415
                fillProperties.setModel(sym.getMarkerFillProperties());
416

    
417
                enableControls(lblFileName.getText()!="");
418

    
419
        }
420

    
421
        private void enableControls(boolean enabled){
422
                btnBrowseFileSelected.setEnabled(enabled);
423
                incrAngle.setEnabled(enabled);
424
                incrScaleX.setEnabled(enabled);
425
                incrScaleY.setEnabled(enabled);
426
                incrAngle.setEnabled(enabled);
427
                incrScaleX.setEnabled(enabled);
428
                incrScaleY.setEnabled(enabled);
429
                jccFillColor.setEnabled(enabled);
430
                btnOutline.setEnabled(enabled);
431
                useBorder.setEnabled(enabled);
432

    
433
                fillProperties.setEnabled(enabled);
434
        }
435

    
436
        public void actionPerformed(ActionEvent e) {
437
                Object s = e.getSource();
438

    
439
                if(s.equals(jccFillColor)) {
440
                        jccFillColor.getColor().getAlpha();
441
                }
442
                outline = (ILineSymbol) btnOutline.getSymbol();
443
                fireSymbolChangedEvent();
444
        }
445

    
446
}