Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SimpleLine.java @ 10437

History | View | Annotate | Download (7.69 KB)

1 9934 jaume
/* 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
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46 10437 jaume
* Revision 1.1.2.6  2007-02-21 07:35:14  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.5  2007/02/12 15:14:41  jaume
50 10274 jaume
* refactored interval legend and added graduated symbol legend
51
*
52
* Revision 1.1.2.4  2007/02/09 11:05:16  jaume
53 10231 jaume
* *** empty log message ***
54
*
55 10232 jaume
* Revision 1.1.2.3  2007/02/09 11:00:02  jaume
56
* *** empty log message ***
57
*
58 10231 jaume
* Revision 1.1.2.2  2007/02/08 15:43:05  jaume
59 10218 jaume
* some bug fixes in the editor and removed unnecessary imports
60
*
61
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
62 9934 jaume
* *** empty log message ***
63
*
64
*
65
*/
66
package com.iver.cit.gvsig.gui.styling;
67
68
import java.awt.BasicStroke;
69 10274 jaume
import java.awt.BorderLayout;
70 9934 jaume
import java.awt.Color;
71 10231 jaume
import java.awt.Component;
72 9934 jaume
import java.awt.Dimension;
73
import java.awt.FlowLayout;
74 10231 jaume
import java.awt.Graphics2D;
75
import java.awt.Rectangle;
76 9934 jaume
import java.awt.event.ActionEvent;
77
import java.awt.event.ActionListener;
78 10231 jaume
import java.awt.geom.AffineTransform;
79 9934 jaume
import java.text.NumberFormat;
80
import java.util.ArrayList;
81
82 10231 jaume
import javax.swing.DefaultListCellRenderer;
83
import javax.swing.JList;
84 9934 jaume
import javax.swing.JPanel;
85
86
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
87
88
import com.iver.andami.PluginServices;
89
import com.iver.andami.messages.NotificationManager;
90 10437 jaume
import com.iver.cit.gvsig.fmap.core.IStyle;
91
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
92 10231 jaume
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
93
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
94 10437 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
95 9934 jaume
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
96
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
97 10231 jaume
import com.iver.utiles.swing.JComboBox;
98 9934 jaume
99
import de.ios.framework.swing.JDecimalField;
100
101
public class SimpleLine extends AbstractTypeSymbolEditorPanel implements ActionListener {
102 10231 jaume
        private static final int PREDEFINED_STYLE_COUNT = 6;
103
        private static final int LINE_WIDTH = 10;
104
        private static final int DOT_WIDTH = 2;
105
106 9934 jaume
        private ColorChooserPanel jccColor;
107
        private JDecimalField txtWidth;
108
        // TODO faltar?a el style
109
        private ArrayList tabs = new ArrayList();
110 10231 jaume
        private JComboBox cmbLineStyles;
111
        private int endCap;
112
        private int lineJoin;
113
        private float miterlimit;
114
        private float[][] dash = new float[PREDEFINED_STYLE_COUNT][];
115 9934 jaume
116 10231 jaume
        {
117
                BasicStroke dummy = new BasicStroke();
118
                endCap = dummy.getEndCap();
119
                lineJoin = dummy.getLineJoin();
120
                miterlimit = dummy.getMiterLimit();
121
        }
122 9934 jaume
123
        public SimpleLine(SymbolEditor owner) {
124
                super(owner);
125
                initialize();
126
        }
127
128
        private void initialize() {
129
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
130
                myTab.setName(PluginServices.getText(this, "simple_fill"));
131 10274 jaume
                JPanel aux2 = new JPanel(new BorderLayout(5,5));
132 9934 jaume
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
133
134
                // color chooser
135
                jccColor = new ColorChooserPanel();
136
                jccColor.setAlpha(255);
137
138
                aux.addComponent(PluginServices.getText(this, "color")+":",
139
                                jccColor        );
140
141
                // line width
142
                txtWidth = new JDecimalField(25);
143
                aux.addComponent(PluginServices.getText(this, "width")+":",
144
                                txtWidth );
145
                aux.setPreferredSize(new Dimension(300, 300));
146 10231 jaume
147
                // line style combo
148
                cmbLineStyles = new JComboBox();
149
                cmbLineStyles.setRenderer(new DefaultListCellRenderer() {
150
                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
151
                                Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
152
                                if (value == null)
153
                                        return c;
154
                                final SimpleLineStyle style = (SimpleLineStyle) value;
155
                                SymbolPreviewer sp = new SymbolPreviewer();
156
157
                                SimpleLineSymbol sym = new SimpleLineSymbol() {
158
                                        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
159
                                                style.drawInsideRectangle(g, r);
160
                                        }
161
                                };
162
                                sp.setPreferredSize(c.getPreferredSize());
163
                                sp.setSymbol(sym);
164
                                return sp;
165
                        };
166
                });
