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

History | View | Annotate | Download (14.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
 * 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.JLabel;
39
import javax.swing.JPanel;
40
import javax.swing.JScrollPane;
41
import javax.swing.JTabbedPane;
42

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

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

    
67
        private static final long serialVersionUID = 2888673056424008867L;
68

    
69
        private static final Logger LOG = LoggerFactory
70
                        .getLogger(DefaultJDynObjectComponent.class);
71

    
72
        private final I18nManager i18nManager = ToolsLocator.getI18nManager();
73

    
74
        private DynObject parameters;
75

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

    
80
        private JTabbedPane tabPanel;
81

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

    
92
        }
93
        
94
        private void addGridBagComponent(JPanel panel, DynField field, int row) throws ServiceException {
95
            
96
                    JDynFieldComponent input =  getJDynFieldComponent(field, this);
97
                
98
                StatusLabel label = this.createFieldLabel(input);
99
                input.addValueChangedListener(label);
100
                
101
                Component component = (Component) input.getComponent();
102
                component.setName(field.getName());        
103
                addComponentToList(component,input);
104
                        
105
                //Arranging label and component into panel
106
                
107
                GridBagConstraints constr = new GridBagConstraints();
108
                constr.insets = new Insets(2, 2, 2, 2);
109
                constr.weighty = row;
110
                constr.ipadx = 2;
111
                constr.ipady = 2;
112
                constr.fill = GridBagConstraints.HORIZONTAL;
113
                constr.anchor = GridBagConstraints.NORTHWEST;
114

    
115
                constr.gridx = 0;
116
                constr.gridy = row;
117
                panel.add(label, constr);
118

    
119

    
120
                constr.fill = GridBagConstraints.HORIZONTAL;
121
                constr.gridx = 1;
122
                constr.weightx = 0.7;
123
                panel.add(component, constr);
124
                
125
                input.fireValueChangedEvent();
126
        }
127

    
128
        private void addParametersFromModel() throws ServiceException {
129
            DynField field;
130
            JPanel pane;
131
            String[] groups = this.getModel().getGroups();
132

    
133
            for (String group: groups){
134
                List items = this.getModel().getGroupElements(group);
135
                
136
                pane = new JPanel(new GridBagLayout());
137
                
138
                    for (int i=0;i<items.size();i++) {
139
                    field = ((DynFieldComponentModel)items.get(i))
140
                                    .getDynField();
141
                    addGridBagComponent(pane,field,i);
142

    
143
                }
144
                this.tabPanel.addTab(group,createScrollPane(pane));
145
            }
146
        }
147

    
148
        /**
149
         * @param label
150
         * @param field
151
         */
152
        private void checkValidation(JLabel label, JDynFieldComponent field) {
153
            if (label==null) return;
154
            if (!isValid(field)){
155
                label.setForeground(this.mandatoryLabelColor);
156
            }else{
157
                label.setForeground(Color.BLACK);
158
            }
159
        }
160
        
161
        protected void closeWindow() {
162
                LOG.debug("Result DynObject: {}", parameters);
163

    
164
                // if (PluginServices.getMainFrame() == null) {
165
                // ((JFrame) (getParent().getParent().getParent().getParent()))
166
                // .dispose();
167
                // } else {
168
                // PluginServices.getMDIManager().closeWindow(this);
169
                // }
170
        }
171
        
172
        private StatusLabel createFieldLabel(JDynFieldComponent component) {
173
                    DynField field = component.getDynField();
174
                String text = i18nManager.getTranslation(field.getDescription());
175

    
176
                StatusLabel label = new StatusLabel(text, field.isMandatory());
177
        
178
                //adding label to a hashmap
179
                this.labelList.put (field,label);
180
                
181
                return label;
182
        }
183

    
184

    
185
        private Component createScrollPane(JPanel panel) throws ServiceException {
186
            JScrollPane scrollPane = new JScrollPane();
187
            scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);         
