Statistics
| Revision:

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

History | View | Annotate | Download (15.2 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/* CVS MESSAGES:
24
 *
25
 * $Id: StyleSelector.java 33213 2010-07-27 08:34:53Z nbrodin $
26
 * $Log$
27
 * Revision 1.11  2007-08-21 09:32:53  jvidal
28
 * javadoc
29
 *
30
 * Revision 1.10  2007/05/31 09:36:22  jaume
31
 * *** empty log message ***
32
 *
33
 * Revision 1.9  2007/05/10 09:47:50  jaume
34
 * *** empty log message ***
35
 *
36
 * Revision 1.8  2007/05/08 15:44:07  jaume
37
 * *** empty log message ***
38
 *
39
 * Revision 1.7  2007/04/27 12:10:17  jaume
40
 * *** empty log message ***
41
 *
42
 * Revision 1.6  2007/04/11 16:02:43  jaume
43
 * file filter
44
 *
45
 * Revision 1.5  2007/04/05 16:08:34  jaume
46
 * Styled labeling stuff
47
 *
48
 * Revision 1.4  2007/04/04 16:01:14  jaume
49
 * *** empty log message ***
50
 *
51
 * Revision 1.2  2007/03/09 11:25:00  jaume
52
 * Advanced symbology (start committing)
53
 *
54
 * Revision 1.1.2.4  2007/02/21 07:35:14  jaume
55
 * *** empty log message ***
56
 *
57
 * Revision 1.1.2.3  2007/02/08 15:43:04  jaume
58
 * some bug fixes in the editor and removed unnecessary imports
59
 *
60
 * Revision 1.1.2.2  2007/01/30 18:10:10  jaume
61
 * start commiting labeling stuff
62
 *
63
 * Revision 1.1.2.1  2007/01/26 13:49:03  jaume
64
 * *** empty log message ***
65
 *
66
 *
67
 */
68
package com.iver.cit.gvsig.gui.styling;
69

    
70
import java.awt.BorderLayout;
71
import java.awt.Color;
72
import java.awt.Component;
73
import java.awt.Dimension;
74
import java.awt.FlowLayout;
75
import java.awt.event.ActionEvent;
76
import java.awt.event.ActionListener;
77
import java.io.File;
78
import java.io.FileWriter;
79
import java.util.prefs.Preferences;
80

    
81
import javax.swing.BorderFactory;
82
import javax.swing.BoxLayout;
83
import javax.swing.JComponent;
84
import javax.swing.JFileChooser;
85
import javax.swing.JLabel;
86
import javax.swing.JList;
87
import javax.swing.JOptionPane;
88
import javax.swing.JPanel;
89
import javax.swing.JTextField;
90
import javax.swing.ListCellRenderer;
91
import javax.swing.event.ListSelectionEvent;
92
import javax.swing.event.ListSelectionListener;
93

    
94
import org.exolab.castor.xml.Marshaller;
95
import org.gvsig.gui.beans.AcceptCancelPanel;
96
import org.gvsig.gui.beans.controls.dnd.JDnDList;
97
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
98

    
99
import com.iver.andami.PluginServices;
100
import com.iver.andami.messages.NotificationManager;
101
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
102
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
103
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
104
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
105
import com.iver.cit.gvsig.gui.JComboBoxUnits;
106
import com.iver.cit.gvsig.gui.panels.ImageSizePanel;
107
import com.iver.utiles.XMLEntity;
108

    
109
/**
110
 * Creates a panel where the user can select a style for an object that allows
111
 * to manage this property.This panel will be similar to the symbol selector panel
112
 * and, on it, the user will have a previsualization of the style of objects
113
 * stored and posibilities to modify an existing one, to create a new one
114
 * and so on.
115
 *
116
 * @author jaume dominguez faus - jaume.dominguez@iver.es
117
 *
118
 */
119
public class StyleSelector extends SymbolSelector{
120
        private static final long serialVersionUID = -7476555713446755512L;
121
        private ImageSizePanel isp;
122
        private IStyle styleType;
123
        private IStyle previousStyle;
124
        private boolean showPanelOptions = true;
125

    
126
        public StyleSelector(IStyle style, int shapeType) {
127
                this(style, shapeType, new StyleSelectorFilter() {
128

    
129
                        public boolean accepts(Object obj) {
130
                                return obj instanceof IStyle;
131
                        }
132

    
133
                        public IStyle getAllowedObject() {
134
                                return null;
135
                        }
136
                });
137
        }
138
        /**
139
         * Constructor method
140
         *
141
         * @param style
142
         * @param shapeType
143
         * @param filter
144
         */
145
        public StyleSelector(IStyle style, int shapeType, StyleSelectorFilter filter) {
146
                this(style, shapeType, filter, true);
147
        }
148

    
149

    
150
        public StyleSelector(IStyle style, int shapeType, StyleSelectorFilter filter, boolean showPanelOptions) {
151
                super(null, shapeType, filter, false);
152

    
153
                this.previousStyle = (IStyle)style;
154
                IStyle clonedStyle = null;
155
                if (style != null){
156
                        clonedStyle = SymbologyFactory.createStyleFromXML(
157
                                ((IStyle)style).getXMLEntity(), "");
158
                }
159

    
160

    
161
                this.showPanelOptions = showPanelOptions;
162

    
163
                styleType = ((StyleSelectorFilter)filter).getAllowedObject();
164
                Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
165
                rootDir = new File(prefs.get("SymbolStylesFolder", com.iver.andami.Launcher.getAppHomeDir() + "Styles"));
166
                if (!rootDir.exists())
167
                        rootDir.mkdir();
168

    
169
                try {
170
                        initialize(clonedStyle);
171
                } catch (ClassNotFoundException e) {
172
                        throw new Error(e);
173
                }
174
                lblTitle.setText(PluginServices.getText(this, "label_styles"));
175
                treeRootName = PluginServices.getText(this, "style_library");
176

    
177
        }
178

    
179

    
180

    
181
        @Override
182
        protected void initialize(Object currentElement) throws ClassNotFoundException {
183
                library = new StyleLibrary(rootDir);
184

    
185
                this.setLayout(new BorderLayout());
186
                this.setSize(400, 221);
187

    
188
                this.add(getJNorthPanel(), BorderLayout.NORTH);
189
                this.add(getJSplitPane(), BorderLayout.CENTER);
190
                this.add(getJEastPanel(), BorderLayout.EAST);
191
                ActionListener okAction = new ActionListener() {
192
                        public void actionPerformed(ActionEvent e) {
193
                                accepted = true;
194
                                PluginServices.getMDIManager().closeWindow(StyleSelector.this);
195
                        }
196
                }, cancelAction = new ActionListener() {
197
                        public void actionPerformed(ActionEvent e) {
198
                                accepted = false;
199
                                PluginServices.getMDIManager().closeWindow(StyleSelector.this);
200
                        }
201
                };
202

    
203
                okCancelPanel = new AcceptCancelPanel();
204
                okCancelPanel.setOkButtonActionListener(okAction);
205
                okCancelPanel.setCancelButtonActionListener(cancelAction);
206

    
207
                this.add(okCancelPanel, BorderLayout.SOUTH);
208
                libraryBrowser.setSelectionRow(0);
209

    
210
                SillyDragNDropAction dndAction = new SillyDragNDropAction();
211
                libraryBrowser.addMouseListener(dndAction);
212
                libraryBrowser.addMouseMotionListener(dndAction);
213
                getJListSymbols().addMouseListener(dndAction);
214
                getJListSymbols().addMouseMotionListener(dndAction);
215

    
216
                setSymbol(currentElement);
217
        }
218

    
219
        @Override
220
        public void setSymbol(Object style) {
221
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) style);
222
                updateOptionsPanel();
223
        }
