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  import java.awt.event.FocusEvent;
20  import java.awt.event.FocusListener;
21  
22  import javax.swing.Icon;
23  import javax.swing.ImageIcon;
24  import javax.swing.JToggleButton;
25  
26  import org.xulux.api.dataprovider.IField;
27  import org.xulux.api.dataprovider.IMapping;
28  import org.xulux.api.gui.IWidgetInitializer;
29  import org.xulux.core.XuluxContext;
30  import org.xulux.gui.XuluxWidget;
31  import org.xulux.guilayer.swing.listeners.PrePostFieldListener;
32  import org.xulux.guilayer.swing.util.SwingUtils;
33  import org.xulux.utils.BooleanUtils;
34  
35  /**
36   * Represents a togglebutton in the gui.
37   *
38   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
39   * @version $Id: ToggleButton.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
40   */
41  public class ToggleButton extends XuluxWidget {
42  
43      /**
44       * The native togglebutton
45       */
46      protected JToggleButton toggleButton;
47      /**
48       * the itemlistner
49       */
50      protected PrePostFieldListener itemListener;
51      /**
52       * the focuslistener
53       */
54      protected FocusListener focusListener;
55  
56      /**
57       * @param name the name of the togglebutton
58       */
59      public ToggleButton(String name) {
60          super(name);
61      }
62  
63      /**
64       * @see org.xulux.gui.XuluxWidget#destroy()
65       */
66      public void destroy() {
67          if (!initialized) {
68              return;
69          }
70          processDestroy();
71          removeAllRules();
72          if (toggleButton != null) {
73              if (itemListener != null) {
74                  toggleButton.removeItemListener(itemListener);
75              }
76              toggleButton.removeAll();
77              Container container = toggleButton.getParent();
78              if (container != null) {
79                  container.remove(toggleButton);
80              }
81              toggleButton = null;
82          }
83          getPart().removeWidget(this, this);
84  
85      }
86  
87      /**
88       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
89       */
90      public Object getNativeWidget() {
91          initialize();
92          return this.toggleButton;
93      }
94  
95      /**
96       * @see org.xulux.nyx.gui.XuluxWidget#initialize()
97       */
98      public void initialize() {
99          if (initialized) {
100             return;
101         }
102         toggleButton = new JToggleButton();
103         itemListener = new PrePostFieldListener(this);
104         toggleButton.addItemListener(this.itemListener);
105         initialized = true;
106         refresh();
107         processInit();
108 
109     }
110 
111     /**
112      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
113      */
114     public void refresh() {
115         if (isRefreshing()) {
116             return;
117         }
118         isRefreshing = true;
119         initialize();
120         if (getProperty("text") != null) {
121             toggleButton.setText(getProperty("text"));
122         }
123         if (getProperty("selected") != null) {
124             setValue(BooleanUtils.toBooleanObject(getProperty("selected")));
125             setProperty("selected", null);
126         }
127         if (getValue() instanceof Boolean) {
128             toggleButton.setSelected(BooleanUtils.toBoolean((Boolean) getValue()));
129         } else if (getValue() instanceof String) {
130             toggleButton.setSelected(BooleanUtils.toBoolean((String) getValue()));
131         }
132         toggleButton.setEnabled(isEnabled());
133         // @todo check to see the repainting problem nyx has with togglebuttons.
134         // currently I got to the point when the rollover image was still active
135         // when leaving the button with the mouse, no events were fired or eaten?
136         // need to investigate further.
137         //        toggleButton.getModel().addChangeListener(new ChangeListener() {
138         //
139         //            public void stateChanged(ChangeEvent e) {
140         //                System.out.println("statechanged..."+e);
141         //                toggleButton.invalidate();
142         //                toggleButton.repaint();
143         //            }
144         //
145         //        });
146         String image = getProperty("image");
147         if (image != null) {
148             ImageIcon normalIcon = SwingUtils.getIcon(image, this);
149             toggleButton.setIcon(normalIcon);
150             toggleButton.setFocusPainted(true);
151         }
152         String disabledImage = getProperty("image-disabled");
153         if (disabledImage != null) {
154             ImageIcon icon = SwingUtils.getIcon(disabledImage, this);
155             toggleButton.setDisabledIcon(icon);
156             toggleButton.setDisabledSelectedIcon(icon);
157         }
158         String rolloverImage = getProperty("image-rollover");
159         if (rolloverImage != null) {
160             ImageIcon icon = SwingUtils.getIcon(rolloverImage, this);
161             toggleButton.setRolloverEnabled(true);
162             toggleButton.setRolloverIcon(icon);
163         }
164 
165         String selectedImage = getProperty("image-selected");
166         if (selectedImage != null) {
167             ImageIcon icon = SwingUtils.getIcon(selectedImage, this);
168             toggleButton.setSelectedIcon(icon);
169             toggleButton.setPressedIcon(icon);
170             toggleButton.setRolloverSelectedIcon(icon);
171             toggleButton.setRolloverEnabled(true);
172             // we need to add a focuslistener to set the image
173             // when focus is there to the selected image
174             if (this.focusListener == null) {
175                 this.focusListener = new FocusListener() {
176                     Icon normalIcon;
177                     /**
178                      * @see java.awt.event.FocusListener#focusGained(FocusEvent)
179                      */
180                     public void focusGained(FocusEvent e) {
181                         if (normalIcon == null) {
182                             normalIcon = toggleButton.getIcon();
183                         }
184                         toggleButton.setIcon(toggleButton.getSelectedIcon());
185                     }
186 
187                     /**
188                      * @see java.awt.event.FocusListener#focusLost(FocusEvent)
189                      */
190                     public void focusLost(FocusEvent e) {
191                         String image = getProperty("image");
192                         if (image != null) {
193                             ImageIcon normalIcon = SwingUtils.getIcon(image, this);
194                             if (toggleButton != null) {
195                                 toggleButton.setIcon(normalIcon);
196                                 toggleButton.setFocusPainted(true);
197                             }
198                         }
199                     }
200                 };
201                 toggleButton.addFocusListener(focusListener);
202             }
203 
204         }
205         toggleButton.setToolTipText(getProperty("tooltip"));
206         
207         toggleButton.repaint();
208         isRefreshing = false;
209     }
210 
211     /**
212      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
213      */
214     public Object getGuiValue() {
215         if (initialized) {
216             return BooleanUtils.toBooleanObject(toggleButton.isSelected());
217         }
218         return null;
219     }
220 
221     /**
222      * @see org.xulux.nyx.gui.XuluxWidget#focus()
223      */
224     public void focus() {
225 
226     }
227 
228     /**
229      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
230      */
231     public boolean isValueEmpty() {
232         return false;
233     }
234 
235     /**
236      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
237      */
238     public boolean canContainValue() {
239         return true;
240     }
241 
242     /**
243      * @see org.xulux.nyx.gui.XuluxWidget#getValue()
244      */
245     public Object getValue() {
246         if (getPart().getBean() == null || getField() == null) {
247             Object retValue = super.getValue();
248             if (retValue == null) {
249                 retValue = getGuiValue();
250             }
251             return retValue;
252         }
253         IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean().getClass());
254         if (map != null) {
255             IField field = map.getField(getField());
256             if (field != null) {
257                 return field.getValue(getPart().getBean());
258             }
259         }
260         return super.getValue();
261     }
262 
263     /**
264      * @see org.xulux.nyx.gui.XuluxWidget#setValue(java.lang.Object)
265      */
266     public void setValue(Object value) {
267         if (this.value == null) {
268             this.value = "false";
269         }
270         this.previousValue = this.value;
271         IMapping map = XuluxContext.getDictionary().getDefaultProvider().getMapping(getPart().getBean());
272         if (map != null) {
273             if (getField() != null) {
274                 IField f = map.getField(getField());
275                 Class cClass = f.getType();
276                 if (cClass == Boolean.class || cClass == Boolean.TYPE) {
277                     if (value.getClass() == String.class) {
278                         value = BooleanUtils.toBooleanObject((String) value);
279                     }
280                 } else if (cClass == String.class) {
281                     if (value.getClass() == Boolean.class) {
282                         value = BooleanUtils.toStringTrueFalse((Boolean) value);
283                     }
284                 }
285                 f.setValue(getPart().getBean(), value);
286             }
287         }
288         this.value = value;
289         if (initialized) {
290             refresh();
291         }
292     }
293 
294     public IWidgetInitializer getWidgetInitializer() {
295         return null;
296     }
297 }