188
            scrollPane.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
189
           
190
            
191
            scrollPane.setViewportView(panel);
192
            scrollPane.setAutoscrolls(true);
193
////            scrollPane.setBorder(null);
194
//            scrollPane
195
//                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
196
//            scrollPane
197
//                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
198
            return scrollPane;
199
        }
200

    
201
/* (non-Javadoc)
202
 * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
203
 */
204
public Object getComponent() {
205
    return this.tabPanel;
206
//            return this.panel;
207
}
208

    
209
        /**
210
         *         OTRAS PRUEBAS DELAYOUT
211
         */
212
                
213
        //        private GridBagConstraints getDefaultParametersConstraints() {
214
        //        GridBagConstraints constr = new GridBagConstraints();
215
        //        constr.insets = new Insets(2, 2, 2, 2);
216
        //        constr.ipadx = 2;
217
        //        constr.ipady = 2;
218
        //        return constr;
219
        //}
220
        
221
        //        private JPanel createNorthWestPane(JPanel pane){
222
        //            //Make a panel
223
        //            JPanel northWestPanel = new JPanel(new GridLayout(1,1));
224
        //            GridBagConstraints c = new GridBagConstraints();
225
        //            c.gridx = 0;
226
        //            c.gridy = 0;
227
        //            c.anchor = GridBagConstraints.NORTHWEST;
228
        //            c.insets = new Insets(0,0,0,0);
229
        //            northWestPanel.add(pane, c);
230
        //
231
        //            northWestPanel.add(pane);
232
        //            return northWestPanel;
233
        //        }
234
                
235
        //        /**
236
        //         * @throws ServiceException 
237
        //         * 
238
        //         */
239
        //        private void addGridBagParametersFromModel() throws ServiceException {
240
        //
241
        //            DynField field;
242
        //            JPanel pane;
243
        //            String[] groups = this.getModel().getGroups();
244
        //
245
        //            for (String group: groups){
246
        //                List items = this.getModel().getGroupElements(group);
247
        //                
248
        //                pane = new JPanel(new GridBagLayout());
249
        //                    pane.setBorder(BorderFactory.createTitledBorder("Prueba"));
250
        //                
251
        //                    for (int i=0;i<items.size();i++) {
252
        //                    field = ((DynFieldComponentModel)items.get(i))
253
        //                                    .getDynField();
254
        //                    addGridBagFieldSingle(pane,field,i);
255
        //                }
256
        ////                this.tabPanel.addTab(group,createScrollPane(pane));
257
        //                this.tabPanel.addTab(group,pane);
258
        //            }
259
        //            
260
        //            this.tabPanel.setSelectedIndex(0);
261
        //        }
262
                
263
        //        private void addFieldSingle(JPanel panel, DynField field, int row) throws ServiceException {
264
        //            
265
        //                    JDynFieldComponent input =  getJDynFieldComponent(field, this);
266
        //                
267
        //                StatusLabel label = this.createFieldLabel(input);
268
        //                input.addValueChangedListener(label);
269
        //                
270
        //                // JTextField text = new JTextField();
271
        //                //Component input = getComponentFor(field);
272
        //                Component component = (Component) input.getComponent();
273
        //                component.setName(field.getName());        
274
        //                addComponentToList(component,input);
275
        //                
276
        //                
277
        //                //Arranging label and component into panel
278
        //                
279
        //                GridBagConstraints constr = this.getDefaultParametersConstraints();
280
        //                
281
        //                constr.fill = GridBagConstraints.HORIZONTAL;
282
        //                            
283
        //                constr.gridx = 0;
284
        ////                constr.gridy = row;
285
        //                constr.gridheight = GridBagConstraints.RELATIVE;
286
        //                constr.anchor = GridBagConstraints.PAGE_START;
287
        //                constr.weighty = 0;
288
        ////                constr.w =  1;
289
        ////                constr.weightx = 0;
