root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleSelector.java @ 13008
History | View | Annotate | Download (11.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 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 |
|
42 |
/* CVS MESSAGES:
|
43 |
*
|
44 |
* $Id: StyleSelector.java 11919 2007-05-31 09:36:22Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.10 2007-05-31 09:36:22 jaume
|
47 |
* *** empty log message ***
|
48 |
*
|
49 |
* Revision 1.9 2007/05/10 09:47:50 jaume
|
50 |
* *** empty log message ***
|
51 |
*
|
52 |
* Revision 1.8 2007/05/08 15:44:07 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.7 2007/04/27 12:10:17 jaume
|
56 |
* *** empty log message ***
|
57 |
*
|
58 |
* Revision 1.6 2007/04/11 16:02:43 jaume
|
59 |
* file filter
|
60 |
*
|
61 |
* Revision 1.5 2007/04/05 16:08:34 jaume
|
62 |
* Styled labeling stuff
|
63 |
*
|
64 |
* Revision 1.4 2007/04/04 16:01:14 jaume
|
65 |
* *** empty log message ***
|
66 |
*
|
67 |
* Revision 1.2 2007/03/09 11:25:00 jaume
|
68 |
* Advanced symbology (start committing)
|
69 |
*
|
70 |
* Revision 1.1.2.4 2007/02/21 07:35:14 jaume
|
71 |
* *** empty log message ***
|
72 |
*
|
73 |
* Revision 1.1.2.3 2007/02/08 15:43:04 jaume
|
74 |
* some bug fixes in the editor and removed unnecessary imports
|
75 |
*
|
76 |
* Revision 1.1.2.2 2007/01/30 18:10:10 jaume
|
77 |
* start commiting labeling stuff
|
78 |
*
|
79 |
* Revision 1.1.2.1 2007/01/26 13:49:03 jaume
|
80 |
* *** empty log message ***
|
81 |
*
|
82 |
*
|
83 |
*/
|
84 |
package com.iver.cit.gvsig.gui.styling; |
85 |
|
86 |
import java.awt.Color; |
87 |
import java.awt.Component; |
88 |
import java.awt.Dimension; |
89 |
import java.awt.FlowLayout; |
90 |
import java.awt.Rectangle; |
91 |
import java.awt.event.ActionEvent; |
92 |
import java.awt.event.ActionListener; |
93 |
import java.io.File; |
94 |
import java.io.FileFilter; |
95 |
import java.io.FileWriter; |
96 |
import java.util.prefs.Preferences; |
97 |
|
98 |
import javax.swing.BorderFactory; |
99 |
import javax.swing.BoxLayout; |
100 |
import javax.swing.JComponent; |
101 |
import javax.swing.JFileChooser; |
102 |
import javax.swing.JLabel; |
103 |
import javax.swing.JList; |
104 |
import javax.swing.JPanel; |
105 |
import javax.swing.JTextField; |
106 |
import javax.swing.ListCellRenderer; |
107 |
import javax.swing.event.ListSelectionEvent; |
108 |
import javax.swing.event.ListSelectionListener; |
109 |
import javax.swing.event.TreeModelListener; |
110 |
import javax.swing.tree.TreeModel; |
111 |
import javax.swing.tree.TreePath; |
112 |
|
113 |
import org.exolab.castor.xml.Marshaller; |
114 |
import org.gvsig.gui.beans.swing.GridBagLayoutPanel; |
115 |
|
116 |
import com.iver.andami.PluginServices; |
117 |
import com.iver.andami.messages.NotificationManager; |
118 |
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle; |
119 |
import com.iver.cit.gvsig.fmap.core.styles.IStyle; |
120 |
import com.iver.cit.gvsig.gui.panels.ImageSizePanel; |
121 |
import com.iver.utiles.XMLEntity; |
122 |
|
123 |
/**
|
124 |
*
|
125 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
126 |
*
|
127 |
*/
|
128 |
public class StyleSelector extends SymbolSelector { |
129 |
private ImageSizePanel isp;
|
130 |
|
131 |
public StyleSelector(IStyle style, int shapeType) { |
132 |
this(style, shapeType, new SelectorFilter() { |
133 |
public boolean accepts(Object obj) { |
134 |
return obj instanceof IStyle; |
135 |
} |
136 |
}); |
137 |
} |
138 |
|
139 |
public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) { |
140 |
super(null, shapeType, filter); |
141 |
|
142 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
143 |
rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles")); |
144 |
if (!rootDir.exists())
|
145 |
rootDir.mkdir(); |
146 |
lblTitle.setText(PluginServices.getText(this, "label_styles")); |
147 |
treeRootName = PluginServices.getText(this, "style_library"); |
148 |
treeModel = new TreeModel() { |
149 |
final class MyFile extends File { |
150 |
public MyFile(String pathname) { |
151 |
super(pathname);
|
152 |
} |
153 |
|
154 |
public String toString() { |
155 |
if (this.equals(root)) |
156 |
return treeRootName;
|
157 |
String path = getAbsolutePath();
|
158 |
String prefixToRemove = rootDir.getAbsolutePath();
|
159 |
path = path.substring(prefixToRemove.length()+1, path.length());
|
160 |
return path;
|
161 |
} |
162 |
}; |
163 |
MyFile root = new MyFile(rootDir.getAbsolutePath());
|
164 |
|
165 |
private FileFilter ff = new FileFilter() { |
166 |
public boolean accept(File pathname) { |
167 |
return pathname.isDirectory();
|
168 |
} |
169 |
}; |
170 |
|
171 |
public Object getRoot() { |
172 |
return root;
|
173 |
} |
174 |
|
175 |
public int getChildCount(Object parent) { |
176 |
return ((File) parent).listFiles(ff).length; |
177 |
} |
178 |
|
179 |
public boolean isLeaf(Object node) { |
180 |
return getChildCount(node)==0; |
181 |
} |
182 |
|
183 |
public void addTreeModelListener(TreeModelListener l) { } |
184 |
public void removeTreeModelListener(TreeModelListener l) { } |
185 |
public void valueForPathChanged(TreePath path, Object newValue) {} |
186 |
|
187 |
public Object getChild(Object parent, int index) { |
188 |
return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath()); |
189 |
} |
190 |
|
191 |
public int getIndexOfChild(Object parent, Object child) { |
192 |
if (parent == null) |
193 |
return -1; |
194 |
File[] files = ((File) parent).listFiles(ff); |
195 |
for (int i = 0; i < files.length; i++) { |
196 |
if (files[i].equals((File) child)) |
197 |
return i;
|
198 |
} |
199 |
return -1; |
200 |
} |
201 |
|
202 |
|
203 |
|
204 |
}; |
205 |
getTreeFav().setModel(treeModel); |
206 |
|
207 |
ActionListener cancelAction = new ActionListener() { |
208 |
public void actionPerformed(ActionEvent e) { |
209 |
setStyle(null);
|
210 |
PluginServices.getMDIManager().closeWindow(StyleSelector.this); |
211 |
} |
212 |
}; |
213 |
okCancelPanel.setCancelButtonActionListener(cancelAction); |
214 |
|
215 |
} |
216 |
|
217 |
protected SymbolSelectorListModel newListModel() {
|
218 |
StyleSelectorListModel listModel = new StyleSelectorListModel(
|
219 |
dir, |
220 |
selectedElement, |
221 |
sFilter, |
222 |
StyleSelectorListModel.STYLE_FILE_EXTENSION); |
223 |
return listModel;
|
224 |
|
225 |
} |
226 |
|
227 |
protected JPanel getJPanelOptions() { |
228 |
if (jPanelOptions == null) { |
229 |
jPanelOptions = new GridBagLayoutPanel();
|
230 |
jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options"))); |
231 |
jPanelOptions.addComponent(getImageSizePanel()); |
232 |
|
233 |
} |
234 |
return jPanelOptions;
|
235 |
} |
236 |
|
237 |
private ImageSizePanel getImageSizePanel() {
|
238 |
if (isp == null) { |
239 |
isp = new ImageSizePanel();
|
240 |
isp.addActionListener(new ActionListener() { |
241 |
public void actionPerformed(ActionEvent e) { |
242 |
ILabelStyle st = (ILabelStyle) selectedElement; |
243 |
double[] sz = isp.getImageDimension(); |
244 |
st.setSize(sz[0], sz[1], isp.getUnitConversionFactor()); |
245 |
} |
246 |
}); |
247 |
} |
248 |
|
249 |
return isp;
|
250 |
} |
251 |
|
252 |
protected void updateOptionsPanel() { |
253 |
IStyle s = ((IStyle) jListSymbols.getSelectedValue()); |
254 |
if (s instanceof ILabelStyle) { |
255 |
ILabelStyle lab = (ILabelStyle) s; |
256 |
Rectangle r = lab.getBounds(null); |
257 |
getImageSizePanel().setImageDimension(r.getWidth(), r.getHeight()); |
258 |
|
259 |
} |
260 |
} |
261 |
|
262 |
|
263 |
/**
|
264 |
* This method initializes jList
|
265 |
*
|
266 |
* @return javax.swing.JList
|
267 |
*/
|
268 |
protected JList getJListSymbols() { |
269 |
if (jListSymbols == null) { |
270 |
jListSymbols = new JList(); |
271 |
jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); |
272 |
jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
|
273 |
jListSymbols.setVisibleRowCount(-1);
|
274 |
jListSymbols.addListSelectionListener(new ListSelectionListener() { |
275 |
public void valueChanged(ListSelectionEvent e) { |
276 |
setStyle(jListSymbols.getSelectedValue()); |
277 |
updateOptionsPanel(); |
278 |
} |
279 |
}); |
280 |
ListCellRenderer renderer = new ListCellRenderer() { |
281 |
private Color mySelectedBGColor = new Color(255,145,100,255); |
282 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
283 |
IStyle sty = (IStyle) value; |
284 |
JPanel pnl = new JPanel(); |
285 |
BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS); |
286 |
pnl.setLayout(layout); |
287 |
Color bgColor = (isSelected) ? mySelectedBGColor
|
288 |
: getJListSymbols().getBackground(); |
289 |
|
290 |
pnl.setBackground(bgColor); |
291 |
StylePreviewer sp = new StylePreviewer();
|
292 |
sp.setShowOutline(false);
|
293 |
sp.setAlignmentX(Component.CENTER_ALIGNMENT);
|
294 |
sp.setPreferredSize(new Dimension(50, 50)); |
295 |
sp.setStyle(sty); |
296 |
sp.setBackground(bgColor); |
297 |
pnl.add(sp); |
298 |
JLabel lbl = new JLabel(sty.getDescription()); |
299 |
lbl.setBackground(bgColor); |
300 |
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
|
301 |
pnl.add(lbl); |
302 |
|
303 |
return pnl;
|
304 |
} |
305 |
|
306 |
}; |
307 |
jListSymbols.setCellRenderer(renderer); |
308 |
} |
309 |
return jListSymbols;
|
310 |
} |
311 |
|
312 |
protected void setStyle(Object selectedValue) { |
313 |
selectedElement = selectedValue; |
314 |
((StylePreviewer) jPanelPreview).setStyle((IStyle) selectedValue); |
315 |
doLayout(); |
316 |
repaint(); |
317 |
} |
318 |
|
319 |
protected void propertiesPressed() { |
320 |
StyleEditor se = new StyleEditor((IStyle) selectedElement);
|
321 |
PluginServices.getMDIManager().addWindow(se); |
322 |
setStyle(se.getStyle()); |
323 |
} |
324 |
|
325 |
protected void savePressed() { |
326 |
if (getSelectedObject() ==null) |
327 |
return;
|
328 |
|
329 |
JFileChooser jfc = new JFileChooser(rootDir); |
330 |
javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
|
331 |
public boolean accept(File f) { |
332 |
return f.getAbsolutePath().
|
333 |
toLowerCase(). |
334 |
endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION); |
335 |
} |
336 |
|
337 |
public String getDescription() { |
338 |
return PluginServices.getText(
|
339 |
this, "gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")"; |
340 |
} |
341 |
}; |
342 |
jfc.setFileFilter(ff); |
343 |
JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5)); |
344 |
accessory.add(new JLabel(PluginServices.getText(this, "enter_description"))); |
345 |
JTextField txtDesc = new JTextField(25); |
346 |
accessory.add(txtDesc); |
347 |
jfc.setAccessory(accessory); |
348 |
if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { |
349 |
File targetFile = jfc.getSelectedFile();
|
350 |
|
351 |
String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
|
352 |
|
353 |
// apply description
|
354 |
String desc;
|
355 |
if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) { |
356 |
// default to file name
|
357 |
String s = targetFile.getAbsolutePath();
|
358 |
desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, ""); |
359 |
} else {
|
360 |
desc = txtDesc.getText().trim(); |
361 |
} |
362 |
IStyle s = (IStyle) getSelectedObject(); |
363 |
s.setDescription(desc); |
364 |
|
365 |
// save it
|
366 |
XMLEntity xml = s.getXMLEntity(); |
367 |
if (!targetFile.
|
368 |
getAbsolutePath(). |
369 |
toLowerCase(). |
370 |
endsWith(fExtension)) |
371 |
targetFile = new File(targetFile.getAbsolutePath() + fExtension); |
372 |
FileWriter writer;
|
373 |
try {
|
374 |
writer = new FileWriter(targetFile.getAbsolutePath()); |
375 |
Marshaller m = new Marshaller(writer);
|
376 |
m.setEncoding("ISO-8859-1");
|
377 |
m.marshal(xml.getXmlTag()); |
378 |
|
379 |
} catch (Exception ex) { |
380 |
NotificationManager.addError( |
381 |
PluginServices.getText(this, "save_error"), ex); |
382 |
} |
383 |
getJListSymbols().setModel(newListModel()); |
384 |
} |
385 |
} |
386 |
|
387 |
/**
|
388 |
* This method initializes jPanel1
|
389 |
*
|
390 |
* @return javax.swing.JPanel
|
391 |
*/
|
392 |
protected JComponent getJPanelPreview() { |
393 |
if (jPanelPreview == null) { |
394 |
jPanelPreview = new StylePreviewer();
|
395 |
jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100)); |
396 |
jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
397 |
((StylePreviewer) jPanelPreview).setShowOutline(true);
|
398 |
} |
399 |
return jPanelPreview;
|
400 |
} |
401 |
} |