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

History | View | Annotate | Download (33.9 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.DataStoreParameters;
112
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
113
import org.gvsig.fmap.geom.Geometry.TYPES;
114
import org.gvsig.fmap.geom.GeometryException;
115
import org.gvsig.fmap.geom.GeometryLocator;
116
import org.gvsig.fmap.geom.primitive.Envelope;
117
import org.gvsig.fmap.geom.type.GeometryType;
118
import org.gvsig.fmap.mapcontext.MapContext;
119
import org.gvsig.fmap.mapcontext.MapContextLocator;
120
import org.gvsig.fmap.mapcontext.MapContextManager;
121
import org.gvsig.fmap.mapcontext.ViewPort;
122
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
123
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend;
124
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
125
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
126
import org.gvsig.fmap.mapcontext.rendering.legend.styling.IPlacementConstraints;
127
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
128
import org.gvsig.gui.beans.AcceptCancelPanel;
129
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
130
import org.gvsig.gui.beans.swing.JBlank;
131
import org.gvsig.i18n.Messages;
132
import org.gvsig.labeling.label.GeneralLabelingStrategy;
133
import org.gvsig.labeling.lang.LabelClassUtils;
134
import org.gvsig.labeling.placements.MultiShapePlacementConstraints;
135
import org.gvsig.labeling.placements.PlacementManager;
136
import org.gvsig.labeling.placements.PointLabelPositioner;
137
import org.gvsig.labeling.placements.PointPlacementConstraints;
138
import org.gvsig.symbology.SymbologyLocator;
139
import org.gvsig.symbology.SymbologyManager;
140
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.ISimpleFillSymbol;
141
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ISimpleLineSymbol;
142
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IArrowDecoratorStyle;
143
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ISimpleLineStyle;
144
import org.slf4j.Logger;
145
import org.slf4j.LoggerFactory;
146

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

    
165
        public IPlacementConstraints constraints;
166

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

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

    
199

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

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

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

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

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

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

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

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

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

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

    
263

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

    
270
        private void initialize(
271
                        IPlacementConstraints constraints,
272
                        int shapeType, DuplicateLayersMode duplicatesMode) {
273

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

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

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

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

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

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

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

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

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

    
394
                return locationPanel;
395
        }
396

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

    
403
                return cmbLocationAlongLines;
404
        }
405

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

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

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

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

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

    
441
                return pointSettingsPanel;
442
        }
443

    
444

    
445

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

    
452
                return btnChangeLocation;
453
        }
454

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

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

    
469
                }
470

    
471
                return rdBtnOffsetLabelOnTopPoint;
472
        }
473

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

    
479
                }
480

    
481
                return rdBtnOffsetLabelHorizontally;
482
        }
483

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

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

    
503
                return polygonSettingsPanel;
504
        }
505

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

    
512
                }
513

    
514
                return chkFitInsidePolygon;
515
        }
516

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

    
524
                return rdBtnAlwaysStraight;
525
        }
526

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

    
534
                return rdBtnAlwaysHorizontal;
535
        }
536

    
537

    
538

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

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

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

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

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

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

    
584
                return cmbOrientationSystem;
585
        }
586

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

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

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

    
612
        private GridBagLayoutPanel getOrientationPanel() {
613
                if (orientationPanel == null) {
614
                        orientationPanel = new GridBagLayoutPanel();
615
                        orientationPanel.setBorder(BorderFactory.
616
                                        createTitledBorder(null,
617
                                                        Messages.getText("orientation")));
618
                        orientationPanel.addComponent(getRdBtnHorizontal());
619
                        orientationPanel.addComponent(getRdBtnParallel());
620
                        orientationPanel.addComponent(getRdBtnFollowingLine());
621
                        orientationPanel.addComponent(getRdBtnPerpendicular());
622
                        ButtonGroup group = new ButtonGroup();
623
                        group.add(getRdBtnHorizontal());
624
                        group.add(getRdBtnParallel());
625
                        group.add(getRdBtnFollowingLine());
626
                        group.add(getRdBtnPerpendicular());
627

    
628

    
629

    
630
                }
631
                return orientationPanel;
632
        }
633

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

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

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

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

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

    
671

    
672

    
673

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

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

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

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

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

    
745

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

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

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

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

    
808
        }
809

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

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

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

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

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

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

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

    
876

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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