Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolSelector.java @ 23502

History | View | Annotate | Download (35.2 KB)

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

    
46
import java.awt.BorderLayout;
47
import java.awt.Color;
48
import java.awt.Component;
49
import java.awt.Dimension;
50
import java.awt.FlowLayout;
51
import java.awt.Font;
52
import java.awt.GridLayout;
53
import java.awt.Point;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56
import java.awt.event.InputEvent;
57
import java.awt.event.MouseEvent;
58
import java.awt.event.MouseListener;
59
import java.awt.event.MouseMotionListener;
60
import java.io.File;
61

    
62
import javax.swing.BorderFactory;
63
import javax.swing.BoxLayout;
64
import javax.swing.JComponent;
65
import javax.swing.JLabel;
66
import javax.swing.JList;
67
import javax.swing.JPanel;
68
import javax.swing.JScrollPane;
69
import javax.swing.JSplitPane;
70
import javax.swing.JTextField;
71
import javax.swing.JToggleButton;
72
import javax.swing.ListCellRenderer;
73
import javax.swing.event.ListSelectionEvent;
74
import javax.swing.event.ListSelectionListener;
75
import javax.swing.event.TreeSelectionListener;
76
import javax.swing.tree.DefaultMutableTreeNode;
77

    
78
import org.gvsig.gui.beans.AcceptCancelPanel;
79
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
80
import org.gvsig.gui.beans.swing.JButton;
81
import org.gvsig.gui.beans.swing.JComboBoxFontSizes;
82
import org.gvsig.gui.beans.swing.JComboBoxFonts;
83
import org.gvsig.gui.beans.swing.JFileChooser;
84
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
85

    
86
import com.iver.andami.PluginServices;
87
import com.iver.andami.ui.mdiManager.WindowInfo;
88
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
89
import com.iver.cit.gvsig.fmap.core.FPoint2D;
90
import com.iver.cit.gvsig.fmap.core.FShape;
91
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
92
import com.iver.cit.gvsig.fmap.core.IGeometry;
93
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
94
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
95
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
96
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
97
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
98
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
99
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
100
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
101
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
102
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerLineSymbol;
103
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerMarkerSymbol;
104
import com.iver.cit.gvsig.gui.JComboBoxUnits;
105
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
106
import com.iver.cit.gvsig.project.documents.view.legend.gui.ISymbolSelector;
107

    
108
/**
109
 * Creates the panel where the user has the options to select a symbol.
110
 * Apart from the option to select one, the user will have a previsualization
111
 * of all the symbols stored and posibilities to modify an existing one, to create
112
 * a new symbol and so on.
113
 *
114
 * @author jaume dominguez faus - jaume.dominguez@iver.es
115
 */
116
public class SymbolSelector extends JPanel implements ISymbolSelector, ActionListener {
117

    
118
        private static final long serialVersionUID = -6405660392303659551L;
119
        private JPanel jPanel = null;
120
        protected JScrollPane jScrollPane = null;
121
        private JScrollPane jScrollPane1 = null;
122
        private WindowInfo wi;
123
        private JSplitPane jSplitPane = null;
124
        protected AcceptCancelPanel okCancelPanel;
125
        private JPanel northPanel;
126
        private ColorChooserPanel jcc1;
127
        private ColorChooserPanel jcc2;
128
        private JIncrementalNumberField txtSize;
129
        private JIncrementalNumberField txtAngle;
130
        private JPanel jPanelButtons;
131
        private JButton btnProperties;
132
        private int shapeType;
133
        private JButton btnSaveSymbol;
134
        private JButton btnResetSymbol;
135
        private JButton btnNewSymbol;
136
        private JComboBoxFonts cmbFonts;
137
        private JToggleButton btnBold;
138
        private JToggleButton btnItalic;
139
        private JToggleButton btnUnderlined;
140
        protected JLabel lblTitle;
141
        protected File dir;
142
        protected File rootDir;
143
        protected JComponent jPanelPreview = null;
144
        protected GridBagLayoutPanel jPanelOptions = null;
145
        protected JList jListSymbols = null;
146
        protected String treeRootName;
147
        protected ILibraryModel library;
148
        private JIncrementalNumberField txtWidth;
149
        protected boolean act = true;
150
        boolean accepted = true;
151

    
152

    
153
        protected SelectorFilter sFilter = new SelectorFilter() {
154
                private final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D());
155
                private final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX());
156
                private final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX());
157

    
158
                public boolean accepts(Object obj) {
159
                        if (obj instanceof ISymbol) {
160
                                ISymbol sym = (ISymbol) obj;
161

    
162
                                IGeometry compareGeometry = null;
163
                                switch (SymbolSelector.this.shapeType) {
164
                                case FShape.TEXT:
165
                                        return sym instanceof ITextSymbol;
166
                                case FShape.POINT:
167
                                        compareGeometry = dummyPointGeom;
168
                                        break;
169
                                case FShape.LINE:
170
                                        compareGeometry = dummyLineGeom;
171
                                        break;
172
                                case FShape.POLYGON:
173
                                        compareGeometry = dummyPolygonGeom;
174
                                        break;
175
                                }
176
                                return sym.isSuitableFor(compareGeometry);
177
                        }
178
                        return false;
179
                }
180
        };
181
        protected JComboBoxUnits cmbUnits;
182
        protected JComboBoxUnitsReferenceSystem cmbReferenceSystem;
183
        private JComboBoxFontSizes cmbFontSize;