290
        ////                constr.gridheight = GridBagConstraints.RELATIVE;
291
        //                panel.add(label, BorderLayout.WEST);
292
        //
293
        //
294
        //                
295
        //                panel.add(component, BorderLayout.CENTER);
296
        //                
297
        //                input.fireValueChangedEvent();
298
        //        }
299
        
300
        //        private void initialize() throws ServiceException {
301
        //                this.panel = new JPanel(new BorderLayout(6,6));
302
        //                this.panel.setPreferredSize(new Dimension(800, 560));
303
        //                
304
        ////                this.panel.add(this.getParametersScroll(), BorderLayout.PAGE_START);
305
        //
306
        //                this.panel.add(this.getParametersPanel(isMainPanel), BorderLayout.PAGE_START);
307
        ////                this.panel =  this.getParametersScroll();
308
        //
309
        //                //                this.panel.add(this.panParameters);
310
        //                // if (this.showButtons) {
311
        //                // this.add(this.getButtonsPanel(), BorderLayout.SOUTH);
312
        //                // }
313
        ////                this.fromParamsToUI();
314
        //        }
315
        
316
                // private JPanel getButtonsPanel() {
317
                // if (this.panButtons == null) {
318
                // this.panButtons = new JPanel();
319
                // this.panButtons.setLayout(new GridBagLayout());
320
                // GridBagConstraints constr = new GridBagConstraints();
321
                // constr.anchor = GridBagConstraints.LAST_LINE_END;
322
                // constr.fill = GridBagConstraints.HORIZONTAL;
323
                // constr.weightx = 1;
324
                // constr.weighty = 0;
325
                // this.panButtons.add(new JLabel(), constr);
326
                //
327
                // const3r = this.getDefaultParametersConstraints();
328
                // constr33.fill = GridBagConstraints.NONE;
329
                // constr.weightx = 0;
330
                // constr.weighty = 0;
331
                //
332
                // this.panButtons.add(this.getAcceptButton(), constr);
333
                // this.panButtons.add(this.getCancelButton(), constr);
334
                // this.panButtons.add(this.getRestoreDefaults(), constr);
335
                // }
336
                // return this.panButtons;
337
                // }
338
        
339
                // private JButton getRestoreDefaults() {
340
                // if (this.botRestoreDefaults == null) {
341
                // this.botRestoreDefaults = usManager.createJButton(i18nManager
342
                // .getTranslation("restoreDefaults"));
343
                // this.botRestoreDefaults.addActionListener(this);
344
                // }
345
                // return this.botRestoreDefaults;
346
                // }
347
                //
348
                // private JButton getCancelButton() {
349
                // if (this.botCancel == null) {
350
                // this.botCancel = usManager.createJButton(i18nManager
351
                // .getTranslation("cancel"));
352
                // this.botCancel.addActionListener(this);
353
                // }
354
                // return this.botCancel;
355
                // }
356
                //
357
                // private JButton getAcceptButton() {
358
                // if (this.botAcept == null) {
359
                // this.botAcept = usManager.createJButton(i18nManager
360
                // .getTranslation("accept"));
361
                // this.botAcept.addActionListener(this);
362
                // }
363
                // return this.botAcept;
364
                // }
365
        
366
        //        private void addGridLayoutComponent(JPanel panel, DynField field, int row) throws ServiceException {
367
        //            
368
        //                    JDynFieldComponent input =  getJDynFieldComponent(field, this);
369
        //                
370
        //                StatusLabel label = this.createFieldLabel(input);
371
        //                input.addValueChangedListener(label);
372
        //                
373
        //                // JTextField text = new JTextField();
374
        //                //Component input = getComponentFor(field);
375
        //                Component component = (Component) input.getComponent();
376
        //                component.setName(field.getName());        
377
        //                addComponentToList(component,input);
378
        //                
379
        //                
380
        //                //Arranging label and component into panel
381
        //                
