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

View differences:

StatusLabel.java
33 33
 */
34 34
package org.gvsig.tools.swing.impl.dynobject;
35 35

  
36
import java.awt.BorderLayout;
37 36
import java.awt.Color;
38 37
import java.awt.Component;
39
import java.awt.Dimension;
40 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;
41 43

  
42 44
import javax.swing.Icon;
43 45
import javax.swing.ImageIcon;
46
import javax.swing.JComponent;
44 47
import javax.swing.JLabel;
45 48
import javax.swing.JPanel;
49
import javax.swing.JScrollPane;
46 50
import javax.swing.SwingConstants;
47 51

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

  
......
53 60
 * JLabel with a "not validated" image, whose validation is validated and shown
54 61
 * whenever the listener is triggered.
55 62
 * 
56
 * @author 2010 - <a href="cordinyana@gvsig.org">C�sar Ordi�ana</a> - gvSIG Team
57
 * @author 2010 - <a href="mailto:reinhold@uji.es">Cristian Mart�n&nbsp;</a> -
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> -
58 65
 *         gvSIG Team
59 66
 * @version $Id$
60 67
 * 
61 68
 */
62
public class StatusLabel extends JLabel implements ValueChangedListener {
69
public class StatusLabel implements ValueChangedListener {
63 70

  
64 71
    /**
65 72
     * 
......
68 75

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

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

  
71 80
    private static final Color COLOR_MANDATORY = Color.RED.darker();
72 81
    private JLabel validationLabel;
82
    private JLabel validationMessage;
73 83

  
74 84
    private JPanel validationPanel;
75 85

  
76
    public StatusLabel(String text, boolean isMandatory) {
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() {
77 100
        validationLabel = new JLabel();
78 101
        validationLabel.setOpaque(true);
79
        validationLabel.setHorizontalAlignment(RIGHT);
80
        validationLabel.setIconTextGap(10);
81
        validationLabel.setVerticalAlignment(TOP);
82
        setOpaque(true);
83
        setHorizontalTextPosition(SwingConstants.LEFT);
84
        setHorizontalAlignment(LEFT);
85
        setVerticalAlignment(TOP);
86
        setText(text);
87
        if (isMandatory) {
88
            this.setForeground(COLOR_MANDATORY);
89
            this.setFont(this.getFont().deriveFont(Font.BOLD));
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));
90 120
        }
91 121
    }
92 122

  
......
94 124
     * @param notValidated
95 125
     * @return
96 126
     */
97
    private Icon getIcon(String notValidated, String description) {
98
        String path = this.getClass().getResource(NOT_VALIDATED).getPath();
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();
99 133
        if ((path == null) || (path.equals(""))) {
100 134
            return null;
101 135
        }
102 136
        ImageIcon icon = new ImageIcon(path);
103 137
        icon.setDescription(description);
104
        icon.setImageObserver(this);
138
        icon.setImageObserver(fieldLabel);
105 139
        return icon;
106 140
    }
107 141

  
......
116 150
    // }
117 151

  
118 152
    public Component getValidationLabel() {
119
        return this.validationLabel;
153
        return this.fieldLabel;
120 154
    }
121

  
122
    /**
123
     * @return
124
     */
155
   
156
    public Component getFieldComponent(){
157
        JComponent component = this.fieldComponent.asJComponent();
158
        component.setName(dynField.getName());
159
        return component;
160
    }
161
    
125 162
    public Component getValidationPanel() {
126 163
        if (validationPanel == null) {
127
            validationPanel = new JPanel(new BorderLayout());
128
            validationPanel.setSize(new Dimension(15, 15));
129
            validationPanel.setOpaque(true);
130
            validationPanel.add(validationLabel, BorderLayout.LINE_START);
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);
131 202
        }
132
        return this.validationPanel;
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;
133 210
    }
134 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

  
135 223
    /*
136 224
     * (non-Javadoc)
137 225
     * 
......
145 233
        // Set the icon and text. If icon was null, say so.
146 234
        if (!field.isValid()) {
147 235
            validationLabel.setIcon(getIcon(NOT_VALIDATED, "Not Validated"));
236
            validationMessage.setText(getErrorMessage(field));
148 237
        } else {
149
            validationLabel.setIcon(null);
238
            validationLabel.setIcon(getIcon(VALIDATED, "Validated"));
239
            validationMessage.setText("");
150 240
        }
241
        updateValidationMessageStatus();
151 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
    }
152 267
}

Also available in: Unified diff