184
        protected LibraryBrowser libraryBrowser;
185
        /**
186
         * Constructor method
187
         *
188
         * @param currentElement
189
         * @param shapeType
190
         */
191
        private SymbolSelector(Object currentElement, int shapeType, boolean initialize) {
192
                super();
193

    
194
                // TODO  09/08/07 check the currentElement type is suitable for the shapeType specified
195
                if (currentElement != null && currentElement instanceof ISymbol) {
196
                        ISymbol sym = (ISymbol) currentElement;
197
                        currentElement = SymbologyFactory.createSymbolFromXML(
198
                                        sym.getXMLEntity(), "");
199
                        String desc = sym.getDescription();
200
//                        desc = " ("+PluginServices.getText(this, "current")+")";
201
//                        ((ISymbol)currentElement).setDescription(desc);
202
                }
203

    
204
                // for symbols MULTIPOINT is the same than POINT
205
                if (shapeType == FShape.MULTIPOINT) shapeType = FShape.POINT;
206

    
207
                this.shapeType = shapeType;
208

    
209
//                Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
210
                rootDir = new File(SymbologyFactory.SymbolLibraryPath);
211
//                prefs.get("SymbolLibraryFolder", System.getProperty("user.home")+"/gvSIG/Symbols"));
212
                if (!rootDir.exists())
213
                        rootDir.mkdir();
214
                treeRootName = PluginServices.getText(this, "symbol_library");
215
                try {
216
                        if (initialize)
217
                                initialize(currentElement);
218
                } catch (ClassNotFoundException e) {
219
                        throw new Error(e);
220
                }
221

    
222

    
223
        }
224

    
225
        /**
226
         * Constructor method, it is <b>protected</b> by convenience to let StyleSelector
227
         * to invoke it, but rigorously it should be <b>private</b>.
228
         *
229
         * @param symbol
230
         * @param shapeType
231
         * @param filter
232
         */
233
        protected SymbolSelector(Object symbol, int shapeType, SelectorFilter filter, boolean initialize) {
234
                this(symbol, shapeType, initialize);
235
                sFilter = filter;
236
        }
237

    
238
        /**
239
         * This method initializes this
240
         * @param currentElement
241
         * @throws ClassNotFoundException
242
         *
243
         */
244
        protected void initialize(Object currentElement) throws ClassNotFoundException {
245
                library = new SymbolLibrary(rootDir);
246

    
247
                this.setLayout(new BorderLayout());
248
                this.setSize(400, 221);
249

    
250
                this.add(getJNorthPanel(), BorderLayout.NORTH);
251
                this.add(getJSplitPane(), BorderLayout.CENTER);
252
                this.add(getJEastPanel(), BorderLayout.EAST);
253
                ActionListener okAction = new ActionListener() {
254
                        public void actionPerformed(ActionEvent e) {
255
                                PluginServices.getMDIManager().closeWindow(SymbolSelector.this);
256
                        }
257
                }, cancelAction = new ActionListener() {
258
                        public void actionPerformed(ActionEvent e) {
259
                                accepted = false;
260

    
261
                                setSymbol(null);
262
                                PluginServices.getMDIManager().closeWindow(SymbolSelector.this);
263
                        }
264
                };
265

    
266
                okCancelPanel = new AcceptCancelPanel();
267
                okCancelPanel.setOkButtonActionListener(okAction);
268
                okCancelPanel.setCancelButtonActionListener(cancelAction);
269

    
270
                this.add(okCancelPanel, BorderLayout.SOUTH);
271
                libraryBrowser.setSelectionRow(0);
272

    
273
                SillyDragNDropAction dndAction = new SillyDragNDropAction();
274
                libraryBrowser.addMouseListener(dndAction);
275
                libraryBrowser.addMouseMotionListener(dndAction);
276
                getJListSymbols().addMouseListener(dndAction);
277
                getJListSymbols().addMouseMotionListener(dndAction);
278
                setSymbol(currentElement);
279
        }
280

    
281
        /**
282
         * Creates a new symbol selector list model in order to allow the user
283
         * to select an existing symbol previously created.
284
         *
285
         * @return listModel SymbolSelectorListModel
286
         */
287
        protected SymbolSelectorListModel newListModel() {
288
                SymbolSelectorListModel listModel = new SymbolSelectorListModel(
289
                                dir,
290
                                sFilter,
291
                                SymbolLibrary.SYMBOL_FILE_EXTENSION);
292
                return listModel;
293
        }
294
        /**
295
         * Initializes tha JNorthPanel.
296
         *
297
         * @return northPanel JPanel
298
         * @throws IllegalArgumentException
299
         */
300
        protected JPanel getJNorthPanel() throws IllegalArgumentException {
301
                if (northPanel == null) {
302
                        String text = "";
303
                        switch (shapeType) {
304
                        case FShape.POINT:
305
                                text = PluginServices.getText(this, "point_symbols");
306
                                break;
307
                        case FShape.LINE:
308
                                text = PluginServices.getText(this, "line_symbols");
309
                                break;
310
                        case FShape.POLYGON:
311
                                text = PluginServices.getText(this, "polygon_symbols");
312
                                break;
313
                        case FShape.TEXT:
314
                                text = PluginServices.getText(this, "text_symbols");
315
                                break;
316
                        default:
317
                                throw new IllegalArgumentException(PluginServices.getText(this, "shape_type_not_yet_supported"));
318
                        }
319
                        northPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
320
                        lblTitle = new JLabel(text);
321
                        lblTitle.setFont(lblTitle.getFont().deriveFont(Font.BOLD));
322
                        northPanel.add(lblTitle);
323
                }
324
                return northPanel;
325
        }