382
        //                GridBagConstraints constr = this.getDefaultParametersConstraints();
383
        //                
384
        ////                constr.fill = GridBagConstraints.HORIZONTAL;
385
        ////                            
386
        ////                constr.gridx = 0;
387
        //////                constr.gridy = row;
388
        ////                constr.gridheight = GridBagConstraints.RELATIVE;
389
        ////                if (row==0){
390
        ////                    constr.anchor = GridBagConstraints.NORTHWEST;
391
        ////                }else{
392
        ////                    constr.anchor = GridBagConstraints.PAGE_START;
393
        ////                }
394
        ////                constr.weighty = 0;
395
        ////                constr.w =  1;
396
        ////                constr.weightx = 0;
397
        ////                constr.gridheight = GridBagConstraints.RELATIVE;
398
        //                panel.add(label);
399
        //
400
        //
401
        ////                constr.fill = GridBagConstraints.BOTH;
402
        ////                if (row==0){
403
        ////                    constr.anchor = GridBagConstraints.NORTHWEST;
404
        ////                }else{
405
        ////                    constr.anchor = GridBagConstraints.PAGE_START;
406
        ////                }
407
        ////                constr.gridx = 1;
408
        ////                constr.gridwidth = GridBagConstraints.NONE;
409
        ////                constr.weightx = 0.70;
410
        //                panel.add(component);
411
        //                
412
        //                input.fireValueChangedEvent();
413
        //        }
414
        
415
                
416
                private JLabel getFieldLabel (DynField field){
417
                    return this.labelList.get(field);
418
                }
419

    
420
        // /* (non-Javadoc)
421
        // * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
422
        // */
423
        // public WindowInfo getWindowInfo() {
424
        // WindowInfo m_viewinfo;
425
        // if (this.modal) {
426
        // m_viewinfo = new WindowInfo(WindowInfo.MODALDIALOG
427
        // | WindowInfo.RESIZABLE);
428
        // } else {
429
        // m_viewinfo = new WindowInfo(WindowInfo.RESIZABLE);
430
        // }
431
        // m_viewinfo.setTitle(this.title);
432
        // m_viewinfo.setHeight(500);
433
        // m_viewinfo.setWidth(520);
434
        // return m_viewinfo;
435
        // }
436

    
437
        /**
438
         * @return the mandatoryLabelColor
439
         */
440
        public Color getMandatoryLabelColor() {
441
                return mandatoryLabelColor;
442
        }
443

    
444
        /* (non-Javadoc)
445
         * @see org.gvsig.tools.swing.spi.AbstractDynObjectComponent#getValue()
446
         */
447
        @Override
448
        public Object getValue() {
449
            return this.getDynObject();
450
        }
451

    
452
        /* (non-Javadoc)
453
         * @see org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged(org.gvsig.tools.swing.api.dynobject.JDynObjectComponent, org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
454
         */
455
        public void handleValueChanged(JDynFieldComponent field) {
456
            JLabel label = getFieldLabel(field.getDynField());
457
            this.checkValidation(label, field);
458
        }
459

    
460
        /**
461
         * @param field
462
         * @return
463
         */
464
        protected boolean isValid(JDynFieldComponent field) {   
465
            return field.isValid();
466
        }
467

    
468

    
469
        /* (non-Javadoc)
470
         * @see org.gvsig.tools.swing.api.dynobject.JDynObjectComponent#requestFocus()
471
         */
472
        public void requestFocus() {
473
        }
474

    
475
        /**
476
         * @param mandatoryLabelColor
477
         *            the mandatoryLabelColor to set
478
         */
479
        public void setMandatoryLabelColor(Color mandatoryLabelColor) {
480
                this.mandatoryLabelColor = mandatoryLabelColor;
481
        }
482

    
483
        /* (non-Javadoc)
484
         * @see org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java.lang.Object)
485
         */
486
        public void setValue(Object value) {
487
           
488
        }
489

    
490
}