Statistics
| Revision:

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

History | View | Annotate | Download (15.5 KB)

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

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: StyleSelector.java 27576 2009-03-26 12:41:07Z vcaballero $
45
 * $Log$
46
 * Revision 1.11  2007-08-21 09:32:53  jvidal
47
 * javadoc
48
 *
49
 * Revision 1.10  2007/05/31 09:36:22  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.9  2007/05/10 09:47:50  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.8  2007/05/08 15:44:07  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.7  2007/04/27 12:10:17  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.6  2007/04/11 16:02:43  jaume
62
 * file filter
63
 *
64
 * Revision 1.5  2007/04/05 16:08:34  jaume
65
 * Styled labeling stuff
66
 *
67
 * Revision 1.4  2007/04/04 16:01:14  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.2  2007/03/09 11:25:00  jaume
71
 * Advanced symbology (start committing)
72
 *
73
 * Revision 1.1.2.4  2007/02/21 07:35:14  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.1.2.3  2007/02/08 15:43:04  jaume
77
 * some bug fixes in the editor and removed unnecessary imports
78
 *
79
 * Revision 1.1.2.2  2007/01/30 18:10:10  jaume
80
 * start commiting labeling stuff
81
 *
82
 * Revision 1.1.2.1  2007/01/26 13:49:03  jaume
83
 * *** empty log message ***
84
 *
85
 *
86
 */
87
package com.iver.cit.gvsig.gui.styling;
88

    
89
import java.awt.BorderLayout;
90
import java.awt.Color;
91
import java.awt.Component;
92
import java.awt.Dimension;
93
import java.awt.FlowLayout;
94
import java.awt.event.ActionEvent;
95
import java.awt.event.ActionListener;
96
import java.io.File;
97
import java.io.FileWriter;
98
import java.util.prefs.Preferences;
99

    
100
import javax.swing.BorderFactory;
101
import javax.swing.BoxLayout;
102
import javax.swing.JComponent;
103
import javax.swing.JFileChooser;
104
import javax.swing.JLabel;
105
import javax.swing.JList;
106
import javax.swing.JOptionPane;
107
import javax.swing.JPanel;
108
import javax.swing.JTextField;
109
import javax.swing.ListCellRenderer;
110
import javax.swing.event.ListSelectionEvent;
111
import javax.swing.event.ListSelectionListener;
112

    
113
import org.exolab.castor.xml.Marshaller;
114
import org.gvsig.gui.beans.AcceptCancelPanel;
115
import org.gvsig.gui.beans.controls.dnd.JDnDList;
116
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
117

    
118
import com.iver.andami.PluginServices;
119
import com.iver.andami.messages.NotificationManager;
120
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
121
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
122
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
123
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
124
import com.iver.cit.gvsig.gui.JComboBoxUnits;
125
import com.iver.cit.gvsig.gui.panels.ImageSizePanel;
126
import com.iver.utiles.XMLEntity;
127

    
128
/**
129
 * Creates a panel where the user can select a style for an object that allows
130
 * to manage this property.This panel will be similar to the symbol selector panel
131
 * and, on it, the user will have a previsualization of the style of objects
132
 * stored and posibilities to modify an existing one, to create a new one
133
 * and so on.
134
 *
135
 * @author jaume dominguez faus - jaume.dominguez@iver.es
136
 *
137
 */
