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 @ 41185

History | View | Annotate | Download (14.8 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.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
99

    
100
import org.gvsig.andami.PluginServices;
101
import org.gvsig.andami.messages.NotificationManager;
102
import org.gvsig.app.gui.JComboBoxUnits;
103
import org.gvsig.app.gui.panels.ImageSizePanel;
104
import org.gvsig.fmap.mapcontext.MapContextLocator;
105
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
106
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
107
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
108
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
109
import org.gvsig.gui.beans.AcceptCancelPanel;
110
import org.gvsig.gui.beans.controls.dnd.JDnDList;
111
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
112
import org.gvsig.i18n.Messages;
113
import org.gvsig.utils.XMLEntity;
114

    
115

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

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

    
154
                rootDir = new File(
155
                                MapContextLocator.getSymbolManager().getSymbolPreferences()
156
                                .getSymbolLibraryPath());
157
                
158
            // Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
159
                // rootDir = new File(prefs.get("SymbolStylesFolder",
160
                // System.getProperty("user.home")+"/gvSIG/Styles"));
161
                
162
                if (!rootDir.exists())
163
                        rootDir.mkdir();
164

    
165
                initialize(style);
166
                
167
                lblTitle.setText(Messages.getText("label_styles"));
168
                treeRootName = Messages.getText("style_library");
169

    
170
        }
171

    
172
        @Override
173
        protected void initialize(Object currentElement) throws IllegalArgumentException {
174
            library = new StyleLibrary(rootDir);
175

    
176
            this.setLayout(new BorderLayout());
177
            this.setSize(400, 221);
178

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

    
194
            okCancelPanel = new AcceptCancelPanel();
195
            okCancelPanel.setOkButtonActionListener(okAction);
196
            okCancelPanel.setCancelButtonActionListener(cancelAction);
197

    
198
            this.add(okCancelPanel, BorderLayout.SOUTH);
199
            libraryBrowser.setSelectionRow(0);
200

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

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

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

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

    
226
                return mySelectedElement;
227
        }
228

    
229
        protected ListModel newListModel() {
230
                StyleSelectorListModel listModel = new StyleSelectorListModel(
231
                                dir,
232
                                sFilter,
233
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
234
                return listModel;
235

    
236
        }
237

    
238
        protected JPanel getJPanelOptions() {
239
                if (jPanelOptions == null) {
240
                        jPanelOptions = new GridBagLayoutPanel();
241
                        jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, Messages.getText("options")));
242
                        jPanelOptions.addComponent(getImageSizePanel());
243
                        jPanelOptions.addComponent(Messages.getText("units"),
244
                                        cmbUnits = new JComboBoxUnits(true));
245
                        jPanelOptions.addComponent("",
246
                                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem());
247
                }
248
            return jPanelOptions;
249
    }
250

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

    
271
                return isp;
272
        }
273

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

    
283

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

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

    
324
                                    return pnl;
325
                            }
326

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

    
345
        protected void propertiesPressed() {
346
                StyleEditor se = new StyleEditor((IStyle) getSelectedObject());
347
                PluginServices.getMDIManager().addWindow(se);
348
                setStyle(se.getStyle());
349
        }
350

    
351
        protected void savePressed() {
352
                if (getSelectedObject() ==null)
353
                        return;
354

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

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

    
377
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
378

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

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

    
421
                        } catch (Exception ex) {
422
                                NotificationManager.addError(
423
                                                Messages.getText("save_error"), ex);
424
                        }
425
                        getJListSymbols().setModel(newListModel());
426
                }
427
        }
428
        
429
        protected void newPressed() {
430
        IStyle curr_sty = ((StylePreviewer) jPanelPreview).getStyle();
431
        
432
        try {
433
            curr_sty = (IStyle) curr_sty.clone();
434
        } catch (Exception cnse) {
435
            logger.info("Unable to clone style. "
436
                + "This can cause an empty style if user cancels dialog.",
437
                cnse);
438
        }
439

    
440
         StyleEditor se = new StyleEditor(curr_sty);
441
        PluginServices.getMDIManager().addWindow(se);
442
        this.setStyle(se.getStyle());
443
    }
444
        
445
    /**
446
     * This method initializes jPanelPreview
447
     *
448
     * @return javax.swing.JComponent
449
     */
450
    protected JComponent getJPanelPreview() {
451
            if (jPanelPreview == null) {
452
                    jPanelPreview = new StylePreviewer();
453
                    jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
454
                    jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
455
                    ((StylePreviewer) jPanelPreview).setShowOutline(true);
456
            }
457
            return jPanelPreview;
458
    }
459

    
460
    @Override
461
    public void actionPerformed(ActionEvent e) {
462
            if (!act) return;
463
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
464
                performActionOn(selectedElement, e);
465
                StyleSelector.this.repaint();
466
    }
467

    
468
    public int getUnit() {
469
            return cmbUnits.getSelectedUnitIndex();
470
    }
471

    
472
    public int getReferenceSystem() {
473
            return cmbReferenceSystem.getSelectedIndex();
474
    }
475

    
476
        public void setUnit(int unit) {
477
                cmbUnits.setSelectedUnitIndex(unit);
478
        }
479

    
480
        public void setReferenceSystem(int referenceSystem) {
481
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
482
        }
483
}