Statistics
| Revision:

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

History | View | Annotate | Download (25.3 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.GraphicsEnvironment;
53
import java.awt.GridLayout;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56
import java.io.File;
57
import java.io.FileFilter;
58
import java.io.FileWriter;
59
import java.util.prefs.Preferences;
60

    
61
import javax.swing.BorderFactory;
62
import javax.swing.BoxLayout;
63
import javax.swing.ImageIcon;
64
import javax.swing.JComponent;
65
import javax.swing.JFileChooser;
66
import javax.swing.JLabel;
67
import javax.swing.JList;
68
import javax.swing.JOptionPane;
69
import javax.swing.JPanel;
70
import javax.swing.JScrollPane;
71
import javax.swing.JSplitPane;
72
import javax.swing.JTextField;
73
import javax.swing.JToggleButton;
74
import javax.swing.JTree;
75
import javax.swing.ListCellRenderer;
76
import javax.swing.event.ListSelectionEvent;
77
import javax.swing.event.ListSelectionListener;
78
import javax.swing.event.TreeModelListener;
79
import javax.swing.tree.TreeModel;
80
import javax.swing.tree.TreePath;
81

    
82
import org.exolab.castor.xml.Marshaller;
83
import org.gvsig.gui.beans.AcceptCancelPanel;
84
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
85
import org.gvsig.gui.beans.swing.JButton;
86

    
87
import com.iver.andami.PluginServices;
88
import com.iver.andami.messages.NotificationManager;
89
import com.iver.andami.ui.mdiManager.IWindow;
90
import com.iver.andami.ui.mdiManager.WindowInfo;
91
import com.iver.cit.gvsig.fmap.core.FPoint2D;
92
import com.iver.cit.gvsig.fmap.core.FShape;
93
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
94
import com.iver.cit.gvsig.fmap.core.IGeometry;
95
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
96
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
97
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
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.gui.IncrementalNumberField;
101
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
102
import com.iver.utiles.IPersistance;
103
import com.iver.utiles.StringUtilities;
104
import com.iver.utiles.XMLEntity;
105
import com.iver.utiles.swing.JComboBox;
106

    
107
/**
108
 * @author jaume dominguez faus - jaume.dominguez@iver.es
109
 */
110
public class SymbolSelector extends JPanel implements IWindow{
111

    
112
    private JTree treeFamilies = null;
113
    private JPanel jPanel = null;
114
    private JScrollPane jScrollPane = null;
115
    private JScrollPane jScrollPane1 = null;
116
    private WindowInfo wi;
117
        private JSplitPane jSplitPane = null;
118
        private AcceptCancelPanel okCancelPanel;
119
        private JPanel northPanel;
120
        private ColorChooserPanel jcc1;
121
        private ColorChooserPanel jcc2;
122
        private IncrementalNumberField txtSize;
123
        private IncrementalNumberField txtAngle;
124
        private JPanel jPanelButtons;
125
        private JButton btnProperties;
126
        private int shapeType;
127
        private JButton btnSaveSymbol;
128
        private JButton btnResetSymbol;
129
        private ActionListener buttonListener = new MyButtonListener();
130
        private JButton btnNewSymbol;
131
        private JComboBox cmbFonts;
132
        private JToggleButton btnBold;
133
        private JToggleButton btnItalic;
134
        private JToggleButton btnUnderlined;
135
        protected JLabel lblTitle;
136
        protected File dir;
137
        protected Object selectedElement = null;
138
        protected File rootDir;
139
        protected JComponent jPanelPreview = null;
140
    protected GridBagLayoutPanel jPanelOptions = null;
141
    protected JList jListSymbols = null;
142
    protected String treeRootName;
143
    protected TreeModel treeModel;
144
    private IncrementalNumberField txtWidth;
145

    
146
        protected SelectorFilter sFilter = new SelectorFilter() {
147
                private final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D());
148
                private final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX());
149
                private final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX());
