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 @ 632
History | View | Annotate | Download (12.6 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 |
* AUTHORS (In addition to CIT):
|
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
|
29 |
*/
|
30 |
package org.gvsig.tools.swing.impl.dynobject; |
31 |
|
32 |
import java.awt.Component; |
33 |
import java.awt.GridBagConstraints; |
34 |
import java.awt.GridBagLayout; |
35 |
import java.awt.Insets; |
36 |
import java.util.HashMap; |
37 |
import java.util.Iterator; |
38 |
import java.util.List; |
39 |
import java.util.Map; |
40 |
|
41 |
import javax.swing.JComponent; |
42 |
import javax.swing.JLabel; |
43 |
import javax.swing.JPanel; |
44 |
import javax.swing.JScrollPane; |
45 |
import javax.swing.JTabbedPane; |
46 |
|
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.dataTypes.DataTypes; |
52 |
import org.gvsig.tools.dynobject.DynField; |
53 |
import org.gvsig.tools.dynobject.DynObject; |
54 |
import org.gvsig.tools.dynobject.DynStruct; |
55 |
import org.gvsig.tools.i18n.I18nManager; |
56 |
import org.gvsig.tools.service.ServiceException; |
57 |
import org.gvsig.tools.swing.api.dynobject.DynFieldModel; |
58 |
import org.gvsig.tools.swing.api.dynobject.DynObjectModel; |
59 |
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent; |
60 |
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener; |
61 |
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
62 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
63 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynObjectValueField; |
64 |
import org.gvsig.tools.swing.spi.AbstractDynObjectComponent; |
65 |
|
66 |
/**
|
67 |
* Initial implementation based on a simple swing form generation.
|
68 |
*
|
69 |
* @author 2008-2009 Jose Manuel Viv�
|
70 |
* @author 2010- Cesar Ordinyana - gvSIG team
|
71 |
*/
|
72 |
public class DefaultJDynObjectComponent extends AbstractDynObjectComponent |
73 |
implements JDynObjectComponent, ValueField, ValueChangedListener {
|
74 |
|
75 |
private static final Logger LOG = LoggerFactory |
76 |
.getLogger(DefaultJDynObjectComponent.class); |
77 |
|
78 |
private final I18nManager i18nManager = ToolsLocator.getI18nManager(); |
79 |
|
80 |
protected Map<String, Object> tempValue = new HashMap<String, Object>(); |
81 |
private HashMap<DynField, StatusLabel> labelList; |
82 |
|
83 |
private JTabbedPane tabPanel; |
84 |
|
85 |
private JDynFieldComponent input;
|
86 |
|
87 |
private int maxColumns = 3; |
88 |
|
89 |
private int tabPosition; |
90 |
|
91 |
private JPanel firstPanel; |
92 |
|
93 |
private HashMap tabComponents; |
94 |
|
95 |
public DefaultJDynObjectComponent(DynStruct definition,
|
96 |
DynObject currentValue, DynObjectModel model, int tabPosition)
|
97 |
throws ServiceException {
|
98 |
super();
|
99 |
this.tabPosition = tabPosition;
|
100 |
this.setDynField(definition);
|
101 |
this.tabComponents = new HashMap(); |
102 |
this.setModel(model);
|
103 |
setValue(currentValue); |
104 |
} |
105 |
|
106 |
private StatusLabel addGridBagComponent(JPanel panel, ValueField field, |
107 |
int row, int numItems) throws ServiceException { |
108 |
DynField dynField = field.getDynField(); |
109 |
input = getJDynFieldComponent(field, this);
|
110 |
input.setValue(field.getFieldValue()); |
111 |
|
112 |
// Setting label
|
113 |
String srcText = dynField.getName();
|
114 |
String text = i18nManager.getTranslation(srcText);
|
115 |
JLabel label = new JLabel(text); |
116 |
boolean isDynObject = field.getDynField().getType() == DataTypes.DYNOBJECT;
|
117 |
boolean isList = field.getDynField().getType() == DataTypes.LIST;
|
118 |
|
119 |
// Setting component's wrapper
|
120 |
StatusLabel right = new StatusLabel(input, label, dynField);
|
121 |
|
122 |
this.getLabelList().put(input.getDynField(), right);
|
123 |
input.addValueChangedListener(right); |
124 |
|
125 |
// Arranging label and component into panel
|
126 |
GridBagConstraints constr = new GridBagConstraints(); |
127 |
constr.anchor = GridBagConstraints.PAGE_START;
|
128 |
constr.insets = new Insets(5, 5, 5, 5); |
129 |
|
130 |
if (row >= numItems - 1) { |
131 |
constr.weighty = 1;
|
132 |
} |
133 |
int column = -1; |
134 |
|
135 |
if (isDynObject || isList) {
|
136 |
if (numItems == 1) { |
137 |
column += 1;
|
138 |
constr.gridx = column + 1;
|
139 |
constr.ipady = 2;
|
140 |
constr.weightx = 4;
|
141 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
142 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
143 |
constr.gridwidth = 2;
|
144 |
if (isDynObject) {
|
145 |
constr.gridwidth = 4;
|
146 |
} |
147 |
panel.add(right.getValidationPanel(false), constr);
|
148 |
} else {
|
149 |
column += 1;
|
150 |
constr.gridx = column; |
151 |
constr.gridy = row; |
152 |
|
153 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
154 |
constr.fill = GridBagConstraints.NONE;
|
155 |
panel.add(right.getValidationLabel(), constr); |
156 |
|
157 |
column += 1;
|
158 |
constr.gridx = column; |
159 |
constr.ipady = 2;
|
160 |
constr.weightx = 4;
|
161 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
162 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
163 |
constr.gridwidth = 2;
|
164 |
if (isDynObject) {
|
165 |
constr.gridwidth = 4;
|
166 |
} |
167 |
panel.add(right.getValidationPanel(isDynObject), constr); |
168 |
} |
169 |
} else {
|
170 |
constr.weightx = 0;
|
171 |
constr.ipadx = 2;
|
172 |
constr.ipady = 2;
|
173 |
|
174 |
constr.gridy = row; |
175 |
|
176 |
if (!isDynObject) {
|
177 |
constr.gridx = column; |
178 |
panel.add(label, constr); |
179 |
} |
180 |
|
181 |
column += 1;
|
182 |
constr.gridx = column; |
183 |
constr.ipady = 14;
|
184 |
constr.fill = GridBagConstraints.NONE;
|
185 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
186 |
panel.add(right.getValidationLabel(), constr); |
187 |
|
188 |
column += 1;
|
189 |
if (maxColumns > column) {
|
190 |
constr.gridwidth = maxColumns - column; |
191 |
} else {
|
192 |
maxColumns = column; |
193 |
} |
194 |
|
195 |
constr.gridx = column; |
196 |
constr.ipady = 2;
|
197 |
constr.weightx = 2;
|
198 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
199 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
200 |
panel.add(right.getValidationPanel(false), constr);
|
201 |
} |
202 |
|
203 |
Component component = right.getFieldComponent();
|
204 |
this.getSelectedComponents().addComponentToList(component, input);
|
205 |
|
206 |
input.fireValueChangedEvent(); |
207 |
return right;
|
208 |
} |
209 |
|
210 |
private Map<DynField, StatusLabel> getLabelList() { |
211 |
if (this.labelList == null) { |
212 |
this.labelList = new HashMap<DynField, StatusLabel>(); |
213 |
} |
214 |
return this.labelList; |
215 |
} |
216 |
|
217 |
protected void closeWindow() { |
218 |
LOG.debug("Result DynObject: {}", this.getDynObject()); |
219 |
} |
220 |
|
221 |
/*
|
222 |
* (non-Javadoc)
|
223 |
*
|
224 |
* @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
|
225 |
*/
|
226 |
public JComponent asJComponent() { |
227 |
return getMainComponent();
|
228 |
} |
229 |
|
230 |
private JComponent getMainComponent() { |
231 |
if (this.tabPanel != null) { |
232 |
return this.tabPanel; |
233 |
} |
234 |
return this.firstPanel; |
235 |
} |
236 |
|
237 |
public int size() { |
238 |
return this.tabPanel.getTabCount(); |
239 |
} |
240 |
|
241 |
private StatusLabel getFieldLabel(DynField field) {
|
242 |
return this.getLabelList().get(field); |
243 |
} |
244 |
|
245 |
private void initTabComponent() throws ServiceException { |
246 |
if (this.tabPanel == null) { |
247 |
this.tabPanel = new JTabbedPane(); |
248 |
// this.tabPanel.setBorder(BorderFactory.createTitledBorder(this
|
249 |
// .getDefinition().getName()));
|
250 |
// Uncomment the following line to use scrolling tabs.
|
251 |
this.tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); |
252 |
} |
253 |
|
254 |
MyTab tab; |
255 |
JPanel pane;
|
256 |
for (String group : getGroups()) { |
257 |
tab = (MyTab) this.tabComponents.get(group);
|
258 |
if (tab == null) { |
259 |
pane = (MyTab) createPanel(group); |
260 |
this.getTabComponent().addTab(getGroupText(group),
|
261 |
new JScrollPane(pane)); |
262 |
tabComponents.put(group, pane); |
263 |
} else {
|
264 |
tab.load(this.getDynObject());
|
265 |
} |
266 |
} |
267 |
this.tabPanel.setTabPlacement(tabPosition);
|
268 |
} |
269 |
|
270 |
private String getGroupText(String groupName) { |
271 |
if (this.getModel().isGroupCompulsory(groupName)) { |
272 |
return groupName + "*"; |
273 |
} |
274 |
return groupName;
|
275 |
} |
276 |
|
277 |
private String[] getGroups() { |
278 |
return this.getModel().getGroups(); |
279 |
} |
280 |
|
281 |
private int getGroupsCount() { |
282 |
return getGroups().length;
|
283 |
} |
284 |
|
285 |
protected void addParametersFromModel() throws ServiceException { |
286 |
if ((getDefinition() == null) && (getGroupsCount() < 1)) { |
287 |
return;
|
288 |
} |
289 |
switch (getGroupsCount()) {
|
290 |
case 1: |
291 |
initSinglePanel(); |
292 |
break;
|
293 |
default:
|
294 |
initTabPanel(); |
295 |
break;
|
296 |
} |
297 |
} |
298 |
|
299 |
private JTabbedPane getTabComponent() throws ServiceException { |
300 |
if (this.tabPanel == null) { |
301 |
this.initTabComponent();
|
302 |
} |
303 |
return this.tabPanel; |
304 |
} |
305 |
|
306 |
private JPanel createPanel(String group) throws ServiceException { |
307 |
List<DynFieldModel> items = this.getModel().getGroupElements(group); |
308 |
DynField field = null;
|
309 |
|
310 |
JPanel pane = new MyTab(this.getDefinition(), group, items); |
311 |
return pane;
|
312 |
} |
313 |
|
314 |
private void initTabPanel() throws ServiceException { |
315 |
this.setDynObject(this.getDynObject()); |
316 |
initTabComponent(); |
317 |
} |
318 |
|
319 |
private void initSinglePanel() throws ServiceException { |
320 |
if (this.firstPanel == null) { |
321 |
this.firstPanel = createPanel(getGroups()[0]); |
322 |
// this.firstPanel.setBorder(BorderFactory.createTitledBorder(""));
|
323 |
} |
324 |
} |
325 |
|
326 |
/*
|
327 |
* (non-Javadoc)
|
328 |
*
|
329 |
* @see org.gvsig.tools.swing.spi.AbstractDynObjectComponent#getValue()
|
330 |
*/
|
331 |
public Object getValue() { |
332 |
return this.getDynObject(); |
333 |
} |
334 |
|
335 |
/*
|
336 |
* (non-Javadoc)
|
337 |
*
|
338 |
* @see
|
339 |
*
|
340 |
*
|
341 |
*
|
342 |
* org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
|
343 |
* (org.gvsig.tools.swing.api.dynobject.JDynObjectComponent,
|
344 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
|
345 |
*/
|
346 |
public void handleValueChanged(JDynFieldComponent field) { |
347 |
// StatusLabel label = getFieldLabel(field.getDynField());
|
348 |
// if (label != null) {
|
349 |
// label.handleValueChanged(field);
|
350 |
// }
|
351 |
String fieldName = field.getDynField().getName();
|
352 |
// DynField currentDynField =
|
353 |
// this.getDefinition().getDynField(fieldName);
|
354 |
if (hasDynField(fieldName)) {
|
355 |
|
356 |
if (field.isValid()
|
357 |
|| (field.getDynField().getType() == DataTypes.DYNOBJECT)) { |
358 |
this.getDynObject().setDynValue(fieldName, field.getValue());
|
359 |
} else {
|
360 |
this.getDynObject().setDynValue(fieldName, null); |
361 |
} |
362 |
} |
363 |
} |
364 |
|
365 |
private boolean hasDynField(String fieldName) { |
366 |
return this.getDefinition().getDynField(fieldName) != null; |
367 |
} |
368 |
|
369 |
/*
|
370 |
* (non-Javadoc)
|
371 |
*
|
372 |
* @see
|
373 |
* org.gvsig.tools.swing.api.dynobject.JDynObjectComponent#requestFocus()
|
374 |
*/
|
375 |
public void requestFocus() { |
376 |
} |
377 |
|
378 |
/*
|
379 |
* (non-Javadoc)
|
380 |
*
|
381 |
* @see
|
382 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
|
383 |
* .lang.Object)
|
384 |
*/
|
385 |
public void setValue(Object value) { |
386 |
fillValues((DynObject) value); |
387 |
} |
388 |
|
389 |
public DynField getDynField() {
|
390 |
return input.getDynField();
|
391 |
} |
392 |
|
393 |
public Object getDefaultFieldValue() { |
394 |
return input.getDefaultFieldValue();
|
395 |
} |
396 |
|
397 |
public Object getFieldValue() { |
398 |
return input.getFieldValue();
|
399 |
} |
400 |
|
401 |
public void setFieldValue(Object value) { |
402 |
input.setFieldValue(value); |
403 |
} |
404 |
|
405 |
public DynField getParentDynField() {
|
406 |
return this.getDynField(); |
407 |
} |
408 |
|
409 |
public class MyTab extends JPanel { |
410 |
|
411 |
private DynStruct definition;
|
412 |
private String groupName; |
413 |
private HashMap mapComponents; |
414 |
|
415 |
public MyTab(DynStruct definition, String group, |
416 |
List<DynFieldModel> items) throws ServiceException { |
417 |
super();
|
418 |
this.setLayout(new GridBagLayout()); |
419 |
this.definition = definition;
|
420 |
this.groupName = group;
|
421 |
initUI(items); |
422 |
|
423 |
} |
424 |
|
425 |
public void load(DynObject dynObject) { |
426 |
StatusLabel statusLabel; |
427 |
String fieldName;
|
428 |
Iterator iter = mapComponents.entrySet().iterator();
|
429 |
while (iter.hasNext()) {
|
430 |
Map.Entry keySet = (Map.Entry) iter.next(); |
431 |
fieldName = (String) keySet.getKey();
|
432 |
statusLabel = (StatusLabel) keySet.getValue(); |
433 |
statusLabel.setValue(dynObject.getDynValue(fieldName)); |
434 |
} |
435 |
|
436 |
} |
437 |
|
438 |
public boolean isGroup(String groupName) { |
439 |
if (groupName == null) { |
440 |
return false; |
441 |
} |
442 |
return groupName.equals(this.groupName); |
443 |
} |
444 |
|
445 |
public boolean hasField(DynField field) { |
446 |
return this.mapComponents.containsKey(field.getName()); |
447 |
} |
448 |
|
449 |
private void initUI(List<DynFieldModel> items) throws ServiceException { |
450 |
this.setName(this.definition.getName()); |
451 |
StatusLabel statusLabel; |
452 |
mapComponents = new HashMap(); |
453 |
DynField field = null;
|
454 |
for (int i = 0; i < items.size(); i++) { |
455 |
field = items.get(i).getDynField(); |
456 |
if (field.getType() != DataTypes.OBJECT) {
|
457 |
statusLabel = addGridBagComponent(this,
|
458 |
new DynObjectValueField(this.definition, |
459 |
getDynObject(), field.getName()), i, |
460 |
items.size()); |
461 |
mapComponents.put(field.getName(), statusLabel); |
462 |
} |
463 |
|
464 |
} |
465 |
} |
466 |
|
467 |
public void load(String fieldName, Object value) { |
468 |
StatusLabel statusLabel = (StatusLabel) this.mapComponents
|
469 |
.get(fieldName); |
470 |
if (statusLabel == null) { |
471 |
return;
|
472 |
} |
473 |
statusLabel.setValue(value); |
474 |
} |
475 |
} |
476 |
} |