View Javadoc

1   /*
2    * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
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  
17  package org.gwtwidgets.client.ui;
18  
19  import com.google.gwt.user.client.ui.MouseListenerAdapter;
20  import com.google.gwt.user.client.ui.Widget;
21  
22  public class ToggleButton extends ImageButton
23  {
24      private int trueState;
25  
26  
27      public ToggleButton (String url, int width, int height)
28      {
29          super(url, width, height);
30      }
31  
32      protected void init ()
33      {
34          addMouseListener(new MouseListenerAdapter(){
35  
36              public void onMouseEnter (Widget sender)
37              {
38                  trueState = getState();
39                  setState(ON_STATE);
40                  setColors();
41              }
42  
43              public void onMouseLeave (Widget sender)
44              {
45                  setState(trueState);
46                  setColors();
47              }
48          });
49      }
50      
51      public void setOn (boolean on)
52      {
53          if (on) {
54              setState(ON_STATE);
55              trueState = ON_STATE;
56          }
57          else {
58              setState(OFF_STATE);
59              trueState = OFF_STATE;
60          }
61          setColors();
62      }
63  
64  
65      /***
66       * Toggle button state
67       * @param on
68       */
69      public void toggle ()
70      {
71          if (trueState == ON_STATE) {
72              trueState = OFF_STATE;
73          }
74          else {
75              trueState = ON_STATE;
76          }
77          setColors();
78      }
79  }