View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.widgets.impl;
17  
18  import org.kuali.student.common.ui.client.util.DebugIdUtils;
19  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract;
20  
21  import com.google.gwt.event.dom.client.ClickHandler;
22  import com.google.gwt.event.dom.client.MouseOutHandler;
23  import com.google.gwt.event.dom.client.MouseOverHandler;
24  import com.google.gwt.event.shared.HandlerRegistration;
25  import com.google.gwt.user.client.DOM;
26  import com.google.gwt.user.client.ui.Anchor;
27  import com.google.gwt.user.client.ui.ComplexPanel;
28  import com.google.gwt.user.client.ui.InlineLabel;
29  import com.google.gwt.user.client.ui.Widget;
30  
31  public class KSButtonImpl extends KSButtonAbstract {
32  
33      private static class SpanPanel extends ComplexPanel {
34  
35          public SpanPanel() {
36              setElement(DOM.createSpan());
37          }
38  
39          /**
40           * Adds a new child widget to the panel.
41           * 
42           * @param w the widget to be added
43           */
44          @Override
45          public void add(Widget w) {
46              add(w, getElement());
47          }
48  
49          /**
50           * Inserts a widget before the specified index.
51           * 
52           * @param w the widget to be inserted
53           * @param beforeIndex the index before which it will be inserted
54           * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of range
55           */
56          public void insert(Widget w, int beforeIndex) {
57              insert(w, getElement(), beforeIndex, true);
58          }
59      }
60  
61      private SpanPanel panel = new SpanPanel();
62      private Anchor anchor;
63      private InlineLabel disabledLabel = new InlineLabel();
64      private boolean enabled = true;
65      private ButtonStyle currentStyle;
66  
67      public boolean isEnabled() {
68          return enabled;
69      }
70  
71      public void setEnabled(boolean enabled) {
72          anchor.setEnabled(enabled);
73          if (enabled) {
74              panel.remove(disabledLabel);
75              panel.add(anchor);
76          } else {
77              panel.remove(anchor);
78              panel.add(disabledLabel);
79          }
80      }
81  
82      @Override
83      public HandlerRegistration addClickHandler(ClickHandler handler) {
84          return anchor.addClickHandler(handler);
85      }
86  
87      @Override
88      public void setText(String text) {
89          anchor.setText(text);
90          disabledLabel.setText(text);
91      }
92  
93      @Override
94      public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
95          return anchor.addMouseOverHandler(handler);
96      }
97  
98      @Override
99      public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
100         return anchor.addMouseOutHandler(handler);
101     }
102 
103     @Override
104     public void init(String text) {
105         init(text, ButtonStyle.PRIMARY);
106     }
107 
108     @Override
109     public void init(String text, ButtonStyle style) {
110 
111         this.currentStyle = style;
112         disabledLabel.setText(text);
113         anchor = new Anchor();
114         if (currentStyle.getStyle() != null) {
115             disabledLabel.setStyleName(currentStyle.getStyle());
116             anchor.setStyleName(currentStyle.getStyle());
117         }
118         String disabledStyle = currentStyle.getDisabledStyle();
119         if (disabledStyle == null) {
120             disabledLabel.addStyleName("disabled");
121         } else {
122             disabledLabel.setStyleName(disabledStyle);
123         }
124         anchor.setText(text);
125         anchor.setHref("javascript:return false;");
126 
127         panel.add(anchor);
128         anchor.setTabIndex(0);
129 
130         this.initWidget(panel);
131     }
132 
133     @Override
134     public void addStyleName(String style) {
135         anchor.addStyleName(style);
136         disabledLabel.addStyleName(style);
137     }
138 
139     @Override
140     public void removeStyleName(String style) {
141         anchor.removeStyleName(style);
142         disabledLabel.removeStyleName(style);
143     }
144 
145     @Override
146     public void setStyleName(String style) {
147         anchor.setStyleName(style);
148         disabledLabel.setStyleName(style);
149     }
150 
151     @Override
152     public void init() {
153         init("", ButtonStyle.PRIMARY);
154     }
155 
156     @Override
157     public void init(String text, ClickHandler handler) {
158         init(text, ButtonStyle.PRIMARY);
159         anchor.addClickHandler(handler);
160     }
161 
162     public void init(String text, ButtonStyle style, ClickHandler handler) {
163         init(text, style);
164         anchor.addClickHandler(handler);
165     }
166 
167     @Override
168     protected void onEnsureDebugId(String baseID) {
169         super.onEnsureDebugId(baseID);
170         if (anchor != null) {
171             anchor.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(baseID + "-anchor"));
172         }
173     }
174 
175 }