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.Icon;
21  import javax.swing.UIManager;
22  
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.api.dataprovider.InvalidValueException;
27  import org.xulux.api.gui.IXuluxListener;
28  import org.xulux.core.XuluxContext;
29  import org.xulux.gui.utils.ColorUtils;
30  import org.xulux.guilayer.swing.SwingWidget;
31  import org.xulux.guilayer.swing.extensions.NyxJCheckBox;
32  import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
33  import org.xulux.utils.BooleanUtils;
34  
35  /**
36   * The nyx to swing implementation of a checkbox.
37   * NOTE : setting the background color is a one time thing.
38   *        It will not repaint the background with a new color!
39   *
40   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
41   * @version $Id: CheckBox.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
42   */
43  public class CheckBox extends SwingWidget {
44  
45      /**
46       * The native checkbox
47       */
48      private NyxJCheckBox checkBox;
49  
50      /**
51       * The checkbox listener
52       */
53      private PrePostFieldListener itemListener;
54  
55      /**
56       * @param name the name of the checkbox
57       */
58      public CheckBox(String name) {
59          super(name);
60      }
61  
62  
63      /**
64       * @see org.xulux.api.gui.IWidget#destroy()
65       */
66      public void destroy() {
67          processDestroy();
68          removeAllRules();
69          if (checkBox != null) {
70              Container container = checkBox.getParent();
71              checkBox.setVisible(false);
72              if (container != null) {
73                  container.remove(checkBox);
74              }
75              if (itemListener != null) {
76                  checkBox.removeItemListener(itemListener);
77                  checkBox.removeFocusListener(itemListener);
78              }
79              itemListener = null;
80              checkBox = null;
81          }
82          removeAllRules();
83          getPart().removeWidget(this, this);
84      }
85  
86  
87      /**
88       * @see org.xulux.api.gui.IWidget#getNativeWidget()
89       */
90      public Object getNativeWidget() {
91          initialize();
92          return checkBox;
93      }
94  
95  
96      /**
97       * @see org.xulux.api.gui.IWidget#initialize()
98       */
99      public void initialize() {
100         if (this.initialized) {
101             return;
102         }
103         this.initialized = true;
104         this.checkBox = new NyxJCheckBox();
105         String backgroundColor = null;
106         if (isRequired() && isEnabled()) {
107             backgroundColor = getProperty("required-background-color");
108         } else if (!isEnabled()) {
109             backgroundColor = getProperty("disabled-background-color");
110         } else {
111             backgroundColor = getProperty("default-background-color");
112         }
113         if (backgroundColor != null) {
114             checkBox.setRealBackground(ColorUtils.getSwingColor(backgroundColor));
115         }
116         // set the icon to what is default in Swing..
117         this.checkBox.setIcon((Icon) UIManager.get("CheckBox.icon"));
118         this.checkBox.setSelectedIcon((Icon) UIManager.get("CheckBox.icon"));
119         this.itemListener = new PrePostFieldListener(this);
120         // we always need to add a itemlistener to change
121         // the value..
122         checkBox.addItemListener(this.itemListener);
123         checkBox.addFocusListener(this.itemListener);
124         if ("false".equalsIgnoreCase(getProperty("focuspossible"))) {
125             checkBox.setFocusTraversable(false);
126         }
127         refresh();
128         processInit();
129     }
130 
131 
132     /**
133      * @see org.xulux.api.gui.IWidget#refresh()
134      */
135     public void refresh() {
136         isRefreshing = true;
137         initialize();
138         checkBox.setVisible(isVisible());
139         if (getProperty("text") != null) {
140             checkBox.setText(getProperty("text"));
141         }
142         if (getValue() instanceof Boolean) {
143             checkBox.setSelected(BooleanUtils.toBoolean((Boolean) getValue()));
144         } else if (getValue() instanceof String) {
145             checkBox.setSelected(BooleanUtils.toBoolean((String) getValue()));
146         }
147         checkBox.setEnabled(isEnabled());
148 //        String backgroundColor = null;
149 //        if (isRequired() && isEnabled()) {
150 //            backgroundColor = getProperty("required-background-color");
151 //        } else if (!isEnabled()) {
152 //            backgroundColor = getProperty("disabled-background-color");
153 //        } else {
154 //            backgroundColor = getProperty("default-background-color");
155 //        }
156 //        if (backgroundColor != null) {
157 //            checkBox.setRealBackground(ColorUtils.getSwingColor(backgroundColor));
158 //        }
159         isRefreshing = false;
160     }
161 
162 
163     /**
164      * @see org.xulux.api.gui.IWidget#getValue()
165      */
166     public Object getValue() {
167         if (getProvider() != null) {
168           return this.value;
169         } else  if (getPart().getBean() == null || getField() == null) {
170             return super.getValue();
171         }
172         IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean().getClass());
173         if (map != null) {
174             IField field = map.getField(getField());
175             if (field != null) {
176                 return field.getValue(getPart().getBean());
177             }
178         }
179         return super.getValue();
180     }
181 
182     /**
183      * @see org.xulux.api.gui.IWidget#setValue(java.lang.Object)
184      */
185     public void setValue(Object object) {
186         if (getProvider() != null) {
187           IDataProvider pr = XuluxContext.getDictionary().getProvider(getProvider());
188           IMapping mapping = null;
189           IField field = null;
190           if (!(object instanceof String) && this.value == null) {
191             this.value = object;
192           } else {
193             mapping = pr.getMapping(this.value);
194             if (getField() != null) {
195                 field = mapping.getField(getField());
196             } else {
197                 field = mapping.getField(this.value);
198             }
199             try {
200               field.setValue(this.value, object);
201               setValidValue(true);
202             } catch(InvalidValueException ive) {
203               setValidValue(false);
204             }
205           }
206         } else {
207             if (this.value == null) {
208                 this.value = "false";
209             }
210             this.previousValue = this.value;
211             IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
212             if (map != null) {
213                 if (getField() != null) {
214                     IField f = map.getField(getField());
215                     Class cClass = f.getType();
216                     if (cClass == Boolean.class || cClass == Boolean.TYPE) {
217                         if (object.getClass() == String.class) {
218                             object = BooleanUtils.toBooleanObject((String) object);
219                         }
220                     } else if (cClass == String.class) {
221                         if (object.getClass() == Boolean.class) {
222                             object = BooleanUtils.toStringTrueFalse((Boolean) object);
223                         }
224                     }
225                     f.setValue(getPart().getBean(), object);
226                 }
227             }
228             this.value = object;
229         }
230         if (initialized) {
231             refresh();
232         }
233     }
234 
235 
236     /**
237      * @see org.xulux.api.gui.IWidget#getGuiValue()
238      */
239     public Object getGuiValue() {
240         return BooleanUtils.toBooleanObject(checkBox.isSelected());
241     }
242 
243 
244     /**
245      * @see org.xulux.api.gui.IWidget#canContainValue()
246      */
247     public boolean canContainValue() {
248         return true;
249     }
250 
251     /**
252      * @see org.xulux.api.gui.IWidget#isValueEmpty()
253      */
254     public boolean isValueEmpty() {
255         return false;
256     }
257 
258     /**
259      * @see org.xulux.api.gui.IWidget#addXuluxListener(org.xulux.api.gui.IXuluxListener)
260      */
261     public void addXuluxListener(IXuluxListener listener) {
262     }
263 
264 }