Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / SimpleLine.java @ 34291

History | View | Annotate | Download (7.47 KB)

1 34291 fdiaz
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.gui.styling;
23
24
import java.awt.BasicStroke;
25
import java.awt.Color;
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.util.ArrayList;
31
32
import javax.swing.JPanel;
33
34
import org.gvsig.andami.messages.NotificationManager;
35
import org.gvsig.app.gui.panels.ColorChooserPanel;
36
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
37
import org.gvsig.gui.beans.listeners.BeanListener;
38
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
39
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
40
import org.gvsig.i18n.Messages;
41
import org.gvsig.symbology.SymbologyLocator;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IArrowDecoratorStyle;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle;
46
47
48
/**
49
 * SimpleLine allows the user to store and modify the main properties that
50
 * define a <b>simple line</b>.<p>
51
 * <p>
52
 * This functionality is carried out thanks to two tabs (simple line and arrow
53
 * decorator)which are included in the panel to edit the properities of a symbol
54
 * (SymbolEditor)how is explained in AbstractTypeSymbolEditor.<p>
55
 * <p>
56
 * The first tab (Simple Line)allows the user to change the color (<b>jccColor</b>),
57
 * the width (<b>txtWidth</b>) and the style of the line (<b>cmbLinStyles</b>).<p>
58
 * <p>
59
 * The second tab (<b>arrowDecorator</b>)allows the user to insert a symbol in the
60
 * line (for example an arrow to specify its orientation)and to modify it.
61
 *
62
 *@see ArrowDecorator
63
 *@see AbstractTypeSymbolEditor
64
 *@author jaume dominguez faus - jaume.dominguez@iver.es
65
 */
