Statistics
| Revision:

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

History | View | Annotate | Download (12.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 13189 2007-08-21 09:33:59Z jvidal $
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.Color;
90
import java.awt.Component;
91
import java.awt.Dimension;
92
import java.awt.FlowLayout;
93
import java.awt.Rectangle;
94
import java.awt.event.ActionEvent;
95
import java.awt.event.ActionListener;
96
import java.io.File;
97
import java.io.FileFilter;
98
import java.io.FileWriter;
99
import java.util.prefs.Preferences;
100

    
101
import javax.swing.BorderFactory;
102
import javax.swing.BoxLayout;
103
import javax.swing.JComponent;
104
import javax.swing.JFileChooser;
105
import javax.swing.JLabel;
106
import javax.swing.JList;
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
import javax.swing.event.TreeModelListener;
113
import javax.swing.tree.TreeModel;
114
import javax.swing.tree.TreePath;
115

    
116
import org.exolab.castor.xml.Marshaller;
117
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
118

    
119
import com.iver.andami.PluginServices;
120
import com.iver.andami.messages.NotificationManager;
121
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
122
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
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 ImageSizePanel isp;
138

    
139
        public StyleSelector(IStyle style, int shapeType) {
140
                this(style, shapeType, new SelectorFilter() {
141
                        public boolean accepts(Object obj) {
142
                                return obj instanceof IStyle;
143
                        }
144
                });
145
        }
146
        /**
147
         * Constructor method
148
         *
149
         * @param style
150
         * @param shapeType
151
         * @param filter
152
         */
153
        public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) {
154
                super(null, shapeType, filter);
155

    
156
            Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
157
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
158
                if (!rootDir.exists())
159
                        rootDir.mkdir();
160
                lblTitle.setText(PluginServices.getText(this, "label_styles"));
161
                treeRootName = PluginServices.getText(this, "style_library");
162
                treeModel = new TreeModel() {
163
                    final class MyFile extends File {
164
                            public MyFile(String pathname) {
165
                                    super(pathname);
166
                            }
167

    
168
                            public String toString() {
169
                                    if (this.equals(root))
170
                                            return treeRootName;
171
                                    String path = getAbsolutePath();
172
                                    String prefixToRemove = rootDir.getAbsolutePath();
173
                                    path = path.substring(prefixToRemove.length()+1, path.length());
174
                                    return path;
175
                            }
176
                    };
177
                    MyFile root = new MyFile(rootDir.getAbsolutePath());
178

    
179
                    private FileFilter ff = new FileFilter() {
180
                            public boolean accept(File pathname) {
181
                                    return pathname.isDirectory();
182
                            }
183
                    };
184

    
185
                    public Object getRoot() {
186
                            return root;
187
                    }
188

    
189
                    public int getChildCount(Object parent) {
190
                            return ((File) parent).listFiles(ff).length;
191
                    }
192

    
193
                    public boolean isLeaf(Object node) {
194
                            return getChildCount(node)==0;
195
                    }
196

    
197
                    public void addTreeModelListener(TreeModelListener l) {        }
198
                    public void removeTreeModelListener(TreeModelListener l) { }
199
                    public void valueForPathChanged(TreePath path, Object newValue) {}
200

    
201
                    public Object getChild(Object parent, int index) {
202
                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
203
                    }
204

    
205
                    public int getIndexOfChild(Object parent, Object child) {
206
                            if (parent == null)
207
                                    return -1;
208
                            File[] files = ((File) parent).listFiles(ff);
209
                            for (int i = 0; i < files.length; i++) {
210
                                    if (files[i].equals((File) child))
211
                                            return i;
212
                            }
213
                            return -1;
214
                    }
215

    
216

    
217

    
218
            };
219
                getTreeFav().setModel(treeModel);
220

    
221
                ActionListener cancelAction = new ActionListener() {
222
                    public void actionPerformed(ActionEvent e) {
223
                            setStyle(null);
224
                            PluginServices.getMDIManager().closeWindow(StyleSelector.this);
225
                    }
226
            };
227
            okCancelPanel.setCancelButtonActionListener(cancelAction);
228

    
229
        }
230

    
231
        protected SymbolSelectorListModel newListModel() {
232
                StyleSelectorListModel listModel = new StyleSelectorListModel(
233
                                dir,
234
                                selectedElement,
235
                                sFilter,
236
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
237
                return listModel;
238

    
239
        }
240

    
241
        protected JPanel getJPanelOptions() {
242
                if (jPanelOptions == null) {
243
                        jPanelOptions = new GridBagLayoutPanel();
244
                        jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, PluginServices.getText(this, "options")));
245
                        jPanelOptions.addComponent(getImageSizePanel());
246

    
247
                }
248
            return jPanelOptions;
249
    }
