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 @ 497

History | View | Annotate | Download (8.95 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.Icon;
45
import javax.swing.ImageIcon;
46
import javax.swing.JComponent;
47
import javax.swing.JLabel;
48
import javax.swing.JPanel;
49
import javax.swing.JScrollPane;
50
import javax.swing.SwingConstants;
51

    
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.dataTypes.DataTypes;
54
import org.gvsig.tools.dynobject.DynField;
55
import org.gvsig.tools.swing.api.dynobject.ValueChangedListener;
56
import org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent;
57

    
58
/**
59
 * 
60
 * JLabel with a "not validated" image, whose validation is validated and shown
61
 * whenever the listener is triggered.
62
 * 
63
 * @author 2010 - <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG Team
64
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart?n  Reinhold</a> -
65
 *         gvSIG Team
66
 * @version $Id$
67
 * 
68
 */
69
public class StatusLabel implements ValueChangedListener {
70

    
71
    /**
72
     * 
73
     */
74
    private static final long serialVersionUID = -8404494423411589010L;
75

    
76
    private static final String NOT_VALIDATED = "unavailable.png";
77

    
78
    private static final String VALIDATED = "transparent.png";
79

    
80
    private static final Color COLOR_MANDATORY = Color.RED.darker();
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, DynField dynField) {
92
        this.fieldComponent = input;
93
        this.fieldLabel = fieldLabel;
94
        this.dynField = dynField;
95
        
96
       initUI();
97
    }
98

    
99
    private void initUI() {
100
        validationLabel = new JLabel();
101
        validationLabel.setOpaque(true);
102
        validationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
103
        validationLabel.setIconTextGap(1);
104
        validationLabel.setText(" ");
105
        validationLabel.setVerticalAlignment(SwingConstants.TOP);
106
        
107
        
108
        validationMessage = new JLabel();
109
        validationLabel.setOpaque(true);
110
        validationLabel.setText("");
111
        validationLabel.setVerticalAlignment(SwingConstants.CENTER);
112
        
113
        fieldLabel.setOpaque(true);
114
        fieldLabel.setHorizontalTextPosition(SwingConstants.LEFT);
115
        fieldLabel.setHorizontalAlignment(SwingConstants.LEFT);
116
        fieldLabel.setVerticalAlignment(SwingConstants.TOP);
117
        if (dynField.isMandatory()) {
118
            fieldLabel.setForeground(COLOR_MANDATORY);
119
            fieldLabel.setFont(fieldLabel.getFont().deriveFont(Font.BOLD));
120
        }
121
    }
122

    
123
    /**
124
     * @param notValidated
125
     * @return
126
     */
127
    private Icon getIcon(String iconName, String description) {
128
        URL res = this.getClass().getResource(iconName);
129
        if (res == null){
130
            return null;
131
        }
132
        String path = res.getPath();
133
        if ((path == null) || (path.equals(""))) {
134
            return null;
135
        }
136
        ImageIcon icon = new ImageIcon(path);
137
        icon.setDescription(description);
138
        icon.setImageObserver(fieldLabel);
139
        return icon;
140
    }
141

    
142
    //
143
    // // Set the font and text when no image was found.
144
    // protected void setText(String text, Font normalFont) {
145
    // if (uhOhFont == null) { // lazily create this font
146
    // uhOhFont = normalFont.deriveFont(Font.ITALIC);
147
    // }
148
    // setFont(uhOhFont);
149
    // setText(uhOhText);
150
    // }
151

    
152
    public Component getValidationLabel() {
153
        return this.fieldLabel;
154
    }
155
   
156
    public Component getFieldComponent(){
157
        JComponent component = this.fieldComponent.asJComponent();
158
        component.setName(dynField.getName());
159
        return component;
160
    }
161
    
162
    public Component getValidationPanel() {
163
        if (validationPanel == null) {
164
            validationPanel = new JPanel(new GridBagLayout());
165

    
166
                GridBagConstraints constr = getValidationPanelConstraints();
167
                validationPanel.setOpaque(true);
168

    
169
                constr.weightx = 0.0; 
170
                constr.weighty = 0.0; 
171
                constr.gridx = 0; 
172
                constr.gridy = 1; 
173
                constr.fill = GridBagConstraints.NONE;
174
                validationPanel.add(validationLabel, constr);
175

    
176

    
177
                constr.weightx = 1.0; 
178
                constr.weighty = 0.0; 
179
                constr.gridx = 0; 
180
                constr.gridy = 0; 
181
                if(this.dynField.getType()== DataTypes.LIST){
182
                    constr.gridwidth=3;
183
                    
184
                }else{
185
                    constr.gridwidth=2;                    
186
                }
187
//                constr.fill = GridBagConstraints.RELATIVE;
188
                constr.fill = GridBagConstraints.BOTH; 
189
                constr.anchor = GridBagConstraints.NORTHWEST;
190
                validationPanel.add(getFieldComponent(),  constr);
191

    
192
                constr = new GridBagConstraints();
193
                constr.insets = new Insets(1, 2, 2, 2);
194
                constr.gridx = 1; 
195
                constr.gridy = 1; 
196
                constr.ipadx = 5;
197
                constr.ipady = 1;
198
                constr.fill = GridBagConstraints.HORIZONTAL;
199
                constr.anchor = GridBagConstraints.NORTHWEST;
200

    
201
                validationPanel.add(validationMessage, constr);
202
        }
203
     
204
        JScrollPane jScrollPane = new javax.swing.JScrollPane(); 
205
        JPanel newPanel = new JPanel(new GridBagLayout());
206
        jScrollPane.setViewportView(validationPanel);
207
           
208
        newPanel.add(jScrollPane, getValidationPanelConstraints()); 
209
        return newPanel;
210
    }
211

    
212
    private GridBagConstraints getValidationPanelConstraints() {
213
        GridBagConstraints constr = new GridBagConstraints();
214
        constr.insets = new Insets(2, 2, 2, 2);
215
        constr.weightx = 1.0; 
216
        constr.weighty = 1.0; 
217
        constr.ipadx = 2;
218
        constr.ipady = 2;
219
        constr.fill = GridBagConstraints.BOTH; 
220
        constr.anchor = GridBagConstraints.NORTHWEST;
221
        return constr;    }
222

    
223
    /*
224
     * (non-Javadoc)
225
     * 
226
     * @see
227
     * 
228
     * org.gvsig.tools.swing.api.dynobject.ValueChangedListener#handleValueChanged
229
     * (org.gvsig.tools.swing.api.dynobject.dynfield.JDynFieldComponent)
230
     */
231
    public void handleValueChanged(JDynFieldComponent field) {
232

    
233
        // Set the icon and text. If icon was null, say so.
234
        if (!field.isValid()) {
235
            validationLabel.setIcon(getIcon(NOT_VALIDATED, "Not Validated"));
236
            validationMessage.setText(getErrorMessage(field));
237
        } else {
238
            validationLabel.setIcon(getIcon(VALIDATED, "Validated"));
239
            validationMessage.setText("");
240
        }
241
        updateValidationMessageStatus();
242
    }
243

    
244
    private String getErrorMessage(JDynFieldComponent field) {
245
        String msg = field.getValidationMessage();
246
        if ((field.isMandatory())&&(field.getValue()==null)){
247
            return translate("Mandatory field. Please insert value.");
248
        }
249
        if ((msg == null) ||(msg.equals(""))){
250
            return translate("Incompatible type. Not Validated");
251
        }
252
        return msg;        
253
    }
254

    
255
    private String translate(String message) {
256
        return ToolsLocator.getI18nManager().getTranslation(message);
257
    }
258

    
259
    private void updateValidationMessageStatus() {
260
        if (validationMessage.getText().equals("")){
261
            validationMessage.setVisible(false);
262
        }else{
263
            validationMessage.setVisible(true);            
264
            
265
        }
266
    }
267
}