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

History | View | Annotate | Download (35.2 KB)

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

    
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
                this.shapeType = shapeType;
277
                this.oldConstraints = constraints;
278
//                this.constraints = constraints != null ?
279
//                                PlacementManager.createPlacementConstraints(constraints.getXMLEntity())        :
280
//                                PlacementManager.createPlacementConstraints(shapeType);
281
                if( constraints != null ) {
282
                        
283
                        this.constraints = (IPlacementConstraints) LabelClassUtils.clone(constraints);
284
                        
285
                        if (constraints instanceof PointPlacementConstraints) {
286
                                PointLabelPositioner positioneer = ((PointPlacementConstraints)constraints).getPositioner();
287
                                if (positioneer != null) this.setPointStyle(positioneer);
288
                                this.lblPointPosDesc.setText(this.getPointStyle().getDescription());
289
                        }
290
                } else {
291
                        try {
292
                                this.constraints = PlacementManager.createPlacementConstraints(shapeType);
293
                        } catch (Exception e) {
294
                                logger.error("While initializing placement constraints.", e);
295
                                return;
296
                        }
297
                }
298

    
299
                linePositionItem.add(Messages.getText("in_the_middle"));
300
                linePositionItem.add(Messages.getText("at_begin"));
301
                linePositionItem.add(Messages.getText("at_end"));
302
                linePositionItem.add(Messages.getText("at_best"));
303

    
304
        this.setLayout(new BorderLayout());
305
        this.setSize(new Dimension(410,380));
306
        this.add(getPnlContent(), BorderLayout.CENTER);
307
         }
