Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / SymbolSelector.java @ 10679

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.ListModel;
77
import javax.swing.event.ListSelectionEvent;
78
import javax.swing.event.ListSelectionListener;
79
import javax.swing.event.TreeModelListener;
80
import javax.swing.tree.TreeModel;
81
import javax.swing.tree.TreePath;
82

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

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

    
106
import de.ios.framework.swing.JDecimalField;
107
import de.ios.framework.swing.JNumberField;
108

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

    
114
    private JTree treeFamilies = null;
115
    private JPanel jPanel = null;
116
    private JScrollPane jScrollPane = null;
117
    private JScrollPane jScrollPane1 = null;
118
    private WindowInfo wi;
119
        private JSplitPane jSplitPane = null;
120
        private AcceptCancelPanel okCancelPanel;
121
        private Object currentElement = null;
122
        private JPanel northPanel;
123
        private ColorChooserPanel jcc1;
124
        private ColorChooserPanel jcc2;
125
        private JNumberField txtSize;
126
        private JDecimalField txtAngle;
127
        private JPanel jPanelButtons;
128
        private JButton btnProperties;
129
        private int shapeType;
130
        private JButton btnSaveSymbol;
131
        private JButton btnResetSymbol;
132
        private ActionListener buttonListener = new MyButtonListener();
133
        private JButton btnNewSymbol;
134
        private JComboBox cmbFonts;
135
        private JToggleButton btnBold;
136
        private JToggleButton btnItalic;
137
        private JToggleButton btnUnderlined;
138
        protected JLabel lblTitle;
139
        protected File dir;
140
        protected Object selectedElement = null;
141
        protected File rootDir;
142
        protected JComponent jPanelPreview = null;
143
    protected GridBagLayoutPanel jPanelOptions = null;
144
    protected JList jListSymbols = null;
145
    protected String treeRootName;
146
    private JDecimalField txtWidth;
147
        
148
        protected SelectorFilter sFilter = new SelectorFilter() {
149
                private final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D());
150
                private final IGeometry dummyLineGeom = ShapeFactory.createPolyline2D(new GeneralPathX());
151
                private final IGeometry dummyPolygonGeom = ShapeFactory.createPolygon2D(new GeneralPathX());
152

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

    
157
                                IGeometry compareGeometry = null;
158
                                switch (SymbolSelector.this.shapeType) {
159
                                case FShape.POINT:
160
                                        compareGeometry = dummyPointGeom;
161
                                        break;
162
                                case FShape.LINE:
163
                                        compareGeometry = dummyLineGeom;
164
                                        break;
165
                                case FShape.POLYGON:
166
                                        compareGeometry = dummyPolygonGeom;
167
                                        break;
168
                                }
169
                                return sym.isSuitableFor(compareGeometry);
170
                        }
171
                        return false;
172
                }
173
        };
174
        private JLineStyleComboBox cmbLineStyles;
175
        
176
        public SymbolSelector(Object currentElement, int shapeType) {
177
            super();
178
            if (currentElement != null && currentElement instanceof ISymbol) {
179
                    ISymbol sym = (ISymbol) currentElement;
180
                    currentElement = SymbologyFactory.createSymbolFromXML(
181
                                    sym.getXMLEntity(), "");
182
                    String desc = sym.getDescription();
183
                    desc += " ("+PluginServices.getText(this, "current")+")";
184
                    ((ISymbol)currentElement).setDescription(desc);
185
            }
186
            this.selectedElement = currentElement;
187
            this.shapeType = shapeType;
188
            Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
189
                rootDir = new File(prefs.get("SymbolLibraryFolder", System.getProperty("user.home")+"/gvSIG/Symbols"));
190
                if (!rootDir.exists())
191
                        rootDir.mkdir();
192
                treeRootName = PluginServices.getText(this, "symbol_library");
193
            initialize();
194
    }
195
    
196
    public SymbolSelector(Object symbol, int shapeType, SelectorFilter filter) {
197
            this(symbol, shapeType);
198
            sFilter = filter;
199
    }
200

    
201
    /**
202
     * This method initializes this
203
     *
204
     */
