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 / MarkerFill.java @ 41399

History | View | Annotate | Download (10.2 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

    
25
package org.gvsig.app.gui.styling;
26

    
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.ButtonGroup;
35
import javax.swing.JCheckBox;
36
import javax.swing.JComponent;
37
import javax.swing.JPanel;
38
import javax.swing.JRadioButton;
39
import javax.swing.JSlider;
40
import javax.swing.event.ChangeEvent;
41
import javax.swing.event.ChangeListener;
42

    
43
import org.gvsig.andami.PluginServices;
44
import org.gvsig.andami.messages.NotificationManager;
45
import org.gvsig.app.gui.panels.ColorChooserPanel;
46
import org.gvsig.app.project.documents.view.legend.gui.ISymbolSelector;
47
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
51
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
52
import org.gvsig.gui.beans.swing.JBlank;
53
import org.gvsig.gui.beans.swing.JButton;
54
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
55
import org.gvsig.i18n.Messages;
56
import org.gvsig.symbology.SymbologyLocator;
57
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IMarkerFillSymbol;
58
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
59
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
60
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMarkerFillPropertiesStyle;
61
/**
62
 * <b>MarkerFill</b> allows the user to store and modify the properties that fills a
63
 * polygon with a padding made of markers and an outline<p>
64
 * <p>
65
 * This functionality is carried out thanks to two tabs (marker fill and MarkerFillProperties)
66
 * which are included in the panel to edit the properities of a symbol (SymbolEditor)
67
 * how is explained in AbstractTypeSymbolEditor.<p>
68
 * <p>
69
 * The first tab (marker fill)permits the user to select the marker for the padding and
70
 * other options such as the color for the fill (<b>btnChooseMarker</b>),to select the
71
 * ouline (<b>btnOutline</b>)and the distribution (grid or random) of the marker inside
72
 * the padding (<b>rdGrid,rdRandom</b>).
73
 * <p>
74
 * The second tab is implementes as a MarkerFillProperties class and offers the possibilities
75
 * to change the separtion and the offset.
76
 *
77
 *
78
 *@see MarkerFillProperties
79
 *@see AbstractTypeSymbolEditor
80
 *@author jaume dominguez faus - jaume.dominguez@iver.es
81
 */
82
public class MarkerFill extends AbstractTypeSymbolEditor implements ActionListener,ChangeListener {
83
        private ArrayList<JPanel> tabs = new ArrayList<JPanel>();
84
        private ColorChooserPanel markerCC;
85
        private JButton btnChooseMarker;
86
        private MarkerFillProperties panelStyle = new MarkerFillProperties();
87
        private JRadioButton rdGrid;
88
        private JRadioButton rdRandom;
89
        private IMarkerSymbol marker = SymbologyLocator.getSymbologyManager().createSimpleMarkerSymbol();
90

    
91
        private JIncrementalNumberField txtOutlineWidth;
92
        private JSymbolPreviewButton btnOutline;
93
        private JSlider sldOutlineTransparency;
94
        private int outlineAlpha = 255;
95
        private ILineSymbol outline;
96
        private JCheckBox useBorder;
97

    
98
        /**
99
         * constructor method
100
         * @param owner
101
         */
102
        public MarkerFill(SymbolEditor owner) {
103
                super(owner);
104
                initialize();
105
        }
106

    
107
        /**
108
         * Initializes the parameters that allows the user to fill the padding of
109
         * a polygon with a style made of markers.To do it, two tabs are created (marker
110
         * fill and MarkerFillProperties)inside the SymbolEditor panel with default values
111
         * for the different attributes.
112
         */
113
        private void initialize() {
114
//                GridLayout layout;
115
                JPanel myTab;
116
                // Marker fill tab
117

    
118
                {
119
                        myTab = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
120
                        myTab.setName(Messages.getText("marker_fill"));
121

    
122
                        GridBagLayoutPanel p = new GridBagLayoutPanel();
123
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
124
                        markerCC = new ColorChooserPanel(true);
125
                        markerCC.setAlpha(255);
126
                        markerCC.addActionListener(this);
127
                        aux.add(markerCC);
128

    
129
                        p.addComponent(Messages.getText("color")+":", aux);
130
                        btnChooseMarker = new JButton(Messages.getText("choose_marker"));
131
                        btnChooseMarker.addActionListener(this);
132
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
133
                        aux.add(btnChooseMarker);
134
                        p.addComponent("", aux);
135

    
136
                        ButtonGroup group = new ButtonGroup();
137
                        rdGrid = new JRadioButton(Messages.getText("grid"));
138
                        rdGrid.addActionListener(this);
139
                        rdRandom = new JRadioButton(Messages.getText("random"));
140
                        rdRandom.addActionListener(this);
141
                        group.add(rdGrid);
142
                        group.add(rdRandom);
143

    
144
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
145
                        aux.add(rdGrid);
146
                        aux.add(rdRandom);
147
                        rdGrid.setSelected(true);
148
                        p.addComponent("", aux);
149

    
150

    
151
                        JPanel myTab2 = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,5));
152
                        GridBagLayoutPanel aux3 = new GridBagLayoutPanel();
153

    
154
                        JPanel aux2 = new JPanel();
155
                        btnOutline = new JSymbolPreviewButton(Geometry.TYPES.CURVE);
156
                        btnOutline.setPreferredSize(new Dimension(100, 35));
157
                        aux2.add(btnOutline);
158

    
159
                        aux3.addComponent(new JBlank(10, 10));
160
                        useBorder = new JCheckBox(Messages.getText("use_outline"));
161
                        aux3.addComponent(useBorder, aux2);
162
                        aux3.addComponent(new JBlank(10, 10));
163

    
164
                        sldOutlineTransparency = new JSlider();
165
                        sldOutlineTransparency.setValue(100);
166
                        aux3.addComponent(Messages.getText("outline")+":",
167
                                        aux2);
168
                        aux3.addComponent(Messages.getText("outline_opacity")+":", sldOutlineTransparency);
169
                        txtOutlineWidth = new JIncrementalNumberField("", 25, 0, Double.MAX_VALUE, 1);
170
                        aux3.addComponent(Messages.getText("outline_width")+":", txtOutlineWidth);
171
                        myTab2.add(aux3);
172

    
173
                        p.addComponent("", myTab2);
174
                        myTab.add(p);
175

    
176
                        useBorder.addActionListener(this);
177
                        btnOutline.addActionListener(this);
178
                        txtOutlineWidth.addActionListener(this);
179
                        sldOutlineTransparency.addChangeListener(this);
180

    
181
                }
182
                tabs.add(myTab);
183

    
184
                // Fill properties tab
185
                tabs.add(panelStyle);
186
                panelStyle.addActionListener(this);
187
        }