250
    /**
251
     * This method initializes ImageSizePanel
252
     *
253
     * @return isp ImageSizePanel
254
     */
255
         private ImageSizePanel getImageSizePanel() {
256
                 if (isp == null) {
257
                        isp = new ImageSizePanel();
258
                        isp.addActionListener(new ActionListener() {
259
                                public void actionPerformed(ActionEvent e) {
260
                                        ILabelStyle st = (ILabelStyle) selectedElement;
261
                                        double[] sz = isp.getImageDimension();
262
                                        st.setSize(sz[0], sz[1], isp.getUnitConversionFactor());
263
                                }
264
                        });
265
                }
266

    
267
                return isp;
268
        }
269

    
270
        protected void updateOptionsPanel() {
271
                IStyle s = ((IStyle) jListSymbols.getSelectedValue());
272
            if (s instanceof ILabelStyle) {
273
                        ILabelStyle lab = (ILabelStyle) s;
274
                        Rectangle r = lab.getBounds(null);
275
                        getImageSizePanel().setImageDimension(r.getWidth(), r.getHeight());
276

    
277
                }
278
         }
279

    
280

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

    
308
                                    pnl.setBackground(bgColor);
309
                                    StylePreviewer sp = new StylePreviewer();
310
                                    sp.setShowOutline(false);
311
                                    sp.setAlignmentX(Component.CENTER_ALIGNMENT);
312
                                    sp.setPreferredSize(new Dimension(50, 50));
313
                                    sp.setStyle(sty);
314
                                    sp.setBackground(bgColor);
315
                                    pnl.add(sp);
316
                                    JLabel lbl = new JLabel(sty.getDescription());
317
                                    lbl.setBackground(bgColor);
318
                                    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
319
                                    pnl.add(lbl);
320

    
321
                                    return pnl;
322
                            }
323

    
324
                };
325
                jListSymbols.setCellRenderer(renderer);
326
            }
327
            return jListSymbols;
328
    }
329
    /**
330
     * Modify the previsualization showed in the panel with the style of the
331
     * new object selected.
332
     *
333
     * @param selectedValue
334
     */
335
   protected void setStyle(Object selectedValue) {
336
                selectedElement = selectedValue;
337
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) selectedValue);
338
                doLayout();
339
                repaint();
340
        }
341

    
342
        protected void propertiesPressed() {
343
                StyleEditor se = new StyleEditor((IStyle) selectedElement);
344
                PluginServices.getMDIManager().addWindow(se);
345
                setStyle(se.getStyle());
346
        }
347

    
348
        protected void savePressed() {
349
                if (getSelectedObject() ==null)
350
                        return;
351

    
352
                JFileChooser jfc = new JFileChooser(rootDir);
353
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
354
                        public boolean accept(File f) {
355
                                return f.getAbsolutePath().
356
                                toLowerCase().
357
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
358
                        }
359

    
360
                        public String getDescription() {
361
                                return PluginServices.getText(
362
                                                this, "gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
363
                        }
364
                };
365
                jfc.setFileFilter(ff);
366
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
367
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
368
                JTextField txtDesc = new JTextField(25);
369
                accessory.add(txtDesc);
370
                jfc.setAccessory(accessory);
371
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
372
                        File targetFile = jfc.getSelectedFile();
373

    
374
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
375

    
376
                        // apply description
377
                        String desc;
378
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
379
                                // default to file name
380
                                String s = targetFile.getAbsolutePath();
381
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
382
                        } else {
383
                                desc = txtDesc.getText().trim();
384
                        }
385
                        IStyle s = (IStyle) getSelectedObject();
386
                        s.setDescription(desc);
387

    
388
                        // save it
389
                        XMLEntity xml = s.getXMLEntity();
390
                        if (!targetFile.
391
                                        getAbsolutePath().
392
                                        toLowerCase().
393
                                        endsWith(fExtension))
394
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
395
                        FileWriter writer;
396
                        try {
397
                                writer = new FileWriter(targetFile.getAbsolutePath());
398
                                Marshaller m = new Marshaller(writer);
399
                                m.setEncoding("ISO-8859-1");
400
                                m.marshal(xml.getXmlTag());
401

    
402
                        } catch (Exception ex) {
403
                                NotificationManager.addError(
404
                                                PluginServices.getText(this, "save_error"), ex);
405
                        }
406
                        getJListSymbols().setModel(newListModel());
407
                }
408
        }
409

    
410
    /**
411
     * This method initializes jPanelPreview
412
     *
413
     * @return javax.swing.JComponent
414
     */
415
    protected JComponent getJPanelPreview() {
416
            if (jPanelPreview == null) {
417
                    jPanelPreview = new StylePreviewer();
418
                    jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
419
                    jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
420
                    ((StylePreviewer) jPanelPreview).setShowOutline(true);
421
            }
422
            return jPanelPreview;
423
    }
424
}