224

    
225
        @Override
226
        public Object getSelectedObject() {
227
//                if (!accepted) return null;
228
                Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
229

    
230
                if(mySelectedElement == null)
231
                        return null;
232

    
233
                if (mySelectedElement instanceof CartographicSupport) {
234
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
235
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
236
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
237
                }
238

    
239
                return mySelectedElement;
240
        }
241

    
242
        protected SymbolSelectorListModel newListModel() {
243
                StyleSelectorListModel listModel = new StyleSelectorListModel(
244
                                dir,
245
                                sFilter,
246
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
247
                return listModel;
248

    
249
        }
250

    
251
        protected JPanel getJPanelOptions() {
252
                if (jPanelOptions == null) {
253
                        jPanelOptions = new GridBagLayoutPanel();
254
                        if(showPanelOptions){
255
                                jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
256
                                jPanelOptions.addComponent(getImageSizePanel());
257
                                jPanelOptions.addComponent(PluginServices.getText(this, "units"),
258
                                                cmbUnits = new JComboBoxUnits(true));
259
                                jPanelOptions.addComponent("",
260
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
261
                        }
262
                }
263
                return jPanelOptions;
264
        }
265

    
266
        /**
267
         *
268
         * This method initializes ImageSizePanel
269
         *
270
         * @return isp ImageSizePanel
271
         */
272
        private ImageSizePanel getImageSizePanel() {
273
                if (isp == null) {
274
                        isp = new ImageSizePanel();
275
                        isp.addActionListener(new ActionListener() {
276
                                public void actionPerformed(ActionEvent e) {
277
                                        ILabelStyle st = (ILabelStyle) getSelectedObject();
278
                                        if (st != null) {
279
                                                double[] sz = isp.getImageDimension();
280
                                                st.setSize(sz[0], sz[1]);
281
                                        }
282
                                }
283
                        });
284
                }
285

    
286
                return isp;
287
        }
288

    
289
        protected void updateOptionsPanel() {
290
                if(showPanelOptions){
291
                        IStyle s = ((StylePreviewer) jPanelPreview).getStyle();
292
                        if (s == null){
293
                                Dimension sz = new Dimension(0,0);
294
                                getImageSizePanel().setImageSize(sz);
295
                        }
296
                        if (s instanceof ILabelStyle) {
297
                                ILabelStyle lab = (ILabelStyle) s;
298
                                Dimension sz = lab.getSize();
299
                                getImageSizePanel().setImageSize(sz);
300
                        }
301
                }
302
        }
303

    
304

    
305
        /**
306
         * This method initializes jList
307
         *
308
         * @return javax.swing.JList
309
         */
310
        protected JList getJListSymbols() {
311
                if (jListSymbols == null) {
312
                        jListSymbols = new JDnDList();
313
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
314
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
315
                        jListSymbols.setVisibleRowCount(-1);
316
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
317
                                public void valueChanged(ListSelectionEvent e) {
318
                                        if (jListSymbols.getSelectedValue()!=null) {
319
                                                IStyle selStyle = SymbologyFactory.createStyleFromXML(
320
                                                                ((IStyle) jListSymbols.getSelectedValue()).getXMLEntity(), null);
321
                                                setStyle(selStyle);
322
                                                updateOptionsPanel();
323
                                        }
324
                                }
325

    
326
                        });
327
                        ListCellRenderer renderer = new ListCellRenderer() {
328
                                private Color mySelectedBGColor = new Color(255,145,100,255);
329
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
330
                                        IStyle sty = (IStyle) value;
331
                                        JPanel pnl = new JPanel();
332
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
333
                                        pnl.setLayout(layout);
334
                                        Color bgColor = (isSelected) ? mySelectedBGColor
335
                                                        : getJListSymbols().getBackground();
336

    
337
                                        pnl.setBackground(bgColor);
338
                                        StylePreviewer sp = new StylePreviewer();
339
                                        sp.setShowOutline(false);
340
                                        sp.setAlignmentX(Component.CENTER_ALIGNMENT);
341
                                        sp.setPreferredSize(new Dimension(50, 50));
342
                                        sp.setStyle(sty);
343
                                        sp.setBackground(bgColor);
344
                                        pnl.add(sp);
345
                                        JLabel lbl = new JLabel(sty.getDescription());
346
                                        lbl.setBackground(bgColor);
347
                                        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
348
                                        pnl.add(lbl);
349

    
350
                                        return pnl;
351
                                }
352

    
353
                        };
354
                        jListSymbols.setCellRenderer(renderer);
355
                }