188

    
189
        public void refreshControls(ISymbol layer) {
190
                if (layer == null) {
191
                        System.err.println(getClass().getName()+":: should be unreachable code");
192
                        // set defaults
193
                        markerCC.setColor(Color.BLACK);
194
                        rdGrid.setSelected(true);
195
                        rdRandom.setSelected(false);
196
                } else {
197

    
198
                        IMarkerFillSymbol mfs = (IMarkerFillSymbol) layer;
199
                        int fillStyle = mfs.getMarkerFillProperties().getFillStyle();
200
                        marker = mfs.getMarker();
201
                        rdGrid.setSelected(fillStyle == IMarkerFillPropertiesStyle.GRID_FILL);
202
                        rdRandom.setSelected(fillStyle == IMarkerFillPropertiesStyle.RANDOM_FILL);
203
                        panelStyle.setModel(mfs.getMarkerFillProperties());
204
                        markerCC.setColor(marker.getColor());
205

    
206
                        //outline
207
                        sldOutlineTransparency.removeChangeListener(this);
208

    
209
                        outline=mfs.getOutline();
210
                        btnOutline.setSymbol(outline);
211
                        useBorder.setSelected(mfs.hasOutline());
212

    
213
                        if (outline != null) {
214
                                outlineAlpha = outline.getAlpha();
215
                                sldOutlineTransparency.setValue((int)((outlineAlpha/255D)*100));
216
                                txtOutlineWidth.setDouble(outline.getLineWidth());
217
                        } else {
218
                                sldOutlineTransparency.setValue(100);
219
                        }
220

    
221
                        sldOutlineTransparency.addChangeListener(this);
222

    
223

    
224

    
225
                }
226
        }