326

    
327
        /**
328
         * This method initializes jList
329
         *
330
         * @return javax.swing.JList
331
         */
332
        protected JList getJListSymbols() {
333
                if (jListSymbols == null) {
334
                        jListSymbols = new JList() ;
335
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
336
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
337
                        jListSymbols.setVisibleRowCount(-1);
338
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
339
                                public void valueChanged(ListSelectionEvent e) {
340
                                        if (jListSymbols.getSelectedValue()!=null) {
341
                                                ISymbol selSym = SymbologyFactory.createSymbolFromXML(
342
                                                                ((ISymbol) jListSymbols.getSelectedValue()).getXMLEntity(), null);
343
                                                setSymbol(selSym);
344
                                                updateOptionsPanel();
345
                                        }
346
                                }
347
                        });
348
                        ListCellRenderer renderer = new ListCellRenderer() {
349
                                private Color mySelectedBGColor = new Color(255,145,100,255);
350
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
351
                                        ISymbol sym = (ISymbol) value;
352
                                        JPanel pnl = new JPanel();
353
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
354
                                        pnl.setLayout(layout);
355
                                        Color bgColor = (isSelected) ? mySelectedBGColor
356
                                                        : getJListSymbols().getBackground();
357

    
358
                                        pnl.setBackground(bgColor);
359
                                        SymbolPreviewer sp = new SymbolPreviewer();
360
                                        sp.setAlignmentX(Component.CENTER_ALIGNMENT);
361
                                        sp.setPreferredSize(new Dimension(50, 50));
362
                                        sp.setSymbol(sym);
363
                                        sp.setBackground(bgColor);
364
                                        pnl.add(sp);
365
                                        String desc = sym.getDescription();
366
                                        if (desc == null) {
367
                                                desc = "["+PluginServices.getText(this, "no_desc")+"]";
368
                                        }
369
                                        JLabel lbl = new JLabel(desc);
370
                                        lbl.setBackground(bgColor);
371
                                        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
372
                                        pnl.add(lbl);
373

    
374
                                        return pnl;
375
                                }
376

    
377
                        };
378
                        jListSymbols.setCellRenderer(renderer);
379
                }
380
                return jListSymbols;
381
        }
382
        /**
383
         * Updates the options panel depending on the type of symbol that the user
384
         * is controlling or using to show specific options for each one.
385
         *
386
         */
387
        protected void updateOptionsPanel() throws IllegalArgumentException {
388
                Object mySelectedElement = ((SymbolPreviewer) jPanelPreview).getSymbol();
389

    
390
                if (mySelectedElement == null) return;
391
                act = false; // disable events
392

    
393
                if (mySelectedElement instanceof CartographicSupport) {
394
                        CartographicSupport cs = (CartographicSupport) mySelectedElement;
395
                        cmbUnits.setSelectedUnitIndex(cs.getUnit());
396
                        cmbReferenceSystem.setSelectedIndex(cs.getReferenceSystem());
397
                }
398

    
399
                if (mySelectedElement instanceof IMultiLayerSymbol){
400
                        if (((IMultiLayerSymbol)mySelectedElement).getLayerCount() == 1)
401
                                mySelectedElement = ((IMultiLayerSymbol)mySelectedElement).getLayer(0);
402
                }
403

    
404

    
405
                try {
406

    
407
                        jcc1.setEnabled(true);
408
                        jcc2.setEnabled(true);
409

    
410
                        if(mySelectedElement instanceof IMultiLayerSymbol){
411
                                jcc1.setColor(Color.WHITE);
412
                                jcc2.setColor(Color.WHITE);
413
                                jcc1.setEnabled(false);
414
                                jcc2.setEnabled(false);
415
                        }
416

    
417

    
418
                        if (shapeType == FShape.POINT) {
419
                                IMarkerSymbol m = (IMarkerSymbol) mySelectedElement;
420
                                jcc1.setColor(m.getColor());
421
                                txtSize.setDouble(m.getSize());
422
                                txtAngle.setDouble(m.getRotation());
423
                        }
424

    
425
                        if (shapeType == FShape.LINE) {
426
                                ILineSymbol l = (ILineSymbol) mySelectedElement;
427
                                jcc1.setColor(l.getColor());
428
                                jcc1.setAlpha(l.getAlpha());
429
                                txtSize.setDouble(l.getLineWidth());
430
                        }
431

    
432
                        if (shapeType == FShape.POLYGON) {
433
                                IFillSymbol f = (IFillSymbol) mySelectedElement;
434

    
435
                                txtWidth.setEnabled(true);
436
                                cmbReferenceSystem.setEnabled(true);
437
                                cmbUnits.setEnabled(true);
438

    
439
                                jcc1.setUseColorIsSelected(f.hasFill());
440
                                jcc1.setColor(f.getFillColor());
441
                                jcc1.setAlpha(f.getFillAlpha());
442
                                jcc2.setUseColorIsSelected(f.hasOutline());
443
                                ILineSymbol outline = f.getOutline();
444
                                if (outline != null) {
445
                                        jcc2.setColor(outline.getColor());
446
                                        txtWidth.setDouble(outline.getLineWidth());
447
                                }
448

    
449
                                if(f instanceof MultiLayerFillSymbol){
450
                                        txtWidth.setEnabled(false);
451
                                        cmbReferenceSystem.setEnabled(false);
452
                                        cmbUnits.setEnabled(false);
453
                                }
454
                        }
455

    
456

    
457
                        if (shapeType == FShape.TEXT) {
458
                                ITextSymbol t = (ITextSymbol) mySelectedElement;
459
                                jcc1.setColor(t.getTextColor());
460
                                Double s = new Double(t.getFont().getSize());
461
                                cmbFontSize.setSelectedItem(s);
462
                                int i = cmbFontSize.getSelectedIndex();
463
                                if (i == -1) {
464
                                        cmbFontSize.addItem(s);
465
                                        cmbFontSize.setSelectedItem(s);
466
                                }
467
                        }
468
                } catch (NullPointerException npEx) {
469
                        throw new IllegalArgumentException(npEx);
470
                } catch (ClassCastException ccEx) {
471
                        throw new IllegalArgumentException(ccEx);
472
                }
473

    
474
                act = true;  // enable events
475
        }