138
public class StyleSelector extends SymbolSelector{
139
        private static final long serialVersionUID = -7476555713446755512L;
140
        private ImageSizePanel isp;
141
        private IStyle styleType;
142
        private IStyle previousStyle;
143
        private boolean showPanelOptions = true;
144

    
145
        public StyleSelector(IStyle style, int shapeType) {
146
                this(style, shapeType, new StyleSelectorFilter() {
147

    
148
                        public boolean accepts(Object obj) {
149
                                return obj instanceof IStyle;
150
                        }
151

    
152
                        public IStyle getAllowedObject() {
153
                                return null;
154
                        }
155
                });
156
        }
157
        /**
158
         * Constructor method
159
         *
160
         * @param style
161
         * @param shapeType
162
         * @param filter
163
         */
164
        public StyleSelector(IStyle style, int shapeType, StyleSelectorFilter filter) {
165
                this(style, shapeType, filter, true);
166
        }
167

    
168

    
169
        public StyleSelector(IStyle style, int shapeType, StyleSelectorFilter filter, boolean showPanelOptions) {
170
                super(null, shapeType, filter, false);
171

    
172
                this.previousStyle = (IStyle)style;
173
                IStyle clonedStyle = null;
174
                if (style != null){
175
                        clonedStyle = SymbologyFactory.createStyleFromXML(
176
                                ((IStyle)style).getXMLEntity(), "");
177
                }
178

    
179

    
180
                this.showPanelOptions = showPanelOptions;
181

    
182
                styleType = ((StyleSelectorFilter)filter).getAllowedObject();
183
                Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
184
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
185
                if (!rootDir.exists())
186
                        rootDir.mkdir();
187

    
188
                try {
189
                        initialize(clonedStyle);
190
                } catch (ClassNotFoundException e) {
191
                        throw new Error(e);
192
                }
193
                lblTitle.setText(PluginServices.getText(this, "label_styles"));
194
                treeRootName = PluginServices.getText(this, "style_library");
195

    
196
        }
197

    
198

    
199

    
200
        @Override
201
        protected void initialize(Object currentElement) throws ClassNotFoundException {
202
                library = new StyleLibrary(rootDir);
203

    
204
                this.setLayout(new BorderLayout());
205
                this.setSize(400, 221);
206

    
207
                this.add(getJNorthPanel(), BorderLayout.NORTH);
208
                this.add(getJSplitPane(), BorderLayout.CENTER);
209
                this.add(getJEastPanel(), BorderLayout.EAST);
210
                ActionListener okAction = new ActionListener() {
211
                        public void actionPerformed(ActionEvent e) {
212
                                accepted = true;
213
                                PluginServices.getMDIManager().closeWindow(StyleSelector.this);
214
                        }
215
                }, cancelAction = new ActionListener() {
216
                        public void actionPerformed(ActionEvent e) {
217
                                accepted = false;
218
                                PluginServices.getMDIManager().closeWindow(StyleSelector.this);
219
                        }
220
                };
221

    
222
                okCancelPanel = new AcceptCancelPanel();
223
                okCancelPanel.setOkButtonActionListener(okAction);
224
                okCancelPanel.setCancelButtonActionListener(cancelAction);
225

    
226
                this.add(okCancelPanel, BorderLayout.SOUTH);
227
                libraryBrowser.setSelectionRow(0);
228

    
229
                SillyDragNDropAction dndAction = new SillyDragNDropAction();
230
                libraryBrowser.addMouseListener(dndAction);
231
                libraryBrowser.addMouseMotionListener(dndAction);
232
                getJListSymbols().addMouseListener(dndAction);
233
                getJListSymbols().addMouseMotionListener(dndAction);
234

    
235
                setSymbol(currentElement);
236
        }
237

    
238
        @Override
239
        public void setSymbol(Object style) {
240
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) style);
241
                updateOptionsPanel();
242
        }
243

    
244
        @Override
245
        public Object getSelectedObject() {
246
//                if (!accepted) return null;
247
                Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
248

    
249
                if(mySelectedElement == null)
250
                        return null;
251

    
252
                if (mySelectedElement instanceof CartographicSupport) {
253
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
254
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
255
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
256
                }
257

    
258
                return mySelectedElement;
259
        }