205
    private void initialize() {
206

    
207
        this.setLayout(new BorderLayout());
208
        this.setSize(400, 221);
209

    
210
        this.add(getJNorthPanel(), BorderLayout.NORTH);
211
        this.add(getJSplitPane(), BorderLayout.CENTER);
212
        this.add(getJEastPanel(), BorderLayout.EAST);
213
        ActionListener okAction = new ActionListener() {
214
                    public void actionPerformed(ActionEvent e) {
215
                            PluginServices.getMDIManager().closeWindow(SymbolSelector.this);
216
                    }
217
            }, cancelAction = new ActionListener() {
218
                    public void actionPerformed(ActionEvent e) {
219
                            setSymbol(null);
220
                            PluginServices.getMDIManager().closeWindow(SymbolSelector.this);
221
                    }
222
            };
223

    
224
        okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
225

    
226

    
227
        this.add(okCancelPanel, BorderLayout.SOUTH);
228
    }
229

    
230
    protected SelectorListModel newListModel() {
231
            SelectorListModel listModel = new SelectorListModel(
232
                            dir,
233
                            (ISymbol) selectedElement,
234
                            sFilter,
235
                            SelectorListModel.SYMBOL_FILE_EXTENSION);
236
            return listModel;
237
    }
238

    
239
    private JPanel getJNorthPanel() throws IllegalArgumentException {
240
                if (northPanel == null) {
241
                        String text = "";
242
                        switch (shapeType) {
243
                        case FShape.POINT:
244
                                text = PluginServices.getText(this, "point_symbols");
245
                                break;
246
                        case FShape.LINE:
247
                                text = PluginServices.getText(this, "line_symbols");
248
                                break;
249
                        case FShape.POLYGON:
250
                                text = PluginServices.getText(this, "polygon_symbols");
251
                                break;
252
                        case FShape.TEXT:
253
                                text = PluginServices.getText(this, "text_symbols");
254
                                break;
255
                        default:
256
                                throw new IllegalArgumentException(PluginServices.getText(this, "shape_type_not_yet_supported"));
257
                        }
258
                        northPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
259
                        lblTitle = new JLabel(text);
260
                        lblTitle.setFont(lblTitle.getFont().deriveFont(Font.BOLD));
261
                        northPanel.add(lblTitle);
262
                }
263
                return northPanel;
264
        }
265

    
266
        /**
267
     * This method initializes jList
268
     *
269
     * @return javax.swing.JList
270
     */
271
    private JTree getJListFav() {
272
            if (treeFamilies == null) {
273
                    treeFamilies = new JTree();
274
                    treeFamilies.setPreferredSize(new java.awt.Dimension(70,100));
275
                    try {
276
                            treeFamilies.setModel(new TreeModel() {
277
                                    final class MyFile extends File {
278
                                            public MyFile(String pathname) {
279
                                                    super(pathname);
280
                                            }
281

    
282
                                            public String toString() {
283
                                                    if (this.equals(root))
284
                                                            return treeRootName;
285
                                                    String path = getAbsolutePath();
286
                                                    String prefixToRemove = rootDir.getAbsolutePath();
287
                                                    path = path.substring(prefixToRemove.length()+1, path.length());
288
                                                    return path;
289
                                            }
290
                                    };
291
                                    MyFile root = new MyFile(rootDir.getAbsolutePath());
292

    
293
                                    private FileFilter ff = new FileFilter() {
294
                                            public boolean accept(File pathname) {
295
                                                    return pathname.isDirectory();
296
                                            }
297
                                    };
298

    
299
                                    public Object getRoot() {
300
                                            return root;
301
                                    }
302

    
303
                                    public int getChildCount(Object parent) {
304
                                            return ((File) parent).listFiles(ff).length;
305
                                    }
306

    
307
                                    public boolean isLeaf(Object node) {
308
                                            return getChildCount(node)==0;
309
                                    }
310

    
311
                                    public void addTreeModelListener(TreeModelListener l) {
312
                                            // TODO Necessite?
313

    
314
                                    }
315

    
316
                                    public void removeTreeModelListener(TreeModelListener l) {
317
                                            // TODO Necessite?
318
                                    }
319

    
320
                                    public Object getChild(Object parent, int index) {
321
                                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
322
                                    }
323

    
324
                                    public int getIndexOfChild(Object parent, Object child) {
325
                                            if (parent == null)
326
                                                    return -1;
327
                                            File[] files = ((File) parent).listFiles(ff);
328
                                            for (int i = 0; i < files.length; i++) {
329
                                                    if (files[i].equals((File) child))
330
                                                            return i;
331
                                            }
332
                                            return -1;
333
                                    }
334

    
335
                                    public void valueForPathChanged(TreePath path, Object newValue) {
336
                                            // TODO Auto-generated method stub
337
                                    }
338

    
339
                            });
340
                            treeFamilies.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
341
                                        public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
342
                                                dir = (File)
343
                                                treeFamilies.getLastSelectedPathComponent();
344

    
345
                                                if (dir == null) return;
346

    
347
                                                jListSymbols.setModel(newListModel());
348
                                        }
349
                                });
