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 @ 539
History | View | Annotate | Download (11.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 |
* 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.List; |
38 |
import java.util.Map; |
39 |
|
40 |
import javax.swing.BorderFactory; |
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 String firstGroup;
|
94 |
|
95 |
public DefaultJDynObjectComponent(DynStruct definition, DynObject currentValue,
|
96 |
DynObjectModel model, int tabPosition) throws ServiceException { |
97 |
super();
|
98 |
this.setDynField(definition);
|
99 |
this.setModel(model);
|
100 |
setDynObject(currentValue); |
101 |
this.tabPosition = tabPosition;
|
102 |
} |
103 |
|
104 |
private void initUI(int tabPosition) { |
105 |
this.labelList = new HashMap<DynField, StatusLabel>(); |
106 |
if (this.tabPanel == null){ |
107 |
this.tabPanel = new JTabbedPane(); |
108 |
// Uncomment the following line to use scrolling tabs.
|
109 |
this.tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); |
110 |
} |
111 |
this.tabPanel.setTabPlacement(tabPosition);
|
112 |
} |
113 |
|
114 |
|
115 |
private void addGridBagComponent(JPanel panel, ValueField field, int row, |
116 |
int numItems) throws ServiceException { |
117 |
DynField dynField = field.getDynField(); |
118 |
input = getJDynFieldComponent(field, this);
|
119 |
input.setValue(field.getFieldValue()); |
120 |
|
121 |
// Setting label
|
122 |
String srcText = dynField.getName();
|
123 |
String text = i18nManager.getTranslation(srcText);
|
124 |
JLabel label = new JLabel(text); |
125 |
boolean isDynObject =
|
126 |
field.getDynField().getType() == DataTypes.DYNOBJECT; |
127 |
boolean isList =
|
128 |
field.getDynField().getType() == DataTypes.LIST; |
129 |
// Setting component's wrapper
|
130 |
StatusLabel right = |
131 |
new StatusLabel(input, label, dynField);
|
132 |
|
133 |
this.labelList.put(input.getDynField(), right);
|
134 |
input.addValueChangedListener(right); |
135 |
|
136 |
|
137 |
// Arranging label and component into panel
|
138 |
|
139 |
GridBagConstraints constr = new GridBagConstraints(); |
140 |
constr.anchor = GridBagConstraints.PAGE_START;
|
141 |
constr.insets = new Insets(5, 5, 5, 5); |
142 |
|
143 |
if (row >= numItems - 1) { |
144 |
constr.weighty = 1;
|
145 |
} |
146 |
int column = -1; |
147 |
|
148 |
if (isDynObject||isList){
|
149 |
// column += 1;
|
150 |
// constr.gridx = column;
|
151 |
// constr.ipady = 14;
|
152 |
// constr.fill = GridBagConstraints.NONE;
|
153 |
// constr.weightx = 0;
|
154 |
// constr.ipadx = 2;
|
155 |
// constr.ipady = 2;
|
156 |
|
157 |
column += 1;
|
158 |
constr.gridx = column; |
159 |
constr.gridy = row; |
160 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
161 |
constr.fill = GridBagConstraints.NONE;
|
162 |
panel.add(right.getValidationLabel(), constr); |
163 |
|
164 |
column += 1;
|
165 |
constr.gridx = column; |
166 |
constr.ipady = 2;
|
167 |
constr.weightx = 4;
|
168 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
169 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
170 |
constr.gridwidth = 2;
|
171 |
if (isDynObject){
|
172 |
constr.gridwidth = 4;
|
173 |
} |
174 |
panel.add(right.getValidationPanel(isDynObject), constr); |
175 |
// panel.add(new JScrollPane(input.asJComponent()), constr);
|
176 |
|
177 |
} else {
|
178 |
constr.weightx = 0;
|
179 |
constr.ipadx = 2;
|
180 |
constr.ipady = 2;
|
181 |
|
182 |
constr.gridy = row; |
183 |
|
184 |
if (!isDynObject) {
|
185 |
// column += 1;
|
186 |
constr.gridx = column; |
187 |
|
188 |
panel.add(label, constr); |
189 |
|
190 |
} |
191 |
|
192 |
column += 1;
|
193 |
constr.gridx = column; |
194 |
constr.ipady = 14;
|
195 |
constr.fill = GridBagConstraints.NONE;
|
196 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
197 |
panel.add(right.getValidationLabel(), constr); |
198 |
|
199 |
column += 1;
|
200 |
if (maxColumns > column) {
|
201 |
constr.gridwidth = maxColumns - column; |
202 |
} else {
|
203 |
maxColumns = column; |
204 |
} |
205 |
|
206 |
constr.gridx = column; |
207 |
constr.ipady = 2;
|
208 |
constr.weightx = 2;
|
209 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
210 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
211 |
panel.add(right.getValidationPanel(false), constr);
|
212 |
} |
213 |
|
214 |
Component component = right.getFieldComponent();
|
215 |
this.getSelectedComponents().addComponentToList(component, input);
|
216 |
|
217 |
input.fireValueChangedEvent(); |
218 |
} |
219 |
|
220 |
|
221 |
protected void closeWindow() { |
222 |
LOG.debug("Result DynObject: {}", this.getDynObject()); |
223 |
} |
224 |
|
225 |
// private StatusLabel createFieldLabel(JDynFieldComponent component) {
|
226 |
// DynField field = component.getDynField();
|
227 |
//
|
228 |
//
|
229 |
// StatusLabel label = new StatusLabel(component.asJComponent(),
|
230 |
// field.isMandatory());
|
231 |
//
|
232 |
// // adding label to a hashmap
|
233 |
// // this.labelList.put(field, label);
|
234 |
//
|
235 |
// return label;
|
236 |
// }
|
237 |
|
238 |
// private Component createScrollPane(JPanel panel) throws ServiceException
|
239 |
// {
|
240 |
// JScrollPane scrollPane = new JScrollPane();
|
241 |
// scrollPane.setViewportView(panel);
|
242 |
// scrollPane.setBorder(BorderFactory.createTitledBorder(""));
|
243 |
// // scrollPane.setBorder(BorderFactory.createEmptyBorder());
|
244 |
// return scrollPane;
|
245 |
// }
|
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
*
|
250 |
* @see org.gvsig.tools.swing.api.dynobject.JComponent#getComponent()
|
251 |
*/
|
252 |
public JComponent asJComponent() { |
253 |
try {
|
254 |
this.load(getDynObject());
|
255 |
} catch (ServiceException e) {
|
256 |
// TODO Auto-generated catch block
|
257 |
e.printStackTrace(); |
258 |
} |
259 |
if (tabPanel.getTabCount()==1){ |
260 |
this.tabPanel.removeAll();
|
261 |
// tabPanel.remove(0);
|
262 |
return firstPanel;
|
263 |
} |
264 |
return this.tabPanel; |
265 |
} |
266 |
private StatusLabel getFieldLabel(DynField field) {
|
267 |
return this.labelList.get(field); |
268 |
} |
269 |
|
270 |
protected void addParametersFromModel() throws ServiceException { |
271 |
DynField field; |
272 |
JPanel pane;
|
273 |
if (getDefinition()== null){ |
274 |
return;
|
275 |
} |
276 |
|
277 |
if (this.tabPanel == null){ |
278 |
initUI(tabPosition); |
279 |
} |
280 |
if (this.tabPanel.getTabCount()>0){ |
281 |
return;
|
282 |
} |
283 |
// for (int index = 0;index<this.tabPanel.getTabCount();index++){
|
284 |
// this.tabPanel.remove(index);
|
285 |
// }
|
286 |
// if (!this.hasDynObject()){
|
287 |
// return;
|
288 |
// }
|
289 |
|
290 |
|
291 |
String[] groups = this.getModel().getGroups(); |
292 |
|
293 |
|
294 |
for (String group : groups) { |
295 |
List<DynFieldModel> items = this.getModel().getGroupElements(group); |
296 |
|
297 |
pane = new JPanel(new GridBagLayout()); |
298 |
pane.setBorder(BorderFactory.createTitledBorder("rightPanel border: " + this.getDefinition().getName())); |
299 |
pane.setName(this.getDefinition().getName());
|
300 |
for (int i = 0; i < items.size(); i++) { |
301 |
field = items.get(i).getDynField(); |
302 |
addGridBagComponent( |
303 |
pane, |
304 |
new DynObjectValueField(this.getDefinition(), this.getDynObject(), field |
305 |
.getName()), i, items.size()); |
306 |
} |
307 |
// this.tabPanel.addTab(group, pane);
|
308 |
// JPanel borderPane = new JPanel(new BorderLayout());
|
309 |
// borderPane.add(pane, BorderLayout.WEST);
|
310 |
if (firstPanel == null){ |
311 |
firstPanel = pane; |
312 |
} |
313 |
this.tabPanel.addTab(group, new JScrollPane(pane)); |
314 |
} |
315 |
} |
316 |
|
317 |
/*
|
318 |
* (non-Javadoc)
|
319 |
*
|
320 |
* @see org.gvsig.tools.swing.spi.AbstractDynObjectComponent#getValue()
|
321 |
*/
|
322 |
public Object getValue() { |
323 |
return this.getDynObject(); |
324 |
} |
325 |
|
326 |
/*
|
327 |
* (non-Javadoc)
|
328 |
*
|
329 |
* @see
|
330 |
*
|
331 |
*
|
332 |
*
|
333 |
* org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
|
334 |
* (org.gvsig.tools.swing.api.dynobject.JDynObjectComponent,
|
335 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
|
336 |
*/
|
337 |
public void handleValueChanged(JDynFieldComponent field) { |
338 |
StatusLabel label = getFieldLabel(field.getDynField()); |
339 |
if (label != null) { |
340 |
label.handleValueChanged(field); |
341 |
} |
342 |
// this.tabPanel.updateUI();
|
343 |
// this.checkValidation(label, field);
|
344 |
} |
345 |
|
346 |
/*
|
347 |
* (non-Javadoc)
|
348 |
*
|
349 |
* @see
|
350 |
* org.gvsig.tools.swing.api.dynobject.JDynObjectComponent#requestFocus()
|
351 |
*/
|
352 |
public void requestFocus() { |
353 |
} |
354 |
|
355 |
/*
|
356 |
* (non-Javadoc)
|
357 |
*
|
358 |
* @see
|
359 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
|
360 |
* .lang.Object)
|
361 |
*/
|
362 |
public void setValue(Object value) { |
363 |
fillValues((DynObject) value); |
364 |
} |
365 |
|
366 |
public DynField getDynField() {
|
367 |
return input.getDynField();
|
368 |
} |
369 |
|
370 |
public Object getDefaultFieldValue() { |
371 |
return input.getDefaultFieldValue();
|
372 |
} |
373 |
|
374 |
public Object getFieldValue() { |
375 |
return input.getFieldValue();
|
376 |
} |
377 |
|
378 |
public void setFieldValue(Object value) { |
379 |
input.setFieldValue(value); |
380 |
} |
381 |
|
382 |
public DynField getParentDynField() {
|
383 |
// TODO Auto-generated method stub
|
384 |
return null; |
385 |
} |
386 |
|
387 |
} |