Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / StyleSelector.java @ 34294

History | View | Annotate | Download (14.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 30289 2009-07-31 11:10:41Z jpiera $
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 org.gvsig.app.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.JOptionPane;
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

    
113
import org.exolab.castor.xml.Marshaller;
114
import org.gvsig.andami.PluginServices;
115
import org.gvsig.andami.messages.NotificationManager;
116
import org.gvsig.app.gui.JComboBoxUnits;
117
import org.gvsig.app.gui.panels.ImageSizePanel;
118
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
119
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
120
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
121
import org.gvsig.gui.beans.AcceptCancelPanel;
122
import org.gvsig.gui.beans.controls.dnd.JDnDList;
123
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
124
import org.gvsig.i18n.Messages;
125
import org.gvsig.utils.XMLEntity;
126

    
127

    
128
/**
129
 * Creates a panel where the user can select a style for an object that allows
130
 * to manage this property.This panel will be similar to the symbol selector panel
131
 * and, on it, the user will have a previsualization of the style of objects
132
 * stored and posibilities to modify an existing one, to create a new one
133
 * and so on.
134
 *
135
 * @author jaume dominguez faus - jaume.dominguez@iver.es
136
 *
137
 */
138
public class StyleSelector extends SymbolSelector {
139
        private static final long serialVersionUID = -7476555713446755512L;
140
        private ImageSizePanel isp;
141

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

    
159
            Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
160
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
161
                if (!rootDir.exists())
162
                        rootDir.mkdir();
163

    
164
                try {
165
                        initialize(style);
166
                } catch (ClassNotFoundException e) {
167
                        throw new Error(e);
168
                }
169
                lblTitle.setText(Messages.getText("label_styles"));
170
                treeRootName = Messages.getText("style_library");
171

    
172
        }
173

    
174
        @Override
175
        protected void initialize(Object currentElement) throws ClassNotFoundException {
176
            library = new StyleLibrary(rootDir);
177

    
178
            this.setLayout(new BorderLayout());
179
            this.setSize(400, 221);
180

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

    
196
            okCancelPanel = new AcceptCancelPanel();
197
            okCancelPanel.setOkButtonActionListener(okAction);
198
            okCancelPanel.setCancelButtonActionListener(cancelAction);
199

    
200
            this.add(okCancelPanel, BorderLayout.SOUTH);
201
            libraryBrowser.setSelectionRow(0);
202

    
203
            SillyDragNDropAction dndAction = new SillyDragNDropAction();
204
            libraryBrowser.addMouseListener(dndAction);
205
            libraryBrowser.addMouseMotionListener(dndAction);
206
            getJListSymbols().addMouseListener(dndAction);
207
            getJListSymbols().addMouseMotionListener(dndAction);
208
            setSymbol(currentElement);
209
    }
210

    
211
        @Override
212
        public void setSymbol(Object style) {
213
                ((StylePreviewer) jPanelPreview).setStyle((IStyle) style);
214
                updateOptionsPanel();
215
        }
216

    
217
        @Override
218
        public Object getSelectedObject() {
219
                if (!accepted) return null;
220
                Object mySelectedElement = ((StylePreviewer) jPanelPreview).getStyle();
221

    
222
                if (mySelectedElement instanceof CartographicSupport) {
223
                        CartographicSupport csSym = (CartographicSupport) mySelectedElement;
224
                        csSym.setUnit(cmbUnits.getSelectedUnitIndex());
225
                        csSym.setReferenceSystem(cmbReferenceSystem.getSelectedIndex());
226
                }
227

    
228
                return mySelectedElement;
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, Messages.getText("options")));
245
                        jPanelOptions.addComponent(getImageSizePanel());
246
                        jPanelOptions.addComponent(Messages.getText("units"),
247
                                        cmbUnits = new JComboBoxUnits(true));
248
                        jPanelOptions.addComponent("",
249
                                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
250
                }
251
            return jPanelOptions;
252
    }
253

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

    
274
                return isp;
275
        }
276

    
277
        protected void updateOptionsPanel() {
278
                IStyle s = ((StylePreviewer) jPanelPreview).getStyle();
279
            if (s instanceof ILabelStyle) {
280
                        ILabelStyle lab = (ILabelStyle) s;
281
                        Dimension sz = lab.getSize();
282
                        getImageSizePanel().setImageSize(sz);
283
                }
284
         }
285

    
286

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

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

    
327
                                    return pnl;
328
                            }
329

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

    
348
        protected void propertiesPressed() {
349
                StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
350
                PluginServices.getMDIManager().addWindow(se);
351
                setStyle(se.getStyle());
352
        }
353

    
354
        protected void savePressed() {
355
                if (getSelectedObject() ==null)
356
                        return;
357

    
358
                JFileChooser jfc = new JFileChooser(rootDir);
359
                javax.swing.filechooser.FileFilter ff = new javax.swing.filechooser.FileFilter() {
360
                        public boolean accept(File f) {
361
                                return f.getAbsolutePath().
362
                                toLowerCase().
363
                                endsWith(StyleSelectorListModel.STYLE_FILE_EXTENSION);
364
                        }
365

    
366
                        public String getDescription() {
367
                                return Messages.getText("gvSIG_style_definition_file")+ " ("+StyleSelectorListModel.STYLE_FILE_EXTENSION+")";
368
                        }
369
                };
370
                jfc.setFileFilter(ff);
371
                JPanel accessory = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
372
                accessory.add(new JLabel(Messages.getText("enter_description")));
373
                JTextField txtDesc = new JTextField(25);
374
                txtDesc.setText(((IStyle) getSelectedObject()).getDescription());
375
                accessory.add(txtDesc);
376
                jfc.setAccessory(accessory);
377
                if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
378
                        File targetFile = jfc.getSelectedFile();
379

    
380
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
381

    
382
                        // apply description
383
                        String desc;
384
                        if (txtDesc.getText()==null || txtDesc.getText().trim().equals("")) {
385
                                // default to file name
386
                                String s = targetFile.getAbsolutePath();
387
                                desc = s.substring(s.lastIndexOf(File.separator)+1).replaceAll(fExtension, "");
388
                        } else {
389
                                desc = txtDesc.getText().trim();
390
                        }
391
                        IStyle s = (IStyle) getSelectedObject();
392
                        s.setDescription(desc);
393

    
394
                        // save it
395
                        XMLEntity xml=null;
396
//XMLENTITY-UPDATED        
397
//                        try {
398
//                                
399
//                                xml = s.getXMLEntity();
400
//                        } catch (XMLException e) {
401
//                                NotificationManager.addWarning("Symbol layer", e);
402
//                        }
403
                        if (!targetFile.
404
                                        getAbsolutePath().
405
                                        toLowerCase().
406
                                        endsWith(fExtension))
407
                                targetFile = new File(targetFile.getAbsolutePath() + fExtension);
408
                        if(targetFile.exists()){
409
                                int resp = JOptionPane.showConfirmDialog(
410
                                                (Component) PluginServices.getMainFrame(),
411
                                                Messages.getText("fichero_ya_existe_seguro_desea_guardarlo"),
412
                                                Messages.getText("guardar"), JOptionPane.YES_NO_OPTION);
413
                                if (resp != JOptionPane.YES_OPTION) {
414
                                        return;
415
                                }
416
                        }
417
                        FileWriter writer;
418
                        try {
419
                                writer = new FileWriter(targetFile.getAbsolutePath());
420
                                Marshaller m = new Marshaller(writer);
421
                                m.setEncoding("ISO-8859-1");
422
                                m.marshal(xml.getXmlTag());
423

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

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

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

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

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

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

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