Statistics
| Revision:

root / branches / piloto3d / libraries / libCorePlugin / src / com / iver / core / preferences / general / FolderingPage.java @ 9535

History | View | Annotate | Download (9.35 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 9535 2007-01-04 09:55:25Z jcampos $
45
* $Log$
46
* Revision 1.7.2.2  2007-01-04 09:52:40  jcampos
47
* Upgrade new version
48
*
49
* Revision 1.1.2.6  2006/11/15 00:08:23  jjdelcerro
50
* *** empty log message ***
51
*
52
* Revision 1.7  2006/10/02 07:12:12  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.6  2006/09/13 16:21:00  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.5  2006/09/12 12:09:51  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.4  2006/09/12 11:49:04  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.3  2006/09/12 10:11:25  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.1.2.1  2006/09/08 11:56:24  jaume
68
* *** empty log message ***
69
*
70
*
71
*/
72
package com.iver.core.preferences.general;
73

    
74
import java.awt.event.ActionEvent;
75
import java.awt.event.ActionListener;
76
import java.io.File;
77
import java.util.prefs.Preferences;
78

    
79
import javax.swing.ImageIcon;
80
import javax.swing.JFileChooser;
81
import javax.swing.JLabel;
82
import javax.swing.JPanel;
83
import javax.swing.JTextField;
84
import javax.swing.filechooser.FileFilter;
85

    
86
import org.gvsig.gui.beans.swing.JButton;
87

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

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

    
115
        public FolderingPage() {
116
                super();
117
                setParentID(GeneralPage.id);
118
                icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/folder.png"));
119

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

    
131
                                // The file filter for the JFileChooser
132
                                FileFilter def =  new FileFilter(){
133
                                        public boolean accept(File f) {
134
                                                return (f.isDirectory());
135
                                        }
136

    
137
                                        public String getDescription() {
138
                                                return null;
139
                                        }
140
                                };
141

    
142
                                File file = new File(path);
143
                                JFileChooser fc;
144
                                if (file.exists()) {
145
                                        fc = new JFileChooser(file);
146
                                } else {
147
                                        fc = new JFileChooser();
148
                                }
149

    
150
                                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
151
                fc.setMultiSelectionEnabled(false);
152
                fc.setAcceptAllFileFilterUsed(false);
153
                fc.addChoosableFileFilter(def);
154
                int result = fc.showOpenDialog(FolderingPage.this);
155

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

    
167
                };
168
                btnSelectProjectsFolder = new JButton(PluginServices.getText(this, "browse"));
169
                btnSelectProjectsFolder.addActionListener(btnFileChooserAction);
170
                btnSelectDataFolder = new JButton(PluginServices.getText(this, "browse"));
171
                btnSelectDataFolder.addActionListener(btnFileChooserAction);
172
                btnSelectTemplatesFolder = new JButton(PluginServices.getText(this, "browse"));
173
                btnSelectTemplatesFolder.addActionListener(btnFileChooserAction);
174

    
175

    
176
                JLabel lblProjectsFolder = new JLabel("<html><b>" +
177
                                PluginServices.
178
                                getText(this, "options.foldering.projects_folder") +
179
                "</b></html>");
180
                addComponent(lblProjectsFolder);
181
                addComponent(txtProjectsFolder = new JTextField(30), btnSelectProjectsFolder);
182
                addComponent(new JLabel(" "));
183

    
184
                JLabel lblDataFolder = new JLabel("<html><b>" +
185
                                PluginServices.
186
                                getText(this, "options.foldering.data_folder") +
187
                "</b></html>");
188
                addComponent(lblDataFolder);
189
                addComponent(txtDataFolder = new JTextField(30), btnSelectDataFolder);
190
                addComponent(new JLabel(" "));
191

    
192
                JLabel lblTemplatesFolder = new JLabel("<html><b>" +
193
                                PluginServices.
194
                                getText(this, "options.foldering.template_folder") +
195
                "</b></html>");
196
                addComponent(lblTemplatesFolder);
197
                addComponent(txtTemplatesFolder = new JTextField(30), btnSelectTemplatesFolder);
198
                addComponent(new JLabel(" "));
199

    
200
                PluginServices ps = PluginServices.getPluginServices(this);
201
                XMLEntity xml = ps.getPersistentXML();
202

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

    
207
                if (xml.contains(DATA_FOLDER_PROPERTY_NAME)) {
208
                        prefs.put(DATA_FOLDER_PROPERTY_NAME, xml.getStringProperty(DATA_FOLDER_PROPERTY_NAME));
209
                }
210

    
211
                if (xml.contains(TEMPLATES_FOLDER_PROPERTY_NAME)) {
212
                        prefs.put(TEMPLATES_FOLDER_PROPERTY_NAME, xml.getStringProperty(TEMPLATES_FOLDER_PROPERTY_NAME));
213
                }
214

    
215
        }