66
public class SimpleLine extends AbstractTypeSymbolEditor implements ActionListener {
67
68
        private ColorChooserPanel jccColor;
69
        private JIncrementalNumberField txtWidth;
70
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
71
        private ArrowDecorator arrowDecorator;
72
        private LineProperties lineProperties;
73
        private JIncrementalNumberField txtOffset;
74
75
76
        public SimpleLine(SymbolEditor owner) {
77
                super(owner);
78
                initialize();
79
        }
80
81
82
        /**
83
         * Initializes the parameters that define a simpleline.To do it, two tabs
84
         * are created inside the SymbolEditor panel with default values for the
85
         * different attributes of the simple line.This two tabs will be simple
86
         * line tab (options of color, width and style of the line)and arrow
87
         * decorator tab (options to "decorate" the line with a symbol).
88
         */
89
        private void initialize() {
90
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
91
                myTab.setName(Messages.getText("simple_line"));
92
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
93
94
                // color chooser
95
                jccColor = new ColorChooserPanel(true);
96
                jccColor.setAlpha(255);
97
                aux.addComponent(Messages.getText("color"),
98
                                jccColor        );
99
100
                // line width
101
                txtWidth = new JIncrementalNumberField("3", 25, 0, Double.POSITIVE_INFINITY, 1);
102
                aux.addComponent(Messages.getText("width")+":",
103
                                txtWidth );
104
105
                // line offset
106
                txtOffset = new JIncrementalNumberField("0", 25, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1);
107
                aux.addComponent(Messages.getText("offset")+":",
108
                                txtOffset );
109
110
111
112
                aux.setPreferredSize(new Dimension(300, 300));
113
                myTab.add(aux);
114
115
                // initialize defaults
116
                jccColor.setColor(Color.BLACK);
117
                txtWidth.setDouble(1.0);
118
                jccColor.addActionListener(this);
119
                txtWidth.addActionListener(this);
120
                txtOffset.addActionListener(this);
121
                tabs.add(myTab);
122
123
                // Arrow Decorator
124
                arrowDecorator = new ArrowDecorator();
125
                arrowDecorator.addListener(new BeanListener() {
126
127
                        public void beanValueChanged(Object value) {
128
                                fireSymbolChangedEvent();
129
                        }
130
131
                });
132
                tabs.add(arrowDecorator);
133
134
                lineProperties = new LineProperties((float) txtWidth.getDouble());
135
                lineProperties.addListener(new BeanListener(){
136
137
                        public void beanValueChanged(Object value) {
138
                                fireSymbolChangedEvent();
139
                        }
140
                });
141
                tabs.add(lineProperties);
142
        }
143
144
145
        public ISymbol getLayer() {
146
                ISimpleLineSymbol layer = SymbologyLocator.getSymbologyManager().createSimpleLineSymbol();
147
                layer.setLineColor(jccColor.getColor());
148
//                layer.setIsShapeVisible(true); //true is the default value for this property
149
                // clone the selected style in the combo box
150
151
                ISimpleLineStyle simplLine= SymbologyLocator.getSymbologyManager().createSimpleLineStyle(); //new SimpleLineStyle();
152
153
                simplLine.setStroke(lineProperties.getLinePropertiesStyle());
154
                simplLine.setOffset(-txtOffset.getDouble());
155
156
                IArrowDecoratorStyle ads= arrowDecorator.getArrowDecoratorStyle();
157
                if (ads != null) {
158
                        ads.getMarker().setColor(jccColor.getColor());
159
                }
160
                simplLine.setArrowDecorator(arrowDecorator.getArrowDecoratorStyle());
161
                layer.setLineStyle(simplLine);
162
                layer.setLineWidth((float) txtWidth.getDouble());
163
164
                return layer;
165
        }
166
167
        public String getName() {
168
                return Messages.getText("simple_line");
169
        }
170
171
        public JPanel[] getTabs() {
172
                return (JPanel[]) tabs.toArray(new JPanel[0]);
173
        }
174
175
        public void refreshControls(ISymbol layer) {
176
                ISimpleLineSymbol sym;
177
                try {
178
                        if (layer == null) {
179
                                // initialize defaults
180
                                System.err.println(getClass().getName()+":: should be unreachable code");
181
                                jccColor.setColor(Color.BLACK);
182
                                txtWidth.setDouble(1.0);
183
                                txtOffset.setDouble(0);
184
                        } else {
185
                                sym = (ISimpleLineSymbol) layer;
186
                                jccColor.setColor(sym.getColor());
187
                                txtWidth.setDouble(sym.getLineStyle().getLineWidth());
188
                                txtOffset.setDouble(sym.getLineStyle().getOffset() == 0 ? 0 : -sym.getLineStyle().getOffset() );
189
                                arrowDecorator.setArrowDecoratorStyle( sym.getLineStyle().getArrowDecorator());
190
                                /*
191
                                 * this line discards any temp changes in the linestyle
192
                                 * widths made by any previous rendering and sets all the
193
                                 * values to those to be persisted.
194
                                 */
195
                                ILineStyle tempLineStyle = (ILineStyle) sym.getLineStyle().clone();
196
                                lineProperties.setLinePropertiesStyle((BasicStroke) tempLineStyle.getStroke());
197
                        }
198
                } catch (IndexOutOfBoundsException ioEx) {
199
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
200
                } catch (ClassCastException ccEx) {
201
                        NotificationManager.addWarning("Illegal casting from " +
202
                                        layer.getClass().getName() + " to ISimpleLineSymbol.", ccEx);
203
                } catch (CloneNotSupportedException e) {
204
                        NotificationManager.addWarning(
205
                                        "Symbol line style does not support cloning", e);
206
                }
207
        }
208
209
        public void actionPerformed(ActionEvent e) {
210
                fireSymbolChangedEvent();
211
        }
212
213
        public EditorTool[] getEditorTools() {
214
                return null;
215
        }
216
217
218
        @Override
219
        public boolean canManageSymbol(ISymbol symbol) {
220
                return symbol instanceof ISimpleLineSymbol;
221
        }
222
}