Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/nonpublic/WidgetTabBar.java

Diff revisions: vs.
  @@ -1,255 +1,255 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.GWT.client.widgets.nonpublic;
3 -
4 - import org.litesoft.commonfoundation.html.*;
5 -
6 - import com.google.gwt.user.client.*;
7 - import com.google.gwt.user.client.ui.*;
8 -
9 - /**
10 - * Derived from com.google.gwt.user.client.ui.TabBar
11 - * because they did not allow direct access to the underlying HorizontalPanel.
12 - * <p/>
13 - * A horizontal bar of folder-style tabs, most commonly used as part of a
14 - * {@link com.google.gwt.user.client.ui.TabPanel}.
15 - * <p/>
16 - * <img class='gallery' src='TabBar.png'/>
17 - * </p>
18 - * <h3>CSS Style Rules</h3>
19 - * <ul class='css'>
20 - * <li>.gwt-TabBar { the tab bar itself }</li>
21 - * <li>.gwt-TabBar .gwt-TabBarFirst { the left edge of the bar }</li>
22 - * <li>.gwt-TabBar .gwt-TabBarRest { the right edge of the bar }</li>
23 - * <li>.gwt-TabBar .gwt-TabBarItem { unselected tabs }</li>
24 - * <li>.gwt-TabBar .gwt-TabBarItem-selected { additional style for selected
25 - * tabs } </li>
26 - * </ul>
27 - * <p/>
28 - * </p>
29 - */
30 - public class WidgetTabBar extends Composite implements SourcesTabEvents,
31 - ClickListener {
32 -
33 - private HorizontalPanel panel = new HorizontalPanel();
34 - private Widget selectedTab;
35 - private TabListenerCollection tabListeners;
36 -
37 - /**
38 - * Creates an empty tab bar.
39 - */
40 - public WidgetTabBar() {
41 - initWidget( panel );
42 - sinkEvents( Event.ONCLICK );
43 - //noinspection GWTStyleCheck
44 - setStyleName( "gwt-TabBar" );
45 -
46 - panel.setVerticalAlignment( HasVerticalAlignment.ALIGN_BOTTOM );
47 -
48 - HTML first = new HTML( HTMLConstants.NBSP, true ), rest = new HTML( HTMLConstants.NBSP, true );
49 - //noinspection GWTStyleCheck
50 - first.setStyleName( "gwt-TabBarFirst" );
51 - //noinspection GWTStyleCheck
52 - rest.setStyleName( "gwt-TabBarRest" );
53 - // first.setHeight( "100%" );
54 - // rest.setHeight( "100%" );
55 -
56 - panel.add( first );
57 - panel.add( rest );
58 - // first.setHeight( "100%" );
59 - panel.setCellHeight( first, "100%" );
60 - panel.setCellWidth( rest, "100%" );
61 - }
62 -
63 - /**
64 - * Adds a new tab with the specified text.
65 - *
66 - * @param text the new tab's text
67 - */
68 - public void addTab( String text ) {
69 - insertTab( text, getTabCount() );
70 - }
71 -
72 - /**
73 - * Adds a new tab with the specified text.
74 - *
75 - * @param text the new tab's text
76 - * @param asHTML <code>true</code> to treat the specified text as html
77 - */
78 - public void addTab( String text, boolean asHTML ) {
79 - insertTab( text, asHTML, getTabCount() );
80 - }
81 -
82 - @Override
83 - public void addTabListener( TabListener listener ) {
84 - if ( tabListeners == null ) {
85 - tabListeners = new TabListenerCollection();
86 - }
87 - tabListeners.add( listener );
88 - }
89 -
90 - /**
91 - * Gets the tab that is currently selected.
92 - *
93 - * @return the selected tab
94 - */
95 - public int getSelectedTab() {
96 - if ( selectedTab == null ) {
97 - return -1;
98 - }
99 - return panel.getWidgetIndex( selectedTab ) - 1;
100 - }
101 -
102 - /**
103 - * Gets the number of tabs present.
104 - *
105 - * @return the tab count
106 - */
107 - public int getTabCount() {
108 - return panel.getWidgetCount() - 2;
109 - }
110 -
111 - /**
112 - * Gets the specified tab's HTML.
113 - *
114 - * @param index the index of the tab whose HTML is to be retrieved
115 - *
116 - * @return the tab's HTML
117 - */
118 - public String getTabHTML( int index ) {
119 - if ( index >= getTabCount() ) {
120 - return null;
121 - }
122 - Widget widget = panel.getWidget( index + 1 );
123 - if ( widget instanceof HTML ) {
124 - return ((HTML) widget).getHTML();
125 - } else {
126 - return ((Label) widget).getText();
127 - }
128 - }
129 -
130 - public Widget getWidget( int pIndex ) {
131 - return panel.getWidget( pIndex + 1 );
132 - }
133 -
134 - /**
135 - * Inserts a new tab at the specified index.
136 - *
137 - * @param text the new tab's text
138 - * @param asHTML <code>true</code> to treat the specified text as HTML
139 - * @param beforeIndex the index before which this tab will be inserted
140 - */
141 - public void insertTab( String text, boolean asHTML, int beforeIndex ) {
142 - if ( (beforeIndex < 0) || (beforeIndex > getTabCount()) ) {
143 - throw new IndexOutOfBoundsException( String.valueOf( beforeIndex ) );
144 - }
145 -
146 - Label item;
147 - if ( asHTML ) {
148 - item = new HTML( text );
149 - } else {
150 - item = new Label( text );
151 - }
152 -
153 - item.setWordWrap( false );
154 - item.addClickListener( this );
155 - //noinspection GWTStyleCheck
156 - item.setStyleName( "gwt-TabBarItem" );
157 - item.addStyleName( "gwt-TabBarItem-NonError" );
158 - panel.insert( item, beforeIndex + 1 );
159 - }
160 -
161 - /**
162 - * Inserts a new tab at the specified index.
163 - *
164 - * @param text the new tab's text
165 - * @param beforeIndex the index before which this tab will be inserted
166 - */
167 - public void insertTab( String text, int beforeIndex ) {
168 - insertTab( text, false, beforeIndex );
169 - }
170 -
171 - @Override
172 - public void onClick( Widget sender ) {
173 - for ( int i = 1; i < panel.getWidgetCount() - 1; ++i ) {
174 - if ( panel.getWidget( i ) == sender ) {
175 - selectTab( i - 1 );
176 - return;
177 - }
178 - }
179 - }
180 -
181 - /**
182 - * Removes the tab at the specified index.
183 - *
184 - * @param index the index of the tab to be removed
185 - */
186 - public void removeTab( int index ) {
187 - checkTabIndex( index );
188 -
189 - // (index + 1) to account for 'first' placeholder widget.
190 - Widget toRemove = panel.getWidget( index + 1 );
191 - if ( toRemove == selectedTab ) {
192 - selectedTab = null;
193 - }
194 - panel.remove( toRemove );
195 - }
196 -
197 - @Override
198 - public void removeTabListener( TabListener listener ) {
199 - if ( tabListeners != null ) {
200 - tabListeners.remove( listener );
201 - }
202 - }
203 -
204 - /**
205 - * Programmatically selects the specified tab. Use index -1 to specify that no
206 - * tab should be selected.
207 - *
208 - * @param index the index of the tab to be selected.
209 - *
210 - * @return <code>true</code> if successful, <code>false</code> if the
211 - * change is denied by the {@link TabListener}.
212 - */
213 - public boolean selectTab( int index ) {
214 - checkTabIndex( index );
215 -
216 - if ( tabListeners != null ) {
217 - if ( !tabListeners.fireBeforeTabSelected( this, index ) ) {
218 - return false;
219 - }
220 - }
221 -
222 - // Check for -1.
223 - setSelectionStyle( selectedTab, false );
224 - if ( index == -1 ) {
225 - selectedTab = null;
226 - return true;
227 - }
228 -
229 - selectedTab = panel.getWidget( index + 1 );
230 - setSelectionStyle( selectedTab, true );
231 -
232 - if ( tabListeners != null ) {
233 - tabListeners.fireTabSelected( this, index );
234 - }
235 - return true;
236 - }
237 -
238 - private void checkTabIndex( int index ) {
239 - if ( (index < -1) || (index >= getTabCount()) ) {
240 - throw new IndexOutOfBoundsException( String.valueOf( index ) );
241 - }
242 - }
243 -
244 - private void setSelectionStyle( Widget item, boolean selected ) {
245 - if ( item != null ) {
246 - if ( selected ) {
247 - //noinspection GWTStyleCheck
248 - item.addStyleName( "gwt-TabBarItem-selected" );
249 - } else {
250 - //noinspection GWTStyleCheck
251 - item.removeStyleName( "gwt-TabBarItem-selected" );
252 - }
253 - }
254 - }
255 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.GWT.client.widgets.nonpublic;
3 +
4 + import org.litesoft.commonfoundation.html.*;
5 +
6 + import com.google.gwt.user.client.*;
7 + import com.google.gwt.user.client.ui.*;
8 +
9 + /**
10 + * Derived from com.google.gwt.user.client.ui.TabBar
11 + * because they did not allow direct access to the underlying HorizontalPanel.
12 + * <p/>
13 + * A horizontal bar of folder-style tabs, most commonly used as part of a
14 + * {@link com.google.gwt.user.client.ui.TabPanel}.
15 + * <p/>
16 + * <img class='gallery' src='TabBar.png'/>
17 + * </p>
18 + * <h3>CSS Style Rules</h3>
19 + * <ul class='css'>
20 + * <li>.gwt-TabBar { the tab bar itself }</li>
21 + * <li>.gwt-TabBar .gwt-TabBarFirst { the left edge of the bar }</li>
22 + * <li>.gwt-TabBar .gwt-TabBarRest { the right edge of the bar }</li>
23 + * <li>.gwt-TabBar .gwt-TabBarItem { unselected tabs }</li>
24 + * <li>.gwt-TabBar .gwt-TabBarItem-selected { additional style for selected
25 + * tabs } </li>
26 + * </ul>
27 + * <p/>
28 + * </p>
29 + */
30 + public class WidgetTabBar extends Composite implements SourcesTabEvents,
31 + ClickListener {
32 +
33 + private HorizontalPanel panel = new HorizontalPanel();
34 + private Widget selectedTab;
35 + private TabListenerCollection tabListeners;
36 +
37 + /**
38 + * Creates an empty tab bar.
39 + */
40 + public WidgetTabBar() {
41 + initWidget( panel );
42 + sinkEvents( Event.ONCLICK );
43 + //noinspection GWTStyleCheck
44 + setStyleName( "gwt-TabBar" );
45 +
46 + panel.setVerticalAlignment( HasVerticalAlignment.ALIGN_BOTTOM );
47 +
48 + HTML first = new HTML( HtmlEntity.NBSP, true ), rest = new HTML( HtmlEntity.NBSP, true );
49 + //noinspection GWTStyleCheck
50 + first.setStyleName( "gwt-TabBarFirst" );
51 + //noinspection GWTStyleCheck
52 + rest.setStyleName( "gwt-TabBarRest" );
53 + // first.setHeight( "100%" );
54 + // rest.setHeight( "100%" );
55 +
56 + panel.add( first );
57 + panel.add( rest );
58 + // first.setHeight( "100%" );
59 + panel.setCellHeight( first, "100%" );
60 + panel.setCellWidth( rest, "100%" );
61 + }
62 +
63 + /**
64 + * Adds a new tab with the specified text.
65 + *
66 + * @param text the new tab's text
67 + */
68 + public void addTab( String text ) {
69 + insertTab( text, getTabCount() );
70 + }
71 +
72 + /**
73 + * Adds a new tab with the specified text.
74 + *
75 + * @param text the new tab's text
76 + * @param asHTML <code>true</code> to treat the specified text as html
77 + */
78 + public void addTab( String text, boolean asHTML ) {
79 + insertTab( text, asHTML, getTabCount() );
80 + }
81 +
82 + @Override
83 + public void addTabListener( TabListener listener ) {
84 + if ( tabListeners == null ) {
85 + tabListeners = new TabListenerCollection();
86 + }
87 + tabListeners.add( listener );
88 + }
89 +
90 + /**
91 + * Gets the tab that is currently selected.
92 + *
93 + * @return the selected tab
94 + */
95 + public int getSelectedTab() {
96 + if ( selectedTab == null ) {
97 + return -1;
98 + }
99 + return panel.getWidgetIndex( selectedTab ) - 1;
100 + }
101 +
102 + /**
103 + * Gets the number of tabs present.
104 + *
105 + * @return the tab count
106 + */
107 + public int getTabCount() {
108 + return panel.getWidgetCount() - 2;
109 + }
110 +
111 + /**
112 + * Gets the specified tab's HTML.
113 + *
114 + * @param index the index of the tab whose HTML is to be retrieved
115 + *
116 + * @return the tab's HTML
117 + */
118 + public String getTabHTML( int index ) {
119 + if ( index >= getTabCount() ) {
120 + return null;
121 + }
122 + Widget widget = panel.getWidget( index + 1 );
123 + if ( widget instanceof HTML ) {
124 + return ((HTML) widget).getHTML();
125 + } else {
126 + return ((Label) widget).getText();
127 + }
128 + }
129 +
130 + public Widget getWidget( int pIndex ) {
131 + return panel.getWidget( pIndex + 1 );
132 + }
133 +
134 + /**
135 + * Inserts a new tab at the specified index.
136 + *
137 + * @param text the new tab's text
138 + * @param asHTML <code>true</code> to treat the specified text as HTML
139 + * @param beforeIndex the index before which this tab will be inserted
140 + */
141 + public void insertTab( String text, boolean asHTML, int beforeIndex ) {
142 + if ( (beforeIndex < 0) || (beforeIndex > getTabCount()) ) {
143 + throw new IndexOutOfBoundsException( String.valueOf( beforeIndex ) );
144 + }
145 +
146 + Label item;
147 + if ( asHTML ) {
148 + item = new HTML( text );
149 + } else {
150 + item = new Label( text );
151 + }
152 +
153 + item.setWordWrap( false );
154 + item.addClickListener( this );
155 + //noinspection GWTStyleCheck
156 + item.setStyleName( "gwt-TabBarItem" );
157 + item.addStyleName( "gwt-TabBarItem-NonError" );
158 + panel.insert( item, beforeIndex + 1 );
159 + }
160 +
161 + /**
162 + * Inserts a new tab at the specified index.
163 + *
164 + * @param text the new tab's text
165 + * @param beforeIndex the index before which this tab will be inserted
166 + */
167 + public void insertTab( String text, int beforeIndex ) {
168 + insertTab( text, false, beforeIndex );
169 + }
170 +
171 + @Override
172 + public void onClick( Widget sender ) {
173 + for ( int i = 1; i < panel.getWidgetCount() - 1; ++i ) {
174 + if ( panel.getWidget( i ) == sender ) {
175 + selectTab( i - 1 );
176 + return;
177 + }
178 + }
179 + }
180 +
181 + /**
182 + * Removes the tab at the specified index.
183 + *
184 + * @param index the index of the tab to be removed
185 + */
186 + public void removeTab( int index ) {
187 + checkTabIndex( index );
188 +
189 + // (index + 1) to account for 'first' placeholder widget.
190 + Widget toRemove = panel.getWidget( index + 1 );
191 + if ( toRemove == selectedTab ) {
192 + selectedTab = null;
193 + }
194 + panel.remove( toRemove );
195 + }
196 +
197 + @Override
198 + public void removeTabListener( TabListener listener ) {
199 + if ( tabListeners != null ) {
200 + tabListeners.remove( listener );
201 + }
202 + }
203 +
204 + /**
205 + * Programmatically selects the specified tab. Use index -1 to specify that no
206 + * tab should be selected.
207 + *
208 + * @param index the index of the tab to be selected.
209 + *
210 + * @return <code>true</code> if successful, <code>false</code> if the
211 + * change is denied by the {@link TabListener}.
212 + */
213 + public boolean selectTab( int index ) {
214 + checkTabIndex( index );
215 +
216 + if ( tabListeners != null ) {
217 + if ( !tabListeners.fireBeforeTabSelected( this, index ) ) {
218 + return false;
219 + }
220 + }
221 +
222 + // Check for -1.
223 + setSelectionStyle( selectedTab, false );
224 + if ( index == -1 ) {
225 + selectedTab = null;
226 + return true;
227 + }
228 +
229 + selectedTab = panel.getWidget( index + 1 );
230 + setSelectionStyle( selectedTab, true );
231 +
232 + if ( tabListeners != null ) {
233 + tabListeners.fireTabSelected( this, index );
234 + }
235 + return true;
236 + }
237 +
238 + private void checkTabIndex( int index ) {
239 + if ( (index < -1) || (index >= getTabCount()) ) {
240 + throw new IndexOutOfBoundsException( String.valueOf( index ) );
241 + }
242 + }
243 +
244 + private void setSelectionStyle( Widget item, boolean selected ) {
245 + if ( item != null ) {
246 + if ( selected ) {
247 + //noinspection GWTStyleCheck
248 + item.addStyleName( "gwt-TabBarItem-selected" );
249 + } else {
250 + //noinspection GWTStyleCheck
251 + item.removeStyleName( "gwt-TabBarItem-selected" );
252 + }
253 + }
254 + }
255 + }