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