Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / DefaultJDynObjectComponent.java @ 306

History | View | Annotate | Download (17.1 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
 * 2010 {}  {{Task}}
26
 */
27
package org.gvsig.tools.swing.impl.dynobject;
28

    
29
import java.awt.Color;
30
import java.awt.Component;
31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.awt.Insets;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37

    
38
import javax.swing.BorderFactory;
39
import javax.swing.JComponent;
40
import javax.swing.JPanel;
41
import javax.swing.JScrollPane;
42
import javax.swing.JTabbedPane;
43

    
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

    
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dynobject.DynField;
49
import org.gvsig.tools.dynobject.DynObject;
50
import org.gvsig.tools.i18n.I18nManager;
51
import org.gvsig.tools.service.ServiceException;
52
import org.gvsig.tools.swing.api.dynobject.DynFieldModel;
53
import org.gvsig.tools.swing.api.dynobject.DynObjectModel;
54
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent;
55
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
56
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
57
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
58
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynObjectValueField;
59
import org.gvsig.tools.swing.spi.AbstractDynObjectComponent;
60

    
61
/**
62
 * Initial implementation based on a simple swing form generation.
63
 * 
64
 * @author 2008-2009 Jos? Manuel Viv?
65
 * @author 2010- C?sar Ordi?ana - gvSIG team
66
 */
67
public class DefaultJDynObjectComponent extends AbstractDynObjectComponent
68
    implements JDynObjectComponent, ValueField, ValueChangedListener {
69

    
70
    // implements ActionListener {
71

    
72
    private static final long serialVersionUID = 2888673056424008867L;
73

    
74
    private static final Logger LOG =
75
        LoggerFactory.getLogger(DefaultJDynObjectComponent.class);
76

    
77
    private final I18nManager i18nManager = ToolsLocator.getI18nManager();
78

    
79
    private DynObject parameters;
80

    
81
    private Color mandatoryLabelColor = Color.red;
82
    protected Map<String, Object> tempValue = new HashMap<String, Object>();
83
    private HashMap<DynField, StatusLabel> labelList;
84

    
85
    private JTabbedPane tabPanel;
86

    
87
    private JDynFieldComponent input;
88

    
89
    public DefaultJDynObjectComponent(DynObject parameters, DynObjectModel model)
90
        throws ServiceException {
91
        super(parameters, model);
92
        // added to handle labels correctly
93
        this.labelList = new HashMap<DynField, StatusLabel>();
94
        this.parameters = parameters;
95
        this.tabPanel = new JTabbedPane();
96
        this.tabPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,
97
            0));
98
        this.tabPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
99

    
100
        // Uncomment the following line to use scrolling tabs.
101
        this.tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
102
        addParametersFromModel();
103

    
104
    }
105

    
106
    private void addGridBagComponent(JPanel panel, ValueField field, int row,
107
        int numItems) throws ServiceException {
108

    
109
        input = getJDynFieldComponent(field, this);
110

    
111
        StatusLabel label = this.createFieldLabel(input);
112
        this.labelList.put(input.getDynField(), label);
113
        input.addValueChangedListener(label);
114

    
115
        Component component = input.asJComponent();
116
        component.setName(field.getDynField().getName());
117
        addComponentToList(component, input);
118

    
119
        // Arranging label and component into panel
120

    
121
        GridBagConstraints constr = new GridBagConstraints();
122
        constr.insets = new Insets(5, 5, 5, 5);
123
        if ((row == 0) && (numItems > 1)) {
124
            constr.weighty = 0;
125
            // constr.weightx = 1;
126
        } else
127
            if ((row == 2)) {
128
                constr.weighty = 1;
129
            } else {
130
                constr.weighty = 1;
131
            }
132
        // constr.weighty = 1;
133
        // if (row % 2 == 0){
134
        // constr.weighty = 1;
135
        // }else
136
        // constr.weighty = 0;
137

    
138
        constr.weightx = 0;
139
        constr.ipadx = 2;
140
        constr.ipady = 2;
141
        constr.fill = GridBagConstraints.RELATIVE;
142
        constr.anchor = GridBagConstraints.NORTHWEST;
143

    
144
        // if ((row==0)&&(numItems>1)){
145
        // constr.weighty = 1;
146
        // // constr.weightx = 1;
147
        // }
148
        // else{
149
        // constr.weighty = 0;
150
        // }
151

    
152
        constr.gridx = 0;
153
        constr.gridy = row;
154

    
155
        panel.add(label, constr);
156

    
157
        constr.gridx = 1;
158
        constr.ipady = 14;
159
        constr.fill = GridBagConstraints.NONE;
160
        panel.add(label.getValidationPanel(), constr);
161

    
162
        constr.gridx = 2;
163
        constr.ipady = 2;
164
        constr.weightx = 1;
165
        constr.fill = GridBagConstraints.HORIZONTAL;
166
        panel.add(component, constr);
167

    
168
        input.fireValueChangedEvent();
169
    }
