Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / StyleSelector.java @ 40688

History | View | Annotate | Download (14 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: StyleSelector.java 30289 2009-07-31 11:10:41Z jpiera $
27
* $Log$
28
* Revision 1.11  2007-08-21 09:32:53  jvidal
29
* javadoc
30
*
31
* Revision 1.10  2007/05/31 09:36:22  jaume
32
* *** empty log message ***
33
*
34
* Revision 1.9  2007/05/10 09:47:50  jaume
35
* *** empty log message ***
36
*
37
* Revision 1.8  2007/05/08 15:44:07  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.7  2007/04/27 12:10:17  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.6  2007/04/11 16:02:43  jaume
44
* file filter
45
*
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 org.gvsig.app.gui.styling;
70

    
71
import java.awt.BorderLayout;
72
import java.awt.Color;
73
import java.awt.Component;
74
import java.awt.Dimension;
75
import java.awt.FlowLayout;
76
import java.awt.event.ActionEvent;
77
import java.awt.event.ActionListener;
78
import java.io.File;
79
import java.io.FileWriter;
80
import java.util.prefs.Preferences;
81

    
82
import javax.swing.BorderFactory;
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.JOptionPane;
89
import javax.swing.JPanel;
90
import javax.swing.JTextField;
91
import javax.swing.ListCellRenderer;
92
import javax.swing.ListModel;
93
import javax.swing.event.ListSelectionEvent;
94
import javax.swing.event.ListSelectionListener;
95

    
96
import org.exolab.castor.xml.Marshaller;
97
import org.gvsig.andami.PluginServices;
98
import org.gvsig.andami.messages.NotificationManager;
99
import org.gvsig.app.gui.JComboBoxUnits;
100
import org.gvsig.app.gui.panels.ImageSizePanel;
101
import org.gvsig.fmap.mapcontext.MapContextLocator;
102
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
103
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
104
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
105
import org.gvsig.gui.beans.AcceptCancelPanel;
106
import org.gvsig.gui.beans.controls.dnd.JDnDList;
107
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
108
import org.gvsig.i18n.Messages;
109
import org.gvsig.utils.XMLEntity;
110

    
111

    
112
/**
113
 * Creates a panel where the user can select a style for an object that allows
114
 * to manage this property.This panel will be similar to the symbol selector panel
115
 * and, on it, the user will have a previsualization of the style of objects
116
 * stored and posibilities to modify an existing one, to create a new one
117
 * and so on.
118
 *
119
 * @author jaume dominguez faus - jaume.dominguez@iver.es
120
 *
121
 */
122
public class StyleSelector extends SymbolSelector {
123
        private static final long serialVersionUID = -7476555713446755512L;
124
        private ImageSizePanel isp;
125

    
126
        public StyleSelector(IStyle style, int shapeType) {
127
                this(style, shapeType, new SelectorFilter() {
128
                        public boolean accepts(Object obj) {
129
                                return obj instanceof IStyle;
130
                        }
131
                });
132
        }
133
        /**
134
         * Constructor method
135
         *
136
         * @param style
137
         * @param shapeType
138
         * @param filter
139
         */
140
        public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) {
141
                super(null, shapeType, filter, false);
142

    
143
                rootDir = new File(
144
                                MapContextLocator.getSymbolManager().getSymbolPreferences()
145
                                .getSymbolLibraryPath());
146
                
147
            // Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
148
                // rootDir = new File(prefs.get("SymbolStylesFolder",
149
                // System.getProperty("user.home")+"/gvSIG/Styles"));
150
                
151
                if (!rootDir.exists())
152
                        rootDir.mkdir();
153

    
154
                try {
155
                        initialize(style);
156
                } catch (ClassNotFoundException e) {
157
                        throw new Error(e);
158
                }
159
                lblTitle.setText(Messages.getText("label_styles"));
160
                treeRootName = Messages.getText("style_library");
161

    
162
        }
163

    
164
        @Override
165
        protected void initialize(Object currentElement) throws ClassNotFoundException {
166
            library = new StyleLibrary(rootDir);
167

    
168
            this.setLayout(new BorderLayout());
169
            this.setSize(400, 221);
170

    
171
            this.add(getJNorthPanel(), BorderLayout.NORTH);
172
            this.add(getJSplitPane(), BorderLayout.CENTER);
173
            this.add(getJEastPanel(), BorderLayout.EAST);
174
            ActionListener okAction = new ActionListener() {
175
                    public void actionPerformed(ActionEvent e) {
176
                            accepted = true;
177
                            PluginServices.getMDIManager().closeWindow(StyleSelector.this);
178
                    }
179
            }, cancelAction = new ActionListener() {
180
                    public void actionPerformed(ActionEvent e) {
181
                            setSymbol(null);
182
                            PluginServices.getMDIManager().closeWindow(StyleSelector.this);
183
                    }
184
            };
185

    
186
            okCancelPanel = new AcceptCancelPanel();
187
            okCancelPanel.setOkButtonActionListener(okAction);
188
            okCancelPanel.setCancelButtonActionListener(cancelAction);
189

    
190
            this.add(okCancelPanel, BorderLayout.SOUTH);
191
            libraryBrowser.setSelectionRow(0);
192

    
193
            SillyDragNDropAction dndAction = new SillyDragNDropAction();
194
            libraryBrowser.addMouseListener(dndAction);
195
            libraryBrowser.addMouseMotionListener(dndAction);
196
            getJListSymbols().addMouseListener(dndAction);
197
            getJListSymbols().addMouseMotionListener(dndAction);
198
            setSymbol(currentElement);
199
    }
200

    
201
        @Override
202
        public void setSymbol(Object style) {
203
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) style);
204
                updateOptionsPanel();
