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 / GeneralLabeling.java @ 43508

History | View | Annotate | Download (13.4 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
package org.gvsig.labeling.gui.layerproperties;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Component;
28
import java.awt.Container;
29
import java.awt.Dimension;
30
import java.awt.FlowLayout;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.beans.PropertyChangeEvent;
34
import java.util.ArrayList;
35
import java.util.Comparator;
36
import java.util.Date;
37
import java.util.Iterator;
38
import java.util.TreeMap;
39

    
40
import javax.swing.BorderFactory;
41
import javax.swing.JButton;
42
import javax.swing.JCheckBox;
43
import javax.swing.JComboBox;
44
import javax.swing.JComponent;
45
import javax.swing.JLabel;
46
import javax.swing.JPanel;
47

    
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.andami.messages.NotificationManager;
52
import org.gvsig.app.ApplicationLocator;
53
import org.gvsig.app.project.documents.view.legend.gui.ILabelingStrategyPanel;
54
import org.gvsig.fmap.dal.exception.ReadException;
55
import org.gvsig.fmap.mapcontext.layers.FLayer;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
57
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
58
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
59
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
60
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
61
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints;
62
import org.gvsig.i18n.Messages;
63
import org.gvsig.labeling.label.GeneralLabelingFactory;
64
import org.gvsig.labeling.label.GeneralLabelingStrategy;
65
import org.gvsig.labeling.lang.LabelClassUtils;
66
import org.gvsig.symbology.SymbologyLocator;
67

    
68
/**
69
 * @author gvSIG Team
70
 *
71
 */
72
public class GeneralLabeling extends JPanel implements
73
ILabelingStrategyPanel, ActionListener {
74

    
75
        private static final long serialVersionUID = 8864709758980903351L;
76
           private static final Logger logger = LoggerFactory
77
           .getLogger(GeneralLabeling.class);
78
        private static Comparator<Class<? extends ILabelingMethod>> comparator =
79
                        new Comparator<Class<? extends ILabelingMethod>>(){
80
                public int compare(Class<? extends ILabelingMethod> o1,
81
                                Class<? extends ILabelingMethod> o2) {
82
                        return o1.getName().compareTo(o2.getName());
83
                }};
84

    
85
        private static TreeMap<
86
                        Class<? extends ILabelingMethod>,
87
                        Class<? extends AbstractLabelingMethodPanel>
88
                > methods
89
                = new TreeMap<
90
                        Class<? extends ILabelingMethod>,
91
                        Class<? extends AbstractLabelingMethodPanel>
92
                >(comparator);
93

    
94
        private JButton btnVisualization;
95
        private JButton btnPlacement;
96
        private JComboBox<?> cmbMethod;
97
        private JPanel methodPanel;
98
        private IPlacementConstraints placementConstraints;
99
        private IZoomConstraints zoomConstraints;
100
        private boolean noEvent;
101
        private FLyrVect targetLayer;
102
        private JCheckBox chkAllowLabelOverlapping;
103
        private FLyrVect auxLayer;
104
        private GeneralLabelingStrategy gStr;
105
        /**
106
         *
107
         */
108
        public GeneralLabeling() {
109
                initialize();
110
        }
111

    
112
        private void initialize() {
113

    
114
                setLayout(new BorderLayout());
115
                JPanel center = new JPanel(new BorderLayout(10, 10));
116
                center.setBorder(BorderFactory.createTitledBorder(
117
                                null, Messages.getText("classes")));
118
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
119
                aux.add(new JLabel(Messages.getText("method")+":"));
120
                aux.add(getCmbMethod());
121
                aux.setPreferredSize(new Dimension(605, 40));
122
                center.add(aux, BorderLayout.NORTH);
123

    
124

    
125
                // el panell del m?tode de moltes FeatureDependantLabelingMethod
126
                methodPanel = getMethodPanel();
127
                center.add(methodPanel, BorderLayout.CENTER);
128
                add(center, BorderLayout.CENTER);
129

    
130
                JPanel south = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
131
                south.setBorder(BorderFactory.createTitledBorder(
132
                                null, Messages.getText("options")));
133
                south.add(getBtnVisualization());
134
                south.add(getBtnPlacement());
135
                south.add(getChkAllowLabelOverlapping());
136
                south.setPreferredSize(new Dimension(612, 60));
137

    
138
                add(south, BorderLayout.SOUTH);
139
        }
140

    
141

    
142
        private JPanel getMethodPanel() {
143
                if (methodPanel == null){
144
                        methodPanel = new JPanel(new BorderLayout(10, 0));
145
                }
146
                return methodPanel;
147
        }
148

    
149
        private JCheckBox getChkAllowLabelOverlapping() {
150
                if (chkAllowLabelOverlapping == null) {
151
                        chkAllowLabelOverlapping = new JCheckBox(
152
                                        Messages.getText("allow_label_overlapping"));
153
                        chkAllowLabelOverlapping.addActionListener(this);
154
                }
155
                return chkAllowLabelOverlapping;
156
        }
157

    
158
        private void refreshControls() {
159
                // fires an event from the methods combo box
160
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
161
        }
162

    
163
        private JButton getBtnVisualization() {
164
                if (btnVisualization == null) {
165
                        btnVisualization = new JButton(
166
                                        Messages.getText("visualization")+"...");
167
                        btnVisualization.setName("BTNVISUALIZATION");
168
                        btnVisualization.addActionListener(this);
169
                }
170
                return btnVisualization;
171
        }
172

    
173
        private JButton getBtnPlacement() {
174
                if (btnPlacement == null) {
175
                        btnPlacement = new JButton(
176
                                        Messages.getText("placement")+"...");
177
                        btnPlacement.setName("BTNPLACEMENT");
178
                        btnPlacement.addActionListener(this);
179
                }
180
                return btnPlacement;
181
        }
182

    
183
        @SuppressWarnings({ "rawtypes", "unchecked" })
184
    private JComboBox getCmbMethod() {
185
                if (cmbMethod == null) {
186
                        Iterator<Class<? extends AbstractLabelingMethodPanel>> it = methods.values().iterator();
187
                        ArrayList<AbstractLabelingMethodPanel> panels = new ArrayList<AbstractLabelingMethodPanel>();
188
                        while (it.hasNext()) {
189
                                try {
190
                                        panels.add(it.next().newInstance());
191
                                } catch (Exception e) {
192
                                        throw new Error(e);
193
                                }
194
                        }
195
                        cmbMethod = new JComboBox(panels.toArray());
196
                        cmbMethod.setSize(new Dimension(300, 22));
197
                        cmbMethod.setName("CMBMETHOD");
198
                        cmbMethod.addActionListener(this);
199
                }
200
                return cmbMethod;
201
        }
202

    
203
        public ILabelingStrategy getLabelingStrategy() {
204
                GeneralLabelingStrategy resp = GeneralLabelingFactory.
205
                                                createStrategy((FLayer) targetLayer,
206
                                                                getMethod(),
207
                                                                getPlacementConstraints(),
208
                                                                getZoomConstraints());
209

    
210
                resp.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
211
                return resp;
212
        }
213

    
214

    
215
        public void setModel(FLayer layer, ILabelingStrategy str) {
216
                if (layer instanceof FLyrVect) {
217
                        try {
218
                                targetLayer = (FLyrVect) layer;//.cloneLayer();
219

    
220
                                auxLayer = (FLyrVect) targetLayer.cloneLayer();
221
                                auxLayer.setParentLayer(layer.getParentLayer());
222
                                auxLayer.setLegend((IVectorLegend)targetLayer.getLegend());
223
                                auxLayer.setProjection(targetLayer.getProjection());
224
                if( str == null ) {
225
                    str = new GeneralLabelingStrategy();
226
                    str.setLayer(layer);
227
                }
228
                                if (str instanceof GeneralLabelingStrategy) {
229
                                        GeneralLabelingStrategy gls = (GeneralLabelingStrategy) str;
230
                                        gStr = (GeneralLabelingStrategy) gls.clone();
231
                                        auxLayer.setLabelingStrategy(gStr);
232
                                        gStr.setLayer(auxLayer);
233
                                        setMethod(gStr.getLabelingMethod(), auxLayer);
234
                                        placementConstraints = gStr.getPlacementConstraints();
235
                                        zoomConstraints = gStr.getZoomConstraints();
236
                                        getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
237
                                } else {
238
                                        ILabelingStrategy ils = ((FLyrVect) layer).getLabelingStrategy();
239
                                        if (ils != null) {
240
                                                ils = (ILabelingStrategy) LabelClassUtils.clone(ils);
241
                                                auxLayer.setLabelingStrategy(ils);
242
                    }
243
                                }
244
                        } catch (Exception e) {
245
                                NotificationManager.addError(
246
                                                Messages.getText("While setting model."), e);
247
                        }
248
                        refreshControls();
249
                }
250
        }
251

    
252

    
253
        /**
254
         * @param iLabelingMethodClass
255
         */
256
        public static void addLabelingMethod(Class<? extends AbstractLabelingMethodPanel> iLabelingMethodClass) {
257
                try {
258
                        methods.put(
259
                                        iLabelingMethodClass.newInstance().getLabelingMethodClass(),
260
                                        iLabelingMethodClass);
261
                } catch (Exception e) {
262
                        NotificationManager.addError(
263
                                        Messages.getText("cannot_install_labeling_method"), e);
264
                }
265
        }
266

    
267
        private void setMethod(ILabelingMethod labelingMethod, FLyrVect srcLayer) {
268
                getMethodPanel().removeAll();
269
                AbstractLabelingMethodPanel p;
270
                try {
271
                        p = methods.get(labelingMethod.getClass()).newInstance();
272
                        p.setModel(labelingMethod, srcLayer);
273
                        cmbMethod.setSelectedItem(p);
274
                } catch (Exception e) {
275
                        // should be impossible;
276
                        NotificationManager.addWarning(e.getLocalizedMessage());
277
                }
278

    
279
        }
280

    
281
        private ILabelingMethod getMethod() {
282
                AbstractLabelingMethodPanel p =
283
                                ((AbstractLabelingMethodPanel)cmbMethod.getSelectedItem());
284
                if (p != null){
285
                        return p.getMethod();
286
                }
287

    
288
                return SymbologyLocator.getSymbologyManager().createDefaultLabelingMethod();
289
        }
290

    
291
        private IZoomConstraints getZoomConstraints() {
292
                if (zoomConstraints == null) {
293
                        zoomConstraints =
294
                                        SymbologyLocator.getSymbologyManager().createDefaultZoomConstraints();
295

    
296
                }
297
                return zoomConstraints;
298
        }
299

    
300
        private IPlacementConstraints getPlacementConstraints() {
301
                return placementConstraints;
302
        }
303

    
304
        public void actionPerformed(ActionEvent e) {
305

    
306
                if (noEvent) return;
307
                JComponent c = (JComponent)e.getSource();
308

    
309
                if (c.equals(btnPlacement)) {
310

    
311
                        try {
312
                                IPlacementConstraints oldValue = getPlacementConstraints();
313
                                IPlacementProperties pp = PlacementProperties.createPlacementProperties(
314
                                                getPlacementConstraints(),
315
                                                ((FLyrVect) auxLayer).getShapeType());
316

    
317
                                ApplicationLocator.getManager().getUIManager().addWindow(pp);
318

    
319
                                placementConstraints = pp.getPlacementConstraints();
320

    
321
                                ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
322
                                        propertyChange(new PropertyChangeEvent(
323
                                                        this,
324
                                                        AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS,
325
                                                        oldValue,
326
                                                        placementConstraints));
327

    
328
                        } catch (ClassCastException ccEx) {
329
                                NotificationManager.addError(
330
                                                "Placement constraints not prepared for:"
331
                                                +auxLayer.getClass().getName(),
332
                                                ccEx);
333
                        } catch (ReadException dEx) {
334
                                NotificationManager.addError(
335
                                                "While getting layer type", dEx);
336
                        }
337

    
338
                } else if (c.equals(btnVisualization)) {
339
                        IZoomConstraints oldValue = getZoomConstraints();
340
                        LabelScaleRange lsr = new LabelScaleRange(oldValue.getMinScale(), oldValue.getMaxScale());
341
                        ApplicationLocator.getManager().getUIManager().addWindow(lsr);
342
                        zoomConstraints =
343
                                        SymbologyLocator.getSymbologyManager().createDefaultZoomConstraints();
344
                        zoomConstraints.setMaxScale(lsr.getMaxScale());
345
                        zoomConstraints.setMinScale(lsr.getMinScale());
346
                        zoomConstraints.setMode(
347
                                        lsr.getMaxScale() ==-1 && lsr.getMinScale() == -1 ?
348
                                                        IZoomConstraints.DEFINED_BY_THE_LAYER :
349
                                                        IZoomConstraints.DEFINED_BY_THE_USER);
350

    
351
                        ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
352
                        propertyChange(new PropertyChangeEvent(
353
                                        this,
354
                                        AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS,
355
                                        oldValue,
356
                                        getZoomConstraints()));
357

    
358
                } else if (c.equals(chkAllowLabelOverlapping)) {
359
                        boolean newValue = chkAllowLabelOverlapping.isSelected();
360
                        ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
361
                        propertyChange(new PropertyChangeEvent(
362
                                        this,
363
                                        AbstractLabelingMethodPanel.ALLOW_OVERLAP,
364
                                        !newValue,
365
                                        newValue));
366

    
367
        } else if (c.equals(getCmbMethod())) {
368
            AbstractLabelingMethodPanel p = (AbstractLabelingMethodPanel) cmbMethod.getSelectedItem();
369
            Container cont = methodPanel.getParent();
370
            cont.remove(methodPanel);
371
            emptyContainer(methodPanel);
372

    
373
            try {
374
                if (gStr != null) {
375
                    if (gStr.getLabelingMethod() != null) {
376
                        p.setModel(gStr.getLabelingMethod(), auxLayer);
377
                    }
378
                } else {
379
                    p.setModel(getMethod(), auxLayer);
380
                }
381
                methodPanel.add(p, BorderLayout.CENTER);
382

    
383
                methodPanel.repaint();
384
                setVisible(false);
385
                setVisible(true);
386
            } catch (Exception e1) {
387
                NotificationManager.addError(new Date(System.currentTimeMillis()).toString(), e1);
388
            }
389
            cont.add(methodPanel, BorderLayout.CENTER);
390
        }
391

    
392
        }
393

    
394
    private void emptyContainer(Container c) {
395
        for (int i = 0; i < c.getComponentCount(); i++) {
396
            c.remove(i);
397
        }
398
    }
399

    
400
        public String getLabelingStrategyName() {
401
                return Messages.getText("user_defined_labels");
402
        }
403

    
404
        public Class<? extends ILabelingStrategy> getLabelingStrategyClass() {
405
                return GeneralLabelingStrategy.class;
406
        }
407

    
408
        public void setEnabled(boolean enabled) {
409
                super.setEnabled(enabled);
410
                getBtnPlacement().setEnabled(enabled);
411
                getBtnVisualization().setEnabled(enabled);
412
                getChkAllowLabelOverlapping().setEnabled(enabled);
413
                getCmbMethod().setEnabled(enabled);
414
                JPanel mp = getMethodPanel();//.setEnabled(enabled);
415
                mp.setEnabled(enabled);
416
                for (int i=0; i<mp.getComponentCount(); i++){
417
                        Component c = mp.getComponent(i);
418
                        c.setEnabled(enabled);
419
                }
420

    
421
        }
422
}