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 / SimpleLine.java @ 40560

History | View | Annotate | Download (7.52 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.BasicStroke;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.awt.FlowLayout;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.util.ArrayList;
33

    
34
import javax.swing.JPanel;
35

    
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.app.gui.panels.ColorChooserPanel;
38
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
39
import org.gvsig.gui.beans.listeners.BeanListener;
40
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
41
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
42
import org.gvsig.i18n.Messages;
43
import org.gvsig.symbology.SymbologyLocator;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IArrowDecoratorStyle;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle;
48

    
49

    
50
/**
51
 * SimpleLine allows the user to store and modify the main properties that
52
 * define a <b>simple line</b>.<p>
53
 * <p>
54
 * This functionality is carried out thanks to two tabs (simple line and arrow
55
 * decorator)which are included in the panel to edit the properities of a symbol
56
 * (SymbolEditor)how is explained in AbstractTypeSymbolEditor.<p>
57
 * <p>
58
 * The first tab (Simple Line)allows the user to change the color (<b>jccColor</b>),
59
 * the width (<b>txtWidth</b>) and the style of the line (<b>cmbLinStyles</b>).<p>
60
 * <p>
61
 * The second tab (<b>arrowDecorator</b>)allows the user to insert a symbol in the
62
 * line (for example an arrow to specify its orientation)and to modify it.
63
 *
64
 *@see ArrowDecorator
65
 *@see AbstractTypeSymbolEditor
66
 *@author jaume dominguez faus - jaume.dominguez@iver.es
67
 */
68
public class SimpleLine extends AbstractTypeSymbolEditor implements ActionListener {
69

    
70
        private ColorChooserPanel jccColor;
71
        private JIncrementalNumberField txtWidth;
72
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
73
        private ArrowDecorator arrowDecorator;
74
        private LineProperties lineProperties;
75
        private JIncrementalNumberField txtOffset;
76

    
77

    
78
        public SimpleLine(SymbolEditor owner) {
79
                super(owner);
80
                initialize();
81
        }
82

    
83

    
84
        /**
85
         * Initializes the parameters that define a simpleline.To do it, two tabs
86
         * are created inside the SymbolEditor panel with default values for the
87
         * different attributes of the simple line.This two tabs will be simple
88
         * line tab (options of color, width and style of the line)and arrow
89
         * decorator tab (options to "decorate" the line with a symbol).
90
         */
91
        private void initialize() {
92
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
93
                myTab.setName(Messages.getText("simple_line"));
94
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
95

    
96
                // color chooser
97
                jccColor = new ColorChooserPanel(true);
98
                jccColor.setAlpha(255);
99
                aux.addComponent(Messages.getText("color"),
100
                                jccColor        );
101

    
102
                // line width
103
                txtWidth = new JIncrementalNumberField("3", 25, 0, Double.POSITIVE_INFINITY, 1);
104
                aux.addComponent(Messages.getText("width")+":",
105
                                txtWidth );
106

    
107
                // line offset
108
                txtOffset = new JIncrementalNumberField("0", 25, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1);
109
                aux.addComponent(Messages.getText("offset")+":",
110
                                txtOffset );
111

    
112

    
113

    
114
                aux.setPreferredSize(new Dimension(300, 300));
115
                myTab.add(aux);
116

    
117
                // initialize defaults
118
                jccColor.setColor(Color.BLACK);
119
                txtWidth.setDouble(1.0);
120
                jccColor.addActionListener(this);
121
                txtWidth.addActionListener(this);
122
                txtOffset.addActionListener(this);
123
                tabs.add(myTab);
124

    
125
                // Arrow Decorator
126
                arrowDecorator = new ArrowDecorator();
127
                arrowDecorator.addListener(new BeanListener() {
128

    
129
                        public void beanValueChanged(Object value) {
130
                                fireSymbolChangedEvent();
131
                        }
132

    
133
                });
134
                tabs.add(arrowDecorator);
135

    
136
                lineProperties = new LineProperties((float) txtWidth.getDouble());
137
                lineProperties.addListener(new BeanListener(){
138

    
139
                        public void beanValueChanged(Object value) {
140
                                fireSymbolChangedEvent();
141
                        }
142
                });
143
                tabs.add(lineProperties);
144
        }
145

    
146

    
147
        public ISymbol getLayer() {
148
                ISimpleLineSymbol layer = SymbologyLocator.getSymbologyManager().createSimpleLineSymbol();
149
                layer.setLineColor(jccColor.getColor());
150
//                layer.setIsShapeVisible(true); //true is the default value for this property 
151
                // clone the selected style in the combo box
152

    
153
                ISimpleLineStyle simplLine= SymbologyLocator.getSymbologyManager().createSimpleLineStyle(); //new SimpleLineStyle();
154

    
155
                simplLine.setStroke(lineProperties.getLinePropertiesStyle());
156
                simplLine.setOffset(-txtOffset.getDouble());
157

    
158
                IArrowDecoratorStyle ads= arrowDecorator.getArrowDecoratorStyle();
159
                if (ads != null) {
160
                        ads.getMarker().setColor(jccColor.getColor());
161
                }
162
                simplLine.setArrowDecorator(arrowDecorator.getArrowDecoratorStyle());
163
                layer.setLineStyle(simplLine);
164
                layer.setLineWidth((float) txtWidth.getDouble());
165

    
166
                return layer;
167
        }
168

    
169
        public String getName() {
170
                return Messages.getText("simple_line");
171
        }
172

    
173
        public JPanel[] getTabs() {
174
                return (JPanel[]) tabs.toArray(new JPanel[0]);
175
        }
176

    
177
        public void refreshControls(ISymbol layer) {
178
                ISimpleLineSymbol sym;
179
                try {
180
                        if (layer == null) {
181
                                // initialize defaults
182
                                System.err.println(getClass().getName()+":: should be unreachable code");
183
                                jccColor.setColor(Color.BLACK);
184
                                txtWidth.setDouble(1.0);
185
                                txtOffset.setDouble(0);
186
                        } else {
187
                                sym = (ISimpleLineSymbol) layer;
188
                                jccColor.setColor(sym.getColor());
189
                                txtWidth.setDouble(sym.getLineStyle().getLineWidth());
190
                                txtOffset.setDouble(sym.getLineStyle().getOffset() == 0 ? 0 : -sym.getLineStyle().getOffset() );
191
                                arrowDecorator.setArrowDecoratorStyle( sym.getLineStyle().getArrowDecorator());
192
                                /*
193
                                 * this line discards any temp changes in the linestyle
194
                                 * widths made by any previous rendering and sets all the
195
                                 * values to those to be persisted.
196
                                 */
197
                                ILineStyle tempLineStyle = (ILineStyle) sym.getLineStyle().clone(); 
198
                                lineProperties.setLinePropertiesStyle((BasicStroke) tempLineStyle.getStroke());
199
                        }
200
                } catch (IndexOutOfBoundsException ioEx) {
201
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
202
                } catch (ClassCastException ccEx) {
203
                        NotificationManager.addWarning("Illegal casting from " +
204
                                        layer.getClass().getName() + " to ISimpleLineSymbol.", ccEx);
205
                } catch (CloneNotSupportedException e) {
206
                        NotificationManager.addWarning(
207
                                        "Symbol line style does not support cloning", e);
208
                }
209
        }
210

    
211
        public void actionPerformed(ActionEvent e) {
212
                fireSymbolChangedEvent();
213
        }
214

    
215
        public EditorTool[] getEditorTools() {
216
                return null;
217
        }
218

    
219

    
220
        @Override
221
        public boolean canManageSymbol(ISymbol symbol) {
222
                return symbol instanceof ISimpleLineSymbol;
223
        }
224
}