Revision 497 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

View differences:

DefaultJDynObjectComponent.java
19 19
 * MA  02110-1301, USA.
20 20
 * 
21 21
 */
22

  
23 22
/*
24 23
 * AUTHORS (In addition to CIT):
25
 * 2010 {}  {{Task}}
24
 * 2010 Institute of New Imaging Technologies (INIT): 
25
 *   http://www.init.uji.es
26
 * Geographic Information research group: 
27
 *   http://www.geoinfo.uji.es
28
 * Universitat Jaume I, Spain
26 29
 */
27 30
package org.gvsig.tools.swing.impl.dynobject;
28 31

  
......
34 37
import java.util.List;
35 38
import java.util.Map;
36 39

  
37
import javax.swing.BorderFactory;
38 40
import javax.swing.JComponent;
41
import javax.swing.JLabel;
39 42
import javax.swing.JPanel;
40 43
import javax.swing.JScrollPane;
41 44
import javax.swing.JTabbedPane;
......
67 70
public class DefaultJDynObjectComponent extends AbstractDynObjectComponent
68 71
    implements JDynObjectComponent, ValueField, ValueChangedListener {
69 72

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

  
73 76
    private final I18nManager i18nManager = ToolsLocator.getI18nManager();
74 77

  
75
    private DynObject parameters;
76

  
77 78
    protected Map<String, Object> tempValue = new HashMap<String, Object>();
78 79
    private HashMap<DynField, StatusLabel> labelList;
79 80

  
......
81 82

  
82 83
    private JDynFieldComponent input;
83 84

  
84
    public DefaultJDynObjectComponent(DynObject parameters, DynObjectModel model)
85
        throws ServiceException {
86
        super(parameters, model);
87
        // added to handle labels correctly
85
    private int maxColumns = 3;
86

  
87
    private int tabPosition;
88

  
89
    public DefaultJDynObjectComponent(DynObject dynObject,
90
        DynObjectModel model, int tabPosition) throws ServiceException {
91
        super();
92
        this.setModel(model);
93
        setDynObject(dynObject);
94
        this.tabPosition = tabPosition;
95
    }
96

  
97
    private void initUI(int tabPosition) {
88 98
        this.labelList = new HashMap<DynField, StatusLabel>();
89
        this.parameters = parameters;
90 99
        this.tabPanel = new JTabbedPane();
91 100

  
92 101
        // Uncomment the following line to use scrolling tabs.
93 102
        this.tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
94
        addParametersFromModel();
95

  
103
        this.tabPanel.setTabPlacement(tabPosition);
96 104
    }
97 105

  
106

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

  
109
        DynField dynField = field.getDynField();
101 110
        input = getJDynFieldComponent(field, this);
111
        input.setValue(field.getFieldValue());
102 112

  
103
        StatusLabel label = this.createFieldLabel(input);
104
        this.labelList.put(input.getDynField(), label);
105
        input.addValueChangedListener(label);
113
        // Setting label
114
        String srcText = dynField.getName();
115
        String text = i18nManager.getTranslation(srcText);
116
        JLabel label = new JLabel(text);
117
        boolean isDynObject =
118
            field.getDynField().getType() == DataTypes.DYNOBJECT;
119
        // Setting component's wrapper
120
        StatusLabel right =
121
            new StatusLabel(input, label, dynField);
106 122

  
107
        Component component = input.asJComponent();
108
        component.setName(field.getDynField().getName());
109
        addComponentToList(component, input);
123
        this.labelList.put(input.getDynField(), right);
124
        input.addValueChangedListener(right);
110 125

  
126

  
111 127
        // Arranging label and component into panel
112 128

  
113 129
        GridBagConstraints constr = new GridBagConstraints();
......
117 133
        if (row >= numItems - 1) {
118 134
            constr.weighty = 1;
119 135
        }
136
        int column = -1;
120 137

  
121 138
        if (input.getDynField().getType() == DataTypes.DYNOBJECT) {
122
            constr.gridx = 1;
139
            column += 1;
140
            constr.gridx = column;
123 141
            constr.ipady = 14;
124 142
            constr.fill = GridBagConstraints.NONE;
125 143
            constr.weightx = 0;
126 144
            constr.ipadx = 2;
127 145
            constr.ipady = 2;
128 146

  
129
            constr.gridx = 0;
147
            column += 1;
148
            constr.gridx = column;
130 149
            constr.gridy = row;
131
            panel.add(label.getValidationPanel(), constr);
150
            constr.anchor = GridBagConstraints.NORTHWEST;
151
            panel.add(right.getValidationLabel(), constr);
132 152

  
133
            constr.gridx = 2;
153
            column += 1;
154
            constr.gridx = column;
134 155
            constr.ipady = 2;
135 156
            constr.weightx = 1;
136 157
            constr.fill = GridBagConstraints.HORIZONTAL;
137
            panel.add(component, constr);
158
            panel.add(right.getValidationPanel(), constr);
159
            // panel.add(new JScrollPane(input.asJComponent()), constr);
138 160

  
139 161
        } else {
140 162
            constr.weightx = 0;
141 163
            constr.ipadx = 2;
142 164
            constr.ipady = 2;
143 165

  
144
            constr.gridx = 0;
145 166
            constr.gridy = row;
146 167

  
147
            panel.add(label, constr);
168
            if (!isDynObject) {
169
                column += 1;
170
                constr.gridx = column;
148 171

  
149
            constr.gridx = 1;
172
                panel.add(label, constr);
173

  
174
            }
175

  
176
            column += 1;
177
            constr.gridx = column;
150 178
            constr.ipady = 14;
151 179
            constr.fill = GridBagConstraints.NONE;
152
            panel.add(label.getValidationPanel(), constr);
180
            constr.anchor = GridBagConstraints.NORTHWEST;
181
            panel.add(right.getValidationLabel(), constr);
153 182

  
154
            constr.gridx = 2;
183
            column += 1;
184
            if (maxColumns > column) {
185
                constr.gridwidth = maxColumns - column;
186
            } else {
187
                maxColumns = column;
188
            }
189

  
190
            constr.gridx = column;
155 191
            constr.ipady = 2;
156 192
            constr.weightx = 1;
157 193
            constr.fill = GridBagConstraints.HORIZONTAL;
158
            panel.add(component, constr);
194
            constr.anchor = GridBagConstraints.NORTHWEST;
195
            panel.add(right.getValidationPanel(), constr);
159 196
        }
197

  
198
        Component component = right.getFieldComponent();
199
        addComponentToList(component, input);
200
        
160 201
        input.fireValueChangedEvent();
161 202
    }
162 203

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

  
168
        for (String group : groups) {
169
            List<DynFieldModel> items = this.getModel().getGroupElements(group);
170

  
171
            pane = new JPanel(new GridBagLayout());
172

  
173
            for (int i = 0; i < items.size(); i++) {
174
                field = items.get(i).getDynField();
175
                addGridBagComponent(pane, new DynObjectValueField(this
176
                    .getDynObject(), field.getName()), i, items.size());
177
            }
178
            this.tabPanel.addTab(group, createScrollPane(pane));
179
        }
180
    }
