Statistics
| Revision:

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

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

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

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

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

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

    
82
        private JPanel pnlNorth = null;
83
        private JPanel jPanel = null;
84
        private JPanel pnlCenter = null;
85
        private AcceptCancelPanel pnlButtons = null;
86
        private JLabel lblTitle;
87
        private StylePreviewer preview;
88
        private JPanel pnlTools;
89
        private JTextField txtDesc;
90
        private ActionListener okAction = new ActionListener() {
91
                public void actionPerformed(ActionEvent e) {
92
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
93
                }
94
        };
95
        private ActionListener cancelAction = new ActionListener() {
96
                public void actionPerformed(ActionEvent e) {
97
                        getStylePreviewer().setStyle(null);
98
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
99
                };
100
        };
101

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

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

    
126

    
127

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

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

    
147
                }
148

    
149
                return txtDesc;
150
        }
151

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

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

    
174
                return preview;
175
        }
176

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

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

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

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

    
210

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

    
224
                        }
225
                        return cursor;
226
                }
227

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

    
235
                }
236

    
237
                public void mouseReleased(MouseEvent e) {
238

    
239
                }
240

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

    
250
                        repaint();
251
                }
252

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

    
261
                public void mousePressed(MouseEvent e) {
262
                        pIni = e.getPoint();
263
                        pEnd = e.getPoint();
264

    
265
                }
266
                public void mouseReleased(MouseEvent e) {
267

    
268
                }
269

    
270
                public void mouseDragged(MouseEvent e) {
271
                        pEnd = e.getPoint();
272

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

    
286

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

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

    
310
                        ButtonGroup group = new ButtonGroup();
311
                        group.add(getBtnPan());
312
                        group.add(getBtnNewTextArea());
313
                }
314

    
315
                return pnlTools;
316
        }
317

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

    
322
                }
323

    
324
                return btnBackground;
325
        }
326

    
327
        private JToggleButton getBtnNewTextArea() {
328
                if (btnNewTextArea == null) {
329
                        btnNewTextArea = new JToggleButton("new_text");
330
                        btnNewTextArea.addActionListener(this);
331
                }
332
                return btnNewTextArea;
333
        }
334

    
335
        private JToggleButton getBtnPan() {
336
                if (btnPan == null) {
337
                        btnPan = new JToggleButton("pan");
338
                        btnPan.addActionListener(this);
339
                }
340

    
341
                return btnPan;
342
        }
343

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