Statistics
| Revision:

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
/* 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 com.iver.cit.gvsig.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.fmap.mapcontext.rendering.symbols.SymbologyFactory;
64
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
65
import org.gvsig.gui.beans.AcceptCancelPanel;
66

    
67
import com.iver.andami.PluginServices;
68
import com.iver.andami.messages.NotificationManager;
69
import com.iver.andami.ui.mdiManager.IWindow;
70
import com.iver.andami.ui.mdiManager.WindowInfo;
71
import com.iver.utiles.XMLException;
72

    
73
/**
74
 * 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
 * @author jaume dominguez faus - jaume.dominguez@iver.es
80
 */
81
public class StyleEditor extends JPanel implements IWindow, ActionListener {
82
        private static final long serialVersionUID = -4002620456610864510L;
83
        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
        private Hashtable<AbstractButton, EditorTool> tools = new Hashtable<AbstractButton, EditorTool>();
92
        private static ArrayList<Class> installedTools = new ArrayList<Class>();
93
        private EditorTool prevTool;
94

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

    
101
        private ActionListener cancelAction = new ActionListener() {
102
                public void actionPerformed(ActionEvent e) {
103
                        getStylePreviewer().setStyle(null);
104
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
105
                };
106
        };
107
        /**
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
        public StyleEditor(IStyle style) {
115
                if (style!=null) {
116
                        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
                        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

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

    
153
        /**
154
         * This method initializes the north JPanel
155
         *
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
        /**
169
         * This method initializes the textDesc JTextfield
170
         *
171
         * @return javax.swing.JTextfield
172
         */
173
        private JTextField getTxtDesc() {
174
                if (txtDesc == null) {
175
                        txtDesc = new JTextField(30);
176
                        txtDesc.addActionListener(this);
177

    
178
                }
179

    
180
                return txtDesc;
181
        }
182

    
183
        /**
184
         * This method initializes the center JPanel
185
         *
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
        /**
199
         * This method initializes the StylePreviewer
200
         *
201
         * @return StylePreviewer for the label
202
         */
203
        private StylePreviewer getStylePreviewer() {
204
                if (preview == null) {
205
                        preview = new StylePreviewer();
206
                        preview.setShowOutline(true);
207
                }
208

    
209
                return preview;
210
        }
211

    
212
        /**
213
         * This method initializes pnlButtons JPanel
214
         *
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
        /**
233
         * Obtains the style for the label
234
         *
235
         * @return IStyle
236
         */
237
        public IStyle getStyle() {
238
                IStyle style = getStylePreviewer().getStyle();
239
                if (style != null)
240
                        style.setDescription(txtDesc.getText());
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

    
315
}  //  @jve:decl-index=0:visual-constraint="10,10"