svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolSelector.java @ 11404
History | View | Annotate | Download (25.3 KB)
1 | 9745 | jaume | /*
|
---|---|---|---|
2 | * Created on 26-abr-2005
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | *
|
||
6 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | *
|
||
34 | * or
|
||
35 | *
|
||
36 | * IVER T.I. S.A
|
||
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | *
|
||
41 | * +34 963163400
|
||
42 | * dac@iver.es
|
||
43 | */
|
||
44 | package com.iver.cit.gvsig.gui.styling; |
||
45 | |||
46 | import java.awt.BorderLayout; |
||
47 | import java.awt.Color; |
||
48 | import java.awt.Component; |
||
49 | import java.awt.Dimension; |
||
50 | import java.awt.FlowLayout; |
||
51 | import java.awt.Font; |
||
52 | 10679 | jaume | import java.awt.GraphicsEnvironment; |
53 | 9745 | jaume | import java.awt.GridLayout; |
54 | import java.awt.event.ActionEvent; |
||
55 | import java.awt.event.ActionListener; |
||
56 | import java.io.File; |
||
57 | import java.io.FileFilter; |
||
58 | import java.io.FileWriter; |
||
59 | 10679 | jaume | import java.util.prefs.Preferences; |
60 | 9745 | jaume | |
61 | import javax.swing.BorderFactory; |
||
62 | import javax.swing.BoxLayout; |
||
63 | 10679 | jaume | import javax.swing.ImageIcon; |
64 | 9745 | jaume | import javax.swing.JComponent; |
65 | import javax.swing.JFileChooser; |
||
66 | import javax.swing.JLabel; |
||
67 | import javax.swing.JList; |
||
68 | import javax.swing.JOptionPane; |
||
69 | import javax.swing.JPanel; |
||
70 | import javax.swing.JScrollPane; |
||
71 | import javax.swing.JSplitPane; |
||
72 | 10679 | jaume | import javax.swing.JTextField; |
73 | import javax.swing.JToggleButton; |
||
74 | 9745 | jaume | import javax.swing.JTree; |
75 | import javax.swing.ListCellRenderer; |
||
76 | import javax.swing.event.ListSelectionEvent; |
||
77 | import javax.swing.event.ListSelectionListener; |
||
78 | import javax.swing.event.TreeModelListener; |
||
79 | import javax.swing.tree.TreeModel; |
||
80 | import javax.swing.tree.TreePath; |
||
81 | |||
82 | import org.exolab.castor.xml.Marshaller; |
||
83 | import org.gvsig.gui.beans.AcceptCancelPanel; |
||
84 | import org.gvsig.gui.beans.swing.GridBagLayoutPanel; |
||
85 | import org.gvsig.gui.beans.swing.JButton; |
||
86 | |||
87 | import com.iver.andami.PluginServices; |
||
88 | import com.iver.andami.messages.NotificationManager; |
||
89 | import com.iver.andami.ui.mdiManager.IWindow; |
||
90 | import com.iver.andami.ui.mdiManager.WindowInfo; |
||
91 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.FPoint2D; |
92 | 9745 | jaume | import com.iver.cit.gvsig.fmap.core.FShape; |
93 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
94 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
95 | import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
||
96 | import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
||
97 | 10927 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol; |
98 | 10679 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol; |
99 | 9745 | jaume | import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
100 | 11404 | jaume | import com.iver.cit.gvsig.gui.IncrementalNumberField; |
101 | 9745 | jaume | import com.iver.cit.gvsig.gui.panels.ColorChooserPanel; |
102 | 10679 | jaume | import com.iver.utiles.IPersistance; |
103 | 9745 | jaume | import com.iver.utiles.StringUtilities; |
104 | import com.iver.utiles.XMLEntity; |
||
105 | 10679 | jaume | import com.iver.utiles.swing.JComboBox; |
106 | 9745 | jaume | |
107 | /**
|
||
108 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
109 | */
|
||
110 | public class SymbolSelector extends JPanel implements IWindow{ |
||
111 | |||
112 | private JTree treeFamilies = null; |
||
113 | private JPanel jPanel = null; |
||
114 | private JScrollPane jScrollPane = null; |
||
115 | private JScrollPane jScrollPane1 = null; |
||
116 | 10679 | jaume | private WindowInfo wi;
|
117 | 9745 | jaume | private JSplitPane jSplitPane = null; |
118 | private AcceptCancelPanel okCancelPanel;
|
||
119 | private JPanel northPanel; |
||
120 | private ColorChooserPanel jcc1;
|
||
121 | private ColorChooserPanel jcc2;
|
||
122 | 11404 | jaume | private IncrementalNumberField txtSize;
|
123 | private IncrementalNumberField txtAngle;
|
||
124 | 9745 | jaume | private JPanel jPanelButtons; |
125 | private JButton btnProperties; |
||
126 | private int shapeType; |
||
127 | private JButton btnSaveSymbol; |
||
128 | private JButton btnResetSymbol; |
||
129 | private ActionListener buttonListener = new MyButtonListener(); |
||
130 | 10679 | jaume | private JButton btnNewSymbol; |
131 | private JComboBox cmbFonts; |
||
132 | private JToggleButton btnBold; |
||
133 | private JToggleButton btnItalic; |
||
134 | private JToggleButton btnUnderlined; |
||
135 | protected JLabel lblTitle; |
||
136 | protected File dir; |
||
137 | protected Object selectedElement = null; |
||
138 | protected File rootDir; |
||
139 | protected JComponent jPanelPreview = null; |
||
140 | protected GridBagLayoutPanel jPanelOptions = null; |
||
141 | protected JList jListSymbols = null; |
||
142 | protected String treeRootName; |
||
143 | 10995 | jaume | protected TreeModel treeModel; |
144 | 11404 | jaume | private IncrementalNumberField txtWidth;
|
145 | 10927 | jaume | |
146 | 10679 | jaume | protected SelectorFilter sFilter = new SelectorFilter() { |
147 | private final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D()); |
||
148 | private final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX()); |
||
149 | private final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX()); |
||
150 | |||
151 | public boolean accepts(Object obj) { |
||
152 | if (obj instanceof ISymbol) { |
||
153 | ISymbol sym = (ISymbol) obj; |
||
154 | |||
155 | IGeometry compareGeometry = null;
|
||
156 | switch (SymbolSelector.this.shapeType) {
|
||
157 | case FShape.POINT:
|
||
158 | compareGeometry = dummyPointGeom; |
||
159 | break;
|
||
160 | case FShape.LINE:
|
||
161 | compareGeometry = dummyLineGeom; |
||
162 | break;
|
||
163 | case FShape.POLYGON:
|
||
164 | compareGeometry = dummyPolygonGeom; |
||
165 | break;
|
||
166 | } |
||
167 | return sym.isSuitableFor(compareGeometry);
|
||
168 | } |
||
169 | return false; |
||
170 | } |
||
171 | }; |
||
172 | private JLineStyleComboBox cmbLineStyles;
|
||
173 | 10927 | jaume | |
174 | 10679 | jaume | public SymbolSelector(Object currentElement, int shapeType) { |
175 | 9745 | jaume | super();
|
176 | 10679 | jaume | if (currentElement != null && currentElement instanceof ISymbol) { |
177 | ISymbol sym = (ISymbol) currentElement; |
||
178 | currentElement = SymbologyFactory.createSymbolFromXML( |
||
179 | sym.getXMLEntity(), "");
|
||
180 | String desc = sym.getDescription();
|
||
181 | desc += " ("+PluginServices.getText(this, "current")+")"; |
||
182 | ((ISymbol)currentElement).setDescription(desc); |
||
183 | } |
||
184 | this.selectedElement = currentElement;
|
||
185 | 9745 | jaume | this.shapeType = shapeType;
|
186 | 10679 | jaume | Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
187 | rootDir = new File(prefs.get("SymbolLibraryFolder", System.getProperty("user.home")+"/gvSIG/Symbols")); |
||
188 | if (!rootDir.exists())
|
||
189 | rootDir.mkdir(); |
||
190 | treeRootName = PluginServices.getText(this, "symbol_library"); |
||
191 | 9745 | jaume | initialize(); |
192 | } |
||
193 | 10927 | jaume | |
194 | 10679 | jaume | public SymbolSelector(Object symbol, int shapeType, SelectorFilter filter) { |
195 | this(symbol, shapeType);
|
||
196 | sFilter = filter; |
||
197 | } |
||
198 | 9745 | jaume | |
199 | /**
|
||
200 | * This method initializes this
|
||
201 | *
|
||
202 | */
|
||
203 | private void initialize() { |
||
204 | 10995 | jaume | treeModel = new TreeModel() { |
205 | final class MyFile extends File { |
||
206 | public MyFile(String pathname) { |
||
207 | super(pathname);
|
||
208 | } |
||
209 | 9745 | jaume | |
210 | 10995 | jaume | public String toString() { |
211 | if (this.equals(root)) |
||
212 | return treeRootName;
|
||
213 | String path = getAbsolutePath();
|
||
214 | String prefixToRemove = rootDir.getAbsolutePath();
|
||
215 | path = path.substring(prefixToRemove.length()+1, path.length());
|
||
216 | return path;
|
||
217 | } |
||
218 | }; |
||
219 | MyFile root = new MyFile(rootDir.getAbsolutePath());
|
||
220 | 9745 | jaume | |
221 | 10995 | jaume | private FileFilter ff = new FileFilter() { |
222 | public boolean accept(File pathname) { |
||
223 | return pathname.isDirectory();
|
||
224 | } |
||
225 | }; |
||
226 | |||
227 | public Object getRoot() { |
||
228 | return root;
|
||
229 | } |
||
230 | |||
231 | public int getChildCount(Object parent) { |
||
232 | return ((File) parent).listFiles(ff).length; |
||
233 | } |
||
234 | |||
235 | public boolean isLeaf(Object node) { |
||
236 | return getChildCount(node)==0; |
||
237 | } |
||
238 | |||
239 | public void addTreeModelListener(TreeModelListener l) { |
||
240 | // TODO Necessite?
|
||
241 | |||
242 | } |
||
243 | |||
244 | public void removeTreeModelListener(TreeModelListener l) { |
||
245 | // TODO Necessite?
|
||
246 | } |
||
247 | |||
248 | public Object getChild(Object parent, int index) { |
||
249 | return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath()); |
||
250 | } |
||
251 | |||
252 | public int getIndexOfChild(Object parent, Object child) { |
||
253 | if (parent == null) |
||
254 | return -1; |
||
255 | File[] files = ((File) parent).listFiles(ff); |
||
256 | for (int i = 0; i < files.length; i++) { |
||
257 | if (files[i].equals((File) child)) |
||
258 | return i;
|
||
259 | } |
||
260 | return -1; |
||
261 | } |
||
262 | |||
263 | public void valueForPathChanged(TreePath path, Object newValue) { |
||
264 | // TODO Auto-generated method stub
|
||
265 | } |
||
266 | |||
267 | }; |
||
268 | |||
269 | this.setLayout(new BorderLayout()); |
||
270 | this.setSize(400, 221); |
||
271 | |||
272 | this.add(getJNorthPanel(), BorderLayout.NORTH); |
||
273 | this.add(getJSplitPane(), BorderLayout.CENTER); |
||
274 | this.add(getJEastPanel(), BorderLayout.EAST); |
||
275 | ActionListener okAction = new ActionListener() { |
||
276 | 9745 | jaume | public void actionPerformed(ActionEvent e) { |
277 | PluginServices.getMDIManager().closeWindow(SymbolSelector.this); |
||
278 | } |
||
279 | }, cancelAction = new ActionListener() { |
||
280 | public void actionPerformed(ActionEvent e) { |
||
281 | setSymbol(null);
|
||
282 | PluginServices.getMDIManager().closeWindow(SymbolSelector.this); |
||
283 | } |
||
284 | }; |
||
285 | |||
286 | 10995 | jaume | okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
|
287 | 9745 | jaume | |
288 | |||
289 | 10995 | jaume | this.add(okCancelPanel, BorderLayout.SOUTH); |
290 | 9745 | jaume | } |
291 | |||
292 | 10995 | jaume | protected SymbolSelectorListModel newListModel() {
|
293 | SymbolSelectorListModel listModel = new SymbolSelectorListModel(
|
||
294 | 10679 | jaume | dir, |
295 | (ISymbol) selectedElement, |
||
296 | sFilter, |
||
297 | 10995 | jaume | SymbolSelectorListModel.SYMBOL_FILE_EXTENSION); |
298 | 10679 | jaume | return listModel;
|
299 | } |
||
300 | |||
301 | 9745 | jaume | private JPanel getJNorthPanel() throws IllegalArgumentException { |
302 | if (northPanel == null) { |
||
303 | String text = ""; |
||
304 | switch (shapeType) {
|
||
305 | case FShape.POINT:
|
||
306 | text = PluginServices.getText(this, "point_symbols"); |
||
307 | break;
|
||
308 | case FShape.LINE:
|
||
309 | text = PluginServices.getText(this, "line_symbols"); |
||
310 | break;
|
||
311 | case FShape.POLYGON:
|
||
312 | text = PluginServices.getText(this, "polygon_symbols"); |
||
313 | break;
|
||
314 | 10679 | jaume | case FShape.TEXT:
|
315 | text = PluginServices.getText(this, "text_symbols"); |
||
316 | break;
|
||
317 | 9745 | jaume | default:
|
318 | throw new IllegalArgumentException(PluginServices.getText(this, "shape_type_not_yet_supported")); |
||
319 | } |
||
320 | northPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); |
||
321 | 10679 | jaume | lblTitle = new JLabel(text); |
322 | lblTitle.setFont(lblTitle.getFont().deriveFont(Font.BOLD));
|
||
323 | northPanel.add(lblTitle); |
||
324 | 9745 | jaume | } |
325 | return northPanel;
|
||
326 | } |
||
327 | |||
328 | 10995 | jaume | |
329 | protected JTree getTreeFav() { |
||
330 | 9745 | jaume | if (treeFamilies == null) { |
331 | treeFamilies = new JTree(); |
||
332 | treeFamilies.setPreferredSize(new java.awt.Dimension(70,100)); |
||
333 | try {
|
||
334 | 10995 | jaume | treeFamilies.setModel(treeModel); |
335 | 9745 | jaume | treeFamilies.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
|
336 | public void valueChanged(javax.swing.event.TreeSelectionEvent e) { |
||
337 | 10679 | jaume | dir = (File)
|
338 | treeFamilies.getLastSelectedPathComponent(); |
||
339 | 9745 | jaume | |
340 | 10679 | jaume | if (dir == null) return; |
341 | |||
342 | jListSymbols.setModel(newListModel()); |
||
343 | 9745 | jaume | } |
344 | }); |
||
345 | 10927 | jaume | |
346 | 9745 | jaume | } catch ( ExceptionInInitializerError ex ) { |
347 | JOptionPane.showMessageDialog(this, PluginServices.getText(this, "could_not_find_symbol_directory")); |
||
348 | } |
||
349 | } |
||
350 | return treeFamilies;
|
||
351 | } |
||
352 | |||
353 | /**
|
||
354 | * This method initializes jList
|
||
355 | *
|
||
356 | * @return javax.swing.JList
|
||
357 | */
|
||
358 | 10679 | jaume | protected JList getJListSymbols() { |
359 | 9745 | jaume | if (jListSymbols == null) { |
360 | jListSymbols = new JList(); |
||
361 | jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); |
||
362 | jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
|
||
363 | jListSymbols.setVisibleRowCount(-1);
|
||
364 | jListSymbols.addListSelectionListener(new ListSelectionListener() { |
||
365 | public void valueChanged(ListSelectionEvent e) { |
||
366 | setSymbol((ISymbol) jListSymbols.getSelectedValue()); |
||
367 | updateOptionsPanel(); |
||
368 | } |
||
369 | }); |
||
370 | ListCellRenderer renderer = new ListCellRenderer() { |
||
371 | private Color mySelectedBGColor = new Color(255,145,100,255); |
||
372 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||
373 | ISymbol sym = (ISymbol) value; |
||
374 | JPanel pnl = new JPanel(); |
||
375 | BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS); |
||
376 | pnl.setLayout(layout); |
||
377 | Color bgColor = (isSelected) ? mySelectedBGColor
|
||
378 | : getJListSymbols().getBackground(); |
||
379 | |||
380 | pnl.setBackground(bgColor); |
||
381 | 10679 | jaume | SymbolPreviewer sp = new SymbolPreviewer();
|
382 | 9745 | jaume | sp.setAlignmentX(Component.CENTER_ALIGNMENT);
|
383 | sp.setPreferredSize(new Dimension(50, 50)); |
||
384 | sp.setSymbol(sym); |
||
385 | sp.setBackground(bgColor); |
||
386 | pnl.add(sp); |
||
387 | JLabel lbl = new JLabel(sym.getDescription()); |
||
388 | lbl.setBackground(bgColor); |
||
389 | lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||
390 | pnl.add(lbl); |
||
391 | |||
392 | return pnl;
|
||
393 | } |
||
394 | |||
395 | }; |
||
396 | jListSymbols.setCellRenderer(renderer); |
||
397 | } |
||
398 | return jListSymbols;
|
||
399 | } |
||
400 | |||
401 | protected void updateOptionsPanel() { |
||
402 | 10956 | jaume | IPersistance sym = ((IPersistance) jListSymbols.getSelectedValue()); |
403 | 9745 | jaume | if (sym == null) |
404 | return;
|
||
405 | |||
406 | XMLEntity xml = sym.getXMLEntity(); |
||
407 | |||
408 | Color c = null; |
||
409 | if (xml.contains("color")) { |
||
410 | c = StringUtilities.string2Color(xml.getStringProperty("color"));
|
||
411 | } |
||
412 | 10956 | jaume | if (jcc1 != null) |
413 | jcc1.setColor(c); |
||
414 | 9745 | jaume | |
415 | 10956 | jaume | if (jcc2 != null) { |
416 | if (sym instanceof ILineSymbol && xml.contains("outline")) { |
||
417 | c = StringUtilities.string2Color(xml.getStringProperty("outline"));
|
||
418 | jcc2.setEnabled(true);
|
||
419 | jcc2.setColor(c); |
||
420 | } else {
|
||
421 | jcc2.setEnabled(false);
|
||
422 | } |
||
423 | 9745 | jaume | } |
424 | } |
||
425 | |||
426 | /**
|
||
427 | * This method initializes jPanel
|
||
428 | *
|
||
429 | * @return javax.swing.JPanel
|
||
430 | */
|
||
431 | private JPanel getJEastPanel() { |
||
432 | if (jPanel == null) { |
||
433 | jPanel = new JPanel(); |
||
434 | jPanel.setLayout(new BorderLayout()); |
||
435 | jPanel.add(getJPanelOptions(), BorderLayout.CENTER);
|
||
436 | JPanel aux = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5)); |
||
437 | aux.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "preview"))); |
||
438 | aux.add(getJPanelPreview()); |
||
439 | jPanel.add(aux, BorderLayout.NORTH);
|
||
440 | 10927 | jaume | |
441 | 10679 | jaume | jPanel.add(getJPanelOptions()); |
442 | 9745 | jaume | aux = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5)); |
443 | aux.add(getJPanelButtons()); |
||
444 | jPanel.add(aux, BorderLayout.SOUTH);
|
||
445 | } |
||
446 | return jPanel;
|
||
447 | } |
||
448 | |||
449 | private JPanel getJPanelButtons() { |
||
450 | if (jPanelButtons == null) { |
||
451 | jPanelButtons = new JPanel(); |
||
452 | GridLayout layout = new GridLayout(); |
||
453 | layout.setColumns(1);
|
||
454 | layout.setVgap(5);
|
||
455 | 10679 | jaume | jPanelButtons.add(getBtnNewSymbol()); |
456 | 9745 | jaume | jPanelButtons.add(getBtnSaveSymbol()); |
457 | jPanelButtons.add(getBtnResetSymbol()); |
||
458 | jPanelButtons.add(getBtnProperties()); |
||
459 | |||
460 | // do not add components bellow this line!
|
||
461 | layout.setRows(jPanelButtons.getComponentCount()); |
||
462 | jPanelButtons.setLayout(layout); |
||
463 | } |
||
464 | return jPanelButtons;
|
||
465 | } |
||
466 | |||
467 | 10679 | jaume | private JButton getBtnNewSymbol() { |
468 | if (btnNewSymbol == null) { |
||
469 | btnNewSymbol = new JButton(); |
||
470 | btnNewSymbol.setName("btnNewSymbol");
|
||
471 | btnNewSymbol.setText(PluginServices.getText(this, "new")); |
||
472 | btnNewSymbol.addActionListener(buttonListener); |
||
473 | } |
||
474 | return btnNewSymbol;
|
||
475 | } |
||
476 | |||
477 | 9745 | jaume | private JButton getBtnResetSymbol() { |
478 | if (btnResetSymbol == null) { |
||
479 | btnResetSymbol = new JButton(); |
||
480 | btnResetSymbol.setName("btnResetSymbol");
|
||
481 | btnResetSymbol.setText(PluginServices.getText(this, "reset")); |
||
482 | btnResetSymbol.addActionListener(buttonListener); |
||
483 | } |
||
484 | return btnResetSymbol;
|
||
485 | } |
||
486 | |||
487 | private JButton getBtnSaveSymbol() { |
||
488 | if (btnSaveSymbol == null) { |
||
489 | btnSaveSymbol = new JButton(); |
||
490 | btnSaveSymbol.setName("btnSaveSymbol");
|
||
491 | btnSaveSymbol.setText(PluginServices.getText(this, "save")); |
||
492 | btnSaveSymbol.addActionListener(buttonListener); |
||
493 | } |
||
494 | return btnSaveSymbol;
|
||
495 | } |
||
496 | |||
497 | private JButton getBtnProperties() { |
||
498 | if (btnProperties == null) { |
||
499 | btnProperties = new JButton(); |
||
500 | btnProperties.setName("btnProperties");
|
||
501 | btnProperties.setText(PluginServices.getText(this, "properties")); |
||
502 | btnProperties.addActionListener(buttonListener); |
||
503 | } |
||
504 | return btnProperties;
|
||
505 | } |
||
506 | |||
507 | /**
|
||
508 | * This method initializes jScrollPane
|
||
509 | *
|
||
510 | * @return javax.swing.JScrollPane
|
||
511 | */
|
||
512 | private JScrollPane getJScrollPane() { |
||
513 | if (jScrollPane == null) { |
||
514 | jScrollPane = new JScrollPane(); |
||
515 | jScrollPane.setPreferredSize(new java.awt.Dimension(80,130)); |
||
516 | jScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
||
517 | 10995 | jaume | jScrollPane.setViewportView(getTreeFav()); |
518 | 9745 | jaume | } |
519 | return jScrollPane;
|
||
520 | } |
||
521 | |||
522 | /**
|
||
523 | * This method initializes jScrollPane1
|
||
524 | *
|
||
525 | * @return javax.swing.JScrollPane
|
||
526 | */
|
||
527 | private JScrollPane getJScrollPane1() { |
||
528 | if (jScrollPane1 == null) { |
||
529 | jScrollPane1 = new JScrollPane(); |
||
530 | jScrollPane1.setViewportView(getJListSymbols()); |
||
531 | } |
||
532 | return jScrollPane1;
|
||
533 | } |
||
534 | |||
535 | /**
|
||
536 | * This method initializes jPanel1
|
||
537 | *
|
||
538 | * @return javax.swing.JPanel
|
||
539 | */
|
||
540 | 10679 | jaume | protected JComponent getJPanelPreview() { |
541 | 9745 | jaume | if (jPanelPreview == null) { |
542 | 10679 | jaume | jPanelPreview = new SymbolPreviewer();
|
543 | 9745 | jaume | jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100)); |
544 | jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
||
545 | } |
||
546 | return jPanelPreview;
|
||
547 | } |
||
548 | |||
549 | 10679 | jaume | protected JPanel getJPanelOptions() { |
550 | 9745 | jaume | if (jPanelOptions == null) { |
551 | jPanelOptions = new GridBagLayoutPanel();
|
||
552 | 10679 | jaume | jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options"))); |
553 | 9745 | jaume | jcc1 = new ColorChooserPanel();
|
554 | jcc2 = new ColorChooserPanel();
|
||
555 | jcc1.setAlpha(255);
|
||
556 | jcc2.setAlpha(255);
|
||
557 | jcc1.addActionListener(buttonListener); |
||
558 | jcc2.addActionListener(buttonListener); |
||
559 | if (shapeType == FShape.POINT) {
|
||
560 | jPanelOptions.addComponent( |
||
561 | PluginServices.getText(this, "color")+":", jcc1); |
||
562 | jPanelOptions.addComponent( |
||
563 | PluginServices.getText(this, "size")+":", |
||
564 | 11404 | jaume | txtSize = new IncrementalNumberField(String.valueOf(3))); |
565 | 9745 | jaume | jPanelOptions.addComponent( |
566 | PluginServices.getText(this, "angle")+":", |
||
567 | 11404 | jaume | txtAngle = new IncrementalNumberField());
|
568 | 9745 | jaume | } else if (shapeType == FShape.LINE) { |
569 | jPanelOptions.addComponent( |
||
570 | PluginServices.getText(this, "color")+":", jcc1); |
||
571 | jPanelOptions.addComponent( |
||
572 | PluginServices.getText(this, "width")+":", |
||
573 | 11404 | jaume | txtSize = new IncrementalNumberField(String.valueOf(3))); |
574 | 10679 | jaume | jPanelOptions.addComponent(PluginServices.getText(this, "line_style"), cmbLineStyles = new JLineStyleComboBox()); |
575 | 9745 | jaume | } else if (shapeType == FShape.POLYGON) { |
576 | jPanelOptions.addComponent( |
||
577 | PluginServices.getText(this, "fill_color")+":", jcc1); |
||
578 | jPanelOptions.addComponent( |
||
579 | PluginServices.getText(this, "outline_color")+":", jcc2); |
||
580 | jPanelOptions.addComponent( |
||
581 | 10927 | jaume | PluginServices.getText(this, "outline_width"), |
582 | 11404 | jaume | txtWidth = new IncrementalNumberField());
|
583 | 10679 | jaume | } else if (shapeType == FShape.TEXT) { |
584 | jPanelOptions.addComponent( |
||
585 | PluginServices.getText(this, "font")+":", getCmbFonts()); |
||
586 | 10927 | jaume | |
587 | 10679 | jaume | jPanelOptions.addComponent( |
588 | PluginServices.getText(this, "color")+":", jcc1); |
||
589 | jPanelOptions.addComponent( |
||
590 | PluginServices.getText(this, "size")+":", |
||
591 | 11404 | jaume | txtSize = new IncrementalNumberField());
|
592 | 10679 | jaume | JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING,0,1)); |
593 | aux.add(getBtnBold()); |
||
594 | aux.add(getBtnItalic()); |
||
595 | aux.add(getBtnUnderlined()); |
||
596 | jPanelOptions.addComponent( |
||
597 | PluginServices.getText(this, "style")+":", aux); |
||
598 | 10927 | jaume | |
599 | 10679 | jaume | } |
600 | 9745 | jaume | } |
601 | return jPanelOptions;
|
||
602 | } |
||
603 | 10927 | jaume | |
604 | 10679 | jaume | private JToggleButton getBtnUnderlined() { |
605 | if (btnUnderlined == null) { |
||
606 | btnUnderlined = new JToggleButton(new ImageIcon( |
||
607 | getClass().getClassLoader().getResource("images/underlined.png")));
|
||
608 | } |
||
609 | return btnUnderlined;
|
||
610 | } |
||
611 | 9745 | jaume | |
612 | 10679 | jaume | private JToggleButton getBtnItalic() { |
613 | if (btnItalic == null) { |
||
614 | btnItalic = new JToggleButton(new ImageIcon( |
||
615 | getClass().getClassLoader().getResource("images/italic.png")));
|
||
616 | } |
||
617 | return btnItalic;
|
||
618 | } |
||
619 | |||
620 | private JToggleButton getBtnBold() { |
||
621 | if (btnBold == null) { |
||
622 | btnBold = new JToggleButton(new ImageIcon( |
||
623 | getClass().getClassLoader().getResource("images/bold.png")));
|
||
624 | } |
||
625 | return btnBold;
|
||
626 | } |
||
627 | |||
628 | 10927 | jaume | |
629 | 10679 | jaume | private JComboBox getCmbFonts() { |
630 | if (cmbFonts == null) { |
||
631 | 11404 | jaume | // Font info is obtained from the current graphics environment.
|
632 | 10679 | jaume | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
633 | |||
634 | 11404 | jaume | // --- Get an array of font names (smaller than the number of fonts)
|
635 | 10679 | jaume | String[] fontNames = ge.getAvailableFontFamilyNames(); |
636 | cmbFonts = new JComboBox(fontNames); |
||
637 | } |
||
638 | return cmbFonts;
|
||
639 | } |
||
640 | 10927 | jaume | |
641 | 9745 | jaume | public WindowInfo getWindowInfo() {
|
642 | if (wi == null) { |
||
643 | wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
|
||
644 | wi.setWidth(706);
|
||
645 | wi.setHeight(500);
|
||
646 | wi.setTitle(PluginServices.getText(this, "symbol_selector")); |
||
647 | } |
||
648 | return wi;
|
||
649 | } |
||
650 | |||
651 | private JSplitPane getJSplitPane() { |
||
652 | if (jSplitPane == null) { |
||
653 | jSplitPane = new JSplitPane(); |
||
654 | jSplitPane.setDividerLocation(200);
|
||
655 | jSplitPane.setResizeWeight(0.4);
|
||
656 | jSplitPane.setLeftComponent(getJScrollPane()); |
||
657 | jSplitPane.setRightComponent(getJScrollPane1()); |
||
658 | } |
||
659 | return jSplitPane;
|
||
660 | } |
||
661 | |||
662 | /**
|
||
663 | 10679 | jaume | * Returns the symbol selected by the user or null if none. It tries to return
|
664 | * the simplest possible symbol that suits the user settings.
|
||
665 | 9745 | jaume | * @return
|
666 | */
|
||
667 | 10679 | jaume | public Object getSelectedObject() { |
668 | // if this symbol only has one layer, then no multilayer is needed
|
||
669 | if (selectedElement instanceof IMultiLayerSymbol) { |
||
670 | if (((IMultiLayerSymbol) selectedElement).getLayerCount()==1) |
||
671 | return ((IMultiLayerSymbol) selectedElement).getLayer(0); |
||
672 | } |
||
673 | return selectedElement;
|
||
674 | 9745 | jaume | } |
675 | |||
676 | 10679 | jaume | public void setSymbol(Object symbol) { |
677 | selectedElement = symbol; |
||
678 | ((SymbolPreviewer) jPanelPreview).setSymbol((ISymbol) symbol); |
||
679 | 9745 | jaume | } |
680 | |||
681 | 10679 | jaume | /**
|
682 | * Invoked when the PROPERTIES button is pressed
|
||
683 | */
|
||
684 | protected void propertiesPressed() { |
||
685 | if (selectedElement ==null) |
||
686 | return;
|
||
687 | 9745 | jaume | |
688 | 10679 | jaume | SymbolEditor se = new SymbolEditor((ISymbol) selectedElement, shapeType);
|
689 | PluginServices.getMDIManager().addWindow(se); |
||
690 | setSymbol(se.getSymbol()); |
||
691 | } |
||
692 | |||
693 | /**
|
||
694 | * Invoked when the NEW button is pressed
|
||
695 | */
|
||
696 | protected void newPressed() { |
||
697 | SymbolEditor se = new SymbolEditor(null, shapeType); |
||
698 | PluginServices.getMDIManager().addWindow(se); |
||
699 | setSymbol(se.getSymbol()); |
||
700 | } |
||
701 | |||
702 | /**
|
||
703 | * Invoked when the RESET button is pressed
|
||
704 | */
|
||
705 | protected void resetPressed() { |
||
706 | setSymbol(null);
|
||
707 | } |
||
708 | |||
709 | /**
|
||
710 | * Invoked when the SAVE button is pressed
|
||
711 | */
|
||
712 | protected void savePressed() { |
||
713 | if (getSelectedObject() ==null) |
||
714 | return;
|
||
715 | |||
716 | |||
717 | JFileChooser jfc = new JFileChooser(rootDir); |
||
718 | javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
|
||
719 | public boolean accept(File f) { |
||
720 | return f.getAbsolutePath().
|
||
721 | toLowerCase(). |
||
722 | 10995 | jaume | endsWith(SymbolSelectorListModel.SYMBOL_FILE_EXTENSION); |
723 | 10679 | jaume | } |
724 | |||
725 | public String getDescription() { |
||
726 | return PluginServices.getText(
|
||
727 | this, "gvSIG_symbol_definition_file")+ " (*.sym)"; |
||
728 | } |
||
729 | }; |
||
730 | jfc.setFileFilter(ff); |
||
731 | JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5)); |
||
732 | accessory.add(new JLabel(PluginServices.getText(this, "enter_description"))); |
||
733 | JTextField txtDesc = new JTextField(25); |
||
734 | accessory.add(txtDesc); |
||
735 | jfc.setAccessory(accessory); |
||
736 | if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { |
||
737 | File targetFile = jfc.getSelectedFile();
|
||
738 | |||
739 | 10995 | jaume | String fExtension = SymbolSelectorListModel.SYMBOL_FILE_EXTENSION;
|
740 | 10679 | jaume | |
741 | // apply description
|
||
742 | String desc;
|
||
743 | if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) { |
||
744 | // default to file name
|
||
745 | String s = targetFile.getAbsolutePath();
|
||
746 | desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, ""); |
||
747 | } else {
|
||
748 | desc = txtDesc.getText().trim(); |
||
749 | } |
||
750 | ISymbol s = (ISymbol) getSelectedObject(); |
||
751 | s.setDescription(desc); |
||
752 | |||
753 | // save it
|
||
754 | XMLEntity xml = s.getXMLEntity(); |
||
755 | if (!targetFile.
|
||
756 | getAbsolutePath(). |
||
757 | toLowerCase(). |
||
758 | endsWith(fExtension)) |
||
759 | targetFile = new File(targetFile.getAbsolutePath() + fExtension); |
||
760 | FileWriter writer;
|
||
761 | try {
|
||
762 | writer = new FileWriter(targetFile.getAbsolutePath()); |
||
763 | Marshaller m = new Marshaller(writer);
|
||
764 | m.setEncoding("ISO-8859-1");
|
||
765 | m.marshal(xml.getXmlTag()); |
||
766 | |||
767 | } catch (Exception ex) { |
||
768 | NotificationManager.addError( |
||
769 | PluginServices.getText(this, "save_error"), ex); |
||
770 | } |
||
771 | getJListSymbols().setModel(newListModel()); |
||
772 | } |
||
773 | } |
||
774 | |||
775 | |||
776 | 9745 | jaume | private class MyButtonListener implements ActionListener { |
777 | public void actionPerformed(ActionEvent e) { |
||
778 | |||
779 | JComponent comp = (JComponent) e.getSource(); |
||
780 | |||
781 | if ( comp.equals(getBtnProperties()) ) {
|
||
782 | // properties pressed
|
||
783 | 10679 | jaume | propertiesPressed(); |
784 | } else if ( comp.equals(getBtnNewSymbol()) ) { |
||
785 | // new pressed
|
||
786 | newPressed(); |
||
787 | 9745 | jaume | } else if ( comp.equals(getBtnResetSymbol()) ) { |
788 | // reset pressed
|
||
789 | 10679 | jaume | resetPressed(); |
790 | 9745 | jaume | } else if ( comp.equals(getBtnSaveSymbol()) ) { |
791 | // save pressed
|
||
792 | 10679 | jaume | savePressed(); |
793 | } else if (comp.equals(jcc1) && ((IPersistance) selectedElement).getXMLEntity() != null) { |
||
794 | if (selectedElement ==null) |
||
795 | return;
|
||
796 | 9745 | jaume | |
797 | 10679 | jaume | XMLEntity xml =((IPersistance) selectedElement).getXMLEntity(); |
798 | 9745 | jaume | |
799 | xml.putProperty("color", StringUtilities.color2String(jcc1.getColor()));
|
||
800 | 10679 | jaume | ((IPersistance) selectedElement).setXMLEntity(xml); |
801 | setSymbol(selectedElement); |
||
802 | } else if (comp.equals(jcc2) && ((IPersistance) selectedElement).getXMLEntity() != null) { |
||
803 | if (selectedElement ==null) |
||
804 | return;
|
||
805 | |||
806 | XMLEntity xml = ((IPersistance) selectedElement).getXMLEntity(); |
||
807 | |||
808 | 9745 | jaume | xml.putProperty("outline", StringUtilities.color2String(jcc2.getColor()));
|
809 | 10679 | jaume | ((IPersistance) selectedElement).setXMLEntity(xml); |
810 | setSymbol(selectedElement); |
||
811 | 9745 | jaume | } |
812 | |||
813 | SymbolSelector.this.repaint(); |
||
814 | } |
||
815 | } |
||
816 | 10679 | jaume | } |