476

    
477
        /**
478
         * This method initializes jPanel
479
         *
480
         * @return javax.swing.JPanel
481
         */
482
        protected JPanel getJEastPanel() {
483
                if (jPanel == null) {
484
                        jPanel = new JPanel();
485
                        jPanel.setLayout(new BorderLayout());
486
                        jPanel.add(getJPanelOptions(), BorderLayout.CENTER);
487
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
488
                        aux.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "preview")));
489
                        aux.add(getJPanelPreview());
490
                        jPanel.add(aux, BorderLayout.NORTH);
491

    
492
                        jPanel.add(getJPanelOptions());
493
                        aux = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
494
                        aux.add(getJPanelButtons());
495
                        jPanel.add(aux, BorderLayout.SOUTH);
496
                }
497
                return jPanel;
498
        }
499

    
500
        private JPanel getJPanelButtons() {
501
                if (jPanelButtons == null) {
502
                        jPanelButtons = new JPanel();
503
                        GridLayout layout = new GridLayout();
504
                        layout.setColumns(1);
505
                        layout.setVgap(5);
506
                        jPanelButtons.add(getBtnNewSymbol());
507
                        jPanelButtons.add(getBtnSaveSymbol());
508
                        jPanelButtons.add(getBtnResetSymbol());
509
                        jPanelButtons.add(getBtnProperties());
510

    
511
                        // do not add components bellow this line!
512
                        layout.setRows(jPanelButtons.getComponentCount());
513
                        jPanelButtons.setLayout(layout);
514
                }
515
                return jPanelButtons;
516
        }
517

    
518
        private JButton getBtnNewSymbol() {
519
                if (btnNewSymbol == null) {
520
                        btnNewSymbol = new JButton();
521
                        btnNewSymbol.setName("btnNewSymbol");
522
                        btnNewSymbol.setText(PluginServices.getText(this, "new"));
523
                        btnNewSymbol.addActionListener(this);
524
                }
525
                return btnNewSymbol;
526
        }
527

    
528
        private JButton getBtnResetSymbol() {
529
                if (btnResetSymbol == null) {
530
                        btnResetSymbol = new JButton();
531
                        btnResetSymbol.setName("btnResetSymbol");
532
                        btnResetSymbol.setText(PluginServices.getText(this, "reset"));
533
                        btnResetSymbol.addActionListener(this);
534
                }
535
                return btnResetSymbol;
536
        }
537

    
538
        private JButton getBtnSaveSymbol() {
539
                if (btnSaveSymbol == null) {
540
                        btnSaveSymbol = new JButton();
541
                        btnSaveSymbol.setName("btnSaveSymbol");
542
                        btnSaveSymbol.setText(PluginServices.getText(this, "save"));
543
                        btnSaveSymbol.addActionListener(this);
544
                }
545
                return btnSaveSymbol;
546
        }
547

    
548
        private JButton getBtnProperties() {
549
                if (btnProperties == null) {
550
                        btnProperties = new JButton();
551
                        btnProperties.setName("btnProperties");
552
                        btnProperties.setText(PluginServices.getText(this, "properties"));
553
                        btnProperties.addActionListener(this);
554
                }
555
                return btnProperties;
556
        }
557

    
558
        /**
559
         * This method initializes jScrollPane
560
         *
561
         * @return javax.swing.JScrollPane
562
         * @throws ClassNotFoundException
563
         */
564
        protected JScrollPane getLeftJScrollPane() throws ClassNotFoundException {
565
                if (jScrollPane == null) {
566
                        jScrollPane = new JScrollPane();
567
                        jScrollPane.setPreferredSize(new java.awt.Dimension(80,130));
568
                        jScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
569
                        libraryBrowser = new LibraryBrowser(library);
570
                        libraryBrowser.addTreeSelectionListener(new TreeSelectionListener() {
571
                                public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
572
                                        dir = (File) ((DefaultMutableTreeNode)
573
                                                        libraryBrowser.getLastSelectedPathComponent()).getUserObject();
574

    
575
                                        if (dir == null) return;
576

    
577
                                        jListSymbols.setModel(newListModel());
578
//                                        jListSymbols.setSelectedValue(selectedElement, true);
579
                                }
580
                        });
581
                        jScrollPane.setViewportView(libraryBrowser);
582
                }
583
                return jScrollPane;
584
        }
