Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / styling / GradientFill.java @ 25807

History | View | Annotate | Download (9.61 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
package org.gvsig.symbology.gui.styling;
42

    
43
import java.awt.Color;
44
import java.awt.FlowLayout;
45
import java.awt.GridLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.util.ArrayList;
49

    
50
import javax.swing.JCheckBox;
51
import javax.swing.JComponent;
52
import javax.swing.JPanel;
53

    
54
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
55
import org.gvsig.gui.beans.swing.JBlank;
56
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
57
import org.gvsig.gui.beans.swing.ValidatingTextField;
58
import org.gvsig.raster.datastruct.ColorItem;
59
import org.gvsig.symbology.fmap.symbols.GradientFillSymbol;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.cit.gvsig.fmap.core.FShape;
63
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
64
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
65
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
66
import com.iver.cit.gvsig.gui.styling.AbstractTypeSymbolEditor;
67
import com.iver.cit.gvsig.gui.styling.EditorTool;
68
import com.iver.cit.gvsig.gui.styling.JComboBoxColorScheme;
69
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
70
import com.iver.cit.gvsig.project.documents.view.legend.gui.JSymbolPreviewButton;
71
import com.iver.utiles.swing.JComboBox;
72

    
73
/**
74
* <b>GradientFill</b> allows to store and modify the properties that fills a
75
* polygon with a padding with a gradient<p>
76
* <p>
77
* This functionality is carried out thanks to a tab (gradient fill)which is included
78
* in the panel to edit the properities of a symbol (SymbolEditor)how is explained
79
* in AbstractTypeSymbolEditor.<p>
80
* <p>
81
* This tab permits the user to change the different properties of the gradient such
82
* as its style <b>gradientStyle</b>, angle of rotation <b>gradientAngle</b>,
83
* percentage <b>gradientPercentage</b>, intervals <b>gradientIntervals</b> and the outline <b>outline</b> of the polygon.Also the
84
* user can select the colors for the gradient <b>gradientColor</b>.
85
*
86
*
87
*
88
*@see AbstractTypeSymbolEditor
89
*@author pepe vidal salvador - jose.vidal.salvador@iver.es
90
*/
91
public class GradientFill extends AbstractTypeSymbolEditor implements ActionListener {
92

    
93
        private JIncrementalNumberField gradientIntervals;
94
        private JIncrementalNumberField gradientPercentage;
95
        private JIncrementalNumberField gradientAngle;
96
        private JComboBoxColorScheme gradientColor;
97
        private JComboBox gradientStyle;
98
        private JSymbolPreviewButton btnOutline;
99
        private ILineSymbol outline;
100
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
101
        private JCheckBox useBorder;
102

    
103
        /**
104
         * Constructor method
105
         * @param owner
106
         */
107
        public GradientFill(SymbolEditor owner) {
108
                super(owner);
109
                initialize();
110
        }
111
        /**
112
         * This method initializes this
113
         *
114
         */
115
        private void initialize() {
116

    
117
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
118
                myTab.setName(PluginServices.getText(this, "gradient_fill"));
119

    
120
                JPanel aux = new JPanel(new GridLayout(1, 2, 20, 5));
121

    
122
                GridBagLayoutPanel aux2;
123

    
124
                aux2= new GridBagLayoutPanel();
125

    
126
                aux2.addComponent(new JBlank(5,20));
127
                gradientIntervals = new JIncrementalNumberField("",
128
                                                                                                                10,
129
                                                                                                                ValidatingTextField.INTEGER_VALIDATOR,
130
                                                                                                                ValidatingTextField.NUMBER_CLEANER,
131
                                                                                                                1,
132
                                                                                                                100,
133
                                                                                                                1);
134
                aux2.addComponent(PluginServices.getText(this, "intervals")+":", gradientIntervals);
135
                aux2.addComponent(new JBlank(5,5));
136

    
137
                gradientPercentage = new JIncrementalNumberField("",10,0,100,1);
138
                aux2.addComponent(PluginServices.getText(this,"percentage")+":",gradientPercentage);
139
                gradientPercentage.setDouble(100);
140
                aux2.addComponent(new JBlank(5,5));
141

    
142
                gradientAngle = new JIncrementalNumberField("",10,0,360,1);
143
                aux2.addComponent(PluginServices.getText(this,"angle")+":",gradientAngle);
144

    
145
                aux.add(aux2);
146

    
147
                aux2 = new GridBagLayoutPanel();
148

    
149
                aux2.addComponent(new JBlank(5,20));
150
                gradientStyle = getCmbgradientStyle();
151
                aux2.addComponent(PluginServices.getText(this,"style")+":",gradientStyle);
152

    
153

    
154
                aux2.addComponent(new JBlank(5,5));
155
                useBorder = new JCheckBox(PluginServices.getText(this, "use_outline"));
156
                aux2.addComponent(useBorder);
157
                aux2.addComponent(PluginServices.getText(this, "outline")+":",
158
                                btnOutline = new JSymbolPreviewButton(FShape.LINE));
159
                aux2.addComponent(new JBlank(5,5));
160

    
161

    
162

    
163
                JPanel aux3 = new JPanel(new GridLayout(1, 2, 20, 5));
164

    
165
                GridBagLayoutPanel aux4 = new GridBagLayoutPanel();
166

    
167

    
168
                aux4.addComponent(new JBlank(5,5));
169
                aux4.addComponent(PluginServices.getText(this, "gradient_color")+":",
170
                                gradientColor = new JComboBoxColorScheme(false));
171

    
172

    
173
                int colorCount = gradientColor.getSelectedColors().length;
174
                gradientIntervals.setMaxValue(colorCount);
175

    
176

    
177
                aux3.add(aux4);
178
                aux.add(aux2);
179
                myTab.add(aux);
180
                myTab.add(aux3);
181

    
182

    
183

    
184
                gradientIntervals.addActionListener(this);
185
                gradientPercentage.addActionListener(this);
186
                gradientAngle.addActionListener(this);
187
                gradientColor.addActionListener(this);
188
                gradientStyle.addActionListener(this);
189
                btnOutline.addActionListener(this);
190
                useBorder.addActionListener(this);
191

    
192

    
193

    
194
                tabs.add(myTab);
195
        }
196

    
197
        public EditorTool[] getEditorTools() {
198
                return null;
199
        }
200

    
201
        public ISymbol getLayer() {
202

    
203
                GradientFillSymbol layer=new GradientFillSymbol();
204

    
205
                layer.setHasOutline(useBorder.isSelected());
206
                outline = (ILineSymbol) btnOutline.getSymbol();
207
                layer.setOutline(outline);
208

    
209
                layer.setStyle(gradientStyle.getSelectedIndex());
210
                layer.setIntervals(gradientIntervals.getInteger());
211

    
212
                if(gradientStyle.getSelectedIndex()==2) {
213
                        gradientAngle.setEnabled(false);
214
                }
215
                layer.setAngle(gradientAngle.getDouble()*FConstant.DEGREE_TO_RADIANS);
216
                layer.setPercentage(gradientPercentage.getDouble());
217

    
218

    
219
//                ColorItem[] array=new ColorItem [gradientColor.getSelectedColors().length];
220
//                array=gradientColor.getSelectedColors();
221
//                ColorItem[] selcolors = new ColorItem [gradientColor.getSelectedColors().length];;
222
//
223
//                for(int i=0;i<array.length;i++) {
224
//                        selcolors[i].setColor(array[i].getColor());
225
//                }
226

    
227
                if(gradientColor.getSelectedColors() != null) {
228
                        Color[] colors = new Color[gradientColor.getSelectedColors().length];
229
                        for (int i = 0; i < colors.length; i++) {
230
                                colors[i] = gradientColor.getSelectedColors()[i].getColor();
231
                        }
232
                        layer.setGradientColor(colors);
233
                }
234

    
235
                return layer;
236
        }
237

    
238
        public String getName() {
239
                return PluginServices.getText(GradientFill.class, "gradient_fill_symbol");
240
        }
241

    
242
        public Class getSymbolClass() {
243
                return GradientFillSymbol.class;
244
        }
245

    
246
        public JPanel[] getTabs() {
247
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
248
        }
249

    
250
        public void refreshControls(ISymbol layer) {
251

    
252
                gradientStyle.removeActionListener(this);
253
                GradientFillSymbol sym = (GradientFillSymbol) layer;
254
                gradientStyle.setSelectedIndex(sym.getStyle());
255

    
256
                outline=sym.getOutline();
257
                btnOutline.setSymbol(outline);
258
                useBorder.setSelected(sym.hasOutline());
259

    
260
                int colorCount = (sym.getGradientColor().length);
261
                gradientIntervals.setMaxValue(colorCount);
262
                gradientIntervals.setInteger(sym.getIntervals());
263
                gradientAngle.setDouble(sym.getAngle()/FConstant.DEGREE_TO_RADIANS);
264
                gradientPercentage.setDouble(sym.getPercentage());
265

    
266
                if(sym.getGradientColor() != null) {
267
                        ColorItem[] colors = new ColorItem[sym.getGradientColor().length];
268
                        for (int i = 0; i < sym.getGradientColor().length; i++) {
269
                                colors[i] = new ColorItem();
270
                                colors[i].setColor(sym.getGradientColor()[i]);
271
                        }
272
                        gradientColor.setSelectedColors(colors);
273
                }
274

    
275
                gradientStyle.addActionListener(this);
276
        }
277

    
278
        public void actionPerformed(ActionEvent e) {
279
                JComponent c = (JComponent) e.getSource();
280
                if (c.equals(gradientColor)) {
281

    
282
                        gradientIntervals.setMaxValue(gradientColor.getSelectedColors().length);
283

    
284
                        if (gradientIntervals.getInteger() > gradientColor.getSelectedColors().length)
285
                                gradientIntervals.setInteger(gradientColor.getSelectedColors().length);
286
                        else
287
                                gradientIntervals.setInteger(gradientIntervals.getInteger());
288

    
289
                }
290
                if (c.equals(gradientStyle)) {
291

    
292
                        if(gradientStyle.getSelectedIndex()==2) {
293
                                gradientAngle.setInteger(0);
294
                                gradientAngle.setEnabled(false);
295
                        }
296
                        else gradientAngle.setEnabled(true);
297
                }
298

    
299
                outline = (ILineSymbol) btnOutline.getSymbol();
300
                fireSymbolChangedEvent();
301
        }
302

    
303
        /**
304
         * Establishes the values for the JCombobox where the user can select the
305
         * different styles for the gradient
306
         * @return
307
         */
308
        private JComboBox getCmbgradientStyle() {
309
                if (gradientStyle == null) {
310
                        gradientStyle = new JComboBox(new String[] {
311
                                        PluginServices.getText(this, "buffered"),
312
                                        PluginServices.getText(this, "lineal"),
313
                                        PluginServices.getText(this, "circular"),
314
                                        PluginServices.getText(this, "rectangular")
315
                        });
316

    
317
                }
318

    
319
                return gradientStyle;
320
        }
321

    
322
}