Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / layerproperties / DefaultLabeling.java @ 23138

History | View | Annotate | Download (8.38 KB)

1
/* 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
package org.gvsig.symbology.gui.layerproperties;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.FlowLayout;
45
import java.awt.IllegalComponentStateException;
46
import java.awt.Rectangle;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.beans.PropertyChangeEvent;
50
import java.util.Random;
51

    
52
import javax.swing.JPanel;
53
import javax.swing.JSplitPane;
54

    
55
import org.gvsig.gui.beans.swing.JBlank;
56
import org.gvsig.gui.beans.swing.JButton;
57
import org.gvsig.symbology.fmap.labeling.GeneralLabelingStrategy;
58
import org.gvsig.symbology.fmap.labeling.OnSelectionLabeled;
59
import org.gvsig.symbology.gui.styling.LayerPreview;
60

    
61
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.messages.NotificationManager;
64
import com.iver.cit.gvsig.fmap.core.IFeature;
65
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
66
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
67
import com.iver.cit.gvsig.fmap.layers.FBitSet;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
70
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
71
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
72
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
73
import com.iver.cit.gvsig.fmap.layers.SelectionListener;
74
import com.iver.cit.gvsig.fmap.layers.SelectionSupport;
75
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
76
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.DefaultLabelingMethod;
77
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingMethod;
78
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
79
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints;
80
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IZoomConstraints;
81
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass;
82
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelingFactory;
83

    
84
public class DefaultLabeling extends AbstractLabelingMethodPanel implements ActionListener {
85
        private static final long serialVersionUID = 7100208944546698724L;
86
        private LabelClass defaultLabel;
87
        private IPlacementConstraints placementConstraints;
88
        private LayerPreview layerPrev;
89
        private LabelClassPreview labelPrev;
90
        private JButton btnProperties;
91
        private String[] fieldNames;
92
        private int[] fieldTypes;
93
        private boolean allowOverlap;
94
        private IZoomConstraints zoomConstraints;
95
        @Override
96
        public Class<? extends ILabelingMethod> getLabelingMethodClass() {
97
                return DefaultLabelingMethod.class;
98
        }
99

    
100
        @Override
101
        public String getName() {
102
                return PluginServices.getText(this, "label_features_in_the_same_way")+".";
103
        }
104

    
105
        @Override
106
        public void fillPanel(ILabelingMethod method, SelectableDataSource dataSource) {
107
                try {
108
                        layerPrev.setLayer(layer);
109
                        fieldNames = dataSource.getFieldNames();
110
                        fieldTypes = new int[fieldNames.length];
111
                        for (int i = 0; i < fieldTypes.length; i++) {
112
                                fieldTypes[i] = dataSource.getFieldType(i);
113
                        }
114
                        ILabelingStrategy labeling = layer.getLabelingStrategy();
115
                        if (!(labeling instanceof GeneralLabelingStrategy)) {
116
                                labeling = new GeneralLabelingStrategy();
117
                                // (!!)
118
                                labeling.setLayer(layer);
119
                                layer.setLabelingStrategy(labeling);
120
                        }
121
                        else {
122
                                labeling = (GeneralLabelingStrategy) layer.getLabelingStrategy();
123
                        }
124

    
125
                } catch (Exception e) {
126
                        NotificationManager.addWarning(e.getMessage(), e);
127
                }
128

    
129
                LabelClass lc = null;
130
                if (method.getLabelClasses() != null && method.getLabelClasses().length > 0) {
131
                        lc = method.getLabelClasses()[0];
132
                } else {
133
                        lc = new LabelClass();
134
                }
135
                setLabel(lc);
136
        }
137

    
138
        @Override
139
        protected void initializePanel() {
140
                setLayout(new BorderLayout());
141
                JSplitPane scrl = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
142
                scrl.add(layerPrev = new LayerPreview(), JSplitPane.LEFT);
143

    
144
                labelPrev = new LabelClassPreview();
145
                JPanel aux = new JPanel(new BorderLayout());
146
                aux.add(new JBlank(10, 10), BorderLayout.NORTH );
147
                aux.add(new JBlank(10, 10), BorderLayout.WEST );
148
                aux.add(labelPrev, BorderLayout.CENTER);
149
                aux.add(new JBlank(10, 10), BorderLayout.EAST );
150
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
151
                aux2.add(btnProperties = new JButton(PluginServices.getText(this, "properties")));
152
                btnProperties.addActionListener(this);
153
                aux.add(aux2, BorderLayout.SOUTH);
154
                scrl.add(aux, JSplitPane.RIGHT);
155
                add(scrl, BorderLayout.CENTER);
156
                scrl.setDividerLocation(500);
157

    
158
        }
159

    
160
        public void actionPerformed(ActionEvent e) {
161
                if (e.getSource().equals(btnProperties)) {
162
                        LabelClassProperties lcProp = new LabelClassProperties(fieldNames, fieldTypes);
163
                        lcProp.setLabelClass(method.getLabelClasses()[0]);
164
                        PluginServices.getMDIManager().addWindow(lcProp);
165
                        setLabel(lcProp.getLabelClass());
166
                }
167
        }
168

    
169
        private void setLabel(LabelClass labelClass) {
170
                defaultLabel = LabelingFactory.createLabelClassFromXML(labelClass.getXMLEntity());
171
                labelPrev.setLabelClass(defaultLabel);
172
                method = newMethodForThePreview(defaultLabel);
173

    
174
                updatePreview();
175

    
176
        }
177

    
178
        protected ILabelingMethod newMethodForThePreview(LabelClass defaultLabel) {
179
                return new DefaultLabelingMethod(defaultLabel);
180
        }
181

    
182
        private void updatePreview() {
183
                GeneralLabelingStrategy s = (GeneralLabelingStrategy) layer.
184
                getLabelingStrategy();
185
                s.setLabelingMethod(newMethodForThePreview(defaultLabel));
186
//                s.setPlacementConstraints(placementConstraints);
187
//                s.setAllowOverlapping(allowOverlap);
188
//                s.setZoomConstraints(zoomConstraints);
189

    
190
                layer.setIsLabeled(true);
191

    
192
                /* If the selected labeling method is OnSelectionLabeled we have to select some rows
193
                 * in the previous sample image*/