150

    
151
                public boolean accepts(Object obj) {
152
                        if (obj instanceof ISymbol) {
153
                                ISymbol sym = (ISymbol) obj;
154

    
155
                                IGeometry compareGeometry = null;
156
                                switch (SymbolSelector.this.shapeType) {
157
                                case FShape.POINT:
158
                                        compareGeometry = dummyPointGeom;
159
                                        break;
160
                                case FShape.LINE:
161
                                        compareGeometry = dummyLineGeom;
162
                                        break;
163
                                case FShape.POLYGON:
164
                                        compareGeometry = dummyPolygonGeom;
165
                                        break;
166
                                }
167
                                return sym.isSuitableFor(compareGeometry);
168
                        }
169
                        return false;
170
                }
171
        };
172
        private JLineStyleComboBox cmbLineStyles;
173

    
174
        public SymbolSelector(Object currentElement, int shapeType) {
175
            super();
176
            if (currentElement != null && currentElement instanceof ISymbol) {
177
                    ISymbol sym = (ISymbol) currentElement;
178
                    currentElement = SymbologyFactory.createSymbolFromXML(
179
                                    sym.getXMLEntity(), "");
180
                    String desc = sym.getDescription();
181
                    desc += " ("+PluginServices.getText(this, "current")+")";
182
                    ((ISymbol)currentElement).setDescription(desc);
183
            }
184
            this.selectedElement = currentElement;
185
            this.shapeType = shapeType;
186
            Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
187
                rootDir = new File(prefs.get("SymbolLibraryFolder", System.getProperty("user.home")+"/gvSIG/Symbols"));
188
                if (!rootDir.exists())
189
                        rootDir.mkdir();
190
                treeRootName = PluginServices.getText(this, "symbol_library");
191
            initialize();
192
    }
193

    
194
    public SymbolSelector(Object symbol, int shapeType, SelectorFilter filter) {
195
            this(symbol, shapeType);
196
            sFilter = filter;
197
    }
198

    
199
    /**
200
     * This method initializes this
201
     *
202
     */
203
    private void initialize() {
204
            treeModel = new TreeModel() {
205
                    final class MyFile extends File {
206
                            public MyFile(String pathname) {
207
                                    super(pathname);
208
                            }
209

    
210
                            public String toString() {
211
                                    if (this.equals(root))
212
                                            return treeRootName;
213
                                    String path = getAbsolutePath();
214
                                    String prefixToRemove = rootDir.getAbsolutePath();
215
                                    path = path.substring(prefixToRemove.length()+1, path.length());
216
                                    return path;
217
                            }
218
                    };
219
                    MyFile root = new MyFile(rootDir.getAbsolutePath());
220

    
221
                    private FileFilter ff = new FileFilter() {
222
                            public boolean accept(File pathname) {
223
                                    return pathname.isDirectory();
224
                            }
225
                    };
226

    
227
                    public Object getRoot() {
228
                            return root;
229
                    }
230

    
231
                    public int getChildCount(Object parent) {
232
                            return ((File) parent).listFiles(ff).length;
233
                    }
234

    
235
                    public boolean isLeaf(Object node) {
236
                            return getChildCount(node)==0;
237
                    }
238

    
239
                    public void addTreeModelListener(TreeModelListener l) {
240
                            // TODO Necessite?
241

    
242
                    }
243

    
244
                    public void removeTreeModelListener(TreeModelListener l) {
245
                            // TODO Necessite?
246
                    }
247

    
248
                    public Object getChild(Object parent, int index) {
249
                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
250
                    }
251

    
252
                    public int getIndexOfChild(Object parent, Object child) {
253
                            if (parent == null)
254
                                    return -1;
255
                            File[] files = ((File) parent).listFiles(ff);
256
                            for (int i = 0; i < files.length; i++) {
257
                                    if (files[i].equals((File) child))
258
                                            return i;
259
                            }
260
                            return -1;
261
                    }
262

    
263
                    public void valueForPathChanged(TreePath path, Object newValue) {
264
                            // TODO Auto-generated method stub
265
                    }
266

    
267
            };
268

    
269
            this.setLayout(new BorderLayout());
270
            this.setSize(400, 221);
271

    
272
            this.add(getJNorthPanel(), BorderLayout.NORTH);
273
            this.add(getJSplitPane(), BorderLayout.CENTER);
274
            this.add(getJEastPanel(), BorderLayout.EAST);
275
            ActionListener okAction = new ActionListener() {
276
                    public void actionPerformed(ActionEvent e) {
277
                            PluginServices.getMDIManager().closeWindow(SymbolSelector.this);
278
                    }
279
            }, cancelAction = new ActionListener() {
280
                    public void actionPerformed(ActionEvent e) {
281
                            setSymbol(null);
282
                            PluginServices.getMDIManager().closeWindow(SymbolSelector.this);
283
                    }
284
            };
285

    
286
            okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
287

    
288

    
289
            this.add(okCancelPanel, BorderLayout.SOUTH);
290
    }
