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 */
016 package com.github.gwtbootstrap.client.ui;
017
018 import com.github.gwtbootstrap.client.ui.base.HasIcon;
019 import com.github.gwtbootstrap.client.ui.base.IconAnchor;
020 import com.github.gwtbootstrap.client.ui.base.ListItem;
021 import com.github.gwtbootstrap.client.ui.constants.Constants;
022 import com.github.gwtbootstrap.client.ui.constants.IconSize;
023 import com.github.gwtbootstrap.client.ui.constants.IconType;
024 import com.google.gwt.event.dom.client.ClickEvent;
025 import com.google.gwt.event.dom.client.ClickHandler;
026 import com.google.gwt.event.dom.client.HasClickHandlers;
027 import com.google.gwt.event.shared.HandlerRegistration;
028 import com.google.gwt.user.client.ui.Widget;
029
030 //@formatter:off
031 /**
032 * A Container for Widgets in a Nav context (Navbar, NavList, ...). Example:<br>
033 * <br>
034 *
035 * {@code <b:NavWidget text="Inbox"><b:Badge /></b:NavWidget>}
036 *
037 * @since 2.0.4.0
038 * @author Dominik Mayer
039 *
040 * @see <a href="http://twitter.github.com/bootstrap/components.html#navbar">Bootstrap documentation (Navbar)</a>
041 * @see <a href="http://twitter.github.com/bootstrap/components.html#navs">Bootstrap documentation (Navs)</a>
042 * @see NavList
043 * @see WellNavList
044 * @see Dropdown
045 * @see Navbar
046 * @see ResponsiveNavbar
047 */
048 //@formatter:on
049 public class NavWidget extends ListItem implements HasClickHandlers, HasIcon {
050
051 private final IconAnchor anchor = new IconAnchor();
052
053 public NavWidget() {
054 super.add(anchor);
055 }
056
057 public NavWidget(Widget w) {
058 this();
059 add(w);
060 }
061
062 public void setHref(String href) {
063 anchor.setHref(href);
064 }
065
066 public void setTargetHistoryToken(String targetHistoryToken) {
067 anchor.setTargetHistoryToken(targetHistoryToken);
068 }
069
070 public void setText(String text) {
071 anchor.setText(text);
072 }
073
074 public String getText() {
075 return anchor.getText();
076 }
077
078 public void setIcon(IconType type) {
079 anchor.setIcon(type);
080 }
081
082 @Override
083 public void setIconSize(IconSize size) {
084 anchor.setIconSize(size);
085 }
086
087 public void setActive(boolean active) {
088
089 if (active)
090 addStyleName(Constants.ACTIVE);
091 else
092 removeStyleName(Constants.ACTIVE);
093 }
094
095 public boolean isActive() {
096 return this.getStyleName().contains(Constants.ACTIVE);
097 }
098
099 public void setDisabled(boolean disabled) {
100
101 if (disabled)
102 addStyleName(Constants.DISABLED);
103 else
104 removeStyleName(Constants.DISABLED);
105
106 anchor.setEnabled(!disabled);
107 }
108
109 public boolean isDisabled() {
110 return !anchor.isEnabled();
111 }
112
113 public IconAnchor getAnchor() {
114 return anchor;
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 public HandlerRegistration addClickHandler(ClickHandler handler) {
121 return anchor.addDomHandler(handler, ClickEvent.getType());
122 }
123
124 /**
125 * {@inheritDoc}
126 */
127 @Override
128 public void add(Widget w) {
129 anchor.add(w);
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 @Override
136 public void clear() {
137 anchor.clear();
138 }
139 }