001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.HasIcon;
019import com.github.gwtbootstrap.client.ui.base.IconAnchor;
020import com.github.gwtbootstrap.client.ui.base.ListItem;
021import com.github.gwtbootstrap.client.ui.constants.*;
022import com.google.gwt.event.dom.client.ClickEvent;
023import com.google.gwt.event.dom.client.ClickHandler;
024import com.google.gwt.event.dom.client.HasClickHandlers;
025import com.google.gwt.event.shared.HandlerRegistration;
026import com.google.gwt.uibinder.client.UiChild;
027import com.google.gwt.user.client.ui.Widget;
028
029//@formatter:off
030/**
031 * A Container for Widgets in a Nav context (Navbar, NavList, ...). Example:<br>
032 * <br>
033 * 
034 * {@code <b:NavWidget text="Inbox"><b:Badge /></b:NavWidget>}
035 * 
036 * @since 2.0.4.0
037 * @author Dominik Mayer
038 * 
039 * @see <a href="http://getbootstrap.com/2.3.2/components.html#navbar">Bootstrap documentation (Navbar)</a>
040 * @see <a href="http://getbootstrap.com/2.3.2/components.html#navs">Bootstrap documentation (Navs)</a>
041 * @see NavList
042 * @see WellNavList
043 * @see Dropdown
044 * @see Navbar
045 * @see ResponsiveNavbar
046 */
047//@formatter:on
048public class NavWidget extends ListItem implements HasClickHandlers, HasIcon {
049
050        private final IconAnchor anchor = new IconAnchor();
051
052        public NavWidget() {
053                super.add(anchor);
054        }
055
056        public NavWidget(Widget w) {
057                this();
058                add(w);
059        }
060
061        public void setHref(String href) {
062                anchor.setHref(href);
063        }
064
065        public void setTargetHistoryToken(String targetHistoryToken) {
066                anchor.setTargetHistoryToken(targetHistoryToken);
067        }
068
069        public void setText(String text) {
070                anchor.setText(text);
071        }
072
073        public String getText() {
074                return anchor.getText();
075        }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public void setIcon(IconType type) {
082        setBaseIcon(type);
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public void setBaseIcon(BaseIconType type) {
090        anchor.setBaseIcon(type);
091    }
092
093    @Override
094    public void setIconSize(IconSize size) {
095        anchor.setIconSize(size);
096    }
097
098        public void setActive(boolean active) {
099
100                if (active)
101                        addStyleName(Constants.ACTIVE);
102                else
103                        removeStyleName(Constants.ACTIVE);
104        }
105        
106        public boolean isActive() {
107                return this.getStyleName().contains(Constants.ACTIVE);
108        }
109
110        public void setDisabled(boolean disabled) {
111                
112                if (disabled)
113                        addStyleName(Constants.DISABLED);
114                else
115                        removeStyleName(Constants.DISABLED);
116                
117                anchor.setEnabled(!disabled);
118        }
119        
120        public boolean isDisabled() {
121                return !anchor.isEnabled();
122        }
123
124        public IconAnchor getAnchor() {
125                return anchor;
126        }
127
128        /**
129         * {@inheritDoc}
130         */
131        public HandlerRegistration addClickHandler(ClickHandler handler) {
132                return anchor.addDomHandler(handler, ClickEvent.getType());
133        }
134
135        /**
136         * Add widget to inner anchor
137         * {@inheritDoc}
138         */
139        @Override
140        public void add(Widget w) {
141                anchor.add(w);
142        }
143        
144        /**
145         * Add widget to this widget
146         * @param w
147         */
148        @UiChild(tagname="widget")
149        public void addWidget(Widget w) {
150            super.add(w);
151        }
152        
153        /**
154         * {@inheritDoc}
155         */
156        @Override
157        public void clear() {
158                anchor.clear();
159        }
160        
161        /**
162         * Set anchor target attribute.
163         * @param target target name
164         */
165        public void setTarget(String target) {
166            anchor.setTarget(target);
167        }
168        
169        /**
170         * Get anchor target attribute.
171         * @return target name
172         */
173        public String getTarget() {
174            return anchor.getTarget();
175        }
176        
177        /**
178         * Set anchor name
179         * @param name anchor name
180         */
181        public void setName(String name) {
182            anchor.setName(name);
183        }
184        
185        /**
186         * Get anchor name
187         * @return anchor name
188         */
189        public String getName() {
190            return anchor.getName();
191        }
192
193    @Override
194    public void setCustomIconStyle(String customIconStyle) {
195        anchor.setCustomIconStyle(customIconStyle);
196    }
197
198    @Override
199    public void setIconPosition(IconPosition position) {
200        anchor.setIconPosition(position);
201    }
202}