170

    
171
    private void addParametersFromModel() throws ServiceException {
172
        DynField field;
173
        JPanel pane;
174
        String[] groups = this.getModel().getGroups();
175

    
176
        for (String group : groups) {
177
                        List<DynFieldModel> items = this.getModel().getGroupElements(group);
178

    
179
            pane = new JPanel(new GridBagLayout());
180
            pane.setBorder(BorderFactory.createEmptyBorder());
181

    
182
            for (int i = 0; i < items.size(); i++) {
183
                                field = items.get(i).getDynField();
184
                addGridBagComponent(pane, new DynObjectValueField(this
185
                    .getDynObject(), field.getName()), i, items.size());
186
            }
187
            this.tabPanel.addTab(group, createScrollPane(pane));
188
        }
189
    }
190

    
191
//    /**
192
//     * @param label
193
//     * @param field
194
//     */
195
//    private void checkValidation(JLabel label, JDynFieldComponent field) {
196
//        if (label == null)
197
//            return;
198
//        // if (!isValid(field)){
199
//        // label.setForeground(this.mandatoryLabelColor);
200
//        // }else{
201
//        // label.setForeground(Color.BLACK);
202
//        // }
203
//    }
204

    
205
    protected void closeWindow() {
206
        LOG.debug("Result DynObject: {}", parameters);
207

    
208
        // if (PluginServices.getMainFrame() == null) {
209
        // ((JFrame) (getParent().getParent().getParent().getParent()))
210
        // .dispose();
211
        // } else {
212
        // PluginServices.getMDIManager().closeWindow(this);
213
        // }
214
    }
215

    
216
    private StatusLabel createFieldLabel(JDynFieldComponent component) {
217
        DynField field = component.getDynField();
218

    
219
        String srcText = field.getDescription();
220
        srcText =
221
            ((srcText == null) || (srcText.length() == 0) ? field.getName()
222
                : srcText);
223
        String text = i18nManager.getTranslation(srcText);
224

    
225
        StatusLabel label = new StatusLabel(text, field.isMandatory());
226

    
227
        // adding label to a hashmap
228
        this.labelList.put(field, label);
229

    
230
        return label;
231
    }
232

    
233
    private Component createScrollPane(JPanel panel) throws ServiceException {
234
        JScrollPane scrollPane = new JScrollPane();
235
        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
236
        scrollPane.setAlignmentY(Component.TOP_ALIGNMENT);
237

    
238
        scrollPane.setViewportView(panel);
239
        scrollPane.setAutoscrolls(true);
240
        // // scrollPane.setBorder(null);
241
        // scrollPane
242
        // .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
243
        // scrollPane
244
        // .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
245
        return scrollPane;
246
    }
247

    
248
    /*
249
     * (non-Javadoc)
250
     * 
251
     * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
252
     */
253
    public JComponent asJComponent() {
254
        return this.tabPanel;
255
        // return this.panel;
256
    }
257

    
258
    /**
259
     * OTRAS PRUEBAS DELAYOUT
260
     */
261

    
262
    // private GridBagConstraints getDefaultParametersConstraints() {
263
    // GridBagConstraints constr = new GridBagConstraints();
264
    // constr.insets = new Insets(2, 2, 2, 2);
265
    // constr.ipadx = 2;
266
    // constr.ipady = 2;
267
    // return constr;
268
    // }
269

    
270
    // private JPanel createNorthWestPane(JPanel pane){
271
    // //Make a panel
272
    // JPanel northWestPanel = new JPanel(new GridLayout(1,1));
273
    // GridBagConstraints c = new GridBagConstraints();
274
    // c.gridx = 0;
275
    // c.gridy = 0;
276
    // c.anchor = GridBagConstraints.NORTHWEST;
277
    // c.insets = new Insets(0,0,0,0);
278
    // northWestPanel.add(pane, c);
279
    //
280
    // northWestPanel.add(pane);
281
    // return northWestPanel;
282
    // }
283

    
284
    // /**
285
    // * @throws ServiceException
286
    // *
287
    // */
288
    // private void addGridBagParametersFromModel() throws ServiceException {
289
    //
290
    // DynField field;
291
    // JPanel pane;
292
    // String[] groups = this.getModel().getGroups();
293
    //
294
    // for (String group: groups){
295
    // List items = this.getModel().getGroupElements(group);
296
    //                
297
    // pane = new JPanel(new GridBagLayout());
