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.Constants;
022import com.github.gwtbootstrap.client.ui.constants.IconSize;
023import com.github.gwtbootstrap.client.ui.constants.IconType;
024import com.google.gwt.event.dom.client.ClickEvent;
025import com.google.gwt.event.dom.client.ClickHandler;
026import com.google.gwt.event.dom.client.HasClickHandlers;
027import com.google.gwt.event.shared.HandlerRegistration;
028import com.google.gwt.uibinder.client.UiChild;
029import com.google.gwt.user.client.ui.Widget;
030
031//@formatter:off
032/**
033 * A Container for Widgets in a Nav context (Navbar, NavList, ...). Example:<br>
034 * <br>
035 * 
036 * {@code <b:NavWidget text="Inbox"><b:Badge /></b:NavWidget>}
037 * 
038 * @since 2.0.4.0
039 * @author Dominik Mayer
040 * 
041 * @see <a href="http://twitter.github.com/bootstrap/components.html#navbar">Bootstrap documentation (Navbar)</a>
042 * @see <a href="http://twitter.github.com/bootstrap/components.html#navs">Bootstrap documentation (Navs)</a>
043 * @see NavList
044 * @see WellNavList
045 * @see Dropdown
046 * @see Navbar
047 * @see ResponsiveNavbar
048 */
049//@formatter:on
050public class NavWidget extends ListItem implements HasClickHandlers, HasIcon {
051
052        private final IconAnchor anchor = new IconAnchor();
053
054        public NavWidget() {
055                super.add(anchor);
056        }
057
058        public NavWidget(Widget w) {
059                this();
060                add(w);
061        }
062
063        public void setHref(String href) {
064                anchor.setHref(href);
065        }
066
067        public void setTargetHistoryToken(String targetHistoryToken) {
068                anchor.setTargetHistoryToken(targetHistoryToken);
069        }
070
071        public void setText(String text) {
072                anchor.setText(text);
073        }
074
075        public String getText() {
076                return anchor.getText();
077        }
078
079        public void setIcon(IconType type) {
080                anchor.setIcon(type);
081        }
082
083    @Override
084    public void setIconSize(IconSize size) {
085        anchor.setIconSize(size);
086    }
087
088        public void setActive(boolean active) {
089
090                if (active)
091                        addStyleName(Constants.ACTIVE);
092                else
093                        removeStyleName(Constants.ACTIVE);
094        }
095        
096        public boolean isActive() {
097                return this.getStyleName().contains(Constants.ACTIVE);
098        }
099
100        public void setDisabled(boolean disabled) {
101                
102                if (disabled)
103                        addStyleName(Constants.DISABLED);
104                else
105                        removeStyleName(Constants.DISABLED);
106                
107                anchor.setEnabled(!disabled);
108        }
109        
110        public boolean isDisabled() {
111                return !anchor.isEnabled();
112        }
113
114        public IconAnchor getAnchor() {
115                return anchor;
116        }
117
118        /**
119         * {@inheritDoc}
120         */
121        public HandlerRegistration addClickHandler(ClickHandler handler) {
122                return anchor.addDomHandler(handler, ClickEvent.getType());
123        }
124
125        /**
126         * Add widget to inner anchor
127         * {@inheritDoc}
128         */
129        @Override
130        public void add(Widget w) {
131                anchor.add(w);
132        }
133        
134        /**
135         * Add widget to this widget
136         * @param w
137         */
138        @UiChild(tagname="widget")
139        public void addWidget(Widget w) {
140            super.add(w);
141        }
142        
143        /**
144         * {@inheritDoc}
145         */
146        @Override
147        public void clear() {
148                anchor.clear();
149        }
150        
151        /**
152         * Set anchor target attribute.
153         * @param target target name
154         */
155        public void setTarget(String target) {
156            anchor.setTarget(target);
157        }
158        
159        /**
160         * Get anchor target attribute.
161         * @return target name
162         */
163        public String getTarget() {
164            return anchor.getTarget();
165        }
166        
167        /**
168         * Set anchor name
169         * @param name anchor name
170         */
171        public void setName(String name) {
172            anchor.setName(name);
173        }
174        
175        /**
176         * Get anchor name
177         * @return anchor name
178         */
179        public String getName() {
180            return anchor.getName();
181        }
182
183    @Override
184    public void setCustomIconStyle(String customIconStyle) {
185        anchor.setCustomIconStyle(customIconStyle);
186    }
187}