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

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

    
103
import org.cresques.cts.IProjection;
104
import org.gvsig.andami.ui.mdiManager.WindowInfo;
105
import org.gvsig.app.ApplicationLocator;
106
import org.gvsig.app.gui.styling.SingleStyleSelectorFilter;
107
import org.gvsig.app.gui.styling.StylePreviewer;
108
import org.gvsig.app.gui.styling.StyleSelector;
109
import org.gvsig.fmap.crs.CRSFactory;
110
import org.gvsig.fmap.dal.DALLocator;
111
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
112
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider;
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 GridBagLayoutPanel 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(getWidth());
313
                viewInfo.setHeight(getHeight());
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(0);
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 GridBagLayoutPanel getOrientationPanel() {
614
                if (orientationPanel == null) {
615
                        orientationPanel = new GridBagLayoutPanel();
616
                        orientationPanel.setBorder(BorderFactory.
617
                                        createTitledBorder(null,
618
                                                        Messages.getText("orientation")));
619
                        orientationPanel.addComponent(getRdBtnHorizontal());
620
                        orientationPanel.addComponent(getRdBtnParallel());
621
                        orientationPanel.addComponent(getRdBtnFollowingLine());
622
                        orientationPanel.addComponent(getRdBtnPerpendicular());
623
                        ButtonGroup group = new ButtonGroup();
624
                        group.add(getRdBtnHorizontal());
625
                        group.add(getRdBtnParallel());
626
                        group.add(getRdBtnFollowingLine());
627
                        group.add(getRdBtnPerpendicular());
628

    
629

    
630

    
631
                }
632
                return orientationPanel;
633
        }
634

    
635
        private JRadioButton getRdBtnParallel() {
636
                if (rdBtnParallel == null) {
637
                        rdBtnParallel = new JRadioButton(
638
                                        Messages.getText("parallel"));
639

    
640
                        rdBtnParallel.addActionListener(this);
641
                }
642
                return rdBtnParallel;
643
        }
644

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

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

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

    
672

    
673

    
674

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

    
701
                                constraints.setAboveTheLine(getChkAbove().isSelected());
702
                                constraints.setBelowTheLine(getChkBelow().isSelected());
703
                                constraints.setOnTheLine(getChkOnTheLine().isSelected());
704

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

    
727
                                        constraints.setFitInsidePolygon(getChkFitInsidePolygon().isSelected());
728
                                }
729
                        }
730
                }                
731
                
732
                constraints.setPlacementMode(mode);
733
                constraints.setDuplicateLabelsMode(duplicateLabelsMode.getMode());
734
        }
735

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

    
746

    
747
        public IPlacementConstraints getPlacementConstraints() {
748
                return constraints;
749
        }
750

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

    
764
                        if ("CANCEL".equals(e.getActionCommand()))
765
                                constraints = oldConstraints;
766
                        
767
                        ApplicationLocator.getManager().getUIManager().closeWindow(this);
768

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

    
809
        }
810

    
811
        private void applyToPreview() {
812
                if (preview != null) {
813
                        try {
814
                                applyConstraints();
815
                        } catch (GeometryException e) {
816
                                logger.error("While applying constraints.", e);
817
                        }
818
                        preview.setConstraints(constraints);
819
                }
820
        }
821

    
822
        private class JLabelHTML extends JLabel {
823
                private static final long serialVersionUID = -5031405572546951108L;
824

    
825
                public JLabelHTML(String text) {
826
                        super(text);
827
                        setPreferredSize(new Dimension(250, 60));
828
                }
829

    
830
                @Override
831
                public void setText(String text) {
832
                        // silly fix to avoid too large text lines
833
                        super.setText("<html>"+text+"</html>");
834
                }
835
        }
836

    
837
        public static IPlacementProperties createPlacementProperties(
838
                        IPlacementConstraints placementConstraints, int shapeType) {
839
                return createPlacementProperties(placementConstraints, shapeType, null);
840
        }