227

    
228
        public String getName() {
229
                return Messages.getText("marker_fill_symbol");
230
        }
231

    
232
        public JPanel[] getTabs() {
233
                return (JPanel[]) tabs.toArray(new JPanel[0]);
234
        }
235

    
236
        public void actionPerformed(ActionEvent e) {
237

    
238
                JComponent comp = (JComponent) e.getSource();
239
                if (comp.equals(btnChooseMarker)) {
240
                        ISymbolSelector symSelect = SymbolSelector.createSymbolSelector(marker, Geometry.TYPES.POINT);
241
                        PluginServices.getMDIManager().addWindow(symSelect);
242
                        marker = (IMarkerSymbol) symSelect.getSelectedObject();
243

    
244
                        if (marker == null) return;
245

    
246
                }
247

    
248
                if (!(marker instanceof IMultiLayerSymbol)) {
249
                        marker.setColor(markerCC.getColor());
250
                }
251

    
252
                if (comp.equals(btnOutline)) {
253
                        ISymbol sym = btnOutline.getSymbol();
254
                        if (sym instanceof ILineSymbol) {
255
                                ILineSymbol outline = (ILineSymbol) sym;
256
                                if (outline != null)
257
                                        txtOutlineWidth.setDouble(outline.getLineWidth());
258
                                }
259

    
260
                }
261

    
262
                fireSymbolChangedEvent();
263
        }
264

    
265
        public ISymbol getLayer() {
266
                IMarkerFillSymbol mfs = SymbologyLocator.getSymbologyManager().createMarkerFillSymbol();
267
                IMarkerFillPropertiesStyle prop = panelStyle.getMarkerFillProperties();
268
                prop.setFillStyle(rdGrid.isSelected() ?
269
                                IMarkerFillPropertiesStyle.GRID_FILL : IMarkerFillPropertiesStyle.RANDOM_FILL);
270

    
271
                IMarkerSymbol myMarker;
272
                try {
273
                        myMarker = (IMarkerSymbol) marker.clone();
274
                        mfs.setMarker(myMarker);
275
                } catch (CloneNotSupportedException e) {
276
                        NotificationManager.addWarning(
277
                                        "Marker Symbol does not support cloning", e);
278
                }
279

    
280
                mfs.setMarkerFillProperties(prop);
281

    
282
                mfs.setHasOutline(useBorder.isSelected());
283
                outline = (ILineSymbol) btnOutline.getSymbol();
284

    
285
                if (outline!=null) {
286
                        outline.setLineWidth(txtOutlineWidth.getDouble());
287
                        outline.setAlpha(outlineAlpha);
288
                }
289

    
290
                mfs.setOutline(outline);
291

    
292
                return mfs;
293
        }
294

    
295
        public EditorTool[] getEditorTools() {
296
                return null;
297
        }
298

    
299
        public void stateChanged(ChangeEvent e) {
300
                Object s = e.getSource();
301

    
302
                if (s.equals(sldOutlineTransparency)) {
303
                        outlineAlpha = (int) (255*(sldOutlineTransparency.getValue()/100.0));
304
                }
305

    
306
                outline = (ILineSymbol) btnOutline.getSymbol();
307
                fireSymbolChangedEvent();
308

    
309
        }
310

    
311
        @Override
312
        public boolean canManageSymbol(ISymbol symbol) {
313
                return symbol instanceof IMarkerFillSymbol;
314
        }
315
}