Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / styling / StyleEditor.java @ 30011

History | View | Annotate | Download (8.81 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
package org.gvsig.app.gui.styling;
43

    
44
import java.awt.BorderLayout;
45
import java.awt.FlowLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
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

    
54
import javax.swing.AbstractButton;
55
import javax.swing.BorderFactory;
56
import javax.swing.BoxLayout;
57
import javax.swing.ButtonGroup;
58
import javax.swing.JComponent;
59
import javax.swing.JLabel;
60
import javax.swing.JPanel;
61
import javax.swing.JTextField;
62

    
63
import org.gvsig.andami.PluginServices;
64
import org.gvsig.andami.messages.NotificationManager;
65
import org.gvsig.andami.ui.mdiManager.IWindow;
66
import org.gvsig.andami.ui.mdiManager.WindowInfo;
67
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
68
import org.gvsig.gui.beans.AcceptCancelPanel;
69
import org.gvsig.utils.XMLException;
70

    
71

    
72
/**
73
 * Implements the panel which is composed by a previsualization of the style for the label
74
 * and the different tools that the user has to modify that style in order to improve the
75
 * representation of the labelling in the layer. The different available tools and the preview
76
 * of the style change according with the geometry of the layer.
77
 *
78
 * @author jaume dominguez faus - jaume.dominguez@iver.es
79
 */
80
public class StyleEditor extends JPanel implements IWindow, ActionListener {
81
        private static final long serialVersionUID = -4002620456610864510L;
82
        private JPanel pnlNorth = null;
83
        private JPanel jPanel = null;
84
        private JPanel pnlCenter = null;
85
        private AcceptCancelPanel pnlButtons = null;
86
        private JLabel lblTitle;
87
        private StylePreviewer preview;
88
        private JPanel pnlTools;
89
        private JTextField txtDesc;
90
        private Hashtable<AbstractButton, EditorTool> tools = new Hashtable<AbstractButton, EditorTool>();
91
        private static ArrayList<Class> installedTools = new ArrayList<Class>();
92
        private EditorTool prevTool;
93

    
94
        private ActionListener okAction = new ActionListener() {
95
                public void actionPerformed(ActionEvent e) {
96
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
97
                }
98
        };
99

    
100
        private ActionListener cancelAction = new ActionListener() {
101
                public void actionPerformed(ActionEvent e) {
102
                        getStylePreviewer().setStyle(null);
103
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
104
                };
105
        };
106
        /**
107
         * Constructor method
108
         *
109
         * @param style, an specific style for the labelling of the layer.This style depends on the
110
         * geometry of the layer.
111
         *
112
         */
113
        public StyleEditor(IStyle style) {
114
                if (style!=null) {
115
                        IStyle sty=null;
116
                        try {
117
                                sty = (IStyle) style.clone(); 
118
                        } catch (CloneNotSupportedException e) {
119
                                NotificationManager.addWarning("Symbol layer", e);
120
                        }
121
                        getStylePreviewer().setStyle(sty);
122
                        String desc = sty.getDescription();
123
                        getTxtDesc().setText(desc);
124
                } else {
125
                        getTxtDesc().setText(PluginServices.getText(this, "new_style"));
126
                }
127
                initialize();
128
        }
129

    
130
        /**
131
         * This method initializes this
132
         *
133
         */
134
        private void initialize() {
135
        this.setLayout(new BorderLayout());
136
        this.setSize(417,284);
137
        this.add(getPnlNorth(), java.awt.BorderLayout.NORTH);
138
        this.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
139
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
140

    
141
        }
142
        /**
143
         * Sets the previewer of the style in the panel
144
         *
145
         * @param sty, style to be previewed
146
         *
147
         */
148
        public void setStyle(IStyle sty) {
149
                preview.setStyle(sty);
150
        }
151

    
152
        /**
153
         * This method initializes the north JPanel
154
         *
155
         * @return javax.swing.JPanel
156
         */
157
        private JPanel getPnlNorth() {
158
                if (pnlNorth == null) {
159
                        pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEADING));
160
                        lblTitle = new JLabel(PluginServices.getText(this, "editing"));
161
                        pnlNorth.add(lblTitle);
162
                        pnlNorth.add(getTxtDesc());
163
                }
164
                return pnlNorth;
165
        }
