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 @ 40911

History | View | Annotate | Download (13.1 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.gvsig.andami.messages.NotificationManager;
49
import org.gvsig.app.ApplicationLocator;
50
import org.gvsig.app.project.documents.view.legend.gui.ILabelingStrategyPanel;
51
import org.gvsig.fmap.dal.exception.ReadException;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.mapcontext.MapContextLocator;
54
import org.gvsig.fmap.mapcontext.layers.FLayer;
55
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
56
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
57
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
58
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
59
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
60
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints;
61
import org.gvsig.i18n.Messages;
62
import org.gvsig.labeling.label.GeneralLabelingFactory;
63
import org.gvsig.labeling.label.GeneralLabelingStrategy;
64
import org.gvsig.labeling.lang.LabelClassUtils;
65
import org.gvsig.symbology.SymbologyLocator;
66

    
67
public class GeneralLabeling extends JPanel implements
68
ILabelingStrategyPanel, ActionListener {
69
        
70
        private static final long serialVersionUID = 8864709758980903351L;
71
        private static Comparator comparator =
72
                        new Comparator<Class<? extends ILabelingMethod>>(){
73
                public int compare(Class<? extends ILabelingMethod> o1,
74
                                Class<? extends ILabelingMethod> o2) {
75
                        return o1.getName().compareTo(o2.getName());
76
                }};
77
                
78
        private static TreeMap<
79
                        Class<? extends ILabelingMethod>,
80
                        Class<? extends AbstractLabelingMethodPanel>
81
                > methods
82
                = new TreeMap<
83
                        Class<? extends ILabelingMethod>,
84
                        Class<? extends AbstractLabelingMethodPanel>
85
                >(comparator);
86
        
87
        private JButton btnVisualization;
88
        private JButton btnPlacement;
89
        private JComboBox cmbMethod;
90
        private JPanel methodPanel;
91
        private IPlacementConstraints placementConstraints;
92
        private IZoomConstraints zoomConstraints;
93
        private boolean noEvent;
94
        private FLyrVect targetLayer;
95
        private JCheckBox chkAllowLabelOverlapping;
96
        private FLyrVect auxLayer;
97
        private GeneralLabelingStrategy gStr;
98
        private AbstractLabelingMethodPanel previousMethodPanel = null;
99
        public GeneralLabeling() {
100
                initialize();
101
        }
102

    
103
        private void initialize() {
104
                
105
                setLayout(new BorderLayout());
106
                JPanel center = new JPanel(new BorderLayout(10, 10));
107
                center.setBorder(BorderFactory.createTitledBorder(
108
                                null, Messages.getText("classes")));
109
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
110
                aux.add(new JLabel(Messages.getText("method")+":"));
111
                aux.add(getCmbMethod());
112
                aux.setPreferredSize(new Dimension(605, 40));
113
                center.add(aux, BorderLayout.NORTH);
114

    
115

    
116
                // el panell del m?tode de moltes FeatureDependantLabelingMethod
117
                methodPanel = getMethodPanel();
118
                center.add(methodPanel, BorderLayout.CENTER);
119
                add(center, BorderLayout.CENTER);
120

    
121
                JPanel south = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
122
                south.setBorder(BorderFactory.createTitledBorder(
123
                                null, Messages.getText("options")));
124
                south.add(getBtnVisualization());
125
                south.add(getBtnPlacement());
126
                south.add(getChkAllowLabelOverlapping());
127
                south.setPreferredSize(new Dimension(612, 60));
128

    
129
                add(south, BorderLayout.SOUTH);
130
        }
131

    
132

    
133
        private JPanel getMethodPanel() {
134
                if (methodPanel == null){
135
                        methodPanel = new JPanel(new BorderLayout(10, 0));
136
                }
137
                return methodPanel;
138
        }
139

    
140
        private JCheckBox getChkAllowLabelOverlapping() {
141
                if (chkAllowLabelOverlapping == null) {
142
                        chkAllowLabelOverlapping = new JCheckBox(
143
                                        Messages.getText("allow_label_overlapping"));
144
                        chkAllowLabelOverlapping.addActionListener(this);
145
                }
146
                return chkAllowLabelOverlapping;
147
        }
148

    
149
        private void refreshControls() {
150
                // fires an event from the methods combo box
151
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
152
        }
153

    
154
        private JButton getBtnVisualization() {
155
                if (btnVisualization == null) {
156
                        btnVisualization = new JButton(
157
                                        Messages.getText("visualization")+"...");
158
                        btnVisualization.setName("BTNVISUALIZATION");
159
                        btnVisualization.addActionListener(this);
160
                }
161
                return btnVisualization;
162
        }
163

    
164
        private JButton getBtnPlacement() {
165
                if (btnPlacement == null) {
166
                        btnPlacement = new JButton(
167
                                        Messages.getText("placement")+"...");
168
                        btnPlacement.setName("BTNPLACEMENT");
169
                        btnPlacement.addActionListener(this);
170
                }
171
                return btnPlacement;
172
        }
173

    
174
        private JComboBox getCmbMethod() {
175
                if (cmbMethod == null) {
176
                        Iterator<Class<? extends AbstractLabelingMethodPanel>> it = methods.values().iterator();
177
                        ArrayList<AbstractLabelingMethodPanel> panels = new ArrayList<AbstractLabelingMethodPanel>();
178
                        while (it.hasNext()) {
179
                                try {
180
                                        panels.add(it.next().newInstance());
181
                                } catch (Exception e) {
182
                                        throw new Error(e);
183
                                }
184
                        }
185
                        cmbMethod = new JComboBox(panels.toArray());
186
                        cmbMethod.setSize(new Dimension(300, 22));
187
                        cmbMethod.setName("CMBMETHOD");
188
                        cmbMethod.addActionListener(this);
189
                }
190
                return cmbMethod;
191
        }
192

    
193
        public ILabelingStrategy getLabelingStrategy() {
194
                GeneralLabelingStrategy resp = GeneralLabelingFactory.
195
                                                createStrategy((FLayer) targetLayer,
196
                                                                getMethod(),
197
                                                                getPlacementConstraints(),
198
                                                                getZoomConstraints());
199

    
200
                resp.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
201
                return resp;
202
        }
203

    
204
        
205
        public void setModel(FLayer layer, ILabelingStrategy str) {
206
                if (layer instanceof FLyrVect) {
207
                        try {
208
                                targetLayer = (FLyrVect) layer;//.cloneLayer();
209
                                
210
                                auxLayer = (FLyrVect) targetLayer.cloneLayer();
211
                                auxLayer.setParentLayer(layer.getParentLayer());
212
                                auxLayer.setLegend((IVectorLegend)targetLayer.getLegend());
213
                                auxLayer.setProjection(targetLayer.getProjection());
214
                                
215
                                if (str instanceof GeneralLabelingStrategy) {
216
                                        GeneralLabelingStrategy gls = (GeneralLabelingStrategy) str; 
217
                                        gStr = (GeneralLabelingStrategy) gls.clone();
218
                                        auxLayer.setLabelingStrategy(gStr);
219
                                        gStr.setLayer(auxLayer);
220
                                        setMethod(gStr.getLabelingMethod(), auxLayer);
221
                                        placementConstraints = gStr.getPlacementConstraints();
222
                                        zoomConstraints = gStr.getZoomConstraints();
223
                                        getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
224
                                } else {
225
                                        ILabelingStrategy ils = ((FLyrVect) layer).getLabelingStrategy();
226
                                        if (ils != null) {
227
                                                ils = (ILabelingStrategy) LabelClassUtils.clone(ils);
228
                                                auxLayer.setLabelingStrategy(ils);
229
                                        }
230
                                }
231
                        } catch (Exception e) {
232
                                NotificationManager.addError(
233
                                                Messages.getText("While setting model."), e);
234
                        }
235
                        refreshControls();
236
                }
237
        }
238

    
239

    
240
        public static void addLabelingMethod(Class<? extends AbstractLabelingMethodPanel> iLabelingMethodClass) {
241
                try {
242
                        methods.put(
243
                                        iLabelingMethodClass.newInstance().getLabelingMethodClass(),
244
                                        iLabelingMethodClass);
245
                } catch (Exception e) {
246
                        NotificationManager.addError(
247
                                        Messages.getText("cannot_install_labeling_method"), e);
248
                }
249
        }
250

    
251
        private void setMethod(ILabelingMethod labelingMethod, FLyrVect srcLayer) {
252
                getMethodPanel().removeAll();
253
                AbstractLabelingMethodPanel p;
254
                try {
255
                        p = methods.get(labelingMethod.getClass()).newInstance();
256
                        p.setModel(labelingMethod, srcLayer);
257
                        cmbMethod.setSelectedItem(p);
258
                } catch (Exception e) {
259
                        // should be impossible;
260
                        NotificationManager.addWarning(e.getLocalizedMessage());
261
                }
262

    
263
        }
264

    
265
        private ILabelingMethod getMethod() {
266
                AbstractLabelingMethodPanel p =
267
                                ((AbstractLabelingMethodPanel)cmbMethod.getSelectedItem());
268
                if (p != null){
269
                        return p.getMethod();
270
                }
271

    
272
                return SymbologyLocator.getSymbologyManager().createDefaultLabelingMethod();
273
        }
274

    
275
        private IZoomConstraints getZoomConstraints() {
276
                if (zoomConstraints == null) {
277
                        zoomConstraints =
278
                                        SymbologyLocator.getSymbologyManager().createDefaultZoomConstraints();
279
                        
280
                }
281
                return zoomConstraints;
282
        }
283

    
284
        private IPlacementConstraints getPlacementConstraints() {
285
                return placementConstraints;
286
        }
287

    
288
        public void actionPerformed(ActionEvent e) {
289

    
290
                if (noEvent) return;
291
                JComponent c = (JComponent)e.getSource();
292

    
293
                if (c.equals(btnPlacement)) {
294

    
295
                        try {
296
                                IPlacementConstraints oldValue = getPlacementConstraints();
297
                                IPlacementProperties pp = PlacementProperties.createPlacementProperties(
298
                                                getPlacementConstraints(),
299
                                                ((FLyrVect) auxLayer).getShapeType());
300
                                
301
                                ApplicationLocator.getManager().getUIManager().addWindow(pp);
302
                                
303
                                placementConstraints = pp.getPlacementConstraints();
304

    
305
                                ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
306
                                        propertyChange(new PropertyChangeEvent(
307
                                                        this,
308
                                                        AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS,
309
                                                        oldValue,
310
                                                        placementConstraints));
311

    
312
                        } catch (ClassCastException ccEx) {
313
                                NotificationManager.addError(
314
                                                "Placement constraints not prepared for:"
315
                                                +auxLayer.getClass().getName(),
316
                                                ccEx);
317
                        } catch (ReadException dEx) {
318
                                NotificationManager.addError(
319
                                                "While getting layer type", dEx);
320
                        }
321

    
322
                } else if (c.equals(btnVisualization)) {
323
                        IZoomConstraints oldValue = getZoomConstraints();
324
                        LabelScaleRange lsr = new LabelScaleRange(oldValue.getMinScale(), oldValue.getMaxScale());
325
                        ApplicationLocator.getManager().getUIManager().addWindow(lsr);
326
                        zoomConstraints =
327
                                        SymbologyLocator.getSymbologyManager().createDefaultZoomConstraints();
328
                        zoomConstraints.setMaxScale(lsr.getMaxScale());
329
                        zoomConstraints.setMinScale(lsr.getMinScale());
330
                        zoomConstraints.setMode(
331
                                        lsr.getMaxScale() ==-1 && lsr.getMinScale() == -1 ?
332
                                                        IZoomConstraints.DEFINED_BY_THE_LAYER :
333
                                                        IZoomConstraints.DEFINED_BY_THE_USER);
334

    
335
                        ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
336
                        propertyChange(new PropertyChangeEvent(
337
                                        this,
338
                                        AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS,
339
                                        oldValue,
340
                                        getZoomConstraints()));
341

    
342
                } else if (c.equals(chkAllowLabelOverlapping)) {
343
                        boolean newValue = chkAllowLabelOverlapping.isSelected();
344
                        ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
345
                        propertyChange(new PropertyChangeEvent(
346
                                        this,
347
                                        AbstractLabelingMethodPanel.ALLOW_OVERLAP,
348
                                        !newValue,
349
                                        newValue));
350

    
351
                } else if (c.equals(cmbMethod)) {
352
                        AbstractLabelingMethodPanel p = (AbstractLabelingMethodPanel) cmbMethod.getSelectedItem();
353
                        if(previousMethodPanel == null || previousMethodPanel != p ){
354
                                Container cont = methodPanel.getParent();
355
                                cont.remove(methodPanel);
356
                                emptyContainer(methodPanel);
357

    
358
                                try {
359
                                        if(gStr != null){
360
                                                if(gStr.getLabelingMethod() != null){
361
                                                        p.setModel(gStr.getLabelingMethod(), auxLayer);
362
                                                }
363
                                        } else {
364
                                                p.setModel(getMethod(),auxLayer);
365
                                        }
366
                                        methodPanel.add(
367
                                                        p,
368
                                                        BorderLayout.CENTER);
369

    
370
                                        methodPanel.repaint();
371
                                        setVisible(false);
372
                                        setVisible(true);
373
                                } catch (Exception e1) {
374
                                        NotificationManager.addError(new Date(System.currentTimeMillis()).toString(), e1);
375
                                }
376
                                cont.add(methodPanel, BorderLayout.CENTER);
377
                        }
378
                        previousMethodPanel = p;
379
                }
380
                
381
        }
382

    
383
        private void emptyContainer(Container c) {
384
                for (int i = 0; i < c.getComponentCount(); i++) {
385
                        if (c.getComponent(i) instanceof Container) {
386
                                emptyContainer((Container) c.getComponent(i));
387
                        }
388
                        c.remove(i);
389
                }
390
        }
391

    
392
        public String getLabelingStrategyName() {
393
                return Messages.getText("user_defined_labels");
394
        }
395

    
396
        public Class<? extends ILabelingStrategy> getLabelingStrategyClass() {
397
                return GeneralLabelingStrategy.class;
398
        }
399

    
400
        public void setEnabled(boolean enabled) {
401
                super.setEnabled(enabled);
402
                getBtnPlacement().setEnabled(enabled);
403
                getBtnVisualization().setEnabled(enabled);
404
                getChkAllowLabelOverlapping().setEnabled(enabled);
405
                getCmbMethod().setEnabled(enabled);
406
                JPanel mp = getMethodPanel();//.setEnabled(enabled);
407
                mp.setEnabled(enabled);
408
                for (int i=0; i<mp.getComponentCount(); i++){
409
                        Component c = mp.getComponent(i);
410
                        c.setEnabled(enabled);
411
                }
412

    
413
        }
414
}