298
    // pane.setBorder(BorderFactory.createTitledBorder("Prueba"));
299
    //                
300
    // for (int i=0;i<items.size();i++) {
301
    // field = ((DynFieldModel)items.get(i))
302
    // .getDynField();
303
    // addGridBagFieldSingle(pane,field,i);
304
    // }
305
    // // this.tabPanel.addTab(group,createScrollPane(pane));
306
    // this.tabPanel.addTab(group,pane);
307
    // }
308
    //            
309
    // this.tabPanel.setSelectedIndex(0);
310
    // }
311

    
312
    // private void addFieldSingle(JPanel panel, DynField field, int row) throws
313
    // ServiceException {
314
    //            
315
    // JDynFieldComponent input = getJDynFieldComponent(field, this);
316
    //                
317
    // StatusLabel label = this.createFieldLabel(input);
318
    // input.addValueChangedListener(label);
319
    //                
320
    // // JTextField text = new JTextField();
321
    // //Component input = getComponentFor(field);
322
    // Component component = (Component) input.getComponent();
323
    // component.setName(field.getName());
324
    // addComponentToList(component,input);
325
    //                
326
    //                
327
    // //Arranging label and component into panel
328
    //                
329
    // GridBagConstraints constr = this.getDefaultParametersConstraints();
330
    //                
331
    // constr.fill = GridBagConstraints.HORIZONTAL;
332
    //                            
333
    // constr.gridx = 0;
334
    // // constr.gridy = row;
335
    // constr.gridheight = GridBagConstraints.RELATIVE;
336
    // constr.anchor = GridBagConstraints.PAGE_START;
337
    // constr.weighty = 0;
338
    // // constr.w = 1;
339
    // // constr.weightx = 0;
340
    // // constr.gridheight = GridBagConstraints.RELATIVE;
341
    // panel.add(label, BorderLayout.WEST);
342
    //
343
    //
344
    //                
345
    // panel.add(component, BorderLayout.CENTER);
346
    //                
347
    // input.fireValueChangedEvent();
348
    // }
349

    
350
    // private void initialize() throws ServiceException {
351
    // this.panel = new JPanel(new BorderLayout(6,6));
352
    // this.panel.setPreferredSize(new Dimension(800, 560));
353
    //                
354
    // // this.panel.add(this.getParametersScroll(), BorderLayout.PAGE_START);
355
    //
356
    // this.panel.add(this.getParametersPanel(isMainPanel),
357
    // BorderLayout.PAGE_START);
358
    // // this.panel = this.getParametersScroll();
359
    //
360
    // // this.panel.add(this.panParameters);
361
    // // if (this.showButtons) {
362
    // // this.add(this.getButtonsPanel(), BorderLayout.SOUTH);
363
    // // }
364
    // // this.fromParamsToUI();
365
    // }
366

    
367
    // private JPanel getButtonsPanel() {
368
    // if (this.panButtons == null) {
369
    // this.panButtons = new JPanel();
370
    // this.panButtons.setLayout(new GridBagLayout());
371
    // GridBagConstraints constr = new GridBagConstraints();
372
    // constr.anchor = GridBagConstraints.LAST_LINE_END;
373
    // constr.fill = GridBagConstraints.HORIZONTAL;
374
    // constr.weightx = 1;
375
    // constr.weighty = 0;
376
    // this.panButtons.add(new JLabel(), constr);
377
    //
378
    // const3r = this.getDefaultParametersConstraints();
379
    // constr33.fill = GridBagConstraints.NONE;
380
    // constr.weightx = 0;
381
    // constr.weighty = 0;
382
    //
383
    // this.panButtons.add(this.getAcceptButton(), constr);
384
    // this.panButtons.add(this.getCancelButton(), constr);
385
    // this.panButtons.add(this.getRestoreDefaults(), constr);
386
    // }
387
    // return this.panButtons;
388
    // }
389

    
390
    // private JButton getRestoreDefaults() {
391
    // if (this.botRestoreDefaults == null) {
392
    // this.botRestoreDefaults = usManager.createJButton(i18nManager
393
    // .getTranslation("restoreDefaults"));
394
    // this.botRestoreDefaults.addActionListener(this);
395
    // }
396
    // return this.botRestoreDefaults;
397
    // }
398
    //
399
    // private JButton getCancelButton() {
400
    // if (this.botCancel == null) {
401
    // this.botCancel = usManager.createJButton(i18nManager
402
    // .getTranslation("cancel"));
403
    // this.botCancel.addActionListener(this);
404
    // }
405
    // return this.botCancel;
406
    // }
407
    //
408
    // private JButton getAcceptButton() {
409
    // if (this.botAcept == null) {