308

    
309
        public WindowInfo getWindowInfo() {
310
                WindowInfo viewInfo = new WindowInfo(
311
                                WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
312
                viewInfo.setWidth(440);
313
                viewInfo.setHeight(520);
314
                viewInfo.setTitle(Messages.getText("placement_properties"));
315
                return viewInfo;
316
        }
317

    
318
        public Object getWindowProfile() {
319
                return WindowInfo.DIALOG_PROFILE;
320
        }
321

    
322
        private JPanel getPnlContent() {
323
                if (pnlContent == null) {
324
                        pnlContent = new JPanel();
325
                        pnlContent.setLayout(new BorderLayout());
326
                        pnlContent.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
327
                        pnlContent.add(getPnlDuplicateLabels(), java.awt.BorderLayout.SOUTH);
328
                }
329
                return pnlContent;
330
        }
331

    
332
        private DuplicateLayersMode getPnlDuplicateLabels() {
333
                if (duplicateLabelsMode == null) {
334
                        duplicateLabelsMode = new DuplicateLayersMode(this);
335
                }
336
                return duplicateLabelsMode;
337
        }
338

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

    
384
        private JPanel getLocationPanel() {
385
                if (locationPanel == null) {
386
                        locationPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
387
                        locationPanel.setBorder(BorderFactory.
388
                                        createTitledBorder(null,
389
                                                        Messages.getText("location")));
390
                        locationPanel.add(new JLabel(Messages.getText(
391
                                        "location_along_the_lines")+":"));
392
                        locationPanel.add(getCmbLocationAlongLines());
393
                }
394

    
395
                return locationPanel;
396
        }
397

    
398
        private JComboBox getCmbLocationAlongLines() {
399
                if (cmbLocationAlongLines == null) {
400
                        cmbLocationAlongLines = new JComboBox((String[]) linePositionItem.toArray(new String[linePositionItem.size()]));
401
                        cmbLocationAlongLines.addActionListener(this);
402
                }
403

    
404
                return cmbLocationAlongLines;
405
        }
406

    
407
        private PointLabelPositioner getPointStyle(){
408
                if (pointStyle == null){
409
                        pointStyle = defaultPointStyle;
410
                        lblPointPosDesc.setText(pointStyle.getDescription());
411
                }
412
                return pointStyle;
413
        }
414

    
415
        private void setPointStyle(PointLabelPositioner pointStyle){
416
                this.pointStyle = pointStyle;
417
        }
418

    
419
        private GridBagLayoutPanel getPointSettingsPanel() {
420
                if (pointSettingsPanel == null) {
421
                        pointSettingsPanel = new GridBagLayoutPanel();
422
                        pointSettingsPanel.addComponent(getRdOffsetLabelHorizontally());
423
                        JPanel aux = new JPanel();
424
                        aux.add(getStylePreviewer());
425

    
426
                        JPanel aux2 = new JPanel();
427
                        aux2.setLayout(new BoxLayout(aux2, BoxLayout.Y_AXIS));
428
                        aux2.add(lblPointPosDesc);
429
                        aux2.add(new JBlank(20, 5));
430
                        aux2.add(getBtnChangeLocation());
431

    
432
                        aux.add(aux2);
433
                        pointSettingsPanel.addComponent("", aux);
434
                        pointSettingsPanel.addComponent("",
435
                                        new JLabel(Messages.getText("label-point-priority-help")));
436
                        pointSettingsPanel.addComponent(getRdOffsetLabelOnTopPoint());
437
                        ButtonGroup group = new ButtonGroup();
438
                        group.add(getRdOffsetLabelHorizontally());
439
                        group.add(getRdOffsetLabelOnTopPoint());
440
                }
441

    
442
                return pointSettingsPanel;
443
        }
444

    
445

    
446

    
447
        private JButton getBtnChangeLocation() {
448
                if (btnChangeLocation == null) {
449
                        btnChangeLocation = new JButton(Messages.getText("change_location"));
450
                        btnChangeLocation.addActionListener(this);
451
                }
452

    
453
                return btnChangeLocation;
454
        }
455

    
456
        private StylePreviewer getStylePreviewer() {
457
                if (stylePreview == null) {
458
                        stylePreview = new StylePreviewer();
459
                        stylePreview.setStyle(getPointStyle());
460
                        stylePreview.setPreferredSize(new Dimension(80, 80));
461
                }
462
                return stylePreview;
463
        }
464

    
465
        private JRadioButton getRdOffsetLabelOnTopPoint() {
466
                if (rdBtnOffsetLabelOnTopPoint == null) {
467
                        rdBtnOffsetLabelOnTopPoint = new JRadioButton(
468
                                        Messages.getText("offset_labels_on_top_of_the_points")) ;
469

    
470
                }
471

    
472
                return rdBtnOffsetLabelOnTopPoint;
473
        }
474

    
475
        private JRadioButton getRdOffsetLabelHorizontally() {
476
                if (rdBtnOffsetLabelHorizontally == null) {
477
                        rdBtnOffsetLabelHorizontally = new JRadioButton(
478
                                        Messages.getText("offset_labels_horizontally"));
479

    
480
                }
481

    
482
                return rdBtnOffsetLabelHorizontally;
483
        }
484

    
485
        private JPanel getPolygonSettingsPanel() {
486
                if (polygonSettingsPanel == null) {
487
                        polygonSettingsPanel = new JPanel(new BorderLayout(10, 10));
488
                        JPanel aux = new JPanel();
489
                        aux.setLayout(new BoxLayout(aux, BoxLayout.Y_AXIS));
490
                        aux.add(new JBlank(10,10));
491
                        aux.add(getRdBtnAlwaysHorizontal());
492
                        aux.add(new JBlank(10,10));
493
                        aux.add(getRdBtnAlwaysStraight());
494
                        aux.add(new JBlank(10,50));
495

    
496
                        polygonSettingsPanel.add(getPreview(), BorderLayout.CENTER);
497
                        polygonSettingsPanel.add(aux, BorderLayout.EAST);
498
                        ButtonGroup group = new ButtonGroup();
499
                        group.add(getRdBtnAlwaysHorizontal());
500
                        group.add(getRdBtnAlwaysStraight());
501
                        polygonSettingsPanel.add(getChkFitInsidePolygon(), BorderLayout.SOUTH);
502
                }
503

    
504
                return polygonSettingsPanel;
505
        }
506

    
507
        private JCheckBox getChkFitInsidePolygon() {
508
                if (chkFitInsidePolygon == null) {
509
                        chkFitInsidePolygon = new JCheckBox(
510
                                        Messages.getText("fit_inside_polygon"));
511
                        chkFitInsidePolygon.addActionListener(this);
512

    
513
                }
514

    
515
                return chkFitInsidePolygon;
516
        }
517

    
518
        private JRadioButton getRdBtnAlwaysStraight() {
519
                if (rdBtnAlwaysStraight == null) {
520
                        rdBtnAlwaysStraight = new JRadioButton(
521
                                        Messages.getText("always_straight"));
522
                        rdBtnAlwaysStraight.addActionListener(this);
523
                }
524

    
525
                return rdBtnAlwaysStraight;
526
        }
527

    
528
        private JRadioButton getRdBtnAlwaysHorizontal() {
529
                if (rdBtnAlwaysHorizontal == null) {
530
                        rdBtnAlwaysHorizontal = new JRadioButton(
531
                                        Messages.getText("always_horizontal"));
532
                        rdBtnAlwaysHorizontal.addActionListener(this);
533
                }
534

    
535
                return rdBtnAlwaysHorizontal;
536
        }
537

    
538

    
539

    
540
        private JPanel getPositionPanel() {
541
                if (positionPanel == null) {
542
                        positionPanel = new JPanel(new BorderLayout());
543
                        positionPanel.setBorder(BorderFactory.
544
                                        createTitledBorder(null,
545
                                                        Messages.getText("position")));
546
                        GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
547
                        JPanel aux = new JPanel(new GridLayout(4, 1));
548

    
549
                        aux.add(getChkAbove());
550
                        getChkAbove().setSelected(true);
551
                        aux.add(getChkOnTheLine());
552
                        aux.add(getChkBelow());
553

    
554
                        ButtonGroup group = new ButtonGroup();
555
                        group.add(getChkAbove());
556
                        group.add(getChkOnTheLine());
557
                        group.add(getChkBelow());
558

    
559
                        aux2.addComponent(aux);
560
                        aux2.addComponent(
561
                                        Messages.getText("orientation_system"), getCmbOrientationSystem());
562
                        positionPanel.add(getPreview(), BorderLayout.CENTER);
563
                        positionPanel.add(aux2, BorderLayout.SOUTH);
564
                }
565
                return positionPanel;
566
        }
567

    
568
        private Component getPreview() {
569
                 if (preview == null) {
570
                         preview = new MiniMapContext(shapeType);
571
                 }
572
                 return preview;
573
        }
574

    
575
        private JComboBox getCmbOrientationSystem() {
576
                if (cmbOrientationSystem == null) {
577
                        cmbOrientationSystem = new JComboBox(new String[] {
578
                                        Messages.getText("line"),
579
                                        Messages.getText("page")
580
                        });
581
                        cmbOrientationSystem.setSelectedIndex(1);
582
                        cmbOrientationSystem.addActionListener(this);
583
                }
584

    
585
                return cmbOrientationSystem;
586
        }
587

    
588
        private JRadioButton getChkBelow() {
589
                if (chkBellow == null) {
590
                        chkBellow = new JRadioButton(Messages.getText("below"));
591
                        chkBellow.addActionListener(this);
592
                }
593
                return chkBellow;
594
        }
595

    
596
        private JRadioButton getChkOnTheLine() {
597
                if (chkOnTheLine == null) {
598
                        chkOnTheLine = new JRadioButton(
599
                                        Messages.getText("on_the_line"));
600
                        chkOnTheLine.addActionListener(this);
601
                }
602
                return chkOnTheLine;
603
        }
604

    
605
        private JRadioButton getChkAbove() {
606
                if (chkAbove == null) {
607
                        chkAbove = new JRadioButton(Messages.getText("above"));
608
                        chkAbove.addActionListener(this);
609
                }
610
                return chkAbove;
611
        }
612

    
613
        private JPanel getOrientationPanel() {
614
                if (orientationPanel == null) {
615
                    
616
                    orientationPanel = new JPanel(new BorderLayout());
617
            orientationPanel.setBorder(BorderFactory.
618
                createTitledBorder(null,
619
                        Messages.getText("orientation")));
620
                    
621
            GridBagLayoutPanel auxp = new GridBagLayoutPanel();
622
            auxp.addComponent(getRdBtnHorizontal());
623
            auxp.addComponent(getRdBtnParallel());
624
            auxp.addComponent(getRdBtnFollowingLine());
625
            auxp.addComponent(getRdBtnPerpendicular());
626
            
627
            orientationPanel.add(auxp, BorderLayout.NORTH);
628
            
629
                        ButtonGroup group = new ButtonGroup();
630
                        group.add(getRdBtnHorizontal());
631
                        group.add(getRdBtnParallel());
632
                        group.add(getRdBtnFollowingLine());
633
                        group.add(getRdBtnPerpendicular());
634
                }
635
                return orientationPanel;
636
        }
637

    
638
        private JRadioButton getRdBtnParallel() {
639
                if (rdBtnParallel == null) {
640
                        rdBtnParallel = new JRadioButton(
641
                                        Messages.getText("parallel"));
642

    
643
                        rdBtnParallel.addActionListener(this);
644
                }
645
                return rdBtnParallel;
646
        }
647

    
648
        private JRadioButton getRdBtnFollowingLine() {
649
                if (rdBtnFollowingLine == null) {
650
                        rdBtnFollowingLine = new JRadioButton(
651
                                        Messages.getText("following_line"));
652
                        rdBtnFollowingLine.addActionListener(this);
653
                }
654
                return rdBtnFollowingLine;
655
        }
656

    
657
        private JRadioButton getRdBtnPerpendicular() {
658
                if (rdBtnPerpendicular == null) {
659
                        rdBtnPerpendicular = new JRadioButton(
660
                                        Messages.getText("perpedicular"));
661
                        rdBtnPerpendicular.addActionListener(this);
662
                }
663
                return rdBtnPerpendicular;
664
        }
665

    
666
        private JRadioButton getRdBtnHorizontal() {
667
                if (rdBtnHorizontal == null) {
668
                        rdBtnHorizontal = new JRadioButton(
669
                                        Messages.getText("horizontal"));
670
                        rdBtnHorizontal.addActionListener(this);
671
                }
672
                return rdBtnHorizontal;
673
        }
674

    
675

    
676

    
677

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

    
704
                                constraints.setAboveTheLine(getChkAbove().isSelected());
705
                                constraints.setBelowTheLine(getChkBelow().isSelected());
706
                                constraints.setOnTheLine(getChkOnTheLine().isSelected());
707

    
708
                                constraints.setPageOriented(
709
                                                getCmbOrientationSystem().getSelectedIndex() == 1);
710
                                int i = getCmbLocationAlongLines().getSelectedIndex();
711
                                if (i == 0) {
712
                                        i = IPlacementConstraints.AT_THE_MIDDLE_OF_THE_LINE;
713
                                } else if (i == 1) {
714
                                        i = IPlacementConstraints.AT_THE_BEGINING_OF_THE_LINE;
715
                                } else if (i == 2) {
716
                                        i = IPlacementConstraints.AT_THE_END_OF_THE_LINE;
717
                                } else if (i == 3) {
718
                                        i = IPlacementConstraints.AT_BEST_OF_LINE;
719
                                }
720
                                constraints.setLocationAlongTheLine(i);
721
                        } else {
722
                                if (gt.isTypeOf(TYPES.SURFACE) || gt.isTypeOf(TYPES.MULTISURFACE)) {
723
                                        mode = IPlacementConstraints.HORIZONTAL;
724
                                        if (getRdBtnAlwaysHorizontal().isSelected()) {
725
                                                mode = IPlacementConstraints.HORIZONTAL;
726
                                        } else if (getRdBtnAlwaysStraight().isSelected()) {
727
                                                mode = IPlacementConstraints.PARALLEL;
728
                                        }
729

    
730
                                        constraints.setFitInsidePolygon(getChkFitInsidePolygon().isSelected());
731
                                }
732
                        }