181

  
204
  
182 205
    protected void closeWindow() {
183
        LOG.debug("Result DynObject: {}", parameters);
206
        LOG.debug("Result DynObject: {}", this.getDynObject());
184 207
    }
185 208

  
186
    private StatusLabel createFieldLabel(JDynFieldComponent component) {
187
        DynField field = component.getDynField();
209
    // private StatusLabel createFieldLabel(JDynFieldComponent component) {
210
    // DynField field = component.getDynField();
211
    //
212
    //
213
    // StatusLabel label = new StatusLabel(component.asJComponent(),
214
    // field.isMandatory());
215
    //
216
    // // adding label to a hashmap
217
    // // this.labelList.put(field, label);
218
    //
219
    // return label;
220
    // }
188 221

  
189
        String srcText = field.getName();
190
        String text = i18nManager.getTranslation(srcText);
222
    // private Component createScrollPane(JPanel panel) throws ServiceException
223
    // {
224
    // JScrollPane scrollPane = new JScrollPane();
225
    // scrollPane.setViewportView(panel);
226
    // scrollPane.setBorder(BorderFactory.createTitledBorder(""));
227
    // // scrollPane.setBorder(BorderFactory.createEmptyBorder());
228
    // return scrollPane;
229
    // }
191 230

  
192
        StatusLabel label = new StatusLabel(text, field.isMandatory());
193

  
194
        // adding label to a hashmap
195
        this.labelList.put(field, label);
196

  
197
        return label;
198
    }
199

  
200
    private Component createScrollPane(JPanel panel) throws ServiceException {
201
        JScrollPane scrollPane = new JScrollPane(panel);
202
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
203
        return scrollPane;
204
    }
205

  
206 231
    /*
207 232
     * (non-Javadoc)
208 233
     * 
209 234
     * @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
210 235
     */
211 236
    public JComponent asJComponent() {
237
        try {
238

  
239
            initUI(tabPosition);
240
            this.load(getDynObject());
241
        } catch (ServiceException e) {
242
            // TODO Auto-generated catch block
243
            e.printStackTrace();
244
        }
212 245
        return this.tabPanel;
213 246
    }
214 247

  
......
216 249
        return this.labelList.get(field);
217 250
    }
218 251

  
252
    protected void addParametersFromModel() throws ServiceException {
253
        DynField field;
254
        JPanel pane;
255
        if (!this.hasDynObject()){
256
            for (int index = 0;index<this.tabPanel.getTabCount();index++){
257
                this.tabPanel.remove(index);                
258
            }
259
            return;
260
        }
261
        
262
        String[] groups = this.getModel().getGroups();
263
        
264
        
265
        for (String group : groups) {
266
            List<DynFieldModel> items = this.getModel().getGroupElements(group);
267

  
268
            pane = new JPanel(new GridBagLayout());
269

  
270
            pane.setName(this.getDynObject().getDynClass().getName());
271
            for (int i = 0; i < items.size(); i++) {
272
                field = items.get(i).getDynField();
273
                addGridBagComponent(
274
                    pane,
275
                    new DynObjectValueField(this.getDynObject(), field
276
                        .getName()), i, items.size());
277
            }
278
            // this.tabPanel.addTab(group, pane);
279
            // JPanel borderPane = new JPanel(new BorderLayout());
280
            // borderPane.add(pane, BorderLayout.WEST);
281
            this.tabPanel.addTab(group, new JScrollPane(pane));
282
        }
283
    }
284

  
219 285
    /*
220 286
     * (non-Javadoc)
221 287
     * 
......
241 307
        if (label != null) {
242 308
            label.handleValueChanged(field);
243 309
        }
310
//        this.tabPanel.updateUI();
244 311
        // this.checkValidation(label, field);
245 312
    }
246 313

  
......
285 352
    public void setFieldValue(Object value) {
286 353
        input.setFieldValue(value);
287 354
    }
288

  
355
    
289 356
}

Also available in: Unified diff