Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / preferences / general / FolderingPage.java @ 7251

History | View | Annotate | Download (9.13 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: FolderingPage.java 7251 2006-09-13 16:24:14Z jaume $
45
* $Log$
46
* Revision 1.6  2006-09-13 16:21:00  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.5  2006/09/12 12:09:51  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.4  2006/09/12 11:49:04  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.3  2006/09/12 10:11:25  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.1.2.1  2006/09/08 11:56:24  jaume
59
* *** empty log message ***
60
*
61
*
62
*/
63
package com.iver.core.preferences.general;
64

    
65
import java.awt.event.ActionEvent;
66
import java.awt.event.ActionListener;
67
import java.io.File;
68
import java.util.prefs.Preferences;
69

    
70
import javax.swing.ImageIcon;
71
import javax.swing.JFileChooser;
72
import javax.swing.JLabel;
73
import javax.swing.JPanel;
74
import javax.swing.JTextField;
75
import javax.swing.filechooser.FileFilter;
76

    
77
import org.gvsig.gui.beans.swing.JButton;
78

    
79
import com.iver.andami.PluginServices;
80
import com.iver.andami.preferences.AbstractPreferencePage;
81
import com.iver.andami.preferences.StoreException;
82
import com.iver.utiles.XMLEntity;
83
/**
84
 *
85
 * In the FolderingPage the user sets which folder paths should be used by
86
 * default.
87
 *
88
 * @author jaume dominguez faus - jaume.dominguez@iver.es
89
 *
90
 */
91
public class FolderingPage extends AbstractPreferencePage{
92
        private static Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
93

    
94
        private static final String PROJECTS_FOLDER_PROPERTY_NAME = "ProjectsFolder";
95
        private static final String DATA_FOLDER_PROPERTY_NAME = "DataFolder";
96
        private static final String TEMPLATES_FOLDER_PROPERTY_NAME = "TemplatesFolder";
97
        private JTextField txtProjectsFolder;
98
        private JTextField txtDataFolder;
99
        private JTextField txtTemplatesFolder;
100
        private JButton btnSelectProjectsFolder;
101
        private JButton btnSelectDataFolder;
102
        private JButton btnSelectTemplatesFolder;
103
        private ImageIcon icon;
104
        private ActionListener btnFileChooserAction;
105

    
106
        public FolderingPage() {
107
                super();
108
                setParentID(GeneralPage.id);
109
                icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/folder.png"));
110

    
111
                btnFileChooserAction = new ActionListener() {
112
                        public void actionPerformed(ActionEvent e) {
113
                                String path;
114
                                if (e.getSource().equals(btnSelectProjectsFolder)) {
115
                                        path = txtProjectsFolder.getText();
116
                                } else if (e.getSource().equals(btnSelectDataFolder)) {
117
                                        path = txtDataFolder.getText();
118
                                } else {
119
                                        path = txtTemplatesFolder.getText();
120
                                }
121

    
122
                                // The file filter for the JFileChooser
123
                                FileFilter def =  new FileFilter(){
124
                                        public boolean accept(File f) {
125
                                                return (f.isDirectory());
126
                                        }
127

    
128
                                        public String getDescription() {
129
                                                return null;
130
                                        }
131
                                };
132

    
133
                                File file = new File(path);
134
                                JFileChooser fc;
135
                                if (file.exists()) {
136
                                        fc = new JFileChooser(file);
137
                                } else {
138
                                        fc = new JFileChooser();
139
                                }
140

    
141
                                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
142
                fc.setMultiSelectionEnabled(false);
143
                fc.setAcceptAllFileFilterUsed(false);
144
                fc.addChoosableFileFilter(def);
145
                int result = fc.showOpenDialog(FolderingPage.this);
146

    
147
                if (result == JFileChooser.APPROVE_OPTION && (file = fc.getSelectedFile()) != null) {
148
                        if (e.getSource().equals(btnSelectProjectsFolder)) {
149
                                            txtProjectsFolder.setText(file.getAbsolutePath());
150
                        } else if (e.getSource().equals(btnSelectDataFolder)) {
151
                                txtDataFolder.setText(file.getAbsolutePath());
152
                                } else {
153
                                            txtTemplatesFolder.setText(file.getAbsolutePath());
154
                                    }
155
                }
156
                        }
157

    
158
                };
159
                btnSelectProjectsFolder = new JButton(PluginServices.getText(this, "browse"));
160
                btnSelectProjectsFolder.addActionListener(btnFileChooserAction);
161
                btnSelectDataFolder = new JButton(PluginServices.getText(this, "browse"));
162
                btnSelectDataFolder.addActionListener(btnFileChooserAction);
163
                btnSelectTemplatesFolder = new JButton(PluginServices.getText(this, "browse"));
164
                btnSelectTemplatesFolder.addActionListener(btnFileChooserAction);
165

    
166

    
167
                JLabel lblProjectsFolder = new JLabel("<html><b>" +
168
                                PluginServices.
169
                                getText(this, "options.foldering.projects_folder") +
170
                "</b></html>");
171
                addComponent(lblProjectsFolder);
172
                addComponent(txtProjectsFolder = new JTextField(40), btnSelectProjectsFolder);
173
                addComponent(new JLabel(" "));
174

    
175
                JLabel lblDataFolder = new JLabel("<html><b>" +
176
                                PluginServices.
177
                                getText(this, "options.foldering.data_folder") +
178
                "</b></html>");
179
                addComponent(lblDataFolder);
180
                addComponent(txtDataFolder = new JTextField(40), btnSelectDataFolder);
181
                addComponent(new JLabel(" "));
182

    
183
                JLabel lblTemplatesFolder = new JLabel("<html><b>" +
184
                                PluginServices.
185
                                getText(this, "options.foldering.template_folder") +
186
                "</b></html>");
187
                addComponent(lblTemplatesFolder);
188
                addComponent(txtTemplatesFolder = new JTextField(40), btnSelectTemplatesFolder);
189
                addComponent(new JLabel(" "));
190

    
191
                PluginServices ps = PluginServices.getPluginServices(this);
192
                XMLEntity xml = ps.getPersistentXML();
193

    
194
                if (xml.contains(PROJECTS_FOLDER_PROPERTY_NAME)) {
195
                        prefs.put(PROJECTS_FOLDER_PROPERTY_NAME, xml.getStringProperty(PROJECTS_FOLDER_PROPERTY_NAME));
196
                }
197

    
198
                if (xml.contains(DATA_FOLDER_PROPERTY_NAME)) {
199
                        prefs.put(DATA_FOLDER_PROPERTY_NAME, xml.getStringProperty(DATA_FOLDER_PROPERTY_NAME));
200
                }
201

    
202
                if (xml.contains(TEMPLATES_FOLDER_PROPERTY_NAME)) {
203
                        prefs.put(TEMPLATES_FOLDER_PROPERTY_NAME, xml.getStringProperty(TEMPLATES_FOLDER_PROPERTY_NAME));
204
                }
205

    
206
        }
207

    
208
        public void storeValues() throws StoreException {
209
                File f;
210
                String path, propertyName;
211
                PluginServices ps = PluginServices.getPluginServices(this);
212
                XMLEntity xml = ps.getPersistentXML();
213

    
214
                // Projects folder
215
                propertyName = PROJECTS_FOLDER_PROPERTY_NAME;
216
                path = txtProjectsFolder.getText();
217

    
218
                if (path.equals("")) {
219
                        if (xml.contains(propertyName)) {
220
                                xml.remove(propertyName);
221
                        }
222
                        prefs.remove(propertyName);
223
                } else {
224
                        f = new File(path);
225
                        if (f.exists()) {
226
                                if (xml.contains(propertyName)) {
227
                                        xml.remove(propertyName);
228
                                }
229
                                xml.putProperty(propertyName, f.getAbsolutePath());
230
                                prefs.put(propertyName, f.getAbsolutePath());
231
                        }
232
                }
233

    
234
                // Data folder
235
                propertyName = DATA_FOLDER_PROPERTY_NAME;
236
                path = txtDataFolder.getText();
237

    
238
                if (path.equals("")) {
239
                        if (xml.contains(propertyName)) {
240
                                xml.remove(propertyName);
241
                        }
242
                        prefs.remove(propertyName);
243
                } else {
244
                        f = new File(path);
245
                        if (f.exists()) {
246
                                if (xml.contains(propertyName)) {
247
                                        xml.remove(propertyName);
248
                                }
249
                                xml.putProperty(propertyName, f.getAbsolutePath());
250
                                prefs.put(propertyName, f.getAbsolutePath());
251

    
252
                        }
253
                }
254

    
255
                // Templates folder
256
                propertyName = TEMPLATES_FOLDER_PROPERTY_NAME;
257
                path = txtTemplatesFolder.getText();
258

    
259
                if (path.equals("")) {
260
                        if (xml.contains(propertyName)) {
261
                                xml.remove(propertyName);
262
                        }
263
                        prefs.remove(propertyName);
264
                } else {
265
                        f = new File(path);
266
                        if (f.exists()) {
267
                                if (xml.contains(propertyName)) {
268
                                        xml.remove(propertyName);
269
                                }
270
                                xml.putProperty(propertyName, f.getAbsolutePath());
271
                                prefs.put(propertyName, f.getAbsolutePath());
272

    
273
                        }
274
                }
275

    
276
        }
277

    
278
        public void setChangesApplied() {
279
                setChanged(false);
280
        }
281

    
282
        public String getID() {
283
                return this.getClass().getName();
284
        }
285

    
286
        public String getTitle() {
287
                return PluginServices.getText(this, "options.foldering.title");
288
        }
289

    
290
        public JPanel getPanel() {
291
                return this;
292
        }
293

    
294
        public void initializeValues() {
295
                PluginServices ps = PluginServices.getPluginServices(this);
296
                XMLEntity xml = ps.getPersistentXML();
297
                if (xml.contains(PROJECTS_FOLDER_PROPERTY_NAME)) {
298
                        txtProjectsFolder.setText(xml.getStringProperty(PROJECTS_FOLDER_PROPERTY_NAME));
299
                }
300
                if (xml.contains(DATA_FOLDER_PROPERTY_NAME)) {
301
                        txtDataFolder.setText(xml.getStringProperty(DATA_FOLDER_PROPERTY_NAME));
302
                }
303
                if (xml.contains(TEMPLATES_FOLDER_PROPERTY_NAME)) {
304
                        txtTemplatesFolder.setText(xml.getStringProperty(TEMPLATES_FOLDER_PROPERTY_NAME));
305
                }
306

    
307
        }
308

    
309
        public void initializeDefaults() {
310
                txtDataFolder.setText("");
311
                txtTemplatesFolder.setText("");
312
        }
313

    
314
        public ImageIcon getIcon() {
315
                return icon;
316
        }
317

    
318
        public boolean isValueChanged() {
319
                return super.hasChanged();
320
        }
321

    
322
}