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 @ 281
History | View | Annotate | Download (17.1 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.BorderFactory; |
39 |
import javax.swing.JComponent; |
40 |
import javax.swing.JLabel; |
41 |
import javax.swing.JPanel; |
42 |
import javax.swing.JScrollPane; |
43 |
import javax.swing.JTabbedPane; |
44 |
|
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.dynobject.DynField; |
50 |
import org.gvsig.tools.dynobject.DynObject; |
51 |
import org.gvsig.tools.i18n.I18nManager; |
52 |
import org.gvsig.tools.service.ServiceException; |
53 |
import org.gvsig.tools.swing.api.dynobject.DynFieldModel; |
54 |
import org.gvsig.tools.swing.api.dynobject.DynObjectModel; |
55 |
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent; |
56 |
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener; |
57 |
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
58 |
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField; |
59 |
import org.gvsig.tools.swing.impl.dynobject.valuefield.DynObjectValueField; |
60 |
import org.gvsig.tools.swing.spi.AbstractDynObjectComponent; |
61 |
|
62 |
/**
|
63 |
* Initial implementation based on a simple swing form generation.
|
64 |
*
|
65 |
* @author 2008-2009 Jos? Manuel Viv?
|
66 |
* @author 2010- C?sar Ordi?ana - gvSIG team
|
67 |
*/
|
68 |
public class DefaultJDynObjectComponent extends AbstractDynObjectComponent |
69 |
implements JDynObjectComponent, ValueField, ValueChangedListener {
|
70 |
|
71 |
// implements ActionListener {
|
72 |
|
73 |
private static final long serialVersionUID = 2888673056424008867L; |
74 |
|
75 |
private static final Logger LOG = |
76 |
LoggerFactory.getLogger(DefaultJDynObjectComponent.class); |
77 |
|
78 |
private final I18nManager i18nManager = ToolsLocator.getI18nManager(); |
79 |
|
80 |
private DynObject parameters;
|
81 |
|
82 |
private Color mandatoryLabelColor = Color.red; |
83 |
protected Map<String, Object> tempValue = new HashMap<String, Object>(); |
84 |
private HashMap<DynField, StatusLabel> labelList; |
85 |
|
86 |
private JTabbedPane tabPanel; |
87 |
|
88 |
private JDynFieldComponent input;
|
89 |
|
90 |
public DefaultJDynObjectComponent(DynObject parameters, DynObjectModel model)
|
91 |
throws ServiceException {
|
92 |
super(parameters, model);
|
93 |
// added to handle labels correctly
|
94 |
this.labelList = new HashMap<DynField, StatusLabel>(); |
95 |
this.parameters = parameters;
|
96 |
this.tabPanel = new JTabbedPane(); |
97 |
this.tabPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, |
98 |
0));
|
99 |
this.tabPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); |
100 |
|
101 |
// Uncomment the following line to use scrolling tabs.
|
102 |
this.tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); |
103 |
addParametersFromModel(); |
104 |
|
105 |
} |
106 |
|
107 |
private void addGridBagComponent(JPanel panel, ValueField field, int row, |
108 |
int numItems) throws ServiceException { |
109 |
|
110 |
input = getJDynFieldComponent(field, this);
|
111 |
|
112 |
StatusLabel label = this.createFieldLabel(input);
|
113 |
this.labelList.put(input.getDynField(), label);
|
114 |
|
115 |
Component component = (Component) input.asJComponent(); |
116 |
component.setName(field.getDynField().getName()); |
117 |
addComponentToList(component, input); |
118 |
|
119 |
// Arranging label and component into panel
|
120 |
|
121 |
GridBagConstraints constr = new GridBagConstraints(); |
122 |
constr.insets = new Insets(5, 5, 5, 5); |
123 |
if ((row == 0) && (numItems > 1)) { |
124 |
constr.weighty = 0;
|
125 |
// constr.weightx = 1;
|
126 |
} else
|
127 |
if ((row == 2)) |
128 |
constr.weighty = 1;
|
129 |
else {
|
130 |
constr.weighty = 1;
|
131 |
} |
132 |
// constr.weighty = 1;
|
133 |
// if (row % 2 == 0){
|
134 |
// constr.weighty = 1;
|
135 |
// }else
|
136 |
// constr.weighty = 0;
|
137 |
|
138 |
constr.weightx = 0;
|
139 |
constr.ipadx = 2;
|
140 |
constr.ipady = 2;
|
141 |
constr.fill = GridBagConstraints.RELATIVE;
|
142 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
143 |
|
144 |
// if ((row==0)&&(numItems>1)){
|
145 |
// constr.weighty = 1;
|
146 |
// // constr.weightx = 1;
|
147 |
// }
|
148 |
// else{
|
149 |
// constr.weighty = 0;
|
150 |
// }
|
151 |
|
152 |
constr.gridx = 0;
|
153 |
constr.gridy = row; |
154 |
|
155 |
panel.add(label, constr); |
156 |
|
157 |
constr.gridx = 1;
|
158 |
constr.ipady = 14;
|
159 |
constr.fill = GridBagConstraints.NONE;
|
160 |
panel.add(label.getValidationPanel(), constr); |
161 |
|
162 |
constr.gridx = 2;
|
163 |
constr.ipady = 2;
|
164 |
constr.weightx = 1;
|
165 |
constr.fill = GridBagConstraints.HORIZONTAL;
|
166 |
panel.add(component, constr); |
167 |
|
168 |
input.fireValueChangedEvent(); |
169 |
} |
170 |
|
171 |
private void addParametersFromModel() throws ServiceException { |
172 |
DynField field; |
173 |
JPanel pane;
|
174 |
String[] groups = this.getModel().getGroups(); |
175 |
|
176 |
for (String group : groups) { |
177 |
List items = this.getModel().getGroupElements(group); |
178 |
|
179 |
pane = new JPanel(new GridBagLayout()); |
180 |
pane.setBorder(BorderFactory.createEmptyBorder());
|
181 |
|
182 |
for (int i = 0; i < items.size(); i++) { |
183 |
field = ((DynFieldModel) items.get(i)).getDynField(); |
184 |
addGridBagComponent(pane, new DynObjectValueField(this.getDynObject(),field.getName()), i, items.size()); |
185 |
} |
186 |
this.tabPanel.addTab(group, createScrollPane(pane));
|
187 |
} |
188 |
} |
189 |
|
190 |
// /**
|
191 |
// * @param label
|
192 |
// * @param field
|
193 |
// */
|
194 |
// private void checkValidation(JLabel label, JDynFieldComponent field) {
|
195 |
// if (label == null)
|
196 |
// return;
|
197 |
// // if (!isValid(field)){
|
198 |
// // label.setForeground(this.mandatoryLabelColor);
|
199 |
// // }else{
|
200 |
// // label.setForeground(Color.BLACK);
|
201 |
// // }
|
202 |
// }
|
203 |
|
204 |
protected void closeWindow() { |
205 |
LOG.debug("Result DynObject: {}", parameters);
|
206 |
|
207 |
// if (PluginServices.getMainFrame() == null) {
|
208 |
// ((JFrame) (getParent().getParent().getParent().getParent()))
|
209 |
// .dispose();
|
210 |
// } else {
|
211 |
// PluginServices.getMDIManager().closeWindow(this);
|
212 |
// }
|
213 |
} |
214 |
|
215 |
private StatusLabel createFieldLabel(JDynFieldComponent component) {
|
216 |
DynField field = component.getDynField(); |
217 |
|
218 |
String srcText = field.getDescription();
|
219 |
srcText = |
220 |
(srcText == null || srcText.length() == 0 ? field.getName() |
221 |
: srcText); |
222 |
String text = i18nManager.getTranslation(srcText);
|
223 |
|
224 |
StatusLabel label = new StatusLabel(text, field.isMandatory());
|
225 |
|
226 |
// adding label to a hashmap
|
227 |
this.labelList.put(field, label);
|
228 |
|
229 |
return label;
|
230 |
} |
231 |
|
232 |
private Component createScrollPane(JPanel panel) throws ServiceException { |
233 |
JScrollPane scrollPane = new JScrollPane(); |
234 |
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
|
235 |
scrollPane.setAlignmentY(JScrollPane.TOP_ALIGNMENT);
|
236 |
|
237 |
scrollPane.setViewportView(panel); |
238 |
scrollPane.setAutoscrolls(true);
|
239 |
// // scrollPane.setBorder(null);
|
240 |
// scrollPane
|
241 |
// .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
|
242 |
// scrollPane
|
243 |
// .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
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 |
return this.tabPanel; |
254 |
// return this.panel;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* OTRAS PRUEBAS DELAYOUT
|
259 |
*/
|
260 |
|
261 |
// private GridBagConstraints getDefaultParametersConstraints() {
|
262 |
// GridBagConstraints constr = new GridBagConstraints();
|
263 |
// constr.insets = new Insets(2, 2, 2, 2);
|
264 |
// constr.ipadx = 2;
|
265 |
// constr.ipady = 2;
|
266 |
// return constr;
|
267 |
// }
|
268 |
|
269 |
// private JPanel createNorthWestPane(JPanel pane){
|
270 |
// //Make a panel
|
271 |
// JPanel northWestPanel = new JPanel(new GridLayout(1,1));
|
272 |
// GridBagConstraints c = new GridBagConstraints();
|
273 |
// c.gridx = 0;
|
274 |
// c.gridy = 0;
|
275 |
// c.anchor = GridBagConstraints.NORTHWEST;
|
276 |
// c.insets = new Insets(0,0,0,0);
|
277 |
// northWestPanel.add(pane, c);
|
278 |
//
|
279 |
// northWestPanel.add(pane);
|
280 |
// return northWestPanel;
|
281 |
// }
|
282 |
|
283 |
// /**
|
284 |
// * @throws ServiceException
|
285 |
// *
|
286 |
// */
|
287 |
// private void addGridBagParametersFromModel() throws ServiceException {
|
288 |
//
|
289 |
// DynField field;
|
290 |
// JPanel pane;
|
291 |
// String[] groups = this.getModel().getGroups();
|
292 |
//
|
293 |
// for (String group: groups){
|
294 |
// List items = this.getModel().getGroupElements(group);
|
295 |
//
|
296 |
// pane = new JPanel(new GridBagLayout());
|
297 |
// pane.setBorder(BorderFactory.createTitledBorder("Prueba"));
|
298 |
//
|
299 |
// for (int i=0;i<items.size();i++) {
|
300 |
// field = ((DynFieldModel)items.get(i))
|
301 |
// .getDynField();
|
302 |
// addGridBagFieldSingle(pane,field,i);
|
303 |
// }
|
304 |
// // this.tabPanel.addTab(group,createScrollPane(pane));
|
305 |
// this.tabPanel.addTab(group,pane);
|
306 |
// }
|
307 |
//
|
308 |
// this.tabPanel.setSelectedIndex(0);
|
309 |
// }
|
310 |
|
311 |
// private void addFieldSingle(JPanel panel, DynField field, int row) throws
|
312 |
// ServiceException {
|
313 |
//
|
314 |
// JDynFieldComponent input = getJDynFieldComponent(field, this);
|
315 |
//
|
316 |
// StatusLabel label = this.createFieldLabel(input);
|
317 |
// input.addValueChangedListener(label);
|
318 |
//
|
319 |
// // JTextField text = new JTextField();
|
320 |
// //Component input = getComponentFor(field);
|
321 |
// Component component = (Component) input.getComponent();
|
322 |
// component.setName(field.getName());
|
323 |
// addComponentToList(component,input);
|
324 |
//
|
325 |
//
|
326 |
// //Arranging label and component into panel
|
327 |
//
|
328 |
// GridBagConstraints constr = this.getDefaultParametersConstraints();
|
329 |
//
|
330 |
// constr.fill = GridBagConstraints.HORIZONTAL;
|
331 |
//
|
332 |
// constr.gridx = 0;
|
333 |
// // constr.gridy = row;
|
334 |
// constr.gridheight = GridBagConstraints.RELATIVE;
|
335 |
// constr.anchor = GridBagConstraints.PAGE_START;
|
336 |
// constr.weighty = 0;
|
337 |
// // constr.w = 1;
|
338 |
// // constr.weightx = 0;
|
339 |
// // constr.gridheight = GridBagConstraints.RELATIVE;
|
340 |
// panel.add(label, BorderLayout.WEST);
|
341 |
//
|
342 |
//
|
343 |
//
|
344 |
// panel.add(component, BorderLayout.CENTER);
|
345 |
//
|
346 |
// input.fireValueChangedEvent();
|
347 |
// }
|
348 |
|
349 |
// private void initialize() throws ServiceException {
|
350 |
// this.panel = new JPanel(new BorderLayout(6,6));
|
351 |
// this.panel.setPreferredSize(new Dimension(800, 560));
|
352 |
//
|
353 |
// // this.panel.add(this.getParametersScroll(), BorderLayout.PAGE_START);
|
354 |
//
|
355 |
// this.panel.add(this.getParametersPanel(isMainPanel),
|
356 |
// BorderLayout.PAGE_START);
|
357 |
// // this.panel = this.getParametersScroll();
|
358 |
//
|
359 |
// // this.panel.add(this.panParameters);
|
360 |
// // if (this.showButtons) {
|
361 |
// // this.add(this.getButtonsPanel(), BorderLayout.SOUTH);
|
362 |
// // }
|
363 |
// // this.fromParamsToUI();
|
364 |
// }
|
365 |
|
366 |
// private JPanel getButtonsPanel() {
|
367 |
// if (this.panButtons == null) {
|
368 |
// this.panButtons = new JPanel();
|
369 |
// this.panButtons.setLayout(new GridBagLayout());
|
370 |
// GridBagConstraints constr = new GridBagConstraints();
|
371 |
// constr.anchor = GridBagConstraints.LAST_LINE_END;
|
372 |
// constr.fill = GridBagConstraints.HORIZONTAL;
|
373 |
// constr.weightx = 1;
|
374 |
// constr.weighty = 0;
|
375 |
// this.panButtons.add(new JLabel(), constr);
|
376 |
//
|
377 |
// const3r = this.getDefaultParametersConstraints();
|
378 |
// constr33.fill = GridBagConstraints.NONE;
|
379 |
// constr.weightx = 0;
|
380 |
// constr.weighty = 0;
|
381 |
//
|
382 |
// this.panButtons.add(this.getAcceptButton(), constr);
|
383 |
// this.panButtons.add(this.getCancelButton(), constr);
|
384 |
// this.panButtons.add(this.getRestoreDefaults(), constr);
|
385 |
// }
|
386 |
// return this.panButtons;
|
387 |
// }
|
388 |
|
389 |
// private JButton getRestoreDefaults() {
|
390 |
// if (this.botRestoreDefaults == null) {
|
391 |
// this.botRestoreDefaults = usManager.createJButton(i18nManager
|
392 |
// .getTranslation("restoreDefaults"));
|
393 |
// this.botRestoreDefaults.addActionListener(this);
|
394 |
// }
|
395 |
// return this.botRestoreDefaults;
|
396 |
// }
|
397 |
//
|
398 |
// private JButton getCancelButton() {
|
399 |
// if (this.botCancel == null) {
|
400 |
// this.botCancel = usManager.createJButton(i18nManager
|
401 |
// .getTranslation("cancel"));
|
402 |
// this.botCancel.addActionListener(this);
|
403 |
// }
|
404 |
// return this.botCancel;
|
405 |
// }
|
406 |
//
|
407 |
// private JButton getAcceptButton() {
|
408 |
// if (this.botAcept == null) {
|
409 |
// this.botAcept = usManager.createJButton(i18nManager
|
410 |
// .getTranslation("accept"));
|
411 |
// this.botAcept.addActionListener(this);
|
412 |
// }
|
413 |
// return this.botAcept;
|
414 |
// }
|
415 |
|
416 |
// private void addGridLayoutComponent(JPanel panel, DynField field, int
|
417 |
// row) throws ServiceException {
|
418 |
//
|
419 |
// JDynFieldComponent input = getJDynFieldComponent(field, this);
|
420 |
//
|
421 |
// StatusLabel label = this.createFieldLabel(input);
|
422 |
// input.addValueChangedListener(label);
|
423 |
//
|
424 |
// // JTextField text = new JTextField();
|
425 |
// //Component input = getComponentFor(field);
|
426 |
// Component component = (Component) input.getComponent();
|
427 |
// component.setName(field.getName());
|
428 |
// addComponentToList(component,input);
|
429 |
//
|
430 |
//
|
431 |
// //Arranging label and component into panel
|
432 |
//
|
433 |
// GridBagConstraints constr = this.getDefaultParametersConstraints();
|
434 |
//
|
435 |
// // constr.fill = GridBagConstraints.HORIZONTAL;
|
436 |
// //
|
437 |
// // constr.gridx = 0;
|
438 |
// //// constr.gridy = row;
|
439 |
// // constr.gridheight = GridBagConstraints.RELATIVE;
|
440 |
// // if (row==0){
|
441 |
// // constr.anchor = GridBagConstraints.NORTHWEST;
|
442 |
// // }else{
|
443 |
// // constr.anchor = GridBagConstraints.PAGE_START;
|
444 |
// // }
|
445 |
// // constr.weighty = 0;
|
446 |
// // constr.w = 1;
|
447 |
// // constr.weightx = 0;
|
448 |
// // constr.gridheight = GridBagConstraints.RELATIVE;
|
449 |
// panel.add(label);
|
450 |
//
|
451 |
//
|
452 |
// // constr.fill = GridBagConstraints.BOTH;
|
453 |
// // if (row==0){
|
454 |
// // constr.anchor = GridBagConstraints.NORTHWEST;
|
455 |
// // }else{
|
456 |
// // constr.anchor = GridBagConstraints.PAGE_START;
|
457 |
// // }
|
458 |
// // constr.gridx = 1;
|
459 |
// // constr.gridwidth = GridBagConstraints.NONE;
|
460 |
// // constr.weightx = 0.70;
|
461 |
// panel.add(component);
|
462 |
//
|
463 |
// input.fireValueChangedEvent();
|
464 |
// }
|
465 |
|
466 |
private StatusLabel getFieldLabel(DynField field) {
|
467 |
return this.labelList.get(field); |
468 |
} |
469 |
|
470 |
/**
|
471 |
* @return the mandatoryLabelColor
|
472 |
*/
|
473 |
public Color getMandatoryLabelColor() { |
474 |
return mandatoryLabelColor;
|
475 |
} |
476 |
|
477 |
/*
|
478 |
* (non-Javadoc)
|
479 |
*
|
480 |
* @see org.gvsig.tools.swing.spi.AbstractDynObjectComponent#getValue()
|
481 |
*/
|
482 |
public Object getValue() { |
483 |
return this.getDynObject(); |
484 |
} |
485 |
|
486 |
/*
|
487 |
* (non-Javadoc)
|
488 |
*
|
489 |
* @see
|
490 |
*
|
491 |
* org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
|
492 |
* (org.gvsig.tools.swing.api.dynobject.JDynObjectComponent,
|
493 |
* org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
|
494 |
*/
|
495 |
public void handleValueChanged(JDynFieldComponent field) { |
496 |
StatusLabel label = getFieldLabel(field.getDynField()); |
497 |
if (label != null){ |
498 |
label.handleValueChanged(field); |
499 |
} |
500 |
// this.checkValidation(label, field);
|
501 |
} |
502 |
|
503 |
/*
|
504 |
* (non-Javadoc)
|
505 |
*
|
506 |
* @see
|
507 |
* org.gvsig.tools.swing.api.dynobject.JDynObjectComponent#requestFocus()
|
508 |
*/
|
509 |
public void requestFocus() { |
510 |
} |
511 |
|
512 |
/**
|
513 |
* @param mandatoryLabelColor
|
514 |
* the mandatoryLabelColor to set
|
515 |
*/
|
516 |
public void setMandatoryLabelColor(Color mandatoryLabelColor) { |
517 |
this.mandatoryLabelColor = mandatoryLabelColor;
|
518 |
} |
519 |
|
520 |
/*
|
521 |
* (non-Javadoc)
|
522 |
*
|
523 |
* @see
|
524 |
* org.gvsig.tools.swing.api.dynobject.dynfield.ValueField#setValue(java
|
525 |
* .lang.Object)
|
526 |
*/
|
527 |
public void setValue(Object value) { |
528 |
if (value != null) { |
529 |
if (value instanceof DynObject) { |
530 |
fillValues((DynObject) value); |
531 |
} |
532 |
return;
|
533 |
} |
534 |
this.emptyAll();
|
535 |
} |
536 |
|
537 |
public DynField getDynField() {
|
538 |
return input.getDynField();
|
539 |
} |
540 |
|
541 |
public Object getDefaultFieldValue() { |
542 |
return input.getDefaultFieldValue();
|
543 |
} |
544 |
|
545 |
public Object getFieldValue() { |
546 |
return input.getFieldValue();
|
547 |
} |
548 |
|
549 |
public void setFieldValue(Object value) { |
550 |
input.setFieldValue(value); |
551 |
} |
552 |
|
553 |
} |