Statistics
| Revision:

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

History | View | Annotate | Download (10.2 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 11141 2007-04-11 16:02:43Z jaume $
45
* $Log$
46
* Revision 1.6  2007-04-11 16:02:43  jaume
47
* file filter
48
*
49
* Revision 1.5  2007/04/05 16:08:34  jaume
50
* Styled labeling stuff
51
*
52
* Revision 1.4  2007/04/04 16:01:14  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.2  2007/03/09 11:25:00  jaume
56
* Advanced symbology (start committing)
57
*
58
* Revision 1.1.2.4  2007/02/21 07:35:14  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.1.2.3  2007/02/08 15:43:04  jaume
62
* some bug fixes in the editor and removed unnecessary imports
63
*
64
* Revision 1.1.2.2  2007/01/30 18:10:10  jaume
65
* start commiting labeling stuff
66
*
67
* Revision 1.1.2.1  2007/01/26 13:49:03  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.gui.styling;
73

    
74
import java.awt.Color;
75
import java.awt.Component;
76
import java.awt.Dimension;
77
import java.awt.FlowLayout;
78
import java.io.File;
79
import java.io.FileFilter;
80
import java.io.FileWriter;
81
import java.util.prefs.Preferences;
82

    
83
import javax.swing.BoxLayout;
84
import javax.swing.JComponent;
85
import javax.swing.JFileChooser;
86
import javax.swing.JLabel;
87
import javax.swing.JList;
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
import javax.swing.event.TreeModelListener;
94
import javax.swing.tree.TreeModel;
95
import javax.swing.tree.TreePath;
96

    
97
import org.exolab.castor.xml.Marshaller;
98
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
99

    
100
import com.iver.andami.PluginServices;
101
import com.iver.andami.messages.NotificationManager;
102
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
103
import com.iver.utiles.XMLEntity;
104

    
105
/**
106
 *
107
 * @author jaume dominguez faus - jaume.dominguez@iver.es
108
 *
109
 */
110
public class StyleSelector extends SymbolSelector {
111

    
112
        public StyleSelector(IStyle style, int shapeType) {
113
                this(style, shapeType, new SelectorFilter() {
114
                        public boolean accepts(Object obj) {
115
                                return obj instanceof IStyle;
116
                        }
117
                });
118
        }
119

    
120
        public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) {
121
                super(null, shapeType, filter);
122
                selectedElement = style;
123
            Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
124
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
125
                if (!rootDir.exists())
126
                        rootDir.mkdir();
127
                lblTitle.setText(PluginServices.getText(this, "label_styles"));
128
                treeRootName = PluginServices.getText(this, "style_library");
129
                treeModel = new TreeModel() {
130
                    final class MyFile extends File {
131
                            public MyFile(String pathname) {
132
                                    super(pathname);
133
                            }
134

    
135
                            public String toString() {
136
                                    if (this.equals(root))
137
                                            return treeRootName;
138
                                    String path = getAbsolutePath();
139
                                    String prefixToRemove = rootDir.getAbsolutePath();
140
                                    path = path.substring(prefixToRemove.length()+1, path.length());
141
                                    return path;
142
                            }
143
                    };
144
                    MyFile root = new MyFile(rootDir.getAbsolutePath());
145

    
146
                    private FileFilter ff = new FileFilter() {
147
                            public boolean accept(File pathname) {
148
                                    return pathname.isDirectory();
149
                            }
150
                    };
151

    
152
                    public Object getRoot() {
153
                            return root;
154
                    }
155

    
156
                    public int getChildCount(Object parent) {
157
                            return ((File) parent).listFiles(ff).length;
158
                    }
159

    
160
                    public boolean isLeaf(Object node) {
161
                            return getChildCount(node)==0;
162
                    }
163

    
164
                    public void addTreeModelListener(TreeModelListener l) {
165
                            // TODO Necessite?
166

    
167
                    }
168

    
169
                    public void removeTreeModelListener(TreeModelListener l) {
170
                            // TODO Necessite?
171
                    }
172

    
173
                    public Object getChild(Object parent, int index) {
174
                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
175
                    }
176

    
177
                    public int getIndexOfChild(Object parent, Object child) {
178
                            if (parent == null)
179
                                    return -1;
180
                            File[] files = ((File) parent).listFiles(ff);
181
                            for (int i = 0; i < files.length; i++) {
182
                                    if (files[i].equals((File) child))
183
                                            return i;
184
                            }
185
                            return -1;
186
                    }
187

    
188
                    public void valueForPathChanged(TreePath path, Object newValue) {
189
                            // TODO Auto-generated method stub
190
                    }
191

    
192
            };
193
                getTreeFav().setModel(treeModel);
194
        }
195

    
196
        protected SymbolSelectorListModel newListModel() {
197
                StyleSelectorListModel listModel = new StyleSelectorListModel(
198
                                dir,
199
                                selectedElement,
200
                                sFilter,
201
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
202
                return listModel;
203

    
204
        }
205

    
206
        protected JPanel getJPanelOptions() {
207
                if (jPanelOptions == null) {
208
                        jPanelOptions = new GridBagLayoutPanel();
209
                        // nothing in the panel
210

    
211
                }
212
            return jPanelOptions;
213
    }
214

    
215
    /**
216
     * This method initializes jList
217
     *
218
     * @return javax.swing.JList
219
     */
220
    protected JList getJListSymbols() {
221
            if (jListSymbols == null) {
222
                    jListSymbols = new JList();
223
                    jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
224
            jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
225
            jListSymbols.setVisibleRowCount(-1);
226
            jListSymbols.addListSelectionListener(new ListSelectionListener() {
227
                    public void valueChanged(ListSelectionEvent e) {
228
                            setStyle(jListSymbols.getSelectedValue());
229
                            updateOptionsPanel();
230
                    }
231
            });
232
            ListCellRenderer renderer = new ListCellRenderer() {
233
                        private Color mySelectedBGColor = new Color(255,145,100,255);
234
                            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
235
                                    IStyle sty = (IStyle) value;
236
                                    JPanel pnl = new JPanel();
237
                                    BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
238
                                    pnl.setLayout(layout);
239
                                    Color bgColor = (isSelected) ? mySelectedBGColor
240
                                                             : getJListSymbols().getBackground();
241

    
242
                                    pnl.setBackground(bgColor);
243
                                    StylePreviewer sp = new StylePreviewer();
244
                                    sp.setShowOutline(false);
245
                                    sp.setAlignmentX(Component.CENTER_ALIGNMENT);
246
                                    sp.setPreferredSize(new Dimension(50, 50));
247
                                    sp.setStyle(sty);
248
                                    sp.setBackground(bgColor);
249
                                    pnl.add(sp);
250
                                    JLabel lbl = new JLabel(sty.getDescription());
251
                                    lbl.setBackground(bgColor);
252
                                    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
253
                                    pnl.add(lbl);
254

    
255
                                    return pnl;
256
                            }
257

    
258
                };
259
                jListSymbols.setCellRenderer(renderer);
260
            }
261
            return jListSymbols;
262
    }
263

    
264
        protected void setStyle(Object selectedValue) {
265
                // TODO missing change SymbolPreviewer by a StylePrevier and apply it
266
                // the new style
267
                selectedElement = selectedValue;
268
        }
269

    
270
        protected void propertiesPressed() {
271
                StyleEditor se = new StyleEditor((IStyle) selectedElement);
272
                PluginServices.getMDIManager().addWindow(se);
273
                setStyle(se.getStyle());
274
        }
275

    
276
        protected void savePressed() {
277
                if (getSelectedObject() ==null)
278
                        return;
279

    
280

    
281
                JFileChooser jfc = new JFileChooser(rootDir);
282
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
283
                        public boolean accept(File f) {
284
                                return f.getAbsolutePath().
285
                                toLowerCase().
286
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
287
                        }
288

    
289
                        public String getDescription() {
290
                                return PluginServices.getText(
291
                                                this, "gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
292
                        }
293
                };
294
                jfc.setFileFilter(ff);
295
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
296
                accessory.add(new JLabel(PluginServices.getText(this, "enter_description")));
297
                JTextField txtDesc = new JTextField(25);
298
                accessory.add(txtDesc);
299
                jfc.setAccessory(accessory);
300
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
301
                        File targetFile = jfc.getSelectedFile();
302

    
303
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
304

    
305
                        // apply description
306
                        String desc;
307
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
308
                                // default to file name
309
                                String s = targetFile.getAbsolutePath();
310
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
311
                        } else {
312
                                desc = txtDesc.getText().trim();
313
                        }
314
                        IStyle s = (IStyle) getSelectedObject();
315
                        s.setDescription(desc);
316

    
317
                        // save it
318
                        XMLEntity xml = s.getXMLEntity();
319
                        if (!targetFile.
320
                                        getAbsolutePath().
321
                                        toLowerCase().
322
                                        endsWith(fExtension))
323
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
324
                        FileWriter writer;
325
                        try {
326
                                writer = new FileWriter(targetFile.getAbsolutePath());
327
                                Marshaller m = new Marshaller(writer);
328
                                m.setEncoding("ISO-8859-1");
329
                                m.marshal(xml.getXmlTag());
330

    
331
                        } catch (Exception ex) {
332
                                NotificationManager.addError(
333
                                                PluginServices.getText(this, "save_error"), ex);
334
                        }
335
                        getJListSymbols().setModel(newListModel());
336
                }
337
        }
338

    
339
    /**
340
     * This method initializes jPanel1
341
     *
342
     * @return javax.swing.JPanel
343
     */
344
    protected JComponent getJPanelPreview() {
345
            if (jPanelPreview == null) {
346
                    jPanelPreview = new SymbolPreviewer();
347
                    jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
348
                    jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
349
            }
350
            return jPanelPreview;
351
    }
352
}