Statistics
| Revision:

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

History | View | Annotate | Download (9.38 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.Component;
46
import java.awt.Cursor;
47
import java.awt.Dimension;
48
import java.awt.FlowLayout;
49
import java.awt.Point;
50
import java.awt.Rectangle;
51
import java.awt.Toolkit;
52
import java.awt.event.ActionEvent;
53
import java.awt.event.ActionListener;
54
import java.awt.event.MouseEvent;
55
import java.awt.geom.Point2D;
56
import java.awt.geom.Rectangle2D;
57

    
58
import javax.swing.BorderFactory;
59
import javax.swing.BoxLayout;
60
import javax.swing.ButtonGroup;
61
import javax.swing.ImageIcon;
62
import javax.swing.JLabel;
63
import javax.swing.JPanel;
64
import javax.swing.JSeparator;
65
import javax.swing.JTextField;
66
import javax.swing.JToggleButton;
67

    
68
import org.gvsig.gui.beans.AcceptCancelPanel;
69
import org.gvsig.gui.beans.swing.JButton;
70

    
71
import com.iver.andami.PluginServices;
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
import com.iver.cit.gvsig.fmap.core.styles.SimpleLabelStyle;
77

    
78
/**
79
 * @author jaume dominguez faus - jaume.dominguez@iver.es
80
 */
81
public class StyleEditor extends JPanel implements IWindow, ActionListener {
82

    
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 ActionListener okAction = new ActionListener() {
92
                public void actionPerformed(ActionEvent e) {
93
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
94
                }
95
        };
96
        private ActionListener cancelAction = new ActionListener() {
97
                public void actionPerformed(ActionEvent e) {
98
                        getStylePreviewer().setStyle(null);
99
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
100
                };
101
        };
102

    
103
        public StyleEditor(IStyle style) {
104
                if (style!=null) {
105
                        IStyle sty = SymbologyFactory.createStyleFromXML(style.getXMLEntity(), style.getDescription());
106
                        getStylePreviewer().setStyle(sty);
107
                        String desc = sty.getDescription();
108
                        getTxtDesc().setText(desc);
109
                } else {
110
                        getTxtDesc().setText(PluginServices.getText(this, "new_style"));
111
                }
112
                initialize();
113
        }
114

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

    
127

    
128

    
129
        /**
130
         * This method initializes pnlNorth
131
         *
132
         * @return javax.swing.JPanel
133
         */
134
        private JPanel getPnlNorth() {
135
                if (pnlNorth == null) {
136
                        pnlNorth = new JPanel(new FlowLayout(FlowLayout.LEADING));
137
                        lblTitle = new JLabel(PluginServices.getText(this, "editing"));
138
                        pnlNorth.add(lblTitle);
139
                        pnlNorth.add(getTxtDesc());
140
                }
141
                return pnlNorth;
142
        }
143

    
144
        private JTextField getTxtDesc() {
145
                if (txtDesc == null) {
146
                        txtDesc = new JTextField(30);
147

    
148
                }
149

    
150
                return txtDesc;
151
        }
152

    
153
        /**
154
         * This method initializes jPanel1
155
         *
156
         * @return javax.swing.JPanel
157
         */
158
        private JPanel getPnlCenter() {
159
                if (pnlCenter == null) {
160
                        pnlCenter = new JPanel(new BorderLayout());
161
                        JPanel aux = new JPanel();
162
                        aux.add(getStylePreviewer());
163
                        pnlCenter.add(getStylePreviewer(), BorderLayout.CENTER);
164
                        pnlCenter.add(getPnlTools(), BorderLayout.EAST);
165
                }
166
                return pnlCenter;
167
        }
168

    
169
        private StylePreviewer getStylePreviewer() {
170
                if (preview == null) {
171
                        preview = new StylePreviewer();
172
                        preview.setShowOutline(true);
173
                }
174

    
175
                return preview;
176
        }
177

    
178
        /**
179
         * This method initializes pnlButtons
180
         *
181
         * @return javax.swing.JPanel
182
         */
183
        private JPanel getPnlButtons() {
184
                if (pnlButtons == null) {
185
                        pnlButtons = new AcceptCancelPanel(okAction, cancelAction);
186
                }
187
                return pnlButtons;
188
        }
189

    
190
        public WindowInfo getWindowInfo() {
191
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
192
                wi.setTitle(PluginServices.getText(this, "edit_style"));
193
                wi.setWidth(getWidth()+10);
194
                wi.setHeight(getHeight());
195
                return wi;
196
        }
197

    
198
        public IStyle getStyle() {
199
                return getStylePreviewer().getStyle();
200
        }
201

    
202
        public void actionPerformed(ActionEvent e) {
203
                Component c = (Component) e.getSource();
204
                if (c.equals(getBtnPan())) {
205
                        getStylePreviewer().setEditorTool(panTool);
206
                } else if (c.equals(getBtnNewTextArea())) {
207
                        getStylePreviewer().setEditorTool(newTextTool);
208
                }
209
        }
210

    
211

    
212
        // to be extracted to a specific toolkit class
213
        private JToggleButton btnPan;
214
        private JToggleButton btnNewTextArea;
215
        private JButton btnBackground;
216
        private Dimension smallBtnSize = new Dimension(24, 24);
217
        private EditorTool panTool = new EditorTool(StyleEditor.this) {
218
                private Cursor cursor;
219
                private Point pIni, pEnd;
220
                public Cursor getCursor() {
221
                        if (cursor == null) {
222
                                ImageIcon cursorImage = new ImageIcon(getClass().getClassLoader().getResource("images/Hand.gif"));
223
                                cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage.getImage(),
224
                                                new Point(16, 16), "");
225

    
226
                        }
227
                        return cursor;
228
                }
229

    
230
                public void mousePressed(MouseEvent e) {
231
                        Rectangle bounds = getStylePreviewer().getBounds();
232
                        SimpleLabelStyle sty = (SimpleLabelStyle) getStylePreviewer().getStyle();
233
                        Point2D marker = sty.getMarkerPoint();
234
                        pIni = new Point((int) (marker.getX()*bounds.width), bounds.height - (int) (marker.getY()*bounds.height));
235
                        pEnd = e.getPoint();
236

    
237
                }
238

    
239
                public void mouseReleased(MouseEvent e) {
240

    
241
                }
242

    
243
                public void mouseDragged(MouseEvent e) {
244
                        pEnd = e.getPoint();
245
                        Rectangle bounds = getStylePreviewer().getBounds();
246
                        double xOffset = (pEnd.getX() - pIni.getX())/bounds.getWidth();
247
                        double yOffset = (pEnd.getY() - pIni.getY())/bounds.getHeight();
248
                        SimpleLabelStyle sty = (SimpleLabelStyle) getStylePreviewer().getStyle();
249
                        Point2D marker = sty.getMarkerPoint();
250
                        marker.setLocation(xOffset, -yOffset);
251

    
252
                        repaint();
253
                }
254

    
255
        };
256
        
257
        private EditorTool newTextTool = new EditorTool(StyleEditor.this) {
258
                private /*static*/ final Cursor cursor = Cursor.getDefaultCursor();
259
                private Point pIni, pEnd;
260
                public Cursor getCursor() {
261
                        return cursor;
262
                }
263

    
264
                public void mousePressed(MouseEvent e) {
265
                        pIni = e.getPoint();
266
                        pEnd = e.getPoint();
267

    
268
                }
269
                public void mouseReleased(MouseEvent e) {
270

    
271
                }
272
                public void mouseDragged(MouseEvent e) {
273
                        pEnd = e.getPoint();
274

    
275
                        int minx = pIni.x;
276
                        int miny = pIni.y;
277
                        int width = pEnd.x-pIni.x;
278
                        int height = pEnd.y - pIni.y;
279
                        if (width < 0) {
280
                                minx += width;
281
                                width = -width;
282
                        }
283
                        if (height <0) {
284
                                miny += height;
285
                                height = -height;
286
                        }
287

    
288

    
289
                        SimpleLabelStyle sty = (SimpleLabelStyle) getStylePreviewer().getStyle();
290
                        Rectangle bounds = getStylePreviewer().getBounds();
291
                        Rectangle2D rect = new Rectangle2D.Double(
292
                                        minx/bounds.getWidth(),
293
                                        miny/bounds.getHeight(),
294
                                        width/bounds.getWidth(),
295
                                        height/bounds.getHeight()
296
                                        );
297
                        sty.setTextFieldArea(0, rect);
298
                        repaint();
299
                }
300
        };
301

    
302
        private JPanel getPnlTools() {
303
                if (pnlTools == null) {
304
                        pnlTools = new JPanel();
305
                        pnlTools.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "tools")));