841

    
842
        protected static IPlacementProperties createPlacementProperties(
843
                        IPlacementConstraints placementConstraints, int geotype,
844
                        DuplicateLayersMode duplicatesMode) {
845
                
846
                IPlacementProperties pp = null;
847
                if (geotype == TYPES.GEOMETRY || geotype == TYPES.AGGREGATE) {
848
                        try {
849
                                pp = new MultiShapePlacementProperties(
850
                                                (MultiShapePlacementConstraints) placementConstraints);
851
                        } catch (Exception e) {
852
                                logger.error("While creating MultiShapePlacementProperties", e);
853
                        }
854
                } else {
855
                        pp = new PlacementProperties(placementConstraints, geotype, null);
856
                }
857
                ((JPanel) pp).add(new AcceptCancelPanel(pp, pp), BorderLayout.SOUTH);
858
                return pp;
859
        }
860
}  //  @jve:decl-index=0:visual-constraint="10,10"
861

    
862
class MiniMapContext extends JComponent {
863
        
864
        private static final Logger minilogger =
865
                        LoggerFactory.getLogger(MiniMapContext.class);
866
        
867
        private static final long serialVersionUID = 229128782038834443L;
868
        private MapContext theMapContext;
869
        private FLyrVect line_layer;
870
        private FLyrVect bgpoly_layer;
871
        private int hMargin = 5, vMargin = 5;
872
        private FLyrVect poly_layer;
873
        
874
        private int geotype;
875
        private IPlacementConstraints placement;
876

    
877

    
878
        public MiniMapContext(int type) {
879
                setType(type);
880
        }
881

    
882
        public void setType(int type) {
883
                
884
                GeometryType gtype = null;
885
                try {
886
                        gtype = GeometryLocator.getGeometryManager().getGeometryType(
887
                                        type, SUBTYPES.GEOM2D);
888
                } catch (Exception e) {
889
                        minilogger.error("While getting geo type", e);
890
                }
891
                
892
                if (gtype.isTypeOf(TYPES.CURVE)
893
                                || gtype.getType() == TYPES.MULTICURVE) {
894
                        this.geotype = TYPES.CURVE;
895
                } else {
896
                        if (gtype.isTypeOf(TYPES.SURFACE)
897
                                        || gtype.getType() == TYPES.MULTISURFACE) {
898
                                this.geotype = TYPES.SURFACE;
899
                        } else {
900
                                // Should not get here
901
                                this.geotype = TYPES.GEOMETRY;
902
                        }
903
                }
904
                
905
        }
906

    
907
        public void setConstraints(IPlacementConstraints constraints) {
908
                placement = constraints;
909
                try {
910
                        getMapContext(null);
911
                } catch (Exception e) {
912
                        minilogger.error("While getting mini map context", e);
913
                }
914
                repaint();
915
        }
916

    
917
        private MapContext getMapContext(Dimension img_size) throws Exception {
918
                
919
                Envelope ini_env = null;
920
                ini_env = GeometryLocator.getGeometryManager().createEnvelope(
921
                                289600, 3973500, 289600 + 2000, 3973700 + 2000,
922
                                SUBTYPES.GEOM2D);
923

    
924
                
925
                if (theMapContext == null) {
926
                        
927
                        SymbologyManager symman = SymbologyLocator.getSymbologyManager();
928
                        MapContextManager mcoman = MapContextLocator.getMapContextManager();
929
                        
930
                        line_layer = this.createLayerFromShpDocFile("line.shp", "EPSG:23030");
931
                        ISimpleLineSymbol line_sym = symman.createSimpleLineSymbol();
932
                        line_sym.setColor(Color.red);
933
                        
934
                        ISimpleLineStyle line_sty = symman.createSimpleLineStyle();
935
                        IArrowDecoratorStyle arrow_style = symman.createArrowDecoratorStyle();
936
                        
937
                        arrow_style.getMarker().setSize(15);
938
                        arrow_style.setArrowMarkerCount(1);
939
                        arrow_style.getMarker().setColor(Color.red);
940
                        line_sty.setArrowDecorator(arrow_style);
941

    
942
                        line_sym.setLineStyle(line_sty);
943
                        line_sym.setLineWidth(2);
944
                        
945
                        ISingleSymbolLegend line_leg = (ISingleSymbolLegend) 
946
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
947
                        
948
                        line_leg.setDefaultSymbol(line_sym);
949
                        line_layer.setLegend(line_leg);
950
                        // =====================================================
951
                        
952
                        bgpoly_layer = this.createLayerFromShpDocFile("bg-polygon.shp", "EPSG:23030");
953
                        poly_layer = this.createLayerFromShpDocFile("polygon.shp", "EPSG:23030");
954

    
955
                        ISimpleFillSymbol sym2 = symman.createSimpleFillSymbol();
956
                        sym2.setFillColor(new Color(50, 245, 125));
957
                        ISimpleLineSymbol outline = symman.createSimpleLineSymbol();
958
                        outline.setLineColor(Color.DARK_GRAY);
959
                        outline.setLineWidth(0.5);
960
                        sym2.setOutline(outline);
961

    
962
                        ISingleSymbolLegend poly_leg = (ISingleSymbolLegend) 
963
                        mcoman.createLegend(ISingleSymbolLegend.LEGEND_NAME);
964
                        poly_leg.setDefaultSymbol(sym2);
965
                        poly_layer.setLegend(poly_leg);
966
                        bgpoly_layer.setLegend(poly_leg);
967

    
968
                        GeneralLabelingStrategy labeling1 = new GeneralLabelingStrategy();
969
                        GeneralLabelingStrategy labeling2 = new GeneralLabelingStrategy();
970
                        ILabelingMethod def_method = symman.createDefaultLabelingMethod();
971

    
972
                        ILabelClass lc = null;
973
                        if (def_method.getLabelClasses() != null && def_method.getLabelClasses().length > 0) {
974
                                lc = def_method.getLabelClasses()[0];
975
                        } else {
976
                                lc = symman.createDefaultLabel();
977
                                def_method.addLabelClass(lc);
978
                        }
979

    
980
                        String[] sampleExpression = { Messages.getText("text") };
981
                        lc.setLabelExpressions(sampleExpression);
982

    
983
                        lc.getTextSymbol().setFontSize(16);
984

    
985
                        labeling1.setLabelingMethod(def_method);
986
                        labeling2.setLabelingMethod(def_method);
987
                        labeling1.setLayer(line_layer);
988
                        labeling2.setLayer(poly_layer);
989

    
990
                        line_layer.setLabelingStrategy(labeling1);
991
                        line_layer.setIsLabeled(true);
992
                        poly_layer.setLabelingStrategy(labeling2);
993
                        poly_layer.setIsLabeled(true);
994
                        
995
                        ViewPort theViewPort = new ViewPort(CRSFactory.getCRS("EPSG:23030"));
996
                        theMapContext = new MapContext(theViewPort);
997
                        
998
                        if (img_size != null) {
999
                                theViewPort.setImageSize(img_size);
1000
                        } else {
1001
                                theViewPort.setImageSize(new Dimension(200, 200));
1002
                        }
1003
                        theViewPort.setEnvelope(ini_env);
1004
                        
1005
                        theMapContext.getLayers().addLayer(bgpoly_layer);
1006
                        theMapContext.getLayers().addLayer(poly_layer);
1007
                        theMapContext.getLayers().addLayer(line_layer);
1008
                } else {
1009
                        
1010
                        if (img_size != null) {
1011
                                theMapContext.getViewPort().setImageSize(img_size);
1012
                        } else {
1013
                                theMapContext.getViewPort().setImageSize(new Dimension(200, 200));
1014
                        }
1015
                        theMapContext.getViewPort().setEnvelope(ini_env);
1016
                }
1017

    
1018
                line_layer.getLabelingStrategy().setPlacementConstraints(placement);
1019
                poly_layer.getLabelingStrategy().setPlacementConstraints(placement);
1020
                Dimension sz = getBounds().getSize();
1021
                sz.setSize(sz.width-2*hMargin, sz.height-2*vMargin);
1022
                theMapContext.getViewPort().setImageSize(sz);
1023
                theMapContext.getViewPort().setBackColor(new Color(255, 0, 0));
1024
                theMapContext.invalidate();
1025
                return theMapContext;
1026
        }
1027

    
1028
        @Override
1029
        protected void paintComponent(Graphics g) {
1030
                try {
1031
                        Rectangle bounds = getBounds();
1032
                        Dimension sz = bounds.getSize();
1033
                        sz.setSize(sz.width-2*vMargin, sz.height-2*hMargin);
1034
                        Dimension imageSize = sz;
1035
                        
1036
                        MapContext mco = getMapContext(imageSize);
1037
                        if (this.geotype == TYPES.CURVE) {
1038
                                line_layer.setVisible(true);
1039
                                poly_layer.setVisible(false);
1040
                        } else {
1041
                                if (this.geotype == TYPES.SURFACE) {
1042
                                        line_layer.setVisible(false);
1043
                                        poly_layer.setVisible(true);
1044
                                }
1045
                        }
1046
                        bgpoly_layer.setVisible(line_layer.isVisible());
1047

    
1048
                        BufferedImage bi = new BufferedImage(imageSize.width, imageSize.height, BufferedImage.TYPE_4BYTE_ABGR);
1049
                        mco.invalidate();
1050
                        mco.draw(bi, bi.createGraphics(), mco.getScaleView());
1051
                        g.setColor(new Color(150,180,255));
1052
                        g.fillRect(vMargin, hMargin, bounds.width-2*hMargin, bounds.height-2*vMargin);
1053
                        g.drawImage(bi, vMargin, vMargin, null);
1054
                        bi = null;
1055
                } catch (Exception e) {
1056
                        
1057
                        minilogger.error("While painting.", e);
1058
                        String noneSelected = "["+Messages.getText("preview_not_available")+"]";
1059
                        int vGap = 5, hGap = 5;
1060
                        Rectangle r = getBounds();
1061
                        FontMetrics fm = g.getFontMetrics();
1062
                        int lineWidth = fm.stringWidth(noneSelected);
1063
                        float scale = (float) r.getWidth() / lineWidth;
1064
                        Font f = g.getFont();
1065
                        float fontSize = f.getSize()*scale;
1066
                        g.setFont(        f.deriveFont( fontSize ) );
1067
                        ((Graphics2D) g).drawString(noneSelected, (r.x*scale) - (hGap/2), r.height/2+vGap*scale);
1068
                }
1069

    
1070
        }
1071
        
1072
        private FLyrVect createLayerFromShpDocFile(
1073
                        String shp_file_name, String epsg_code) throws Exception {
1074
                
1075
                File shpfile = getDocFolderFile(shp_file_name);
1076
                IProjection proj = CRSFactory.getCRS(epsg_code);
1077
                SHPStoreParameters ssp = (SHPStoreParameters)
1078
                                DALLocator.getDataManager().createStoreParameters(
1079
                                SHPStoreProvider.NAME);
1080
                ssp.setSHPFile(shpfile);
1081
                ssp.setCRS(proj);
1082
                FLyrVect resp = null;
1083
                resp = (FLyrVect) MapContextLocator.getMapContextManager().createLayer(
1084
                                shp_file_name, ssp);
1085
                return resp;
1086
        }
1087
        
1088
        private File getDocFolderFile(String name) {
1089
                
1090
                URL resource = this.getClass().getClassLoader().getResource(
1091
                                "docs" + File.separator + name);
1092
                File resp = new File(resource.getFile());
1093
                return resp;
1094
        }
1095
}