Statistics
| Revision:

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

History | View | Annotate | Download (13.6 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 22951 2008-08-28 06:29:37Z 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

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

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

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

    
163
                styleType = ((AbstractStyleSelectorFilter)filter).getAllowedObject();
164
                Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
165
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
166
                if (!rootDir.exists())
167
                        rootDir.mkdir();
168

    
169
                try {
170
                        initialize(style);
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
        @Override
180
        protected void initialize(Object currentElement) throws ClassNotFoundException {
181
                library = new StyleLibrary(rootDir);
182

    
183
                this.setLayout(new BorderLayout());
184
                this.setSize(400, 221);
185

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

    
201
                okCancelPanel = new AcceptCancelPanel();
202
                okCancelPanel.setOkButtonActionListener(okAction);
203
                okCancelPanel.setCancelButtonActionListener(cancelAction);
204

    
205
                this.add(okCancelPanel, BorderLayout.SOUTH);
206
                libraryBrowser.setSelectionRow(0);
207

    
208
                SillyDragNDropAction dndAction = new SillyDragNDropAction();
209
                libraryBrowser.addMouseListener(dndAction);
210
                libraryBrowser.addMouseMotionListener(dndAction);
211
                getJListSymbols().addMouseListener(dndAction);
212
                getJListSymbols().addMouseMotionListener(dndAction);
213
                setSymbol(currentElement);
214
        }
215

    
216
        @Override
217
        public void setSymbol(Object style) {
218
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) style);
219
                updateOptionsPanel();
220
        }
221

    
222
        @Override
223
        public Object getSelectedObject() {
224
                if (!accepted) return null;
225
                Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
226

    
227
                if (mySelectedElement instanceof CartographicSupport) {
228
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
229
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
230
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
231
                }
232

    
233
                return mySelectedElement;
234
        }
235

    
236
        protected SymbolSelectorListModel newListModel() {
237
                StyleSelectorListModel listModel = new StyleSelectorListModel(
238
                                dir,
239
                                sFilter,
240
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
241
                return listModel;
242

    
243
        }
244

    
245
        protected JPanel getJPanelOptions() {
246
                if (jPanelOptions == null) {
247
                        jPanelOptions = new GridBagLayoutPanel();
248
                        jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
249
                        jPanelOptions.addComponent(getImageSizePanel());
250
                        jPanelOptions.addComponent(PluginServices.getText(this, "units"),
251
                                        cmbUnits = new JComboBoxUnits(true));
252
                        jPanelOptions.addComponent("",
253
                                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
254
                }
255
                return jPanelOptions;
256
        }
257

    
258
        /**
259
         *
260
         * This method initializes ImageSizePanel
261
         *
262
         * @return isp ImageSizePanel
263
         */
264
        private ImageSizePanel getImageSizePanel() {
265
                if (isp == null) {
266
                        isp = new ImageSizePanel();
267
                        isp.addActionListener(new ActionListener() {
268
                                public void actionPerformed(ActionEvent e) {
269
                                        ILabelStyle st = (ILabelStyle) getSelectedObject();
270
                                        if (st != null) {
271
                                                double[] sz = isp.getImageDimension();
272
                                                st.setSize(sz[0], sz[1]);
273
                                        }
274
                                }
275
                        });
276
                }
277

    
278
                return isp;
279
        }
280

    
281
        protected void updateOptionsPanel() {
282
                IStyle s = ((StylePreviewer) jPanelPreview).getStyle();
283
                if (s instanceof ILabelStyle) {
284
                        ILabelStyle lab = (ILabelStyle) s;
285
                        Dimension sz = lab.getSize();
286
                        getImageSizePanel().setImageSize(sz);
287
                }
288
        }
289

    
290

    
291
        /**
292
         * This method initializes jList
293
         *
294
         * @return javax.swing.JList
295
         */
296
        protected JList getJListSymbols() {
297
                if (jListSymbols == null) {
298
                        jListSymbols = new JDnDList();
299
                        jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
300
                        jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
301
                        jListSymbols.setVisibleRowCount(-1);
302
                        jListSymbols.addListSelectionListener(new ListSelectionListener() {
303
                                public void valueChanged(ListSelectionEvent e) {
304
                                        setStyle(jListSymbols.getSelectedValue());
305
                                        updateOptionsPanel();
306
                                }
307
                        });
308
                        ListCellRenderer renderer = new ListCellRenderer() {
309
                                private Color mySelectedBGColor = new Color(255,145,100,255);
310
                                public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
311
                                        IStyle sty = (IStyle) value;
312
                                        JPanel pnl = new JPanel();
313
                                        BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
314
                                        pnl.setLayout(layout);
315
                                        Color bgColor = (isSelected) ? mySelectedBGColor
316
                                                        : getJListSymbols().getBackground();
317

    
318
                                        pnl.setBackground(bgColor);
319
                                        StylePreviewer sp = new StylePreviewer();
320
                                        sp.setShowOutline(false);
321
                                        sp.setAlignmentX(Component.CENTER_ALIGNMENT);
322
                                        sp.setPreferredSize(new Dimension(50, 50));
323
                                        sp.setStyle(sty);
324
                                        sp.setBackground(bgColor);
325
                                        pnl.add(sp);
326
                                        JLabel lbl = new JLabel(sty.getDescription());
327
                                        lbl.setBackground(bgColor);
328
                                        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
329
                                        pnl.add(lbl);
330

    
331
                                        return pnl;
332
                                }
333

    
334
                        };
335
                        jListSymbols.setCellRenderer(renderer);
336
                }
337
                return jListSymbols;
338
        }
339
        /**
340
         * Modify the previsualization showed in the panel with the style of the
341
         * new object selected.
342
         *
343
         * @param selectedValue
344
         */
345
        protected void setStyle(Object selectedValue) {
346
                //selectedElement = selectedValue;
347
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) selectedValue);
348
                doLayout();
