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

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

    
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
        private boolean initialized;
201

    
202

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

    
218
        PlacementProperties(IPlacementConstraints costraints,
219
                        int shapeType, DuplicateLayersMode duplicatesMode) {
220

    
221
            this.initialized = false;
222
                initialize( costraints, shapeType, duplicatesMode);
223
                refreshComponents();
224
            this.initialized = true;
225
                
226
        }
227

    
228
        private void refreshComponents() {
229
                getChkOnTheLine().setSelected(constraints.isOnTheLine());
230
                getChkAbove().setSelected(constraints.isAboveTheLine());
231
                getChkBelow().setSelected(constraints.isBelowTheLine());
232

    
233
                getRdBtnHorizontal().setSelected(constraints.isHorizontal());
234
                getRdBtnParallel().setSelected(constraints.isParallel());
235
                getRdBtnFollowingLine().setSelected(constraints.isFollowingLine());
236
                getRdBtnPerpendicular().setSelected(constraints.isPerpendicular());
237

    
238
                getCmbOrientationSystem().setSelectedIndex(constraints.isPageOriented() ? 1 : 0);
239

    
240
                // points mode
241
                if (constraints.isOnTopOfThePoint()) {
242
                        getRdOffsetLabelOnTopPoint().setSelected(true);
243
                } else if (constraints.isAroundThePoint()) {
244
                        getRdOffsetLabelHorizontally().setSelected(true);
245
                }
246

    
247
                // lines mode
248
                if (constraints.isAtTheBeginingOfLine()) {
249
                        getCmbLocationAlongLines().setSelectedIndex(1);
250
                } else if (constraints.isInTheMiddleOfLine()) {
251
                        getCmbLocationAlongLines().setSelectedIndex(0);
252
                } else if (constraints.isAtTheEndOfLine()) {
253
                        getCmbLocationAlongLines().setSelectedIndex(2);
254
                } else if (constraints.isAtBestOfLine()) {
255
                        getCmbLocationAlongLines().setSelectedIndex(3);
256
                }
257

    
258
                // polygon mode
259
                getChkFitInsidePolygon().setSelected(constraints.isFitInsidePolygon());
260
                if (constraints.isHorizontal()) {
261
                        getRdBtnAlwaysHorizontal().setSelected(true);
262
                } else if (constraints.isParallel()) {
263
                        getRdBtnAlwaysStraight().setSelected(true);
264
                }
265

    
266
//                if(constraints.isFollowingLine()){
267
//                        setComponentEnabled(getPositionPanel(), false);
268
//                }
269

    
270

    
271
                // duplicates mode
272
                int dupMode = constraints.getDuplicateLabelsMode();
273
                duplicateLabelsMode.setMode(dupMode);
274
                
275
                getPreview().setConstraints(constraints);
276
                
277
//                applyToPreview();
278
        }
279

    
280
        private void initialize(
281
                        IPlacementConstraints constraints,
282
                        int shapeType, DuplicateLayersMode duplicatesMode) {
283
            
284
        this.duplicateLabelsMode = duplicatesMode;
285

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

    
315
                linePositionItem.add(Messages.getText("in_the_middle"));
316
                linePositionItem.add(Messages.getText("at_begin"));
317
                linePositionItem.add(Messages.getText("at_end"));
318
                linePositionItem.add(Messages.getText("at_best"));
319

    
320
        this.setLayout(new BorderLayout());
321
        this.setSize(new Dimension(410,380));
322
        this.add(getPnlContent(), BorderLayout.CENTER);
323
         }