167 10274 jaume
                aux2.add(aux);
168
                myTab.add(aux2);
169 10231 jaume
                aux.addComponent(PluginServices.getText(this, "style")+":",
170
                                cmbLineStyles);
171 9934 jaume
172 10274 jaume
173 9934 jaume
                // initialize defaults
174
                jccColor.setColor(Color.BLACK);
175
                txtWidth.setText(NumberFormat.getInstance().format(1.0));
176 10274 jaume
                refreshCmbStyles();
177 9934 jaume
178
                jccColor.addActionListener(this);
179
                txtWidth.addActionListener(this);
180
                tabs.add(myTab);
181
        }
182
183 10231 jaume
        private void refreshCmbStyles() {
184
                float width = txtWidth.getValue().floatValue();
185
186
                dash[0] = new BasicStroke().getDashArray(); // line (no dash)
187
188 10274 jaume
                dash[1] = new float[] {        LINE_WIDTH        }; // lines
189 10231 jaume
190
191 10274 jaume
                dash[2] = new float[] {        DOT_WIDTH }; // dots
192 10231 jaume
193
194 10274 jaume
                dash[3] = new float[] {        LINE_WIDTH,
195
                                                                DOT_WIDTH        }; // line + dot
196 10231 jaume
197 10274 jaume
                dash[4] = new float[] {        LINE_WIDTH,
198
                                                                DOT_WIDTH,
199
                                                                DOT_WIDTH}; // line + dot + dot
200
                dash[5] = new float[] {        LINE_WIDTH,
201
                                                                LINE_WIDTH,
202
                                                                DOT_WIDTH,
203
                                                                DOT_WIDTH}; // line + line + dot + dot
204 10231 jaume
205
                ILineStyle[] styles = new ILineStyle[PREDEFINED_STYLE_COUNT];
206
                for (int i = 0; i < styles.length; i++) {
207
                        styles[i] = new SimpleLineStyle(width, endCap, lineJoin, miterlimit, dash[i], width*5);
208
                }
209
210
                cmbLineStyles.removeAll();
211
                for (int i = 0; i < styles.length; i++) {
212
                        cmbLineStyles.addItem(styles[i]);
213
                }
214 10232 jaume
215 10231 jaume
        };
216
217 9934 jaume
        public ISymbol getLayer() {
218
                SimpleLineSymbol s = new SimpleLineSymbol();
219
                s.setColor(jccColor.getColor());
220
                s.setIsShapeVisible(true);
221 10437 jaume
                // clone the selected style in the combo box
222
                ILineStyle sty = (ILineStyle) SymbologyFactory.createStyleFromXML(
223
                                ((IStyle) cmbLineStyles.getSelectedItem()).getXMLEntity(), null);
224
                sty.setLineWidth(txtWidth.getValue().floatValue());
225
                s.setLineStyle(sty);
226 9934 jaume
                return s;
227
        }
228
229
        public String getName() {
230
                return PluginServices.getText(this, "simple_line");
231
        }
232
233
        public JPanel[] getTabs() {
234
                return (JPanel[]) tabs.toArray(new JPanel[0]);
235
        }
236
237
        public void refreshControls(ISymbol layer) {
238
                SimpleLineSymbol sym;
239
                try {
240
                        if (layer == null) {
241
                                // initialize defaults
242
                                System.err.println("SimpleLine.java:: should be unreachable code");
243
                                jccColor.setColor(Color.BLACK);
244
                                txtWidth.setText(NumberFormat.getInstance().format(1.0));
245
                        } else {
246
                                sym = (SimpleLineSymbol) layer;
247
                                jccColor.setColor(sym.getColor());
248
                                txtWidth.setText(String.valueOf(
249 10231 jaume
                                                sym.getLineStyle().getLineWidth()));
250 9934 jaume
                        }
251
                } catch (IndexOutOfBoundsException ioEx) {
252
                        NotificationManager.addWarning("Symbol layer index out of bounds", ioEx);
253
                } catch (ClassCastException ccEx) {
254
                        NotificationManager.addWarning("Illegal casting from " +
255
                                        layer.getClassName() + " to " + getSymbolClass().
256
                                        getName() + ".", ccEx);
257
                }
258
        }
259
260
        public Class getSymbolClass() {
261
                return SimpleLineSymbol.class;
262
        }
263
264
        public void actionPerformed(ActionEvent e) {
265 10231 jaume
                refreshCmbStyles();
266 9934 jaume
                fireSymbolChangedEvent();
267
        }
268
}