Statistics
| Revision:

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 168 cmartin
/* 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 180 cmartin
import java.awt.Component;
38 334 fdiaz
import java.awt.Font;
39 497 cmartin
import java.awt.GridBagConstraints;
40
import java.awt.GridBagLayout;
41
import java.awt.Insets;
42
import java.net.URL;
43 168 cmartin
44
import javax.swing.ImageIcon;
45 497 cmartin
import javax.swing.JComponent;
46 168 cmartin
import javax.swing.JLabel;
47 281 cmartin
import javax.swing.JPanel;
48 298 cmartin
import javax.swing.SwingConstants;
49 168 cmartin
50 497 cmartin
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.dataTypes.DataTypes;
52
import org.gvsig.tools.dynobject.DynField;
53 168 cmartin
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
54
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
55
56
/**
57
 *
58 298 cmartin
 * JLabel with a "not validated" image, whose validation is validated and shown
59
 * whenever the listener is triggered.
60 281 cmartin
 *
61 497 cmartin
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
62 539 cmartin
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n Reinhold</a>
63
 *         -
64 298 cmartin
 *         gvSIG Team
65 281 cmartin
 * @version $Id$
66 298 cmartin
 *
67 168 cmartin
 */
68 497 cmartin
public class StatusLabel implements ValueChangedListener {
69 168 cmartin
70 298 cmartin
    /**
71
     *
72
     */
73
    private static final long serialVersionUID = -8404494423411589010L;
74
75 168 cmartin
    private static final String NOT_VALIDATED = "unavailable.png";
76 270 cmartin
77 497 cmartin
    private static final String VALIDATED = "transparent.png";
78
79 334 fdiaz
    private static final Color COLOR_MANDATORY = Color.RED.darker();
80 577 cmartin
81 180 cmartin
    private JLabel validationLabel;
82 497 cmartin
    private JLabel validationMessage;
83 168 cmartin
84 298 cmartin
    private JPanel validationPanel;
85 281 cmartin
86 497 cmartin
    private JDynFieldComponent fieldComponent;
87
    private JLabel fieldLabel;
88
89
    private DynField dynField;
90
91 539 cmartin
    public StatusLabel(JDynFieldComponent input, JLabel fieldLabel,
92
        DynField dynField) {
93 497 cmartin
        this.fieldComponent = input;
94
        this.fieldLabel = fieldLabel;
95
        this.dynField = dynField;
96 539 cmartin
        if (dynField == null){
97
            this.dynField = input.getDynField();
98
        }
99
100
        initUI();
101 497 cmartin
    }
102
103
    private void initUI() {
104 270 cmartin
        validationLabel = new JLabel();
105
        validationLabel.setOpaque(true);
106 497 cmartin
        validationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
107
        validationLabel.setIconTextGap(1);
108
        validationLabel.setText(" ");
109
        validationLabel.setVerticalAlignment(SwingConstants.TOP);
110 539 cmartin
111 497 cmartin
        validationMessage = new JLabel();
112
        validationLabel.setOpaque(true);
113
        validationLabel.setText("");
114
        validationLabel.setVerticalAlignment(SwingConstants.CENTER);
115 539 cmartin
116 497 cmartin
        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 298 cmartin
        }
124 168 cmartin
    }
125
126 584 cmartin
        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 168 cmartin
139 298 cmartin
    public Component getValidationLabel() {
140 497 cmartin
        return this.fieldLabel;
141 298 cmartin
    }
142 539 cmartin
143
    public Component getFieldComponent() {
144 497 cmartin
        JComponent component = this.fieldComponent.asJComponent();
145
        component.setName(dynField.getName());
146
        return component;
147
    }
148 539 cmartin
149 577 cmartin
        public void setValue(Object value) {
150
                this.fieldComponent.setValue(value);
151
        }
152
153 539 cmartin
    public Component getValidationPanel(boolean addScrollPane) {
154 298 cmartin
        if (validationPanel == null) {
155 497 cmartin
            validationPanel = new JPanel(new GridBagLayout());
156 539 cmartin
            validationPanel.setOpaque(true);
157 497 cmartin
158 539 cmartin
            GridBagConstraints constr = getValidationPanelConstraints();
159
            constr.weightx = 0.0;
160
            constr.weighty = 0.0;
161 497 cmartin
162 539 cmartin
            constr.gridx = 0;
163
            constr.gridy = 1;
164
            constr.fill = GridBagConstraints.NONE;
165
166
            validationPanel.add(validationLabel, constr);
167 497 cmartin
168 539 cmartin
            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 548 cmartin
                constr.weighty = 1.0;
175 497 cmartin
176 539 cmartin
            } else {
177
                constr.gridwidth = 2;
178
            }
179 497 cmartin
180 539 cmartin
            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 497 cmartin
187 539 cmartin
            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 298 cmartin
        }
198 539 cmartin
199 548 cmartin
        return validationPanel;
200 298 cmartin
    }
201
202 497 cmartin
    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 539 cmartin
        constr.fill = GridBagConstraints.BOTH;
208 497 cmartin
        constr.anchor = GridBagConstraints.NORTHWEST;
209 539 cmartin
        return constr;
210
    }
211 497 cmartin
212 270 cmartin
    /*
213
     * (non-Javadoc)
214
     *
215
     * @see
216 298 cmartin
     *
217 270 cmartin
     * org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
218
     * (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
219 168 cmartin
     */
220
    public void handleValueChanged(JDynFieldComponent field) {
221
222 270 cmartin
        // Set the icon and text. If icon was null, say so.
223
        if (!field.isValid()) {
224
            validationLabel.setIcon(getIcon(NOT_VALIDATED, "Not Validated"));
225 649 cmartin
            // validationLabel.setToolTipText(getErrorMessage(field));
226
            fieldLabel.setToolTipText(getErrorMessage(field));
227 497 cmartin
            validationMessage.setText(getErrorMessage(field));
228 270 cmartin
        } else {
229 497 cmartin
            validationLabel.setIcon(getIcon(VALIDATED, "Validated"));
230 649 cmartin
            // validationLabel.setToolTipText(null);
231
            fieldLabel.setToolTipText(null);
232 497 cmartin
            validationMessage.setText("");
233 270 cmartin
        }
234 497 cmartin
        updateValidationMessageStatus();
235 168 cmartin
    }
236 497 cmartin
237
    private String getErrorMessage(JDynFieldComponent field) {
238
        String msg = field.getValidationMessage();
239 649 cmartin
240 591 cmartin
                if (msg == null || msg.equals("")) {
241 649 cmartin
            if (field.isMandatory()) {
242
                return translate("Mandatory field. Please insert value.");
243
            }
244 497 cmartin
            return translate("Incompatible type. Not Validated");
245
        }
246 591 cmartin
                return msg.trim();
247 497 cmartin
    }
248
249
    private String translate(String message) {
250
        return ToolsLocator.getI18nManager().getTranslation(message);
251
    }
252
253
    private void updateValidationMessageStatus() {
254 539 cmartin
        if (validationMessage.getText().equals("")) {
255 497 cmartin
            validationMessage.setVisible(false);
256 649 cmartin
            validationLabel.setVisible(false);
257 539 cmartin
        } else {
258
            validationMessage.setVisible(true);
259 649 cmartin
            validationLabel.setVisible(true);
260 497 cmartin
        }
261
    }
262 168 cmartin
}