Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / DynObjectEditor.java @ 30754

History | View | Annotate | Download (26.5 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.Color;
35
import java.awt.Component;
36
import java.awt.GridBagConstraints;
37
import java.awt.GridBagLayout;
38
import java.awt.Insets;
39
import java.awt.event.ActionEvent;
40
import java.awt.event.ActionListener;
41
import java.util.ArrayList;
42
import java.util.Arrays;
43
import java.util.HashMap;
44
import java.util.Iterator;
45
import java.util.List;
46
import java.util.Map;
47
import java.util.Map.Entry;
48

    
49
import javax.swing.JComboBox;
50
import javax.swing.JFrame;
51
import javax.swing.JLabel;
52
import javax.swing.JPanel;
53
import javax.swing.JScrollPane;
54
import javax.swing.JSpinner;
55
import javax.swing.JTextField;
56
import javax.swing.ScrollPaneConstants;
57
import javax.swing.SpinnerModel;
58
import javax.swing.SpinnerNumberModel;
59

    
60
import org.cresques.cts.IProjection;
61
import org.gvsig.andami.PluginServices;
62
import org.gvsig.andami.messages.NotificationManager;
63
import org.gvsig.andami.ui.mdiManager.IWindow;
64
import org.gvsig.andami.ui.mdiManager.WindowInfo;
65
import org.gvsig.app.gui.panels.CRSSelectPanel;
66
import org.gvsig.fmap.dal.DataTypes;
67
import org.gvsig.gui.beans.swing.JButton;
68
import org.gvsig.tools.ToolsLocator;
69
import org.gvsig.tools.dynobject.DynClass;
70
import org.gvsig.tools.dynobject.DynField;
71
import org.gvsig.tools.dynobject.DynObject;
72
import org.gvsig.tools.dynobject.DynObjectValueItem;
73
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
74

    
75

    
76
/**
77
 * @author jmvivo
78
 *
79
 */
80
public class DynObjectEditor extends JPanel implements IWindow,
81
                ActionListener {
82

    
83
        /**
84
         *
85
         */
86
        private static final long serialVersionUID = 23898787077741411L;
87
        public static final int SHOW_ALL = 0;
88
        public static final int SHOW_ONLY_THIS_PARAMS = 1;
89
        public static final int HIDDE_THIS_PARAMS = 2;
90

    
91
        private static boolean debug_mode = false;
92

    
93
        private String title;
94

    
95
        private DynObject parameters;
96
        private List<DynField> fields;
97
        private JButton botAcept;
98
        private JButton botCancel;
99
        private JButton botRestoreDefaults;
100
        private boolean showButtons;
101
        private JPanel panButtons;
102
        private JPanel panParameters;
103
        private Map<DynField, Component> componentField;
104

    
105
        private Color mandatoryLabelColor = Color.red;
106
        private boolean modal;
107
        private JScrollPane parametersScroll;
108

    
109
        protected Map<String, Object> tempValue = new HashMap<String, Object>();
110
        public boolean srsShowTransformPanel;
111

    
112

    
113
        public DynObjectEditor(DynObject parameters) {
114
                this(parameters, SHOW_ALL, null);
115
        }
116

    
117

    
118
        public DynObjectEditor(DynObject parameters, int mode,
119
                        List<String> paramsNames) {
120
                this(parameters, mode, paramsNames, true, false);
121

    
122
        }
123

    
124
        public DynObjectEditor(DynObject parameters, int mode,
125
                        List<String> paramsNames, boolean showButtons,
126
                        boolean srsShowTransformPanel) {
127

    
128
                super();
129
                this.parameters = parameters;
130
                this.fields = Arrays.asList(parameters.getDynClass().getDynFields());
131
                this.showButtons = showButtons;
132
                this.srsShowTransformPanel = srsShowTransformPanel;
133
                if (mode == SHOW_ONLY_THIS_PARAMS || mode == HIDDE_THIS_PARAMS) {
134
                        if (paramsNames == null) {
135
                                throw new IllegalArgumentException("must expecify a name list");
136
                        }
137
                        ArrayList<DynField> toShow = new ArrayList<DynField>();
138
                        List<DynField> fields = Arrays.asList(parameters.getDynClass()
139
                                        .getDynFields());
140
                        Iterator<DynField> iter = fields.iterator();
141
                        DynField field;
142
                        while (iter.hasNext()) {
143
                                field = iter.next();
144

    
145
                                if (mode == SHOW_ONLY_THIS_PARAMS
146
                                                && paramsNames.contains(field.getName())) {
147
                                        toShow.add(field);
148
                                } else if (mode == HIDDE_THIS_PARAMS
149
                                                && !(paramsNames.contains(field.getName()))) {
150
                                        toShow.add(field);
151
                                }
152
                        }
153
                        this.fields = toShow;
154
                } else if (mode == SHOW_ALL) {
155

    
156
                } else{
157
                        throw new IllegalArgumentException();
158
                }
159
                this.initialize();
160

    
161

    
162
        }
163

    
164
        private void initialize() {
165
                this.setLayout(new BorderLayout(6, 6));
166
                this.add(this.getParametersScroll(), BorderLayout.CENTER);
167

    
168
                if (this.showButtons) {
169
                        this.add(this.getButtonsPanel(), BorderLayout.SOUTH);
170
                }
171
                this.fromParamsToUI();
172
        }
173

    
174
        private JPanel getButtonsPanel() {
175
                if (this.panButtons == null) {
176
                        this.panButtons = new JPanel();
177
                        this.panButtons.setLayout(new GridBagLayout());
178
                        GridBagConstraints constr = new GridBagConstraints();
179
                        constr.anchor = GridBagConstraints.LAST_LINE_END;
180
                        constr.fill = GridBagConstraints.HORIZONTAL;
181
                        constr.weightx = 1;
182
                        constr.weighty = 0;
183
                        this.panButtons.add(new JLabel(), constr);
184

    
185
                        constr = this.getDefaultParametersConstraints();
186
                        constr.fill = GridBagConstraints.NONE;
187
                        constr.weightx = 0;
188
                        constr.weighty = 0;
189

    
190
                        this.panButtons.add(this.getAcceptButton(), constr);
191
                        this.panButtons.add(this.getCancelButton(), constr);
192
                        this.panButtons.add(this.getRestoreDefaults(), constr);
193
                }
194
                return this.panButtons;
195
        }
196

    
197
        private JButton getRestoreDefaults() {
198
                if (this.botRestoreDefaults == null) {
199
                        this.botRestoreDefaults = new JButton();
200
                        this.botRestoreDefaults.addActionListener(this);
201
                        this.botRestoreDefaults
202
                                        .setText(PluginServices.getText(this, "restoreDefaults"));
203
                }
204
                return this.botRestoreDefaults;
205
        }
206

    
207
        private JButton getCancelButton() {
208
                if (this.botCancel == null) {
209
                        this.botCancel = new JButton();
210
                        this.botCancel.addActionListener(this);
211
                        this.botCancel.setText(PluginServices.getText(this, "cancel"));
212
                }
213
                return this.botCancel;
214
        }
215

    
216
        private JButton getAcceptButton() {
217
                if (this.botAcept == null){
218
                        this.botAcept = new JButton();
219
                        this.botAcept.addActionListener(this);
220
                        this.botAcept.setText(PluginServices.getText(this, "accept"));
221
                }
222
                return this.botAcept;
223
        }
224

    
225
        private GridBagConstraints getDefaultParametersConstraints() {
226
                GridBagConstraints constr = new GridBagConstraints();
227
                constr.insets = new Insets(2, 2, 2, 2);
228
                constr.ipadx = 2;
229
                constr.ipady = 2;
230
                constr.anchor = GridBagConstraints.PAGE_START;
231
                return constr;
232

    
233
        }
234

    
235
        private Component getParametersScroll() {
236
                if (parametersScroll == null) {
237
                        parametersScroll = new JScrollPane();
238
                        parametersScroll.setViewportView(getParametersPanel());
239
                        parametersScroll.setBorder(null);
240
                        parametersScroll
241
                                        .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
242
                        parametersScroll
243
                                        .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
244
                }
245
                return parametersScroll;
246
        }
247

    
248

    
249
        private JPanel getParametersPanel() {
250
                if (this.panParameters == null) {
251
                        this.panParameters = new JPanel();
252
                        this.panParameters.setLayout(new GridBagLayout());
253
//                        this.panParameters.setBorder(BorderFactory
254
//                                        .createTitledBorder(this
255
//                                        .getLocalizedText("values")));
256

    
257
                        Iterator<DynField> iter = this.fields.iterator();
258
                        DynField field;
259
                        while (iter.hasNext()) {
260
                                field = iter.next();
261
                                switch (field.getTheTypeOfAvailableValues()) {
262
                                case DynField.SINGLE:
263
                                        this.addFieldSingle(this.panParameters, field);
264
                                        break;
265
                                case DynField.CHOICE:
266
                                        this.addFieldChoice(this.panParameters, field);
267
                                        break;
268
                                case DynField.RANGE:
269
                                        this.addFieldRange(this.panParameters, field);
270
                                        break;
271

    
272
                                default:
273
                                        //FIXME lanzar un warning???
274
                                        this.addFieldSingle(this.panParameters, field);
275
                                        break;
276
                                }
277

    
278
                        }
279
                        GridBagConstraints constr = new GridBagConstraints();
280
                        constr.fill = GridBagConstraints.BOTH;
281
                        constr.weightx = 1;
282
                        constr.weighty = 1;
283
                        constr.gridheight = GridBagConstraints.REMAINDER;
284
                        constr.gridwidth = GridBagConstraints.REMAINDER;
285
                        this.panParameters.add(new JLabel(), constr);
286
                }
287
                return this.panParameters;
288
        }
289

    
290
        private JLabel createFieldLabel(DynField field) {
291
                JLabel label = new JLabel();
292
                label.setText(PluginServices.getText(this, field.getDescription()));
293

    
294
                if (field.isMandatory()) {
295
                        label.setForeground(this.mandatoryLabelColor);
296
                }
297
                return label;
298
        }
299

    
300
        private void addFieldRange(JPanel panel, DynField field) {
301
                GridBagConstraints constr = this.getDefaultParametersConstraints();
302
                constr.fill = GridBagConstraints.HORIZONTAL;
303
                constr.weighty = 0;
304
                constr.weightx = 0;
305

    
306
                JLabel label = this.createFieldLabel(field);
307
                constr.gridwidth = GridBagConstraints.RELATIVE;
308
                panel.add(label, constr);
309

    
310

    
311
                constr.gridwidth = GridBagConstraints.REMAINDER;
312
                constr.weightx = 1;
313

    
314
                Object max, min;
315
                double dmax, dmin,step;
316
                max = field.getMaxValue();
317
                min = field.getMinValue();
318
                boolean decimal;
319

    
320
                if (!(max instanceof Number)) {
321
                        max = null;
322
                }
323
                if (!(min instanceof Number)) {
324
                        min = null;
325
                }
326
                dmax = 0;
327
                dmin = 0;
328
                step = 1;
329

    
330
                switch (field.getType()) {
331
                case DataTypes.INT:
332
                        decimal = false;
333
                        if (max == null) {
334
                                max = Integer.MAX_VALUE;
335
                        } else {
336
                                dmax = ((Integer) max).doubleValue();
337
                        }
338
                        if (min == null) {
339
                                dmin = Integer.MIN_VALUE;
340
                        } else {
341
                                dmin = ((Integer) min).doubleValue();
342
                        }
343
                        break;
344

    
345
                case DataTypes.LONG:
346
                        decimal = false;
347
                        if (max == null) {
348
                                dmax = Long.MAX_VALUE;
349
                        } else {
350
                                dmax = ((Long) max).doubleValue();
351
                        }
352
                        if (min == null) {
353
                                dmin = Long.MIN_VALUE;
354
                        } else {
355
                                dmin = ((Long) min).doubleValue();
356
                        }
357
                        break;
358

    
359
                case DataTypes.BYTE:
360
                        decimal = false;
361
                        if (max == null) {
362
                                dmax = Byte.MAX_VALUE;
363
                        } else {
364
                                dmax = ((Byte) max).doubleValue();
365
                        }
366
                        if (min == null) {
367
                                dmin = Byte.MIN_VALUE;
368
                        } else {
369
                                dmin = ((Byte) min).doubleValue();
370
                        }
371
                        break;
372

    
373
                case DataTypes.FLOAT:
374
                        decimal = true;
375
                        if (max == null) {
376
                                dmax = Float.MAX_VALUE;
377
                        } else {
378
                                dmax = ((Float) max).doubleValue();
379
                        }
380
                        if (min == null) {
381
                                dmin = Float.MIN_VALUE;
382
                        } else {
383
                                dmin = ((Float) min).doubleValue();
384
                        }
385
                        break;
386

    
387
                case DataTypes.DOUBLE:
388
                        decimal = true;
389
                        if (max == null) {
390
                                dmax = Double.MAX_VALUE;
391
                        } else {
392
                                dmax = ((Double) max).doubleValue();
393
                        }
394
                        if (min == null) {
395
                                dmin = Double.MIN_VALUE;
396
                        } else {
397
                                dmin = ((Double) min).doubleValue();
398
                        }
399
                        break;
400

    
401
                default:
402
                        JTextField text = new JTextField();
403
                        text.setEnabled(false);
404
                        panel.add(text, constr);
405
                        this.getComponentField().put(field, text);
406
                        return;
407
                }
408
                if ((dmax -dmin) != 0){
409
                        step = (dmax - dmin) / 20;
410
                }
411

    
412

    
413
                SpinnerModel spinnerModel;
414
                if (decimal){
415
                        spinnerModel = new SpinnerNumberModel(dmin, dmin, dmax, step);
416
                } else{
417
                        spinnerModel = new SpinnerNumberModel((int) dmin, (int) dmin,
418
                                        (int) dmax, (int) step);
419
                }
420
                JSpinner spinner = new JSpinner(spinnerModel);
421
                spinner.setName(field.getName());
422
                constr.gridwidth = GridBagConstraints.REMAINDER;
423
                constr.weightx = 1;
424
                panel.add(spinner, constr);
425
                this.getComponentField().put(field, spinner);
426
        }
427

    
428
        private void addFieldChoice(JPanel panel, DynField field) {
429
                GridBagConstraints constr = this.getDefaultParametersConstraints();
430
                constr.fill = GridBagConstraints.HORIZONTAL;
431
                constr.weighty = 0;
432
                constr.weightx = 0;
433

    
434
                JLabel label = this.createFieldLabel(field);
435
                constr.gridwidth = GridBagConstraints.RELATIVE;
436
                panel.add(label, constr);
437

    
438
                JComboBox combo = new JComboBox(field.getAvailableValues());
439
                combo.setName(field.getName());
440
                constr.gridwidth = GridBagConstraints.REMAINDER;
441
                constr.weightx = 1;
442
                panel.add(combo, constr);
443
                this.getComponentField().put(field, combo);
444

    
445
        }
446

    
447
        private void addFieldSingle(JPanel panel,DynField field) {
448
                GridBagConstraints constr = this.getDefaultParametersConstraints();
449
                constr.fill = GridBagConstraints.HORIZONTAL;
450
                constr.weighty = 0;
451
                constr.weightx = 0;
452

    
453
                JLabel label = this.createFieldLabel(field);
454
                constr.gridwidth = GridBagConstraints.RELATIVE;
455
                panel.add(label, constr);
456

    
457

    
458
//                JTextField text = new JTextField();
459
                Component input = getComponentFor(field);
460
                input.setName(field.getName());
461
                constr.gridwidth = GridBagConstraints.REMAINDER;
462
                constr.weightx = 1;
463
                panel.add(input, constr);
464
                this.getComponentField().put(field, input);
465

    
466
        }
467

    
468
        private Component getComponentFor(DynField field) {
469
                int dataType = field.getType();
470
                Component result = null;
471

    
472
                switch (dataType) {
473
                case DataTypes.SRS:
474
                        result = new MyProjectionSelector();
475
                        break;
476

    
477
                default:
478
                        result = new JTextField();
479
                        break;
480
                }
481

    
482

    
483
                return result;
484
        }
485

    
486
        class MyProjectionSelector extends JPanel {
487
                /**
488
                 *
489
                 */
490
                private static final long serialVersionUID = -6331922386501903468L;
491

    
492
                private CRSSelectPanel crsSelector = null;
493
                private IProjection srs = null;
494

    
495
                MyProjectionSelector(){
496
                        super();
497
                        init();
498
                }
499

    
500
                private void init() {
501
                        this.setValue(null);
502
                }
503

    
504
                private void setCurrentProj(IProjection proj) {
505
                        if (proj == null){
506
                                srs = null;
507
                        } else {
508
                                srs = proj;
509
                        }
510
                }
511

    
512
                public void setValue(IProjection proj) {
513
                        if (crsSelector != null) {
514
                                this.remove(crsSelector);
515
                        }
516

    
517
                        crsSelector = CRSSelectPanel.getPanel(proj);
518
                        crsSelector
519
                                        .setTransPanelActive(DynObjectEditor.this.srsShowTransformPanel);
520
                        crsSelector.addActionListener(new ActionListener() {
521
                                public void actionPerformed(ActionEvent e) {
522
                                        setCurrentProj(crsSelector.getCurProj());
523
                                }
524
                        });
525

    
526
                        this.add(crsSelector);
527
                        this.doLayout();
528
                }
529

    
530
                public IProjection getValue() {
531
                        return srs;
532
                }
533

    
534

    
535

    
536
        }
537

    
538
        private void fromParamsToUISingle(DynField field, Component component) {
539

    
540
                Object value = parameters.getDynValue(field.getName());
541
                JTextField text;
542
                switch (field.getType()) {
543
                case DataTypes.STRING:
544
                        text = (JTextField) component;
545
                        if (value == null) {
546
                                text.setText("");
547
                                return;
548
                        }
549

    
550
                        text.setText((String) value);
551
                        break;
552

    
553
                case DataTypes.SRS:
554
                        ((MyProjectionSelector) component).setValue((IProjection) value);
555
                        break;
556

    
557
                default:
558
                        text = (JTextField) component;
559
                        if (value == null) {
560
                                text.setText("");
561
                                return;
562
                        }
563

    
564
                        text.setText(value.toString());
565
                        break;
566
                }
567

    
568

    
569
        }
570

    
571
        private void fromParamsToUIRange(DynField field, Component component) {
572
                JSpinner spinner = (JSpinner) component;
573

    
574
                Object value = parameters.getDynValue(field.getName());
575
                double dValue = 0;
576
                if (value == null) {
577
                        value = field.getMinValue();
578
                }
579
                if (value instanceof Double) {
580
                        dValue = ((Double) value).doubleValue();
581
                } else if (value instanceof String) {
582
                        dValue = Double.parseDouble((String) value);
583
                } else {
584
                        // FIXME ????
585
                        dValue = 0;
586
                }
587
                spinner.setValue(dValue);
588

    
589
        }
590

    
591
        private void fromParamsToUIChoice(DynField field, Component component) {
592
                JComboBox combo = (JComboBox) component;
593
                Object value = parameters.getDynValue(field.getName());
594

    
595
                DynObjectValueItem item = null;
596
                for (int i = 0; i < field.getAvailableValues().length; i++) {
597
                        if (value == null) {
598
                                if (field.getAvailableValues()[i].getValue() == null) {
599
                                        item = field.getAvailableValues()[i];
600
                                        break;
601
                                }
602
                        } else {
603
                                if (field.getAvailableValues()[i].getValue().equals(value)) {
604
                                        item = field.getAvailableValues()[i];
605
                                        break;
606
                                }
607

    
608
                        }
609
                }
610

    
611
                combo.getModel().setSelectedItem(item);
612

    
613
        }
614

    
615

    
616
        private void fromDefaultsToUISingle(DynField field, Component component) {
617
                JTextField text = (JTextField) component;
618

    
619
                if (field.getDefaultValue() == null) {
620
                        text.setText("");
621
                } else if (field.getType() == DataTypes.STRING) {
622
                        text.setText((String) field.getDefaultValue());
623
                } else {
624
                        text.setText(field.getDefaultValue().toString());
625
                }
626

    
627
        }
628

    
629
        private void fromDefaultsToUIChoice(DynField field, Component component) {
630
                JComboBox combo = (JComboBox) component;
631
                Object value = field.getDefaultValue();
632

    
633
                DynObjectValueItem item = null;
634
                for (int i = 0; i < field.getAvailableValues().length; i++) {
635
                        if (value == null) {
636
                                if (field.getAvailableValues()[i].getValue() == null) {
637
                                        item = field.getAvailableValues()[i];
638
                                        break;
639
                                }
640
                        } else {
641
                                if (field.getAvailableValues()[i].getValue().equals(value)) {
642
                                        item = field.getAvailableValues()[i];
643
                                        break;
644
                                }
645

    
646
                        }
647
                }
648

    
649
                combo.getModel().setSelectedItem(item);
650
        }
651

    
652
        private void fromDefaultsToUIRange(DynField field, Component component) {
653
                JSpinner spinner = (JSpinner) component;
654
                Object value = field.getDefaultValue();
655
                double dValue = 0;
656
                if (value == null) {
657
                        value = field.getMinValue();
658
                }
659

    
660

    
661
                if (value instanceof Double) {
662
                        dValue = ((Double) value).doubleValue();
663
                } else if (value instanceof String) {
664
                        dValue = Double.parseDouble((String) value);
665
                } else {
666
                        // FIXME ????
667
                        dValue = 0;
668
                }
669

    
670
                spinner.setValue(dValue);
671
        }
672

    
673
        public void fromParamsToUI() {
674
                Iterator<Entry<DynField, Component>> iter = this.getComponentField().entrySet().iterator();
675
                Entry<DynField, Component> entry;
676
                while (iter.hasNext()) {
677
                        entry = iter.next();
678

    
679
                        switch (entry.getKey().getTheTypeOfAvailableValues()) {
680
                        case DynField.SINGLE:
681
                                this.fromParamsToUISingle(entry.getKey(),entry.getValue());
682
                                break;
683
                        case DynField.CHOICE:
684
                                this.fromParamsToUIChoice(entry.getKey(), entry.getValue());
685
                                break;
686
                        case DynField.RANGE:
687
                                this.fromParamsToUIRange(entry.getKey(), entry.getValue());
688
                                break;
689

    
690
                        default:
691
                                // FIXME warning??
692
                                this.fromParamsToUISingle(entry.getKey(),entry.getValue());
693
                                break;
694
                        }
695

    
696
                }
697
        }
698

    
699
        public void fromDefaultsToUI() {
700
                Iterator<Entry<DynField, Component>> iter = this.getComponentField()
701
                                .entrySet().iterator();
702
                Entry<DynField, Component> entry;
703
                while (iter.hasNext()) {
704
                        entry = iter.next();
705

    
706
                        switch (entry.getKey().getTheTypeOfAvailableValues()) {
707
                        case DynField.SINGLE:
708
                                this.fromDefaultsToUISingle(entry.getKey(), entry.getValue());
709
                                break;
710
                        case DynField.CHOICE:
711
                                this.fromDefaultsToUIChoice(entry.getKey(), entry.getValue());
712
                                break;
713
                        case DynField.RANGE:
714
                                this.fromDefaultsToUIRange(entry.getKey(), entry.getValue());
715
                                break;
716

    
717
                        default:
718
                                // FIXME warning??
719
                                this.fromParamsToUISingle(entry.getKey(), entry.getValue());
720
                                break;
721
                        }
722

    
723
                }
724
        }
725

    
726

    
727
        public void fromUIToParams() {
728
                Iterator<Entry<DynField, Component>> iter = this.getComponentField()
729
                                .entrySet().iterator();
730
                Entry<DynField, Component> entry;
731
                while (iter.hasNext()) {
732
                        entry = iter.next();
733

    
734
                        switch (entry.getKey().getTheTypeOfAvailableValues()) {
735
                        case DynField.SINGLE:
736
                                this.fromUIToParamsSingle(entry.getKey(), entry.getValue());
737
                                break;
738
                        case DynField.CHOICE:
739
                                this.fromUIToParamsChoice(entry.getKey(), entry.getValue());
740
                                break;
741
                        case DynField.RANGE:
742
                                this.fromUIToParamsRange(entry.getKey(), entry.getValue());
743
                                break;
744

    
745
                        default:
746
                                // FIXME warning??
747
                                this.fromUIToParamsSingle(entry.getKey(), entry.getValue());
748
                                break;
749
                        }
750

    
751
                }
752

    
753
        }
754

    
755
        private void fromUIToParamsSingle(DynField field, Component component) {
756
                JTextField text;
757

    
758
                switch (field.getType()) {
759
                case DataTypes.STRING:
760
                        text = (JTextField) component;
761
                        if (text.getText().length() == 0){
762
                                String curValue = (String) parameters.getDynValue(field
763
                                                .getName());
764
                                if (curValue == null || curValue.length() == 0) {
765
                                        return;
766
                                }
767
                        }
768
                        parameters.setDynValue(field.getName(), text.getText());
769
                        break;
770

    
771
                case DataTypes.INT:
772
                        text = (JTextField) component;
773
                        if (text.getText().length() == 0) {
774
                                Integer curValue = (Integer) parameters.getDynValue(field
775
                                                .getName());
776
                                if (curValue == null) {
777
                                        return;
778
                                }
779
                        }
780

    
781
                        try{
782
                                parameters.setDynValue(field.getName(), new Integer(Integer
783
                                                .parseInt(text.getText())));
784
                        } catch (NumberFormatException e) {
785
                                NotificationManager.addError(e);
786
                        }
787
                        break;
788

    
789
                case DataTypes.BOOLEAN:
790
                        text = (JTextField) component;
791

    
792
                        if (text.getText().length() == 0) {
793
                                Boolean curValue = (Boolean) parameters.getDynValue(field
794
                                                .getName());
795
                                if (curValue == null) {
796
                                        return;
797
                                }
798
                        }
799

    
800

    
801
                        parameters.setDynValue(field.getName(), new Boolean(Boolean
802
                                                .parseBoolean(text.getText())));
803

    
804
                        break;
805

    
806
                case DataTypes.SRS:
807
                        parameters.setDynValue(field.getName(),
808
                                        ((MyProjectionSelector) component).getValue());
809

    
810
                        break;
811

    
812
                case DataTypes.OBJECT:
813
                        //TODO
814

    
815

    
816
                        break;
817
                default:
818
                        //FIXME Warning !!!
819
                        text = (JTextField) component;
820
                        if (text.getText().length() == 0) {
821
                                Object curValue = parameters.getDynValue(field.getName());
822
                                if (curValue == null) {
823
                                        return;
824
                                }
825
                        }
826

    
827
                        parameters.setDynValue(field.getName(), text.getText());
828
                        break;
829
                }
830
        }
831

    
832

    
833
        private void fromUIToParamsChoice(DynField field, Component component) {
834
                JComboBox combo = (JComboBox) component;
835
                parameters.setDynValue(field.getName(), ((DynObjectValueItem) combo
836
                                .getSelectedItem()).getValue());
837
        }
838

    
839
        private void fromUIToParamsRange(DynField field, Component component) {
840
                JSpinner spinner = (JSpinner) component;
841
                parameters.setDynValue(field.getName(), spinner.getValue());
842

    
843
        }
844

    
845

    
846
        private Map<DynField, Component> getComponentField() {
847
                if (this.componentField == null) {
848
                        this.componentField = new HashMap<DynField, Component>();
849
                }
850
                return componentField;
851

    
852
        }
853

    
854

    
855
        public void actionPerformed(ActionEvent e) {
856
                Component source = (Component) e.getSource();
857
                if (source == this.botAcept) {
858
                        this.fromUIToParams();
859
                        this.closeWindow();
860

    
861
                } else if (source == this.botCancel) {
862
                        //TODO Close windows
863
                        this.closeWindow();
864
                } else if (source == this.botRestoreDefaults) {
865
                        this.fromDefaultsToUI();
866

    
867
                }
868
                if (!this.getComponentField().containsValue(e.getSource())) {
869
                        return;
870
                }
871
                System.out.println("Action perform:" + source);
872

    
873
        }
874

    
875
        protected void closeWindow() {
876
                if (debug_mode) {
877
                        System.out.println("Resut:");
878
                        Iterator<DynField> iter = Arrays.asList(
879
                                        this.parameters.getDynClass().getDynFields()).iterator();
880
                        DynField field;
881
                        while (iter.hasNext()) {
882
                                field = iter.next();
883
                                System.out.println("\t"
884
                                                + field.getName()
885
                                                + " = "
886
                                                + this.parameters.getDynValue(field.getName())
887
                                                                .toString());
888
                        }
889

    
890
                }
891
                if (PluginServices.getMainFrame() == null) {
892
                        ((JFrame) (getParent().getParent().getParent().getParent()))
893
                                        .dispose();
894
                } else {
895
                        PluginServices.getMDIManager().closeWindow(this);
896
                }
897
        }
898

    
899
        /* (non-Javadoc)
900
         * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
901
         */
902
        public WindowInfo getWindowInfo() {
903
                WindowInfo m_viewinfo;
904
                if (this.modal) {
905
                        m_viewinfo = new WindowInfo(WindowInfo.MODALDIALOG
906
                                        | WindowInfo.RESIZABLE);
907
                } else {
908
                        m_viewinfo = new WindowInfo(WindowInfo.RESIZABLE);
909
                }
910
                m_viewinfo.setTitle(this.title);
911
                m_viewinfo.setHeight(500);
912
                m_viewinfo.setWidth(520);
913
                return m_viewinfo;
914
        }
915

    
916
        /**
917
         * @param mandatoryLabelColor
918
         *            the mandatoryLabelColor to set
919
         */
920
        public void setMandatoryLabelColor(Color mandatoryLabelColor) {
921
                this.mandatoryLabelColor = mandatoryLabelColor;
922
        }
923

    
924
        /**
925
         * @return the mandatoryLabelColor
926
         */
927
        public Color getMandatoryLabelColor() {
928
                return mandatoryLabelColor;
929
        }
930

    
931
        /**
932
         * @param args
933
         */
934
        public static void main(String[] args) {
935
                try {
936
                        new DefaultLibrariesInitializer().fullInitialize();
937
                        //                        JFrame frame = new JFrame();
938
                        //
939
                        //                        DataParameters params;
940
                        ////
941
                        //                        //                        params = DALLocator.getDataManager().createStoreParameters(
942
                        //                        //                                        SHPStoreProvider.NAME);
943
                        //                        //                        ((SHPStoreParameters) params).setSHPFileName("test.shp");
944
                        //
945
                        //                        params = DALLocator.getDataManager()
946
                        //                                        .createServerExplorerParameters(
947
                        //                                                        FilesystemServerExplorer.NAME);
948
                        //
949
                        //                                                params.setDynValue("root", "/");
950
                        //                        params.setDynValue("initialpath", "/home");
951
                        //
952
                        //                        FilesystemServerExplorer fileExp = (FilesystemServerExplorer) DALLocator
953
                        //                                        .getDataManager()
954
                        //                                        .createServerExplorer((DataServerExplorerParameters) params);
955
                        //
956
                        //                        params = fileExp.getAddParameters(new File("x.shp"));
957
                        //
958
//
959
                        //                        DynObjectEditor editor = new DynObjectEditor(params);
960

    
961
                        JFrame frame = new JFrame();
962

    
963
                        DynClass dClass = ToolsLocator.getDynObjectManager()
964
                                        .createDynClass("DynObjectEditorTestClass",
965
                                                        "DynClas to test DynObjectEditor");
966

    
967
                        DynField field = dClass.addDynField("field1");
968
                        field.setDefaultDynValue("field1");
969
                        field.setDescription("field1 (simple) string");
970
                        field.setType(DataTypes.STRING);
971
                        field.setMandatory(true);
972

    
973
                        field = dClass.addDynField("field2");
974
                        field.setDefaultDynValue("field2");
975
                        field.setDescription("field2 (choice) string");
976
                        field.setType(DataTypes.STRING);
977
                        field.setTheTypeOfAvailableValues(DynField.CHOICE);
978
                        field.setAvailableValues(new DynObjectValueItem[] {
979
                                        new DynObjectValueItem("v1", "Value1 (v1)"),
980
                                        new DynObjectValueItem("v2", "Value2 (v2)"),
981
                                        new DynObjectValueItem("v3", "Value3 (v3)"),
982
                                        new DynObjectValueItem("v4", "Value4 (v4)"), });
983

    
984
                        field.setDefaultDynValue("v3");
985

    
986
                        field = dClass.addDynField("field3");
987
                        field.setDescription("field3 (range) integer(-5,28)");
988
                        field.setDefaultDynValue(new Integer(10));
989
                        field.setType(DataTypes.INT);
990
                        field.setTheTypeOfAvailableValues(DynField.RANGE);
991
                        field.setMinValue(new Integer(-5));
992
                        field.setMaxValue(new Integer(28));
993

    
994
                        field = dClass.addDynField("field4");
995
                        field.setDescription("field3 (range) float(-5.2,28.55)");
996
                        field.setDefaultDynValue(new Float(10));
997
                        field.setType(DataTypes.FLOAT);
998
                        field.setTheTypeOfAvailableValues(DynField.RANGE);
999
                        field.setMinValue(new Float(-5.2));
1000
                        field.setMaxValue(new Float(28.55));
1001

    
1002

    
1003
                        DynObject dynObj = ToolsLocator.getDynObjectManager()
1004
                                        .createDynObject(dClass);
1005

    
1006
                        DynObjectEditor editor = new DynObjectEditor(dynObj);
1007
                        DynObjectEditor.debug_mode = true;
1008

    
1009
                        frame.setLayout(new BorderLayout());
1010
                        frame.add(editor);
1011

    
1012
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1013
                        frame.setBounds(10, 10, 400, 400);
1014
                        frame.setVisible(true);
1015

    
1016
                } catch (Exception e) {
1017
                        e.printStackTrace();
1018
                        System.exit(-1);
1019
                        return;
1020
                }
1021

    
1022
        }
1023

    
1024
        public void editObject(boolean modal) {
1025
                this.modal = modal;
1026
                try {
1027
                        PluginServices.getMDIManager().addWindow(this);
1028
                } catch (Exception e) {
1029
                        JFrame frame = new JFrame();
1030
                        frame.setLayout(new BorderLayout());
1031
                        frame.add(this);
1032
                        frame.setBounds(10, 10, 400, 400);
1033
                        frame.setVisible(true);
1034
                }
1035
        }
1036

    
1037
        public String getTitle() {
1038
                return title;
1039
        }
1040

    
1041
        public void setTitle(String title) {
1042
                this.title = title;
1043
        }
1044

    
1045
        public Object getWindowProfile() {
1046
                return WindowInfo.PROPERTIES_PROFILE;
1047
        }
1048

    
1049
        public boolean isSrsShowTransformPanel() {
1050
                return srsShowTransformPanel;
1051
        }
1052

    
1053
        public void setSrsShowTransformPanel(boolean srsShowTransformPanel) {
1054
                this.srsShowTransformPanel = srsShowTransformPanel;
1055
        }
1056

    
1057
}