260

    
261
        protected SymbolSelectorListModel newListModel() {
262
                StyleSelectorListModel listModel = new StyleSelectorListModel(
263
                                dir,
264
                                sFilter,
265
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
266
                return listModel;
267

    
268
        }
269

    
270
        protected JPanel getJPanelOptions() {
271
                if (jPanelOptions == null) {
272
                        jPanelOptions = new GridBagLayoutPanel();
273
                        if(showPanelOptions){
274
                                jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
275
                                jPanelOptions.addComponent(getImageSizePanel());
276
                                jPanelOptions.addComponent(PluginServices.getText(this, "units"),
277
                                                cmbUnits = new JComboBoxUnits(true));
278
                                jPanelOptions.addComponent("",
279
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
280
                        }
281
                }
282
                return jPanelOptions;
283
        }
284

    
285
        /**
286
         *
287
         * This method initializes ImageSizePanel
288
         *
289
         * @return isp ImageSizePanel
290
         */
291
        private ImageSizePanel getImageSizePanel() {
292
                if (isp == null) {
293
                        isp = new ImageSizePanel();
294
                        isp.addActionListener(new ActionListener() {
295
                                public void actionPerformed(ActionEvent e) {
296
                                        ILabelStyle st = (ILabelStyle) getSelectedObject();
297
                                        if (st != null) {
298
                                                double[] sz = isp.getImageDimension();
299
                                                st.setSize(sz[0], sz[1]);
300
                                        }
301
                                }
302
                        });
303
                }
304

    
305
                return isp;
306
        }
307

    
308
        protected void updateOptionsPanel() {
309
                if(showPanelOptions){
310
                        IStyle s = ((StylePreviewer) jPanelPreview).getStyle();
311
                        if (s == null){
312
                                Dimension sz = new Dimension(0,0);
313
                                getImageSizePanel().setImageSize(sz);
314
                        }
315
                        if (s instanceof ILabelStyle) {
316
                                ILabelStyle lab = (ILabelStyle) s;
317
                                Dimension sz = lab.getSize();
318
                                getImageSizePanel().setImageSize(sz);
319
                        }
320
                }
321
        }
322

    
323

    
324
        /**
325
         * This method initializes jList
326
         *
327
         * @return javax.swing.JList
328
         */
329
        protected JList getJListSymbols() {
330
                if (jListSymbols == null) {
331
                        jListSymbols = new JDnDList();
332
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
333
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
334
                        jListSymbols.setVisibleRowCount(-1);
335
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
336
                                public void valueChanged(ListSelectionEvent e) {
337
                                        if (jListSymbols.getSelectedValue()!=null) {
338
                                                IStyle selStyle = SymbologyFactory.createStyleFromXML(
339
                                                                ((IStyle) jListSymbols.getSelectedValue()).getXMLEntity(), null);
340
                                                setStyle(selStyle);
341
                                                updateOptionsPanel();
342
                                        }
343
                                }
344

    
345
                        });
346
                        ListCellRenderer renderer = new ListCellRenderer() {
347
                                private Color mySelectedBGColor = new Color(255,145,100,255);
348
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
349
                                        IStyle sty = (IStyle) value;
350
                                        JPanel pnl = new JPanel();
351
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
352
                                        pnl.setLayout(layout);
353
                                        Color bgColor = (isSelected) ? mySelectedBGColor
354
                                                        : getJListSymbols().getBackground();
355

    
356
                                        pnl.setBackground(bgColor);
357
                                        StylePreviewer sp = new StylePreviewer();
358
                                        sp.setShowOutline(false);
359
                                        sp.setAlignmentX(Component.CENTER_ALIGNMENT);
360
                                        sp.setPreferredSize(new Dimension(50, 50));
361
                                        sp.setStyle(sty);
362
                                        sp.setBackground(bgColor);
363
                                        pnl.add(sp);
364
                                        JLabel lbl = new JLabel(sty.getDescription());
365
                                        lbl.setBackground(bgColor);
366
                                        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
367
                                        pnl.add(lbl);
368

    
369
                                        return pnl;
370
                                }
371

    
372
                        };
373
                        jListSymbols.setCellRenderer(renderer);
374
                }
375
                return jListSymbols;
376
        }
377
        /**
378
         * Modify the previsualization showed in the panel with the style of the
379
         * new object selected.
380
         *
381
         * @param selectedValue
382
         */
383
        protected void setStyle(Object selectedValue) {
384
                //selectedElement = selectedValue;
385
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) selectedValue);
386
                doLayout();
387
                updateOptionsPanel();
388
                repaint();
389
        }
390

    
391
        protected void newPressed() {
392

    
393
                StyleEditor se = new StyleEditor(styleType);
394
                PluginServices.getMDIManager().addWindow(se);
395
                setStyle(se.getStyle());
396

    
397
        }
398

    
399
        protected void propertiesPressed() {
400
//                boolean state = accepted;
401
//                accepted = true;
402
                StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
403
                if(se != null){
404
                        PluginServices.getMDIManager().addWindow(se);
405
                        setStyle(se.getStyle());
406
                }
407
//                accepted = state;
408
        }
