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

History | View | Annotate | Download (15.1 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.FileOutputStream;
80

    
81
import javax.swing.BorderFactory;
82
import javax.swing.BoxLayout;
83
import javax.swing.JComponent;
84
import javax.swing.JFileChooser;
85
import javax.swing.JLabel;
86
import javax.swing.JList;
87
import javax.swing.JOptionPane;
88
import javax.swing.JPanel;
89
import javax.swing.JTextField;
90
import javax.swing.ListCellRenderer;
91
import javax.swing.ListModel;
92
import javax.swing.event.ListSelectionEvent;
93
import javax.swing.event.ListSelectionListener;
94

    
95
import org.gvsig.andami.PluginServices;
96
import org.gvsig.andami.messages.NotificationManager;
97
import org.gvsig.app.gui.JComboBoxUnits;
98
import org.gvsig.app.gui.panels.ImageSizePanel;
99
import org.gvsig.fmap.mapcontext.MapContextLocator;
100
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
101
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
102
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
103
import org.gvsig.gui.beans.AcceptCancelPanel;
104
import org.gvsig.gui.beans.controls.dnd.JDnDList;
105
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
106
import org.gvsig.i18n.Messages;
107
import org.gvsig.symbology.SymbologyLocator;
108
import org.gvsig.tools.ToolsLocator;
109
import org.gvsig.tools.persistence.PersistenceManager;
110
import org.gvsig.tools.persistence.PersistentState;
111
import org.slf4j.Logger;
112
import org.slf4j.LoggerFactory;
113

    
114

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

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

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

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

    
169
        }
170

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

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

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

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

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

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

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

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

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

    
225
                return mySelectedElement;
226
        }
227

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

    
235
        }
236

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

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

    
270
                return isp;
271
        }
272

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

    
282

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

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

    
323
                                    return pnl;
324
                            }
325

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

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

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

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

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

    
376
                        String fExtension = StyleSelectorListModel.STYLE_FILE_EXTENSION;
377

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

    
390
                        // save it
391

    
392
                        if (!targetFile.
393
                                        getAbsolutePath().
394
                                        toLowerCase().
395
                                        endsWith(fExtension)) {
396
                            
397
                            targetFile = new File(targetFile.getAbsolutePath() + fExtension);
398
                        }
399

    
400
                        if(targetFile.exists()){
401
                                int resp = JOptionPane.showConfirmDialog(
402
                                                (Component) PluginServices.getMainFrame(),
403
                                                Messages.getText("fichero_ya_existe_seguro_desea_guardarlo"),
404
                                                Messages.getText("guardar"), JOptionPane.YES_NO_OPTION);
405
                                if (resp != JOptionPane.YES_OPTION) {
406
                                        return;
407
                                }
408
                        }
409
                        
410
                        // the object to persist is 's'
411
                        try {
412
                            
413
                            PersistenceManager pman = ToolsLocator.getPersistenceManager();
414
                            PersistentState psta = pman.getState(s, true);
415
                    if (psta.getContext().getErrors() != null) {
416
                        throw psta.getContext().getErrors();
417
                    }
418
                    FileOutputStream fos = new FileOutputStream(targetFile);
419
                    pman.saveState(psta, fos);
420
                    fos.close();
421

    
422
                        } catch (Exception ex) {
423
                                NotificationManager.addError(
424
                                                Messages.getText("save_error"), ex);
425
                        }
426
                        getJListSymbols().setModel(newListModel());
427
                }
428
        }
429
        
430
        protected void newPressed() {
431
        IStyle curr_sty = ((StylePreviewer) jPanelPreview).getStyle();
432
        
433
        if(curr_sty != null){
434
                try {
435
                    curr_sty = (IStyle) curr_sty.clone();
436
                } catch (Exception cnse) {
437
                    logger.info("Unable to clone style. "
438
                        + "Creating a new empty SimpleLabelStyle...",
439
                        cnse);
440
                    curr_sty = SymbologyLocator.getSymbologyManager().createDefaultLabelStyle();
441
                }
442
        }else{
443
                curr_sty = SymbologyLocator.getSymbologyManager().createDefaultLabelStyle();
444
        }
445
        
446
        StyleEditor se = new StyleEditor(curr_sty);
447
        PluginServices.getMDIManager().addWindow(se);
448
        this.setStyle(se.getStyle());
449
    }
450
        
451
    /**
452
     * This method initializes jPanelPreview
453
     *
454
     * @return javax.swing.JComponent
455
     */
456
    protected SymbolPreviewer getJPanelPreview() {
457
            if (jPanelPreview == null) {
458
                    jPanelPreview = new StylePreviewer(getSampleFeature());
459
                    jPanelPreview.setPreferredSize(new java.awt.Dimension(100,100));
460
                    jPanelPreview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
461
                    ((StylePreviewer) jPanelPreview).setShowOutline(true);
462
            }
463
            return jPanelPreview;
464
    }
465

    
466
    @Override
467
    public void actionPerformed(ActionEvent e) {
468
            if (!act) return;
469
                Object selectedElement = ((StylePreviewer) jPanelPreview).getStyle();//getSelectedObject();
470
                performActionOn(selectedElement, e);
471
                StyleSelector.this.repaint();
472
    }
473

    
474
    public int getUnit() {
475
            return cmbUnits.getSelectedUnitIndex();
476
    }
477

    
478
    public int getReferenceSystem() {
479
            return cmbReferenceSystem.getSelectedIndex();
480
    }
481

    
482
        public void setUnit(int unit) {
483
                cmbUnits.setSelectedUnitIndex(unit);
484
        }
485

    
486
        public void setReferenceSystem(int referenceSystem) {
487
                cmbReferenceSystem.setSelectedIndex(referenceSystem);
488
        }
489
}