291

    
292
    protected SymbolSelectorListModel newListModel() {
293
            SymbolSelectorListModel listModel = new SymbolSelectorListModel(
294
                            dir,
295
                            (ISymbol) selectedElement,
296
                            sFilter,
297
                            SymbolSelectorListModel.SYMBOL_FILE_EXTENSION);
298
            return listModel;
299
    }
300

    
301
    private JPanel getJNorthPanel() throws IllegalArgumentException {
302
                if (northPanel == null) {
303
                        String text = "";
304
                        switch (shapeType) {
305
                        case FShape.POINT:
306
                                text = PluginServices.getText(this, "point_symbols");
307
                                break;
308
                        case FShape.LINE:
309
                                text = PluginServices.getText(this, "line_symbols");
310
                                break;
311
                        case FShape.POLYGON:
312
                                text = PluginServices.getText(this, "polygon_symbols");
313
                                break;
314
                        case FShape.TEXT:
315
                                text = PluginServices.getText(this, "text_symbols");
316
                                break;
317
                        default:
318
                                throw new IllegalArgumentException(PluginServices.getText(this, "shape_type_not_yet_supported"));
319
                        }
320
                        northPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
321
                        lblTitle = new JLabel(text);
322
                        lblTitle.setFont(lblTitle.getFont().deriveFont(Font.BOLD));
323
                        northPanel.add(lblTitle);
324
                }
325
                return northPanel;
326
        }
327

    
328

    
329
    protected JTree getTreeFav() {
330
            if (treeFamilies == null) {
331
                    treeFamilies = new JTree();
332
                    treeFamilies.setPreferredSize(new java.awt.Dimension(70,100));
333
                    try {
334
                            treeFamilies.setModel(treeModel);
335
                            treeFamilies.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
336
                                        public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
337
                                                dir = (File)
338
                                                treeFamilies.getLastSelectedPathComponent();
339

    
340
                                                if (dir == null) return;
341

    
342
                                                jListSymbols.setModel(newListModel());
343
                                        }
344
                                });
345

    
346
                    } catch ( ExceptionInInitializerError ex ) {
347
                            JOptionPane.showMessageDialog(this, PluginServices.getText(this, "could_not_find_symbol_directory"));
348
                    }
349
            }
350
            return treeFamilies;
351
    }
352

    
353
    /**
354
     * This method initializes jList
355
     *
356
     * @return javax.swing.JList
357
     */
358
    protected JList getJListSymbols() {
359
            if (jListSymbols == null) {
360
                    jListSymbols = new JList();
361
                    jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
362
            jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
363
            jListSymbols.setVisibleRowCount(-1);
364
            jListSymbols.addListSelectionListener(new ListSelectionListener() {
365
                    public void valueChanged(ListSelectionEvent e) {
366
                            setSymbol((ISymbol) jListSymbols.getSelectedValue());
367
                            updateOptionsPanel();
368
                    }
369
            });
370
            ListCellRenderer renderer = new ListCellRenderer() {
371
                        private Color mySelectedBGColor = new Color(255,145,100,255);
372
                            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
373
                                    ISymbol sym = (ISymbol) value;
374
                                    JPanel pnl = new JPanel();
375
                                    BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
376
                                    pnl.setLayout(layout);
377
                                    Color bgColor = (isSelected) ? mySelectedBGColor
378
                                                             : getJListSymbols().getBackground();
379

    
380
                                    pnl.setBackground(bgColor);
381
                                    SymbolPreviewer sp = new SymbolPreviewer();
382
                                    sp.setAlignmentX(Component.CENTER_ALIGNMENT);
383
                                    sp.setPreferredSize(new Dimension(50, 50));
384
                                    sp.setSymbol(sym);
385
                                    sp.setBackground(bgColor);
386
                                    pnl.add(sp);
387
                                    JLabel lbl = new JLabel(sym.getDescription());
388
                                    lbl.setBackground(bgColor);
389
                                    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
390
                                    pnl.add(lbl);
391

    
392
                                    return pnl;
393
                            }
394

    
395
                };
396
                jListSymbols.setCellRenderer(renderer);
397
            }
