root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleEditor.java @ 24962
History | View | Annotate | Download (8.85 KB)
1 | 18623 | jdominguez | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | 11055 | jaume | *
|
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 | 18623 | jdominguez | * Av. Blasco Ib??ez, 50
|
24 | 11055 | jaume | * 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 | package com.iver.cit.gvsig.gui.styling; |
||
43 | |||
44 | import java.awt.BorderLayout; |
||
45 | import java.awt.FlowLayout; |
||
46 | 11073 | jaume | import java.awt.event.ActionEvent; |
47 | import java.awt.event.ActionListener; |
||
48 | 18623 | jdominguez | import java.lang.reflect.Constructor; |
49 | import java.util.ArrayList; |
||
50 | import java.util.Date; |
||
51 | import java.util.Hashtable; |
||
52 | import java.util.Iterator; |
||
53 | 11055 | jaume | |
54 | 12982 | jaume | import javax.swing.AbstractButton; |
55 | 11055 | jaume | import javax.swing.BorderFactory; |
56 | import javax.swing.BoxLayout; |
||
57 | import javax.swing.ButtonGroup; |
||
58 | 18623 | jdominguez | import javax.swing.JComponent; |
59 | 11055 | jaume | import javax.swing.JLabel; |
60 | import javax.swing.JPanel; |
||
61 | import javax.swing.JTextField; |
||
62 | |||
63 | 20994 | jmvivo | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory; |
64 | import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle; |
||
65 | 11055 | jaume | import org.gvsig.gui.beans.AcceptCancelPanel; |
66 | |||
67 | import com.iver.andami.PluginServices; |
||
68 | 18623 | jdominguez | import com.iver.andami.messages.NotificationManager; |
69 | 11055 | jaume | import com.iver.andami.ui.mdiManager.IWindow; |
70 | import com.iver.andami.ui.mdiManager.WindowInfo; |
||
71 | 23176 | vcaballero | import com.iver.utiles.XMLException; |
72 | 11055 | jaume | |
73 | /**
|
||
74 | 22752 | vcaballero | * Implements the panel which is composed by a previsualization of the style for the label
|
75 | * and the different tools that the user has to modify that style in order to improve the
|
||
76 | * representation of the labelling in the layer. The different available tools and the preview
|
||
77 | * of the style change according with the geometry of the layer.
|
||
78 | *
|
||
79 | 11055 | jaume | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
80 | */
|
||
81 | 11073 | jaume | public class StyleEditor extends JPanel implements IWindow, ActionListener { |
82 | 18623 | jdominguez | private static final long serialVersionUID = -4002620456610864510L; |
83 | 11055 | jaume | private JPanel pnlNorth = null; |
84 | private JPanel jPanel = null; |
||
85 | private JPanel pnlCenter = null; |
||
86 | private AcceptCancelPanel pnlButtons = null; |
||
87 | private JLabel lblTitle; |
||
88 | private StylePreviewer preview;
|
||
89 | private JPanel pnlTools; |
||
90 | private JTextField txtDesc; |
||
91 | 22752 | vcaballero | private Hashtable<AbstractButton, EditorTool> tools = new Hashtable<AbstractButton, EditorTool>(); |
92 | 18623 | jdominguez | private static ArrayList<Class> installedTools = new ArrayList<Class>(); |
93 | private EditorTool prevTool;
|
||
94 | 22752 | vcaballero | |
95 | 11073 | jaume | private ActionListener okAction = new ActionListener() { |
96 | public void actionPerformed(ActionEvent e) { |
||
97 | PluginServices.getMDIManager().closeWindow(StyleEditor.this); |
||
98 | } |
||
99 | }; |
||
100 | 22752 | vcaballero | |
101 | 11073 | jaume | private ActionListener cancelAction = new ActionListener() { |
102 | public void actionPerformed(ActionEvent e) { |
||
103 | getStylePreviewer().setStyle(null);
|
||
104 | PluginServices.getMDIManager().closeWindow(StyleEditor.this); |
||
105 | }; |
||
106 | }; |
||
107 | 22752 | vcaballero | /**
|
108 | * Constructor method
|
||
109 | *
|
||
110 | * @param style, an specific style for the labelling of the layer.This style depends on the
|
||
111 | * geometry of the layer.
|
||
112 | *
|
||
113 | */
|
||
114 | 11055 | jaume | public StyleEditor(IStyle style) {
|
115 | if (style!=null) { |
||
116 | 23176 | vcaballero | IStyle sty=null;
|
117 | try {
|
||
118 | sty = SymbologyFactory.createStyleFromXML(style.getXMLEntity(), style.getDescription()); |
||
119 | } catch (XMLException e) {
|
||
120 | NotificationManager.addWarning("Symbol layer", e);
|
||
121 | } |
||
122 | 11055 | jaume | getStylePreviewer().setStyle(sty); |
123 | String desc = sty.getDescription();
|
||
124 | getTxtDesc().setText(desc); |
||
125 | } else {
|
||
126 | getTxtDesc().setText(PluginServices.getText(this, "new_style")); |
||
127 | } |
||
128 | initialize(); |
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | * This method initializes this
|
||
133 | *
|
||
134 | */
|
||
135 | private void initialize() { |
||
136 | this.setLayout(new BorderLayout()); |
||
137 | this.setSize(417,284); |
||
138 | this.add(getPnlNorth(), java.awt.BorderLayout.NORTH);
|
||
139 | this.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
|
||
140 | this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
|
||
141 | 22752 | vcaballero | |
142 | 11055 | jaume | } |
143 | 22752 | vcaballero | /**
|
144 | * Sets the previewer of the style in the panel
|
||
145 | *
|
||
146 | * @param sty, style to be previewed
|
||
147 | *
|
||
148 | */
|
||
149 | 18623 | jdominguez | public void setStyle(IStyle sty) { |
150 | preview.setStyle(sty); |
||
151 | } |
||
152 | 11055 | jaume | |
153 | /**
|
||
154 | 22752 | vcaballero | * This method initializes the north JPanel
|
155 | 11055 | jaume | *
|
156 | * @return javax.swing.JPanel
|
||
157 | */
|
||
158 | private JPanel getPnlNorth() { |
||
159 | if (pnlNorth == null) { |
||
160 | pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEADING)); |
||
161 | lblTitle = new JLabel(PluginServices.getText(this, "editing")); |
||
162 | pnlNorth.add(lblTitle); |
||
163 | pnlNorth.add(getTxtDesc()); |
||
164 | } |
||
165 | return pnlNorth;
|
||
166 | } |
||
167 | |||
168 | 22752 | vcaballero | /**
|
169 | * This method initializes the textDesc JTextfield
|
||
170 | *
|
||
171 | * @return javax.swing.JTextfield
|
||
172 | */
|
||
173 | 11055 | jaume | private JTextField getTxtDesc() { |
174 | if (txtDesc == null) { |
||
175 | txtDesc = new JTextField(30); |
||
176 | 22752 | vcaballero | txtDesc.addActionListener(this);
|
177 | 11055 | jaume | |
178 | } |
||
179 | |||
180 | return txtDesc;
|
||
181 | } |
||
182 | |||
183 | /**
|
||
184 | 22752 | vcaballero | * This method initializes the center JPanel
|
185 | 11055 | jaume | *
|
186 | * @return javax.swing.JPanel
|
||
187 | */
|
||
188 | private JPanel getPnlCenter() { |
||
189 | if (pnlCenter == null) { |
||
190 | pnlCenter = new JPanel(new BorderLayout()); |
||
191 | JPanel aux = new JPanel(); |
||
192 | aux.add(getStylePreviewer()); |
||
193 | pnlCenter.add(getStylePreviewer(), BorderLayout.CENTER);
|
||
194 | pnlCenter.add(getPnlTools(), BorderLayout.EAST);
|
||
195 | } |
||
196 | return pnlCenter;
|
||
197 | } |
||
198 | 22752 | vcaballero | /**
|
199 | * This method initializes the StylePreviewer
|
||
200 | *
|
||
201 | * @return StylePreviewer for the label
|
||
202 | */
|
||
203 | 11073 | jaume | private StylePreviewer getStylePreviewer() {
|
204 | if (preview == null) { |
||
205 | preview = new StylePreviewer();
|
||
206 | preview.setShowOutline(true);
|
||
207 | } |
||
208 | |||
209 | return preview;
|
||
210 | } |
||
211 | |||
212 | /**
|
||
213 | 22752 | vcaballero | * This method initializes pnlButtons JPanel
|
214 | 11073 | jaume | *
|
215 | * @return javax.swing.JPanel
|
||
216 | */
|
||
217 | private JPanel getPnlButtons() { |
||
218 | if (pnlButtons == null) { |
||
219 | pnlButtons = new AcceptCancelPanel(okAction, cancelAction);
|
||
220 | } |
||
221 | return pnlButtons;
|
||
222 | } |
||
223 | |||
224 | public WindowInfo getWindowInfo() {
|
||
225 | WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
|
||
226 | wi.setTitle(PluginServices.getText(this, "edit_style")); |
||
227 | wi.setWidth(getWidth()+10);
|
||
228 | wi.setHeight(getHeight()); |
||
229 | return wi;
|
||
230 | } |
||
231 | |||
232 | 22752 | vcaballero | /**
|
233 | * Obtains the style for the label
|
||
234 | *
|
||
235 | * @return IStyle
|
||
236 | */
|
||
237 | 11073 | jaume | public IStyle getStyle() {
|
238 | 22752 | vcaballero | IStyle style = getStylePreviewer().getStyle(); |
239 | if (style != null) |
||
240 | style.setDescription(txtDesc.getText()); |
||
241 | return style;
|
||
242 | 11073 | jaume | } |
243 | |||
244 | public void actionPerformed(ActionEvent e) { |
||
245 | 18623 | jdominguez | AbstractButton srcButton = (AbstractButton) e.getSource(); |
246 | EditorTool currTool = tools.get(srcButton); |
||
247 | if (currTool != null) { |
||
248 | prevTool = preview.setEditorTool(currTool); |
||
249 | 11073 | jaume | } |
250 | } |
||
251 | 22752 | vcaballero | /**
|
252 | * This method initializes pnlTools JPanel
|
||
253 | *
|
||
254 | * @return javax.swing.JPanel
|
||
255 | */
|
||
256 | 11055 | jaume | private JPanel getPnlTools() { |
257 | if (pnlTools == null) { |
||
258 | pnlTools = new JPanel(); |
||
259 | pnlTools.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "tools"))); |
||
260 | pnlTools.setLayout(new BoxLayout(pnlTools, BoxLayout.Y_AXIS)); |
||
261 | 22752 | vcaballero | |
262 | 18623 | jdominguez | ArrayList<EditorTool> availableTools = new ArrayList<EditorTool>(); |
263 | IStyle sty = preview.getStyle(); |
||
264 | Class<?> editorClazz = null; |
||
265 | Class[] constrLocator = new Class[] {JComponent.class}; |
||
266 | Object[] constrInitargs = new Object[] { this }; |
||
267 | try {
|
||
268 | for (Iterator<Class> iterator = installedTools.iterator(); iterator |
||
269 | .hasNext();) { |
||
270 | editorClazz = iterator.next(); |
||
271 | Constructor<EditorTool> constructor = (Constructor<EditorTool>) editorClazz.getConstructor(constrLocator); |
||
272 | EditorTool editorTool = constructor.newInstance(constrInitargs); |
||
273 | if (editorTool.isSuitableFor(sty)) {
|
||
274 | editorTool.setModel(sty); |
||
275 | availableTools.add(editorTool); |
||
276 | } |
||
277 | 11055 | jaume | |
278 | 18623 | jdominguez | } |
279 | 11055 | jaume | |
280 | 18623 | jdominguez | ButtonGroup group = new ButtonGroup(); |
281 | 22752 | vcaballero | for (Iterator<EditorTool> iterator = availableTools.iterator(); |
282 | 18623 | jdominguez | iterator.hasNext();) { |
283 | EditorTool editorTool = iterator.next(); |
||
284 | AbstractButton button = editorTool.getButton();
|
||
285 | button.addActionListener(this);
|
||
286 | pnlTools.add(button); |
||
287 | group.add(button); |
||
288 | 22752 | vcaballero | |
289 | 18623 | jdominguez | tools.put(button, editorTool); |
290 | 22752 | vcaballero | |
291 | 18623 | jdominguez | } |
292 | } catch (Exception e) { |
||
293 | NotificationManager.addWarning(PluginServices.getText(this, "could_not_initialize_editor_")+"'"+editorClazz+"'"+ |
||
294 | " ["+new Date(System.currentTimeMillis()).toString()+"]"); |
||
295 | } |
||
296 | 22752 | vcaballero | } |
297 | 11055 | jaume | return pnlTools;
|
298 | } |
||
299 | 22752 | vcaballero | /**
|
300 | * Sets the tool to be used with the previous one
|
||
301 | *
|
||
302 | */
|
||
303 | 18623 | jdominguez | public void restorePreviousTool() { |
304 | preview.setEditorTool(prevTool); |
||
305 | 11055 | jaume | } |
306 | 22752 | vcaballero | /**
|
307 | * Adds a new tool for the style
|
||
308 | *
|
||
309 | * @param styleEditorClass, the new tool to be added
|
||
310 | */
|
||
311 | 18623 | jdominguez | public static void addEditorTool(Class styleEditorClass) { |
312 | installedTools.add(styleEditorClass); |
||
313 | 11055 | jaume | } |
314 | |||
315 | } // @jve:decl-index=0:visual-constraint="10,10" |