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 / StyleSelector.java @ 40560
History | View | Annotate | Download (13.9 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40560 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | /* CVS MESSAGES:
|
||
25 | *
|
||
26 | * $Id: StyleSelector.java 30289 2009-07-31 11:10:41Z jpiera $
|
||
27 | * $Log$
|
||
28 | * Revision 1.11 2007-08-21 09:32:53 jvidal
|
||
29 | * javadoc
|
||
30 | *
|
||
31 | * Revision 1.10 2007/05/31 09:36:22 jaume
|
||
32 | * *** empty log message ***
|
||
33 | *
|
||
34 | * Revision 1.9 2007/05/10 09:47:50 jaume
|
||
35 | * *** empty log message ***
|
||
36 | *
|
||
37 | * Revision 1.8 2007/05/08 15:44:07 jaume
|
||
38 | * *** empty log message ***
|
||
39 | *
|
||
40 | * Revision 1.7 2007/04/27 12:10:17 jaume
|
||
41 | * *** empty log message ***
|
||
42 | *
|
||
43 | * Revision 1.6 2007/04/11 16:02:43 jaume
|
||
44 | * file filter
|
||
45 | *
|
||
46 | * Revision 1.5 2007/04/05 16:08:34 jaume
|
||
47 | * Styled labeling stuff
|
||
48 | *
|
||
49 | * Revision 1.4 2007/04/04 16:01:14 jaume
|
||
50 | * *** empty log message ***
|
||
51 | *
|
||
52 | * Revision 1.2 2007/03/09 11:25:00 jaume
|
||
53 | * Advanced symbology (start committing)
|
||
54 | *
|
||
55 | * Revision 1.1.2.4 2007/02/21 07:35:14 jaume
|
||
56 | * *** empty log message ***
|
||
57 | *
|
||
58 | * Revision 1.1.2.3 2007/02/08 15:43:04 jaume
|
||
59 | * some bug fixes in the editor and removed unnecessary imports
|
||
60 | *
|
||
61 | * Revision 1.1.2.2 2007/01/30 18:10:10 jaume
|
||
62 | * start commiting labeling stuff
|
||
63 | *
|
||
64 | * Revision 1.1.2.1 2007/01/26 13:49:03 jaume
|
||
65 | * *** empty log message ***
|
||
66 | *
|
||
67 | *
|
||
68 | */
|
||
69 | package org.gvsig.app.gui.styling; |
||
70 | |||
71 | import java.awt.BorderLayout; |
||
72 | import java.awt.Color; |
||
73 | import java.awt.Component; |
||
74 | import java.awt.Dimension; |
||
75 | import java.awt.FlowLayout; |
||
76 | import java.awt.event.ActionEvent; |
||
77 | import java.awt.event.ActionListener; |
||
78 | import java.io.File; |
||
79 | import java.io.FileWriter; |
||
80 | import java.util.prefs.Preferences; |
||
81 | |||
82 | import javax.swing.BorderFactory; |
||
83 | import javax.swing.BoxLayout; |
||
84 | import javax.swing.JComponent; |
||
85 | import javax.swing.JFileChooser; |
||
86 | import javax.swing.JLabel; |
||
87 | import javax.swing.JList; |
||
88 | import javax.swing.JOptionPane; |
||
89 | import javax.swing.JPanel; |
||
90 | import javax.swing.JTextField; |
||
91 | import javax.swing.ListCellRenderer; |
||
92 | import javax.swing.event.ListSelectionEvent; |
||
93 | import javax.swing.event.ListSelectionListener; |
||
94 | |||
95 | import org.exolab.castor.xml.Marshaller; |
||
96 | import org.gvsig.andami.PluginServices; |
||
97 | import org.gvsig.andami.messages.NotificationManager; |
||
98 | import org.gvsig.app.gui.JComboBoxUnits; |
||
99 | import org.gvsig.app.gui.panels.ImageSizePanel; |
||
100 | import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
||
101 | import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle; |
||
102 | import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle; |
||
103 | import org.gvsig.gui.beans.AcceptCancelPanel; |
||
104 | import org.gvsig.gui.beans.controls.dnd.JDnDList; |
||
105 | import org.gvsig.gui.beans.swing.GridBagLayoutPanel; |
||
106 | import org.gvsig.i18n.Messages; |
||
107 | import org.gvsig.utils.XMLEntity; |
||
108 | |||
109 | |||
110 | /**
|
||
111 | * Creates a panel where the user can select a style for an object that allows
|
||
112 | * to manage this property.This panel will be similar to the symbol selector panel
|
||
113 | * and, on it, the user will have a previsualization of the style of objects
|
||
114 | * stored and posibilities to modify an existing one, to create a new one
|
||
115 | * and so on.
|
||
116 | *
|
||
117 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
118 | *
|
||
119 | */
|
||
120 | public class StyleSelector extends SymbolSelector { |
||
121 | private static final long serialVersionUID = -7476555713446755512L; |
||
122 | private ImageSizePanel isp;
|
||
123 | |||
124 | public StyleSelector(IStyle style, int shapeType) { |
||
125 | this(style, shapeType, new SelectorFilter() { |
||
126 | public boolean accepts(Object obj) { |
||
127 | return obj instanceof IStyle; |
||
128 | } |
||
129 | }); |
||
130 | } |
||
131 | /**
|
||
132 | * Constructor method
|
||
133 | *
|
||
134 | * @param style
|
||
135 | * @param shapeType
|
||
136 | * @param filter
|
||
137 | */
|
||
138 | public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) { |
||
139 | super(null, shapeType, filter, false); |
||
140 | |||
141 | Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
||
142 | rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles")); |
||
143 | if (!rootDir.exists())
|
||
144 | rootDir.mkdir(); |
||
145 | |||
146 | try {
|
||
147 | initialize(style); |
||
148 | } catch (ClassNotFoundException e) { |
||
149 | throw new Error(e); |
||
150 | } |
||
151 | lblTitle.setText(Messages.getText("label_styles"));
|
||
152 | treeRootName = Messages.getText("style_library");
|
||
153 | |||
154 | } |
||
155 | |||
156 | @Override
|
||
157 | protected void initialize(Object currentElement) throws ClassNotFoundException { |
||
158 | library = new StyleLibrary(rootDir);
|
||
159 | |||
160 | this.setLayout(new BorderLayout()); |
||
161 | this.setSize(400, 221); |
||
162 | |||
163 | this.add(getJNorthPanel(), BorderLayout.NORTH); |
||
164 | this.add(getJSplitPane(), BorderLayout.CENTER); |
||
165 | this.add(getJEastPanel(), BorderLayout.EAST); |
||
166 | ActionListener okAction = new ActionListener() { |
||
167 | public void actionPerformed(ActionEvent e) { |
||
168 | accepted = true;
|
||
169 | PluginServices.getMDIManager().closeWindow(StyleSelector.this); |
||
170 | } |
||
171 | }, cancelAction = new ActionListener() { |
||
172 | public void actionPerformed(ActionEvent e) { |
||
173 | setSymbol(null);
|
||
174 | PluginServices.getMDIManager().closeWindow(StyleSelector.this); |
||
175 | } |
||
176 | }; |
||
177 | |||
178 | okCancelPanel = new AcceptCancelPanel();
|
||
179 | okCancelPanel.setOkButtonActionListener(okAction); |
||
180 | okCancelPanel.setCancelButtonActionListener(cancelAction); |
||
181 | |||
182 | this.add(okCancelPanel, BorderLayout.SOUTH); |
||
183 | libraryBrowser.setSelectionRow(0);
|
||
184 | |||
185 | SillyDragNDropAction dndAction = new SillyDragNDropAction();
|
||
186 | libraryBrowser.addMouseListener(dndAction); |
||
187 | libraryBrowser.addMouseMotionListener(dndAction); |
||
188 | getJListSymbols().addMouseListener(dndAction); |
||
189 | getJListSymbols().addMouseMotionListener(dndAction); |
||
190 | setSymbol(currentElement); |
||
191 | } |
||
192 | |||
193 | @Override
|
||
194 | public void setSymbol(Object style) { |
||
195 | ((StylePreviewer) jPanelPreview).setStyle((IStyle) style); |
||
196 | updateOptionsPanel(); |
||
197 | } |
||
198 | |||
199 | @Override
|
||
200 | public Object getSelectedObject() { |
||
201 | if (!accepted) return null; |
||
202 | Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
|
||
203 | |||
204 | if (mySelectedElement instanceof CartographicSupport) { |
||
205 | CartographicSupport csSym = (CartographicSupport) mySelectedElement; |
||
206 | csSym.setUnit(cmbUnits.getSelectedUnitIndex()); |
||
207 | csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex()); |
||
208 | } |
||
209 | |||
210 | return mySelectedElement;
|
||
211 | } |
||
212 | |||
213 | protected SymbolSelectorListModel newListModel() {
|
||
214 | StyleSelectorListModel listModel = new StyleSelectorListModel(
|
||
215 | dir, |
||
216 | // selectedElement,
|
||
217 | sFilter, |
||
218 | StyleSelectorListModel.STYLE_FILE_EXTENSION); |
||
219 | return listModel;
|
||
220 | |||
221 | } |
||
222 | |||
223 | protected JPanel getJPanelOptions() { |
||
224 | if (jPanelOptions == null) { |
||
225 | jPanelOptions = new GridBagLayoutPanel();
|
||
226 | jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, Messages.getText("options"))); |
||
227 | jPanelOptions.addComponent(getImageSizePanel()); |
||
228 | jPanelOptions.addComponent(Messages.getText("units"),
|
||
229 | cmbUnits = new JComboBoxUnits(true)); |
||
230 | jPanelOptions.addComponent("",
|
||
231 | cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
|
||
232 | } |
||
233 | return jPanelOptions;
|
||
234 | } |
||
235 | |||
236 | /**
|
||
237 | *
|
||
238 | * This method initializes ImageSizePanel
|
||
239 | *
|
||
240 | * @return isp ImageSizePanel
|
||
241 | */
|
||
242 | private ImageSizePanel getImageSizePanel() {
|
||
243 | if (isp == null) { |
||
244 | isp = new ImageSizePanel();
|
||
245 | isp.addActionListener(new ActionListener() { |
||
246 | public void actionPerformed(ActionEvent e) { |
||
247 | ILabelStyle st = (ILabelStyle) getSelectedObject(); |
||
248 | if (st != null) { |
||
249 | double[] sz = isp.getImageDimension(); |
||
250 | st.setSize(sz[0], sz[1]); |
||
251 | } |
||
252 | } |
||
253 | }); |
||
254 | } |
||
255 | |||
256 | return isp;
|
||
257 | } |
||
258 | |||
259 | protected void updateOptionsPanel() { |
||
260 | IStyle s = ((StylePreviewer) jPanelPreview).getStyle(); |
||
261 | if (s instanceof ILabelStyle) { |
||
262 | ILabelStyle lab = (ILabelStyle) s; |
||
263 | Dimension sz = lab.getSize();
|
||
264 | getImageSizePanel().setImageSize(sz); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | |||
269 | /**
|
||
270 | * This method initializes jList
|
||
271 | *
|
||
272 | * @return javax.swing.JList
|
||
273 | */
|
||
274 | protected JList getJListSymbols() { |
||
275 | if (jListSymbols == null) { |
||
276 | jListSymbols = new JDnDList();
|
||
277 | jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); |
||
278 | jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
|
||
279 | jListSymbols.setVisibleRowCount(-1);
|
||
280 | jListSymbols.addListSelectionListener(new ListSelectionListener() { |
||
281 | public void valueChanged(ListSelectionEvent e) { |
||
282 | setStyle(jListSymbols.getSelectedValue()); |
||
283 | updateOptionsPanel(); |
||
284 | } |
||
285 | }); |
||
286 | ListCellRenderer renderer = new ListCellRenderer() { |
||
287 | private Color mySelectedBGColor = new Color(255,145,100,255); |
||
288 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||
289 | IStyle sty = (IStyle) value; |
||
290 | JPanel pnl = new JPanel(); |
||
291 | BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS); |
||
292 | pnl.setLayout(layout); |
||
293 | Color bgColor = (isSelected) ? mySelectedBGColor
|
||
294 | : getJListSymbols().getBackground(); |
||
295 | |||
296 | pnl.setBackground(bgColor); |
||
297 | StylePreviewer sp = new StylePreviewer();
|
||
298 | sp.setShowOutline(false);
|
||
299 | sp.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||
300 | sp.setPreferredSize(new Dimension(50, 50)); |
||
301 | sp.setStyle(sty); |
||
302 | sp.setBackground(bgColor); |
||
303 | pnl.add(sp); |
||
304 | JLabel lbl = new JLabel(sty.getDescription()); |
||
305 | lbl.setBackground(bgColor); |
||
306 | lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||
307 | pnl.add(lbl); |
||
308 | |||
309 | return pnl;
|
||
310 | } |
||
311 | |||
312 | }; |
||
313 | jListSymbols.setCellRenderer(renderer); |
||
314 | } |
||
315 | return jListSymbols;
|
||
316 | } |
||
317 | /**
|
||
318 | * Modify the previsualization showed in the panel with the style of the
|
||
319 | * new object selected.
|
||
320 | *
|
||
321 | * @param selectedValue
|
||
322 | */
|
||
323 | protected void setStyle(Object selectedValue) { |
||
324 | //selectedElement = selectedValue;
|
||
325 | ((StylePreviewer) jPanelPreview).setStyle((IStyle) selectedValue); |
||
326 | doLayout(); |
||
327 | repaint(); |
||
328 | } |
||
329 | |||
330 | protected void propertiesPressed() { |
||
331 | StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
|
||
332 | PluginServices.getMDIManager().addWindow(se); |
||
333 | setStyle(se.getStyle()); |
||
334 | } |
||
335 | |||
336 | protected void savePressed() { |
||
337 | if (getSelectedObject() ==null) |
||
338 | return;
|
||
339 | |||
340 | JFileChooser jfc = new JFileChooser(rootDir); |
||
341 | javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
|
||
342 | public boolean accept(File f) { |
||
343 | return f.getAbsolutePath().
|
||
344 | toLowerCase(). |
||
345 | endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION); |
||
346 | } |
||
347 | |||
348 | public String getDescription() { |
||
349 | return Messages.getText("gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")"; |
||
350 | } |
||
351 | }; |
||
352 | jfc.setFileFilter(ff); |
||
353 | JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5)); |
||
354 | accessory.add(new JLabel(Messages.getText("enter_description"))); |
||
355 | JTextField txtDesc = new JTextField(25); |
||
356 | txtDesc.setText(((IStyle) getSelectedObject()).getDescription()); |
||
357 | accessory.add(txtDesc); |
||
358 | jfc.setAccessory(accessory); |
||
359 | if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { |
||
360 | File targetFile = jfc.getSelectedFile();
|
||
361 | |||
362 | String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
|
||
363 | |||
364 | // apply description
|
||
365 | String desc;
|
||
366 | if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) { |
||
367 | // default to file name
|
||
368 | String s = targetFile.getAbsolutePath();
|
||
369 | desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, ""); |
||
370 | } else {
|
||
371 | desc = txtDesc.getText().trim(); |
||
372 | } |
||
373 | IStyle s = (IStyle) getSelectedObject(); |
||
374 | s.setDescription(desc); |
||
375 | |||
376 | // save it
|
||
377 | XMLEntity xml=null;
|
||
378 | //XMLENTITY-UPDATED
|
||
379 | // try {
|
||
380 | //
|
||
381 | // xml = s.getXMLEntity();
|
||
382 | // } catch (XMLException e) {
|
||
383 | // NotificationManager.addWarning("Symbol layer", e);
|
||
384 | // }
|
||
385 | if (!targetFile.
|
||
386 | getAbsolutePath(). |
||
387 | toLowerCase(). |
||
388 | endsWith(fExtension)) |
||
389 | targetFile = new File(targetFile.getAbsolutePath() + fExtension); |
||
390 | if(targetFile.exists()){
|
||
391 | int resp = JOptionPane.showConfirmDialog( |
||
392 | (Component) PluginServices.getMainFrame(),
|
||
393 | Messages.getText("fichero_ya_existe_seguro_desea_guardarlo"),
|
||
394 | Messages.getText("guardar"), JOptionPane.YES_NO_OPTION); |
||
395 | if (resp != JOptionPane.YES_OPTION) { |
||
396 | return;
|
||
397 | } |
||
398 | } |
||
399 | FileWriter writer;
|
||
400 | try {
|
||
401 | writer = new FileWriter(targetFile.getAbsolutePath()); |
||
402 | Marshaller m = new Marshaller(writer);
|
||
403 | m.setEncoding("ISO-8859-1");
|
||
404 | m.marshal(xml.getXmlTag()); |
||
405 | |||
406 | } catch (Exception ex) { |
||
407 | NotificationManager.addError( |
||
408 | Messages.getText("save_error"), ex);
|
||
409 | } |
||
410 | getJListSymbols().setModel(newListModel()); |
||
411 | } |
||
412 | } |
||
413 | |||
414 | /**
|
||
415 | * This method initializes jPanelPreview
|
||
416 | *
|
||
417 | * @return javax.swing.JComponent
|
||
418 | */
|
||
419 | protected JComponent getJPanelPreview() { |
||
420 | if (jPanelPreview == null) { |
||
421 | jPanelPreview = new StylePreviewer();
|
||
422 | jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100)); |
||
423 | jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
||
424 | ((StylePreviewer) jPanelPreview).setShowOutline(true);
|
||
425 | } |
||
426 | return jPanelPreview;
|
||
427 | } |
||
428 | |||
429 | @Override
|
||
430 | public void actionPerformed(ActionEvent e) { |
||
431 | if (!act) return; |
||
432 | Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject(); |
||
433 | performActionOn(selectedElement, e); |
||
434 | StyleSelector.this.repaint(); |
||
435 | } |
||
436 | |||
437 | public int getUnit() { |
||
438 | return cmbUnits.getSelectedUnitIndex();
|
||
439 | } |
||
440 | |||
441 | public int getReferenceSystem() { |
||
442 | return cmbReferenceSystem.getSelectedIndex();
|
||
443 | } |
||
444 | |||
445 | public void setUnit(int unit) { |
||
446 | cmbUnits.setSelectedUnitIndex(unit); |
||
447 | } |
||
448 | |||
449 | public void setReferenceSystem(int referenceSystem) { |
||
450 | cmbReferenceSystem.setSelectedIndex(referenceSystem); |
||
451 | } |
||
452 | } |