410
    // this.botAcept = usManager.createJButton(i18nManager
411
    // .getTranslation("accept"));
412
    // this.botAcept.addActionListener(this);
413
    // }
414
    // return this.botAcept;
415
    // }
416

    
417
    // private void addGridLayoutComponent(JPanel panel, DynField field, int
418
    // row) throws ServiceException {
419
    //            
420
    // JDynFieldComponent input = getJDynFieldComponent(field, this);
421
    //                
422
    // StatusLabel label = this.createFieldLabel(input);
423
    // input.addValueChangedListener(label);
424
    //                
425
    // // JTextField text = new JTextField();
426
    // //Component input = getComponentFor(field);
427
    // Component component = (Component) input.getComponent();
428
    // component.setName(field.getName());
429
    // addComponentToList(component,input);
430
    //                
431
    //                
432
    // //Arranging label and component into panel
433
    //                
434
    // GridBagConstraints constr = this.getDefaultParametersConstraints();
435
    //                
436
    // // constr.fill = GridBagConstraints.HORIZONTAL;
437
    // //
438
    // // constr.gridx = 0;
439
    // //// constr.gridy = row;
440
    // // constr.gridheight = GridBagConstraints.RELATIVE;
441
    // // if (row==0){
442
    // // constr.anchor = GridBagConstraints.NORTHWEST;
443
    // // }else{
444
    // // constr.anchor = GridBagConstraints.PAGE_START;
445
    // // }
446
    // // constr.weighty = 0;
447
    // // constr.w = 1;
448
    // // constr.weightx = 0;
449
    // // constr.gridheight = GridBagConstraints.RELATIVE;
450
    // panel.add(label);
451
    //
452
    //
453
    // // constr.fill = GridBagConstraints.BOTH;
454
    // // if (row==0){
455
    // // constr.anchor = GridBagConstraints.NORTHWEST;
456
    // // }else{
457
    // // constr.anchor = GridBagConstraints.PAGE_START;
458
    // // }
459
    // // constr.gridx = 1;
460
    // // constr.gridwidth = GridBagConstraints.NONE;
461
    // // constr.weightx = 0.70;
462
    // panel.add(component);
463
    //                
464
    // input.fireValueChangedEvent();
465
    // }
466

    
467
    private StatusLabel getFieldLabel(DynField field) {
468
        return this.labelList.get(field);
469
    }
470

    
471
    /**
472
     * @return the mandatoryLabelColor
473
     */
474
    public Color getMandatoryLabelColor() {
475
        return mandatoryLabelColor;
476
    }
477

    
478
    /*
479
     * (non-Javadoc)
480
     * 
481
     * @see org.gvsig.tools.swing.spi.AbstractDynObjectComponent#getValue()
482
     */
483
    public Object getValue() {
484
        return this.getDynObject();
485
    }
486

    
487
    /*
488
     * (non-Javadoc)
489
     * 
490
     * @see
491
     * 
492
     * org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
493
     * (org.gvsig.tools.swing.api.dynobject.JDynObjectComponent,
494
     * org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
495
     */
496
    public void handleValueChanged(JDynFieldComponent field) {
497
        StatusLabel label = getFieldLabel(field.getDynField());
498
        if (label != null) {
499
            label.handleValueChanged(field);
500
        }
501
        // this.checkValidation(label, field);
502
    }
503

    
504
    /*
505
     * (non-Javadoc)
506
     * 
507
     * @see
508
     * org.gvsig.tools.swing.api.dynobject.JDynObjectComponent#requestFocus()
509
     */
510
    public void requestFocus() {
511
    }
512

    
513
    /**
514
     * @param mandatoryLabelColor
515
     *            the mandatoryLabelColor to set
516
     */
517
    public void setMandatoryLabelColor(Color mandatoryLabelColor) {
518
        this.mandatoryLabelColor = mandatoryLabelColor;
519
    }
520

    
521
    /*
522
     * (non-Javadoc)
523
     * 
524
     * @see
525
     * org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
526
     * .lang.Object)
527
     */
528
    public void setValue(Object value) {
529
        if (value != null) {
530
            if (value instanceof DynObject) {
531
                fillValues((DynObject) value);
532
            }
533
            return;
534
        }
535
        this.emptyAll();
536
    }
537

    
538
    public DynField getDynField() {
539
       return input.getDynField();
540
    }
541

    
542
    public Object getDefaultFieldValue() {
543
        return input.getDefaultFieldValue();
544
    }
545

    
546
    public Object getFieldValue() {
547
        return input.getFieldValue();
548
    }
549

    
550
    public void setFieldValue(Object value) {
551
        input.setFieldValue(value);
552
    }
553

    
554
}