356
                return jListSymbols;
357
        }
358
        /**
359
         * Modify the previsualization showed in the panel with the style of the
360
         * new object selected.
361
         *
362
         * @param selectedValue
363
         */
364
        protected void setStyle(Object selectedValue) {
365
                //selectedElement = selectedValue;
366
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) selectedValue);
367
                doLayout();
368
                updateOptionsPanel();
369
                repaint();
370
        }
371

    
372
        protected void newPressed() {
373

    
374
                StyleEditor se = new StyleEditor(styleType);
375
                PluginServices.getMDIManager().addWindow(se);
376
                setStyle(se.getStyle());
377

    
378
        }
379

    
380
        protected void propertiesPressed() {
381
//                boolean state = accepted;
382
//                accepted = true;
383
                StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
384
                if(se != null){
385
                        PluginServices.getMDIManager().addWindow(se);
386
                        setStyle(se.getStyle());
387
                }
388
//                accepted = state;
389
        }
390

    
391
        protected void savePressed() {
392
                if (getSelectedObject() == null)
393
                        return;
394

    
395
                JFileChooser jfc = new JFileChooser(rootDir);
396
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
397
                        public boolean accept(File f) {
398
                                return f.getAbsolutePath().
399
                                toLowerCase().
400
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
401
                        }
402

    
403
                        public String getDescription() {
404
                                return PluginServices.getText(
405
                                                this, "gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
406
                        }
407
                };
408
                jfc.setFileFilter(ff);
409
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
410
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
411
                JTextField txtDesc = new JTextField(25);
412
                txtDesc.setText(((IStyle) getSelectedObject()).getDescription());
413
                accessory.add(txtDesc);
414
                jfc.setAccessory(accessory);
415
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
416
                        File targetFile = jfc.getSelectedFile();
417

    
418
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
419

    
420
                        // apply description
421
                        String desc;
422
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
423
                                // default to file name
424
                                String s = targetFile.getAbsolutePath();
425
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
426
                        } else {
427
                                desc = txtDesc.getText().trim();
428
                        }
