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 / StatusLabel.java @ 649
History | View | Annotate | Download (8.47 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 |
|
31 |
/**
|
32 |
*
|
33 |
*/
|
34 |
package org.gvsig.tools.swing.impl.dynobject; |
35 |
|
36 |
import java.awt.Color; |
37 |
import java.awt.Component; |
38 |
import java.awt.Font; |
39 |
import java.awt.GridBagConstraints; |
40 |
import java.awt.GridBagLayout; |
41 |
import java.awt.Insets; |
42 |
import java.net.URL; |
43 |
|
44 |
import javax.swing.ImageIcon; |
45 |
import javax.swing.JComponent; |
46 |
import javax.swing.JLabel; |
47 |
import javax.swing.JPanel; |
48 |
import javax.swing.SwingConstants; |
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.swing.api.dynobject.ValueChangedListener; |
54 |
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent; |
55 |
|
56 |
/**
|
57 |
*
|
58 |
* JLabel with a "not validated" image, whose validation is validated and shown
|
59 |
* whenever the listener is triggered.
|
60 |
*
|
61 |
* @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
|
62 |
* @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n Reinhold</a>
|
63 |
* -
|
64 |
* gvSIG Team
|
65 |
* @version $Id$
|
66 |
*
|
67 |
*/
|
68 |
public class StatusLabel implements ValueChangedListener { |
69 |
|
70 |
/**
|
71 |
*
|
72 |
*/
|
73 |
private static final long serialVersionUID = -8404494423411589010L; |
74 |
|
75 |
private static final String NOT_VALIDATED = "unavailable.png"; |
76 |
|
77 |
private static final String VALIDATED = "transparent.png"; |
78 |
|
79 |
private static final Color COLOR_MANDATORY = Color.RED.darker(); |
80 |
|
81 |
private JLabel validationLabel; |
82 |
private JLabel validationMessage; |
83 |
|
84 |
private JPanel validationPanel; |
85 |
|
86 |
private JDynFieldComponent fieldComponent;
|
87 |
private JLabel fieldLabel; |
88 |
|
89 |
private DynField dynField;
|
90 |
|
91 |
public StatusLabel(JDynFieldComponent input, JLabel fieldLabel, |
92 |
DynField dynField) { |
93 |
this.fieldComponent = input;
|
94 |
this.fieldLabel = fieldLabel;
|
95 |
this.dynField = dynField;
|
96 |
if (dynField == null){ |
97 |
this.dynField = input.getDynField();
|
98 |
} |
99 |
|
100 |
initUI(); |
101 |
} |
102 |
|
103 |
private void initUI() { |
104 |
validationLabel = new JLabel(); |
105 |
validationLabel.setOpaque(true);
|
106 |
validationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
|
107 |
validationLabel.setIconTextGap(1);
|
108 |
validationLabel.setText(" ");
|
109 |
validationLabel.setVerticalAlignment(SwingConstants.TOP);
|
110 |
|
111 |
validationMessage = new JLabel(); |
112 |
validationLabel.setOpaque(true);
|
113 |
validationLabel.setText("");
|
114 |
validationLabel.setVerticalAlignment(SwingConstants.CENTER);
|
115 |
|
116 |
fieldLabel.setOpaque(true);
|
117 |
fieldLabel.setHorizontalTextPosition(SwingConstants.LEFT);
|
118 |
fieldLabel.setHorizontalAlignment(SwingConstants.LEFT);
|
119 |
fieldLabel.setVerticalAlignment(SwingConstants.TOP);
|
120 |
if (dynField.isMandatory()) {
|
121 |
fieldLabel.setForeground(COLOR_MANDATORY); |
122 |
fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
|
123 |
} |
124 |
} |
125 |
|
126 |
private ImageIcon getIcon(String name, String description) { |
127 |
URL iconurl = this.getClass().getResource(name); |
128 |
ImageIcon icon = null; |
129 |
if (iconurl == null) { |
130 |
icon = new ImageIcon(); |
131 |
} else {
|
132 |
icon = new ImageIcon(iconurl); |
133 |
} |
134 |
icon.setDescription(description); |
135 |
icon.setImageObserver(fieldLabel); |
136 |
return icon;
|
137 |
} |
138 |
|
139 |
public Component getValidationLabel() { |
140 |
return this.fieldLabel; |
141 |
} |
142 |
|
143 |
public Component getFieldComponent() { |
144 |
JComponent component = this.fieldComponent.asJComponent(); |
145 |
component.setName(dynField.getName()); |
146 |
return component;
|
147 |
} |
148 |
|
149 |
public void setValue(Object value) { |
150 |
this.fieldComponent.setValue(value);
|
151 |
} |
152 |
|
153 |
public Component getValidationPanel(boolean addScrollPane) { |
154 |
if (validationPanel == null) { |
155 |
validationPanel = new JPanel(new GridBagLayout()); |
156 |
validationPanel.setOpaque(true);
|
157 |
|
158 |
GridBagConstraints constr = getValidationPanelConstraints();
|
159 |
constr.weightx = 0.0;
|
160 |
constr.weighty = 0.0;
|
161 |
|
162 |
constr.gridx = 0;
|
163 |
constr.gridy = 1;
|
164 |
constr.fill = GridBagConstraints.NONE;
|
165 |
|
166 |
validationPanel.add(validationLabel, constr); |
167 |
|
168 |
constr.weightx = 1.0;
|
169 |
constr.weighty = 0.0;
|
170 |
constr.gridx = 0;
|
171 |
constr.gridy = 0;
|
172 |
if (this.dynField.getType() == DataTypes.LIST) { |
173 |
constr.gridwidth = 3;
|
174 |
constr.weighty = 1.0;
|
175 |
|
176 |
} else {
|
177 |
constr.gridwidth = 2;
|
178 |
} |
179 |
|
180 |
constr.gridwidth = 2;
|
181 |
// constr.fill = GridBagConstraints.RELATIVE;
|
182 |
constr.fill = GridBagConstraints.BOTH;
|
183 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
184 |
|
185 |
validationPanel.add(getFieldComponent(), constr); |
186 |
|
187 |
constr = new GridBagConstraints(); |
188 |
constr.insets = new Insets(1, 2, 2, 2); |
189 |
constr.gridx = 1;
|
190 |
constr.gridy = 1;
|
191 |
constr.ipadx = 5;
|
192 |
constr.ipady = 1;
|
193 |
constr.fill = GridBagConstraints.BOTH;
|
194 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
195 |
|
196 |
validationPanel.add(validationMessage, constr); |
197 |
} |
198 |
|
199 |
return validationPanel;
|
200 |
} |
201 |
|
202 |
private GridBagConstraints getValidationPanelConstraints() { |
203 |
GridBagConstraints constr = new GridBagConstraints(); |
204 |
constr.insets = new Insets(2, 2, 2, 2); |
205 |
constr.ipadx = 2;
|
206 |
constr.ipady = 2;
|
207 |
constr.fill = GridBagConstraints.BOTH;
|
208 |
constr.anchor = GridBagConstraints.NORTHWEST;
|
209 |
return constr;
|
210 |
} |
211 |
|
212 |
/*
|
213 |
* (non-Javadoc)
|
214 |
*
|
215 |
* @see
|
216 |
*
|
217 |
* org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
|
218 |
* (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
|
219 |
*/
|
220 |
public void handleValueChanged(JDynFieldComponent field) { |
221 |
|
222 |
// Set the icon and text. If icon was null, say so.
|
223 |
if (!field.isValid()) {
|
224 |
validationLabel.setIcon(getIcon(NOT_VALIDATED, "Not Validated"));
|
225 |
// validationLabel.setToolTipText(getErrorMessage(field));
|
226 |
fieldLabel.setToolTipText(getErrorMessage(field)); |
227 |
validationMessage.setText(getErrorMessage(field)); |
228 |
} else {
|
229 |
validationLabel.setIcon(getIcon(VALIDATED, "Validated"));
|
230 |
// validationLabel.setToolTipText(null);
|
231 |
fieldLabel.setToolTipText(null);
|
232 |
validationMessage.setText("");
|
233 |
} |
234 |
updateValidationMessageStatus(); |
235 |
} |
236 |
|
237 |
private String getErrorMessage(JDynFieldComponent field) { |
238 |
String msg = field.getValidationMessage();
|
239 |
|
240 |
if (msg == null || msg.equals("")) { |
241 |
if (field.isMandatory()) {
|
242 |
return translate("Mandatory field. Please insert value."); |
243 |
} |
244 |
return translate("Incompatible type. Not Validated"); |
245 |
} |
246 |
return msg.trim();
|
247 |
} |
248 |
|
249 |
private String translate(String message) { |
250 |
return ToolsLocator.getI18nManager().getTranslation(message);
|
251 |
} |
252 |
|
253 |
private void updateValidationMessageStatus() { |
254 |
if (validationMessage.getText().equals("")) { |
255 |
validationMessage.setVisible(false);
|
256 |
validationLabel.setVisible(false);
|
257 |
} else {
|
258 |
validationMessage.setVisible(true);
|
259 |
validationLabel.setVisible(true);
|
260 |
} |
261 |
} |
262 |
} |