svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / DataParametersEditor.java @ 25468
History | View | Annotate | Download (12.7 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 IVER T.I. S.A. {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing; |
32 |
|
33 |
import java.awt.BorderLayout; |
34 |
import java.awt.Component; |
35 |
import java.awt.GridBagConstraints; |
36 |
import java.awt.GridBagLayout; |
37 |
import java.awt.Insets; |
38 |
import java.awt.event.ActionEvent; |
39 |
import java.awt.event.ActionListener; |
40 |
import java.util.ArrayList; |
41 |
import java.util.Arrays; |
42 |
import java.util.HashMap; |
43 |
import java.util.Iterator; |
44 |
import java.util.List; |
45 |
import java.util.Map; |
46 |
import java.util.Map.Entry; |
47 |
|
48 |
import javax.swing.JFrame; |
49 |
import javax.swing.JLabel; |
50 |
import javax.swing.JPanel; |
51 |
import javax.swing.JTextField; |
52 |
|
53 |
import org.gvsig.fmap.dal.DALFileLibrary; |
54 |
import org.gvsig.fmap.dal.DALLibrary; |
55 |
import org.gvsig.fmap.dal.DALLocator; |
56 |
import org.gvsig.fmap.dal.DataParameters; |
57 |
import org.gvsig.fmap.dal.DataTypes; |
58 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
59 |
import org.gvsig.fmap.dal.store.dbf.DBFLibrary; |
60 |
import org.gvsig.fmap.dal.store.shp.SHPLibrary; |
61 |
import org.gvsig.gui.beans.swing.JButton; |
62 |
import org.gvsig.tools.ToolsLibrary; |
63 |
import org.gvsig.tools.dynobject.DynField; |
64 |
import org.gvsig.tools.exception.NotYetImplemented; |
65 |
|
66 |
import com.iver.andami.PluginServices; |
67 |
import com.iver.andami.ui.mdiManager.IWindow; |
68 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
69 |
|
70 |
/**
|
71 |
* @author jmvivo
|
72 |
*
|
73 |
*/
|
74 |
public class DataParametersEditor extends JPanel implements IWindow, |
75 |
ActionListener {
|
76 |
|
77 |
public static final int SHOW_ONLY_THIS_PARAMS = 0; |
78 |
public static final int HIDDE_THIS_PARAMS = 1; |
79 |
|
80 |
private static boolean debug_mode = false; |
81 |
|
82 |
private DataParameters parameters;
|
83 |
private List<DynField> fields; |
84 |
private JButton botAcept; |
85 |
private JButton botCancel; |
86 |
private JButton botRestoreDefaults; |
87 |
private JPanel panButtons; |
88 |
private JPanel panParameters; |
89 |
private Map<DynField, Component> componentField; |
90 |
|
91 |
|
92 |
public DataParametersEditor(DataParameters parameters) {
|
93 |
super();
|
94 |
this.parameters = parameters;
|
95 |
this.fields = Arrays.asList(parameters.getDynClass() |
96 |
.getDynFields());; |
97 |
this.initialize();
|
98 |
} |
99 |
|
100 |
public DataParametersEditor(DataParameters parameters, int mode, |
101 |
List<String> paramsNames) { |
102 |
super();
|
103 |
this.parameters = parameters;
|
104 |
this.fields = Arrays.asList(parameters.getDynClass().getDynFields()); |
105 |
; |
106 |
if (mode == SHOW_ONLY_THIS_PARAMS || mode == HIDDE_THIS_PARAMS) {
|
107 |
ArrayList<DynField> toShow = new ArrayList<DynField>(); |
108 |
List<DynField> fields = Arrays.asList(parameters.getDynClass() |
109 |
.getDynFields()); |
110 |
Iterator<DynField> iter = fields.iterator();
|
111 |
DynField field; |
112 |
while (iter.hasNext()) {
|
113 |
field = iter.next(); |
114 |
|
115 |
if (mode == SHOW_ONLY_THIS_PARAMS
|
116 |
&& paramsNames.contains(field.getName())) { |
117 |
toShow.add(field); |
118 |
} else if (mode == HIDDE_THIS_PARAMS |
119 |
&& !(paramsNames.contains(field.getName()))) { |
120 |
toShow.add(field); |
121 |
} |
122 |
} |
123 |
this.fields = toShow;
|
124 |
|
125 |
} else{
|
126 |
throw new IllegalArgumentException(); |
127 |
} |
128 |
this.initialize();
|
129 |
|
130 |
|
131 |
} |
132 |
|
133 |
private void initialize() { |
134 |
this.setLayout(new BorderLayout(3, 3)); |
135 |
this.add(this.getParametersPanel(),BorderLayout.CENTER); |
136 |
this.add(this.getButtonsPanel(), BorderLayout.SOUTH); |
137 |
this.fromParamsToUI();
|
138 |
} |
139 |
|
140 |
private JPanel getButtonsPanel() { |
141 |
if (this.panButtons == null) { |
142 |
this.panButtons = new JPanel(); |
143 |
// FIXME poner un gridbaglayout
|
144 |
this.panButtons.setLayout(new BorderLayout(2, 2)); |
145 |
this.panButtons.add(this.getAcceptButton(), BorderLayout.WEST); |
146 |
this.panButtons.add(this.getCancelButton(), BorderLayout.CENTER); |
147 |
this.panButtons.add(this.getRestoreDefaults(), BorderLayout.EAST); |
148 |
} |
149 |
return this.panButtons; |
150 |
} |
151 |
|
152 |
private JButton getRestoreDefaults() { |
153 |
if (this.botRestoreDefaults == null) { |
154 |
this.botRestoreDefaults = new JButton(); |
155 |
this.botRestoreDefaults.addActionListener(this); |
156 |
this.botRestoreDefaults
|
157 |
.setText(getLocalizedText("restoreDefaults"));
|
158 |
} |
159 |
return this.botRestoreDefaults; |
160 |
} |
161 |
|
162 |
private JButton getCancelButton() { |
163 |
if (this.botCancel == null) { |
164 |
this.botCancel = new JButton(); |
165 |
this.botCancel.addActionListener(this); |
166 |
this.botCancel.setText(getLocalizedText("cancel")); |
167 |
} |
168 |
return this.botCancel; |
169 |
} |
170 |
|
171 |
private String getLocalizedText(String txt) { |
172 |
try {
|
173 |
return PluginServices.getText(this, txt); |
174 |
} catch (Exception e) { |
175 |
return txt;
|
176 |
} |
177 |
} |
178 |
|
179 |
private JButton getAcceptButton() { |
180 |
if (this.botAcept == null){ |
181 |
this.botAcept = new JButton(); |
182 |
this.botAcept.addActionListener(this); |
183 |
this.botAcept.setText(getLocalizedText("accept")); |
184 |
} |
185 |
return this.botAcept; |
186 |
} |
187 |
|
188 |
private GridBagConstraints getDefaultParametersConstraints() { |
189 |
GridBagConstraints constr = new GridBagConstraints(); |
190 |
constr.insets = new Insets(2, 2, 2, 2); |
191 |
constr.ipadx = 2;
|
192 |
constr.ipady = 2;
|
193 |
constr.anchor = GridBagConstraints.PAGE_START;
|
194 |
return constr;
|
195 |
|
196 |
} |
197 |
|
198 |
private JPanel getParametersPanel() { |
199 |
if (this.panParameters == null) { |
200 |
this.panParameters = new JPanel(); |
201 |
this.panParameters.setLayout(new GridBagLayout()); |
202 |
Iterator<DynField> iter = this.fields.iterator(); |
203 |
DynField field; |
204 |
while (iter.hasNext()) {
|
205 |
field = iter.next(); |
206 |
switch (field.getType()) {
|
207 |
case DynField.SINGLE:
|
208 |
this.addFieldSingle(this.panParameters, field); |
209 |
|
210 |
break;
|
211 |
case DynField.CHOICE:
|
212 |
|
213 |
break;
|
214 |
case DynField.RANGE:
|
215 |
|
216 |
break;
|
217 |
|
218 |
default:
|
219 |
//FIXME lanzar un warning???
|
220 |
this.addFieldSingle(this.panParameters, field); |
221 |
break;
|
222 |
} |
223 |
|
224 |
} |
225 |
GridBagConstraints constr = new GridBagConstraints(); |
226 |
constr.fill = GridBagConstraints.BOTH;
|
227 |
constr.weightx = 1;
|
228 |
constr.weighty = 1;
|
229 |
constr.gridheight = GridBagConstraints.REMAINDER;
|
230 |
constr.gridwidth = GridBagConstraints.REMAINDER;
|
231 |
this.panParameters.add(new JLabel(), constr); |
232 |
} |
233 |
return this.panParameters; |
234 |
} |
235 |
|
236 |
private void addFieldSingle(JPanel panel,DynField field) { |
237 |
GridBagConstraints constr = this.getDefaultParametersConstraints(); |
238 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
239 |
constr.weighty = 0;
|
240 |
constr.weightx = 0;
|
241 |
|
242 |
JLabel label = new JLabel(); |
243 |
label.setText(this.getLocalizedText(field.getDescription()));
|
244 |
constr.gridwidth = GridBagConstraints.RELATIVE;
|
245 |
panel.add(label, constr); |
246 |
|
247 |
JTextField text = new JTextField(); |
248 |
text.setName(field.getName()); |
249 |
constr.gridwidth = GridBagConstraints.REMAINDER;
|
250 |
constr.weightx = 1;
|
251 |
panel.add(text, constr); |
252 |
this.getComponentField().put(field, text);
|
253 |
|
254 |
} |
255 |
|
256 |
private void fromParamsToUISingle(DynField field, Component component) { |
257 |
JTextField text = (JTextField) component; |
258 |
|
259 |
if (field.getTheTypeOfAvailableValues() == DataTypes.STRING) {
|
260 |
text.setText((String) parameters.getDynValue(field.getName()));
|
261 |
} else {
|
262 |
text.setText(parameters.getDynValue(field.getName()).toString()); |
263 |
} |
264 |
|
265 |
} |
266 |
|
267 |
private void fromDefaultsToUISingle(DynField field, Component component) { |
268 |
JTextField text = (JTextField) component; |
269 |
|
270 |
if (field.getTheTypeOfAvailableValues() == DataTypes.STRING) {
|
271 |
text.setText((String) field.getDefaultValue());
|
272 |
} else {
|
273 |
text.setText(field.getDefaultValue().toString()); |
274 |
} |
275 |
|
276 |
} |
277 |
|
278 |
private void fromParamsToUI() { |
279 |
Iterator<Entry<DynField, Component>> iter = this.getComponentField().entrySet().iterator(); |
280 |
Entry<DynField, Component> entry;
|
281 |
while (iter.hasNext()) {
|
282 |
entry = iter.next(); |
283 |
|
284 |
switch (entry.getKey().getType()) {
|
285 |
case DynField.SINGLE:
|
286 |
this.fromParamsToUISingle(entry.getKey(),entry.getValue());
|
287 |
break;
|
288 |
case DynField.CHOICE:
|
289 |
throw new NotYetImplemented(); |
290 |
// break;
|
291 |
case DynField.RANGE:
|
292 |
throw new NotYetImplemented(); |
293 |
// break;
|
294 |
|
295 |
default:
|
296 |
// FIXME warning??
|
297 |
this.fromParamsToUISingle(entry.getKey(),entry.getValue());
|
298 |
break;
|
299 |
} |
300 |
|
301 |
} |
302 |
} |
303 |
|
304 |
private void fromDefaultsToUI() { |
305 |
Iterator<Entry<DynField, Component>> iter = this.getComponentField() |
306 |
.entrySet().iterator(); |
307 |
Entry<DynField, Component> entry;
|
308 |
while (iter.hasNext()) {
|
309 |
entry = iter.next(); |
310 |
|
311 |
switch (entry.getKey().getType()) {
|
312 |
case DynField.SINGLE:
|
313 |
this.fromDefaultsToUISingle(entry.getKey(), entry.getValue());
|
314 |
break;
|
315 |
case DynField.CHOICE:
|
316 |
throw new NotYetImplemented(); |
317 |
// break;
|
318 |
case DynField.RANGE:
|
319 |
throw new NotYetImplemented(); |
320 |
// break;
|
321 |
|
322 |
default:
|
323 |
// FIXME warning??
|
324 |
this.fromParamsToUISingle(entry.getKey(), entry.getValue());
|
325 |
break;
|
326 |
} |
327 |
|
328 |
} |
329 |
} |
330 |
|
331 |
|
332 |
private void fromUIToParams() { |
333 |
Iterator<Entry<DynField, Component>> iter = this.getComponentField() |
334 |
.entrySet().iterator(); |
335 |
Entry<DynField, Component> entry;
|
336 |
while (iter.hasNext()) {
|
337 |
entry = iter.next(); |
338 |
|
339 |
switch (entry.getKey().getType()) {
|
340 |
case DynField.SINGLE:
|
341 |
this.fromUIToParamsSingle(entry.getKey(), entry.getValue());
|
342 |
break;
|
343 |
case DynField.CHOICE:
|
344 |
throw new NotYetImplemented(); |
345 |
// break;
|
346 |
case DynField.RANGE:
|
347 |
throw new NotYetImplemented(); |
348 |
// break;
|
349 |
|
350 |
default:
|
351 |
// FIXME warning??
|
352 |
this.fromDefaultsToUISingle(entry.getKey(), entry.getValue());
|
353 |
break;
|
354 |
} |
355 |
|
356 |
} |
357 |
|
358 |
} |
359 |
|
360 |
private void fromUIToParamsSingle(DynField field, Component component) { |
361 |
JTextField text = (JTextField) component; |
362 |
|
363 |
switch (field.getTheTypeOfAvailableValues()) {
|
364 |
case DataTypes.STRING:
|
365 |
parameters.setDynValue(field.getName(), text.getText()); |
366 |
break;
|
367 |
|
368 |
default:
|
369 |
//FIXME Warning !!!
|
370 |
parameters.setDynValue(field.getName(), text.getText()); |
371 |
break;
|
372 |
} |
373 |
} |
374 |
|
375 |
|
376 |
private Map<DynField, Component> getComponentField() { |
377 |
if (this.componentField == null) { |
378 |
this.componentField = new HashMap<DynField, Component>(); |
379 |
} |
380 |
return componentField;
|
381 |
|
382 |
} |
383 |
|
384 |
|
385 |
public void actionPerformed(ActionEvent e) { |
386 |
Component source = (Component) e.getSource(); |
387 |
if (source == this.botAcept) { |
388 |
this.fromUIToParams();
|
389 |
this.closeWindow();
|
390 |
|
391 |
} else if (source == this.botCancel) { |
392 |
//TODO Close windows
|
393 |
this.closeWindow();
|
394 |
} else if (source == this.botRestoreDefaults) { |
395 |
this.fromDefaultsToUI();
|
396 |
|
397 |
} |
398 |
if (!this.getComponentField().containsValue(e.getSource())) { |
399 |
return;
|
400 |
} |
401 |
System.out.println("Action perform:" + source); |
402 |
|
403 |
} |
404 |
|
405 |
protected void closeWindow() { |
406 |
if (debug_mode) {
|
407 |
System.out.println("Resut:"); |
408 |
Iterator<DynField> iter = Arrays.asList( |
409 |
this.parameters.getDynClass().getDynFields()).iterator();
|
410 |
DynField field; |
411 |
while (iter.hasNext()) {
|
412 |
field = iter.next(); |
413 |
System.out.println("\t" |
414 |
+ field.getName() |
415 |
+ " = "
|
416 |
+ this.parameters.getDynValue(field.getName())
|
417 |
.toString()); |
418 |
} |
419 |
|
420 |
} |
421 |
//TODO implement
|
422 |
System.out.println("**'closeWindow' method must be implemented**"); |
423 |
|
424 |
} |
425 |
|
426 |
/* (non-Javadoc)
|
427 |
* @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
|
428 |
*/
|
429 |
public WindowInfo getWindowInfo() {
|
430 |
// TODO Auto-generated method stub
|
431 |
return null; |
432 |
} |
433 |
|
434 |
/**
|
435 |
* @param args
|
436 |
*/
|
437 |
public static void main(String[] args) { |
438 |
try {
|
439 |
ToolsLibrary toolsLibrary = new ToolsLibrary();
|
440 |
|
441 |
toolsLibrary.initialize(); |
442 |
toolsLibrary.postInitialize(); |
443 |
|
444 |
DALLibrary dalLibrary = new DALLibrary();
|
445 |
dalLibrary.initialize(); |
446 |
dalLibrary.postInitialize(); |
447 |
|
448 |
DALFileLibrary dalFileLibrary = new DALFileLibrary();
|
449 |
dalFileLibrary.initialize(); |
450 |
dalFileLibrary.postInitialize(); |
451 |
|
452 |
DBFLibrary dbfLibrary = new DBFLibrary();
|
453 |
dbfLibrary.initialize(); |
454 |
dbfLibrary.postInitialize(); |
455 |
|
456 |
SHPLibrary shpLibrary = new SHPLibrary();
|
457 |
shpLibrary.initialize(); |
458 |
shpLibrary.postInitialize(); |
459 |
|
460 |
JFrame frame = new JFrame(); |
461 |
|
462 |
DataParameters params; |
463 |
//
|
464 |
// params = DALLocator.getDataManager()
|
465 |
// .createStoreParameters(SHPStoreProvider.NAME);
|
466 |
// params.setSHPFileName("test.shp");
|
467 |
|
468 |
params = DALLocator.getDataManager() |
469 |
.createServerExplorerParameters( |
470 |
FilesystemServerExplorer.NAME); |
471 |
|
472 |
params.setDynValue("root", "/"); |
473 |
params.setDynValue("initialpath", "/home"); |
474 |
|
475 |
|
476 |
|
477 |
|
478 |
DataParametersEditor editor = new DataParametersEditor(params);
|
479 |
editor.debug_mode = true;
|
480 |
|
481 |
frame.setLayout(new BorderLayout()); |
482 |
frame.add(editor); |
483 |
|
484 |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
485 |
frame.setBounds(10, 10, 400, 400); |
486 |
frame.setVisible(true);
|
487 |
|
488 |
} catch (Exception e) { |
489 |
e.printStackTrace(); |
490 |
System.exit(-1); |
491 |
return;
|
492 |
} |
493 |
|
494 |
} |
495 |
|
496 |
} |