Statistics
| Revision:

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

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.Color;
46
import java.awt.Component;
47
import java.awt.Cursor;
48
import java.awt.FlowLayout;
49
import java.awt.Graphics2D;
50
import java.awt.Point;
51
import java.awt.Rectangle;
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 sun.awt.windows.WCustomCursor;
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.andami.ui.mdiManager.IWindow;
75
import com.iver.andami.ui.mdiManager.WindowInfo;
76
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
77
import com.iver.cit.gvsig.fmap.core.FShape;
78
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
79
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
80
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
81
import com.iver.cit.gvsig.fmap.core.styles.SimpleLabelStyle;
82
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
83
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
84

    
85
/**
86
 * @author jaume dominguez faus - jaume.dominguez@iver.es
87
 */
88
public class StyleEditor extends JPanel implements IWindow, ActionListener {
89

    
90
        private JPanel pnlNorth = null;
91
        private JPanel jPanel = null;
92
        private JPanel pnlCenter = null;
93
        private AcceptCancelPanel pnlButtons = null;
94
        private JLabel lblTitle;
95
        private StylePreviewer preview;
96
        private JPanel pnlTools;
97
        private JTextField txtDesc;
98
        private ActionListener okAction = new ActionListener() {
99
                public void actionPerformed(ActionEvent e) {
100
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
101
                }
102
        };
103
        private ActionListener cancelAction = new ActionListener() {
104
                public void actionPerformed(ActionEvent e) {
105
                        getStylePreviewer().setStyle(null);
106
                        PluginServices.getMDIManager().closeWindow(StyleEditor.this);
107
                };
108
        };
109

    
110
        public StyleEditor(IStyle style) {
111
                if (style!=null) {
112
                        IStyle sty = SymbologyFactory.createStyleFromXML(style.getXMLEntity(), style.getDescription());
113
                        getStylePreviewer().setStyle(sty);
114
                        String desc = sty.getDescription();
115
                        getTxtDesc().setText(desc);
116
                } else {
117
                        getTxtDesc().setText(PluginServices.getText(this, "new_style"));
118
                }
119
                initialize();
120
        }
121

    
122
        /**
123
         * This method initializes this
124
         *
125
         */
126
        private void initialize() {
127
        this.setLayout(new BorderLayout());
128
        this.setSize(417,284);
129
        this.add(getPnlNorth(), java.awt.BorderLayout.NORTH);
130
        this.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
131
        this.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
132
        }
133

    
134

    
135

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

    
151
        private JTextField getTxtDesc() {
152
                if (txtDesc == null) {
153
                        txtDesc = new JTextField(30);
154

    
155
                }
156

    
157
                return txtDesc;
158
        }
159

    
160
        /**
161
         * This method initializes jPanel1
162
         *
163
         * @return javax.swing.JPanel
164
         */
165
        private JPanel getPnlCenter() {
166
                if (pnlCenter == null) {
167
                        pnlCenter = new JPanel(new BorderLayout());
168
                        JPanel aux = new JPanel();
169
                        aux.add(getStylePreviewer());
170
                        pnlCenter.add(getStylePreviewer(), BorderLayout.CENTER);
171
                        pnlCenter.add(getPnlTools(), BorderLayout.EAST);
172
                }
173
                return pnlCenter;
174
        }
175

    
176
        private StylePreviewer getStylePreviewer() {
177
                if (preview == null) {
178
                        preview = new StylePreviewer();
179
                        preview.setShowOutline(true);
180
                }
181

    
182
                return preview;
183
        }
184

    
185
        /**
186
         * This method initializes pnlButtons
187
         *
188
         * @return javax.swing.JPanel
189
         */
190
        private JPanel getPnlButtons() {
191
                if (pnlButtons == null) {
192
                        pnlButtons = new AcceptCancelPanel(okAction, cancelAction);
193
                }
194
                return pnlButtons;
195
        }
196

    
197
        public WindowInfo getWindowInfo() {
198
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
199
                wi.setTitle(PluginServices.getText(this, "edit_style"));
200
                wi.setWidth(getWidth()+10);
201
                wi.setHeight(getHeight());
202
                return wi;
203
        }
204

    
205
        public IStyle getStyle() {
206
                return getStylePreviewer().getStyle();
207
        }
208

    
209
        public void actionPerformed(ActionEvent e) {
210
                Component c = (Component) e.getSource();
211
                if (c.equals(getBtnPan())) {
212
                        getStylePreviewer().setEditorTool(panTool);
213
                } else if (c.equals(getBtnNewTextArea())) {
214
                        getStylePreviewer().setEditorTool(newTextTool);
215
                }
216
        }
217

    
218

    
219
        // to be extracted to a specific toolkit class
220
        private JToggleButton btnPan;
221
        private JToggleButton btnNewTextArea;
222
        private JButton btnBackground;
223
        private EditorTool panTool = new EditorTool(StyleEditor.this) {
224
                private Cursor cursor;
225
                private Point pIni, pEnd;
226
                public Cursor getCursor() {
227
                        if (cursor == null) {
228
                                ImageIcon cursorImage = new ImageIcon(getClass().getClassLoader().getResource("images/Hand.gif"));
229
                                Point p = new Point(cursorImage.getIconWidth()/2, cursorImage.getIconHeight()/2);
230
                                cursor = new WCustomCursor(cursorImage.getImage(), p, "as?dlfk");
231

    
232
                        }
233
                        return cursor;
234
                }
235

    
236
                public void mousePressed(MouseEvent e) {
237
                        Rectangle bounds = getStylePreviewer().getBounds();
238
                        SimpleLabelStyle sty = (SimpleLabelStyle) getStylePreviewer().getStyle();
239
                        Point2D marker = sty.getMarkerPoint();
240
                        pIni = new Point((int) (marker.getX()*bounds.width), bounds.height - (int) (marker.getY()*bounds.height));
241
                        pEnd = e.getPoint();
242

    
243
                }
244

    
245
                public void mouseReleased(MouseEvent e) {
246

    
247
                }
248

    
249
                public void mouseDragged(MouseEvent e) {
250
                        pEnd = e.getPoint();
251
                        Rectangle bounds = getStylePreviewer().getBounds();
252
                        double xOffset = (pEnd.getX() - pIni.getX())/bounds.getWidth();
253
                        double yOffset = (pEnd.getY() - pIni.getY())/bounds.getHeight();
254
                        SimpleLabelStyle sty = (SimpleLabelStyle) getStylePreviewer().getStyle();
255
                        Point2D marker = sty.getMarkerPoint();
256
                        marker.setLocation(xOffset, -yOffset);
257

    
258
                        repaint();
259
                }
260

    
261
        };
262
        private EditorTool newTextTool = new EditorTool(StyleEditor.this) {
263
                private /*static*/ final Cursor cursor = Cursor.getDefaultCursor();
264
                private Point pIni, pEnd;
265
                public Cursor getCursor() {
266
                        return cursor;
267
                }
268

    
269
                public void mousePressed(MouseEvent e) {
270
                        pIni = e.getPoint();
271
                        pEnd = e.getPoint();
272

    
273
                }
274
                public void mouseReleased(MouseEvent e) {
275

    
276
                }
277

    
278
                public void mouseDragged(MouseEvent e) {
279
                        pEnd = e.getPoint();
280

    
281
                        int minx = pIni.x;
282
                        int miny = pIni.y;
283
                        int width = pEnd.x-pIni.x;
284
                        int height = pEnd.y - pIni.y;
285
                        if (width < 0) {
286
                                minx += width;
287
                                width = -width;
288
                        }
289
                        if (height <0) {
290
                                miny += height;
291
                                height = -height;
292
                        }
293

    
294

    
295
                        SimpleLabelStyle sty = (SimpleLabelStyle) getStylePreviewer().getStyle();
296
                        Rectangle bounds = getStylePreviewer().getBounds();
297
                        Rectangle2D rect = new Rectangle2D.Double(
298
                                        minx/bounds.getWidth(),
299
                                        miny/bounds.getHeight(),
300
                                        width/bounds.getWidth(),
301
                                        height/bounds.getHeight()
302
                                        );
303
                        sty.setTextFieldArea(0, rect);
304
                        repaint();
305
                }
306
        };
307

    
308
        private JPanel getPnlTools() {
309
                if (pnlTools == null) {
310
                        pnlTools = new JPanel();
311
                        pnlTools.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "tools")));
