Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / gui / layerproperties / DefaultLabeling.java @ 40673

History | View | Annotate | Download (10.9 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.labeling.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.awt.event.ItemEvent;
50
import java.awt.event.ItemListener;
51
import java.beans.PropertyChangeEvent;
52

    
53
import javax.swing.JButton;
54
import javax.swing.JCheckBox;
55
import javax.swing.JOptionPane;
56
import javax.swing.JPanel;
57
import javax.swing.JSplitPane;
58

    
59
import org.gvsig.andami.messages.NotificationManager;
60
import org.gvsig.app.ApplicationLocator;
61
import org.gvsig.fmap.dal.exception.DataException;
62
import org.gvsig.fmap.dal.exception.ReadException;
63
import org.gvsig.fmap.dal.feature.Feature;
64
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
65
import org.gvsig.fmap.dal.feature.FeatureSelection;
66
import org.gvsig.fmap.dal.feature.FeatureType;
67
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
68
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
69
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
70
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
71
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints;
72
import org.gvsig.gui.beans.swing.JBlank;
73
import org.gvsig.i18n.Messages;
74
import org.gvsig.labeling.gui.styling.LayerPreview;
75
import org.gvsig.labeling.label.GeneralLabelingStrategy;
76
import org.gvsig.labeling.lang.LabelClassUtils;
77
import org.gvsig.symbology.SymbologyLocator;
78
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.DefaultLabelingMethod;
79
import org.gvsig.tools.dispose.DisposableIterator;
80
import org.gvsig.tools.evaluator.EvaluatorException;
81
import org.gvsig.tools.evaluator.EvaluatorWithDescriptions;
82
import org.slf4j.Logger;
83
import org.slf4j.LoggerFactory;
84

    
85

    
86
public class DefaultLabeling extends AbstractLabelingMethodPanel implements ActionListener {
87
        
88
        private static final Logger logger =
89
                        LoggerFactory.getLogger(DefaultLabeling.class);
90
        
91
        private static final long serialVersionUID = 7100208944546698724L;
92
        private ILabelClass defaultLabel;
93
        private IPlacementConstraints placementConstraints;
94
        private LayerPreview layerPrev;
95
        private JCheckBox enableLayerPrev;
96
        private LabelClassPreview labelPrev;
97
        private JButton btnProperties;
98
        
99
        private FeatureAttributeDescriptor[] fieldDescs;
100
        
101
        private boolean allowOverlap;
102
        private IZoomConstraints zoomConstraints;
103
        private LabelClassProperties lcProp;
104

    
105
        public Class<? extends ILabelingMethod> getLabelingMethodClass() {
106
                return DefaultLabelingMethod.class;
107
        }
108

    
109

    
110
        public String getName() {
111
                return Messages.getText("label_features_in_the_same_way") + ".";
112
        }
113

    
114
        @Override
115
        public void fillPanel(ILabelingMethod method, FeatureType fty) {
116
                try {
117
                        if (enableLayerPrev.isSelected()) {
118
                                layerPrev.setLayer(layer);
119
                        } else {
120
                                layerPrev.setLayer(null);
121
                        }
122

    
123
                        fieldDescs = fty.getAttributeDescriptors();
124
                        
125
                        /*
126
                         * Why was this here?
127
                         * 
128
                        ILabelingStrategy labeling = layer.getLabelingStrategy();
129
                        if (!(labeling instanceof GeneralLabelingStrategy)) {
130
                                labeling = new GeneralLabelingStrategy();
131
                                labeling.setLayer(layer);
132
                                layer.setLabelingStrategy(labeling);
133
                        }
134
                        */
135

    
136
                } catch (Exception e) {
137
                        NotificationManager.addWarning(e.getMessage(), e);
138
                }
139

    
140
                ILabelClass lc = null;
141
                if (method.getLabelClasses() != null && method.getLabelClasses().length > 0) {
142
                        lc = method.getLabelClasses()[0];
143
                } else {
144
                        lc = SymbologyLocator.getSymbologyManager().createDefaultLabel();
145
                }
146
                setLabel(lc);
147
                getLcProp();
148
        }
149

    
150
        private JButton getBtnProperties(){
151
                if (btnProperties == null){
152
                        btnProperties = new JButton(Messages.getText("properties"));
153
                        btnProperties.addActionListener(this);
154
                }
155
                return btnProperties;
156
        }
157

    
158
        private LabelClassPreview getLabelPrev(){
159
                if (labelPrev == null){
160
                        labelPrev = new LabelClassPreview();
161
                }
162
                return labelPrev;
163
        }
164

    
165
        private JCheckBox getEnableLayerPreview(){
166
                if(enableLayerPrev == null){
167
                        enableLayerPrev = new JCheckBox(
168
                                        Messages.getText("Enable_layer_preview"));
169
                        enableLayerPrev.addItemListener(new ItemListener() {
170

    
171
                                public void itemStateChanged(ItemEvent e) {
172
                                        try {
173
                                                if (e.getStateChange()==ItemEvent.SELECTED) {
174
                                                        layerPrev.setLayer(layer);
175
                                                }
176
                                                else if (e.getStateChange()==ItemEvent.DESELECTED) {
177
                                                        layerPrev.setLayer(null);
178
                                                }
179
                                        } catch (ReadException e1) {
180
                                                logger.error("While setting changing layer in preview", e1);
181
                                        }
182
                                }
183
                        });
184
                }
185
                return enableLayerPrev;
186
        }
187

    
188
        private LayerPreview getLayerPreview(){
189
                if (layerPrev == null){
190
                        layerPrev = new LayerPreview();
191
                }
192
                return layerPrev;
193
        }
194
        @Override
195
        protected void initializePanel() {
196
                setLayout(new BorderLayout());
197
                JSplitPane scrl = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
198
                scrl.add(getLayerPreview(), JSplitPane.LEFT);
199

    
200
                labelPrev = getLabelPrev(); //new LabelClassPreview();
201
                JPanel aux = new JPanel(new BorderLayout());
202
                aux.add(new JBlank(10, 10), BorderLayout.NORTH );
203
                aux.add(new JBlank(10, 10), BorderLayout.WEST );
204
                aux.add(labelPrev, BorderLayout.CENTER);
205
                aux.add(new JBlank(10, 10), BorderLayout.EAST );
206
                JPanel aux2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
207
                btnProperties = getBtnProperties();
208
                aux2.add(btnProperties);
209
//                btnProperties.addActionListener(this);
210
                aux.add(aux2, BorderLayout.SOUTH);
211
                scrl.add(aux, JSplitPane.RIGHT);
212
                add(scrl, BorderLayout.CENTER);
213
                scrl.setDividerLocation(500);
214

    
215
                getEnableLayerPreview().setSelected(false);
216
                add(enableLayerPrev, BorderLayout.SOUTH);
217
        }
218

    
219
        private LabelClassProperties getLcProp(){
220
                if(lcProp == null) {
221
                        
222
                        int n = this.fieldDescs.length;
223
                        String[] fnames = new String[n];
224
                        int[] ftypes = new int[n];
225
                        for (int i=0; i<n; i++) {
226
                                fnames[i] = fieldDescs[i].getName();
227
                                ftypes[i] = fieldDescs[i].getType();
228
                        }
229
                        lcProp = new LabelClassProperties(fnames, ftypes);
230
                        lcProp.setLabelClass(method.getLabelClasses()[0]);
231
                }
232
                return lcProp;
233

    
234
        }
235

    
236
        public void actionPerformed(ActionEvent e) {
237
                if (e.getSource().equals(getBtnProperties())) {
238
//                        LabelClassProperties lcProp = new LabelClassProperties(fieldNames, fieldTypes);
239
//                        lcProp.setLabelClass(method.getLabelClasses()[0]);
240
//                        PluginServices.getMDIManager().addWindow(lcProp);
241
//                        setLabel(lcProp.getLabelClass());
242
                        LabelClassProperties lcProp = getLcProp();
243
                        ILabelClass lc = defaultLabel;
244
                        lcProp.setLabelClass(lc);
245
                        boolean eval = false;
246
                        while (!eval){
247
                                
248
                                ApplicationLocator.getManager().getUIManager().addWindow(lcProp);
249
                                if(!lcProp.isAccepted()){ break; };
250
                                lc = lcProp.getLabelClass();
251
                                eval = checkValidSQL(lc);
252
                        }
253
                        setLabel(lc);
254
                }
255
        }
256

    
257
        private boolean checkValidSQL(ILabelClass lc){
258
                
259
                String sqlQuery = lc.getSQLQuery();
260
                if (sqlQuery != null && sqlQuery.trim().length() > 0) {
261
                        return LabelClassUtils.validExpression(
262
                                        sqlQuery, layer.getFeatureStore(), true);
263
                } else {
264
                        return true;
265
                }
266
        
267
        }
268

    
269
        private void setLabel(ILabelClass labelClass) {
270
                
271
                defaultLabel = labelClass;
272
                // defaultLabel = LabelingFactory.createLabelClassFromXML(labelClass.getXMLEntity());
273
                labelPrev.setLabelClass(defaultLabel);
274
                method = newMethodForThePreview(defaultLabel);
275

    
276
                updatePreview();
277

    
278
        }
279

    
280
        protected ILabelingMethod newMethodForThePreview(ILabelClass defaultLabel) {
281
                
282
                ILabelingMethod resp =
283
                                SymbologyLocator.getSymbologyManager().createDefaultLabelingMethod();
284
                resp.addLabelClass(defaultLabel);
285
                return resp;
286
        }
287

    
288
        private void updatePreview() {
289
                
290
                ILabelingStrategy stra = layer.getLabelingStrategy();
291
                if (method == null){
292
                        stra.setLabelingMethod(newMethodForThePreview(defaultLabel));
293
                } else {
294
                        stra.setLabelingMethod(method);
295
                }
296
//                s.setPlacementConstraints(placementConstraints);
297
//                s.setAllowOverlapping(allowOverlap);
298
//                s.setZoomConstraints(zoomConstraints);
299

    
300
                layer.setIsLabeled(true);
301
                
302
                DisposableIterator diter = null;
303
                FeatureSelection fsel = null;
304
                
305
                try {
306
                        diter = layer.getFeatureStore().getFeatureSet().fastIterator();
307
                        fsel = layer.getFeatureStore().getFeatureSelection();
308
                        fsel.deselectAll();
309
                } catch (DataException de) {
310
                        logger.error("While getting store selection", de);
311
                        return;
312
                }
313
                
314
                long count = 0;
315
                Feature feat = null;
316
                while (diter.hasNext() && count < 200) {
317
                        feat = (Feature) diter.next();
318
                        if (count % 4 == 0) {
319
                                fsel.select(feat);
320
                        }
321
                        count++;
322
                }
323
                diter.dispose();
324

    
325
                try {
326
                        Rectangle r = layerPrev.getBounds();
327
                        r.setLocation(layerPrev.getLocationOnScreen());
328
                        layerPrev.paintImmediately(r);
329
                        layerPrev.doLayout();
330
                } catch (IllegalComponentStateException ex) {
331
                        // this happens when the component is not showing in the
332
                        // screen. If that is the case, then we don't need to do
333
                        // anything.
334
                }
335

    
336
        }
337

    
338
        public void propertyChange(PropertyChangeEvent evt) {
339
                
340
                String prop = evt.getPropertyName();
341
                ILabelingStrategy strat = layer.getLabelingStrategy();
342

    
343
                if (AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS.equals(prop)) {
344
                        placementConstraints = (IPlacementConstraints) evt.getNewValue();
345
                        strat.setPlacementConstraints(placementConstraints);
346
                } else if ((strat instanceof GeneralLabelingStrategy) &&
347
                                AbstractLabelingMethodPanel.ALLOW_OVERLAP.equals(prop)) {
348
                        allowOverlap = (Boolean) evt.getNewValue();
349
                        ((GeneralLabelingStrategy) strat).setAllowOverlapping(allowOverlap);
350
                } else if (AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS.equals(prop)) {
351
                        zoomConstraints = (IZoomConstraints) evt.getNewValue();
352
                        strat.setZoomConstraints(zoomConstraints);
353
                }
354

    
355
                updatePreview();
356
        }
357

    
358
        public void setEnabled(boolean enabled) {
359
                super.setEnabled(enabled);
360
                if (layerPrev!=null) {
361
                        layerPrev.setEnabled(enabled);
362
                };
363
                if (labelPrev!=null) {
364
                        labelPrev.setEnabled(enabled);
365
                };
366
                if (btnProperties!=null) {
367
                        btnProperties.setEnabled(enabled);
368
                };
369
        }
370
        
371

    
372

    
373
    
374
}