svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / PictureFill.java @ 25807
History | View | Annotate | Download (12.9 KB)
1 | 20768 | jdominguez | /* 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 | 20905 | jdominguez | import org.gvsig.gui.beans.swing.JFileChooser; |
65 | 20768 | jdominguez | 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 | 22644 | vcaballero | import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
72 | 20768 | jdominguez | import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol; |
73 | import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
||
74 | 22644 | vcaballero | import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException; |
75 | 20768 | jdominguez | 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 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 | 20905 | jdominguez | JUrlFileChooser jfc = new JUrlFileChooser(getName(), null); |
153 | 20768 | jdominguez | 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 | 20905 | jdominguez | // 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 | 20768 | jdominguez | if(returnVal == JFileChooser.APPROVE_OPTION) { |
177 | 20905 | jdominguez | |
178 | URL url = jfc.getSelectedURL();
|
||
179 | if (url == null) return; |
||
180 | targetLbl.setText(url.toString()); |
||
181 | fireSymbolChangedEvent(); |
||
182 | 20768 | jdominguez | } |
183 | } |
||
184 | |||
185 | }; |
||
186 | |||
187 | private JButton btnBrowseFile; |
||
188 | private JButton btnBrowseFileSelected; |
||
189 | |||
190 | /**
|
||
191 | * Constructor method
|
||
192 | * @param owner
|
||
193 | */
|
||
194 | public PictureFill(SymbolEditor owner) {
|
||
195 | super(owner);
|
||
196 | initialize(); |
||
197 | } |
||
198 | |||
199 | /**
|
||
200 | * Initializes the parameters that allows the user to fill the padding of
|
||
201 | * a polygon with a picture style.To do it, two tabs are created (picture fill and
|
||
202 | * MarkerFillProperties)inside the SymbolEditor panel with default values for the
|
||
203 | * different attributes.
|
||
204 | */
|
||
205 | private void initialize() { |
||
206 | JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5)); |
||
207 | myTab.setName(PluginServices.getText(this, "picture_fill")); |
||
208 | |||
209 | btnBrowseFile = new JButton(PluginServices.getText(this, "browse")); |
||
210 | btnBrowseFile.addActionListener(chooseAction); |
||
211 | |||
212 | btnBrowseFileSelected = new JButton(PluginServices.getText(this,"browse")); |
||
213 | btnBrowseFileSelected.addActionListener(chooseAction); |
||
214 | |||
215 | JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); |
||
216 | |||
217 | JPanel auxLabelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); |
||
218 | JLabel lblName = new JLabel(); |
||
219 | lblName.setFont(lblName.getFont().deriveFont(Font.BOLD));
|
||
220 | lblName.setText(PluginServices.getText(this, "picture_file")+":"); |
||
221 | auxLabelPic.add(lblName); |
||
222 | |||
223 | aux2.add(btnBrowseFile); |
||
224 | aux2.add(lblFileName = new JLabel("")); |
||
225 | |||
226 | JPanel auxLabelSelPic=new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); |
||
227 | JLabel lblSelName = new JLabel(); |
||
228 | lblSelName.setFont(lblSelName.getFont().deriveFont(Font.BOLD));
|
||
229 | lblSelName.setText(PluginServices.getText(this, "selection_picture_file")+":"); |
||
230 | auxLabelSelPic.add(lblSelName); |
||
231 | |||
232 | JPanel aux4 = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); |
||
233 | aux4.add(btnBrowseFileSelected); |
||
234 | aux4.add(lblSelFileName = new JLabel("")); |
||
235 | |||
236 | GridBagLayoutPanel aux = new GridBagLayoutPanel();
|
||
237 | aux.addComponent(new JBlank(5, 5)); |
||
238 | aux.addComponent(auxLabelPic); |
||
239 | aux.addComponent(aux2); |
||
240 | aux.addComponent(auxLabelSelPic); |
||
241 | aux.addComponent(aux4); |
||
242 | |||
243 | |||
244 | aux2 = new JPanel(new GridLayout(1, 2, 20, 5)); |
||
245 | GridBagLayoutPanel aux3; |
||
246 | aux3 = new GridBagLayoutPanel();
|
||
247 | aux3.addComponent(PluginServices.getText(this, "angle")+":", |
||
248 | incrAngle = new JIncrementalNumberField("0", 20)); |
||
249 | aux3.addComponent(PluginServices.getText(this, "scale")+"X:", |
||
250 | incrScaleX = new JIncrementalNumberField(
|
||
251 | "1",
|
||
252 | 20,
|
||
253 | 0.01,
|
||
254 | Double.POSITIVE_INFINITY,
|
||
255 | 0.1));
|
||
256 | incrScaleX.setDouble(1);
|
||
257 | aux3.addComponent(PluginServices.getText(this, "scale")+"Y:", |
||
258 | incrScaleY = new JIncrementalNumberField(
|
||
259 | "1",
|
||
260 | 20,
|
||
261 | 0.01,
|
||
262 | Double.POSITIVE_INFINITY,
|
||
263 | 0.1));
|
||
264 | incrScaleY.setDouble(1);
|
||
265 | aux2.add(aux3); |
||
266 | |||
267 | aux3 = new GridBagLayoutPanel();
|
||
268 | aux3.addComponent(new JBlank(5,5)); |
||
269 | aux3.addComponent(new JLabel (PluginServices.getText(this, "fill_color")+":")); |
||
270 | aux3.addComponent(new JBlank(5,5)); |
||
271 | aux3.addComponent(jccFillColor = new ColorChooserPanel(true,true)); |
||
272 | jccFillColor.setAlpha(255);
|
||
273 | |||
274 | aux3.addComponent(new JBlank(5,5)); |
||
275 | aux3.addComponent(new JBlank(5,5)); |
||
276 | aux2.add(aux3); |
||
277 | |||
278 | aux.addComponent(aux2); |
||
279 | aux.addComponent(new JBlank(10, 10)); |
||
280 | aux2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
||
281 | aux2.add(btnOutline = new JSymbolPreviewButton(FShape.LINE));
|
||
282 | useBorder = new JCheckBox(PluginServices.getText(this, "use_outline")); |
||
283 | aux.addComponent(useBorder); |
||
284 | aux.addComponent(PluginServices.getText(this, "outline")+":", |
||
285 | aux2); |
||
286 | |||
287 | fillProperties = new MarkerFillProperties();
|
||
288 | myTab.add(aux); |
||
289 | |||
290 | fillProperties.addActionListener(this);
|
||
291 | incrAngle.addActionListener(this);
|
||
292 | incrScaleX.addActionListener(this);
|
||
293 | incrScaleY.addActionListener(this);
|
||
294 | jccFillColor.addActionListener(this);
|
||
295 | btnOutline.addActionListener(this);
|
||
296 | useBorder.addActionListener(this);
|
||
297 | |||
298 | tabs.add(myTab); |
||
299 | tabs.add(fillProperties); |
||
300 | |||
301 | } |
||
302 | |||
303 | public EditorTool[] getEditorTools() { |
||
304 | // TODO Auto-generated method stub
|
||
305 | throw new Error("Not yet implemented!"); |
||
306 | } |
||
307 | |||
308 | public ISymbol getLayer() {
|
||
309 | File imageFile = new File(lblFileName.getText()); |
||
310 | File imageSelFile = new File(lblSelFileName.getText()); |
||
311 | |||
312 | |||
313 | |||
314 | PictureFillSymbol sym=null;
|
||
315 | try {
|
||
316 | |||
317 | if( lblFileName.getText().equals("") ) |
||
318 | sym=null;
|
||
319 | |||
320 | else {
|
||
321 | sym = new PictureFillSymbol(new URL(lblFileName.getText()),null); |
||
322 | if (!lblSelFileName.getText().equals("")) |
||
323 | sym = new PictureFillSymbol(new URL(lblFileName.getText()),new URL(lblSelFileName.getText())); |
||
324 | |||
325 | sym.setHasFill(jccFillColor.getUseColorisSelected()); |
||
326 | Color c = jccFillColor.getColor();
|
||
327 | if (c != null) |
||
328 | c = new Color(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()); |
||
329 | sym.setFillColor(c); |
||
330 | |||
331 | sym.setHasOutline(useBorder.isSelected()); |
||
332 | outline = (ILineSymbol) btnOutline.getSymbol(); |
||
333 | sym.setOutline(outline); |
||
334 | |||
335 | sym.setAngle(incrAngle.getDouble()*FConstant.DEGREE_TO_RADIANS); |
||
336 | sym.setXScale(incrScaleX.getDouble()); |
||
337 | sym.setYScale(incrScaleY.getDouble()); |
||
338 | sym.setMarkerFillProperties(fillProperties.getMarkerFillProperties()); |
||
339 | } |
||
340 | |||
341 | } catch (IOException e) { |
||
342 | 22644 | vcaballero | return SymbologyFactory.getWarningSymbol
|
343 | (PluginServices.getText(this, "failed_acessing_files"), null, SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS); |
||
344 | 20768 | jdominguez | } |
345 | |||
346 | 22644 | vcaballero | |
347 | 20768 | jdominguez | return sym;
|
348 | |||
349 | |||
350 | } |
||
351 | |||
352 | private void setControlsEnabled(boolean enabled) { |
||
353 | for (int i = 0; i < tabs.size(); i++) { |
||
354 | setOptionsEnabled(tabs.get(i), enabled); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | |||
359 | |||
360 | private void setOptionsEnabled(Component c, boolean b) { |
||
361 | if (!(c.equals(btnBrowseFile) || tabs.contains(c)))
|
||
362 | c.setEnabled(b); |
||
363 | |||
364 | System.out.println(c.getClass().getName()+", "+c.getName()); |
||
365 | if (c instanceof Container) { |
||
366 | Container cont = (Container) c; |
||
367 | for(int j = 0; j < cont.getComponentCount(); j++) { |
||
368 | setOptionsEnabled(cont.getComponent(j), b); |
||
369 | } |
||
370 | } |
||
371 | |||
372 | } |
||
373 | |||
374 | |||
375 | |||
376 | public String getName() { |
||
377 | return PluginServices.getText(this, "picture_fill_symbol"); |
||
378 | } |
||
379 | |||
380 | public Class getSymbolClass() { |
||
381 | return PictureFillSymbol.class;
|
||
382 | } |
||
383 | |||
384 | public JPanel[] getTabs() { |
||
385 | return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]); |
||
386 | } |
||
387 | |||
388 | public void refreshControls(ISymbol layer) { |
||
389 | PictureFillSymbol sym = (PictureFillSymbol) layer; |
||
390 | |||
391 | File imageFile = new File(sym.getImagePath()); |
||
392 | File selImageFile = new File (sym.getSelImagePath()); |
||
393 | |||
394 | // boolean enabled = imageFile.exists();
|
||
395 | // setControlsEnabled(enabled);
|
||
396 | |||
397 | lblFileName.setText(sym.getImagePath()); |
||
398 | lblSelFileName.setText(sym.getSelImagePath()); |
||
399 | jccFillColor.setUseColorIsSelected(sym.hasFill()); |
||
400 | jccFillColor.setColor(sym.getFillColor()); |
||
401 | |||
402 | outline=sym.getOutline(); |
||
403 | btnOutline.setSymbol(outline); |
||
404 | useBorder.setSelected(sym.hasOutline()); |
||
405 | |||
406 | incrAngle.setDouble(sym.getAngle()/FConstant.DEGREE_TO_RADIANS); |
||
407 | incrScaleX.setDouble(sym.getXScale()); |
||
408 | incrScaleY.setDouble(sym.getYScale()); |
||
409 | fillProperties.setModel(sym.getMarkerFillProperties()); |
||
410 | |||
411 | |||
412 | |||
413 | } |
||
414 | |||
415 | public void actionPerformed(ActionEvent e) { |
||
416 | Object s = e.getSource();
|
||
417 | |||
418 | if(s.equals(jccFillColor)) {
|
||
419 | jccFillColor.getColor().getAlpha(); |
||
420 | } |
||
421 | outline = (ILineSymbol) btnOutline.getSymbol(); |
||
422 | fireSymbolChangedEvent(); |
||
423 | } |
||
424 | |||
425 | } |