Statistics
| Revision:

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

History | View | Annotate | Download (25.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.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.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
    protected TreeModel treeModel;
147
    private JDecimalField txtWidth;
148

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

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

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

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

    
197
    public SymbolSelector(Object symbol, int shapeType, SelectorFilter filter) {
198
            this(symbol, shapeType);
199
            sFilter = filter;
200
    }
201

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

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

    
224
                    private FileFilter ff = new FileFilter() {
225
                            public boolean accept(File pathname) {
226
                                    return pathname.isDirectory();
227
                            }
228
                    };
229

    
230
                    public Object getRoot() {
231
                            return root;
232
                    }
233

    
234
                    public int getChildCount(Object parent) {
235
                            return ((File) parent).listFiles(ff).length;
236
                    }
237

    
238
                    public boolean isLeaf(Object node) {
239
                            return getChildCount(node)==0;
240
                    }
241

    
242
                    public void addTreeModelListener(TreeModelListener l) {
243
                            // TODO Necessite?
244

    
245
                    }
246

    
247
                    public void removeTreeModelListener(TreeModelListener l) {
248
                            // TODO Necessite?
249
                    }
250

    
251
                    public Object getChild(Object parent, int index) {
252
                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
253
                    }
254

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

    
266
                    public void valueForPathChanged(TreePath path, Object newValue) {
267
                            // TODO Auto-generated method stub
268
                    }
269

    
270
            };
271

    
272
            this.setLayout(new BorderLayout());
273
            this.setSize(400, 221);
274

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

    
289
            okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
290

    
291

    
292
            this.add(okCancelPanel, BorderLayout.SOUTH);
293
    }
294

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

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

    
331

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

    
343
                                                if (dir == null) return;
344

    
345
                                                jListSymbols.setModel(newListModel());
346
                                        }
347
                                });
348

    
349
                    } catch ( ExceptionInInitializerError ex ) {
350
                            JOptionPane.showMessageDialog(this, PluginServices.getText(this, "could_not_find_symbol_directory"));
351
                    }
352
            }
353
            return treeFamilies;
354
    }
355

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

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

    
395
                                    return pnl;
396
                            }
397

    
398
                };
399
                jListSymbols.setCellRenderer(renderer);
400
            }
401
            return jListSymbols;
402
    }
403

    
404
    protected void updateOptionsPanel() {
405
            IPersistance sym = ((IPersistance) jListSymbols.getSelectedValue());
406
            if (sym == null)
407
                    return;
408

    
409
                XMLEntity xml = sym.getXMLEntity();
410

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
602
                        }
603
            }
604
            return jPanelOptions;
605
    }
606

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

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

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

    
631

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

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

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

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

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

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

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

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

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

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

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

    
719

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

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

    
742
                        String fExtension = SymbolSelectorListModel.SYMBOL_FILE_EXTENSION;
743

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

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

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

    
778

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

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

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

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

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

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

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

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