409

    
410
        protected void savePressed() {
411
                if (getSelectedObject() == null)
412
                        return;
413

    
414
                JFileChooser jfc = new JFileChooser(rootDir);
415
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
416
                        public boolean accept(File f) {
417
                                return f.getAbsolutePath().
418
                                toLowerCase().
419
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
420
                        }
421

    
422
                        public String getDescription() {
423
                                return PluginServices.getText(
424
                                                this, "gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
425
                        }
426
                };
427
                jfc.setFileFilter(ff);
428
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
429
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
430
                JTextField txtDesc = new JTextField(25);
431
                txtDesc.setText(((IStyle) getSelectedObject()).getDescription());
432
                accessory.add(txtDesc);
433
                jfc.setAccessory(accessory);
434
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
435
                        File targetFile = jfc.getSelectedFile();
436

    
437
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
438

    
439
                        // apply description
440
                        String desc;
441
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
442
                                // default to file name
443
                                String s = targetFile.getAbsolutePath();
444
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
445
                        } else {
446
                                desc = txtDesc.getText().trim();
447
                        }
448
                        IStyle s = (IStyle) getSelectedObject();
449
                        s.setDescription(desc);
450

    
451
                        // save it
452
                        XMLEntity xml = s.getXMLEntity();
453
                        if (!targetFile.
454
                                        getAbsolutePath().
455
                                        toLowerCase().
456
                                        endsWith(fExtension))
457
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
458
                        if(targetFile.exists()){
459
                                int resp = JOptionPane.showConfirmDialog(
460
                                                (Component) PluginServices.getMainFrame(),
461
                                                PluginServices.getText(this,
462
                                                "fichero_ya_existe_seguro_desea_guardarlo"),
463
                                                PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
464
                                if (resp != JOptionPane.YES_OPTION) {
465
                                        return;
466
                                }
467
                        }
468
                        FileWriter writer;
469
                        try {
470
                                writer = new FileWriter(targetFile.getAbsolutePath());
471
                                Marshaller m = new Marshaller(writer);
472
                                m.setEncoding("ISO-8859-1");
473
                                m.marshal(xml.getXmlTag());
474

    
475
                        } catch (Exception ex) {
476
                                NotificationManager.addError(
477
                                                PluginServices.getText(this, "save_error"), ex);
478
                        }
479
                        getJListSymbols().setModel(newListModel());
480
                }
481
        }
482

    
483
        /**
484
         * This method initializes jPanelPreview
485
         *
486
         * @return javax.swing.JComponent
487
         */
488
        protected JComponent getJPanelPreview() {
489
                if (jPanelPreview == null) {
490
                        jPanelPreview = new StylePreviewer();
491
                        jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
492
                        jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
493
                        ((StylePreviewer) jPanelPreview).setShowOutline(true);
494
                }
495
                return jPanelPreview;
496
        }
497

    
498
        @Override
499
        public void actionPerformed(ActionEvent e) {
500
                if (!act) return;
501
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
502
                performActionOn(selectedElement, (JComponent) e.getSource());
503
                StyleSelector.this.repaint();
504
        }
505

    
506
        public int getUnit() {
507
                return cmbUnits.getSelectedUnitIndex();
508
        }
509

    
510
        public int getReferenceSystem() {
511
                return cmbReferenceSystem.getSelectedIndex();
512
        }
513

    
514
        public void setUnit(int unit) {
515
                cmbUnits.setSelectedUnitIndex(unit);
516
        }
517

    
518
        public void setReferenceSystem(int referenceSystem) {
519
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
520
        }
521

    
522
        public void windowClosed() {
523
                if (!accepted) {
524
                        IStyle clonedStyle = null;
525
                        if (previousStyle != null){
526
                                clonedStyle = SymbologyFactory.createStyleFromXML(
527
                                        ((IStyle)previousStyle).getXMLEntity(), "");
528
                        }
529
                        ((StylePreviewer) jPanelPreview).setStyle(clonedStyle);
530
                }
531
        }
532

    
533
        /**
534
         * Invoked when the RESET button is pressed
535
         */
536
        protected void resetPressed() {
537
                IStyle clonedStyle = null;
538
                if (previousStyle != null){
539
                        clonedStyle = SymbologyFactory.createStyleFromXML(
540
                                ((IStyle)previousStyle).getXMLEntity(), "");
541
                }
542
                setStyle(clonedStyle);
543
        }
544

    
545

    
546
}