398
            return jListSymbols;
399
    }
400

    
401
    protected void updateOptionsPanel() {
402
            IPersistance sym = ((IPersistance) jListSymbols.getSelectedValue());
403
            if (sym == null)
404
                    return;
405

    
406
                XMLEntity xml = sym.getXMLEntity();
407

    
408
                Color c = null;
409
            if (xml.contains("color")) {
410
                    c = StringUtilities.string2Color(xml.getStringProperty("color"));
411
            }
412
            if (jcc1 != null)
413
                    jcc1.setColor(c);
414

    
415
            if (jcc2 != null) {
416
                    if (sym instanceof ILineSymbol && xml.contains("outline")) {
417
                            c = StringUtilities.string2Color(xml.getStringProperty("outline"));
418
                            jcc2.setEnabled(true);
419
                            jcc2.setColor(c);
420
                    } else {
421
                            jcc2.setEnabled(false);
422
                    }
423
            }
424
        }
425

    
426
        /**
427
     * This method initializes jPanel
428
     *
429
     * @return javax.swing.JPanel
430
     */
431
    private JPanel getJEastPanel() {
432
            if (jPanel == null) {
433
                    jPanel = new JPanel();
434
                    jPanel.setLayout(new BorderLayout());
435
                    jPanel.add(getJPanelOptions(), BorderLayout.CENTER);
436
                    JPanel aux = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
437
                    aux.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "preview")));
438
                    aux.add(getJPanelPreview());
439
                    jPanel.add(aux, BorderLayout.NORTH);
440

    
441
                    jPanel.add(getJPanelOptions());
442
                    aux = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
443
                    aux.add(getJPanelButtons());
444
                    jPanel.add(aux, BorderLayout.SOUTH);
445
            }
446
            return jPanel;
447
    }
448

    
449
    private JPanel getJPanelButtons() {
450
                if (jPanelButtons == null) {
451
                        jPanelButtons = new JPanel();
452
                        GridLayout layout = new GridLayout();
453
                        layout.setColumns(1);
454
                        layout.setVgap(5);
455
                        jPanelButtons.add(getBtnNewSymbol());
456
                        jPanelButtons.add(getBtnSaveSymbol());
457
                        jPanelButtons.add(getBtnResetSymbol());
458
                        jPanelButtons.add(getBtnProperties());
459

    
460
                        // do not add components bellow this line!
461
                        layout.setRows(jPanelButtons.getComponentCount());
462
                        jPanelButtons.setLayout(layout);
463
                }
464
                return jPanelButtons;
465
        }
466

    
467
        private JButton getBtnNewSymbol() {
468
                if (btnNewSymbol == null) {
469
                        btnNewSymbol = new JButton();
470
                        btnNewSymbol.setName("btnNewSymbol");
471
                        btnNewSymbol.setText(PluginServices.getText(this, "new"));
472
                        btnNewSymbol.addActionListener(buttonListener);
473
                }
474
                return btnNewSymbol;
475
        }
476

    
477
        private JButton getBtnResetSymbol() {
478
                if (btnResetSymbol == null) {
479
                        btnResetSymbol = new JButton();
480
                        btnResetSymbol.setName("btnResetSymbol");
481
                        btnResetSymbol.setText(PluginServices.getText(this, "reset"));
482
                        btnResetSymbol.addActionListener(buttonListener);
483
                }
484
                return btnResetSymbol;
485
        }
486

    
487
        private JButton getBtnSaveSymbol() {
488
                if (btnSaveSymbol == null) {
489
                        btnSaveSymbol = new JButton();
490
                        btnSaveSymbol.setName("btnSaveSymbol");
491
                        btnSaveSymbol.setText(PluginServices.getText(this, "save"));
492
                        btnSaveSymbol.addActionListener(buttonListener);
493
                }
494
                return btnSaveSymbol;
495
        }
