Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / GradientFill.java @ 16421

History | View | Annotate | Download (8.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 com.iver.cit.gvsig.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.JComponent;
51
import javax.swing.JPanel;
52

    
53
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
54
import org.gvsig.gui.beans.swing.JBlank;
55
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
56
import org.gvsig.gui.beans.swing.ValidatingTextField;
57
import org.gvsig.raster.datastruct.ColorItem;
58

    
59
import com.iver.andami.PluginServices;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.symbols.GradientFillSymbol;
62
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
63
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
64
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
65
import com.iver.cit.gvsig.project.documents.view.legend.gui.JSymbolPreviewButton;
66
import com.iver.utiles.swing.JComboBox;
67

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

    
88
        private JIncrementalNumberField gradientIntervals;
89
        private JIncrementalNumberField gradientPercentage;
90
        private JIncrementalNumberField gradientAngle;
91
        private JComboBoxColorScheme gradientColor;
92
        private JComboBox gradientStyle;
93
        private JSymbolPreviewButton btnOutline;
94
        private ILineSymbol outline;
95
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
96
        
97
        /**
98
         * Constructor method
99
         * @param owner
100
         */
101
        public GradientFill(SymbolEditor owner) {
102
                super(owner);
103
                initialize();
104
        }
105
        /**
106
         * This method initializes this
107
         *
108
         */
109
        private void initialize() {
110

    
111
                JPanel myTab = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
112
                myTab.setName(PluginServices.getText(this, "gradient_fill"));
113

    
114
                JPanel aux = new JPanel(new GridLayout(1, 2, 20, 5));
115

    
116
                GridBagLayoutPanel aux2;
117

    
118
                aux2= new GridBagLayoutPanel();
119

    
120
                aux2.addComponent(new JBlank(5,20));
121
                gradientIntervals = new JIncrementalNumberField("",
122
                                                                                                                10,
123
                                                                                                                ValidatingTextField.INTEGER_VALIDATOR,
124
                                                                                                                ValidatingTextField.NUMBER_CLEANER,
125
                                                                                                                1,
126
                                                                                                                100,
127
                                                                                                                1);
128
                aux2.addComponent(PluginServices.getText(this, "intervals"), gradientIntervals);
129
                aux2.addComponent(new JBlank(5,5));
130

    
131
                gradientPercentage = new JIncrementalNumberField("",10,0,100,1);
132
                aux2.addComponent(PluginServices.getText(this,"percentage"),gradientPercentage);
133
                gradientPercentage.setDouble(100);
134
                aux2.addComponent(new JBlank(5,5));
135

    
136
                gradientAngle = new JIncrementalNumberField("",10,0,360,1);
137
                aux2.addComponent(PluginServices.getText(this,"angle"),gradientAngle);
138

    
139
                aux.add(aux2);
140

    
141
                aux2 = new GridBagLayoutPanel();
142

    
143
                aux2.addComponent(new JBlank(5,20));
144
                gradientStyle = getCmbgradientStyle();
145
                aux2.addComponent(PluginServices.getText(this,"style"),gradientStyle);
146

    
147

    
148
                aux2.addComponent(new JBlank(5,5));
149
                aux2.addComponent(PluginServices.getText(this, "outline")+":",
150
                                btnOutline = new JSymbolPreviewButton(FShape.LINE));
151
                aux2.addComponent(new JBlank(5,5));
152

    
153

    
154

    
155
                JPanel aux3 = new JPanel(new GridLayout(1, 2, 20, 5));
156

    
157
                GridBagLayoutPanel aux4 = new GridBagLayoutPanel();
158

    
159

    
160
                aux4.addComponent(new JBlank(5,5));
161
                aux4.addComponent(PluginServices.getText(this, "gradient_color")+":",
162
                                gradientColor = new JComboBoxColorScheme(false));
163

    
164

    
165
                int colorCount = gradientColor.getSelectedColors().length;
166
                gradientIntervals.setMaxValue(colorCount);
167

    
168

    
169
                aux3.add(aux4);
170
                aux.add(aux2);
171
                myTab.add(aux);
172
                myTab.add(aux3);
173

    
174

    
175

    
176
                gradientIntervals.addActionListener(this);
177
                gradientPercentage.addActionListener(this);
178
                gradientAngle.addActionListener(this);
179
                gradientColor.addActionListener(this);
180
                gradientStyle.addActionListener(this);
181
                btnOutline.addActionListener(this);
182

    
183

    
184

    
185
                tabs.add(myTab);
186
        }
187

    
188
        public EditorTool[] getEditorTools() {
189
                return null;
190
        }
191

    
192
        public ISymbol getLayer() {
193

    
194
                GradientFillSymbol layer=new GradientFillSymbol();
195
                layer.setOutline(outline);
196
                layer.setStyle(gradientStyle.getSelectedIndex());
197
                layer.setIntervals(gradientIntervals.getInteger());
198

    
199
                if(gradientStyle.getSelectedIndex()==2) {
200
                        gradientAngle.setEnabled(false);
201
                }
202
                layer.setAngle(gradientAngle.getDouble()*FConstant.DEGREE_TO_RADIANS);
203
                layer.setPercentage(gradientPercentage.getDouble());
204

    
205

    
206
                ColorItem[] array=new ColorItem [gradientColor.getSelectedColors().length];
207
                array=gradientColor.getSelectedColors();
208
                Color[] selcolors = new Color [gradientColor.getSelectedColors().length];;
209

    
210
                for(int i=0;i<array.length;i++) {
211
                        selcolors[i]=array[i].getColor();
212
                }
213

    
214
                layer.setGradientColor(selcolors);
215

    
216
                layer.setindexgradientcolor(gradientColor.getSelectedIndex());
217

    
218
                return layer;
219
        }
220

    
221
        public String getName() {
222
                return PluginServices.getText(GradientFill.class, "gradient_fill_symbol");
223
        }
224

    
225
        public Class getSymbolClass() {
226
                return GradientFillSymbol.class;
227
        }
228

    
229
        public JPanel[] getTabs() {
230
                return (JPanel[]) tabs.toArray(new JPanel[tabs.size()]);
231
        }
232

    
233
        public void refreshControls(ISymbol layer) {
234

    
235
                gradientStyle.removeActionListener(this);
236
                GradientFillSymbol sym = (GradientFillSymbol) layer;
237
                gradientStyle.setSelectedIndex(sym.getStyle());
238
                outline = sym.getOutline();
239
                btnOutline.setSymbol(outline);
240

    
241
                int colorCount = (sym.getGradientColor().length);
242
                gradientIntervals.setMaxValue(colorCount);
243
                gradientIntervals.setInteger(sym.getIntervals());
244
                gradientAngle.setDouble(sym.getAngle()/FConstant.DEGREE_TO_RADIANS);
245
                gradientPercentage.setDouble(sym.getPercentage());
246
                gradientColor.setSelectedIndex(sym.getindexgradientcolor());
247

    
248
                gradientStyle.addActionListener(this);
249
        }
250

    
251
        public void actionPerformed(ActionEvent e) {
252
                JComponent c = (JComponent) e.getSource();
253
                if (c.equals(gradientColor)) {
254

    
255
                        gradientIntervals.setMaxValue(gradientColor.getSelectedColors().length);
256

    
257
                        if (gradientIntervals.getInteger() > gradientColor.getSelectedColors().length)
258
                                gradientIntervals.setInteger(gradientColor.getSelectedColors().length);
259
                        else
260
                                gradientIntervals.setInteger(gradientIntervals.getInteger());
261

    
262
                }
263
                if (c.equals(gradientStyle)) {
264

    
265
                        if(gradientStyle.getSelectedIndex()==2) {
266
                                gradientAngle.setInteger(0);
267
                                gradientAngle.setEnabled(false);
268
                        }
269
                        else gradientAngle.setEnabled(true);
270
                }
271

    
272
                outline = (ILineSymbol) btnOutline.getSymbol();
273
                fireSymbolChangedEvent();
274
        }
275

    
276
        /**
277
         * Establishes the values for the JCombobox where the user can select the
278
         * different styles for the gradient
279
         * @return
280
         */
281
        private JComboBox getCmbgradientStyle() {
282
                if (gradientStyle == null) {
283
                        gradientStyle = new JComboBox(new String[] {
284
                                        PluginServices.getText(this, "buffered"),
285
                                        PluginServices.getText(this, "lineal"),
286
                                        PluginServices.getText(this, "circular"),
287
                                        PluginServices.getText(this, "rectangular")
288
                        });
289

    
290
                }
291

    
292
                return gradientStyle;
293
        }
294

    
295
}