429
                        IStyle s = (IStyle) getSelectedObject();
430
                        s.setDescription(desc);
431

    
432
                        // save it
433
                        XMLEntity xml = s.getXMLEntity();
434
                        if (!targetFile.
435
                                        getAbsolutePath().
436
                                        toLowerCase().
437
                                        endsWith(fExtension))
438
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
439
                        if(targetFile.exists()){
440
                                int resp = JOptionPane.showConfirmDialog(
441
                                                (Component) PluginServices.getMainFrame(),
442
                                                PluginServices.getText(this,
443
                                                "fichero_ya_existe_seguro_desea_guardarlo"),
444
                                                PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
445
                                if (resp != JOptionPane.YES_OPTION) {
446
                                        return;
447
                                }
448
                        }
449
                        FileWriter writer;
450
                        try {
451
                                writer = new FileWriter(targetFile.getAbsolutePath());
452
                                Marshaller m = new Marshaller(writer);
453
                                m.setEncoding("ISO-8859-1");
454
                                m.marshal(xml.getXmlTag());
455

    
456
                        } catch (Exception ex) {
457
                                NotificationManager.addError(
458
                                                PluginServices.getText(this, "save_error"), ex);
459
                        }
460
                        getJListSymbols().setModel(newListModel());
461
                }
462
        }
463

    
464
        /**
465
         * This method initializes jPanelPreview
466
         *
467
         * @return javax.swing.JComponent
468
         */
469
        protected JComponent getJPanelPreview() {
470
                if (jPanelPreview == null) {
471
                        jPanelPreview = new StylePreviewer();
472
                        jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
473
                        jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
474
                        ((StylePreviewer) jPanelPreview).setShowOutline(true);
475
                }
476
                return jPanelPreview;
477
        }
478

    
479
        @Override
480
        public void actionPerformed(ActionEvent e) {
481
                if (!act) return;
482
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
483
                performActionOn(selectedElement, (JComponent) e.getSource());
484
                StyleSelector.this.repaint();
485
        }
486

    
487
        public int getUnit() {
488
                return cmbUnits.getSelectedUnitIndex();
489
        }
490

    
491
        public int getReferenceSystem() {
492
                return cmbReferenceSystem.getSelectedIndex();
493
        }
494

    
495
        public void setUnit(int unit) {
496
                cmbUnits.setSelectedUnitIndex(unit);
497
        }
498

    
499
        public void setReferenceSystem(int referenceSystem) {
500
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
501
        }
502

    
503
        public void windowClosed() {
504
                if (!accepted) {
505
                        IStyle clonedStyle = null;
506
                        if (previousStyle != null){
507
                                clonedStyle = SymbologyFactory.createStyleFromXML(
508
                                        ((IStyle)previousStyle).getXMLEntity(), "");
509
                        }
510
                        ((StylePreviewer) jPanelPreview).setStyle(clonedStyle);
511
                }
512
        }
513

    
514
        /**
515
         * Invoked when the RESET button is pressed
516
         */
517
        protected void resetPressed() {
518
                IStyle clonedStyle = null;
519
                if (previousStyle != null){
520
                        clonedStyle = SymbologyFactory.createStyleFromXML(
521
                                ((IStyle)previousStyle).getXMLEntity(), "");
522
                }
523
                setStyle(clonedStyle);
524
        }
525

    
526

    
527
}