306
                        pnlTools.setLayout(new BoxLayout(pnlTools, BoxLayout.Y_AXIS));
307
                        pnlTools.add(getBtnPan());
308
                        pnlTools.add(getBtnNewTextArea());
309
                        pnlTools.add(new JSeparator(JSeparator.HORIZONTAL));
310
                        pnlTools.add(getBtnBackground());
311

    
312
                        ButtonGroup group = new ButtonGroup();
313
                        group.add(getBtnPan());
314
                        group.add(getBtnNewTextArea());
315
                }
316

    
317
                return pnlTools;
318
        }
319

    
320
        private Component getBtnBackground() {
321
                if (btnBackground == null) {
322
                        btnBackground = new JButton("background");
323

    
324
                }
325

    
326
                return btnBackground;
327
        }
328

    
329
        private JToggleButton getBtnNewTextArea() {
330
                if (btnNewTextArea == null) {
331
                        btnNewTextArea = new JToggleButton(new ImageIcon(getClass().getClassLoader().getResource("images/add-text.png")));
332
                        btnNewTextArea.addActionListener(this);
333
                        btnNewTextArea.setToolTipText(PluginServices.getText(this, "add_text_area"));
334
                }
335
                return btnNewTextArea;
336
        }
337

    
338
        private JToggleButton getBtnPan() {
339
                if (btnPan == null) {
340
                        btnPan = new JToggleButton(new ImageIcon(getClass().getClassLoader().getResource("images/Hand.gif")));
341
                        btnPan.addActionListener(this);
342
                        btnPan.setToolTipText(PluginServices.getText(this, "offset_label"));
343
                }
344

    
345
                return btnPan;
346
        }
347

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