350
                            
351
                    } catch ( ExceptionInInitializerError ex ) {
352
                            JOptionPane.showMessageDialog(this, PluginServices.getText(this, "could_not_find_symbol_directory"));
353
                    }
354
            }
355
            return treeFamilies;
356
    }
357

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

    
385
                                    pnl.setBackground(bgColor);
386
                                    SymbolPreviewer sp = new SymbolPreviewer();
387
                                    sp.setAlignmentX(Component.CENTER_ALIGNMENT);
388
                                    sp.setPreferredSize(new Dimension(50, 50));
389
                                    sp.setSymbol(sym);
390
                                    sp.setBackground(bgColor);
391
                                    pnl.add(sp);
392
                                    JLabel lbl = new JLabel(sym.getDescription());
393
                                    lbl.setBackground(bgColor);
394
                                    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
395
                                    pnl.add(lbl);
396

    
397
                                    return pnl;
398
                            }
399

    
400
                };
401
                jListSymbols.setCellRenderer(renderer);
402
            }
403
            return jListSymbols;
404
    }
405

    
406
    protected void updateOptionsPanel() {
407
            ISymbol sym = ((ISymbol) jListSymbols.getSelectedValue());
408
            if (sym == null)
409
                    return;
410

    
411
                XMLEntity xml = sym.getXMLEntity();
412

    
413
                Color c = null;
414
            if (xml.contains("color")) {
415
                    c = StringUtilities.string2Color(xml.getStringProperty("color"));
416
            }
417
                jcc1.setColor(c);
418

    
419
                if (jcc2 == null)
420
                        return;
421
                if (xml.contains("outline")) {
422
                    c = StringUtilities.string2Color(xml.getStringProperty("outline"));
423
                    jcc2.setEnabled(true);
424
                    jcc2.setColor(c);
425
            } else {
426
                    jcc2.setEnabled(false);
427
            }
428
        }
429

    
430
        /**
431
     * This method initializes jPanel
432
     *
433
     * @return javax.swing.JPanel
434
     */
435
    private JPanel getJEastPanel() {
436
            if (jPanel == null) {
437
                    jPanel = new JPanel();
438
                    jPanel.setLayout(new BorderLayout());
439
                    jPanel.add(getJPanelOptions(), BorderLayout.CENTER);
440
                    JPanel aux = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
441
                    aux.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "preview")));
442
                    aux.add(getJPanelPreview());
443
                    jPanel.add(aux, BorderLayout.NORTH);
444
                    
445
                    jPanel.add(getJPanelOptions());
446
                    aux = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
447
                    aux.add(getJPanelButtons());
448
                    jPanel.add(aux, BorderLayout.SOUTH);
449
            }
450
            return jPanel;
451
    }
452

    
453
    private JPanel getJPanelButtons() {
454
                if (jPanelButtons == null) {
455
                        jPanelButtons = new JPanel();
456
                        GridLayout layout = new GridLayout();
457
                        layout.setColumns(1);
458
                        layout.setVgap(5);
459
                        jPanelButtons.add(getBtnNewSymbol());
460
                        jPanelButtons.add(getBtnSaveSymbol());
461
                        jPanelButtons.add(getBtnResetSymbol());
462
                        jPanelButtons.add(getBtnProperties());
463

    
464
                        // do not add components bellow this line!
465
                        layout.setRows(jPanelButtons.getComponentCount());
466
                        jPanelButtons.setLayout(layout);
467
                }
468
                return jPanelButtons;
469
        }
470

    
471
        private JButton getBtnNewSymbol() {
472
                if (btnNewSymbol == null) {
473
                        btnNewSymbol = new JButton();
474
                        btnNewSymbol.setName("btnNewSymbol");
475
                        btnNewSymbol.setText(PluginServices.getText(this, "new"));
476
                        btnNewSymbol.addActionListener(buttonListener);
477
                }
478
                return btnNewSymbol;
479
        }