205
        }
206

    
207
        @Override
208
        public Object getSelectedObject() {
209
                if (!accepted) return null;
210
                Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
211

    
212
                if (mySelectedElement instanceof CartographicSupport) {
213
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
214
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
215
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
216
                }
217

    
218
                return mySelectedElement;
219
        }
220

    
221
        protected ListModel newListModel() {
222
                StyleSelectorListModel listModel = new StyleSelectorListModel(
223
                                dir,
224
                                sFilter,
225
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
226
                return listModel;
227

    
228
        }
229

    
230
        protected JPanel getJPanelOptions() {
231
                if (jPanelOptions == null) {
232
                        jPanelOptions = new GridBagLayoutPanel();
233
                        jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, Messages.getText("options")));
234
                        jPanelOptions.addComponent(getImageSizePanel());
235
                        jPanelOptions.addComponent(Messages.getText("units"),
236
                                        cmbUnits = new JComboBoxUnits(true));
237
                        jPanelOptions.addComponent("",
238
                                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
239
                }
240
            return jPanelOptions;
241
    }
242

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

    
263
                return isp;
264
        }
265

    
266
        protected void updateOptionsPanel() {
267
                IStyle s = ((StylePreviewer) jPanelPreview).getStyle();
268
            if (s instanceof ILabelStyle) {
269
                        ILabelStyle lab = (ILabelStyle) s;
270
                        Dimension sz = lab.getSize();
271
                        getImageSizePanel().setImageSize(sz);
272
                }
273
         }
274

    
275

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

    
303
                                    pnl.setBackground(bgColor);
304
                                    StylePreviewer sp = new StylePreviewer();
305
                                    sp.setShowOutline(false);
306
                                    sp.setAlignmentX(Component.CENTER_ALIGNMENT);
307
                                    sp.setPreferredSize(new Dimension(50, 50));
308
                                    sp.setStyle(sty);
309
                                    sp.setBackground(bgColor);
310
                                    pnl.add(sp);
311
                                    JLabel lbl = new JLabel(sty.getDescription());
