Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / gui / layerproperties / GeneralLabeling.java @ 23062

History | View | Annotate | Download (28.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.symbology.gui.layerproperties;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.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.Date;
53
import java.util.Hashtable;
54
import java.util.Iterator;
55

    
56
import javax.swing.BorderFactory;
57
import javax.swing.JCheckBox;
58
import javax.swing.JComponent;
59
import javax.swing.JLabel;
60
import javax.swing.JPanel;
61

    
62
import org.apache.log4j.Logger;
63
import org.gvsig.gui.beans.swing.JButton;
64
import org.gvsig.symbology.fmap.labeling.ExtendedLabelingFactory;
65
import org.gvsig.symbology.fmap.labeling.GeneralLabelingStrategy;
66

    
67
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
71
import com.iver.cit.gvsig.fmap.layers.FLayer;
72
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
73
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
74
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
75
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.DefaultLabelingMethod;
76
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingMethod;
77
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
78
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IPlacementConstraints;
79
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.IZoomConstraints;
80
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelingFactory;
81
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ZoomConstraintsImpl;
82
import com.iver.cit.gvsig.project.documents.view.legend.gui.ILabelingStrategyPanel;
83
import com.iver.utiles.swing.JComboBox;
84

    
85

    
86
public class GeneralLabeling extends JPanel implements ILabelingStrategyPanel, ActionListener {
87
        private static final long serialVersionUID = 8864709758980903351L;
88
        private static Hashtable<
89
                        Class<? extends ILabelingMethod>,
90
                        Class<? extends AbstractLabelingMethodPanel>
91
                > methods
92
                = new Hashtable<
93
                        Class<? extends ILabelingMethod>,
94
                        Class<? extends AbstractLabelingMethodPanel>
95
                >();
96
        private JButton btnVisualization;
97
        private JButton btnPlacement;
98
        private JComboBox cmbMethod;
99
        private JPanel methodPanel;
100
        private IPlacementConstraints placementConstraints;
101
        private IZoomConstraints zoomConstraints;
102
        private boolean noEvent;
103
        private FLyrVect targetLayer;
104
        private JCheckBox chkAllowLabelOverlapping;
105
        private FLyrVect auxLayer;
106
        private GeneralLabelingStrategy gStr;
107
        public GeneralLabeling() {
108
                initialize();
109
        }
110

    
111
        private void initialize() {
112
                setLayout(new BorderLayout());
113
                JPanel center = new JPanel(new BorderLayout(10, 10));
114
                center.setBorder(BorderFactory.createTitledBorder(
115
                                null, PluginServices.getText(this, "classes")));
116
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
117
                        aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
118
                        aux.add(getCmbMethod());
119
                        aux.setPreferredSize(new Dimension(605, 40));
120
                        center.add(aux, BorderLayout.NORTH);
121

    
122

    
123
                        // el panell del m?tode de moltes FeatureDependantLabelingMethod
124
                        methodPanel = new JPanel(new BorderLayout(10, 0));
125
                        center.add(methodPanel, BorderLayout.CENTER);
126
                add(center, BorderLayout.CENTER);
127

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

    
136
                add(south, BorderLayout.SOUTH);
137
        }
138

    
139

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

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

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

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

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

    
192
        public ILabelingStrategy getLabelingStrategy() {
193
                ILabelingStrategy st = ExtendedLabelingFactory.
194
                                                createStrategy((FLayer) targetLayer,
195
                                                                getMethod(),
196
                                                                getPlacementConstraints(),
197
                                                                getZoomConstraints());
198
                if (st instanceof GeneralLabelingStrategy) {
199
                        GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) st;
200
                        gStr.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
201
                        gStr.setZoomConstraints(getZoomConstraints());
202
                }
203
                return st;
204
        }
205

    
206
        public void setModel(FLayer layer, ILabelingStrategy str) {
207
                if (layer instanceof FLyrVect) {
208
                        try {
209
                                targetLayer = (FLyrVect) layer;//.cloneLayer();
210

    
211
                                VectorialDriver vd = (VectorialDriver) ((FLyrVect) layer).getSource().getDriver();
212

    
213
                                auxLayer = (FLyrVect) LayerFactory.createLayer(layer.getName(),vd, layer.getProjection());
214

    
215
                                auxLayer.setLegend((IVectorLegend)targetLayer.getLegend());
216

    
217

    
218
                                if (auxLayer.getProjection() == null) {
219
                                        Logger.getLogger(getClass()).debug("Possible bug detected in " +
220
                                                        "FLyrVect.cloneLayer() (missing projection in cloned layer)");
221
                                        // this line should be unnecessary (and included in cloneLayer) method);
222
                                        auxLayer.setProjection(targetLayer.getProjection());
223
                                }
224
                                //
225

    
226
                                if (str instanceof GeneralLabelingStrategy) {
227
                                        gStr = (GeneralLabelingStrategy) LabelingFactory.
228
                                                createStrategyFromXML(str.getXMLEntity(), auxLayer/*layer.cloneLayer()*/);
229
                                        auxLayer.setLabelingStrategy(gStr);
230
                                        gStr.setLayer(auxLayer);
231
                                        setMethod(gStr.getLabelingMethod(), auxLayer);
232
                                        placementConstraints = gStr.getPlacementConstraints();
233
                                        zoomConstraints = gStr.getZoomConstraints();
234
                                        getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
235

    
236
                                }
237
                        } catch (ReadDriverException e) {
238
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
239
                        } catch (Exception e) {
240
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
241
                        }
242
                        refreshControls();
243
                }
244
        }
245

    
246

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

    
258
        private void setMethod(ILabelingMethod labelingMethod, FLyrVect srcLayer) {
259
                methodPanel.removeAll();
260
                AbstractLabelingMethodPanel p;
261
                try {
262
                        p = methods.get(labelingMethod.getClass()).newInstance();
263
                        p.setModel(labelingMethod, srcLayer);
264
                        System.out.println("last labeling method was"+p.getClass().getName());
265
                        getCmbMethod().setSelectedItem(p);
266
                } catch (Exception e) {
267
                        // should be impossible;
268
                        NotificationManager.addWarning(e.getLocalizedMessage());
269
                }
270

    
271
        }
272

    
273
        private ILabelingMethod getMethod() {
274
                if(gStr != null){
275
                        return gStr.getLabelingMethod();
276
                }
277

    
278
                return new DefaultLabelingMethod();
279
        }
280

    
281
        private IZoomConstraints getZoomConstraints() {
282
                if (zoomConstraints == null) {
283
                        zoomConstraints = new ZoomConstraintsImpl();
284
                }
285
                return zoomConstraints;
286
        }
287

    
288
        private IPlacementConstraints getPlacementConstraints() {
289
                return placementConstraints;
290
        }
291

    
292
        public void actionPerformed(ActionEvent e) {
293
                System.err.println("GeneralLabeling.actionPerformed() "+((Component)e.getSource()).getName());
294
                if (noEvent) return;
295
                JComponent c = (JComponent)e.getSource();
296

    
297
                if (c.equals(btnPlacement)) {
298

    
299
                        try {
300
                                IPlacementConstraints oldValue = getPlacementConstraints();
301
                                IPlacementProperties pp = PlacementProperties.createPlacementProperties(
302
                                                getPlacementConstraints(),
303
                                                ((FLyrVect) auxLayer).getShapeType());
304
                                PluginServices.getMDIManager().addWindow(pp);
305
                                placementConstraints = pp.getPlacementConstraints();
306

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

    
314
                                gStr.setPlacementConstraints(placementConstraints);
315

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

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

    
344
                        ((AbstractLabelingMethodPanel) cmbMethod.getSelectedItem()).
345
                        propertyChange(new PropertyChangeEvent(
346
                                        this,
347
                                        AbstractLabelingMethodPanel.ZOOM_CONSTRAINTS,
348
                                        oldValue,
349
                                        getZoomConstraints()));
350

    
351
                        gStr.setZoomConstraints(zoomConstraints);
352

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

    
362
                        gStr.setAllowOverlapping(newValue);
363

    
364
                } else if (c.equals(cmbMethod)) {
365
                        Container cont = methodPanel.getParent();
366
                        cont.remove(methodPanel);
367
                        emptyContainer(methodPanel);
368

    
369
                        AbstractLabelingMethodPanel p = (AbstractLabelingMethodPanel) cmbMethod.getSelectedItem();
370
                        try {
371

    
372
                                p.setModel(getMethod(), auxLayer);
373
                                methodPanel.add(
374
                                                p,
375
                                                BorderLayout.CENTER);
376

    
377
                                methodPanel.repaint();
378
                                setVisible(false);
379
                                setVisible(true);
380
                        } catch (ReadDriverException e1) {
381
                                NotificationManager.addInfo(new Date(System.currentTimeMillis()).toString(), e1);
382
                        }
383
                        cont.add(methodPanel, BorderLayout.CENTER);
384
                }
385
                System.out.println("GeneralLabeling.actionPerformed() exit"+((Component)e.getSource()).getName());
386
        }
387

    
388
        private void emptyContainer(Container c) {
389
                for (int i = 0; i < c.getComponentCount(); i++) {
390
                        if (c.getComponent(i) instanceof Container) {
391
                                emptyContainer((Container) c.getComponent(i));
392
                        }
393
                        c.remove(i);
394
                }
395
        }
396

    
397
        public String getLabelingStrategyName() {
398
                return PluginServices.getText(this, "user_defined_labels");
399
        }
400

    
401
        public Class<? extends ILabelingStrategy> getLabelingStrategyClass() {
402
                return GeneralLabelingStrategy.class;
403
        }
404
}
405

    
406
/*
407
class _GeneralLabeling extends JPanel implements ILabelingStrategyPanel, ActionListener {
408
        private static final long serialVersionUID = 8864709758980903351L;
409
        private static Hashtable<String, Class<? extends ILabelingMethod>> methods
410
                = new Hashtable<String, Class<? extends ILabelingMethod>>();
411
        private static int newClassSuffix = 0;
412
        private JButton btnVisualization;
413
        private JButton btnRenameClass;
414
        private JButton btnSQLQuery;
415
        private JButton btnDelClass;
416
        private JButton btnAddClass;
417
        private JButton btnPlacement;
418
        private JComboBoxLabelingMethod cmbMethod;
419
        private GridBagLayoutPanel classesPanel;
420
        private JComboBox cmbClasses;
421
        private IPlacementConstraints placementConstraints;
422
        private IZoomConstraints zoomConstraints;
423
        private long minScaleView = -1, maxScaleView = -1;
424
        private boolean noEvent;
425
        private JCheckBox chkLabel;
426
        private JCheckBox chkTextOnly;
427
        private FLyrVect layer;
428
        private String[] fieldNames;
429
        private JDnDList lstClassPriorities;
430
        private JCheckBox chkDefinePriorities;
431
        private JScrollPane scrlPan;
432
        private LabelClassRenderingProperties labelClassRenderingProperties;
433
        private JCheckBox chkAllowLabelOverlapping;
434

435
        public _GeneralLabeling() {
436
                initialize();
437
        }
438

439
        private void initialize() {
440
                setLayout(new BorderLayout());
441
                GridBagLayoutPanel left = new GridBagLayoutPanel();
442
                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
443
                aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
444
                aux.add(getCmbMethod());
445
                aux.setPreferredSize(new Dimension(605, 40));
446
                left.addComponent(aux);
447

448

449
                classesPanel = new GridBagLayoutPanel();
450
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
451
                aux.add(new JLabel(PluginServices.getText(this, "class")+":"));
452
                aux.add(getCmbClasses());
453
                aux.add(getChkLabelFeatures());
454
                aux.setPreferredSize(new Dimension(602, 40));
455
                classesPanel.addComponent(aux);
456

457
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
458
                aux.add(getBtnAddClass());
459
                aux.add(getBtnDelClass());
460
                aux.add(getBtnRenameClass());
461
                aux.add(getBtnSQLQuery());
462
                classesPanel.addComponent(aux);
463
                classesPanel.setBorder(BorderFactory.createTitledBorder(
464
                                null, PluginServices.getText(this, "classes")));
465
                left.addComponent(classesPanel);
466

467

468
                left.addComponent(labelClassRenderingProperties = new LabelClassRenderingProperties());
469
                aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
470
                aux.setBorder(BorderFactory.createTitledBorder(
471
                                null, PluginServices.getText(this, "options")));
472
                aux.add(getBtnVisualization());
473

474
                aux.add(getBtnPlacement());
475
                aux.add(getChkAllowLabelOverlapping());
476
                aux.setPreferredSize(new Dimension(612, 60));
477

478
                left.addComponent(aux);
479

480
                add(left, BorderLayout.CENTER);
481

482
                JPanel right = new JPanel(new BorderLayout(15, 15));
483
                aux = new JPanel(new BorderLayout(15, 15));
484
                aux.add(getChkDefinePriorities(), BorderLayout.NORTH);
485
                aux.add(getScrlClassPriorities(), BorderLayout.CENTER);
486
                right.add(new JBlank(10, 10), BorderLayout.NORTH);
487
                right.add(aux, BorderLayout.CENTER);
488
                right.add(new JBlank(10, 10), BorderLayout.SOUTH);
489
                right.add(new JBlank(10, 10), BorderLayout.EAST);
490
                add(right, BorderLayout.EAST);
491
        }
492

493

494
        private JCheckBox getChkAllowLabelOverlapping() {
495
                if (chkAllowLabelOverlapping == null) {
496
                        chkAllowLabelOverlapping = new JCheckBox(PluginServices.getText(this, "allow_label_overlapping"));
497

498
                }
499

500
                return chkAllowLabelOverlapping;
501
        }
502

503
        private Component getScrlClassPriorities() {
504
                if (scrlPan == null) {
505
                        scrlPan = new JScrollPane();
506
                        scrlPan.setViewportView(getLstClassPriorities());
507
                        scrlPan.setPreferredSize(new Dimension(180, 300));
508
                }
509
                return scrlPan;
510
        }
511

512
        private JCheckBox getChkDefinePriorities() {
513
                if (chkDefinePriorities == null) {
514
                        chkDefinePriorities = new JCheckBox(PluginServices.getText(this, "label_priority"));
515
                        chkDefinePriorities.addActionListener(this);
516
                        chkDefinePriorities.setName("CHK_DEFINE_PRIORITIES");
517
                }
518
                return chkDefinePriorities;
519
        }
520

521
        private JDnDList getLstClassPriorities() {
522
                if (lstClassPriorities == null) {
523
                        lstClassPriorities = new JDnDList();
524
                        lstClassPriorities.setName("CLASS_PRIORITY_LIST");
525
                        lstClassPriorities.addMouseListener(new MouseAdapter() {
526
                                @Override
527
                                public void mouseReleased(MouseEvent e) {
528
                                        JDnDListModel m = (JDnDListModel) lstClassPriorities.getModel();
529
                                        for (int i = 0; i < m.getSize(); i++) {
530
                                                ((LabelClass) m.getElementAt(i)).setPriority(i);
531
                                        }
532
                                }
533
                        });
534

535
                }
536
                return lstClassPriorities;
537
        }
538

539
        private void refreshControls() {
540
                // classes combo box
541
                refreshClasses();
542

543
                // fires an event from the methods combo box
544
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
545
        }
546

547
        private JButton getBtnVisualization() {
548
                if (btnVisualization == null) {
549
                        btnVisualization = new JButton(
550
                                        PluginServices.getText(this, "visualization")+"...");
551
                        btnVisualization.setName("BTNVISUALIZATION");
552
                        btnVisualization.addActionListener(this);
553
                }
554
                return btnVisualization;
555
        }
556

557
        private JButton getBtnPlacement() {
558
                if (btnPlacement == null) {
559
                        btnPlacement = new JButton(
560
                                        PluginServices.getText(this, "placement")+"...");
561
                        btnPlacement.setName("BTNPLACEMENT");
562
                        btnPlacement.addActionListener(this);
563
                }
564
                return btnPlacement;
565
        }
566

567
        private JComboBox getCmbMethod() {
568
                if (cmbMethod == null) {
569
                        Iterator<String> it = methods.keySet().iterator();
570
                        ArrayList<MethodItem> aux = new ArrayList<MethodItem>();
571
                        while (it.hasNext()) {
572
                                String name = it.next();
573

574
                                Class<? extends ILabelingMethod> methodClass = (Class<? extends ILabelingMethod>) methods.get(name);
575
                                ILabelingMethod method;
576
                                try {
577
                                        method = (ILabelingMethod) methodClass.newInstance();
578
                                        MethodItem newItem = new MethodItem(name, method);
579
                                        aux.add(newItem);
580

581
                                } catch (InstantiationException e) {
582
                                        NotificationManager.addError("Trying to instantiate an interface" +
583
                                                        " or abstract class + "+methodClass.getName(), e);
584
                                } catch (IllegalAccessException e) {
585
                                        NotificationManager.addError("IllegalAccessException: does " +
586
                                                        methodClass.getName()        + " class have an anonymous" +
587
                                                        " constructor?", e);
588
                                }
589

590
                        }
591
                        cmbMethod = new JComboBoxLabelingMethod(aux.toArray(new MethodItem[0]));
592
                        cmbMethod.setSize(new Dimension(300, 22));
593
                        cmbMethod.setName("CMBMETHOD");
594
                        cmbMethod.addActionListener(this);
595
                }
596
                return cmbMethod;
597
        }
598

599
        private JButton getBtnSQLQuery() {
600
                if (btnSQLQuery == null) {
601
                        btnSQLQuery = new JButton(PluginServices.getText(this, "SQL_query"));
602
                        btnSQLQuery.setName("BTNSQLQUERY");
603
                        btnSQLQuery.addActionListener(this);
604
                }
605
                return btnSQLQuery;
606
        }
607

608
        private JButton getBtnRenameClass() {
609
                if (btnRenameClass == null) {
610
                        btnRenameClass = new JButton(PluginServices.getText(this, "remane_class"));
611
                        btnRenameClass.setName("BTNRENAMECLASS");
612
                        btnRenameClass.addActionListener(this);
613
                }
614
                return btnRenameClass;
615
        }
616

617
        private JButton getBtnDelClass() {
618
                if (btnDelClass == null) {
619
                        btnDelClass = new JButton(PluginServices.getText(this, "delete_class"));
620
                        btnDelClass.setName("BTNDELCLASS");
621
                        btnDelClass.addActionListener(this);
622
                }
623
                return btnDelClass;
624
        }
625

626
        private JButton getBtnAddClass() {
627
                if (btnAddClass == null) {
628
                        btnAddClass = new JButton(PluginServices.getText(this, "add_class"));
629
                        btnAddClass.setName("BTNADDCLASS");
630
                        btnAddClass.addActionListener(this);
631
                }
632
                return btnAddClass;
633
        }
634

635
        private JCheckBox getChkLabelFeatures() {
636
                if (chkLabel == null) {
637
                        chkLabel = new JCheckBox();
638
                        chkLabel.setText(PluginServices.getText(this, "label_features_in_this_class"));
639
                        chkLabel.setName("CHKLABEL");
640
                        chkLabel.addActionListener(this);
641
                }
642
                return chkLabel;
643
        }
644

645
        private JComboBox getCmbClasses() {
646
                if (cmbClasses == null) {
647
                        cmbClasses = new JComboBox();
648
                        cmbClasses.setPreferredSize(new Dimension(150, 20));
649
                        cmbClasses.setName("CMBCLASSES");
650
                        cmbClasses.addActionListener(this);
651
                }
652
                return cmbClasses;
653
        }
654

655
        public ILabelingStrategy getLabelingStrategy() {
656
                ILabelingStrategy st = ExtendedLabelingFactory.
657
                                                createStrategy((FLayer) layer,
658
                                                                getMethod(),
659
                                                                getPlacementConstraints(),
660
                                                                getZoomConstraints());
661
                if (st instanceof GeneralLabelingStrategy) {
662
                        GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) st;
663
                        gStr.setAllowOverlapping(getChkAllowLabelOverlapping().isSelected());
664
                        gStr.setMinScaleView(minScaleView);
665
                        gStr.setMaxScaleView(maxScaleView);
666
                }
667
                return st;
668
        }
669

670
        public void setModel(FLayer layer, ILabelingStrategy str) {
671
                if (layer instanceof FLyrVect) {
672
                        this.layer = (FLyrVect) layer;
673
                        FLyrVect lv = (FLyrVect) layer;
674
                        try {
675
                                fieldNames = lv.getRecordset().getFieldNames();
676
                                labelClassRenderingProperties.setFieldNames(fieldNames);
677
                                if (str instanceof GeneralLabelingStrategy) {
678
                                        try {
679
                                                GeneralLabelingStrategy gStr = (GeneralLabelingStrategy) LabelingFactory.createStrategyFromXML(str.getXMLEntity(), layer.cloneLayer());
680
                                                setMethod(str.getLabelingMethod());
681
                                                placementConstraints = str.getPlacementConstraints();
682
                                                getChkAllowLabelOverlapping().setSelected(gStr.isAllowingOverlap());
683
                                                minScaleView = gStr.getMinScaleView();
684
                                                maxScaleView = gStr.getMaxScaleView();
685

686
                                        } catch (ReadDriverException e) {
687
                                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
688
                                        } catch (Exception e) {
689
                                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
690
                                        }
691
                                }
692

693
                                refreshControls();
694
                                labelClassRenderingProperties.setModel(getActiveClass());
695

696
                        } catch (ReadDriverException e) {
697
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
698
                        }
699
                }
700
        }
701

702

703
        public static void addLabelingMethod(String name, Class<? extends ILabelingMethod> iLabelingMethodClass) {
704
                methods.put(name, iLabelingMethodClass);
705
        }
706

707
        private void setMethod(ILabelingMethod labelingMethod) {
708
                getCmbMethod().setSelectedItem(new MethodItem(null, labelingMethod));
709
        }
710

711
        private ILabelingMethod getMethod() {
712
                return ((MethodItem) getCmbMethod().getSelectedItem()).method;
713
        }
714

715
        private void refreshClasses() {
716
                // label classes
717
                getCmbClasses().removeAllItems();
718
                LabelClass[] lClasses = getMethod().getLabelClasses();
719
                for (int i = 0; i < lClasses.length; i++) {
720
                        getCmbClasses().addItem(lClasses[i]);
721

722
                }
723

724
                labelClassRenderingProperties.setModel(
725
                                (LabelClass) getCmbClasses().getSelectedItem());
726
                // expressions combo box
727
//                refreshCmbExpressions();
728

729
                // panel priorities
730
                refreshPnlPriorities();
731

732
        }
733

734
        private void refreshPnlPriorities() {
735
                TreeSet<LabelClass> ts = new TreeSet<LabelClass>(new LabelClassComparatorByPriority());
736

737
                LabelClass[] lClasses = getMethod().getLabelClasses();
738
                for (int i = 0; i < lClasses.length; i++) {
739
                        ts.add(lClasses[i]);
740
                }
741

742
                // refresh label priority panel
743
                getChkDefinePriorities().setSelected(getMethod().definesPriorities());
744
                JDnDListModel m = new JDnDListModel();
745
                for (LabelClass labelClass : ts) {
746
                        m.addElement(labelClass);
747
                }
748
                getLstClassPriorities().setModel(m);
749

750
        }
751

752
        private LabelClass getActiveClass() {
753
                return (LabelClass) getCmbClasses().getSelectedItem();
754
        }
755

756
        private IZoomConstraints getZoomConstraints() {
757
                return zoomConstraints;
758
        }
759

760
        private IPlacementConstraints getPlacementConstraints() {
761
                return placementConstraints;
762
        }
763

764
        public void actionPerformed(ActionEvent e) {
765
                if (noEvent) return;
766
                JComponent c = (JComponent)e.getSource();
767

768
                if (c.equals(btnAddClass)) {
769
                        LabelClass newClass = new LabelClass();
770
                        newClass.setName(PluginServices.getText(this, "labeling")+String.valueOf(++newClassSuffix));
771
                        getMethod().addLabelClass(newClass);
772
                        refreshClasses();
773
                        getCmbClasses().setSelectedItem(newClass);
774
                } else if (c.equals(btnDelClass)) {
775
                        LabelClass clazz = getActiveClass();
776
                        getMethod().deleteLabelClass(clazz);
777
                        refreshClasses();
778
                } else if (c.equals(btnRenameClass)) {
779
                        LabelClass clazz = getActiveClass();
780
                        String newName = JOptionPane.showInputDialog(
781
                                        PluginServices.getText(this, "enter_new_name"));
782
                        if (newName != null)
783
                                getMethod().renameLabelClass(clazz, newName);
784
                        refreshClasses();
785
                } else if (c.equals(btnSQLQuery)) {
786
                        LabelClass clazz = getActiveClass();
787
                        String query = clazz.getSQLQuery();
788
                        query = JOptionPane.showInputDialog(null,
789
                                          "select from "+layer.getName()+" where ",
790
                                          "SQL query",
791
                                          JOptionPane.QUESTION_MESSAGE);
792
                        if (!query.equals("")) {
793
                                clazz.setSQLQuery(query);
794
                        }
795

796
                } else  if (c.equals(chkLabel)) {
797
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
798
                        if (lc == null)
799
                                lc = getMethod().getDefaultLabelClass();
800
                        lc.setVisible(chkLabel.isSelected());
801
                } else if (c.equals(chkTextOnly)) {
802

803
                } else if (c.equals(cmbClasses)) {
804
                        // refresh expressions
805
//                        LabelClass lc = (LabelClass) cmbClasses.getSelectedItem();
806
//                        if (lc != null && lc.getLabelExpression() != null && !getExpressions().contains(lc.getLabelExpression())) {
807
//                                getExpressions().add(0, lc.getLabelExpression());
808
//                        }
809
//                        refreshCmbExpressions();
810
                } else if (c.equals(btnPlacement)) {
811

812
                        try {
813
                                IPlacementProperties pp = PlacementProperties.createPlacementProperties(
814
                                                getPlacementConstraints(),
815
                                                ((FLyrVect) layer).getShapeType());
816
                                PluginServices.getMDIManager().addWindow(pp);
817
                                placementConstraints = pp.getPlacementConstraints();
818
                        } catch (ClassCastException ccEx) {
819
                                NotificationManager.addError("Placement constraints not prepared for:"
820
                                                +layer.getClass().getName(),
821
                                                ccEx);
822
                        } catch (ReadDriverException dEx) {
823
                                NotificationManager.addWarning("Should be unreachable code", dEx);
824
                                NotificationManager.addError(PluginServices.getText(this, "usupported_layer_type"), dEx);
825
                        }
826

827
                } else if (c.equals(btnVisualization)) {
828
                        LabelScaleRange lsr = new LabelScaleRange(minScaleView, maxScaleView);
829
                        PluginServices.getMDIManager().addWindow(lsr);
830
                        minScaleView = lsr.getMinScale();
831
                        maxScaleView = lsr.getMaxScale();
832
                } else if (c.equals(cmbMethod)) {
833
                        // disable components in class panel
834
                        // multiple class or not enables or disables the class panel
835
                        setComponentEnabled(classesPanel, getMethod().allowsMultipleClass());
836
                        refreshClasses();
837

838
                } else if (c.equals(chkDefinePriorities)) {
839
                        getMethod().setDefinesPriorities(chkDefinePriorities.isSelected());
840
                        refreshPnlPriorities();
841
                }
842
        }
843

844
        private void setComponentEnabled(Component c, boolean b) {
845
                if (c instanceof JComponent) {
846
                        JComponent c1 = (JComponent) c;
847
                        for (int i = 0; i < c1.getComponentCount(); i++) {
848
                                setComponentEnabled(c1.getComponent(i), b);
849
                        }
850
                }
851
                c.setEnabled(b);
852
        }
853

854
        public String getLabelingStrategyName() {
855
                return PluginServices.getText(this, "user_defined_labels");
856
        }
857

858
        public Class getLabelingStrategyClass() {
859
                return GeneralLabelingStrategy.class;
860
        }
861

862
        private class JComboBoxLabelingMethod extends JComboBox {
863
                private static final long serialVersionUID = 5935267402200698145L;
864

865
                public JComboBoxLabelingMethod(MethodItem[] items) {
866
                        super(items);
867
                }
868

869
                @Override
870
                public void setSelectedItem(Object anObject) {
871
                        if (anObject instanceof MethodItem) {
872
                                MethodItem methodItem = (MethodItem) anObject;
873
                                for (int i = 0; i < getItemCount(); i++) {
874
                                        MethodItem aux = (MethodItem) getItemAt(i);
875
                                        if (aux.equals(methodItem)) {
876
                                                aux.method=methodItem.method;
877
                                        }
878
                                }
879
                                super.setSelectedItem(methodItem);
880
//                                ((MethodItem) super.getSelectedItem()).method = methodItem.method;
881
                        }
882
                }
883
        }
884
        private class MethodItem {
885
                private String name;
886
                private ILabelingMethod method;
887

888
                private MethodItem(String name, ILabelingMethod method) {
889
                        this.name = name;
890
                        this.method = method;
891
                }
892

893
                public String toString() {
894
                        return name;
895
                }
896

897
                @Override
898
                public boolean equals(Object obj) {
899
                        if (obj != null && obj instanceof MethodItem) {
900
                                MethodItem methodItem = (MethodItem) obj;
901
                                return methodItem.method.getClass().equals(this.method.getClass());
902
                        }
903
                        return false;
904
                }
905
        }
906

907
}*/