585

    
586
        /**
587
         * This method initializes jScrollPane1
588
         *
589
         * @return javax.swing.JScrollPane
590
         */
591
        private JScrollPane getJScrollPane1() {
592
                if (jScrollPane1 == null) {
593
                        jScrollPane1 = new JScrollPane();
594
                        jScrollPane1.setViewportView(getJListSymbols());
595
                }
596
                return jScrollPane1;
597
        }
598

    
599
        /**
600
         * This method initializes jPanelPreview
601
         *
602
         * @return javax.swing.JComponent
603
         */
604
        protected JComponent getJPanelPreview() {
605
                if (jPanelPreview == null) {
606
                        jPanelPreview = new SymbolPreviewer();
607
                        jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
608
                        jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
609
                }
610
                return jPanelPreview;
611
        }
612
        /**
613
         * This method initializes jPanelOptions
614
         *
615
         * @return javax.swing.JPanel
616
         */
617
        protected JPanel getJPanelOptions() {
618
                if (jPanelOptions == null) {
619
                        jPanelOptions = new GridBagLayoutPanel();
620
                        jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
621
                        jcc2 = new ColorChooserPanel(true,true);
622
                        jcc2.setAlpha(255);
623
                        if (shapeType == FShape.POINT) {
624
                                jcc1 = new ColorChooserPanel(true);
625

    
626
                                jPanelOptions.addComponent(
627
                                                PluginServices.getText(this, "color")+":", jcc1);
628
                                jPanelOptions.addComponent(
629
                                                PluginServices.getText(this, "size")+":",
630
                                                txtSize = new JIncrementalNumberField(String.valueOf(3), 3, 0, Double.MAX_VALUE, 1));
631
                                jPanelOptions.addComponent(PluginServices.getText(this, "units")+":",
632
                                                cmbUnits = new JComboBoxUnits());
633
                                jPanelOptions.addComponent("",
634
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
635
                                jPanelOptions.addComponent(
636
                                                PluginServices.getText(this, "angle")+":",
637
                                                txtAngle = new JIncrementalNumberField());
638

    
639

    
640
                        } else if (shapeType == FShape.LINE) {
641
                                jcc1 = new ColorChooserPanel(true);
642
                                jPanelOptions.addComponent(
643
                                                PluginServices.getText(this, "color")+":", jcc1);
644
                                jPanelOptions.addComponent(
645
                                                PluginServices.getText(this, "width")+":",
646
                                                txtSize = new JIncrementalNumberField(String.valueOf(3), 3, 0, Double.MAX_VALUE, 1));
647
                                jPanelOptions.addComponent(PluginServices.getText(this, "units")+":",
648
                                                cmbUnits = new JComboBoxUnits());
649
                                jPanelOptions.addComponent("",
650
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
651

    
652
                        } else if (shapeType == FShape.POLYGON) {
653
                                jcc1 = new ColorChooserPanel(true, true);
654
                                jPanelOptions.addComponent(
655
                                                PluginServices.getText(this, "fill_color")+":", jcc1);
656
                                jPanelOptions.addComponent(
657
                                                PluginServices.getText(this, "outline_color")+":", jcc2);
658
                                jPanelOptions.addComponent(
659
                                                PluginServices.getText(this, "outline_width"),
660
                                                txtWidth = new JIncrementalNumberField(String.valueOf(3), 3, 0, Double.MAX_VALUE, 1));
661
                                jPanelOptions.addComponent(PluginServices.getText(this, "units")+":",
662
                                                cmbUnits = new JComboBoxUnits());
663
                                jPanelOptions.addComponent("",
664
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
665

    
666
                        } else if (shapeType == FShape.TEXT) {
667
                                jcc1 = new ColorChooserPanel(true);
668
                                jPanelOptions.addComponent(
669
                                                PluginServices.getText(this, "font")+":", getCmbFonts());
670

    
671
                                jPanelOptions.addComponent(
672
                                                PluginServices.getText(this, "color")+":", jcc1);
673
                                jPanelOptions.addComponent(PluginServices.getText(this, "size")+":",
674
                                                cmbFontSize = new JComboBoxFontSizes());
675
                                jPanelOptions.addComponent(PluginServices.getText(this, "units")+":",
676
                                                cmbUnits = new JComboBoxUnits());
677
                                jPanelOptions.addComponent("",
678
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
679

    
680
                                JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING,0,1));
681
                                aux.add(getBtnBold());
682
                                aux.add(getBtnItalic());
683
                                aux.add(getBtnUnderlined());
684
                                jPanelOptions.addComponent(
685
                                                PluginServices.getText(this, "style")+":", aux);
686

    
687
                        }
688

    
689
                        jcc1.setAlpha(255);
690

    
691
                        if (txtSize != null)                        txtSize.addActionListener(this);
692
                        if (cmbUnits != null)                        cmbUnits.addActionListener(this);
693
                        if (cmbReferenceSystem != null)        cmbReferenceSystem.addActionListener(this);
694
                        if (jcc1 != null)                                jcc1.addActionListener(this);
695
                        if (jcc2 != null)                                jcc2.addActionListener(this);
696
                        if (txtWidth != null)                        txtWidth.addActionListener(this);
697
                        if (cmbFontSize != null)                cmbFontSize.addActionListener(this);
698
                        if (txtAngle != null)                        txtAngle.addActionListener(this);
699
                }
700
                return jPanelOptions;
701
        }
702

    
703
        private JToggleButton getBtnUnderlined() {
704
                if (btnUnderlined == null) {
705
                        btnUnderlined = new JToggleButton(PluginServices.getIconTheme().
706
                                        get("underline-icon"));
707
                }
708
                return btnUnderlined;
709
        }
710

    
711
        private JToggleButton getBtnItalic() {
712
                if (btnItalic == null) {
713
                        btnItalic = new JToggleButton(PluginServices.getIconTheme().
714
                                        get("italic-icon"));
715
                }
716
                return btnItalic;
717
        }
718

    
719
        private JToggleButton getBtnBold() {
720
                if (btnBold == null) {
721
                        btnBold = new JToggleButton(PluginServices.getIconTheme().
722
                                        get("bold-icon"));
723
                }
724
                return btnBold;
725
        }
726

    
727

    
728
        private JComboBoxFonts getCmbFonts() {
729
                if (cmbFonts == null) {
730
                        cmbFonts = new JComboBoxFonts();
731
                }
732
                return cmbFonts;
733
        }
734

    
735
        public WindowInfo getWindowInfo() {
736
                if (wi == null) {
737
                        wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
738
                        wi.setWidth(706);
739
                        wi.setHeight(500);
740
                        wi.setTitle(PluginServices.getText(this, "symbol_selector"));
741
                }
742
                return wi;
743
        }
744

    
745
        protected JSplitPane getJSplitPane() throws ClassNotFoundException {
746
                if (jSplitPane == null) {
747
                        jSplitPane = new JSplitPane();
748
                        jSplitPane.setDividerLocation(200);
749
                        jSplitPane.setResizeWeight(0.4);
750
                        jSplitPane.setLeftComponent(getLeftJScrollPane());
751
                        jSplitPane.setRightComponent(getJScrollPane1());
752
                }
753
                return jSplitPane;
754
        }
755

    
756
        public Object getSelectedObject() {
757
                if (!accepted) return null;
758
                Object mySelectedElement = ((SymbolPreviewer) jPanelPreview).getSymbol();
759

    
760
                // if this symbol only has one layer, then no multilayer is needed
761
                if (mySelectedElement instanceof IMultiLayerSymbol) {
762
                        if (((IMultiLayerSymbol) mySelectedElement).getLayerCount()==1)
763
                                return ((IMultiLayerSymbol) mySelectedElement).getLayer(0);
764
                }
765

    
766
                if (mySelectedElement instanceof CartographicSupport) {
767
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
768
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
769
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
770
                }
771

    
772
                return mySelectedElement;
773
        }
774

    
775
        public void setSymbol(Object symbol) {
776
                ((SymbolPreviewer) jPanelPreview).setSymbol((ISymbol) symbol);
777
                updateOptionsPanel();
778
        }
779

    
780
        /**
781
         * Invoked when the PROPERTIES button is pressed
782
         */
783
        protected void propertiesPressed() {
784
                ISymbol mySelectedElement = ((SymbolPreviewer) jPanelPreview).getSymbol();
785
                if (mySelectedElement ==null)
786
                        return;
787

    
788
                ISymbol clonedSymbol = SymbologyFactory.createSymbolFromXML(
789
                                mySelectedElement.getXMLEntity(), null);
790
                SymbolEditor se = new SymbolEditor(clonedSymbol, shapeType);
791
                PluginServices.getMDIManager().addWindow(se);
792

    
793
                ISymbol symbol = se.getSymbol();
794
                if (symbol instanceof IMultiLayerSymbol) {
795
                        IMultiLayerSymbol mSym = (IMultiLayerSymbol) symbol;
796
                        if (mSym.getLayerCount() == 1) {
797
                                symbol =  mSym.getLayer(0);
798
                        }
799
                }
800
                setSymbol(symbol);
801

    
802
        }
803

    
804
        /**
805
         * Invoked when the NEW button is pressed
806
         */
807
        protected void newPressed() {
808
                SymbolEditor se = new SymbolEditor(null, shapeType);
809
                PluginServices.getMDIManager().addWindow(se);
810
                setSymbol(se.getSymbol());
811
        }
812

    
813
        /**
814
         * Invoked when the RESET button is pressed
815
         */
816
        protected void resetPressed() {
817
                setSymbol(null);
818
        }
819

    
820
        /**
821
         * Invoked when the SAVE button is pressed
822
         */
823
        protected void savePressed() {
824
                if (getSelectedObject() ==null)
825
                        return;
826

    
827

    
828
                JFileChooser jfc = new JFileChooser("SYMBOL_SELECTOR_FILECHOOSER", rootDir);
829
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
830
                        public boolean accept(File f) {
831
                                return f.getAbsolutePath().
832
                                toLowerCase().
833
                                endsWith(SymbolLibrary.SYMBOL_FILE_EXTENSION) || f.isDirectory();
834
                        }
835

    
836
                        public String getDescription() {
837
                                return PluginServices.getText(
838
                                                this, "gvSIG_symbol_definition_file")+ " (*.sym)";
839
                        }
840
                };
841
                jfc.setFileFilter(ff);
842
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
843
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
844
                JTextField txtDesc = new JTextField(25);
845
                accessory.add(txtDesc);
846
                jfc.setAccessory(accessory);
847
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
848
                        File targetFile = jfc.getSelectedFile();
849

    
850
                        // apply description
851
                        String desc;
852
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
853
                                // default to file name
854
                                String s = targetFile.getAbsolutePath();
855
                                desc = s.substring(s.lastIndexOf(File.separator)+1).
856
                                replaceAll(SymbolLibrary.SYMBOL_FILE_EXTENSION, "");
857
                        } else {
858
                                desc = txtDesc.getText().trim();
859
                        }
860
                        ISymbol s = (ISymbol) getSelectedObject();
861
                        s.setDescription(desc);
862

    
863

    
864
                        getJListSymbols().setModel(newListModel());
865
//                        getJListSymbols().setSelectedValue(
866
//                        selectedElement, true);
867
                        String symbolFileName = targetFile.getAbsolutePath().substring(
868
                                        targetFile.getAbsolutePath().lastIndexOf(File.separator)+1,
869
                                        targetFile.getAbsolutePath().length());
870
                        File targetDir = new File(targetFile.getAbsolutePath().substring(
871
                                        0,
872
                                        targetFile.getAbsolutePath().lastIndexOf(File.separator)));
873
                        library.addElement(s, symbolFileName , targetDir);
874
                }