166

    
167
        /**
168
         * This method initializes the textDesc JTextfield
169
         *
170
         * @return javax.swing.JTextfield
171
         */
172
        private JTextField getTxtDesc() {
173
                if (txtDesc == null) {
174
                        txtDesc = new JTextField(30);
175
                        txtDesc.addActionListener(this);
176

    
177
                }
178

    
179
                return txtDesc;
180
        }
181

    
182
        /**
183
         * This method initializes the center JPanel
184
         *
185
         * @return javax.swing.JPanel
186
         */
187
        private JPanel getPnlCenter() {
188
                if (pnlCenter == null) {
189
                        pnlCenter = new JPanel(new BorderLayout());
190
                        JPanel aux = new JPanel();
191
                        aux.add(getStylePreviewer());
192
                        pnlCenter.add(getStylePreviewer(), BorderLayout.CENTER);
193
                        pnlCenter.add(getPnlTools(), BorderLayout.EAST);
194
                }
195
                return pnlCenter;
196
        }
197
        /**
198
         * This method initializes the StylePreviewer
199
         *
200
         * @return StylePreviewer for the label
201
         */
202
        private StylePreviewer getStylePreviewer() {
203
                if (preview == null) {
204
                        preview = new StylePreviewer();
205
                        preview.setShowOutline(true);
206
                }
207

    
208
                return preview;
209
        }
210

    
211
        /**
212
         * This method initializes pnlButtons JPanel
213
         *
214
         * @return javax.swing.JPanel
215
         */
216
        private JPanel getPnlButtons() {
217
                if (pnlButtons == null) {
218
                        pnlButtons = new AcceptCancelPanel(okAction, cancelAction);
219
                }
220
                return pnlButtons;
221
        }
222

    
223
        public WindowInfo getWindowInfo() {
224
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
225
                wi.setTitle(PluginServices.getText(this, "edit_style"));
226
                wi.setWidth(getWidth()+10);
227
                wi.setHeight(getHeight());
228
                return wi;
229
        }
230

    
231
        /**
232
         * Obtains the style for the label
233
         *
234
         * @return IStyle
235
         */
236
        public IStyle getStyle() {
237
                IStyle style = getStylePreviewer().getStyle();
238
                if (style != null) {
239
                        style.setDescription(txtDesc.getText());
240
                }
241
                return style;
242
        }
243

    
244
        public void actionPerformed(ActionEvent e) {
245
                AbstractButton srcButton = (AbstractButton) e.getSource();
246
                EditorTool currTool = tools.get(srcButton);
247
                if (currTool != null) {
248
                        prevTool = preview.setEditorTool(currTool);
249
                }
250
        }
251
        /**
252
         * This method initializes pnlTools JPanel
253
         *
254
         * @return javax.swing.JPanel
255
         */
256
        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

    
262
                        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

    
278
                                }
279

    
280
                                ButtonGroup group = new ButtonGroup();
281
                                for (Iterator<EditorTool> iterator = availableTools.iterator();
282
                                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

    
289
                                        tools.put(button, editorTool);
290

    
291
                                }
292
                        } catch (Exception e) {
293
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_initialize_editor_")+"'"+editorClazz+"'"+
294
                                                " ["+new Date(System.currentTimeMillis()).toString()+"]");
295
                        }
296
                }
297
                return pnlTools;
298
        }
299
        /**
300
         * Sets the tool to be used with the previous one
301
         *
302
         */
303
        public void restorePreviousTool() {
304
                preview.setEditorTool(prevTool);
305
        }
306
        /**
307
         * Adds a new tool for the style
308
         *
309
         * @param styleEditorClass, the new tool to be added
310
         */
311
        public static void addEditorTool(Class styleEditorClass) {
312
                installedTools.add(styleEditorClass);
313
        }
314
        public Object getWindowProfile() {
315
                return WindowInfo.DIALOG_PROFILE;
316
        }
317
}  //  @jve:decl-index=0:visual-constraint="10,10"