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.IField;
24  import org.xulux.api.dataprovider.IMapping;
25  import org.xulux.api.gui.IWidgetInitializer;
26  import org.xulux.core.XuluxContext;
27  import org.xulux.gui.XuluxWidget;
28  import org.xulux.gui.utils.ColorUtils;
29  import org.xulux.guilayer.swing.extensions.NyxJRadioButton;
30  import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
31  import org.xulux.utils.BooleanUtils;
32  
33  /**
34   * Represents a radiobutton in the gui.
35   *
36   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
37   * @version $Id: RadioButton.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
38   */
39  public class RadioButton extends XuluxWidget {
40  
41      /**
42       * the native radiobutton
43       */
44      protected NyxJRadioButton radioButton;
45      /**
46       * the itemlistener
47       */
48      protected PrePostFieldListener itemListener;
49  
50      /**
51       * @param name the name of the radiobutton
52       */
53      public RadioButton(String name) {
54          super(name);
55      }
56  
57      /**
58       * @see org.xulux.gui.XuluxWidget#destroy()
59       */
60      public void destroy() {
61          if (!initialized) {
62              return;
63          }
64          processDestroy();
65          removeAllRules();
66          if (radioButton != null) {
67              if (itemListener != null) {
68                  radioButton.removeItemListener(itemListener);
69              }
70              radioButton.removeAll();
71              Container container = radioButton.getParent();
72              if (container != null) {
73                  container.remove(radioButton);
74              }
75              radioButton = null;
76          }
77          removeAllRules();
78          getPart().removeWidget(this, this);
79  
80      }
81  
82      /**
83       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
84       */
85      public Object getNativeWidget() {
86          initialize();
87          return this.radioButton;
88      }
89  
90      /**
91       * @see org.xulux.nyx.gui.XuluxWidget#initialize()
92       */
93      public void initialize() {
94          if (initialized) {
95              return;
96          }
97          radioButton = new NyxJRadioButton();
98          // set the icon to what is default in Swing..
99          radioButton.setIcon((Icon) UIManager.get("RadioButton.icon"));
100         radioButton.setSelectedIcon((Icon) UIManager.get("RadioButton.icon"));
101         itemListener = new PrePostFieldListener(this);
102         radioButton.addItemListener(this.itemListener);
103         radioButton.setSelected(BooleanUtils.toBoolean(getValue()));
104         initialized = true;
105         refresh();
106         processInit();
107 
108     }
109 
110     /**
111      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
112      */
113     public void refresh() {
114         if (isRefreshing()) {
115             return;
116         }
117         isRefreshing = true;
118         initialize();
119         if (getProperty("text") != null) {
120             radioButton.setText(getProperty("text"));
121         }
122         if (getValue() instanceof Boolean) {
123             radioButton.setSelected(BooleanUtils.toBoolean((Boolean) getValue()));
124         } else if (getValue() instanceof String) {
125             radioButton.setSelected(BooleanUtils.toBoolean((String) getValue()));
126         } else {
127             radioButton.setSelected(BooleanUtils.toBoolean(getProperty("selected")));
128         }
129         radioButton.setEnabled(isEnabled());
130         String backgroundColor = null;
131         if (isRequired() && isEnabled()) {
132             backgroundColor = getProperty("required-background-color");
133         } else if (!isEnabled()) {
134             backgroundColor = getProperty("disabled-background-color");
135         } else {
136             backgroundColor = getProperty("default-background-color");
137         }
138         if (backgroundColor != null) {
139             radioButton.setRealBackground(ColorUtils.getSwingColor(backgroundColor));
140         }
141         isRefreshing = false;
142     }
143 
144     /**
145      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
146      */
147     public Object getGuiValue() {
148         if (initialized) {
149             return BooleanUtils.toBooleanObject(radioButton.isSelected());
150         }
151         return null;
152     }
153 
154     /**
155      * @see org.xulux.nyx.gui.XuluxWidget#focus()
156      */
157     public void focus() {
158 
159     }
160 
161     /**
162      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
163      */
164     public boolean isValueEmpty() {
165         return false;
166     }
167 
168     /**
169      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
170      */
171     public boolean canContainValue() {
172         return true;
173     }
174 
175     /**
176      * @see org.xulux.nyx.gui.XuluxWidget#getValue()
177      */
178     public Object getValue() {
179         if (getPart().getBean() == null || getField() == null) {
180             Object retValue = getGuiValue();
181             if (retValue == null) {
182                 retValue = super.getValue();
183             }
184             return retValue;
185         }
186         IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean().getClass());
187         if (map != null) {
188             IField field = map.getField(getField());
189             if (field != null) {
190                 return field.getValue(getPart().getBean());
191             }
192         }
193         return super.getValue();
194     }
195 
196     /**
197      * @see org.xulux.nyx.gui.XuluxWidget#setValue(java.lang.Object)
198      */
199     public void setValue(Object value) {
200         if (this.value == null) {
201             this.value = "false";
202         }
203         this.previousValue = this.value;
204         IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
205         if (map != null) {
206             if (getField() != null) {
207                 IField f = map.getField(getField());
208                 Class cClass = f.getType();
209                 if (cClass == Boolean.class || cClass == Boolean.TYPE) {
210                     if (value.getClass() == String.class) {
211                         value = BooleanUtils.toBooleanObject((String) value);
212                     }
213                 } else if (cClass == String.class) {
214                     if (value.getClass() == Boolean.class) {
215                         value = BooleanUtils.toStringTrueFalse((Boolean) value);
216                     }
217                 }
218                 f.setValue(getPart().getBean(), value);
219             }
220         }
221         this.value = value;
222         if (initialized) {
223             refresh();
224         }
225     }
226 
227     public IWidgetInitializer getWidgetInitializer() {
228         return null;
229     }
230 
231 }