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

History | View | Annotate | Download (29.2 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.Component;
45
import java.awt.Container;
46
import java.awt.Dimension;
47
import java.awt.FlowLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.beans.PropertyChangeEvent;
51
import java.util.ArrayList;
52
import java.util.Comparator;
53
import java.util.Date;
54
import java.util.Hashtable;
55
import java.util.Iterator;
56
import java.util.TreeMap;
57

    
58
import javax.swing.BorderFactory;
59
import javax.swing.JButton;
60
import javax.swing.JCheckBox;
61
import javax.swing.JComboBox;
62
import javax.swing.JComponent;
63
import javax.swing.JLabel;
64
import javax.swing.JPanel;
65

    
66
import org.apache.log4j.Logger;
67
import org.gvsig.app.project.documents.view.legend.gui.ILabelingStrategyPanel;
68
import org.gvsig.fmap.mapcontext.MapContextLocator;
69
import org.gvsig.fmap.mapcontext.layers.FLayer;
70
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
71
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
72
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
73
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
74
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
75
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
76
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IZoomConstraints;
77
import org.gvsig.i18n.Messages;
78
import org.gvsig.labeling.label.ExtendedLabelingFactory;
79
import org.gvsig.labeling.label.GeneralLabelingStrategy;
80

    
81
public class GeneralLabeling extends JPanel implements
82
ILabelingStrategyPanel, ActionListener {
83
        
84
        private static final long serialVersionUID = 8864709758980903351L;
85
        private static Comparator comparator =
86
                        new Comparator<Class<? extends ILabelingMethod>>(){
87
                public int compare(Class<? extends ILabelingMethod> o1,
88
                                Class<? extends ILabelingMethod> o2) {
89
                        return o1.getName().compareTo(o2.getName());
90
                }};
91
                
92
        private static TreeMap<
93
                        Class<? extends ILabelingMethod>,
94
                        Class<? extends AbstractLabelingMethodPanel>
95
                > methods
96
                = new TreeMap<
97
                        Class<? extends ILabelingMethod>,
98
                        Class<? extends AbstractLabelingMethodPanel>
99
                >(comparator);
100
        
101
        private JButton btnVisualization;
102
        private JButton btnPlacement;
103
        private JComboBox cmbMethod;
104
        private JPanel methodPanel;
105
        private IPlacementConstraints placementConstraints;
106
        private IZoomConstraints zoomConstraints;
107
        private boolean noEvent;
108
        private FLyrVect targetLayer;
109
        private JCheckBox chkAllowLabelOverlapping;
110
        private FLyrVect auxLayer;
111
        private GeneralLabelingStrategy gStr;
112
        private AbstractLabelingMethodPanel previousMethodPanel = null;
113
        public GeneralLabeling() {
114
                initialize();
115
        }
116

    
117
        private void initialize() {
118
                
119
                setLayout(new BorderLayout());
120
                JPanel center = new JPanel(new BorderLayout(10, 10));
121
                center.setBorder(BorderFactory.createTitledBorder(
122
                                null, Messages.getText("classes")));
123
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
124
                aux.add(new JLabel(Messages.getText("method")+":"));
125
                aux.add(getCmbMethod());
126
                aux.setPreferredSize(new Dimension(605, 40));
127
                center.add(aux, BorderLayout.NORTH);
128

    
129

    
130
                // el panell del m?tode de moltes FeatureDependantLabelingMethod
131
                methodPanel = getMethodPanel();
132
                center.add(methodPanel, BorderLayout.CENTER);
133
                add(center, BorderLayout.CENTER);
134

    
135
                JPanel south = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
136
                south.setBorder(BorderFactory.createTitledBorder(
137
                                null, Messages.getText("options")));
138
                south.add(getBtnVisualization());
139
                south.add(getBtnPlacement());
140
                south.add(getChkAllowLabelOverlapping());
141
                south.setPreferredSize(new Dimension(612, 60));
142

    
143
                add(south, BorderLayout.SOUTH);
144
        }
145

    
146

    
147
        private JPanel getMethodPanel() {
148
                if (methodPanel == null){
149
                        methodPanel = new JPanel(new BorderLayout(10, 0));
150
                }
151
                return methodPanel;
152
        }
153

    
154
        private JCheckBox getChkAllowLabelOverlapping() {
155
                if (chkAllowLabelOverlapping == null) {
156
                        chkAllowLabelOverlapping = new JCheckBox(
157
                                        Messages.getText("allow_label_overlapping"));
158
                        chkAllowLabelOverlapping.addActionListener(this);
159
                }
160
                return chkAllowLabelOverlapping;
161
        }
162

    
163
        private void refreshControls() {
164
                // fires an event from the methods combo box
165
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
166
        }
167

    
168
        private JButton getBtnVisualization() {
169
                if (btnVisualization == null) {
170
                        btnVisualization = new JButton(
171
                                        Messages.getText("visualization")+"...");
172
                        btnVisualization.setName("BTNVISUALIZATION");
173
                        btnVisualization.addActionListener(this);
174
                }
175
                return btnVisualization;
176
        }
177

    
178
        private JButton getBtnPlacement() {
179
                if (btnPlacement == null) {
180
                        btnPlacement = new JButton(
181
                                        Messages.getText("placement")+"...");
182
                        btnPlacement.setName("BTNPLACEMENT");
183
                        btnPlacement.addActionListener(this);
184
                }
185
                return btnPlacement;
186
        }
187

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

    
207
        public ILabelingStrategy getLabelingStrategy() {
208
                ILabelingStrategy st = ExtendedLabelingFactory.
209
                                                createStrategy((FLayer) targetLayer,
210
                                                                getMethod(),
211
                                                                getPlacementConstraints(),
212
                                                                getZoomConstraints());
213
                if (st instanceof GeneralLabelingStrategy) {
214
                        GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) st;
215
                        gStr.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
216
                        gStr.setZoomConstraints(getZoomConstraints());
217
                }
218
                return st;
219
        }
220

    
221
        public void setModel(FLayer layer, ILabelingStrategy str) {
222
                if (layer instanceof FLyrVect) {
223
                        try {
224
                                targetLayer = (FLyrVect) layer;//.cloneLayer();
225
                                
226
                                auxLayer = (FLyrVect) MapContextLocator.getMapContextManager().createLayer(
227
                                                layer.getName(), ((FLyrVect) layer).getFeatureStore());
228
                                auxLayer.setParentLayer(layer.getParentLayer());
229
                                auxLayer.setLegend((IVectorLegend)targetLayer.getLegend());
230
                                auxLayer.setProjection(targetLayer.getProjection());
231

    
232
                                if (str instanceof GeneralLabelingStrategy) {
233
                                        gStr = (GeneralLabelingStrategy) LabelingFactory.
234
                                                createStrategyFromXML(str.getXMLEntity(), auxLayer/*layer.cloneLayer()*/);
235
                                        auxLayer.setLabelingStrategy(gStr);
236
                                        gStr.setLayer(auxLayer);
237
                                        setMethod(gStr.getLabelingMethod(), auxLayer);
238
                                        placementConstraints = gStr.getPlacementConstraints();
239
                                        zoomConstraints = gStr.getZoomConstraints();
240
                                        getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
241
                                }
242
                        } catch (ReadDriverException e) {
243
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
244
                        } catch (Exception e) {
245
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
246
                        }
247
                        refreshControls();
248
                }
249
        }
250

    
251

    
252
        public static void addLabelingMethod(Class<? extends AbstractLabelingMethodPanel> iLabelingMethodClass) {
253
                try {
254
                        methods.put(
255
                                        iLabelingMethodClass.newInstance().getLabelingMethodClass(),
256
                                        iLabelingMethodClass);
257
                } catch (Exception e) {
258
                        NotificationManager.addError(
259
                                        PluginServices.getText(GeneralLabeling.class, "cannot_install_labeling_method"), e);
260
                }
261
        }
262

    
263
        private void setMethod(ILabelingMethod labelingMethod, FLyrVect srcLayer) {
264
                getMethodPanel().removeAll();
265
                AbstractLabelingMethodPanel p;
266
                try {
267
                        p = methods.get(labelingMethod.getClass()).newInstance();
268
                        System.out.println("last labeling method was"+p.getClass().getName());
269
                        p.setModel(labelingMethod, srcLayer);
270
                        cmbMethod.setSelectedItem(p);
271
                } catch (Exception e) {
272
                        // should be impossible;
273
                        NotificationManager.addWarning(e.getLocalizedMessage());
274
                }
275

    
276
        }
277

    
278
        private ILabelingMethod getMethod() {
279
                AbstractLabelingMethodPanel p = ((AbstractLabelingMethodPanel)cmbMethod.getSelectedItem());
280
                if(p != null){
281
                        return p.getMethod();
282
                }
283

    
284
                return new DefaultLabelingMethod();
285
        }
286

    
287
        private IZoomConstraints getZoomConstraints() {
288
                if (zoomConstraints == null) {
289
                        zoomConstraints = new ZoomConstraintsImpl();
290
                }
291
                return zoomConstraints;
292
        }
293

    
294
        private IPlacementConstraints getPlacementConstraints() {
295
                return placementConstraints;
296
        }
297

    
298
        public void actionPerformed(ActionEvent e) {
299
                System.err.println("GeneralLabeling.actionPerformed() "+((Component)e.getSource()).getName());
300
                if (noEvent) return;
301
                JComponent c = (JComponent)e.getSource();
302

    
303
                if (c.equals(btnPlacement)) {
304

    
305
                        try {
306
                                IPlacementConstraints oldValue = getPlacementConstraints();
307
                                IPlacementProperties pp = PlacementProperties.createPlacementProperties(
308
                                                getPlacementConstraints(),
309
                                                ((FLyrVect) auxLayer).getShapeType());
310
                                PluginServices.getMDIManager().addWindow(pp);
311
                                placementConstraints = pp.getPlacementConstraints();
312

    
313
                                ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
314
                                        propertyChange(new PropertyChangeEvent(
315
                                                        this,
316
                                                        AbstractLabelingMethodPanel.PLACEMENT_CONSTRAINTS,
317
                                                        oldValue,
318
                                                        placementConstraints));
319

    
320
                        } catch (ClassCastException ccEx) {
321
                                NotificationManager.addError(
322
                                                "Placement constraints not prepared for:"
323
                                                +auxLayer.getClass().getName(),
324
                                                ccEx);
325
                        } catch (ReadDriverException dEx) {
326
                                NotificationManager.addWarning(
327
                                                "Should be unreachable code",
328
                                                dEx);
329
                                NotificationManager.addError(
330
                                                PluginServices.getText(
331
                                                                this,
332
                                                                "usupported_layer_type"),
333
                                                dEx);
334
                        }
335

    
336
                } else if (c.equals(btnVisualization)) {
337
                        IZoomConstraints oldValue = getZoomConstraints();
338
                        LabelScaleRange lsr = new LabelScaleRange(oldValue.getMinScale(), oldValue.getMaxScale());
339
                        PluginServices.getMDIManager().addWindow(lsr);
340
                        zoomConstraints = new ZoomConstraintsImpl();
341
                        zoomConstraints.setMaxScale(lsr.getMaxScale());
342
                        zoomConstraints.setMinScale(lsr.getMinScale());
343
                        zoomConstraints.setMode(
344
                                        lsr.getMaxScale() ==-1 && lsr.getMinScale() == -1 ?
345
                                                        IZoomConstraints.DEFINED_BY_THE_LAYER :
346
                                                        IZoomConstraints.DEFINED_BY_THE_USER);
347

    
348
                        ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
349
                        propertyChange(new PropertyChangeEvent(
350
                                        this,
351
                                        AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS,
352
                                        oldValue,
353
                                        getZoomConstraints()));
354

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

    
364
                } else if (c.equals(cmbMethod)) {
365
                        AbstractLabelingMethodPanel p = (AbstractLabelingMethodPanel) cmbMethod.getSelectedItem();
366
                        if(previousMethodPanel == null || previousMethodPanel != p ){
367
                                Container cont = methodPanel.getParent();
368
                                cont.remove(methodPanel);
369
                                emptyContainer(methodPanel);
370

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

    
383
                                        methodPanel.repaint();
384
                                        setVisible(false);
385
                                        setVisible(true);
386
                                } catch (ReadDriverException e1) {
387
                                        NotificationManager.addInfo(new Date(System.currentTimeMillis()).toString(), e1);
388
                                }
389
                                cont.add(methodPanel, BorderLayout.CENTER);
390
                        }
391
                        previousMethodPanel = p;
392
                }
393
                System.out.println("GeneralLabeling.actionPerformed() exit "+((Component)e.getSource()).getName());
394
        }
