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.StyleHelper; 019import com.github.gwtbootstrap.client.ui.constants.Constants; 020import com.google.gwt.core.client.Scheduler; 021import com.google.gwt.core.client.Scheduler.ScheduledCommand; 022import com.google.gwt.user.client.DOM; 023import com.google.gwt.user.client.Element; 024import com.google.gwt.user.client.ui.HasEnabled; 025 026/** 027 * The TabLink for {@link TabPanel} 028 * @author ohashi keisuke 029 * 030 */ 031public class TabLink extends NavWidget implements HasEnabled { 032 033 private TabPane pane; 034 private boolean createTabPane = true; 035 private boolean enabled; 036 037 /** 038 * Create widget with set Effective TabPane 039 * @param pane effective tabPane 040 */ 041 public TabLink(TabPane pane) { 042 this(); 043 setText(pane.getHeading()); 044 setTabPane(pane); 045 } 046 047 /** 048 * Create empty widget 049 */ 050 public TabLink() { 051 super(); 052 enabled = true; 053 getAnchor().getElement().setAttribute(Constants.DATA_TOGGLE, "tab"); 054 } 055 056 /** 057 * Returns true if the widget is enabled, false if not. 058 */ 059 @Override 060 public boolean isEnabled() { 061 return enabled; 062 } 063 064 /** 065 * Sets whether this widget is enabled. 066 * 067 * @param enabled <code>true</code> to enable the widget, <code>false</code> 068 * to disable it 069 */ 070 @Override 071 public void setEnabled(boolean enabled) { 072 if (enabled && !this.enabled) { 073 this.enabled = true; 074 getAnchor().getElement().setAttribute(Constants.DATA_TOGGLE, "tab"); 075 removeStyleName(Constants.DISABLED); 076 } else if (!enabled && this.enabled) { 077 this.enabled = false; 078 getAnchor().getElement().removeAttribute(Constants.DATA_TOGGLE); 079 addStyleName(Constants.DISABLED); 080 } 081 } 082 083 public void setCreateTabPane(boolean createTabPane) { 084 this.createTabPane = createTabPane; 085 } 086 087 public boolean isCreateTabPane() { 088 return this.createTabPane; 089 } 090 091 /** 092 * Set Effective TabPane 093 * @param pane 094 */ 095 public void setTabPane(TabPane pane) { 096 this.pane = pane; 097 098 if(pane.getId() == null || pane.getId().isEmpty()) { 099 pane.setHref(DOM.createUniqueId()); 100 } 101 102 setDataTarget(pane.getId()); 103 104 this.setActive(pane.isActive()); 105 } 106 107 public void setDataTarget(String id) { 108 getAnchor().getElement().setAttribute(Constants.DATA_TARGET,"#" + id); 109 } 110 111 /** 112 * Get Effective TabPane 113 * @return effective TabPane 114 */ 115 public TabPane getTabPane() { 116 return pane; 117 } 118 119 @Override 120 protected void onAttach() { 121 super.onAttach(); 122 123 if(isActive()) { 124 show(); 125 } 126 } 127 128 @Override 129 public void setActive(boolean active) { 130 super.setActive(active); 131 132 if(pane != null) { 133 pane.setActive(active); 134 } 135 136 } 137 138 /** 139 * show tab pane 140 */ 141 public void show() { 142 if(isOrWasAttached()) { 143 show(getAnchor().getElement()); 144 return; 145 } 146 147 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 148 149 @Override 150 public void execute() { 151 show(getAnchor().getElement()); 152 } 153 }); 154 } 155 156 //@formatter:off 157 private native void show(Element e)/*-{ 158 $wnd.jQuery(e).tab('show'); 159 }-*/; 160 //@formatter:on 161 162}