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 / PlacementProperties.java @ 41227

History | View | Annotate | Download (35.4 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: PlacementProperties.java 15674 2007-10-30 16:39:02Z jdominguez $
45
* $Log$
46
* Revision 1.7  2007-04-19 14:22:29  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.6  2007/04/13 12:43:08  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.5  2007/04/13 12:10:56  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.4  2007/04/12 16:01:32  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.3  2007/03/28 15:38:56  jaume
59
* GUI for lines, points and polygons
60
*
61
* Revision 1.2  2007/03/09 11:25:00  jaume
62
* Advanced symbology (start committing)
63
*
64
* Revision 1.1.2.2  2007/02/09 11:00:03  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.1  2007/02/01 12:12:41  jaume
68
* theme manager window and all its components are now dynamic
69
*
70
*
71
*/
72
package org.gvsig.labeling.gui.layerproperties;
73

    
74
import java.awt.BorderLayout;
75
import java.awt.Color;
76
import java.awt.Component;
77
import java.awt.Dimension;
78
import java.awt.FlowLayout;
79
import java.awt.Font;
80
import java.awt.FontMetrics;
81
import java.awt.Graphics;
82
import java.awt.Graphics2D;
83
import java.awt.GridLayout;
84
import java.awt.Rectangle;
85
import java.awt.event.ActionEvent;
86
import java.awt.event.ActionListener;
87
import java.awt.image.BufferedImage;
88
import java.io.File;
89
import java.net.URL;
90
import java.util.ArrayList;
91

    
92
import javax.swing.BorderFactory;
93
import javax.swing.BoxLayout;
94
import javax.swing.ButtonGroup;
95
import javax.swing.JButton;
96
import javax.swing.JCheckBox;
97
import javax.swing.JComboBox;
98
import javax.swing.JComponent;
99
import javax.swing.JLabel;
100
import javax.swing.JPanel;
101
import javax.swing.JRadioButton;
102
import javax.swing.SwingUtilities;
103

    
104
import org.cresques.cts.IProjection;
105
import org.gvsig.andami.ui.mdiManager.WindowInfo;
106
import org.gvsig.app.ApplicationLocator;
107
import org.gvsig.app.gui.styling.SingleStyleSelectorFilter;
108
import org.gvsig.app.gui.styling.StylePreviewer;
109
import org.gvsig.app.gui.styling.StyleSelector;
110
import org.gvsig.fmap.crs.CRSFactory;
111
import org.gvsig.fmap.dal.DALLocator;
112
import org.gvsig.fmap.dal.DataStoreParameters;
113
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
114
import org.gvsig.fmap.geom.Geometry.TYPES;
115
import org.gvsig.fmap.geom.GeometryException;
116
import org.gvsig.fmap.geom.GeometryLocator;
117
import org.gvsig.fmap.geom.primitive.Envelope;
118
import org.gvsig.fmap.geom.type.GeometryType;
119
import org.gvsig.fmap.mapcontext.MapContext;
120
import org.gvsig.fmap.mapcontext.MapContextLocator;
121
import org.gvsig.fmap.mapcontext.MapContextManager;
122
import org.gvsig.fmap.mapcontext.ViewPort;
123
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
124
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend;
125
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
126
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
127
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
128
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
129
import org.gvsig.gui.beans.AcceptCancelPanel;
130
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
131
import org.gvsig.gui.beans.swing.JBlank;
132
import org.gvsig.i18n.Messages;
133
import org.gvsig.labeling.label.GeneralLabelingStrategy;
134
import org.gvsig.labeling.lang.LabelClassUtils;
135
import org.gvsig.labeling.placements.MultiShapePlacementConstraints;
136
import org.gvsig.labeling.placements.PlacementManager;
137
import org.gvsig.labeling.placements.PointLabelPositioner;
138
import org.gvsig.labeling.placements.PointPlacementConstraints;
139
import org.gvsig.symbology.SymbologyLocator;
140
import org.gvsig.symbology.SymbologyManager;
141
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol;
142
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
143
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IArrowDecoratorStyle;
144
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle;
145
import org.slf4j.Logger;
146
import org.slf4j.LoggerFactory;
147

    
148
/**
149
 *
150
 * @author jaume dominguez faus - jaume.dominguez@iver.es
151
 *
152
 */
153
public class PlacementProperties extends JPanel implements
154
IPlacementProperties, ActionListener {
155
        
156
        private static final Logger logger =
157
                        LoggerFactory.getLogger(PlacementProperties.class);
158
        
159
        private static final long serialVersionUID = 1022470370547576765L;
160
        
161
        private PointLabelPositioner defaultPointStyle = new PointLabelPositioner(
162
                        new int[] { 2, 2, 1, 3, 2, 3, 3, 2        },
163
                        Messages.getText("prefer_top_right_all_allowed")
164
                        );
165

    
166
        public IPlacementConstraints constraints;
167

    
168
        private PointLabelPositioner pointStyle = null;
169
        private int shapeType;
170
        private JPanel pnlContent = null;
171
        private JPanel pnlCenter = null;
172
        private JPanel orientationPanel;
173
        private DuplicateLayersMode        duplicateLabelsMode;
174
        private JRadioButton rdBtnHorizontal;
175
        private JRadioButton rdBtnParallel;
176
        private JRadioButton rdBtnFollowingLine;
177
        private JRadioButton rdBtnPerpendicular;
178
        private JPanel positionPanel;
179
        private MiniMapContext preview;
180
        private ArrayList<String> linePositionItem = new ArrayList<String>();
181

    
182
        private JPanel polygonSettingsPanel;
183
        private JRadioButton rdBtnAlwaysHorizontal;
184
        private JRadioButton rdBtnAlwaysStraight;
185
        private GridBagLayoutPanel pointSettingsPanel;
186
        private JRadioButton rdBtnOffsetLabelHorizontally;
187
        private JRadioButton rdBtnOffsetLabelOnTopPoint;
188
        private StylePreviewer stylePreview;
189
        private JButton btnChangeLocation;
190
        private JPanel locationPanel;
191
        private JComboBox cmbLocationAlongLines;
192
        private JRadioButton chkBellow;
193
        private JRadioButton chkOnTheLine;
194
        private JRadioButton chkAbove;
195
        private JComboBox cmbOrientationSystem;
196
        private JLabelHTML lblPointPosDesc = new JLabelHTML(""); //pointStyle.getDescription());
197
        IPlacementConstraints oldConstraints;
198
        private JCheckBox chkFitInsidePolygon;
199

    
200

    
201
        /**
202
         * Constructs a new panel for PlacementProperties.
203
         *
204
         * @param constraints, if not null this parameters is used to fill the panel. If it is null
205
         * this PlacementProperties constructor creates a new default one that is ready to be used
206
         * for the shapetype passed as second parameter
207
         *
208
         * @param shapeType, defines the target shapetype of the IPlacementConstraints obtained;
209
         * @throws ReadDriverException
210
         */
211
        public PlacementProperties(IPlacementConstraints constraints, int shapeType) {
212
                initialize(constraints, shapeType, getPnlDuplicateLabels());
213
        }
214

    
215
        PlacementProperties(IPlacementConstraints costraints,
216
                        int shapeType, DuplicateLayersMode duplicatesMode) {
217

    
218
                initialize( costraints, shapeType, duplicatesMode);
219
                refreshComponents();
220
        }
221

    
222
        private void refreshComponents() {
223
                getChkOnTheLine().setSelected(constraints.isOnTheLine());
224
                getChkAbove().setSelected(constraints.isAboveTheLine());
225
                getChkBelow().setSelected(constraints.isBelowTheLine());
226

    
227
                getRdBtnHorizontal().setSelected(constraints.isHorizontal());
228
                getRdBtnParallel().setSelected(constraints.isParallel());
229
                getRdBtnFollowingLine().setSelected(constraints.isFollowingLine());
230
                getRdBtnPerpendicular().setSelected(constraints.isPerpendicular());
231

    
232
                getCmbOrientationSystem().setSelectedIndex(constraints.isPageOriented() ? 1 : 0);
233

    
234
                // points mode
235
                if (constraints.isOnTopOfThePoint()) {
236
                        getRdOffsetLabelOnTopPoint().setSelected(true);
237
                } else if (constraints.isAroundThePoint()) {
238
                        getRdOffsetLabelHorizontally().setSelected(true);
239
                }
240

    
241
                // lines mode
242
                if (constraints.isAtTheBeginingOfLine()) {
243
                        getCmbLocationAlongLines().setSelectedIndex(1);
244
                } else if (constraints.isInTheMiddleOfLine()) {
245
                        getCmbLocationAlongLines().setSelectedIndex(0);
246
                } else if (constraints.isAtTheEndOfLine()) {
247
                        getCmbLocationAlongLines().setSelectedIndex(2);
248
                } else if (constraints.isAtBestOfLine()) {
249
                        getCmbLocationAlongLines().setSelectedIndex(3);
250
                }
251

    
252
                // polygon mode
253
                getChkFitInsidePolygon().setSelected(constraints.isFitInsidePolygon());
254
                if (constraints.isHorizontal()) {
255
                        getRdBtnAlwaysHorizontal().setSelected(true);
256
                } else if (constraints.isParallel()) {
257
                        getRdBtnAlwaysStraight().setSelected(true);
258
                }
259

    
260
//                if(constraints.isFollowingLine()){
261
//                        setComponentEnabled(getPositionPanel(), false);
262
//                }
263

    
264

    
265
                // duplicates mode
266
                int dupMode = constraints.getDuplicateLabelsMode();
267
                duplicateLabelsMode.setMode(dupMode);
268
                applyToPreview();
269
        }
270

    
271
        private void initialize(
272
                        IPlacementConstraints constraints,
273
                        int shapeType, DuplicateLayersMode duplicatesMode) {
274
            
275
        this.duplicateLabelsMode = duplicatesMode;
276

    
277
                this.shapeType = shapeType;
278
                this.oldConstraints = constraints;
279
//                this.constraints = constraints != null ?
280
//                                PlacementManager.createPlacementConstraints(constraints.getXMLEntity())        :
281
//                                PlacementManager.createPlacementConstraints(shapeType);
282
                if( constraints != null ) {
283
                        
284
                        this.constraints = (IPlacementConstraints) LabelClassUtils.clone(constraints);
285
                        
286
                        if (constraints instanceof PointPlacementConstraints) {
287
                                PointLabelPositioner positioneer = ((PointPlacementConstraints)constraints).getPositioner();
288
                                if (positioneer != null) this.setPointStyle(positioneer);
289
                                this.lblPointPosDesc.setText(this.getPointStyle().getDescription());
290
                        }
291
                } else {
292
                        try {
293
                                this.constraints = PlacementManager.createPlacementConstraints(shapeType);
294
                        } catch (Exception e) {
295
                                logger.error("While initializing placement constraints.", e);
296
                                return;
297
                        }
298
                }
299
                
300
                /*
301
                 * We need to update this here because GUI events would
302
                 * remove the previous value
303
                 */
304
                this.getPnlDuplicateLabels().setMode(this.constraints.getDuplicateLabelsMode());
305

    
306
                linePositionItem.add(Messages.getText("in_the_middle"));
307
                linePositionItem.add(Messages.getText("at_begin"));
308
                linePositionItem.add(Messages.getText("at_end"));
309
                linePositionItem.add(Messages.getText("at_best"));
310

    
311
        this.setLayout(new BorderLayout());
312
        this.setSize(new Dimension(410,380));
313
        this.add(getPnlContent(), BorderLayout.CENTER);
314
         }
315

    
316
        public WindowInfo getWindowInfo() {
317
                WindowInfo viewInfo = new WindowInfo(
318
                                WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
319
                viewInfo.setWidth(440);
320
                viewInfo.setHeight(520);
321
                viewInfo.setTitle(Messages.getText("placement_properties"));
322
                return viewInfo;
323
        }
324

    
325
        public Object getWindowProfile() {
326
                return WindowInfo.DIALOG_PROFILE;
327
        }
328

    
329
        private JPanel getPnlContent() {
330
                if (pnlContent == null) {
331
                        pnlContent = new JPanel();
332
                        pnlContent.setLayout(new BorderLayout());
333
                        pnlContent.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
334
                        pnlContent.add(getPnlDuplicateLabels(), java.awt.BorderLayout.SOUTH);
335
                }
336
                return pnlContent;
337
        }
338

    
339
        private DuplicateLayersMode getPnlDuplicateLabels() {
340
                if (duplicateLabelsMode == null) {
341
                        duplicateLabelsMode = new DuplicateLayersMode(this);
342
                }
343
                return duplicateLabelsMode;
344
        }
345

    
346
        private JPanel getPnlCenter() {
347
                
348
                if (pnlCenter == null) {
349
                        pnlCenter = new JPanel();
350
                        
351
                        GeometryType gt = null;
352
                        try {
353
                                gt = GeometryLocator.getGeometryManager().getGeometryType(
354
                                                shapeType, SUBTYPES.GEOM2D);
355
                        } catch (Exception e) {
356
                                logger.error("While getting panel.", e);
357
                                return null;
358
                        }
359
                        
360
                        if (gt.isTypeOf(TYPES.POINT) || gt.isTypeOf(TYPES.MULTIPOINT)) {
361
                                pnlCenter.setBorder(BorderFactory.
362
                                                createTitledBorder(null,
363
                                                Messages.getText("point_settings")));
364
                                pnlCenter.add(getPointSettingsPanel());
365
                        } else {
366
                                if (gt.isTypeOf(TYPES.CURVE) || gt.isTypeOf(TYPES.MULTICURVE)) {
367
                                        pnlCenter.setLayout(new BorderLayout());
368
                                        pnlCenter.setBorder(BorderFactory.
369
                                                        createTitledBorder(null,
370
                                                                        Messages.getText("line_settings")));
371
                                        JPanel aux = new JPanel(
372
                                                        new GridLayout(1, 2));
373
                                        aux.add(getOrientationPanel());
374
                                        aux.add(getPositionPanel());
375
                                        pnlCenter.add(aux, BorderLayout.CENTER);
376
                                        pnlCenter.add(getLocationPanel(), BorderLayout.SOUTH);
377
                                } else {
378
                                        if (gt.isTypeOf(TYPES.SURFACE) || gt.isTypeOf(TYPES.MULTISURFACE)) {
379
                                                pnlCenter.setLayout(new BorderLayout());
380
                                                pnlCenter.setBorder(BorderFactory.
381
                                                                createTitledBorder(null,
382
                                                                                Messages.getText("polygon_settings")));
383
                                                pnlCenter.add(getPolygonSettingsPanel(), BorderLayout.CENTER);
384
                                        }
385
                                }
386
                        }
387
                }
388
                return pnlCenter;
389
        }
390

    
391
        private JPanel getLocationPanel() {
392
                if (locationPanel == null) {
393
                        locationPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
394
                        locationPanel.setBorder(BorderFactory.
395
                                        createTitledBorder(null,
396
                                                        Messages.getText("location")));
397
                        locationPanel.add(new JLabel(Messages.getText(
398
                                        "location_along_the_lines")+":"));
399
                        locationPanel.add(getCmbLocationAlongLines());
400
                }
401

    
402
                return locationPanel;
403
        }
404

    
405
        private JComboBox getCmbLocationAlongLines() {
406
                if (cmbLocationAlongLines == null) {
407
                        cmbLocationAlongLines = new JComboBox((String[]) linePositionItem.toArray(new String[linePositionItem.size()]));
408
                        cmbLocationAlongLines.addActionListener(this);
409
                }
410

    
411
                return cmbLocationAlongLines;
412
        }
413

    
414
        private PointLabelPositioner getPointStyle(){
415
                if (pointStyle == null){
416
                        pointStyle = defaultPointStyle;
417
                        lblPointPosDesc.setText(pointStyle.getDescription());
418
                }
419
                return pointStyle;
420
        }
421

    
422
        private void setPointStyle(PointLabelPositioner pointStyle){
423
                this.pointStyle = pointStyle;
424
        }
425

    
426
        private GridBagLayoutPanel getPointSettingsPanel() {
427
                if (pointSettingsPanel == null) {
428
                        pointSettingsPanel = new GridBagLayoutPanel();
429
                        pointSettingsPanel.addComponent(getRdOffsetLabelHorizontally());
430
                        JPanel aux = new JPanel();
431
                        aux.add(getStylePreviewer());
432

    
433
                        JPanel aux2 = new JPanel();
434
                        aux2.setLayout(new BoxLayout(aux2, BoxLayout.Y_AXIS));
435
                        aux2.add(lblPointPosDesc);
436
                        aux2.add(new JBlank(20, 5));
437
                        aux2.add(getBtnChangeLocation());
438

    
439
                        aux.add(aux2);
440
                        pointSettingsPanel.addComponent("", aux);
441
                        pointSettingsPanel.addComponent("",
442
                                        new JLabel(Messages.getText("label-point-priority-help")));
443
                        pointSettingsPanel.addComponent(getRdOffsetLabelOnTopPoint());
444
                        ButtonGroup group = new ButtonGroup();
445
                        group.add(getRdOffsetLabelHorizontally());
446
                        group.add(getRdOffsetLabelOnTopPoint());
447
                }
448

    
449
                return pointSettingsPanel;
450
        }
451

    
452

    
453

    
454
        private JButton getBtnChangeLocation() {
455
                if (btnChangeLocation == null) {
456
                        btnChangeLocation = new JButton(Messages.getText("change_location"));
457
                        btnChangeLocation.addActionListener(this);
458
                }
459

    
460
                return btnChangeLocation;
461
        }
462

    
463
        private StylePreviewer getStylePreviewer() {
464
                if (stylePreview == null) {
465
                        stylePreview = new StylePreviewer();
466
                        stylePreview.setStyle(getPointStyle());
467
                        stylePreview.setPreferredSize(new Dimension(80, 80));
468
                }
469
                return stylePreview;
470
        }
471

    
472
        private JRadioButton getRdOffsetLabelOnTopPoint() {
473
                if (rdBtnOffsetLabelOnTopPoint == null) {
474
                        rdBtnOffsetLabelOnTopPoint = new JRadioButton(
475
                                        Messages.getText("offset_labels_on_top_of_the_points")) ;
476

    
477
                }
478

    
479
                return rdBtnOffsetLabelOnTopPoint;
480
        }
481

    
482
        private JRadioButton getRdOffsetLabelHorizontally() {
483
                if (rdBtnOffsetLabelHorizontally == null) {
484
                        rdBtnOffsetLabelHorizontally = new JRadioButton(
485
                                        Messages.getText("offset_labels_horizontally"));
486

    
487
                }
488

    
489
                return rdBtnOffsetLabelHorizontally;
490
        }
491

    
492
        private JPanel getPolygonSettingsPanel() {
493
                if (polygonSettingsPanel == null) {
494
                        polygonSettingsPanel = new JPanel(new BorderLayout(10, 10));
495
                        JPanel aux = new JPanel();
496
                        aux.setLayout(new BoxLayout(aux, BoxLayout.Y_AXIS));
497
                        aux.add(new JBlank(10,10));
498
                        aux.add(getRdBtnAlwaysHorizontal());
499
                        aux.add(new JBlank(10,10));
500
                        aux.add(getRdBtnAlwaysStraight());
501
                        aux.add(new JBlank(10,50));
502

    
503
                        polygonSettingsPanel.add(getPreview(), BorderLayout.CENTER);
504
                        polygonSettingsPanel.add(aux, BorderLayout.EAST);
505
                        ButtonGroup group = new ButtonGroup();
506
                        group.add(getRdBtnAlwaysHorizontal());
507
                        group.add(getRdBtnAlwaysStraight());
508
                        polygonSettingsPanel.add(getChkFitInsidePolygon(), BorderLayout.SOUTH);
509
                }
510

    
511
                return polygonSettingsPanel;
512
        }
513

    
514
        private JCheckBox getChkFitInsidePolygon() {
515
                if (chkFitInsidePolygon == null) {
516
                        chkFitInsidePolygon = new JCheckBox(
517
                                        Messages.getText("fit_inside_polygon"));
518
                        chkFitInsidePolygon.addActionListener(this);
519

    
520
                }
521

    
522
                return chkFitInsidePolygon;
523
        }
524

    
525
        private JRadioButton getRdBtnAlwaysStraight() {
526
                if (rdBtnAlwaysStraight == null) {
527
                        rdBtnAlwaysStraight = new JRadioButton(
528
                                        Messages.getText("always_straight"));
529
                        rdBtnAlwaysStraight.addActionListener(this);
530
                }
531

    
532
                return rdBtnAlwaysStraight;
533
        }
534

    
535
        private JRadioButton getRdBtnAlwaysHorizontal() {
536
                if (rdBtnAlwaysHorizontal == null) {
537
                        rdBtnAlwaysHorizontal = new JRadioButton(
538
                                        Messages.getText("always_horizontal"));
539
                        rdBtnAlwaysHorizontal.addActionListener(this);
540
                }
541

    
542
                return rdBtnAlwaysHorizontal;
543
        }
544

    
545

    
546

    
547
        private JPanel getPositionPanel() {
548
                if (positionPanel == null) {
549
                        positionPanel = new JPanel(new BorderLayout());
550
                        positionPanel.setBorder(BorderFactory.
551
                                        createTitledBorder(null,
552
                                                        Messages.getText("position")));
553
                        GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
554
                        JPanel aux = new JPanel(new GridLayout(4, 1));
555

    
556
                        aux.add(getChkAbove());
557
                        getChkAbove().setSelected(true);
558
                        aux.add(getChkOnTheLine());
559
                        aux.add(getChkBelow());
560

    
561
                        ButtonGroup group = new ButtonGroup();
562
                        group.add(getChkAbove());
563
                        group.add(getChkOnTheLine());
564
                        group.add(getChkBelow());
565

    
566
                        aux2.addComponent(aux);
567
                        aux2.addComponent(
568
                                        Messages.getText("orientation_system"), getCmbOrientationSystem());
569
                        positionPanel.add(getPreview(), BorderLayout.CENTER);
570
                        positionPanel.add(aux2, BorderLayout.SOUTH);
571
                }
572
                return positionPanel;
573
        }
574

    
575
        private Component getPreview() {
576
                 if (preview == null) {
577
                         preview = new MiniMapContext(shapeType);
578
                 }
579
                 return preview;
580
        }
581

    
582
        private JComboBox getCmbOrientationSystem() {
583
                if (cmbOrientationSystem == null) {
584
                        cmbOrientationSystem = new JComboBox(new String[] {
585
                                        Messages.getText("line"),
586
                                        Messages.getText("page")
587
                        });
588
                        cmbOrientationSystem.setSelectedIndex(1);
589
                        cmbOrientationSystem.addActionListener(this);
590
                }
591

    
592
                return cmbOrientationSystem;
593
        }
594

    
595
        private JRadioButton getChkBelow() {
596
                if (chkBellow == null) {
597
                        chkBellow = new JRadioButton(Messages.getText("below"));
598
                        chkBellow.addActionListener(this);
599
                }
600
                return chkBellow;
601
        }
602

    
603
        private JRadioButton getChkOnTheLine() {
604
                if (chkOnTheLine == null) {
605
                        chkOnTheLine = new JRadioButton(
606
                                        Messages.getText("on_the_line"));
607
                        chkOnTheLine.addActionListener(this);
608
                }
609
                return chkOnTheLine;
610
        }
611

    
612
        private JRadioButton getChkAbove() {
613
                if (chkAbove == null) {
614
                        chkAbove = new JRadioButton(Messages.getText("above"));
615
                        chkAbove.addActionListener(this);
616
                }
617
                return chkAbove;
618
        }
619

    
620
        private JPanel getOrientationPanel() {
621
                if (orientationPanel == null) {
622
                    
623
                    orientationPanel = new JPanel(new BorderLayout());
624
            orientationPanel.setBorder(BorderFactory.
625
                createTitledBorder(null,
626
                        Messages.getText("orientation")));
627
                    
628
            GridBagLayoutPanel auxp = new GridBagLayoutPanel();
629
            auxp.addComponent(getRdBtnHorizontal());
630
            auxp.addComponent(getRdBtnParallel());
631
            auxp.addComponent(getRdBtnFollowingLine());
632
            auxp.addComponent(getRdBtnPerpendicular());
633
            
634
            orientationPanel.add(auxp, BorderLayout.NORTH);
635
            
636
                        ButtonGroup group = new ButtonGroup();
637
                        group.add(getRdBtnHorizontal());
638
                        group.add(getRdBtnParallel());
639
                        group.add(getRdBtnFollowingLine());
640
                        group.add(getRdBtnPerpendicular());
641
                }
642
                return orientationPanel;
643
        }
644

    
645
        private JRadioButton getRdBtnParallel() {
646
                if (rdBtnParallel == null) {
647
                        rdBtnParallel = new JRadioButton(
648
                                        Messages.getText("parallel"));
649

    
650
                        rdBtnParallel.addActionListener(this);
651
                }
652
                return rdBtnParallel;
653
        }
654

    
655
        private JRadioButton getRdBtnFollowingLine() {
656
                if (rdBtnFollowingLine == null) {
657
                        rdBtnFollowingLine = new JRadioButton(
658
                                        Messages.getText("following_line"));
659
                        rdBtnFollowingLine.addActionListener(this);
660
                }
661
                return rdBtnFollowingLine;
662
        }
663

    
664
        private JRadioButton getRdBtnPerpendicular() {
665
                if (rdBtnPerpendicular == null) {
666
                        rdBtnPerpendicular = new JRadioButton(
667
                                        Messages.getText("perpedicular"));
668
                        rdBtnPerpendicular.addActionListener(this);
669
                }
670
                return rdBtnPerpendicular;
671
        }
672

    
673
        private JRadioButton getRdBtnHorizontal() {
674
                if (rdBtnHorizontal == null) {
675
                        rdBtnHorizontal = new JRadioButton(
676
                                        Messages.getText("horizontal"));
677
                        rdBtnHorizontal.addActionListener(this);
678
                }
679
                return rdBtnHorizontal;
680
        }
681

    
682

    
683

    
684

    
685
        public void applyConstraints() throws GeometryException {
686
                int mode=0;
687
                GeometryType gt = null;
688
                gt = GeometryLocator.getGeometryManager().getGeometryType(
689
                                shapeType, SUBTYPES.GEOM2D);
690
                
691
                if (gt.isTypeOf(TYPES.POINT) || gt.isTypeOf(TYPES.MULTIPOINT)) {
692
                        if (getRdOffsetLabelOnTopPoint().isSelected()) {
693
                                mode = IPlacementConstraints.ON_TOP_OF_THE_POINT;
694
                        } else if (getRdOffsetLabelHorizontally().isSelected()) {
695
                                mode = IPlacementConstraints.OFFSET_HORIZONTALY_AROUND_THE_POINT;
696
                        }
697
                        ((PointPlacementConstraints) constraints).
698
                                setPositioner((PointLabelPositioner) stylePreview.getStyle());
699
                } else {
700
                        if (gt.isTypeOf(TYPES.CURVE) || gt.isTypeOf(TYPES.MULTICURVE)) {
701
                                if (getRdBtnFollowingLine().isSelected()) {
702
                                        mode = IPlacementConstraints.FOLLOWING_LINE;
703
                                } else if (getRdBtnParallel().isSelected()) {
704
                                        mode = IPlacementConstraints.PARALLEL;
705
                                } else if (getRdBtnPerpendicular().isSelected()) {
706
                                        mode = IPlacementConstraints.PERPENDICULAR;
707
                                } else {
708
                                        mode = IPlacementConstraints.HORIZONTAL;
709
                                }
710

    
711
                                constraints.setAboveTheLine(getChkAbove().isSelected());
712
                                constraints.setBelowTheLine(getChkBelow().isSelected());
713
                                constraints.setOnTheLine(getChkOnTheLine().isSelected());
714

    
715
                                constraints.setPageOriented(
716
                                                getCmbOrientationSystem().getSelectedIndex() == 1);
717
                                int i = getCmbLocationAlongLines().getSelectedIndex();
718
                                if (i == 0) {
719
                                        i = IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE;
720
                                } else if (i == 1) {
721
                                        i = IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE;
722
                                } else if (i == 2) {
723
                                        i = IPlacementConstraints.AT_THE_END_OF_THE_LINE;
724
                                } else if (i == 3) {
725
                                        i = IPlacementConstraints.AT_BEST_OF_LINE;
726
                                }
727
                                constraints.setLocationAlongTheLine(i);
728
                        } else {
729
                                if (gt.isTypeOf(TYPES.SURFACE) || gt.isTypeOf(TYPES.MULTISURFACE)) {
730
                                        mode = IPlacementConstraints.HORIZONTAL;
731
                                        if (getRdBtnAlwaysHorizontal().isSelected()) {
732
                                                mode = IPlacementConstraints.HORIZONTAL;
733
                                        } else if (getRdBtnAlwaysStraight().isSelected()) {
734
                                                mode = IPlacementConstraints.PARALLEL;
735
                                        }
736

    
737
                                        constraints.setFitInsidePolygon(getChkFitInsidePolygon().isSelected());
738
                                }
739
                        }
740
                }                
741
                
742
                constraints.setPlacementMode(mode);
743
                constraints.setDuplicateLabelsMode(duplicateLabelsMode.getMode());
744
        }
745

    
746
        private void setComponentEnabled(Component c, boolean b) {
747
                if (c instanceof JComponent) {
748
                        JComponent c1 = (JComponent) c;
749
                        for (int i = 0; i < c1.getComponentCount(); i++) {
750
                                setComponentEnabled(c1.getComponent(i), b);
751
                        }
752
                }
753
                c.setEnabled(b);
754
        }
755

    
756

    
757
        public IPlacementConstraints getPlacementConstraints() {
758
                return constraints;
759
        }
760

    
761
        public void actionPerformed(ActionEvent e) {
762
                JComponent c = (JComponent) e.getSource();
763
                boolean okPressed = "OK".equals(e.getActionCommand());
764
                boolean cancelPressed = "CANCEL".equals(e.getActionCommand());
765
                if (okPressed || cancelPressed) {
766
                        if (okPressed) {
767
                                try {
768
                                        applyConstraints();
769
                                } catch (GeometryException e1) {
770
                                        logger.error("While applying constraints.", e1);
771
                                }
772
                        }
773

    
774
                        if ("CANCEL".equals(e.getActionCommand()))
775
                                constraints = oldConstraints;
776
                        
777
                        ApplicationLocator.getManager().getUIManager().closeWindow(this);
778

    
779
                        return;
780
                } else if (c.equals(rdBtnAlwaysHorizontal)
781
                                || c.equals(rdBtnAlwaysStraight) ) {
782
                        
783
                        
784
                } else if (c.equals(rdBtnHorizontal)) {
785
                        // lock position panel and location panel
786
                        setComponentEnabled(getPositionPanel(), false);
787
                        setComponentEnabled(getLocationPanel(), false);
788
                } else if (c.equals(rdBtnParallel) || c.equals(rdBtnPerpendicular)) {
789
                        // unlock position panel and location panel but keep orientation system locked
790
                        setComponentEnabled(getLocationPanel(), true);
791
                        setComponentEnabled(getPositionPanel(), true);
792
                        getCmbOrientationSystem().setEnabled(true);
793
                } else if (c.equals(rdBtnFollowingLine)) {
794
                        setComponentEnabled(getLocationPanel(), true);
795
                        setComponentEnabled(getPositionPanel(), true);
796
                        getCmbOrientationSystem().setSelectedItem(Messages.getText("line"));
797
                        getCmbOrientationSystem().setEnabled(false);
798
                } else if (c.equals(btnChangeLocation)) {
799
                        StyleSelector stySel = new StyleSelector(
800
                                getPointStyle(),
801
                                TYPES.POINT,
802
                                new SingleStyleSelectorFilter(PointLabelPositioner.class));
803
                        
804
                        ApplicationLocator.getManager().getUIManager().addWindow(stySel);
805
                        
806
                        IStyle sty = (IStyle) stySel.getSelectedObject();
807
                        if (sty != null) {
808
                                stylePreview.setStyle(sty);
809
                                lblPointPosDesc.setText(sty.getDescription());
810
                        }
811
                } else if (c == this.getChkFitInsidePolygon()) {
812
                        
813
                } else if (c == getPnlDuplicateLabels()) {
814
                        
815
                }
816
                
817
                applyToPreview();
818
                
819
                CompRefreshThread st = new CompRefreshThread(getPnlContent(), 500);
820
        st.start();
821

    
822
        }
823

    
824
        private void applyToPreview() {
825
                if (preview != null) {
826
                        try {
827
                                applyConstraints();
828
                        } catch (GeometryException e) {
829
                                logger.error("While applying constraints.", e);
830
                        }
831
                        preview.setConstraints(constraints);
832
                }
833
        }
834

    
835
        private class JLabelHTML extends JLabel {
836
                private static final long serialVersionUID = -5031405572546951108L;
837

    
838
                public JLabelHTML(String text) {
839
                        super(text);
840
                        setPreferredSize(new Dimension(250, 60));
841
                }
842

    
843
                @Override
844
                public void setText(String text) {
845
                        // silly fix to avoid too large text lines
846
                        super.setText("<html>"+text+"</html>");
847
                }
848
        }
849

    
850
        public static IPlacementProperties createPlacementProperties(
851
                        IPlacementConstraints placementConstraints, int shapeType) {
852
                return createPlacementProperties(placementConstraints, shapeType, null);
853
        }
854

    
855
        protected static IPlacementProperties createPlacementProperties(
856
                        IPlacementConstraints placementConstraints, int geotype,
857
                        DuplicateLayersMode duplicatesMode) {
858
                
859
                IPlacementProperties pp = null;
860
                if (geotype == TYPES.GEOMETRY || geotype == TYPES.AGGREGATE) {
861
                        try {
862
                                pp = new MultiShapePlacementProperties(
863
                                                (MultiShapePlacementConstraints) placementConstraints);
864
                        } catch (Exception e) {
865
                                logger.error("While creating MultiShapePlacementProperties", e);
866
                        }
867
                } else {
868
                        pp = new PlacementProperties(placementConstraints, geotype, null);
869
                }
870
                ((JPanel) pp).add(new AcceptCancelPanel(pp, pp), BorderLayout.SOUTH);
871
                return pp;
872
        }
873
        
874
    class CompRefreshThread extends Thread {
875
        
876
        private int waitt = 0;
877
        private Component compo = null;
878
        
879
        public CompRefreshThread(Component co, int t) {
880
            compo = co;
881
            waitt = t;
882
        }
883
        public void run() {
884
            
885
            try {
886
                /*
887
                 * We might need to wait a bit because the GUI thread needs
888
                 * some time to update (this is because we have used a modal dialog
889
                 * and we refresh after that dialog is closed)
890
                 */
891
                Thread.sleep(waitt);
892
            } catch (Exception exc) {
893
                
894
            }
895
            
896
            SwingUtilities.invokeLater(new Runnable() {
897
                public void run() {
898
                    try {
899
                        compo.repaint(); // .invalidate();
900
                    } catch (Exception exc) {
901
                        // logger.info("Error while refreshing components.", exc);
902
                    }
903
                }
904
            });
905
        }
906
    }
907
    
908
        
909
}  //  @jve:decl-index=0:visual-constraint="10,10"
910

    
911
class MiniMapContext extends JComponent {
912
        
913
        private static final Logger minilogger =
914
                        LoggerFactory.getLogger(MiniMapContext.class);
915
        
916
        private static final long serialVersionUID = 229128782038834443L;
917
        private MapContext theMapContext;
918
        private FLyrVect line_layer;
919
        private FLyrVect bgpoly_layer;
920
        private int hMargin = 5, vMargin = 5;
921
        private FLyrVect poly_layer;
922
        
923
        private int geotype;
924
        private IPlacementConstraints placement;
925

    
926

    
927
        public MiniMapContext(int type) {
928
                setType(type);
929
        }
930

    
931
        public void setType(int type) {
932
                
933
                GeometryType gtype = null;
934
                try {
935
                        gtype = GeometryLocator.getGeometryManager().getGeometryType(
936
                                        type, SUBTYPES.GEOM2D);
937
                } catch (Exception e) {
938
                        minilogger.error("While getting geo type", e);
939
                }
940
                
941
                if (gtype.isTypeOf(TYPES.CURVE)
942
                                || gtype.getType() == TYPES.MULTICURVE) {
943
                        this.geotype = TYPES.CURVE;
944
                } else {
945
                        if (gtype.isTypeOf(TYPES.SURFACE)
946
                                        || gtype.getType() == TYPES.MULTISURFACE) {
947
                                this.geotype = TYPES.SURFACE;
948
                        } else {
949
                                // Should not get here
950
                                this.geotype = TYPES.GEOMETRY;
951
                        }
952
                }
953
                
954
        }
955

    
956
        public void setConstraints(IPlacementConstraints constraints) {
957
                placement = constraints;
958
                try {
959
                        getMapContext(null);
960
                } catch (Exception e) {
961
                        minilogger.error("While getting mini map context", e);
962
                }
963
                repaint();
964
        }
965

    
966
        private MapContext getMapContext(Dimension img_size) throws Exception {
967
                
968
                Envelope ini_env = null;
969
                ini_env = GeometryLocator.getGeometryManager().createEnvelope(
970
                                289600, 3973500, 289600 + 2000, 3973700 + 2000,
971
                                SUBTYPES.GEOM2D);
972

    
973
                
974
                if (theMapContext == null) {
975
                        
976
                        SymbologyManager symman = SymbologyLocator.getSymbologyManager();
977
                        MapContextManager mcoman = MapContextLocator.getMapContextManager();
978
                        
979
                        line_layer = this.createLayerFromShpDocFile("line.shp", "EPSG:23030");
980
                        ISimpleLineSymbol line_sym = symman.createSimpleLineSymbol();
981
                        line_sym.setColor(Color.red);
982
                        
983
                        ISimpleLineStyle line_sty = symman.createSimpleLineStyle();
984
                        IArrowDecoratorStyle arrow_style = symman.createArrowDecoratorStyle();
985
                        
986
                        arrow_style.getMarker().setSize(15);
987
                        arrow_style.setArrowMarkerCount(1);
988
                        arrow_style.getMarker().setColor(Color.red);
989
                        line_sty.setArrowDecorator(arrow_style);
990

    
991
                        line_sym.setLineStyle(line_sty);
992
                        line_sym.setLineWidth(2);
993
                        
994
                        ISingleSymbolLegend line_leg = (ISingleSymbolLegend) 
995
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
996
                        
997
                        line_leg.setDefaultSymbol(line_sym);
998
                        line_layer.setLegend(line_leg);
999
                        // =====================================================
1000
                        
1001
                        bgpoly_layer = this.createLayerFromShpDocFile("bg-polygon.shp", "EPSG:23030");
1002
                        poly_layer = this.createLayerFromShpDocFile("polygon.shp", "EPSG:23030");
1003

    
1004
                        ISimpleFillSymbol sym2 = symman.createSimpleFillSymbol();
1005
                        sym2.setFillColor(new Color(50, 245, 125));
1006
                        ISimpleLineSymbol outline = symman.createSimpleLineSymbol();
1007
                        outline.setLineColor(Color.DARK_GRAY);
1008
                        outline.setLineWidth(0.5);
1009
                        sym2.setOutline(outline);
1010

    
1011
                        ISingleSymbolLegend poly_leg = (ISingleSymbolLegend) 
1012
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
1013
                        poly_leg.setDefaultSymbol(sym2);
1014
                        poly_layer.setLegend(poly_leg);
1015
                        bgpoly_layer.setLegend(poly_leg);
1016

    
1017
                        GeneralLabelingStrategy labeling1 = new GeneralLabelingStrategy();
1018
                        GeneralLabelingStrategy labeling2 = new GeneralLabelingStrategy();
1019
                        ILabelingMethod def_method = symman.createDefaultLabelingMethod();
1020

    
1021
                        ILabelClass lc = null;
1022
                        if (def_method.getLabelClasses() != null && def_method.getLabelClasses().length > 0) {
1023
                                lc = def_method.getLabelClasses()[0];
1024
                        } else {
1025
                                lc = symman.createDefaultLabel();
1026
                                def_method.addLabelClass(lc);
1027
                        }
1028

    
1029
                        String[] sampleExpression = { Messages.getText("text") };
1030
                        lc.setLabelExpressions(sampleExpression);
1031

    
1032
                        lc.getTextSymbol().setFontSize(16);
1033

    
1034
                        labeling1.setLabelingMethod(def_method);
1035
                        labeling2.setLabelingMethod(def_method);
1036
                        labeling1.setLayer(line_layer);
1037
                        labeling2.setLayer(poly_layer);
1038

    
1039
                        line_layer.setLabelingStrategy(labeling1);
1040
                        line_layer.setIsLabeled(true);
1041
                        poly_layer.setLabelingStrategy(labeling2);
1042
                        poly_layer.setIsLabeled(true);
1043
                        
1044
                        ViewPort theViewPort = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
1045
                        theMapContext = new MapContext(theViewPort);
1046
                        
1047
                        if (img_size != null) {
1048
                                theViewPort.setImageSize(img_size);
1049
                        } else {
1050
                                theViewPort.setImageSize(new Dimension(200, 200));
1051
                        }
1052
                        theViewPort.setEnvelope(ini_env);
1053
                        
1054
                        theMapContext.getLayers().addLayer(bgpoly_layer);
1055
                        theMapContext.getLayers().addLayer(poly_layer);
1056
                        theMapContext.getLayers().addLayer(line_layer);
1057
                } else {
1058
                        
1059
                        if (img_size != null) {
1060
                                theMapContext.getViewPort().setImageSize(img_size);
1061
                        } else {
1062
                                theMapContext.getViewPort().setImageSize(new Dimension(200, 200));
1063
                        }
1064
                        theMapContext.getViewPort().setEnvelope(ini_env);
1065
                }
1066

    
1067
                line_layer.getLabelingStrategy().setPlacementConstraints(placement);
1068
                poly_layer.getLabelingStrategy().setPlacementConstraints(placement);
1069
                Dimension sz = getBounds().getSize();
1070
                sz.setSize(sz.width-2*hMargin, sz.height-2*vMargin);
1071
                theMapContext.getViewPort().setImageSize(sz);
1072
                theMapContext.getViewPort().setBackColor(new Color(255, 0, 0));
1073
                theMapContext.invalidate();
1074
                return theMapContext;
1075
        }
1076

    
1077
        @Override
1078
        protected void paintComponent(Graphics g) {
1079
                try {
1080
                        Rectangle bounds = getBounds();
1081
                        Dimension sz = bounds.getSize();
1082
                        sz.setSize(sz.width-2*vMargin, sz.height-2*hMargin);
1083
                        Dimension imageSize = sz;
1084
                        
1085
                        MapContext mco = getMapContext(imageSize);
1086
                        if (this.geotype == TYPES.CURVE) {
1087
                                line_layer.setVisible(true);
1088
                                poly_layer.setVisible(false);
1089
                        } else {
1090
                                if (this.geotype == TYPES.SURFACE) {
1091
                                        line_layer.setVisible(false);
1092
                                        poly_layer.setVisible(true);
1093
                                }
1094
                        }
1095
                        bgpoly_layer.setVisible(line_layer.isVisible());
1096

    
1097
                        BufferedImage bi = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_4BYTE_ABGR);
1098
                        mco.invalidate();
1099
                        mco.draw(bi, bi.createGraphics(), mco.getScaleView());
1100
                        g.setColor(new Color(150,180,255));
1101
                        g.fillRect(vMargin, hMargin, bounds.width-2*hMargin, bounds.height-2*vMargin);
1102
                        g.drawImage(bi, vMargin, vMargin, null);
1103
                        bi = null;
1104
                } catch (Exception e) {
1105
                        
1106
                        minilogger.error("While painting.", e);
1107
                        String noneSelected = "["+Messages.getText("preview_not_available")+"]";
1108
                        int vGap = 5, hGap = 5;
1109
                        Rectangle r = getBounds();
1110
                        FontMetrics fm = g.getFontMetrics();
1111
                        int lineWidth = fm.stringWidth(noneSelected);
1112
                        float scale = (float) r.getWidth() / lineWidth;
1113
                        Font f = g.getFont();
1114
                        float fontSize = f.getSize()*scale;
1115
                        g.setFont(        f.deriveFont( fontSize ) );
1116
                        ((Graphics2D) g).drawString(noneSelected, (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
1117
                }
1118

    
1119
        }
1120
        
1121
        private FLyrVect createLayerFromShpDocFile(
1122
                        String shp_file_name, String epsg_code) throws Exception {
1123
                
1124
                File shpfile = getDocFolderFile(shp_file_name);
1125
                IProjection proj = CRSFactory.getCRS(epsg_code);
1126
                DataStoreParameters ssp = DALLocator.getDataManager().createStoreParameters("Shape");
1127
                ssp.setDynValue("shpfile", shpfile);
1128
                ssp.setDynValue("CRS", proj);
1129
                FLyrVect resp = null;
1130
                resp = (FLyrVect) MapContextLocator.getMapContextManager().createLayer(
1131
                                shp_file_name, ssp);
1132
                return resp;
1133
        }
1134
        
1135
        private File getDocFolderFile(String name) {
1136
                
1137
                URL resource = this.getClass().getClassLoader().getResource(
1138
                                "docs" + File.separator + name);
1139
                File resp = new File(resource.getFile());
1140
                return resp;
1141
        }
1142
    
1143
}