Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / com / iver / core / preferences / general / FolderingPage.java @ 25555

History | View | Annotate | Download (10.8 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 15635 2007-10-30 11:58:33Z jmvivo $
45
* $Log$
46
* Revision 1.8  2007-01-26 13:35:06  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.7  2006/10/02 07:12:12  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.6  2006/09/13 16:21:00  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.5  2006/09/12 12:09:51  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.4  2006/09/12 11:49:04  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.3  2006/09/12 10:11:25  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.1.2.1  2006/09/08 11:56:24  jaume
65
* *** empty log message ***
66
*
67
*
68
*/
69
package com.iver.core.preferences.general;
70

    
71
import java.awt.event.ActionEvent;
72
import java.awt.event.ActionListener;
73
import java.io.File;
74
import java.util.prefs.Preferences;
75

    
76
import javax.swing.ImageIcon;
77
import javax.swing.JFileChooser;
78
import javax.swing.JLabel;
79
import javax.swing.JPanel;
80
import javax.swing.JTextField;
81
import javax.swing.filechooser.FileFilter;
82

    
83
import org.gvsig.gui.beans.swing.JButton;
84

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

    
100
        private static final String PROJECTS_FOLDER_PROPERTY_NAME = "ProjectsFolder";
101
        private static final String DATA_FOLDER_PROPERTY_NAME = "DataFolder";
102
        private static final String TEMPLATES_FOLDER_PROPERTY_NAME = "TemplatesFolder";
103
        private static final String SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME = "SymbolLibraryFolder";
104
        private JTextField txtProjectsFolder;
105
        private JTextField txtDataFolder;
106
        private JTextField txtTemplatesFolder;
107
        private JTextField txtSymbolLibraryFolder;
108
        private JButton btnSelectProjectsFolder;
109
        private JButton btnSelectDataFolder;
110
        private JButton btnSelectTemplatesFolder;
111
        private JButton btnSelectSymbolLibraryFolder;
112
        private ImageIcon icon;
113
        private ActionListener btnFileChooserAction;
114

    
115

    
116

    
117
        public FolderingPage() {
118
                super();
119
                setParentID(GeneralPage.id);
120
                icon = PluginServices.getIconTheme().get("folder-icon");
121

    
122
                btnFileChooserAction = new ActionListener() {
123
                        public void actionPerformed(ActionEvent e) {
124
                                String path;
125
                                if (e.getSource().equals(btnSelectProjectsFolder)) {
126
                                        path = txtProjectsFolder.getText();
127
                                } else if (e.getSource().equals(btnSelectDataFolder)) {
128
                                        path = txtDataFolder.getText();
129
                                } else if (e.getSource().equals(btnSelectSymbolLibraryFolder)) {
130
                                        path = txtSymbolLibraryFolder.getText();
131
                                } else {
132
                                        path = txtTemplatesFolder.getText();
133
                                }
134

    
135
                                // The file filter for the JFileChooser
136
                                FileFilter def =  new FileFilter(){
137
                                        public boolean accept(File f) {
138
                                                return (f.isDirectory());
139
                                        }
140

    
141
                                        public String getDescription() {
142
                                                return null;
143
                                        }
144
                                };
145

    
146
                                File file = new File(path);
147
                                JFileChooser fc;
148
                                if (file.exists()) {
149
                                        fc = new JFileChooser(file);
150
                                } else {
151
                                        fc = new JFileChooser();
152
                                }
153

    
154
                                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
155
                fc.setMultiSelectionEnabled(false);
156
                fc.setAcceptAllFileFilterUsed(false);
157
                fc.addChoosableFileFilter(def);
158
                int result = fc.showOpenDialog(FolderingPage.this);
159

    
160
                if (result == JFileChooser.APPROVE_OPTION && (file = fc.getSelectedFile()) != null) {
161
                        if (e.getSource().equals(btnSelectProjectsFolder)) {
162
                                            txtProjectsFolder.setText(file.getAbsolutePath());
163
                        } else if (e.getSource().equals(btnSelectDataFolder)) {
164
                                txtDataFolder.setText(file.getAbsolutePath());
165
                                } else {
166
                                            txtTemplatesFolder.setText(file.getAbsolutePath());
167
                                    }
168
                }
169
                        }
170

    
171
                };
172
                btnSelectProjectsFolder = new JButton(PluginServices.getText(this, "browse"));
173
                btnSelectProjectsFolder.addActionListener(btnFileChooserAction);
174
                btnSelectDataFolder = new JButton(PluginServices.getText(this, "browse"));
175
                btnSelectDataFolder.addActionListener(btnFileChooserAction);
176
                btnSelectTemplatesFolder = new JButton(PluginServices.getText(this, "browse"));
177
                btnSelectTemplatesFolder.addActionListener(btnFileChooserAction);
178
                btnSelectSymbolLibraryFolder = new JButton(PluginServices.getText(this, "browse"));
179
                btnSelectSymbolLibraryFolder.addActionListener(btnFileChooserAction);
180

    
181

    
182
                JLabel lblProjectsFolder = new JLabel("<html><b>" +
183
                                PluginServices.
184
                                getText(this, "options.foldering.projects_folder") +
185
                "</b></html>");
186
                addComponent(lblProjectsFolder);
187
                addComponent(txtProjectsFolder = new JTextField(30), btnSelectProjectsFolder);
188
                addComponent(new JLabel(" "));
189

    
190
                JLabel lblDataFolder = new JLabel("<html><b>" +
191
                                PluginServices.
192
                                getText(this, "options.foldering.data_folder") +
193
                "</b></html>");
194
                addComponent(lblDataFolder);
195
                addComponent(txtDataFolder = new JTextField(30), btnSelectDataFolder);
196
                addComponent(new JLabel(" "));
197

    
198
                JLabel lblTemplatesFolder = new JLabel("<html><b>" +
199
                                PluginServices.
200
                                getText(this, "options.foldering.template_folder") +
201
                "</b></html>");
202
                addComponent(lblTemplatesFolder);
203
                addComponent(txtTemplatesFolder = new JTextField(30), btnSelectTemplatesFolder);
204
                addComponent(new JLabel(" "));
205

    
206
                JLabel lblSymbolLibraryFolder = new JLabel("<html><b>" +
207
                                PluginServices.
208
                                getText(this, "options.foldering.symbol_library_folder") +
209
                "</b></html>");
210
                addComponent(lblSymbolLibraryFolder);
211
                addComponent(txtSymbolLibraryFolder = new JTextField(30), btnSelectSymbolLibraryFolder);
212
                addComponent(new JLabel(" "));
213
                PluginServices ps = PluginServices.getPluginServices(this);
214
                XMLEntity xml = ps.getPersistentXML();
215

    
216
                if (xml.contains(PROJECTS_FOLDER_PROPERTY_NAME)) {
217
                        prefs.put(PROJECTS_FOLDER_PROPERTY_NAME, xml.getStringProperty(PROJECTS_FOLDER_PROPERTY_NAME));
218
                }
219

    
220
                if (xml.contains(DATA_FOLDER_PROPERTY_NAME)) {
221
                        prefs.put(DATA_FOLDER_PROPERTY_NAME, xml.getStringProperty(DATA_FOLDER_PROPERTY_NAME));
222
                }
223

    
224
                if (xml.contains(TEMPLATES_FOLDER_PROPERTY_NAME)) {
225
                        prefs.put(TEMPLATES_FOLDER_PROPERTY_NAME, xml.getStringProperty(TEMPLATES_FOLDER_PROPERTY_NAME));
226
                }
227

    
228
                if (xml.contains(SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME)) {
229
                        prefs.put(SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME, xml.getStringProperty(SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME));
230
                }
231

    
232
        }
233

    
234
        public void storeValues() throws StoreException {
235
                File f;
236
                String path, propertyName;
237
                PluginServices ps = PluginServices.getPluginServices(this);
238
                XMLEntity xml = ps.getPersistentXML();
239

    
240
                // Projects folder
241
                propertyName = PROJECTS_FOLDER_PROPERTY_NAME;
242
                path = txtProjectsFolder.getText();
243

    
244
                if (path.equals("")) {
245
                        if (xml.contains(propertyName)) {
246
                                xml.remove(propertyName);
247
                        }
248
                        prefs.remove(propertyName);
249
                } else {
250
                        f = new File(path);
251
                        if (f.exists()) {
252
                                if (xml.contains(propertyName)) {
253
                                        xml.remove(propertyName);
254
                                }
255
                                xml.putProperty(propertyName, f.getAbsolutePath());
256
                                prefs.put(propertyName, f.getAbsolutePath());
257
                        }
258
                }
259

    
260
                // Data folder
261
                propertyName = DATA_FOLDER_PROPERTY_NAME;
262
                path = txtDataFolder.getText();
263

    
264
                if (path.equals("")) {
265
                        if (xml.contains(propertyName)) {
266
                                xml.remove(propertyName);
267
                        }
268
                        prefs.remove(propertyName);
269
                } else {
270
                        f = new File(path);
271
                        if (f.exists()) {
272
                                if (xml.contains(propertyName)) {
273
                                        xml.remove(propertyName);
274
                                }
275
                                xml.putProperty(propertyName, f.getAbsolutePath());
276
                                prefs.put(propertyName, f.getAbsolutePath());
277

    
278
                        }
279
                }
280

    
281
                // Templates folder
282
                propertyName = TEMPLATES_FOLDER_PROPERTY_NAME;
283
                path = txtTemplatesFolder.getText();
284

    
285
                if (path.equals("")) {
286
                        if (xml.contains(propertyName)) {
287
                                xml.remove(propertyName);
288
                        }
289
                        prefs.remove(propertyName);
290
                } else {
291
                        f = new File(path);
292
                        if (f.exists()) {
293
                                if (xml.contains(propertyName)) {
294
                                        xml.remove(propertyName);
295
                                }
296
                                xml.putProperty(propertyName, f.getAbsolutePath());
297
                                prefs.put(propertyName, f.getAbsolutePath());
298

    
299
                        }
300
                }
301

    
302
                // Symbol library folder
303
                propertyName = SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME;
304
                path = txtSymbolLibraryFolder.getText();
305

    
306
                if (path.equals("")) {
307
                        if (xml.contains(propertyName)) {
308
                                xml.remove(propertyName);
309
                        }
310
                        prefs.remove(propertyName);
311
                } else {
312
                        f = new File(path);
313
                        if (f.exists()) {
314
                                if (xml.contains(propertyName)) {
315
                                        xml.remove(propertyName);
316
                                }
317
                                xml.putProperty(propertyName, f.getAbsolutePath());
318
                                prefs.put(propertyName, f.getAbsolutePath());
319
                        }
320
                }
321

    
322
        }
323

    
324
        public void setChangesApplied() {
325
                setChanged(false);
326
        }
327

    
328
        public String getID() {
329
                return this.getClass().getName();
330
        }
331

    
332
        public String getTitle() {
333
                return PluginServices.getText(this, "options.foldering.title");
334
        }
335

    
336
        public JPanel getPanel() {
337
                return this;
338
        }
339

    
340
        public void initializeValues() {
341
                PluginServices ps = PluginServices.getPluginServices(this);
342
                XMLEntity xml = ps.getPersistentXML();
343
                if (xml.contains(PROJECTS_FOLDER_PROPERTY_NAME)) {
344
                        txtProjectsFolder.setText(xml.getStringProperty(PROJECTS_FOLDER_PROPERTY_NAME));
345
                }
346
                if (xml.contains(DATA_FOLDER_PROPERTY_NAME)) {
347
                        txtDataFolder.setText(xml.getStringProperty(DATA_FOLDER_PROPERTY_NAME));
348
                }
349
                if (xml.contains(TEMPLATES_FOLDER_PROPERTY_NAME)) {
350
                        txtTemplatesFolder.setText(xml.getStringProperty(TEMPLATES_FOLDER_PROPERTY_NAME));
351
                }
352
                if (xml.contains(SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME)) {
353
                        txtTemplatesFolder.setText(xml.getStringProperty(SYMBOL_LIBRARY_FOLDER_PROPERTY_NAME));
354
                }
355
        }
356

    
357
        public void initializeDefaults() {
358
                txtDataFolder.setText("");
359
                txtTemplatesFolder.setText("");
360
        }
361

    
362
        public ImageIcon getIcon() {
363
                return icon;
364
        }
365

    
366
        public boolean isValueChanged() {
367
                return super.hasChanged();
368
        }
369

    
370
}