349
                repaint();
350
        }
351

    
352
        protected void newPressed() {
353

    
354
                StyleEditor se = new StyleEditor(styleType);
355
                PluginServices.getMDIManager().addWindow(se);
356
                setStyle(se.getStyle());
357

    
358
        }
359

    
360
        protected void propertiesPressed() {
361
                StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
362
                if(se != null){
363
                        PluginServices.getMDIManager().addWindow(se);
364
                        setStyle(se.getStyle());
365
                }
366
        }
367

    
368
        protected void savePressed() {
369
                if (getSelectedObject() ==null)
370
                        return;
371

    
372
                JFileChooser jfc = new JFileChooser(rootDir);
373
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
374
                        public boolean accept(File f) {
375
                                return f.getAbsolutePath().
376
                                toLowerCase().
377
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
378
                        }
379

    
380
                        public String getDescription() {
381
                                return PluginServices.getText(
382
                                                this, "gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
383
                        }
384
                };
385
                jfc.setFileFilter(ff);
386
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
387
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
388
                JTextField txtDesc = new JTextField(25);
389
                txtDesc.setText(((IStyle) getSelectedObject()).getDescription());
390
                accessory.add(txtDesc);
391
                jfc.setAccessory(accessory);
392
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
393
                        File targetFile = jfc.getSelectedFile();
394

    
395
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
396

    
397
                        // apply description
398
                        String desc;
399
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
400
                                // default to file name
401
                                String s = targetFile.getAbsolutePath();
402
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
403
                        } else {
404
                                desc = txtDesc.getText().trim();
405
                        }
406
                        IStyle s = (IStyle) getSelectedObject();
407
                        s.setDescription(desc);
408

    
409
                        // save it
410
                        XMLEntity xml = s.getXMLEntity();
411
                        if (!targetFile.
412
                                        getAbsolutePath().
413
                                        toLowerCase().
414
                                        endsWith(fExtension))
415
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
416
                        FileWriter writer;
417
                        try {
418
                                writer = new FileWriter(targetFile.getAbsolutePath());
419
                                Marshaller m = new Marshaller(writer);
420
                                m.setEncoding("ISO-8859-1");
421
                                m.marshal(xml.getXmlTag());
422

    
423
                        } catch (Exception ex) {
424
                                NotificationManager.addError(
425
                                                PluginServices.getText(this, "save_error"), ex);
426
                        }
427
                        getJListSymbols().setModel(newListModel());
428
                }
429
        }
430

    
431
        /**
432
         * This method initializes jPanelPreview
433
         *
434
         * @return javax.swing.JComponent
435
         */
436
        protected JComponent getJPanelPreview() {
437
                if (jPanelPreview == null) {
438
                        jPanelPreview = new StylePreviewer();
439
                        jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
440
                        jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
441
                        ((StylePreviewer) jPanelPreview).setShowOutline(true);
442
                }
443
                return jPanelPreview;
444
        }
445

    
446
        @Override
447
        public void actionPerformed(ActionEvent e) {
448
                if (!act) return;
449
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
450
                performActionOn(selectedElement, e);
451
                StyleSelector.this.repaint();
452
        }
453

    
454
        public int getUnit() {
455
                return cmbUnits.getSelectedUnitIndex();
456
        }
457

    
458
        public int getReferenceSystem() {
459
                return cmbReferenceSystem.getSelectedIndex();
460
        }
461

    
462
        public void setUnit(int unit) {
463
                cmbUnits.setSelectedUnitIndex(unit);
464
        }
465

    
466
        public void setReferenceSystem(int referenceSystem) {
467
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
468
        }
469
}