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 @ 40560

History | View | Annotate | Download (8.49 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 java.awt.BorderLayout;
27
import java.awt.FlowLayout;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.lang.reflect.Constructor;
31
import java.util.ArrayList;
32
import java.util.Date;
33
import java.util.Hashtable;
34
import java.util.Iterator;
35

    
36
import javax.swing.AbstractButton;
37
import javax.swing.BorderFactory;
38
import javax.swing.BoxLayout;
39
import javax.swing.ButtonGroup;
40
import javax.swing.JComponent;
41
import javax.swing.JLabel;
42
import javax.swing.JPanel;
43
import javax.swing.JTextField;
44

    
45
import org.gvsig.andami.PluginServices;
46
import org.gvsig.andami.messages.NotificationManager;
47
import org.gvsig.andami.ui.mdiManager.IWindow;
48
import org.gvsig.andami.ui.mdiManager.WindowInfo;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
50
import org.gvsig.gui.beans.AcceptCancelPanel;
51
import org.gvsig.i18n.Messages;
52

    
53

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

    
76
        private ActionListener okAction = new ActionListener() {
77
                public void actionPerformed(ActionEvent e) {
78
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
79
                }
80
        };
81

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

    
112
        /**
113
         * This method initializes this
114
         *
115
         */
116
        private void initialize() {
117
        this.setLayout(new BorderLayout());
118
        this.setSize(417,284);
119
        this.add(getPnlNorth(), java.awt.BorderLayout.NORTH);
120
        this.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
121
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
122

    
123
        }
124
        /**
125
         * Sets the previewer of the style in the panel
126
         *
127
         * @param sty, style to be previewed
128
         *
129
         */
130
        public void setStyle(IStyle sty) {
131
                preview.setStyle(sty);
132
        }
133

    
134
        /**
135
         * This method initializes the north JPanel
136
         *
137
         * @return javax.swing.JPanel
138
         */
139
        private JPanel getPnlNorth() {
140
                if (pnlNorth == null) {
141
                        pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEADING));
142
                        lblTitle = new JLabel(Messages.getText("editing"));
143
                        pnlNorth.add(lblTitle);
144
                        pnlNorth.add(getTxtDesc());
145
                }
146
                return pnlNorth;
147
        }
148

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

    
159
                }
160

    
161
                return txtDesc;
162
        }
163

    
164
        /**
165
         * This method initializes the center JPanel
166
         *
167
         * @return javax.swing.JPanel
168
         */
169
        private JPanel getPnlCenter() {
170
                if (pnlCenter == null) {
171
                        pnlCenter = new JPanel(new BorderLayout());
172
                        JPanel aux = new JPanel();
173
                        aux.add(getStylePreviewer());
174
                        pnlCenter.add(getStylePreviewer(), BorderLayout.CENTER);
175
                        pnlCenter.add(getPnlTools(), BorderLayout.EAST);
176
                }
177
                return pnlCenter;
178
        }
179
        /**
180
         * This method initializes the StylePreviewer
181
         *
182
         * @return StylePreviewer for the label
183
         */
184
        private StylePreviewer getStylePreviewer() {
185
                if (preview == null) {
186
                        preview = new StylePreviewer();
187
                        preview.setShowOutline(true);
188
                }
189

    
190
                return preview;
191
        }
192

    
193
        /**
194
         * This method initializes pnlButtons JPanel
195
         *
196
         * @return javax.swing.JPanel
197
         */
198
        private JPanel getPnlButtons() {
199
                if (pnlButtons == null) {
200
                        pnlButtons = new AcceptCancelPanel(okAction, cancelAction);
201
                }
202
                return pnlButtons;
203
        }
204

    
205
        public WindowInfo getWindowInfo() {
206
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
207
                wi.setTitle(Messages.getText("edit_style"));
208
                wi.setWidth(getWidth()+10);
209
                wi.setHeight(getHeight());
210
                return wi;
211
        }
212

    
213
        /**
214
         * Obtains the style for the label
215
         *
216
         * @return IStyle
217
         */
218
        public IStyle getStyle() {
219
                IStyle style = getStylePreviewer().getStyle();
220
                if (style != null) {
221
                        style.setDescription(txtDesc.getText());
222
                }
223
                return style;
224
        }
225

    
226
        public void actionPerformed(ActionEvent e) {
227
                AbstractButton srcButton = (AbstractButton) e.getSource();
228
                EditorTool currTool = tools.get(srcButton);
229
                if (currTool != null) {
230
                        prevTool = preview.setEditorTool(currTool);
231
                }
232
        }
233
        /**
234
         * This method initializes pnlTools JPanel
235
         *
236
         * @return javax.swing.JPanel
237
         */
238
        private JPanel getPnlTools() {
239
                if (pnlTools == null) {
240
                        pnlTools = new JPanel();
241
                        pnlTools.setBorder(BorderFactory.createTitledBorder(Messages.getText("tools")));
242
                        pnlTools.setLayout(new BoxLayout(pnlTools, BoxLayout.Y_AXIS));
243

    
244
                        ArrayList<EditorTool> availableTools = new ArrayList<EditorTool>();
245
                        IStyle sty = preview.getStyle();
246
                        Class<?> editorClazz = null;
247
                        Class[] constrLocator = new Class[] {JComponent.class};
248
                        Object[] constrInitargs = new Object[] { this };
249
                        try {
250
                                for (Iterator<Class> iterator = installedTools.iterator(); iterator
251
                                .hasNext();) {
252
                                        editorClazz = iterator.next();
253
                                        Constructor<EditorTool> constructor = (Constructor<EditorTool>) editorClazz.getConstructor(constrLocator);
254
                                        EditorTool editorTool = constructor.newInstance(constrInitargs);
255
                                        if (editorTool.isSuitableFor(sty)) {
256
                                                editorTool.setModel(sty);
257
                                                availableTools.add(editorTool);
258
                                        }
259

    
260
                                }
261

    
262
                                ButtonGroup group = new ButtonGroup();
263
                                for (Iterator<EditorTool> iterator = availableTools.iterator();
264
                                iterator.hasNext();) {
265
                                        EditorTool editorTool = iterator.next();
266
                                        AbstractButton button = editorTool.getButton();
267
                                        button.addActionListener(this);
268
                                        pnlTools.add(button);
269
                                        group.add(button);
270

    
271
                                        tools.put(button, editorTool);
272

    
273
                                }
274
                        } catch (Exception e) {
275
                                NotificationManager.addWarning(Messages.getText("could_not_initialize_editor_")+"'"+editorClazz+"'"+
276
                                                " ["+new Date(System.currentTimeMillis()).toString()+"]");
277
                        }
278
                }
279
                return pnlTools;
280
        }
281
        /**
282
         * Sets the tool to be used with the previous one
283
         *
284
         */
285
        public void restorePreviousTool() {
286
                preview.setEditorTool(prevTool);
287
        }
288
        /**
289
         * Adds a new tool for the style
290
         *
291
         * @param styleEditorClass, the new tool to be added
292
         */
293
        public static void addEditorTool(Class styleEditorClass) {
294
                installedTools.add(styleEditorClass);
295
        }
296
        public Object getWindowProfile() {
297
                return WindowInfo.DIALOG_PROFILE;
298
        }
299
}  //  @jve:decl-index=0:visual-constraint="10,10"