312
                                    lbl.setBackground(bgColor);
313
                                    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
314
                                    pnl.add(lbl);
315

    
316
                                    return pnl;
317
                            }
318

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

    
337
        protected void propertiesPressed() {
338
                StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
339
                PluginServices.getMDIManager().addWindow(se);
340
                setStyle(se.getStyle());
341
        }
342

    
343
        protected void savePressed() {
344
                if (getSelectedObject() ==null)
345
                        return;
346

    
347
                JFileChooser jfc = new JFileChooser(rootDir);
348
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
349
                        public boolean accept(File f) {
350
                                return f.getAbsolutePath().
351
                                toLowerCase().
352
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
353
                        }
354

    
355
                        public String getDescription() {
356
                                return Messages.getText("gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
357
                        }
358
                };
359
                jfc.setFileFilter(ff);
360
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
361
                accessory.add(new JLabel(Messages.getText("enter_description")));
362
                JTextField txtDesc = new JTextField(25);
363
                txtDesc.setText(((IStyle) getSelectedObject()).getDescription());
364
                accessory.add(txtDesc);
365
                jfc.setAccessory(accessory);
366
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
367
                        File targetFile = jfc.getSelectedFile();
368

    
369
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
370

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

    
383
                        // save it
384
                        XMLEntity xml=null;
385
//XMLENTITY-UPDATED        
386
//                        try {
387
//                                
388
//                                xml = s.getXMLEntity();
389
//                        } catch (XMLException e) {
390
//                                NotificationManager.addWarning("Symbol layer", e);
391
//                        }
392
                        if (!targetFile.
393
                                        getAbsolutePath().
394
                                        toLowerCase().
395
                                        endsWith(fExtension))
396
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
397
                        if(targetFile.exists()){
398
                                int resp = JOptionPane.showConfirmDialog(
399
                                                (Component) PluginServices.getMainFrame(),
400
                                                Messages.getText("fichero_ya_existe_seguro_desea_guardarlo"),
401
                                                Messages.getText("guardar"), JOptionPane.YES_NO_OPTION);
402
                                if (resp != JOptionPane.YES_OPTION) {
403
                                        return;
404
                                }
405
                        }
406
                        FileWriter writer;
407
                        try {
408
                                writer = new FileWriter(targetFile.getAbsolutePath());
409
                                Marshaller m = new Marshaller(writer);
410
                                m.setEncoding("ISO-8859-1");
411
                                m.marshal(xml.getXmlTag());
412

    
413
                        } catch (Exception ex) {
414
                                NotificationManager.addError(
415
                                                Messages.getText("save_error"), ex);
416
                        }
417
                        getJListSymbols().setModel(newListModel());
418
                }
419
        }
420

    
421
    /**
422
     * This method initializes jPanelPreview
423
     *
424
     * @return javax.swing.JComponent
425
     */
426
    protected JComponent getJPanelPreview() {
427
            if (jPanelPreview == null) {
428
                    jPanelPreview = new StylePreviewer();
429
                    jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
430
                    jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
431
                    ((StylePreviewer) jPanelPreview).setShowOutline(true);
432
            }
433
            return jPanelPreview;
434
    }
435

    
436
    @Override
437
    public void actionPerformed(ActionEvent e) {
438
            if (!act) return;
439
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
440
                performActionOn(selectedElement, e);
441
                StyleSelector.this.repaint();
442
    }
443

    
444
    public int getUnit() {
445
            return cmbUnits.getSelectedUnitIndex();
446
    }
447

    
448
    public int getReferenceSystem() {
449
            return cmbReferenceSystem.getSelectedIndex();
450
    }
451

    
452
        public void setUnit(int unit) {
453
                cmbUnits.setSelectedUnitIndex(unit);
454
        }
455

    
456
        public void setReferenceSystem(int referenceSystem) {
457
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
458
        }
459
}