395

    
396
        private void emptyContainer(Container c) {
397
                for (int i = 0; i < c.getComponentCount(); i++) {
398
                        if (c.getComponent(i) instanceof Container) {
399
                                emptyContainer((Container) c.getComponent(i));
400
                        }
401
                        c.remove(i);
402
                }
403
        }
404

    
405
        public String getLabelingStrategyName() {
406
                return PluginServices.getText(this, "user_defined_labels");
407
        }
408

    
409
        public Class<? extends ILabelingStrategy> getLabelingStrategyClass() {
410
                return GeneralLabelingStrategy.class;
411
        }
412

    
413
        public void setEnabled(boolean enabled) {
414
                super.setEnabled(enabled);
415
                getBtnPlacement().setEnabled(enabled);
416
                getBtnVisualization().setEnabled(enabled);
417
                getChkAllowLabelOverlapping().setEnabled(enabled);
418
                getCmbMethod().setEnabled(enabled);
419
                JPanel mp = getMethodPanel();//.setEnabled(enabled);
420
                mp.setEnabled(enabled);
421
                for (int i=0; i<mp.getComponentCount(); i++){
422
                        Component c = mp.getComponent(i);
423
                        c.setEnabled(enabled);
424
                }
425

    
426
        }
427
}
428

    
429
/*
430
class _GeneralLabeling extends JPanel implements ILabelingStrategyPanel, ActionListener {
431
        private static final long serialVersionUID = 8864709758980903351L;
432
        private static Hashtable<String, Class<? extends ILabelingMethod>> methods
433
                = new Hashtable<String, Class<? extends ILabelingMethod>>();
434
        private static int newClassSuffix = 0;
435
        private JButton btnVisualization;
436
        private JButton btnRenameClass;
437
        private JButton btnSQLQuery;
438
        private JButton btnDelClass;
439
        private JButton btnAddClass;
440
        private JButton btnPlacement;
441
        private JComboBoxLabelingMethod cmbMethod;
442
        private GridBagLayoutPanel classesPanel;
443
        private JComboBox cmbClasses;
444
        private IPlacementConstraints placementConstraints;
445
        private IZoomConstraints zoomConstraints;
446
        private long minScaleView = -1, maxScaleView = -1;
447
        private boolean noEvent;
448
        private JCheckBox chkLabel;
449
        private JCheckBox chkTextOnly;
450
        private FLyrVect layer;
451
        private String[] fieldNames;
452
        private JDnDList lstClassPriorities;
453
        private JCheckBox chkDefinePriorities;
454
        private JScrollPane scrlPan;
455
        private LabelClassRenderingProperties labelClassRenderingProperties;
456
        private JCheckBox chkAllowLabelOverlapping;
457

458
        public _GeneralLabeling() {
459
                initialize();
460
        }
461

462
        private void initialize() {
463
                setLayout(new BorderLayout());
464
                GridBagLayoutPanel left = new GridBagLayoutPanel();
465
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
466
                aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
467
                aux.add(getCmbMethod());
468
                aux.setPreferredSize(new Dimension(605, 40));
469
                left.addComponent(aux);
470

471

472
                classesPanel = new GridBagLayoutPanel();
473
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
474
                aux.add(new JLabel(PluginServices.getText(this, "class")+":"));
475
                aux.add(getCmbClasses());
476
                aux.add(getChkLabelFeatures());
477
                aux.setPreferredSize(new Dimension(602, 40));
478
                classesPanel.addComponent(aux);
479

480
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
481
                aux.add(getBtnAddClass());
482
                aux.add(getBtnDelClass());
483
                aux.add(getBtnRenameClass());
484
                aux.add(getBtnSQLQuery());
485
                classesPanel.addComponent(aux);
486
                classesPanel.setBorder(BorderFactory.createTitledBorder(
487
                                null, PluginServices.getText(this, "classes")));
488
                left.addComponent(classesPanel);
489

490

491
                left.addComponent(labelClassRenderingProperties = new LabelClassRenderingProperties());
492
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
493
                aux.setBorder(BorderFactory.createTitledBorder(
494
                                null, PluginServices.getText(this, "options")));
495
                aux.add(getBtnVisualization());
496

497
                aux.add(getBtnPlacement());
498
                aux.add(getChkAllowLabelOverlapping());
499
                aux.setPreferredSize(new Dimension(612, 60));
500

501
                left.addComponent(aux);
502

503
                add(left, BorderLayout.CENTER);
504

505
                JPanel right = new JPanel(new BorderLayout(15, 15));
506
                aux = new JPanel(new BorderLayout(15, 15));
507
                aux.add(getChkDefinePriorities(), BorderLayout.NORTH);
508
                aux.add(getScrlClassPriorities(), BorderLayout.CENTER);
509
                right.add(new JBlank(10, 10), BorderLayout.NORTH);
510
                right.add(aux, BorderLayout.CENTER);
511
                right.add(new JBlank(10, 10), BorderLayout.SOUTH);
512
                right.add(new JBlank(10, 10), BorderLayout.EAST);
513
                add(right, BorderLayout.EAST);
514
        }
515

516

517
        private JCheckBox getChkAllowLabelOverlapping() {
518
                if (chkAllowLabelOverlapping == null) {
519
                        chkAllowLabelOverlapping = new JCheckBox(PluginServices.getText(this, "allow_label_overlapping"));
520

521
                }
522

523
                return chkAllowLabelOverlapping;
524
        }
525

526
        private Component getScrlClassPriorities() {
527
                if (scrlPan == null) {
528
                        scrlPan = new JScrollPane();
529
                        scrlPan.setViewportView(getLstClassPriorities());
530
                        scrlPan.setPreferredSize(new Dimension(180, 300));
531
                }
532
                return scrlPan;
533
        }
534

535
        private JCheckBox getChkDefinePriorities() {
536
                if (chkDefinePriorities == null) {
537
                        chkDefinePriorities = new JCheckBox(PluginServices.getText(this, "label_priority"));
538
                        chkDefinePriorities.addActionListener(this);
539
                        chkDefinePriorities.setName("CHK_DEFINE_PRIORITIES");
540
                }
541
                return chkDefinePriorities;
542
        }
543

544
        private JDnDList getLstClassPriorities() {
545
                if (lstClassPriorities == null) {
546
                        lstClassPriorities = new JDnDList();
547
                        lstClassPriorities.setName("CLASS_PRIORITY_LIST");
548
                        lstClassPriorities.addMouseListener(new MouseAdapter() {
549
                                @Override
550
                                public void mouseReleased(MouseEvent e) {
551
                                        JDnDListModel m = (JDnDListModel) lstClassPriorities.getModel();
552
                                        for (int i = 0; i < m.getSize(); i++) {
553
                                                ((LabelClass) m.getElementAt(i)).setPriority(i);
554
                                        }
555
                                }
556
                        });
557

558
                }
559
                return lstClassPriorities;
560
        }
561

562
        private void refreshControls() {
563
                // classes combo box
564
                refreshClasses();
565

566
                // fires an event from the methods combo box
567
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
568
        }
569

570
        private JButton getBtnVisualization() {
571
                if (btnVisualization == null) {
572
                        btnVisualization = new JButton(
573
                                        PluginServices.getText(this, "visualization")+"...");
574
                        btnVisualization.setName("BTNVISUALIZATION");
575
                        btnVisualization.addActionListener(this);
576
                }
577
                return btnVisualization;
578
        }
579

580
        private JButton getBtnPlacement() {
581
                if (btnPlacement == null) {
582
                        btnPlacement = new JButton(
583
                                        PluginServices.getText(this, "placement")+"...");
584
                        btnPlacement.setName("BTNPLACEMENT");
585
                        btnPlacement.addActionListener(this);
586
                }
587
                return btnPlacement;
588
        }
589

590
        private JComboBox getCmbMethod() {
591
                if (cmbMethod == null) {
592
                        Iterator<String> it = methods.keySet().iterator();
593
                        ArrayList<MethodItem> aux = new ArrayList<MethodItem>();
594
                        while (it.hasNext()) {
595
                                String name = it.next();
596

597
                                Class<? extends ILabelingMethod> methodClass = (Class<? extends ILabelingMethod>) methods.get(name);
598
                                ILabelingMethod method;
599
                                try {
600
                                        method = (ILabelingMethod) methodClass.newInstance();
601
                                        MethodItem newItem = new MethodItem(name, method);
602
                                        aux.add(newItem);
603

604
                                } catch (InstantiationException e) {
605
                                        NotificationManager.addError("Trying to instantiate an interface" +
606
                                                        " or abstract class + "+methodClass.getName(), e);
607
                                } catch (IllegalAccessException e) {
608
                                        NotificationManager.addError("IllegalAccessException: does " +
609
                                                        methodClass.getName()        + " class have an anonymous" +
610
                                                        " constructor?", e);
611
                                }
612

613
                        }
614
                        cmbMethod = new JComboBoxLabelingMethod(aux.toArray(new MethodItem[0]));
615
                        cmbMethod.setSize(new Dimension(300, 22));
616
                        cmbMethod.setName("CMBMETHOD");
617
                        cmbMethod.addActionListener(this);
618
                }
619
                return cmbMethod;
620
        }
621

622
        private JButton getBtnSQLQuery() {
623
                if (btnSQLQuery == null) {
624
                        btnSQLQuery = new JButton(PluginServices.getText(this, "SQL_query"));
625
                        btnSQLQuery.setName("BTNSQLQUERY");
626
                        btnSQLQuery.addActionListener(this);
627
                }
628
                return btnSQLQuery;
629
        }
630

631
        private JButton getBtnRenameClass() {
632
                if (btnRenameClass == null) {
633
                        btnRenameClass = new JButton(PluginServices.getText(this, "remane_class"));
634
                        btnRenameClass.setName("BTNRENAMECLASS");
635
                        btnRenameClass.addActionListener(this);
636
                }
637
                return btnRenameClass;
638
        }
639

640
        private JButton getBtnDelClass() {
641
                if (btnDelClass == null) {
642
                        btnDelClass = new JButton(PluginServices.getText(this, "delete_class"));
643
                        btnDelClass.setName("BTNDELCLASS");
644
                        btnDelClass.addActionListener(this);
645
                }
646
                return btnDelClass;
647
        }
648

649
        private JButton getBtnAddClass() {
650
                if (btnAddClass == null) {
651
                        btnAddClass = new JButton(PluginServices.getText(this, "add_class"));
652
                        btnAddClass.setName("BTNADDCLASS");
653
                        btnAddClass.addActionListener(this);
654
                }
655
                return btnAddClass;
656
        }
657

658
        private JCheckBox getChkLabelFeatures() {
659
                if (chkLabel == null) {
660
                        chkLabel = new JCheckBox();
661
                        chkLabel.setText(PluginServices.getText(this, "label_features_in_this_class"));
662
                        chkLabel.setName("CHKLABEL");
663
                        chkLabel.addActionListener(this);
664
                }
665
                return chkLabel;
666
        }
667

668
        private JComboBox getCmbClasses() {
669
                if (cmbClasses == null) {
670
                        cmbClasses = new JComboBox();
671
                        cmbClasses.setPreferredSize(new Dimension(150, 20));
672
                        cmbClasses.setName("CMBCLASSES");
673
                        cmbClasses.addActionListener(this);
674
                }
675
                return cmbClasses;
676
        }
677

678
        public ILabelingStrategy getLabelingStrategy() {
679
                ILabelingStrategy st = ExtendedLabelingFactory.
680
                                                createStrategy((FLayer) layer,
681
                                                                getMethod(),
682
                                                                getPlacementConstraints(),
683
                                                                getZoomConstraints());
684
                if (st instanceof GeneralLabelingStrategy) {
685
                        GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) st;
686
                        gStr.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
687
                        gStr.setMinScaleView(minScaleView);
688
                        gStr.setMaxScaleView(maxScaleView);
689
                }
690
                return st;
691
        }
692

693
        public void setModel(FLayer layer, ILabelingStrategy str) {
694
                if (layer instanceof FLyrVect) {
695
                        this.layer = (FLyrVect) layer;
696
                        FLyrVect lv = (FLyrVect) layer;
697
                        try {
698
                                fieldNames = lv.getRecordset().getFieldNames();
699
                                labelClassRenderingProperties.setFieldNames(fieldNames);
700
                                if (str instanceof GeneralLabelingStrategy) {
701
                                        try {
702
                                                GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) LabelingFactory.createStrategyFromXML(str.getXMLEntity(), layer.cloneLayer());
703
                                                setMethod(str.getLabelingMethod());
704
                                                placementConstraints = str.getPlacementConstraints();
705
                                                getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
706
                                                minScaleView = gStr.getMinScaleView();
707
                                                maxScaleView = gStr.getMaxScaleView();
708

709
                                        } catch (ReadDriverException e) {
710
                                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
711
                                        } catch (Exception e) {
712
                                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
713
                                        }
714
                                }
715

716
                                refreshControls();
717
                                labelClassRenderingProperties.setModel(getActiveClass());
718

719
                        } catch (ReadDriverException e) {
720
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
721
                        }
722
                }
723
        }
724

725

726
        public static void addLabelingMethod(String name, Class<? extends ILabelingMethod> iLabelingMethodClass) {
727
                methods.put(name, iLabelingMethodClass);
728
        }
729

730
        private void setMethod(ILabelingMethod labelingMethod) {
731
                getCmbMethod().setSelectedItem(new MethodItem(null, labelingMethod));
732
        }
733

734
        private ILabelingMethod getMethod() {
735
                return ((MethodItem) getCmbMethod().getSelectedItem()).method;
736
        }
737

738
        private void refreshClasses() {
739
                // label classes
740
                getCmbClasses().removeAllItems();
741
                LabelClass[] lClasses = getMethod().getLabelClasses();
742
                for (int i = 0; i < lClasses.length; i++) {
743
                        getCmbClasses().addItem(lClasses[i]);
744

745
                }
746

747
                labelClassRenderingProperties.setModel(
748
                                (LabelClass) getCmbClasses().getSelectedItem());
749
                // expressions combo box
750
//                refreshCmbExpressions();
751

752
                // panel priorities
753
                refreshPnlPriorities();
754

755
        }
756

757
        private void refreshPnlPriorities() {
758
                TreeSet<LabelClass> ts = new TreeSet<LabelClass>(new LabelClassComparatorByPriority());
759

760
                LabelClass[] lClasses = getMethod().getLabelClasses();
761
                for (int i = 0; i < lClasses.length; i++) {
762
                        ts.add(lClasses[i]);
763
                }
764

765
                // refresh label priority panel
766
                getChkDefinePriorities().setSelected(getMethod().definesPriorities());
767
                JDnDListModel m = new JDnDListModel();
768
                for (LabelClass labelClass : ts) {
769
                        m.addElement(labelClass);
770
                }
771
                getLstClassPriorities().setModel(m);
772

773
        }
774

775
        private LabelClass getActiveClass() {
776
                return (LabelClass) getCmbClasses().getSelectedItem();
777
        }
778

779
        private IZoomConstraints getZoomConstraints() {
780
                return zoomConstraints;
781
        }
782

783
        private IPlacementConstraints getPlacementConstraints() {
784
                return placementConstraints;
785
        }
786

787
        public void actionPerformed(ActionEvent e) {
788
                if (noEvent) return;
789
                JComponent c = (JComponent)e.getSource();
790

791
                if (c.equals(btnAddClass)) {
792
                        LabelClass newClass = new LabelClass();
793
                        newClass.setName(PluginServices.getText(this, "labeling")+String.valueOf(++newClassSuffix));
794
                        getMethod().addLabelClass(newClass);
795
                        refreshClasses();
796
                        getCmbClasses().setSelectedItem(newClass);
797
                } else if (c.equals(btnDelClass)) {
798
                        LabelClass clazz = getActiveClass();
799
                        getMethod().deleteLabelClass(clazz);
800
                        refreshClasses();
801
                } else if (c.equals(btnRenameClass)) {
802
                        LabelClass clazz = getActiveClass();
803
                        String newName = JOptionPane.showInputDialog(
804
                                        PluginServices.getText(this, "enter_new_name"));
805
                        if (newName != null)
806
                                getMethod().renameLabelClass(clazz, newName);
807
                        refreshClasses();
808
                } else if (c.equals(btnSQLQuery)) {
809
                        LabelClass clazz = getActiveClass();
810
                        String query = clazz.getSQLQuery();
811
                        query = JOptionPane.showInputDialog(null,
812
                                          "select from "+layer.getName()+" where ",
813
                                          "SQL query",
814
                                          JOptionPane.QUESTION_MESSAGE);
815
                        if (!query.equals("")) {
816
                                clazz.setSQLQuery(query);
817
                        }
818

819
                } else  if (c.equals(chkLabel)) {
820
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
821
                        if (lc == null)
822
                                lc = getMethod().getDefaultLabelClass();
823
                        lc.setVisible(chkLabel.isSelected());
824
                } else if (c.equals(chkTextOnly)) {
825

826
                } else if (c.equals(cmbClasses)) {
827
                        // refresh expressions
828
//                        LabelClass lc = (LabelClass) cmbClasses.getSelectedItem();
829
//                        if (lc != null && lc.getLabelExpression() != null && !getExpressions().contains(lc.getLabelExpression())) {
830
//                                getExpressions().add(0, lc.getLabelExpression());
831
//                        }
832
//                        refreshCmbExpressions();
833
                } else if (c.equals(btnPlacement)) {
834

835
                        try {
836
                                IPlacementProperties pp = PlacementProperties.createPlacementProperties(
837
                                                getPlacementConstraints(),
838
                                                ((FLyrVect) layer).getShapeType());
839
                                PluginServices.getMDIManager().addWindow(pp);
840
                                placementConstraints = pp.getPlacementConstraints();
841
                        } catch (ClassCastException ccEx) {
842
                                NotificationManager.addError("Placement constraints not prepared for:"
843
                                                +layer.getClass().getName(),
844
                                                ccEx);
845
                        } catch (ReadDriverException dEx) {
846
                                NotificationManager.addWarning("Should be unreachable code", dEx);
847
                                NotificationManager.addError(PluginServices.getText(this, "usupported_layer_type"), dEx);
848
                        }
849

850
                } else if (c.equals(btnVisualization)) {
851
                        LabelScaleRange lsr = new LabelScaleRange(minScaleView, maxScaleView);
852
                        PluginServices.getMDIManager().addWindow(lsr);
853
                        minScaleView = lsr.getMinScale();
854
                        maxScaleView = lsr.getMaxScale();
855
                } else if (c.equals(cmbMethod)) {
856
                        // disable components in class panel
857
                        // multiple class or not enables or disables the class panel
858
                        setComponentEnabled(classesPanel, getMethod().allowsMultipleClass());
859
                        refreshClasses();
860

861
                } else if (c.equals(chkDefinePriorities)) {
862
                        getMethod().setDefinesPriorities(chkDefinePriorities.isSelected());
863
                        refreshPnlPriorities();
864
                }
865
        }
866

867
        private void setComponentEnabled(Component c, boolean b) {
868
                if (c instanceof JComponent) {
869
                        JComponent c1 = (JComponent) c;
870
                        for (int i = 0; i < c1.getComponentCount(); i++) {
871
                                setComponentEnabled(c1.getComponent(i), b);
872
                        }
873
                }
874
                c.setEnabled(b);
875
        }
876

877
        public String getLabelingStrategyName() {
878
                return PluginServices.getText(this, "user_defined_labels");
879
        }
880

881
        public Class getLabelingStrategyClass() {
882
                return GeneralLabelingStrategy.class;
883
        }
884

885
        private class JComboBoxLabelingMethod extends JComboBox {
886
                private static final long serialVersionUID = 5935267402200698145L;
887

888
                public JComboBoxLabelingMethod(MethodItem[] items) {
889
                        super(items);
890
                }
891

892
                @Override
893
                public void setSelectedItem(Object anObject) {
894
                        if (anObject instanceof MethodItem) {
895
                                MethodItem methodItem = (MethodItem) anObject;
896
                                for (int i = 0; i < getItemCount(); i++) {
897
                                        MethodItem aux = (MethodItem) getItemAt(i);
898
                                        if (aux.equals(methodItem)) {
899
                                                aux.method=methodItem.method;
900
                                        }
901
                                }
902
                                super.setSelectedItem(methodItem);
903
//                                ((MethodItem) super.getSelectedItem()).method = methodItem.method;
904
                        }
905
                }
906
        }
907
        private class MethodItem {
908
                private String name;
909
                private ILabelingMethod method;
910

911
                private MethodItem(String name, ILabelingMethod method) {
912
                        this.name = name;
913
                        this.method = method;
914
                }
915

916
                public String toString() {
917
                        return name;
918
                }
919

920
                @Override
921
                public boolean equals(Object obj) {
922
                        if (obj != null && obj instanceof MethodItem) {
923
                                MethodItem methodItem = (MethodItem) obj;
924
                                return methodItem.method.getClass().equals(this.method.getClass());
925
                        }
926
                        return false;
927
                }
928
        }
929

930
}*/