496

    
497
        private JButton getBtnProperties() {
498
                if (btnProperties == null) {
499
                        btnProperties = new JButton();
500
                        btnProperties.setName("btnProperties");
501
                        btnProperties.setText(PluginServices.getText(this, "properties"));
502
                        btnProperties.addActionListener(buttonListener);
503
                }
504
                return btnProperties;
505
        }
506

    
507
        /**
508
     * This method initializes jScrollPane
509
     *
510
     * @return javax.swing.JScrollPane
511
     */
512
    private JScrollPane getJScrollPane() {
513
            if (jScrollPane == null) {
514
                    jScrollPane = new JScrollPane();
515
                    jScrollPane.setPreferredSize(new java.awt.Dimension(80,130));
516
                    jScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
517
                    jScrollPane.setViewportView(getTreeFav());
518
            }
519
            return jScrollPane;
520
    }
521

    
522
    /**
523
     * This method initializes jScrollPane1
524
     *
525
     * @return javax.swing.JScrollPane
526
     */
527
    private JScrollPane getJScrollPane1() {
528
            if (jScrollPane1 == null) {
529
                    jScrollPane1 = new JScrollPane();
530
                    jScrollPane1.setViewportView(getJListSymbols());
531
            }
532
            return jScrollPane1;
533
    }
534

    
535
    /**
536
     * This method initializes jPanel1
537
     *
538
     * @return javax.swing.JPanel
539
     */
540
    protected JComponent getJPanelPreview() {
541
            if (jPanelPreview == null) {
542
                    jPanelPreview = new SymbolPreviewer();
543
                    jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
544
                    jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
545
            }
546
            return jPanelPreview;
547
    }
548

    
549
    protected JPanel getJPanelOptions() {
550
            if (jPanelOptions == null) {
551
                    jPanelOptions = new GridBagLayoutPanel();
552
                    jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
553
                    jcc1 = new ColorChooserPanel();
554
                    jcc2 = new ColorChooserPanel();
555
                    jcc1.setAlpha(255);
556
                    jcc2.setAlpha(255);
557
                    jcc1.addActionListener(buttonListener);
558
                    jcc2.addActionListener(buttonListener);
559
                    if (shapeType == FShape.POINT) {
560
                            jPanelOptions.addComponent(
561
                                            PluginServices.getText(this, "color")+":", jcc1);
562
                            jPanelOptions.addComponent(
563
                                            PluginServices.getText(this, "size")+":",
564
                                            txtSize = new IncrementalNumberField(String.valueOf(3)));
565
                            jPanelOptions.addComponent(
566
                                            PluginServices.getText(this, "angle")+":",
567
                                            txtAngle = new IncrementalNumberField());
568
                    } else if (shapeType == FShape.LINE) {
569
                            jPanelOptions.addComponent(
570
                                            PluginServices.getText(this, "color")+":", jcc1);
571
                            jPanelOptions.addComponent(
572
                                            PluginServices.getText(this, "width")+":",
573
                                            txtSize = new IncrementalNumberField(String.valueOf(3)));
574
                            jPanelOptions.addComponent(PluginServices.getText(this, "line_style"), cmbLineStyles = new JLineStyleComboBox());
575
                    } else if (shapeType == FShape.POLYGON) {
576
                            jPanelOptions.addComponent(
577
                                            PluginServices.getText(this, "fill_color")+":", jcc1);
578
                            jPanelOptions.addComponent(
579
                                            PluginServices.getText(this, "outline_color")+":", jcc2);
580
                            jPanelOptions.addComponent(
581
                                            PluginServices.getText(this, "outline_width"),
582
                                            txtWidth = new IncrementalNumberField());
583
                    } else if (shapeType == FShape.TEXT) {
584
                            jPanelOptions.addComponent(
585
                                            PluginServices.getText(this, "font")+":", getCmbFonts());
586

    
587
                            jPanelOptions.addComponent(
588
                                            PluginServices.getText(this, "color")+":", jcc1);
589
                            jPanelOptions.addComponent(
590
                                            PluginServices.getText(this, "size")+":",
591
                                            txtSize = new IncrementalNumberField());
592
                            JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING,0,1));
593
                            aux.add(getBtnBold());
594
                            aux.add(getBtnItalic());
595
                            aux.add(getBtnUnderlined());
596
                            jPanelOptions.addComponent(
597
                                            PluginServices.getText(this, "style")+":", aux);
598

    
599
                        }