480

    
481
        private JButton getBtnResetSymbol() {
482
                if (btnResetSymbol == null) {
483
                        btnResetSymbol = new JButton();
484
                        btnResetSymbol.setName("btnResetSymbol");
485
                        btnResetSymbol.setText(PluginServices.getText(this, "reset"));
486
                        btnResetSymbol.addActionListener(buttonListener);
487
                }
488
                return btnResetSymbol;
489
        }
490

    
491
        private JButton getBtnSaveSymbol() {
492
                if (btnSaveSymbol == null) {
493
                        btnSaveSymbol = new JButton();
494
                        btnSaveSymbol.setName("btnSaveSymbol");
495
                        btnSaveSymbol.setText(PluginServices.getText(this, "save"));
496
                        btnSaveSymbol.addActionListener(buttonListener);
497
                }
498
                return btnSaveSymbol;
499
        }
500

    
501
        private JButton getBtnProperties() {
502
                if (btnProperties == null) {
503
                        btnProperties = new JButton();
504
                        btnProperties.setName("btnProperties");
505
                        btnProperties.setText(PluginServices.getText(this, "properties"));
506
                        btnProperties.addActionListener(buttonListener);
507
                }
508
                return btnProperties;
509
        }
510

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

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

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

    
553
    protected JPanel getJPanelOptions() {
554
            if (jPanelOptions == null) {
555
                    jPanelOptions = new GridBagLayoutPanel();
556
                    jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
557
                    jcc1 = new ColorChooserPanel();
558
                    jcc2 = new ColorChooserPanel();
559
                    jcc1.setAlpha(255);
560
                    jcc2.setAlpha(255);
561
                    jcc1.addActionListener(buttonListener);
562
                    jcc2.addActionListener(buttonListener);
563
                    if (shapeType == FShape.POINT) {
564
                            jPanelOptions.addComponent(
565
                                            PluginServices.getText(this, "color")+":", jcc1);
566
                            jPanelOptions.addComponent(
567
                                            PluginServices.getText(this, "size")+":",
568
                                            txtSize = new JNumberField());
569
                            jPanelOptions.addComponent(
570
                                            PluginServices.getText(this, "angle")+":",
571
                                            txtAngle = new JDecimalField());
572
                    } else if (shapeType == FShape.LINE) {
573
                            jPanelOptions.addComponent(
574
                                            PluginServices.getText(this, "color")+":", jcc1);
575
                            jPanelOptions.addComponent(
576
                                            PluginServices.getText(this, "width")+":",
577
                                            txtSize = new JNumberField());
578
                            jPanelOptions.addComponent(PluginServices.getText(this, "line_style"), cmbLineStyles = new JLineStyleComboBox());
579
                    } else if (shapeType == FShape.POLYGON) {
580
                            jPanelOptions.addComponent(
581
                                            PluginServices.getText(this, "fill_color")+":", jcc1);
582
                            jPanelOptions.addComponent(
583
                                            PluginServices.getText(this, "outline_color")+":", jcc2);
584
                            jPanelOptions.addComponent(
585
                                            PluginServices.getText(this, "outline_width"), 
586
                                            txtWidth = new JDecimalField());
587
                    } else if (shapeType == FShape.TEXT) {
588
                            jPanelOptions.addComponent(
589
                                            PluginServices.getText(this, "font")+":", getCmbFonts());
590
                            
591
                            jPanelOptions.addComponent(
592
                                            PluginServices.getText(this, "color")+":", jcc1);
593
                            jPanelOptions.addComponent(
594
                                            PluginServices.getText(this, "size")+":",
595
                                            txtSize = new JNumberField());
596
                            JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING,0,1));
597
                            aux.add(getBtnBold());
598
                            aux.add(getBtnItalic());
599
                            aux.add(getBtnUnderlined());
600
                            jPanelOptions.addComponent(
601
                                            PluginServices.getText(this, "style")+":", aux);
602
                            
603
                        }
604
            }
605
            return jPanelOptions;
606
    }
607
    
608
    private JToggleButton getBtnUnderlined() {
609
            if (btnUnderlined == null) {
610
                        btnUnderlined = new JToggleButton(new ImageIcon(
611
                                        getClass().getClassLoader().getResource("images/underlined.png")));
612
                }
613
                return btnUnderlined;
614
    }
