001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.ui.client.widgets.impl;
017    
018    import org.kuali.student.common.ui.client.util.DebugIdUtils;
019    import org.kuali.student.common.ui.client.widgets.KSButtonAbstract;
020    
021    import com.google.gwt.event.dom.client.ClickHandler;
022    import com.google.gwt.event.dom.client.MouseOutHandler;
023    import com.google.gwt.event.dom.client.MouseOverHandler;
024    import com.google.gwt.event.shared.HandlerRegistration;
025    import com.google.gwt.user.client.DOM;
026    import com.google.gwt.user.client.ui.Anchor;
027    import com.google.gwt.user.client.ui.ComplexPanel;
028    import com.google.gwt.user.client.ui.InlineLabel;
029    import com.google.gwt.user.client.ui.Widget;
030    
031    public class KSButtonImpl extends KSButtonAbstract {
032    
033        private static class SpanPanel extends ComplexPanel {
034    
035            public SpanPanel() {
036                setElement(DOM.createSpan());
037            }
038    
039            /**
040             * Adds a new child widget to the panel.
041             * 
042             * @param w the widget to be added
043             */
044            @Override
045            public void add(Widget w) {
046                add(w, getElement());
047            }
048    
049            /**
050             * Inserts a widget before the specified index.
051             * 
052             * @param w the widget to be inserted
053             * @param beforeIndex the index before which it will be inserted
054             * @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of range
055             */
056            public void insert(Widget w, int beforeIndex) {
057                insert(w, getElement(), beforeIndex, true);
058            }
059        }
060    
061        private SpanPanel panel = new SpanPanel();
062        private Anchor anchor;
063        private InlineLabel disabledLabel = new InlineLabel();
064        private boolean enabled = true;
065        private ButtonStyle currentStyle;
066    
067        public boolean isEnabled() {
068            return enabled;
069        }
070    
071        public void setEnabled(boolean enabled) {
072            anchor.setEnabled(enabled);
073            if (enabled) {
074                panel.remove(disabledLabel);
075                panel.add(anchor);
076            } else {
077                panel.remove(anchor);
078                panel.add(disabledLabel);
079            }
080        }
081    
082        @Override
083        public HandlerRegistration addClickHandler(ClickHandler handler) {
084            return anchor.addClickHandler(handler);
085        }
086    
087        @Override
088        public void setText(String text) {
089            anchor.setText(text);
090            disabledLabel.setText(text);
091        }
092    
093        @Override
094        public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
095            return anchor.addMouseOverHandler(handler);
096        }
097    
098        @Override
099        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    }