733
                }                
734
                
735
                constraints.setPlacementMode(mode);
736
                constraints.setDuplicateLabelsMode(duplicateLabelsMode.getMode());
737
        }
738

    
739
        private void setComponentEnabled(Component c, boolean b) {
740
                if (c instanceof JComponent) {
741
                        JComponent c1 = (JComponent) c;
742
                        for (int i = 0; i < c1.getComponentCount(); i++) {
743
                                setComponentEnabled(c1.getComponent(i), b);
744
                        }
745
                }
746
                c.setEnabled(b);
747
        }
748

    
749

    
750
        public IPlacementConstraints getPlacementConstraints() {
751
                return constraints;
752
        }
753

    
754
        public void actionPerformed(ActionEvent e) {
755
                JComponent c = (JComponent) e.getSource();
756
                boolean okPressed = "OK".equals(e.getActionCommand());
757
                boolean cancelPressed = "CANCEL".equals(e.getActionCommand());
758
                if (okPressed || cancelPressed) {
759
                        if (okPressed) {
760
                                try {
761
                                        applyConstraints();
762
                                } catch (GeometryException e1) {
763
                                        logger.error("While applying constraints.", e1);
764
                                }
765
                        }
766

    
767
                        if ("CANCEL".equals(e.getActionCommand()))
768
                                constraints = oldConstraints;
769
                        
770
                        ApplicationLocator.getManager().getUIManager().closeWindow(this);
771

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

    
815
        }
816

    
817
        private void applyToPreview() {
818
                if (preview != null) {
819
                        try {
820
                                applyConstraints();
821
                        } catch (GeometryException e) {
822
                                logger.error("While applying constraints.", e);
823
                        }
824
                        preview.setConstraints(constraints);
825
                }
826
        }
827

    
828
        private class JLabelHTML extends JLabel {
829
                private static final long serialVersionUID = -5031405572546951108L;
830

    
831
                public JLabelHTML(String text) {
832
                        super(text);
833
                        setPreferredSize(new Dimension(250, 60));
834
                }
835

    
836
                @Override
837
                public void setText(String text) {
838
                        // silly fix to avoid too large text lines
839
                        super.setText("<html>"+text+"</html>");
840
                }
841
        }
842

    
843
        public static IPlacementProperties createPlacementProperties(
844
                        IPlacementConstraints placementConstraints, int shapeType) {
845
                return createPlacementProperties(placementConstraints, shapeType, null);
846
        }
847

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

    
904
class MiniMapContext extends JComponent {
905
        
906
        private static final Logger minilogger =
907
                        LoggerFactory.getLogger(MiniMapContext.class);
908
        
909
        private static final long serialVersionUID = 229128782038834443L;
910
        private MapContext theMapContext;
911
        private FLyrVect line_layer;
912
        private FLyrVect bgpoly_layer;
913
        private int hMargin = 5, vMargin = 5;
914
        private FLyrVect poly_layer;
915
        
916
        private int geotype;
917
        private IPlacementConstraints placement;
918

    
919

    
920
        public MiniMapContext(int type) {
921
                setType(type);
922
        }
923

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

    
949
        public void setConstraints(IPlacementConstraints constraints) {
950
                placement = constraints;
951
                try {
952
                        getMapContext(null);
953
                } catch (Exception e) {
954
                        minilogger.error("While getting mini map context", e);
955
                }
956
                repaint();
957
        }
958

    
959
        private MapContext getMapContext(Dimension img_size) throws Exception {
960
                
961
                Envelope ini_env = null;
962
                ini_env = GeometryLocator.getGeometryManager().createEnvelope(
963
                                289600, 3973500, 289600 + 2000, 3973700 + 2000,
964
                                SUBTYPES.GEOM2D);
965

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

    
984
                        line_sym.setLineStyle(line_sty);
985
                        line_sym.setLineWidth(2);
986
                        
987
                        ISingleSymbolLegend line_leg = (ISingleSymbolLegend) 
988
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
989
                        
990
                        line_leg.setDefaultSymbol(line_sym);
991
                        line_layer.setLegend(line_leg);
992
                        // =====================================================
993
                        
994
                        bgpoly_layer = this.createLayerFromShpDocFile("bg-polygon.shp", "EPSG:23030");
995
                        poly_layer = this.createLayerFromShpDocFile("polygon.shp", "EPSG:23030");
996

    
997
                        ISimpleFillSymbol sym2 = symman.createSimpleFillSymbol();
998
                        sym2.setFillColor(new Color(50, 245, 125));
999
                        ISimpleLineSymbol outline = symman.createSimpleLineSymbol();
1000
                        outline.setLineColor(Color.DARK_GRAY);
1001
                        outline.setLineWidth(0.5);
1002
                        sym2.setOutline(outline);
1003

    
1004
                        ISingleSymbolLegend poly_leg = (ISingleSymbolLegend) 
1005
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
1006
                        poly_leg.setDefaultSymbol(sym2);
1007
                        poly_layer.setLegend(poly_leg);
1008
                        bgpoly_layer.setLegend(poly_leg);
1009

    
1010
                        GeneralLabelingStrategy labeling1 = new GeneralLabelingStrategy();
1011
                        GeneralLabelingStrategy labeling2 = new GeneralLabelingStrategy();
1012
                        ILabelingMethod def_method = symman.createDefaultLabelingMethod();
1013

    
1014
                        ILabelClass lc = null;
1015
                        if (def_method.getLabelClasses() != null && def_method.getLabelClasses().length > 0) {
1016
                                lc = def_method.getLabelClasses()[0];
1017
                        } else {
1018
                                lc = symman.createDefaultLabel();
1019
                                def_method.addLabelClass(lc);
1020
                        }
1021

    
1022
                        String[] sampleExpression = { Messages.getText("text") };
1023
                        lc.setLabelExpressions(sampleExpression);
1024

    
1025
                        lc.getTextSymbol().setFontSize(16);
1026

    
1027
                        labeling1.setLabelingMethod(def_method);
1028
                        labeling2.setLabelingMethod(def_method);
1029
                        labeling1.setLayer(line_layer);
1030
                        labeling2.setLayer(poly_layer);
1031

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

    
1060
                line_layer.getLabelingStrategy().setPlacementConstraints(placement);
1061
                poly_layer.getLabelingStrategy().setPlacementConstraints(placement);
1062
                Dimension sz = getBounds().getSize();
1063
                sz.setSize(sz.width-2*hMargin, sz.height-2*vMargin);
1064
                theMapContext.getViewPort().setImageSize(sz);
1065
                theMapContext.getViewPort().setBackColor(new Color(255, 0, 0));
1066
                theMapContext.invalidate();
1067
                return theMapContext;
1068
        }
1069

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

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

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