Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / gui / styling / editortools / LabelStyleOpenBackgroundFile.java @ 40911

History | View | Annotate | Download (4.74 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
package org.gvsig.labeling.gui.styling.editortools;
42

    
43
import java.awt.Cursor;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
46
import java.awt.event.MouseEvent;
47
import java.io.File;
48
import java.io.IOException;
49

    
50
import javax.swing.AbstractButton;
51
import javax.swing.JButton;
52
import javax.swing.JComponent;
53
import javax.swing.filechooser.FileFilter;
54

    
55
import org.gvsig.andami.IconThemeHelper;
56
import org.gvsig.app.gui.styling.EditorTool;
57
import org.gvsig.app.gui.styling.StyleEditor;
58
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IBackgroundFileStyle;
59
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
60
import org.gvsig.gui.beans.swing.JFileChooser;
61
import org.gvsig.i18n.Messages;
62
import org.gvsig.symbology.SymbologyLocator;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65
/**
66
 *
67
 * LabelStyleOpenBackgroundFile.java
68
 *
69
 *
70
 * @author jaume dominguez faus - jaume.dominguez@iver.es Jan 15, 2008
71
 *
72
 */
73
public class LabelStyleOpenBackgroundFile extends EditorTool {
74

    
75
        private static Logger logger =
76
                        LoggerFactory.getLogger(LabelStyleOpenBackgroundFile.class);
77
        public static final String LABEL_STYLE_BACKGROUND_FILECHOOSER = "LABEL_STYLE_BACKGROUND_FILECHOOSER";
78
        public static String DEFAULT_LABEL_BACKGROUND_FOLDER;
79

    
80
        private JButton btnOpenFile;
81
        private ILabelStyle style;
82

    
83
        public LabelStyleOpenBackgroundFile(JComponent targetEditor) {
84
                super(targetEditor);
85
        }
86

    
87

    
88
        @Override
89
        public AbstractButton getButton() {
90
                return getBtnOpenFile();
91
        }
92

    
93
        private JButton getBtnOpenFile() {
94
                if (btnOpenFile== null) {
95
                        
96
                        btnOpenFile = new JButton(
97
                                        IconThemeHelper.getImageIcon("edit-add-background-image"));
98
                        btnOpenFile.setToolTipText(Messages.getText(
99
                                        "_Set_new_background_image"));
100
                        btnOpenFile.setSize(EditorTool.SMALL_BTN_SIZE);
101
                        btnOpenFile.addActionListener(new ActionListener() {
102
                                public void actionPerformed(ActionEvent e) {
103
                                        JFileChooser jfChooser = new JFileChooser(
104
                                                        LABEL_STYLE_BACKGROUND_FILECHOOSER,
105
                                                        DEFAULT_LABEL_BACKGROUND_FOLDER);
106
                                        FileFilter ff = new FileFilter() {
107

    
108
                                                public boolean accept(File pathname) {
109
                                                        if (pathname.isDirectory()) return true;
110
                                                        String fileName = pathname.getAbsolutePath().toLowerCase();
111
                                                        return fileName.endsWith("jpg") ||
112
                                                                        fileName.endsWith("png") ||
113
                                                                        fileName.endsWith("gif") ||
114
                                                                        fileName.endsWith("svg");
115
                                                }
116

    
117
                                                public String getDescription() {
118
                                                        return Messages.getText(
119
                                                                        "all_supported_background_image_formats")+ " (*.svg, *.jpg, *.png, *.gif)";
120
                                                }
121
                                        };
122

    
123
                                        jfChooser.setFileFilter(ff);
124
                                        if (jfChooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
125
                                                File f = jfChooser.getSelectedFile();
126

    
127
                                                
128
                                                try {
129
                                                        IBackgroundFileStyle bgStyle =
130
                                                                        SymbologyLocator.getSymbologyManager()
131
                                                                        .createBackgroundFileStyle(f.toURI().toURL());
132
                                                        style.setBackgroundFileStyle(bgStyle);
133
                                                } catch (IOException e1) {
134
                                                        logger.error("While getting background style", e1);
135
                                                }
136
                                        }
137

    
138
                                        ((StyleEditor) owner).restorePreviousTool();
139
                                        owner.repaint();
140
                                }
141
                        });
142
                }
143
                return btnOpenFile;
144
        }
145

    
146
        @Override
147
        public Cursor getCursor() {
148
                return Cursor.getDefaultCursor();
149
        }
150

    
151
        @Override
152
        public String getID() {
153
                return "4";
154
        }
155

    
156
        @Override
157
        public boolean isSuitableFor(Object obj) {
158
                return obj instanceof ILabelStyle;
159
        }
160

    
161
        @Override
162
        public void setModel(Object objectToBeEdited) {
163
                style = (ILabelStyle) objectToBeEdited;
164
        }
165

    
166
        public void mousePressed(MouseEvent e) { }
167
        public void mouseReleased(MouseEvent e) { }
168
        public void mouseDragged(MouseEvent e) { }
169

    
170
}