615

    
616
    private JToggleButton getBtnItalic() {
617
            if (btnItalic == null) {
618
                        btnItalic = new JToggleButton(new ImageIcon(
619
                                        getClass().getClassLoader().getResource("images/italic.png")));
620
                }
621
                return btnItalic;
622
        }
623

    
624
        private JToggleButton getBtnBold() {
625
            if (btnBold == null) {
626
                        btnBold = new JToggleButton(new ImageIcon(
627
                                        getClass().getClassLoader().getResource("images/bold.png")));
628
                }
629
                return btnBold;
630
        }
631
        
632

    
633
        private JComboBox getCmbFonts() {
634
                if (cmbFonts == null) {
635
//                         Font info is obtained from the current graphics environment.
636
                        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
637

    
638
//                        --- Get an array of font names (smaller than the number of fonts)
639
                        String[] fontNames = ge.getAvailableFontFamilyNames();
640
                        cmbFonts = new JComboBox(fontNames);
641
                }
642
                return cmbFonts;
643
        }
644
        
645
        public WindowInfo getWindowInfo() {
646
                if (wi == null) {
647
                        wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
648
                        wi.setWidth(706);
649
                        wi.setHeight(500);
650
                        wi.setTitle(PluginServices.getText(this, "symbol_selector"));
651
                }
652
                return wi;
653
        }
654

    
655
        private JSplitPane getJSplitPane() {
656
                if (jSplitPane == null) {
657
                        jSplitPane = new JSplitPane();
658
                        jSplitPane.setDividerLocation(200);
659
                        jSplitPane.setResizeWeight(0.4);
660
                jSplitPane.setLeftComponent(getJScrollPane());
661
                jSplitPane.setRightComponent(getJScrollPane1());
662
                }
663
                return jSplitPane;
664
        }
665

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

    
680
        public void setSymbol(Object symbol) {
681
                selectedElement = symbol;
682
                ((SymbolPreviewer) jPanelPreview).setSymbol((ISymbol) symbol);
683
        }
684

    
685
        /**
686
         * Invoked when the PROPERTIES button is pressed
687
         */
688
        protected void propertiesPressed() {
689
                if (selectedElement ==null)
690
                        return;
691

    
692
                SymbolEditor se = new SymbolEditor((ISymbol) selectedElement, shapeType);
693
                PluginServices.getMDIManager().addWindow(se);
694
                setSymbol(se.getSymbol());
695
        }
696

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

    
706
        /**
707
         * Invoked when the RESET button is pressed
708
         */
709
        protected void resetPressed() {
710
                setSymbol(null);
711
        }
712

    
713
        /**
714
         * Invoked when the SAVE button is pressed
715
         */
716
        protected void savePressed() {
717
                if (getSelectedObject() ==null)
718
                        return;
719

    
720

    
721
                JFileChooser jfc = new JFileChooser(rootDir);
722
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
723
                        public boolean accept(File f) {
724
                                return f.getAbsolutePath().
725
                                toLowerCase().
726
                                endsWith(SelectorListModel.SYMBOL_FILE_EXTENSION);
727
                        }
728

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

    
743
                        String fExtension = SelectorListModel.SYMBOL_FILE_EXTENSION;
744

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

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

    
771
                        } catch (Exception ex) {
772
                                NotificationManager.addError(
773
                                                PluginServices.getText(this, "save_error"), ex);
774
                        }
775
                        getJListSymbols().setModel(newListModel());
776
                }
777
        }
778

    
779

    
780
        private class MyButtonListener implements ActionListener {
781
                public void actionPerformed(ActionEvent e) {
782

    
783
                        JComponent comp = (JComponent) e.getSource();
784

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

    
801
                                XMLEntity xml =((IPersistance) selectedElement).getXMLEntity();
802

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

    
810
                                XMLEntity xml = ((IPersistance) selectedElement).getXMLEntity();
811

    
812
                                xml.putProperty("outline", StringUtilities.color2String(jcc2.getColor()));
813
                                ((IPersistance) selectedElement).setXMLEntity(xml);
814
                                setSymbol(selectedElement);
815
                        }
816

    
817
                        SymbolSelector.this.repaint();
818
                }
819
        }
820
}