Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleSelector.java @ 27278

History | View | Annotate | Download (14.1 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 27278 2009-03-11 08:02:30Z 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.JPanel;
107
import javax.swing.JTextField;
108
import javax.swing.ListCellRenderer;
109
import javax.swing.event.ListSelectionEvent;
110
import javax.swing.event.ListSelectionListener;
111

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

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

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

    
142
        public StyleSelector(IStyle style, int shapeType) {
143
                this(style, shapeType, new StyleSelectorFilter() {
144

    
145
                        public boolean accepts(Object obj) {
146
                                return obj instanceof IStyle;
147
                        }
148

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

    
165

    
166
        public StyleSelector(IStyle style, int shapeType, StyleSelectorFilter filter, boolean showPanelOptions) {
167
                super(null, shapeType, filter, false);
168

    
169
                this.showPanelOptions = showPanelOptions;
170

    
171
                styleType = ((StyleSelectorFilter)filter).getAllowedObject();
172
                Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
173
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
174
                if (!rootDir.exists())
175
                        rootDir.mkdir();
176

    
177
                try {
178
                        initialize(style);
179
                } catch (ClassNotFoundException e) {
180
                        throw new Error(e);
181
                }
182
                lblTitle.setText(PluginServices.getText(this, "label_styles"));
183
                treeRootName = PluginServices.getText(this, "style_library");
184

    
185
        }
186

    
187

    
188

    
189
        @Override
190
        protected void initialize(Object currentElement) throws ClassNotFoundException {
191
                library = new StyleLibrary(rootDir);
192

    
193
                this.setLayout(new BorderLayout());
194
                this.setSize(400, 221);
195

    
196
                this.add(getJNorthPanel(), BorderLayout.NORTH);
197
                this.add(getJSplitPane(), BorderLayout.CENTER);
198
                this.add(getJEastPanel(), BorderLayout.EAST);
199
                ActionListener okAction = new ActionListener() {
200
                        public void actionPerformed(ActionEvent e) {
201
                                accepted = true;
202
                                PluginServices.getMDIManager().closeWindow(StyleSelector.this);
203
                        }
204
                }, cancelAction = new ActionListener() {
205
                        public void actionPerformed(ActionEvent e) {
206
                                accepted = false;
207
                                PluginServices.getMDIManager().closeWindow(StyleSelector.this);
208
                        }
209
                };
210

    
211
                okCancelPanel = new AcceptCancelPanel();
212
                okCancelPanel.setOkButtonActionListener(okAction);
213
                okCancelPanel.setCancelButtonActionListener(cancelAction);
214

    
215
                this.add(okCancelPanel, BorderLayout.SOUTH);
216
                libraryBrowser.setSelectionRow(0);
217

    
218
                SillyDragNDropAction dndAction = new SillyDragNDropAction();
219
                libraryBrowser.addMouseListener(dndAction);
220
                libraryBrowser.addMouseMotionListener(dndAction);
221
                getJListSymbols().addMouseListener(dndAction);
222
                getJListSymbols().addMouseMotionListener(dndAction);
223
                setSymbol(currentElement);
224
        }
225

    
226
        @Override
227
        public void setSymbol(Object style) {
228
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) style);
229
                updateOptionsPanel();
230
        }
231

    
232
        @Override
233
        public Object getSelectedObject() {
234
//                if (!accepted) return null;
235
                Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
236

    
237
                if(mySelectedElement == null)
238
                        return null;
239

    
240
                if (mySelectedElement instanceof CartographicSupport) {
241
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
242
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
243
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
244
                }
245

    
246
                return mySelectedElement;
247
        }
248

    
249
        protected SymbolSelectorListModel newListModel() {
250
                StyleSelectorListModel listModel = new StyleSelectorListModel(
251
                                dir,
252
                                sFilter,
253
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
254
                return listModel;
255

    
256
        }
257

    
258
        protected JPanel getJPanelOptions() {
259
                if (jPanelOptions == null) {
260
                        jPanelOptions = new GridBagLayoutPanel();
261
                        if(showPanelOptions){
262
                                jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
263
                                jPanelOptions.addComponent(getImageSizePanel());
264
                                jPanelOptions.addComponent(PluginServices.getText(this, "units"),
265
                                                cmbUnits = new JComboBoxUnits(true));
266
                                jPanelOptions.addComponent("",
267
                                                cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
268
                        }
269
                }
270
                return jPanelOptions;
271
        }
272

    
273
        /**
274
         *
275
         * This method initializes ImageSizePanel
276
         *
277
         * @return isp ImageSizePanel
278
         */
279
        private ImageSizePanel getImageSizePanel() {
280
                if (isp == null) {
281
                        isp = new ImageSizePanel();
282
                        isp.addActionListener(new ActionListener() {
283
                                public void actionPerformed(ActionEvent e) {
284
                                        ILabelStyle st = (ILabelStyle) getSelectedObject();
285
                                        if (st != null) {
286
                                                double[] sz = isp.getImageDimension();
287
                                                st.setSize(sz[0], sz[1]);
288
                                        }
289
                                }
290
                        });
291
                }
292

    
293
                return isp;
294
        }
295

    
296
        protected void updateOptionsPanel() {
297
                if(showPanelOptions){
298
                        IStyle s = ((StylePreviewer) jPanelPreview).getStyle();
299
                        if (s instanceof ILabelStyle) {
300
                                ILabelStyle lab = (ILabelStyle) s;
301
                                Dimension sz = lab.getSize();
302
                                getImageSizePanel().setImageSize(sz);
303
                        }
304
                }
305
        }
306

    
307

    
308
        /**
309
         * This method initializes jList
310
         *
311
         * @return javax.swing.JList
312
         */
313
        protected JList getJListSymbols() {
314
                if (jListSymbols == null) {
315
                        jListSymbols = new JDnDList();
316
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
317
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
318
                        jListSymbols.setVisibleRowCount(-1);
319
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
320
                                public void valueChanged(ListSelectionEvent e) {
321
                                        setStyle(jListSymbols.getSelectedValue());
322
                                        updateOptionsPanel();
323
                                }
324
                        });
325
                        ListCellRenderer renderer = new ListCellRenderer() {
326
                                private Color mySelectedBGColor = new Color(255,145,100,255);
327
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
328
                                        IStyle sty = (IStyle) value;
329
                                        JPanel pnl = new JPanel();
330
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
331
                                        pnl.setLayout(layout);
332
                                        Color bgColor = (isSelected) ? mySelectedBGColor
333
                                                        : getJListSymbols().getBackground();
334

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

    
348
                                        return pnl;
349
                                }
350

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

    
369
        protected void newPressed() {
370

    
371
                StyleEditor se = new StyleEditor(styleType);
372
                PluginServices.getMDIManager().addWindow(se);
373
                setStyle(se.getStyle());
374

    
375
        }
376

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

    
388
        protected void savePressed() {
389
                if (getSelectedObject() == null)
390
                        return;
391

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

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

    
415
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
416

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

    
429
                        // save it
430
                        XMLEntity xml = s.getXMLEntity();
431
                        if (!targetFile.
432
                                        getAbsolutePath().
433
                                        toLowerCase().
434
                                        endsWith(fExtension))
435
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
436
                        FileWriter writer;
437
                        try {
438
                                writer = new FileWriter(targetFile.getAbsolutePath());
439
                                Marshaller m = new Marshaller(writer);
440
                                m.setEncoding("ISO-8859-1");
441
                                m.marshal(xml.getXmlTag());
442

    
443
                        } catch (Exception ex) {
444
                                NotificationManager.addError(
445
                                                PluginServices.getText(this, "save_error"), ex);
446
                        }
447
                        getJListSymbols().setModel(newListModel());
448
                }
449
        }
450

    
451
        /**
452
         * This method initializes jPanelPreview
453
         *
454
         * @return javax.swing.JComponent
455
         */
456
        protected JComponent getJPanelPreview() {
457
                if (jPanelPreview == null) {
458
                        jPanelPreview = new StylePreviewer();
459
                        jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
460
                        jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
461
                        ((StylePreviewer) jPanelPreview).setShowOutline(true);
462
                }
463
                return jPanelPreview;
464
        }
465

    
466
        @Override
467
        public void actionPerformed(ActionEvent e) {
468
                if (!act) return;
469
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
470
                performActionOn(selectedElement, (JComponent) e.getSource());
471
                StyleSelector.this.repaint();
472
        }
473

    
474
        public int getUnit() {
475
                return cmbUnits.getSelectedUnitIndex();
476
        }
477

    
478
        public int getReferenceSystem() {
479
                return cmbReferenceSystem.getSelectedIndex();
480
        }
481

    
482
        public void setUnit(int unit) {
483
                cmbUnits.setSelectedUnitIndex(unit);
484
        }
485

    
486
        public void setReferenceSystem(int referenceSystem) {
487
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
488
        }
489

    
490
        public void windowClosed() {
491
                if (!accepted) {
492
                        ((StylePreviewer) jPanelPreview).setStyle(null);
493
                }
494
        }
495

    
496
}