216

    
217
        public void storeValues() throws StoreException {
218
                File f;
219
                String path, propertyName;
220
                PluginServices ps = PluginServices.getPluginServices(this);
221
                XMLEntity xml = ps.getPersistentXML();
222

    
223
                // Projects folder
224
                propertyName = PROJECTS_FOLDER_PROPERTY_NAME;
225
                path = txtProjectsFolder.getText();
226

    
227
                if (path.equals("")) {
228
                        if (xml.contains(propertyName)) {
229
                                xml.remove(propertyName);
230
                        }
231
                        prefs.remove(propertyName);
232
                } else {
233
                        f = new File(path);
234
                        if (f.exists()) {
235
                                if (xml.contains(propertyName)) {
236
                                        xml.remove(propertyName);
237
                                }
238
                                xml.putProperty(propertyName, f.getAbsolutePath());
239
                                prefs.put(propertyName, f.getAbsolutePath());
240
                        }
241
                }
242

    
243
                // Data folder
244
                propertyName = DATA_FOLDER_PROPERTY_NAME;
245
                path = txtDataFolder.getText();
246

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

    
261
                        }
262
                }
263

    
264
                // Templates folder
265
                propertyName = TEMPLATES_FOLDER_PROPERTY_NAME;
266
                path = txtTemplatesFolder.getText();
267

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

    
282
                        }
283
                }
284

    
285
        }
286

    
287
        public void setChangesApplied() {
288
                setChanged(false);
289
        }
290

    
291
        public String getID() {
292
                return this.getClass().getName();
293
        }
294

    
295
        public String getTitle() {
296
                return PluginServices.getText(this, "options.foldering.title");
297
        }
298

    
299
        public JPanel getPanel() {
300
                return this;
301
        }
302

    
303
        public void initializeValues() {
304
                PluginServices ps = PluginServices.getPluginServices(this);
305
                XMLEntity xml = ps.getPersistentXML();
306
                if (xml.contains(PROJECTS_FOLDER_PROPERTY_NAME)) {
307
                        txtProjectsFolder.setText(xml.getStringProperty(PROJECTS_FOLDER_PROPERTY_NAME));
308
                }
309
                if (xml.contains(DATA_FOLDER_PROPERTY_NAME)) {
310
                        txtDataFolder.setText(xml.getStringProperty(DATA_FOLDER_PROPERTY_NAME));
311
                }
312
                if (xml.contains(TEMPLATES_FOLDER_PROPERTY_NAME)) {
313
                        txtTemplatesFolder.setText(xml.getStringProperty(TEMPLATES_FOLDER_PROPERTY_NAME));
314
                }
315

    
316
        }
317

    
318
        public void initializeDefaults() {
319
                txtDataFolder.setText("");
320
                txtTemplatesFolder.setText("");
321
        }
322

    
323
        public ImageIcon getIcon() {
324
                return icon;
325
        }
326

    
327
        public boolean isValueChanged() {
328
                return super.hasChanged();
329
        }
330

    
331
}