875
        }
876

    
877

    
878
        public void actionPerformed(ActionEvent e) {
879
                if (!act) return;
880
                Object selectedElement = ((SymbolPreviewer) jPanelPreview).getSymbol();//getSelectedObject();
881
                performActionOn(selectedElement, e);
882
                SymbolSelector.this.repaint();
883
        }
884

    
885
        protected void performActionOn(Object selectedElement, ActionEvent e) {
886
                JComponent comp = (JComponent) e.getSource();
887

    
888
                if (selectedElement instanceof IMultiLayerSymbol){
889
                        if (((IMultiLayerSymbol)selectedElement).getLayerCount() == 1)
890
                                selectedElement = ((IMultiLayerSymbol)selectedElement).getLayer(0);
891
                }
892

    
893
                if ( comp.equals(getBtnProperties()) ) {
894
                        // properties pressed
895
                        propertiesPressed();
896
                } else if ( comp.equals(getBtnNewSymbol()) ) {
897
                        // new pressed
898
                        newPressed();
899
                } else if ( comp.equals(getBtnResetSymbol()) ) {
900
                        // reset pressed
901
                        resetPressed();
902
                } else if ( comp.equals(getBtnSaveSymbol()) ) {
903
                        // save pressed
904
                        savePressed();
905
                } else if (comp.equals(jcc1)) {
906
                        if (selectedElement == null)
907
                                return;
908

    
909
                        Color c = jcc1.getColor();
910

    
911

    
912
                        if (selectedElement instanceof IMarkerSymbol) {
913
                                IMarkerSymbol m = (IMarkerSymbol) selectedElement;
914
                                if (m instanceof MultiLayerMarkerSymbol) {
915
                                        MultiLayerMarkerSymbol mm = (MultiLayerMarkerSymbol) m;
916
                                        mm.setAlpha(jcc1.getAlpha());
917
                                } else {
918
                                        m.setColor(c);
919
                                }
920
                        }
921

    
922
                        if (selectedElement instanceof ILineSymbol) {
923
                                ILineSymbol l = (ILineSymbol) selectedElement;
924
                                if (l instanceof MultiLayerLineSymbol) {
925
                                        MultiLayerLineSymbol ml = (MultiLayerLineSymbol) l;
926
                                        ml.setAlpha(jcc1.getAlpha());
927
                                } else {
928
                                        l.setLineColor(c);
929
                                }
930
                        }
931

    
932
                        if (selectedElement instanceof IFillSymbol) {
933
                                IFillSymbol f = (IFillSymbol) selectedElement;
934

    
935
                                f.setHasFill(jcc1.getUseColorisSelected());
936
                                f.setFillColor(c);
937
                        }
938

    
939
                        if (selectedElement instanceof ITextSymbol) {
940
                                ITextSymbol t = (ITextSymbol) selectedElement;
941
                                t.setTextColor(c);
942
                        }
943

    
944
                } else if (comp.equals(jcc2)) {
945
                        if (selectedElement == null)
946
                                return;
947
                        Color c = jcc2.getColor();
948

    
949

    
950
                        if (selectedElement instanceof IFillSymbol) {
951
                                IFillSymbol f = (IFillSymbol) selectedElement;
952
                                ILineSymbol outline = f.getOutline();
953
                                f.setHasOutline(jcc2.getUseColorisSelected());
954

    
955
                                if (outline!=null) {
956
                                        ILineSymbol l = (ILineSymbol) outline;
957
                                        if (l instanceof MultiLayerLineSymbol && c != null) {
958
                                                MultiLayerLineSymbol ml = (MultiLayerLineSymbol) l;
959
                                                ml.setAlpha(c.getAlpha());
960
                                        } else {
961
                                                l.setLineColor(c);
962
                                        }
963
                                }
964

    
965
                        }
966
                } else if (comp.equals(txtSize)) {
967
                        double s = txtSize.getDouble();
968

    
969
                        if (selectedElement instanceof IMarkerSymbol) {
970
                                IMarkerSymbol m = (IMarkerSymbol) selectedElement;
971
                                m.setSize(s);
972
                        }
973

    
974
                        if (selectedElement instanceof ILineSymbol) {
975
                                ILineSymbol l = (ILineSymbol) selectedElement;
976
                                l.setLineWidth(s);
977
                        }
978
                } else if (comp.equals(cmbUnits)) {
979
                        if (selectedElement instanceof CartographicSupport) {
980
                                CartographicSupport cs = (CartographicSupport) selectedElement;
981
                                cs.setUnit(cmbUnits.getSelectedUnitIndex());
982
                        }
983
                } else if (comp.equals(cmbReferenceSystem)) {
984
                        if (selectedElement instanceof CartographicSupport) {
985
                                CartographicSupport cs = (CartographicSupport) selectedElement;
986
                                cs.setUnit(cmbReferenceSystem.getSelectedIndex());
987
                        }
988
                } else if (comp.equals(txtWidth)) {
989
                        double w = txtWidth.getDouble();
990
                        if (selectedElement instanceof IFillSymbol) {
991
                                IFillSymbol f = (IFillSymbol) selectedElement;
992
                                ILineSymbol outline = f.getOutline();
993
                                if (outline!=null)
994
                                        outline.setLineWidth(w);
995
                        }
996
                } else if (comp.equals(cmbFontSize)) {
997
                        double s = ((Integer) cmbFontSize.getSelectedItem()).doubleValue();
998
                        if (selectedElement instanceof ITextSymbol) {
999
                                ITextSymbol t = (ITextSymbol) selectedElement;
1000
                                t.setFontSize(s);
1001
                        }
1002
                } else if (comp.equals(txtAngle)) {
1003
                        double a = Math.toRadians(txtAngle.getDouble());
1004
                        if (selectedElement instanceof IMarkerSymbol) {
1005
                                IMarkerSymbol m = (IMarkerSymbol) selectedElement;
1006
                                m.setRotation(a);
1007
                        }
1008
                }
1009
        }
