Statistics
| Revision:

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

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

    
71
import java.awt.Color;
72
import java.awt.Component;
73
import java.awt.Dimension;
74
import java.awt.FlowLayout;
75
import java.io.File;
76
import java.io.FileFilter;
77
import java.io.FileWriter;
78
import java.util.prefs.Preferences;
79

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

    
94
import org.exolab.castor.xml.Marshaller;
95
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
96

    
97
import com.iver.andami.PluginServices;
98
import com.iver.andami.messages.NotificationManager;
99
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
100
import com.iver.utiles.XMLEntity;
101

    
102
/**
103
 *
104
 * @author jaume dominguez faus - jaume.dominguez@iver.es
105
 *
106
 */
107
public class StyleSelector extends SymbolSelector {
108

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

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

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

    
143
                    private FileFilter ff = new FileFilter() {
144
                            public boolean accept(File pathname) {
145
                                    return pathname.isDirectory();
146
                            }
147
                    };
148

    
149
                    public Object getRoot() {
150
                            return root;
151
                    }
152

    
153
                    public int getChildCount(Object parent) {
154
                            return ((File) parent).listFiles(ff).length;
155
                    }
156

    
157
                    public boolean isLeaf(Object node) {
158
                            return getChildCount(node)==0;
159
                    }
160

    
161
                    public void addTreeModelListener(TreeModelListener l) {
162
                            // TODO Necessite?
163

    
164
                    }
165

    
166
                    public void removeTreeModelListener(TreeModelListener l) {
167
                            // TODO Necessite?
168
                    }
169

    
170
                    public Object getChild(Object parent, int index) {
171
                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
172
                    }
173

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

    
185
                    public void valueForPathChanged(TreePath path, Object newValue) {
186
                            // TODO Auto-generated method stub
187
                    }
188

    
189
            };
190
                getTreeFav().setModel(treeModel);
191
        }
192

    
193
        protected SymbolSelectorListModel newListModel() {
194
                StyleSelectorListModel listModel = new StyleSelectorListModel(
195
                                dir,
196
                                selectedElement,
197
                                sFilter,
198
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
199
                return listModel;
200

    
201
        }
202

    
203
        protected JPanel getJPanelOptions() {
204
                if (jPanelOptions == null) {
205
                        jPanelOptions = new GridBagLayoutPanel();
206
                        // nothing in the panel
207

    
208
                }
209
            return jPanelOptions;
210
    }
211

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

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

    
252
                                    return pnl;
253
                            }
254

    
255
                };
256
                jListSymbols.setCellRenderer(renderer);
257
            }
258
            return jListSymbols;
259
    }
260

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

    
267
        protected void propertiesPressed() {
268
                StyleEditor se = new StyleEditor((IStyle) selectedElement);
269
                PluginServices.getMDIManager().addWindow(se);
270
                setStyle(se.getStyle());
271
        }
272

    
273
        protected void savePressed() {
274
                if (getSelectedObject() ==null)
275
                        return;
276

    
277

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

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

    
300
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
301

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

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

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

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