324

    
325
        public WindowInfo getWindowInfo() {
326
                WindowInfo viewInfo = new WindowInfo(
327
                                WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
328
                viewInfo.setWidth(440);
329
                viewInfo.setHeight(520);
330
                viewInfo.setTitle(Messages.getText("placement_properties"));
331
                return viewInfo;
332
        }
333

    
334
        public Object getWindowProfile() {
335
                return WindowInfo.DIALOG_PROFILE;
336
        }
337

    
338
        private JPanel getPnlContent() {
339
                if (pnlContent == null) {
340
                        pnlContent = new JPanel();
341
                        pnlContent.setLayout(new BorderLayout());
342
                        pnlContent.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
343
                        pnlContent.add(getPnlDuplicateLabels(), java.awt.BorderLayout.SOUTH);
344
                }
345
                return pnlContent;
346
        }
347

    
348
        private DuplicateLayersMode getPnlDuplicateLabels() {
349
                if (duplicateLabelsMode == null) {
350
                        duplicateLabelsMode = new DuplicateLayersMode(this);
351
                }
352
                return duplicateLabelsMode;
353
        }
354

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

    
400
        private JPanel getLocationPanel() {
401
                if (locationPanel == null) {
402
                        locationPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
403
                        locationPanel.setBorder(BorderFactory.
404
                                        createTitledBorder(null,
405
                                                        Messages.getText("location")));
406
                        locationPanel.add(new JLabel(Messages.getText(
407
                                        "location_along_the_lines")+":"));
408
                        locationPanel.add(getCmbLocationAlongLines());
409
                }
410

    
411
                return locationPanel;
412
        }
413

    
414
        private JComboBox getCmbLocationAlongLines() {
415
                if (cmbLocationAlongLines == null) {
416
                        cmbLocationAlongLines = new JComboBox((String[]) linePositionItem.toArray(new String[linePositionItem.size()]));
417
                        cmbLocationAlongLines.addActionListener(this);
418
                }
419

    
420
                return cmbLocationAlongLines;
421
        }
422

    
423
        private PointLabelPositioner getPointStyle(){
424
                if (pointStyle == null){
425
                        pointStyle = defaultPointStyle;
426
                        lblPointPosDesc.setText(pointStyle.getDescription());
427
                }
428
                return pointStyle;
429
        }
430

    
431
        private void setPointStyle(PointLabelPositioner pointStyle){
432
                this.pointStyle = pointStyle;
433
        }
434

    
435
        private GridBagLayoutPanel getPointSettingsPanel() {
436
                if (pointSettingsPanel == null) {
437
                        pointSettingsPanel = new GridBagLayoutPanel();
438
                        pointSettingsPanel.addComponent(getRdOffsetLabelHorizontally());
439
                        JPanel aux = new JPanel();
440
                        aux.add(getStylePreviewer());
441

    
442
                        JPanel aux2 = new JPanel();
443
                        aux2.setLayout(new BoxLayout(aux2, BoxLayout.Y_AXIS));
444
                        aux2.add(lblPointPosDesc);
445
                        aux2.add(new JBlank(20, 5));
446
                        aux2.add(getBtnChangeLocation());
447

    
448
                        aux.add(aux2);
449
                        pointSettingsPanel.addComponent("", aux);
450
                        pointSettingsPanel.addComponent("",
451
                                        new JLabel(Messages.getText("label-point-priority-help")));
452
                        pointSettingsPanel.addComponent(getRdOffsetLabelOnTopPoint());
453
                        ButtonGroup group = new ButtonGroup();
454
                        group.add(getRdOffsetLabelHorizontally());
455
                        group.add(getRdOffsetLabelOnTopPoint());
456
                }
457

    
458
                return pointSettingsPanel;
459
        }
460

    
461

    
462

    
463
        private JButton getBtnChangeLocation() {
464
                if (btnChangeLocation == null) {
465
                        btnChangeLocation = new JButton(Messages.getText("change_location"));
466
                        btnChangeLocation.addActionListener(this);
467
                }
468

    
469
                return btnChangeLocation;
470
        }
471

    
472
        private StylePreviewer getStylePreviewer() {
473
                if (stylePreview == null) {
474
                        stylePreview = new StylePreviewer();
475
                        stylePreview.setStyle(getPointStyle());
476
                        stylePreview.setPreferredSize(new Dimension(80, 80));
477
                }
478
                return stylePreview;
479
        }
480

    
481
        private JRadioButton getRdOffsetLabelOnTopPoint() {
482
                if (rdBtnOffsetLabelOnTopPoint == null) {
483
                        rdBtnOffsetLabelOnTopPoint = new JRadioButton(
484
                                        Messages.getText("offset_labels_on_top_of_the_points")) ;
485

    
486
                }
487

    
488
                return rdBtnOffsetLabelOnTopPoint;
489
        }
490

    
491
        private JRadioButton getRdOffsetLabelHorizontally() {
492
                if (rdBtnOffsetLabelHorizontally == null) {
493
                        rdBtnOffsetLabelHorizontally = new JRadioButton(
494
                                        Messages.getText("offset_labels_horizontally"));
495

    
496
                }
497

    
498
                return rdBtnOffsetLabelHorizontally;
499
        }
500

    
501
        private JPanel getPolygonSettingsPanel() {
502
                if (polygonSettingsPanel == null) {
503
                        polygonSettingsPanel = new JPanel(new BorderLayout(10, 10));
504
                        JPanel aux = new JPanel();
505
                        aux.setLayout(new BoxLayout(aux, BoxLayout.Y_AXIS));
506
                        aux.add(new JBlank(10,10));
507
                        aux.add(getRdBtnAlwaysHorizontal());
508
                        aux.add(new JBlank(10,10));
509
                        aux.add(getRdBtnAlwaysStraight());
510
                        aux.add(new JBlank(10,50));
511

    
512
                        polygonSettingsPanel.add(getPreview(), BorderLayout.CENTER);
513
                        polygonSettingsPanel.add(aux, BorderLayout.EAST);
514
                        ButtonGroup group = new ButtonGroup();
515
                        group.add(getRdBtnAlwaysHorizontal());
516
                        group.add(getRdBtnAlwaysStraight());
517
                        polygonSettingsPanel.add(getChkFitInsidePolygon(), BorderLayout.SOUTH);
518
                }
519

    
520
                return polygonSettingsPanel;
521
        }
522

    
523
        private JCheckBox getChkFitInsidePolygon() {
524
                if (chkFitInsidePolygon == null) {
525
                        chkFitInsidePolygon = new JCheckBox(
526
                                        Messages.getText("fit_inside_polygon"));
527
                        chkFitInsidePolygon.addActionListener(this);
528

    
529
                }
530

    
531
                return chkFitInsidePolygon;
532
        }
533

    
534
        private JRadioButton getRdBtnAlwaysStraight() {
535
                if (rdBtnAlwaysStraight == null) {
536
                        rdBtnAlwaysStraight = new JRadioButton(
537
                                        Messages.getText("always_straight"));
538
                        rdBtnAlwaysStraight.addActionListener(this);
539
                }
540

    
541
                return rdBtnAlwaysStraight;
542
        }
543

    
544
        private JRadioButton getRdBtnAlwaysHorizontal() {
545
                if (rdBtnAlwaysHorizontal == null) {
546
                        rdBtnAlwaysHorizontal = new JRadioButton(
547
                                        Messages.getText("always_horizontal"));
548
                        rdBtnAlwaysHorizontal.addActionListener(this);
549
                }
550

    
551
                return rdBtnAlwaysHorizontal;
552
        }
553

    
554

    
555

    
556
        private JPanel getPositionPanel() {
557
                if (positionPanel == null) {
558
                        positionPanel = new JPanel(new BorderLayout());
559
                        positionPanel.setBorder(BorderFactory.
560
                                        createTitledBorder(null,
561
                                                        Messages.getText("position")));
562
                        GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
563
                        JPanel aux = new JPanel(new GridLayout(4, 1));
564

    
565
                        aux.add(getChkAbove());
566
                        getChkAbove().setSelected(true);
567
                        aux.add(getChkOnTheLine());
568
                        aux.add(getChkBelow());
569

    
570
                        ButtonGroup group = new ButtonGroup();
571
                        group.add(getChkAbove());
572
                        group.add(getChkOnTheLine());
573
                        group.add(getChkBelow());
574

    
575
                        aux2.addComponent(aux);
576
                        aux2.addComponent(
577
                                        Messages.getText("orientation_system"), getCmbOrientationSystem());
578
                        positionPanel.add(getPreview(), BorderLayout.CENTER);
579
                        positionPanel.add(aux2, BorderLayout.SOUTH);
580
                }
581
                return positionPanel;
582
        }
583

    
584
        private MiniMapContext getPreview() {
585
                 if (preview == null) {
586
                         preview = new MiniMapContext(shapeType);
587
                 }
588
                 return preview;
589
        }
590

    
591
        private JComboBox getCmbOrientationSystem() {
592
                if (cmbOrientationSystem == null) {
593
                        cmbOrientationSystem = new JComboBox(new String[] {
594
                                        Messages.getText("line"),
595
                                        Messages.getText("page")
596
                        });
597
                        cmbOrientationSystem.setSelectedIndex(1);
598
                        cmbOrientationSystem.addActionListener(this);
599
                }
600

    
601
                return cmbOrientationSystem;
602
        }
603

    
604
        private JRadioButton getChkBelow() {
605
                if (chkBellow == null) {
606
                        chkBellow = new JRadioButton(Messages.getText("below"));
607
                        chkBellow.addActionListener(this);
608
                }
609
                return chkBellow;
610
        }
611

    
612
        private JRadioButton getChkOnTheLine() {
613
                if (chkOnTheLine == null) {
614
                        chkOnTheLine = new JRadioButton(
615
                                        Messages.getText("on_the_line"));
616
                        chkOnTheLine.addActionListener(this);
617
                }
618
                return chkOnTheLine;
619
        }
620

    
621
        private JRadioButton getChkAbove() {
622
                if (chkAbove == null) {
623
                        chkAbove = new JRadioButton(Messages.getText("above"));
624
                        chkAbove.addActionListener(this);
625
                }
626
                return chkAbove;
627
        }
628

    
629
        private JPanel getOrientationPanel() {
630
                if (orientationPanel == null) {
631
                    
632
                    orientationPanel = new JPanel(new BorderLayout());
633
            orientationPanel.setBorder(BorderFactory.
634
                createTitledBorder(null,
635
                        Messages.getText("orientation")));
636
                    
637
            GridBagLayoutPanel auxp = new GridBagLayoutPanel();
638
            auxp.addComponent(getRdBtnHorizontal());
639
            auxp.addComponent(getRdBtnParallel());
640
            auxp.addComponent(getRdBtnFollowingLine());
641
            auxp.addComponent(getRdBtnPerpendicular());
642
            
643
            orientationPanel.add(auxp, BorderLayout.NORTH);
644
            
645
                        ButtonGroup group = new ButtonGroup();
646
                        group.add(getRdBtnHorizontal());
647
                        group.add(getRdBtnParallel());
648
                        group.add(getRdBtnFollowingLine());
649
                        group.add(getRdBtnPerpendicular());
650
                }
651
                return orientationPanel;
652
        }
653

    
654
        private JRadioButton getRdBtnParallel() {
655
                if (rdBtnParallel == null) {
656
                        rdBtnParallel = new JRadioButton(
657
                                        Messages.getText("parallel"));
658

    
659
                        rdBtnParallel.addActionListener(this);
660
                }
661
                return rdBtnParallel;
662
        }
663

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

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

    
682
        private JRadioButton getRdBtnHorizontal() {
683
                if (rdBtnHorizontal == null) {
684
                        rdBtnHorizontal = new JRadioButton(
685
                                        Messages.getText("horizontal"));
686
                        rdBtnHorizontal.addActionListener(this);
687
                }
688
                return rdBtnHorizontal;
689
        }
690

    
691

    
692

    
693

    
694
        public void applyConstraints() throws GeometryException {
695
                int mode=0;
696
                GeometryType gt = null;
697
                gt = GeometryLocator.getGeometryManager().getGeometryType(
698
                                shapeType, SUBTYPES.GEOM2D);
699
                
700
                if (gt.isTypeOf(TYPES.POINT) || gt.isTypeOf(TYPES.MULTIPOINT)) {
701
                        if (getRdOffsetLabelOnTopPoint().isSelected()) {
702
                                mode = IPlacementConstraints.ON_TOP_OF_THE_POINT;
703
                        } else if (getRdOffsetLabelHorizontally().isSelected()) {
704
                                mode = IPlacementConstraints.OFFSET_HORIZONTALY_AROUND_THE_POINT;
705
                        }
706
                        ((PointPlacementConstraints) constraints).
707
                                setPositioner((PointLabelPositioner) stylePreview.getStyle());
708
                } else {
709
                        if (gt.isTypeOf(TYPES.CURVE) || gt.isTypeOf(TYPES.MULTICURVE)) {
710
                                if (getRdBtnFollowingLine().isSelected()) {
711
                                        mode = IPlacementConstraints.FOLLOWING_LINE;
712
                                } else if (getRdBtnParallel().isSelected()) {
713
                                        mode = IPlacementConstraints.PARALLEL;
714
                                } else if (getRdBtnPerpendicular().isSelected()) {
715
                                        mode = IPlacementConstraints.PERPENDICULAR;
716
                                } else {
717
                                        mode = IPlacementConstraints.HORIZONTAL;
718
                                }
719

    
720
                                constraints.setAboveTheLine(getChkAbove().isSelected());
721
                                constraints.setBelowTheLine(getChkBelow().isSelected());
722
                                constraints.setOnTheLine(getChkOnTheLine().isSelected());
723

    
724
                                constraints.setPageOriented(
725
                                                getCmbOrientationSystem().getSelectedIndex() == 1);
726
                                int i = getCmbLocationAlongLines().getSelectedIndex();
727
                                if (i == 0) {
728
                                        i = IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE;
729
                                } else if (i == 1) {
730
                                        i = IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE;
731
                                } else if (i == 2) {
732
                                        i = IPlacementConstraints.AT_THE_END_OF_THE_LINE;
733
                                } else if (i == 3) {
734
                                        i = IPlacementConstraints.AT_BEST_OF_LINE;
735
                                }
736
                                constraints.setLocationAlongTheLine(i);
737
                        } else {
738
                                if (gt.isTypeOf(TYPES.SURFACE) || gt.isTypeOf(TYPES.MULTISURFACE)) {
739
                                        mode = IPlacementConstraints.HORIZONTAL;
740
                                        if (getRdBtnAlwaysHorizontal().isSelected()) {
741
                                                mode = IPlacementConstraints.HORIZONTAL;
742
                                        } else if (getRdBtnAlwaysStraight().isSelected()) {
743
                                                mode = IPlacementConstraints.PARALLEL;
744
                                        }
745

    
746
                                        constraints.setFitInsidePolygon(getChkFitInsidePolygon().isSelected());
747
                                }
748
                        }
749
                }                
750
                
751
                constraints.setPlacementMode(mode);
752
                constraints.setDuplicateLabelsMode(duplicateLabelsMode.getMode());
753
        }
754

    
755
        private void setComponentEnabled(Component c, boolean b) {
756
                if (c instanceof JComponent) {
757
                        JComponent c1 = (JComponent) c;
758
                        for (int i = 0; i < c1.getComponentCount(); i++) {
759
                                setComponentEnabled(c1.getComponent(i), b);
760
                        }
761
                }
762
                c.setEnabled(b);
763
        }
764

    
765

    
766
        public IPlacementConstraints getPlacementConstraints() {
767
                return constraints;
768
        }
769

    
770
        public void actionPerformed(ActionEvent e) {
771
                JComponent c = (JComponent) e.getSource();
772
                boolean okPressed = "OK".equals(e.getActionCommand());
773
                boolean cancelPressed = "CANCEL".equals(e.getActionCommand());
774
                if (okPressed || cancelPressed) {
775
                        if (okPressed) {
776
                                try {
777
                                        applyConstraints();
778
                                } catch (GeometryException e1) {
779
                                        logger.error("While applying constraints.", e1);
780
                                }
781
                        }
782

    
783
                        if ("CANCEL".equals(e.getActionCommand()))
784
                                constraints = oldConstraints;
785
                        
786
                        ApplicationLocator.getManager().getUIManager().closeWindow(this);
787

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

    
831
        }
832

    
833
        private void applyToPreview() {
834
                if (preview != null) {
835
                        try {
836
                            if(this.initialized) {
837
                                applyConstraints();
838
                            }
839
                        } catch (GeometryException e) {
840
                                logger.error("While applying constraints.", e);
841
                        }
842
                        preview.setConstraints(constraints);
843
                }
844
        }
845

    
846
        private class JLabelHTML extends JLabel {
847
                private static final long serialVersionUID = -5031405572546951108L;
848

    
849
                public JLabelHTML(String text) {
850
                        super(text);
851
                        setPreferredSize(new Dimension(250, 60));
852
                }
853

    
854
                @Override
855
                public void setText(String text) {
856
                        // silly fix to avoid too large text lines
857
                        super.setText("<html>"+text+"</html>");
858
                }
859
        }
860

    
861
        public static IPlacementProperties createPlacementProperties(
862
                        IPlacementConstraints placementConstraints, int shapeType) {
863
                return createPlacementProperties(placementConstraints, shapeType, null);
864
        }
865

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

    
922
class MiniMapContext extends JComponent {
923
        
924
        private static final Logger minilogger =
925
                        LoggerFactory.getLogger(MiniMapContext.class);
926
        
927
        private static final long serialVersionUID = 229128782038834443L;
928
        private MapContext theMapContext;
929
        private FLyrVect line_layer;
930
        private FLyrVect bgpoly_layer;
931
        private int hMargin = 5, vMargin = 5;
932
        private FLyrVect poly_layer;
933
        
934
        private int geotype;
935
        private IPlacementConstraints placement;
936

    
937

    
938
        public MiniMapContext(int type) {
939
                setType(type);
940
        }
941

    
942
        public void setType(int type) {
943
                
944
                GeometryType gtype = null;
945
                try {
946
                        gtype = GeometryLocator.getGeometryManager().getGeometryType(
947
                                        type, SUBTYPES.GEOM2D);
948
                } catch (Exception e) {
949
                        minilogger.error("While getting geo type", e);
950
                }
951
                
952
                if (gtype.isTypeOf(TYPES.CURVE)
953
                                || gtype.getType() == TYPES.MULTICURVE) {
954
                        this.geotype = TYPES.CURVE;
955
                } else {
956
                        if (gtype.isTypeOf(TYPES.SURFACE)
957
                                        || gtype.getType() == TYPES.MULTISURFACE) {
958
                                this.geotype = TYPES.SURFACE;
959
                        } else {
960
                                // Should not get here
961
                                this.geotype = TYPES.GEOMETRY;
962
                        }
963
                }
964
                
965
        }
966

    
967
        public void setConstraints(IPlacementConstraints constraints) {
968
                placement = constraints;
969
                try {
970
                        getMapContext(null);
971
                } catch (Exception e) {
972
                        minilogger.error("While getting mini map context", e);
973
                }
974
                repaint();
975
        }
976

    
977
        private MapContext getMapContext(Dimension img_size) throws Exception {
978
                
979
                Envelope ini_env = null;
980
                ini_env = GeometryLocator.getGeometryManager().createEnvelope(
981
                                289600, 3973500, 289600 + 2000, 3973700 + 2000,
982
                                SUBTYPES.GEOM2D);
983

    
984
                
985
                if (theMapContext == null) {
986
                        
987
                        SymbologyManager symman = SymbologyLocator.getSymbologyManager();
988
                        MapContextManager mcoman = MapContextLocator.getMapContextManager();
989
                        
990
                        line_layer = this.createLayerFromShpDocFile("line.shp", "EPSG:23030");
991
                        ISimpleLineSymbol line_sym = symman.createSimpleLineSymbol();
992
                        line_sym.setColor(Color.red);
993
                        
994
                        ISimpleLineStyle line_sty = symman.createSimpleLineStyle();
995
                        IArrowDecoratorStyle arrow_style = symman.createArrowDecoratorStyle();
996
                        
997
                        arrow_style.getMarker().setSize(15);
998
                        arrow_style.setArrowMarkerCount(1);
999
                        arrow_style.getMarker().setColor(Color.red);
1000
                        line_sty.setArrowDecorator(arrow_style);
1001

    
1002
                        line_sym.setLineStyle(line_sty);
1003
                        line_sym.setLineWidth(2);
1004
                        
1005
                        ISingleSymbolLegend line_leg = (ISingleSymbolLegend) 
1006
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
1007
                        
1008
                        line_leg.setDefaultSymbol(line_sym);
1009
                        line_layer.setLegend(line_leg);
1010
                        // =====================================================
1011
                        
1012
                        bgpoly_layer = this.createLayerFromShpDocFile("bg-polygon.shp", "EPSG:23030");
1013
                        poly_layer = this.createLayerFromShpDocFile("polygon.shp", "EPSG:23030");
1014

    
1015
                        ISimpleFillSymbol sym2 = symman.createSimpleFillSymbol();
1016
                        sym2.setFillColor(new Color(50, 245, 125));
1017
                        ISimpleLineSymbol outline = symman.createSimpleLineSymbol();
1018
                        outline.setLineColor(Color.DARK_GRAY);
1019
                        outline.setLineWidth(0.5);
1020
                        sym2.setOutline(outline);
1021

    
1022
                        ISingleSymbolLegend poly_leg = (ISingleSymbolLegend) 
1023
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
1024
                        poly_leg.setDefaultSymbol(sym2);
1025
                        poly_layer.setLegend(poly_leg);
1026
                        bgpoly_layer.setLegend(poly_leg);
1027

    
1028
                        GeneralLabelingStrategy labeling1 = new GeneralLabelingStrategy();
1029
                        GeneralLabelingStrategy labeling2 = new GeneralLabelingStrategy();
1030
                        ILabelingMethod def_method = symman.createDefaultLabelingMethod();
1031

    
1032
                        ILabelClass lc = null;
1033
                        if (def_method.getLabelClasses() != null && def_method.getLabelClasses().length > 0) {
1034
                                lc = def_method.getLabelClasses()[0];
1035
                        } else {
1036
                                lc = symman.createDefaultLabel();
1037
                                def_method.addLabelClass(lc);
1038
                        }
1039

    
1040
                        String[] sampleExpression = { Messages.getText("text") };
1041
                        lc.setLabelExpressions(sampleExpression);
1042

    
1043
                        lc.getTextSymbol().setFontSize(16);
1044

    
1045
                        labeling1.setLabelingMethod(def_method);
1046
                        labeling2.setLabelingMethod(def_method);
1047
                        labeling1.setLayer(line_layer);
1048
                        labeling2.setLayer(poly_layer);
1049

    
1050
                        line_layer.setLabelingStrategy(labeling1);
1051
                        line_layer.setIsLabeled(true);
1052
                        poly_layer.setLabelingStrategy(labeling2);
1053
                        poly_layer.setIsLabeled(true);
1054
                        
1055
                        ViewPort theViewPort = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
1056
                        theMapContext = new MapContext(theViewPort);
1057
                        
1058
                        if (img_size != null) {
1059
                                theViewPort.setImageSize(img_size);
1060
                        } else {
1061
                                theViewPort.setImageSize(new Dimension(200, 200));
1062
                        }
1063
                        theViewPort.setEnvelope(ini_env);
1064
                        
1065
                        theMapContext.getLayers().addLayer(bgpoly_layer);
1066
                        theMapContext.getLayers().addLayer(poly_layer);
1067
                        theMapContext.getLayers().addLayer(line_layer);
1068
                } else {
1069
                        
1070
                        if (img_size != null) {
1071
                                theMapContext.getViewPort().setImageSize(img_size);
1072
                        } else {
1073
                                theMapContext.getViewPort().setImageSize(new Dimension(200, 200));
1074
                        }
1075
                        theMapContext.getViewPort().setEnvelope(ini_env);
1076
                }
1077

    
1078
                line_layer.getLabelingStrategy().setPlacementConstraints(placement);
1079
                poly_layer.getLabelingStrategy().setPlacementConstraints(placement);
1080
                Dimension sz = getBounds().getSize();
1081
                sz.setSize(sz.width-2*hMargin, sz.height-2*vMargin);
1082
                theMapContext.getViewPort().setImageSize(sz);
1083
                theMapContext.getViewPort().setBackColor(new Color(255, 0, 0));
1084
                theMapContext.invalidate();
1085
                return theMapContext;
1086
        }
1087

    
1088
        @Override
1089
        protected void paintComponent(Graphics g) {
1090
                try {
1091
                        Rectangle bounds = getBounds();
1092
                        Dimension sz = bounds.getSize();
1093
                        sz.setSize(sz.width-2*vMargin, sz.height-2*hMargin);
1094
                        Dimension imageSize = sz;
1095
                        
1096
                        MapContext mco = getMapContext(imageSize);
1097
                        if (this.geotype == TYPES.CURVE) {
1098
                                line_layer.setVisible(true);
1099
                                poly_layer.setVisible(false);
1100
                        } else {
1101
                                if (this.geotype == TYPES.SURFACE) {
1102
                                        line_layer.setVisible(false);
1103
                                        poly_layer.setVisible(true);
1104
                                }
1105
                        }
1106
                        bgpoly_layer.setVisible(line_layer.isVisible());
1107

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

    
1130
        }
1131
        
1132
        private FLyrVect createLayerFromShpDocFile(
1133
                        String shp_file_name, String epsg_code) throws Exception {
1134
                
1135
                File shpfile = getDocFolderFile(shp_file_name);
1136
                IProjection proj = CRSFactory.getCRS(epsg_code);
1137
                DataStoreParameters ssp = DALLocator.getDataManager().createStoreParameters("Shape");
1138
                ssp.setDynValue("shpfile", shpfile);
1139
                ssp.setDynValue("CRS", proj);
1140
                FLyrVect resp = null;
1141
                resp = (FLyrVect) MapContextLocator.getMapContextManager().createLayer(
1142
                                shp_file_name, ssp);
1143
                return resp;
1144
        }
1145
        
1146
        private File getDocFolderFile(String name) {
1147
                
1148
                URL resource = this.getClass().getClassLoader().getResource(
1149
                                "docs" + File.separator + name);
1150
                File resp = new File(resource.getFile());
1151
                return resp;
1152
        }
1153
    
1154
}