1010

    
1011
        public static ISymbolSelector createSymbolSelector(Object currSymbol, int shapeType) {
1012
                return createSymbolSelector(currSymbol, shapeType, null);
1013
        }
1014

    
1015
        public static ISymbolSelector createSymbolSelector(Object currSymbol, int shapeType, SelectorFilter filter) {
1016
                ISymbolSelector selector = null;
1017

    
1018
                // patch for backwards compatibility
1019
//                if (currSymbol instanceof FSymbol) {
1020
//                currSymbol = SymbologyFactory.deriveFSymbol((FSymbol) currSymbol);
1021
//                }
1022

    
1023
                if (filter==null)
1024
                        selector = (shapeType == FShape.MULTI) ?
1025
                                        new MultiShapeSymbolSelector(currSymbol) :
1026
                                                new SymbolSelector(currSymbol, shapeType, true);
1027
                                        else
1028
                                                selector = (shapeType == FShape.MULTI) ?
1029
                                                                new MultiShapeSymbolSelector(currSymbol) :
1030
                                                                        new SymbolSelector(currSymbol, shapeType, filter, true);
1031
                                                                return selector;
1032
        }
1033

    
1034
        class SillyDragNDropAction implements MouseListener, MouseMotionListener {
1035
                private boolean doDrop = false;
1036
                private Object selected;
1037
                private File sourceFolder;
1038

    
1039
                public void mouseClicked(MouseEvent e) { }
1040
                public void mouseEntered(MouseEvent e) { }
1041
                public void mouseExited(MouseEvent e) { }
1042

    
1043
                public void mousePressed(MouseEvent e) {
1044
                        if (e.getSource().equals(getJListSymbols())) {
1045
                                selected = getJListSymbols().getSelectedValue();
1046
                                doDrop = selected!=null;
1047
                                DefaultMutableTreeNode node = (DefaultMutableTreeNode) libraryBrowser.getLastSelectedPathComponent();
1048
                                if (node.getUserObject() instanceof File) {
1049
                                        sourceFolder = (File) node.getUserObject();
1050
                                }
1051
                        }
1052
                        e.consume();
1053
                }
1054

    
1055
                public void mouseReleased(MouseEvent e) {
1056
                        if (doDrop && e.getSource().equals(getJListSymbols())) {
1057
                                Point p = new Point(getJListSymbols().getLocation().x-e.getPoint().x, getJListSymbols().getLocation().y+e.getPoint().y);
1058
                                if (libraryBrowser.getBounds().contains(p)) {
1059
                                        File destFolder = libraryBrowser.getElementBellow(p);
1060
                                        if (destFolder != null) {
1061
                                                ISymbol sym = (ISymbol) selected;
1062
                                                int move = InputEvent.SHIFT_DOWN_MASK | InputEvent.BUTTON1_DOWN_MASK;
1063
//                                                int copy = MouseEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;
1064

    
1065
                                                library.addElement(sym, sym.getDescription(), destFolder);
1066
                                                if ((e.getModifiers() & (move)) !=0) {
1067
                                                        library.removeElement(sym, sourceFolder);
1068
                                                }
1069

    
1070
                                        }
1071
                                        libraryBrowser.refresh();
1072
                                }
1073

    
1074
                        }
1075
                        doDrop = false;
1076
                }
1077

    
1078
                public void mouseDragged(MouseEvent e) {
1079
                        if (e.getSource().equals(getJListSymbols())) {
1080

    
1081
                                Point p = new Point(getJListSymbols().getLocation().x-e.getPoint().x, getJListSymbols().getLocation().y+e.getPoint().y);
1082
                                if (libraryBrowser.getBounds().contains(p)) {
1083
                                        libraryBrowser.setSelectedElementBellow(p);
1084
                                }
1085
                        }
1086
                }
1087

    
1088
                public void mouseMoved(MouseEvent e) {
1089

    
1090
                }
1091

    
1092
        }
1093

    
1094
}