600
            }
601
            return jPanelOptions;
602
    }
603

    
604
    private JToggleButton getBtnUnderlined() {
605
            if (btnUnderlined == null) {
606
                        btnUnderlined = new JToggleButton(new ImageIcon(
607
                                        getClass().getClassLoader().getResource("images/underlined.png")));
608
                }
609
                return btnUnderlined;
610
    }
611

    
612
    private JToggleButton getBtnItalic() {
613
            if (btnItalic == null) {
614
                        btnItalic = new JToggleButton(new ImageIcon(
615
                                        getClass().getClassLoader().getResource("images/italic.png")));
616
                }
617
                return btnItalic;
618
        }
619

    
620
        private JToggleButton getBtnBold() {
621
            if (btnBold == null) {
622
                        btnBold = new JToggleButton(new ImageIcon(
623
                                        getClass().getClassLoader().getResource("images/bold.png")));
624
                }
625
                return btnBold;
626
        }
627

    
628

    
629
        private JComboBox getCmbFonts() {
630
                if (cmbFonts == null) {
631
                        // Font info is obtained from the current graphics environment.
632
                        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
633

    
634
                        // --- Get an array of font names (smaller than the number of fonts)
635
                        String[] fontNames = ge.getAvailableFontFamilyNames();
636
                        cmbFonts = new JComboBox(fontNames);
637
                }
638
                return cmbFonts;
639
        }
640

    
641
        public WindowInfo getWindowInfo() {
642
                if (wi == null) {
643
                        wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
644
                        wi.setWidth(706);
645
                        wi.setHeight(500);
646
                        wi.setTitle(PluginServices.getText(this, "symbol_selector"));
647
                }
648
                return wi;
649
        }
650

    
651
        private JSplitPane getJSplitPane() {
652
                if (jSplitPane == null) {
653
                        jSplitPane = new JSplitPane();
654
                        jSplitPane.setDividerLocation(200);
655
                        jSplitPane.setResizeWeight(0.4);
656
                jSplitPane.setLeftComponent(getJScrollPane());
657
                jSplitPane.setRightComponent(getJScrollPane1());
658
                }
659
                return jSplitPane;
660
        }
661

    
662
        /**
663
         * Returns the symbol selected by the user or null if none. It tries to return
664
         * the simplest possible symbol that suits the user settings.
665
         * @return
666
         */
667
        public Object getSelectedObject() {
668
                // if this symbol only has one layer, then no multilayer is needed
669
                if (selectedElement instanceof IMultiLayerSymbol) {
670
                        if (((IMultiLayerSymbol) selectedElement).getLayerCount()==1)
671
                                return ((IMultiLayerSymbol) selectedElement).getLayer(0);
672
                }
673
                return selectedElement;
674
        }
675

    
676
        public void setSymbol(Object symbol) {
677
                selectedElement = symbol;
678
                ((SymbolPreviewer) jPanelPreview).setSymbol((ISymbol) symbol);
679
        }
680

    
681
        /**
682
         * Invoked when the PROPERTIES button is pressed
683
         */
684
        protected void propertiesPressed() {
685
                if (selectedElement ==null)
686
                        return;
687

    
688
                SymbolEditor se = new SymbolEditor((ISymbol) selectedElement, shapeType);
689
                PluginServices.getMDIManager().addWindow(se);
690
                setSymbol(se.getSymbol());
691
        }
692

    
693
        /**
694
         * Invoked when the NEW button is pressed
695
         */
696
        protected void newPressed() {
697
                SymbolEditor se = new SymbolEditor(null, shapeType);
698
                PluginServices.getMDIManager().addWindow(se);
699
                setSymbol(se.getSymbol());
700
        }
701

    
702
        /**
703
         * Invoked when the RESET button is pressed
704
         */
