View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.widgets;
17  
18  import java.awt.Container;
19  
20  import javax.swing.JLabel;
21  
22  import org.xulux.api.dataprovider.IConverter;
23  import org.xulux.api.dataprovider.IDataProvider;
24  import org.xulux.api.dataprovider.IField;
25  import org.xulux.api.dataprovider.IMapping;
26  import org.xulux.core.XuluxContext;
27  import org.xulux.dataprovider.Dictionary;
28  import org.xulux.gui.utils.ColorUtils;
29  import org.xulux.guilayer.swing.SwingWidget;
30  import org.xulux.guilayer.swing.util.SwingUtils;
31  import org.xulux.utils.BooleanUtils;
32  import org.xulux.utils.ClassLoaderUtils;
33  
34  /**
35   *
36   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
37   * @version $Id: Label.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
38   */
39  public class Label extends SwingWidget {
40  
41      /**
42       * The native label
43       */
44      private JLabel label;
45  
46      /**
47       *
48       * @param name the name of the label
49       */
50      public Label(String name) {
51          super(name);
52      }
53  
54      /**
55       * @see org.xulux.nyx.gui.XuluxWidget#destroy()
56       */
57      public void destroy() {
58          processDestroy();
59          removeAllRules();
60          if (label != null) {
61              Container container = label.getParent();
62              label.setVisible(false);
63              if (container != null) {
64                  container.remove(label);
65              }
66              label = null;
67          }
68          getPart().removeWidget(this, this);
69      }
70  
71      /**
72       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
73       */
74      public Object getNativeWidget() {
75          initialize();
76          return label;
77      }
78  
79      /**
80       * @see org.xulux.nyx.gui.XuluxWidget#initialize()
81       */
82      public void initialize() {
83          if (this.initialized) {
84              return;
85          }
86          this.initialized = true;
87          this.label = new JLabel();
88          refresh();
89          processInit();
90      }
91      /**
92       * For now aligns to the right by default.
93       *
94       * @see org.xulux.nyx.gui.XuluxWidget#refresh()
95       */
96      public void refresh() {
97          if (isRefreshing()) {
98              return;
99          }
100         isRefreshing = true;
101         initialize();
102         initializeValue();
103         if (getProperty("text") != null) {
104             label.setText(getProperty("text"));
105         } else {
106             label.setText("");
107         }
108         if (getProperty("icon") != null) {
109             label.setIcon(SwingUtils.getIcon(getProperty("icon"), this));
110         }
111         if (getProperty("icon-disabled") != null) {
112             label.setDisabledIcon(SwingUtils.getIcon(getProperty("icon-disabled"), this));
113         }
114         String ha = getProperty("horizontalalignment");
115         // we use the swing default..
116         if (ha != null) {
117             if (ha.equalsIgnoreCase("left")) {
118                 label.setHorizontalAlignment(JLabel.LEFT);
119             } else if (ha.equalsIgnoreCase("center")) {
120                 label.setHorizontalAlignment(JLabel.CENTER);
121             } else {
122                 label.setHorizontalAlignment(JLabel.RIGHT);
123             }
124         }
125         String color = null;
126         if (isEnabled()) {
127             color = getProperty("foreground-color-enabled");
128         } else {
129             color = getProperty("foreground-color-disabled");
130         }
131         if (color != null) {
132             label.setForeground(ColorUtils.getSwingColor(color));
133         }
134         String bgColor = null;
135         if (isEnabled()) {
136             bgColor = getProperty("background-color-enabled");
137         } else {
138             bgColor = getProperty("background-color-disabled");
139         }
140         if (bgColor != null) {
141             label.setBackground(ColorUtils.getSwingColor(bgColor));
142         }
143         if (getProperty("enabled.depends") != null) {
144             String value = getProperty("enabled.depends");
145             Object depValue = getPart().getWidget(value).getValue();
146             if (depValue != null) {
147                 setEnabled(BooleanUtils.toBoolean(depValue.toString()));
148             } else {
149                 setEnabled(false);
150             }
151         }
152         label.setEnabled(isEnabled());
153         label.setVisible(isVisible());
154         label.setToolTipText(getProperty("tooltip"));
155         isRefreshing = false;
156     }
157 
158     /**
159      * Initializes the value. This checks to see
160      * if you need a .
161      *
162      */
163     protected void initializeValue() {
164         // only do a set text when there is a field,
165         // else leave it alone..
166 	      if (getProvider() != null && getField() != null) {
167 	        IDataProvider provider = XuluxContext.getDictionary().getProvider(getProvider());
168 	        Object bean = getPart().getBean();
169 	        //System.out.println("bean class : " + bean.getClass());
170 	        Object value = provider.getValue(bean, getField(), bean);
171 	        if (value == null) {
172 	          value = "";
173 	        }
174 	        setProperty("text", String.valueOf(value));
175 	      }
176         if (getField() != null) {
177             IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
178             if (map == null) {
179                 return;
180             }
181             IField field = map.getField(getField());
182             if (field != null) {
183                 Object value = field.getValue(getPart().getBean());
184                 if (value == null) {
185                     value = "";
186                 }
187                 IConverter converter = Dictionary.getConverter(getValue());
188                 if (converter != null) {
189                     value = converter.getGuiValue(value);
190                 }
191                 if (value == null) {
192                     setProperty("text", "");
193                 } else {
194                     setProperty("text", String.valueOf(value));
195                 }
196             }
197         }
198         String initialValue = getProperty("initialvalue");
199         if (initialValue != null) {
200             String initialUse = getProperty("initialvalue.use");
201             Class clz = ClassLoaderUtils.getClass(initialValue);
202             IMapping mapping = XuluxContext.getDictionary().getDefaultProvider().getMapping(clz);
203             if (mapping != null) {
204                 IField field = mapping.getField(initialUse);
205                 if (field != null) {
206                     setProperty("text", String.valueOf(field.getValue(null)));
207                 }
208             }
209         }
210     }
211 
212     /**
213      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
214      */
215     public Object getGuiValue() {
216         return getValue();
217     }
218 
219     /**
220      * @see org.xulux.nyx.gui.XuluxWidget#setValue(java.lang.Object)
221      */
222     public void setValue(Object value) {
223         if (value == null) {
224             String defaultnullvalue = getProperty("defaultnullvalue");
225             if(defaultnullvalue != null){
226                 value = defaultnullvalue;
227             }else{
228                 value = "";
229             }
230         }
231         String currentText = getProperty("text");
232         if (currentText != null && currentText.equals(value)) {
233             return;
234         }
235         this.previousValue = currentText;
236         // no refresh needed, since it will
237         // refresh on setProperty..
238         setProperty("text", String.valueOf(value));
239     }
240 
241     /**
242      * @see org.xulux.nyx.gui.XuluxWidget#getValue()
243      */
244     public Object getValue() {
245         return getProperty("text");
246     }
247 
248     /**
249      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
250      */
251     public boolean canContainValue() {
252         return true;
253     }
254 
255     /**
256      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
257      */
258     public boolean isValueEmpty() {
259         if (getProperty("text") == null || getProperty("text").equals("")) {
260             return true;
261         }
262         return false;
263     }
264 
265 }