194
                if(s.getLabelingMethod() instanceof OnSelectionLabeled) {
195

    
196
                        try {
197

    
198
                                IFeatureIterator it = layer.getSource().getFeatureIterator();
199
                                SelectionSupport selectionSupport = new SelectionSupport();
200
                                FBitSet bitSet = new FBitSet();
201
                                long rowCount = layer.getRecordset().getRowCount();
202

    
203
                                for (int i = 0; i <= rowCount/4; i++) {
204
                                        bitSet.set(i);
205
                                }
206

    
207
                                selectionSupport.setSelection(bitSet);
208
                                layer.getRecordset().setSelectionSupport(selectionSupport);
209

    
210
                        } catch (ReadDriverException e) {
211
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
212
                        }
213
                }
214

    
215

    
216
                try {
217
                        Rectangle r = layerPrev.getBounds();
218
                        r.setLocation(layerPrev.getLocationOnScreen());
219
                        layerPrev.paintImmediately(r);
220
                        layerPrev.doLayout();
221
                } catch (IllegalComponentStateException ex) {
222
                        // this happens when the component is not showing in the
223
                        // screen. If that is the case, then we don't need to do
224
                        // anything.
225
                }
226

    
227
        }
228

    
229
        public void propertyChange(PropertyChangeEvent evt) {
230
                String prop = evt.getPropertyName();
231
                GeneralLabelingStrategy s = (GeneralLabelingStrategy) layer.
232
                getLabelingStrategy();
233

    
234
                if (AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS.equals(prop)) {
235
                        placementConstraints = (IPlacementConstraints) evt.getNewValue();
236
                        s.setPlacementConstraints(placementConstraints);
237
                } else if (AbstractLabelingMethodPanel.ALLOW_OVERLAP.equals(prop)) {
238
                        allowOverlap = (Boolean) evt.getNewValue();
239
                        s.setAllowOverlapping(allowOverlap);
240
                } else if (AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS.equals(prop)) {
241
                        zoomConstraints = (IZoomConstraints) evt.getNewValue();
242
                        s.setZoomConstraints(zoomConstraints);
243
                }
244

    
245
                updatePreview();
246
        }
247
}