Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / StyleEditor.java @ 42256

History | View | Annotate | Download (9.96 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui.styling;
25

    
26
import org.gvsig.andami.PluginServices;
27
import org.gvsig.andami.messages.NotificationManager;
28
import org.gvsig.andami.ui.mdiManager.IWindow;
29
import org.gvsig.andami.ui.mdiManager.WindowInfo;
30
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
31
import org.gvsig.gui.beans.AcceptCancelPanel;
32
import org.gvsig.i18n.Messages;
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.i18n.I18nManager;
35

    
36
import javax.swing.*;
37
import javax.swing.border.EmptyBorder;
38

    
39
import java.awt.*;
40
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionListener;
42
import java.lang.reflect.Constructor;
43
import java.util.*;
44

    
45

    
46
/**
47
 * Implements the panel which is composed by a previsualization of the style for the label
48
 * and the different tools that the user has to modify that style in order to improve the
49
 * representation of the labelling in the layer. The different available tools and the preview
50
 * of the style change according with the geometry of the layer.
51
 *
52
 * @author jaume dominguez faus - jaume.dominguez@iver.es
53
 */
54
public class StyleEditor extends JPanel implements IWindow, ActionListener {
55
        private static final long serialVersionUID = -4002620456610864510L;
56
        private JPanel pnlNorth = null;
57
        private JPanel jPanel = null;
58
        private JPanel pnlCenter = null;
59
        private AcceptCancelPanel pnlButtons = null;
60
        private JLabel lblTitle;
61
        private StylePreviewer preview;
62
        private JPanel pnlTools;
63
        private JTextField txtDesc;
64
        private Hashtable<AbstractButton, EditorTool> tools = new Hashtable<AbstractButton, EditorTool>();
65
        private static ArrayList<Class> installedTools = new ArrayList<Class>();
66
        private EditorTool prevTool;
67

    
68
        private ActionListener okAction = new ActionListener() {
69
                public void actionPerformed(ActionEvent e) {
70
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
71
                }
72
        };
73

    
74
        private ActionListener cancelAction = new ActionListener() {
75
                public void actionPerformed(ActionEvent e) {
76
                        getStylePreviewer().setStyle(null);
77
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
78
                };
79
        };
80
        /**
81
         * Constructor method
82
         *
83
         * @param style, an specific style for the labelling of the layer.This style depends on the
84
         * geometry of the layer.
85
         *
86
         */
87
        public StyleEditor(IStyle style) {
88
                if (style!=null) {
89
                        IStyle sty=null;
90
                        try {
91
                                sty = (IStyle) style.clone(); 
92
                        } catch (CloneNotSupportedException e) {
93
                                NotificationManager.addWarning("Symbol layer", e);
94
                        }
95
                        getStylePreviewer().setStyle(sty);
96
                        String desc = sty.getDescription();
97
                        getTxtDesc().setText(desc);
98
                } else {
99
                        getTxtDesc().setText(Messages.getText("new_style"));
100
                }
101
                initialize();
102
        }
103

    
104
        /**
105
         * This method initializes this
106
         *
107
         */
108
        private void initialize() {
109
        this.setLayout(new BorderLayout());
110
        this.setSize(500,285);
111
        this.setBorder(new EmptyBorder(5, 5, 5, 5));
112
        
113
        this.add(getPnlNorth(), BorderLayout.NORTH);
114
        this.add(getPnlCenter(), BorderLayout.CENTER);        
115
        this.add(getPnlButtons(), BorderLayout.SOUTH);
116
        }
117
        /**
118
         * Sets the previewer of the style in the panel
119
         *
120
         * @param sty, style to be previewed
121
         *
122
         */
123
        public void setStyle(IStyle sty) {
124
                preview.setStyle(sty);
125
        }
126

    
127
        /**
128
         * This method initializes the north JPanel
129
         *
130
         * @return javax.swing.JPanel
131
         */
132
        private JPanel getPnlNorth() {
133
                if (pnlNorth == null) {
134
                    lblTitle = new JLabel(Messages.getText("Nombre").concat(":"));
135
                    pnlNorth = new JPanel(new GridBagLayout());
136
                    GridBagConstraints c = new GridBagConstraints();
137
                    
138
                    c.fill = GridBagConstraints.BOTH;
139
                    c.insets = new Insets(3,3,3,3);
140
                    c.gridx = 0;
141
                    c.gridy = 0;        
142
                    c.weightx = 1;
143
                        pnlNorth.add(lblTitle,c);
144
                        
145
                        c.gridy = 1;
146
                        pnlNorth.add(getTxtDesc(),c);
147
                }
148
                return pnlNorth;
149
        }
150

    
151
        /**
152
         * This method initializes the textDesc JTextfield
153
         *
154
         * @return javax.swing.JTextfield
155
         */
156
        private JTextField getTxtDesc() {
157
                if (txtDesc == null) {
158
                        txtDesc = new JTextField(30);
159
                        txtDesc.addActionListener(this);
160

    
161
                }
162

    
163
                return txtDesc;
164
        }
165

    
166
        /**
167
         * This method initializes the center JPanel
168
         *
169
         * @return javax.swing.JPanel
170
         */
171
        private JPanel getPnlCenter() {
172
                if (pnlCenter == null) {
173
                        pnlCenter = new JPanel(new GridBagLayout());
174
                        GridBagConstraints c = new GridBagConstraints();
175
                        
176
                        JPanel aux = new JPanel();
177
                        aux.add(getStylePreviewer());
178
                        
179
                        c.fill = GridBagConstraints.BOTH;
180
                        c.insets = new Insets(15, 5, 5, 5);
181
                        
182
                        c.gridx = 0;
183
                        c.gridy = 0;        
184
                        c.weightx = 0.9;
185
                        c.weighty = 1;
186
                        pnlCenter.add(getStylePreviewer(), c);
187
                        
188
                        c.fill = GridBagConstraints.HORIZONTAL;
189
                        c.anchor = GridBagConstraints.WEST;
190
                        c.gridx = 1;
191
            c.weightx = 0.1;
192
                        c.gridwidth = 0;
193
                        pnlCenter.add(getPnlTools(), c);
194
                }
195
                return pnlCenter;
196
        }
197
        /**
198
         * This method initializes the StylePreviewer
199
         *
200
         * @return StylePreviewer for the label
201
         */
202
        public 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(Messages.getText("edit_style"));
226
                wi.setWidth(getWidth());
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
            
258
            I18nManager i18nManager = ToolsLocator.getI18nManager();
259
            
260
                if (pnlTools == null) {
261
                        pnlTools = new JPanel();
262
                        pnlTools.setBorder(BorderFactory.createTitledBorder(Messages.getText("tools")));
263
                        pnlTools.setLayout(new GridBagLayout());
264
            GridBagConstraints c = new GridBagConstraints();
265
                        
266
            c.fill = GridBagConstraints.BOTH;
267
            c.insets = new Insets(5,5,5,5);
268
            
269
            JLabel lblMaxPriority = new JLabel(i18nManager.getTranslation("priority_max"));
270
            JLabel lblIntPriority = new JLabel(i18nManager.getTranslation("priority_int"));
271
            JLabel lblMinPriority = new JLabel(i18nManager.getTranslation("priority_min"));
272
            JLabel lblBlockedPriority = new JLabel(i18nManager.getTranslation("priority_blocked"));
273
            
274
            c.gridx = 1;
275
            c.gridy = 1;
276
            pnlTools.add(lblMaxPriority, c);
277
            
278
            c.gridx = 1;
279
            c.gridy = 2;
280
            pnlTools.add(lblIntPriority, c);
281
            
282
            c.gridx = 1;
283
            c.gridy = 3;
284
            pnlTools.add(lblMinPriority, c);
285
            
286
            c.gridx = 1;
287
            c.gridy = 4;
288
            pnlTools.add(lblBlockedPriority, c);
289
            
290
            c.gridx = 0;
291
            c.gridy = 0;
292

    
293
                        ArrayList<EditorTool> availableTools = new ArrayList<EditorTool>();
294
                        IStyle sty = preview.getStyle();
295
                        Class<?> editorClazz = null;
296
                        Class[] constrLocator = new Class[] {JComponent.class};
297
                        Object[] constrInitargs = new Object[] { this };
298
                        try {
299
                                for (Iterator<Class> iterator = installedTools.iterator(); iterator
300
                                .hasNext();) {
301
                                        editorClazz = iterator.next();
302
                                        Constructor<EditorTool> constructor = (Constructor<EditorTool>) editorClazz.getConstructor(constrLocator);
303
                                        EditorTool editorTool = constructor.newInstance(constrInitargs);
304
                                        if (editorTool.isSuitableFor(sty)) {
305
                                                editorTool.setModel(sty);
306
                                                availableTools.add(editorTool);
307
                                        }
308

    
309
                                }
310
                        
311
                                ButtonGroup group = new ButtonGroup();
312
                                for (Iterator<EditorTool> iterator = availableTools.iterator();
313
                                iterator.hasNext();) {
314
                                        EditorTool editorTool = iterator.next();
315
                                        AbstractButton button = editorTool.getButton();
316
                                        button.addActionListener(this);
317
                                        c.gridy++;
318
                                        pnlTools.add(button,c);    
319
                                        group.add(button);
320

    
321
                                        tools.put(button, editorTool);
322

    
323
                                }
324
                        } catch (Exception e) {
325
                                NotificationManager.addWarning(Messages.getText("could_not_initialize_editor_")+"'"+editorClazz+"'"+
326
                                                " ["+new Date(System.currentTimeMillis()).toString()+"]");
327
                        }
328
                }
329
                return pnlTools;
330
        }
331
        /**
332
         * Sets the tool to be used with the previous one
333
         *
334
         */
335
        public void restorePreviousTool() {
336
                preview.setEditorTool(prevTool);
337
        }
338
        /**
339
         * Adds a new tool for the style
340
         *
341
         * @param styleEditorClass, the new tool to be added
342
         */
343
        public static void addEditorTool(Class styleEditorClass) {
344
                installedTools.add(styleEditorClass);
345
        }
346
        public Object getWindowProfile() {
347
                return WindowInfo.DIALOG_PROFILE;
348
        }
349
}  //  @jve:decl-index=0:visual-constraint="10,10"