705
        protected void resetPressed() {
706
                setSymbol(null);
707
        }
708

    
709
        /**
710
         * Invoked when the SAVE button is pressed
711
         */
712
        protected void savePressed() {
713
                if (getSelectedObject() ==null)
714
                        return;
715

    
716

    
717
                JFileChooser jfc = new JFileChooser(rootDir);
718
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
719
                        public boolean accept(File f) {
720
                                return f.getAbsolutePath().
721
                                toLowerCase().
722
                                endsWith(SymbolSelectorListModel.SYMBOL_FILE_EXTENSION);
723
                        }
724

    
725
                        public String getDescription() {
726
                                return PluginServices.getText(
727
                                                this, "gvSIG_symbol_definition_file")+ " (*.sym)";
728
                        }
729
                };
730
                jfc.setFileFilter(ff);
731
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
732
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
733
                JTextField txtDesc = new JTextField(25);
734
                accessory.add(txtDesc);
735
                jfc.setAccessory(accessory);
736
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
737
                        File targetFile = jfc.getSelectedFile();
738

    
739
                        String fExtension = SymbolSelectorListModel.SYMBOL_FILE_EXTENSION;
740

    
741
                        // apply description
742
                        String desc;
743
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
744
                                // default to file name
745
                                String s = targetFile.getAbsolutePath();
746
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
747
                        } else {
748
                                desc = txtDesc.getText().trim();
749
                        }
750
                        ISymbol s = (ISymbol) getSelectedObject();
751
                        s.setDescription(desc);
752

    
753
                        // save it
754
                        XMLEntity xml = s.getXMLEntity();
755
                        if (!targetFile.
756
                                        getAbsolutePath().
757
                                        toLowerCase().
758
                                        endsWith(fExtension))
759
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
760
                        FileWriter writer;
761
                        try {
762
                                writer = new FileWriter(targetFile.getAbsolutePath());
763
                                Marshaller m = new Marshaller(writer);
764
                                m.setEncoding("ISO-8859-1");
765
                                m.marshal(xml.getXmlTag());
766

    
767
                        } catch (Exception ex) {
768
                                NotificationManager.addError(
769
                                                PluginServices.getText(this, "save_error"), ex);
770
                        }
771
                        getJListSymbols().setModel(newListModel());
772
                }
773
        }
774

    
775

    
776
        private class MyButtonListener implements ActionListener {
777
                public void actionPerformed(ActionEvent e) {
778

    
779
                        JComponent comp = (JComponent) e.getSource();
780

    
781
                        if ( comp.equals(getBtnProperties()) ) {
782
                                // properties pressed
783
                                propertiesPressed();
784
                        } else if ( comp.equals(getBtnNewSymbol()) ) {
785
                                // new pressed
786
                                newPressed();
787
                        } else if ( comp.equals(getBtnResetSymbol()) ) {
788
                                // reset pressed
789
                                resetPressed();
790
                        } else if ( comp.equals(getBtnSaveSymbol()) ) {
791
                                // save pressed
792
                                savePressed();
793
                        } else if (comp.equals(jcc1) && ((IPersistance) selectedElement).getXMLEntity() != null) {
794
                                if (selectedElement ==null)
795
                                        return;
796

    
797
                                XMLEntity xml =((IPersistance) selectedElement).getXMLEntity();
798

    
799
                                xml.putProperty("color", StringUtilities.color2String(jcc1.getColor()));
800
                                ((IPersistance) selectedElement).setXMLEntity(xml);
801
                                setSymbol(selectedElement);
802
                        } else if (comp.equals(jcc2) && ((IPersistance) selectedElement).getXMLEntity() != null) {
803
                                if (selectedElement ==null)
804
                                        return;
805

    
806
                                XMLEntity xml = ((IPersistance) selectedElement).getXMLEntity();
807

    
808
                                xml.putProperty("outline", StringUtilities.color2String(jcc2.getColor()));
809
                                ((IPersistance) selectedElement).setXMLEntity(xml);
810
                                setSymbol(selectedElement);
811
                        }
812

    
813
                        SymbolSelector.this.repaint();
814
                }
815
        }
816
}