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 @ 202

History | View | Annotate | Download (15.9 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.ArrayList;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38

    
39
import javax.swing.JLabel;
40
import javax.swing.JPanel;
41
import javax.swing.JScrollPane;
42
import javax.swing.JTabbedPane;
43

    
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.dynobject.DynField;
46
import org.gvsig.tools.dynobject.DynObject;
47
import org.gvsig.tools.i18n.I18nManager;
48
import org.gvsig.tools.service.ServiceException;
49
import org.gvsig.tools.swing.api.dynobject.DynFieldComponentModel;
50
import org.gvsig.tools.swing.api.dynobject.DynObjectModel;
51
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent;
52
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
53
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
54
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
55
import org.gvsig.tools.swing.spi.AbstractDynObjectComponent;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

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

    
69
    private static final long serialVersionUID = 2888673056424008867L;
70

    
71
    private static final Logger LOG = LoggerFactory
72
            .getLogger(DefaultJDynObjectComponent.class);
73

    
74
    private final I18nManager i18nManager = ToolsLocator.getI18nManager();
75

    
76
    private DynObject parameters;
77

    
78
    private Color mandatoryLabelColor = Color.red;
79
    protected Map<String, Object> tempValue = new HashMap<String, Object>();
80
    private HashMap<DynField, JLabel> labelList;
81

    
82
    private JTabbedPane tabPanel;
83

    
84
    public DefaultJDynObjectComponent(DynObject parameters, DynObjectModel model)
85
            throws ServiceException {
86
        super(parameters, model);
87
        // added to handle labels correctly
88
        this.labelList = new HashMap<DynField, JLabel>();
89
        this.parameters = parameters;
90
        this.tabPanel = new JTabbedPane();
91
        // Uncomment the following line to use scrolling tabs.
92
        this.tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
93
        addParametersFromModel();
94

    
95
    }
96

    
97

    
98
    private void addGridBagComponent(JPanel panel, DynField field, int row,
99
            int numItems) throws ServiceException {
100

    
101
        JDynFieldComponent input = getJDynFieldComponent(field, this);
102

    
103
        StatusLabel label = this.createFieldLabel(input);
104
        input.addValueChangedListener(label);
105

    
106
        Component component = (Component) input.getComponent();
107
        component.setName(field.getName());
108
        addComponentToList(component, input);
109

    
110
        // Arranging label and component into panel
111

    
112
        GridBagConstraints constr = new GridBagConstraints();
113
        constr.insets = new Insets(3, 3, 3, 3);
114
        if ((row == 0) && (numItems > 1)) {
115
            constr.weighty = 0;
116
            // constr.weightx = 1;
117
        } else if ((row == 2))
118
            constr.weighty = 1;
119
        else {
120
            constr.weighty = 1;
121
        }
122
        // constr.weighty = 1;
123
        // if (row % 2 == 0){
124
        // constr.weighty = 1;
125
        // }else
126
        // constr.weighty = 0;
127

    
128
        constr.weightx = 0;
129
        constr.ipadx = 2;
130
        constr.ipady = 2;
131
        constr.fill = GridBagConstraints.RELATIVE;
132
        constr.anchor = GridBagConstraints.NORTHWEST;
133

    
134
        // if ((row==0)&&(numItems>1)){
135
        // constr.weighty = 1;
136
        // // constr.weightx = 1;
137
        // }
138
        // else{
139
        // constr.weighty = 0;
140
        // }
141

    
142
        constr.gridx = 0;
143
        constr.gridy = row;
144

    
145
        panel.add(label, constr);
146

    
147
        constr.gridx = 1;
148
        constr.ipady = 14;
149
        constr.fill = GridBagConstraints.NONE;
150
        panel.add(label.getValidationLabel(), constr);
151

    
152
        constr.gridx = 2;
153
        constr.ipady = 2;
154
        constr.weightx = 1;
155
        constr.fill = GridBagConstraints.HORIZONTAL;
156
        panel.add(component, constr);
157

    
158
        input.fireValueChangedEvent();
159
    }
160

    
161
    private void addParametersFromModel() throws ServiceException {
162
        DynField field;
163
        JPanel pane;
164
        String[] groups = this.getModel().getGroups();
165

    
166
        for (String group : groups) {
167
            List items = this.getModel().getGroupElements(group);
168

    
169
            pane = new JPanel(new GridBagLayout());
170

    
171
            for (int i = 0; i < items.size(); i++) {
172
                field = ((DynFieldComponentModel) items.get(i)).getDynField();
173
                addGridBagComponent(pane, field, i, items.size());
174

    
175
            }
176
            this.tabPanel.addTab(group, createScrollPane(pane));
177
        }
178
    }
179

    
180
    /**
181
     * @param label
182
     * @param field
183
     */
184
    private void checkValidation(JLabel label, JDynFieldComponent field) {
185
        if (label == null)
186
            return;
187
        // if (!isValid(field)){
188
        // label.setForeground(this.mandatoryLabelColor);
189
        // }else{
190
        // label.setForeground(Color.BLACK);
191
        // }
192
    }
193

    
194
    protected void closeWindow() {
195
        LOG.debug("Result DynObject: {}", parameters);
196

    
197
        // if (PluginServices.getMainFrame() == null) {
198
        // ((JFrame) (getParent().getParent().getParent().getParent()))
199
        // .dispose();
200
        // } else {
201
        // PluginServices.getMDIManager().closeWindow(this);
202
        // }
203
    }
204

    
205
    private StatusLabel createFieldLabel(JDynFieldComponent component) {
206
        DynField field = component.getDynField();
207
        String text = i18nManager.getTranslation(field.getDescription());
208

    
209
        StatusLabel label = new StatusLabel(text, field.isMandatory());
210

    
211
        // adding label to a hashmap
212
        this.labelList.put(field, label);
213

    
214
        return label;
215
    }
216

    
217
    private Component createScrollPane(JPanel panel) throws ServiceException {
218
        JScrollPane scrollPane = new JScrollPane();
219
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
220
        scrollPane.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
221

    
222
        scrollPane.setViewportView(panel);
223
        scrollPane.setAutoscrolls(true);
224
        // // scrollPane.setBorder(null);
225
        // scrollPane
226
        // .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
227
        // scrollPane
228
        // .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
229
        return scrollPane;
230
    }
231

    
232
    /*
233
     * (non-Javadoc)
234
     * 
235
     * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
236
     */
237
    public Object getComponent() {
238
        return this.tabPanel;
239
        // return this.panel;
240
    }
241

    
242
    /**
243
     * OTRAS PRUEBAS DELAYOUT
244
     */
245

    
246
    // private GridBagConstraints getDefaultParametersConstraints() {
247
    // GridBagConstraints constr = new GridBagConstraints();
248
    // constr.insets = new Insets(2, 2, 2, 2);
249
    // constr.ipadx = 2;
250
    // constr.ipady = 2;
251
    // return constr;
252
    // }
253

    
254
    // private JPanel createNorthWestPane(JPanel pane){
255
    // //Make a panel
256
    // JPanel northWestPanel = new JPanel(new GridLayout(1,1));
257
    // GridBagConstraints c = new GridBagConstraints();
258
    // c.gridx = 0;
259
    // c.gridy = 0;
260
    // c.anchor = GridBagConstraints.NORTHWEST;
261
    // c.insets = new Insets(0,0,0,0);
262
    // northWestPanel.add(pane, c);
263
    //
264
    // northWestPanel.add(pane);
265
    // return northWestPanel;
266
    // }
267

    
268
    // /**
269
    // * @throws ServiceException
270
    // *
271
    // */
272
    // private void addGridBagParametersFromModel() throws ServiceException {
273
    //
274
    // DynField field;
275
    // JPanel pane;
276
    // String[] groups = this.getModel().getGroups();
277
    //
278
    // for (String group: groups){
279
    // List items = this.getModel().getGroupElements(group);
280
    //                
281
    // pane = new JPanel(new GridBagLayout());
282
    // pane.setBorder(BorderFactory.createTitledBorder("Prueba"));
283
    //                
284
    // for (int i=0;i<items.size();i++) {
285
    // field = ((DynFieldComponentModel)items.get(i))
286
    // .getDynField();
287
    // addGridBagFieldSingle(pane,field,i);
288
    // }
289
    // // this.tabPanel.addTab(group,createScrollPane(pane));
290
    // this.tabPanel.addTab(group,pane);
291
    // }
292
    //            
293
    // this.tabPanel.setSelectedIndex(0);
294
    // }
295

    
296
    // private void addFieldSingle(JPanel panel, DynField field, int row) throws
297
    // ServiceException {
298
    //            
299
    // JDynFieldComponent input = getJDynFieldComponent(field, this);
300
    //                
301
    // StatusLabel label = this.createFieldLabel(input);
302
    // input.addValueChangedListener(label);
303
    //                
304
    // // JTextField text = new JTextField();
305
    // //Component input = getComponentFor(field);
306
    // Component component = (Component) input.getComponent();
307
    // component.setName(field.getName());
308
    // addComponentToList(component,input);
309
    //                
310
    //                
311
    // //Arranging label and component into panel
312
    //                
313
    // GridBagConstraints constr = this.getDefaultParametersConstraints();
314
    //                
315
    // constr.fill = GridBagConstraints.HORIZONTAL;
316
    //                            
317
    // constr.gridx = 0;
318
    // // constr.gridy = row;
319
    // constr.gridheight = GridBagConstraints.RELATIVE;
320
    // constr.anchor = GridBagConstraints.PAGE_START;
321
    // constr.weighty = 0;
322
    // // constr.w = 1;
323
    // // constr.weightx = 0;
324
    // // constr.gridheight = GridBagConstraints.RELATIVE;
325
    // panel.add(label, BorderLayout.WEST);
326
    //
327
    //
328
    //                
329
    // panel.add(component, BorderLayout.CENTER);
330
    //                
331
    // input.fireValueChangedEvent();
332
    // }
333

    
334
    // private void initialize() throws ServiceException {
335
    // this.panel = new JPanel(new BorderLayout(6,6));
336
    // this.panel.setPreferredSize(new Dimension(800, 560));
337
    //                
338
    // // this.panel.add(this.getParametersScroll(), BorderLayout.PAGE_START);
339
    //
340
    // this.panel.add(this.getParametersPanel(isMainPanel),
341
    // BorderLayout.PAGE_START);
342
    // // this.panel = this.getParametersScroll();
343
    //
344
    // // this.panel.add(this.panParameters);
345
    // // if (this.showButtons) {
346
    // // this.add(this.getButtonsPanel(), BorderLayout.SOUTH);
347
    // // }
348
    // // this.fromParamsToUI();
349
    // }
350

    
351
    // private JPanel getButtonsPanel() {
352
    // if (this.panButtons == null) {
353
    // this.panButtons = new JPanel();
354
    // this.panButtons.setLayout(new GridBagLayout());
355
    // GridBagConstraints constr = new GridBagConstraints();
356
    // constr.anchor = GridBagConstraints.LAST_LINE_END;
357
    // constr.fill = GridBagConstraints.HORIZONTAL;
358
    // constr.weightx = 1;
359
    // constr.weighty = 0;
360
    // this.panButtons.add(new JLabel(), constr);
361
    //
362
    // const3r = this.getDefaultParametersConstraints();
363
    // constr33.fill = GridBagConstraints.NONE;
364
    // constr.weightx = 0;
365
    // constr.weighty = 0;
366
    //
367
    // this.panButtons.add(this.getAcceptButton(), constr);
368
    // this.panButtons.add(this.getCancelButton(), constr);
369
    // this.panButtons.add(this.getRestoreDefaults(), constr);
370
    // }
371
    // return this.panButtons;
372
    // }
373

    
374
    // private JButton getRestoreDefaults() {
375
    // if (this.botRestoreDefaults == null) {
376
    // this.botRestoreDefaults = usManager.createJButton(i18nManager
377
    // .getTranslation("restoreDefaults"));
378
    // this.botRestoreDefaults.addActionListener(this);
379
    // }
380
    // return this.botRestoreDefaults;
381
    // }
382
    //
383
    // private JButton getCancelButton() {
384
    // if (this.botCancel == null) {
385
    // this.botCancel = usManager.createJButton(i18nManager
386
    // .getTranslation("cancel"));
387
    // this.botCancel.addActionListener(this);
388
    // }
389
    // return this.botCancel;
390
    // }
391
    //
392
    // private JButton getAcceptButton() {
393
    // if (this.botAcept == null) {
394
    // this.botAcept = usManager.createJButton(i18nManager
395
    // .getTranslation("accept"));
396
    // this.botAcept.addActionListener(this);
397
    // }
398
    // return this.botAcept;
399
    // }
400

    
401
    // private void addGridLayoutComponent(JPanel panel, DynField field, int
402
    // row) throws ServiceException {
403
    //            
404
    // JDynFieldComponent input = getJDynFieldComponent(field, this);
405
    //                
406
    // StatusLabel label = this.createFieldLabel(input);
407
    // input.addValueChangedListener(label);
408
    //                
409
    // // JTextField text = new JTextField();
410
    // //Component input = getComponentFor(field);
411
    // Component component = (Component) input.getComponent();
412
    // component.setName(field.getName());
413
    // addComponentToList(component,input);
414
    //                
415
    //                
416
    // //Arranging label and component into panel
417
    //                
418
    // GridBagConstraints constr = this.getDefaultParametersConstraints();
419
    //                
420
    // // constr.fill = GridBagConstraints.HORIZONTAL;
421
    // //
422
    // // constr.gridx = 0;
423
    // //// constr.gridy = row;
424
    // // constr.gridheight = GridBagConstraints.RELATIVE;
425
    // // if (row==0){
426
    // // constr.anchor = GridBagConstraints.NORTHWEST;
427
    // // }else{
428
    // // constr.anchor = GridBagConstraints.PAGE_START;
429
    // // }
430
    // // constr.weighty = 0;
431
    // // constr.w = 1;
432
    // // constr.weightx = 0;
433
    // // constr.gridheight = GridBagConstraints.RELATIVE;
434
    // panel.add(label);
435
    //
436
    //
437
    // // constr.fill = GridBagConstraints.BOTH;
438
    // // if (row==0){
439
    // // constr.anchor = GridBagConstraints.NORTHWEST;
440
    // // }else{
441
    // // constr.anchor = GridBagConstraints.PAGE_START;
442
    // // }
443
    // // constr.gridx = 1;
444
    // // constr.gridwidth = GridBagConstraints.NONE;
445
    // // constr.weightx = 0.70;
446
    // panel.add(component);
447
    //                
448
    // input.fireValueChangedEvent();
449
    // }
450

    
451
    private JLabel getFieldLabel(DynField field) {
452
        return this.labelList.get(field);
453
    }
454

    
455
    // /* (non-Javadoc)
456
    // * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
457
    // */
458
    // public WindowInfo getWindowInfo() {
459
    // WindowInfo m_viewinfo;
460
    // if (this.modal) {
461
    // m_viewinfo = new WindowInfo(WindowInfo.MODALDIALOG
462
    // | WindowInfo.RESIZABLE);
463
    // } else {
464
    // m_viewinfo = new WindowInfo(WindowInfo.RESIZABLE);
465
    // }
466
    // m_viewinfo.setTitle(this.title);
467
    // m_viewinfo.setHeight(500);
468
    // m_viewinfo.setWidth(520);
469
    // return m_viewinfo;
470
    // }
471

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

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

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

    
502
    /**
503
     * @param field
504
     * @return
505
     */
506
    protected boolean isValid(JDynFieldComponent field) {
507
        return field.isValid();
508
    }
509

    
510
    /*
511
     * (non-Javadoc)
512
     * 
513
     * @see
514
     * org.gvsig.tools.swing.api.dynobject.JDynObjectComponent#requestFocus()
515
     */
516
    public void requestFocus() {
517
    }
518

    
519
    /**
520
     * @param mandatoryLabelColor
521
     *            the mandatoryLabelColor to set
522
     */
523
    public void setMandatoryLabelColor(Color mandatoryLabelColor) {
524
        this.mandatoryLabelColor = mandatoryLabelColor;
525
    }
526

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

    
544
}