Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleEditor.java @ 22951

History | View | Annotate | Download (9.02 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.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48
import java.awt.LayoutManager;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.lang.reflect.Constructor;
52
import java.util.ArrayList;
53
import java.util.Date;
54
import java.util.Hashtable;
55
import java.util.Iterator;
56

    
57
import javax.swing.AbstractButton;
58
import javax.swing.BorderFactory;
59
import javax.swing.BoxLayout;
60
import javax.swing.ButtonGroup;
61
import javax.swing.JComponent;
62
import javax.swing.JLabel;
63
import javax.swing.JPanel;
64
import javax.swing.JTextField;
65
import javax.swing.border.Border;
66
import javax.swing.border.TitledBorder;
67

    
68
import org.gvsig.gui.beans.AcceptCancelPanel;
69

    
70
import com.iver.andami.PluginServices;
71
import com.iver.andami.messages.NotificationManager;
72
import com.iver.andami.ui.mdiManager.IWindow;
73
import com.iver.andami.ui.mdiManager.WindowInfo;
74
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
75
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
76

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

    
100
        private ActionListener okAction = new ActionListener() {
101
                public void actionPerformed(ActionEvent e) {
102
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
103
                }
104
        };
105

    
106
        private ActionListener cancelAction = new ActionListener() {
107
                public void actionPerformed(ActionEvent e) {
108
                        getStylePreviewer().setStyle(oldStyle);
109
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
110
                };
111
        };
112

    
113
        /**
114
         * Constructor method
115
         *
116
         * @param style, an specific style for the labelling of the layer.This style depends on the
117
         * geometry of the layer.
118
         *
119
         */
120
        public StyleEditor(IStyle style) {
121
                if (style!=null) {
122
                        oldStyle = style;
123
                        IStyle sty = SymbologyFactory.createStyleFromXML(style.getXMLEntity(), style.getDescription());
124
                        getStylePreviewer().setStyle(sty);
125
                        String desc = sty.getDescription();
126
                        getTxtDesc().setText(desc);
127
                } else {
128
                        getTxtDesc().setText(PluginServices.getText(this, "new_style"));
129
                }
130
                initialize();
131
        }
132

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

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

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

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

    
180
                }
181

    
182
                return txtDesc;
183
        }
184

    
185
        /**
186
         * This method initializes the center JPanel
187
         *
188
         * @return javax.swing.JPanel
189
         */
190
        private JPanel getPnlCenter() {
191
                if (pnlCenter == null) {
192
                        pnlCenter = new JPanel(new BorderLayout());
193
                        JPanel aux = new JPanel();
194
                        aux.add(getStylePreviewer());
195
                        pnlCenter.add(getStylePreviewer(), BorderLayout.CENTER);
196
                        pnlCenter.add(getPnlTools(), BorderLayout.EAST);
197
                }
198
                return pnlCenter;
199
        }
200

    
201
        /**
202
         * This method initializes the StylePreviewer
203
         *
204
         * @return StylePreviewer for the label
205
         */
206
        public StylePreviewer getStylePreviewer() {
207
                if (preview == null) {
208
                        preview = new StylePreviewer();
209
                        preview.setShowOutline(true);
210
                }
211

    
212
                return preview;
213
        }
214

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

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

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

    
247
        public void actionPerformed(ActionEvent e) {
248
                AbstractButton srcButton = (AbstractButton) e.getSource();
249
                EditorTool currTool = tools.get(srcButton);
250
                if (currTool != null) {
251
                        prevTool = preview.setEditorTool(currTool);
252
                }
253
        }
254
        /**
255
         * This method initializes pnlTools JPanel
256
         *
257
         * @return javax.swing.JPanel
258
         */
259
        private JPanel getPnlTools() {
260
                if (pnlTools == null) {
261
                        pnlTools = new JPanel();
262
                        pnlTools.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "tools")));
263
                        pnlTools.setLayout(new BoxLayout(pnlTools, BoxLayout.Y_AXIS));
264
                        pnlTools.setPreferredSize(new Dimension(this.getWidth()/4,0));
265
                        GridLayout grid = new GridLayout(0,1,1,1);
266
                        pnlTools.setLayout(grid);
267

    
268
                        ArrayList<EditorTool> availableTools = new ArrayList<EditorTool>();
269
                        IStyle sty = preview.getStyle();
270
                        Class<?> editorClazz = null;
271
                        Class[] constrLocator = new Class[] {JComponent.class};
272
                        Object[] constrInitargs = new Object[] { this };
273
                        try {
274
                                for (Iterator<Class> iterator = installedTools.iterator(); iterator
275
                                .hasNext();) {
276
                                        editorClazz = iterator.next();
277
                                        Constructor<EditorTool> constructor = (Constructor<EditorTool>) editorClazz.getConstructor(constrLocator);
278
                                        EditorTool editorTool = constructor.newInstance(constrInitargs);
279
                                        if (editorTool.isSuitableFor(sty)) {
280
                                                editorTool.setModel(sty);
281
                                                availableTools.add(editorTool);
282
                                        }
283

    
284
                                }
285

    
286
                                ButtonGroup group = new ButtonGroup();
287
                                for (Iterator<EditorTool> iterator = availableTools.iterator();
288
                                iterator.hasNext();) {
289
                                        EditorTool editorTool = iterator.next();
290
                                        AbstractButton button = editorTool.getButton();
291
                                        button.addActionListener(this);
292

    
293
                                        pnlTools.add(button);
294
                                        group.add(button);
295

    
296
                                        tools.put(button, editorTool);
297

    
298
                                }
299

    
300

    
301

    
302
                        } catch (Exception e) {
303
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_initialize_editor_")+"'"+editorClazz+"'"+
304
                                                " ["+new Date(System.currentTimeMillis()).toString()+"]");
305
                        }
306
                }
307
                return pnlTools;
308
        }
309

    
310
        /**
311
         * Sets the tool to be used with the previous one
312
         *
313
         */
314
        public void restorePreviousTool() {
315
                preview.setEditorTool(prevTool);
316
        }
317
        /**
318
         * Adds a new tool for the style
319
         *
320
         * @param styleEditorClass, the new tool to be added
321
         */
322
        public static void addEditorTool(Class styleEditorClass) {
323
                installedTools.add(styleEditorClass);
324
        }
325

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