312
                        pnlTools.setLayout(new BoxLayout(pnlTools, BoxLayout.Y_AXIS));
313
                        pnlTools.add(getBtnPan());
314
                        pnlTools.add(getBtnNewTextArea());
315
                        pnlTools.add(new JSeparator(JSeparator.HORIZONTAL));
316
                        pnlTools.add(getBtnBackground());
317

    
318
                        ButtonGroup group = new ButtonGroup();
319
                        group.add(getBtnPan());
320
                        group.add(getBtnNewTextArea());
321
                }
322

    
323
                return pnlTools;
324
        }
325

    
326
        private Component getBtnBackground() {
327
                if (btnBackground == null) {
328
                        btnBackground = new JButton("background");
329

    
330
                }
331

    
332
                return btnBackground;
333
        }
334

    
335
        private JToggleButton getBtnNewTextArea() {
336
                if (btnNewTextArea == null) {
337
                        btnNewTextArea = new JToggleButton("new_text");
338
                        btnNewTextArea.addActionListener(this);
339
                }
340
                return btnNewTextArea;
341
        }
342

    
343
        private JToggleButton getBtnPan() {
344
                if (btnPan == null) {
345
                        btnPan = new JToggleButton("pan");
346
                        btnPan.addActionListener(this);
347
                }
348

    
349
                return btnPan;
350
        }
351

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