Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.GWT.client.widgets.nonpublic;

import org.litesoft.commonfoundation.html.*;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;

/**
 * Derived from  com.google.gwt.user.client.ui.TabBar
 * because they did not allow direct access to the underlying HorizontalPanel.
 * <p/>
 * A horizontal bar of folder-style tabs, most commonly used as part of a
 * {@link com.google.gwt.user.client.ui.TabPanel}.
 * <p/>
 * <img class='gallery' src='TabBar.png'/>
 * </p>
 * <h3>CSS Style Rules</h3>
 * <ul class='css'>
 * <li>.gwt-TabBar { the tab bar itself }</li>
 * <li>.gwt-TabBar .gwt-TabBarFirst { the left edge of the bar }</li>
 * <li>.gwt-TabBar .gwt-TabBarRest { the right edge of the bar }</li>
 * <li>.gwt-TabBar .gwt-TabBarItem { unselected tabs }</li>
 * <li>.gwt-TabBar .gwt-TabBarItem-selected { additional style for selected
 * tabs } </li>
 * </ul>
 * <p/>
 * </p>
 */
public class WidgetTabBar extends Composite implements SourcesTabEvents,
                                                       ClickListener {

    private HorizontalPanel panel = new HorizontalPanel();
    private Widget selectedTab;
    private TabListenerCollection tabListeners;

    /**
     * Creates an empty tab bar.
     */
    public WidgetTabBar() {
        initWidget( panel );
        sinkEvents( Event.ONCLICK );
        //noinspection GWTStyleCheck
        setStyleName( "gwt-TabBar" );

        panel.setVerticalAlignment( HasVerticalAlignment.ALIGN_BOTTOM );

        HTML first = new HTML( HTMLConstants.NBSP, true ), rest = new HTML( HTMLConstants.NBSP, true );
        //noinspection GWTStyleCheck
        first.setStyleName( "gwt-TabBarFirst" );
        //noinspection GWTStyleCheck
        rest.setStyleName( "gwt-TabBarRest" );
//        first.setHeight( "100%" );
//        rest.setHeight( "100%" );

        panel.add( first );
        panel.add( rest );
//        first.setHeight( "100%" );
        panel.setCellHeight( first, "100%" );
        panel.setCellWidth( rest, "100%" );
    }

    /**
     * Adds a new tab with the specified text.
     *
     * @param text the new tab's text
     */
    public void addTab( String text ) {
        insertTab( text, getTabCount() );
    }

    /**
     * Adds a new tab with the specified text.
     *
     * @param text   the new tab's text
     * @param asHTML <code>true</code> to treat the specified text as html
     */
    public void addTab( String text, boolean asHTML ) {
        insertTab( text, asHTML, getTabCount() );
    }

    @Override
    public void addTabListener( TabListener listener ) {
        if ( tabListeners == null ) {
            tabListeners = new TabListenerCollection();
        }
        tabListeners.add( listener );
    }

    /**
     * Gets the tab that is currently selected.
     *
     * @return the selected tab
     */
    public int getSelectedTab() {
        if ( selectedTab == null ) {
            return -1;
        }
        return panel.getWidgetIndex( selectedTab ) - 1;
    }

    /**
     * Gets the number of tabs present.
     *
     * @return the tab count
     */
    public int getTabCount() {
        return panel.getWidgetCount() - 2;
    }

    /**
     * Gets the specified tab's HTML.
     *
     * @param index the index of the tab whose HTML is to be retrieved
     *
     * @return the tab's HTML
     */
    public String getTabHTML( int index ) {
        if ( index >= getTabCount() ) {
            return null;
        }
        Widget widget = panel.getWidget( index + 1 );
        if ( widget instanceof HTML ) {
            return ((HTML) widget).getHTML();
        } else {
            return ((Label) widget).getText();
        }
    }

    public Widget getWidget( int pIndex ) {
        return panel.getWidget( pIndex + 1 );
    }

    /**
     * Inserts a new tab at the specified index.
     *
     * @param text        the new tab's text
     * @param asHTML      <code>true</code> to treat the specified text as HTML
     * @param beforeIndex the index before which this tab will be inserted
     */
    public void insertTab( String text, boolean asHTML, int beforeIndex ) {
        if ( (beforeIndex < 0) || (beforeIndex > getTabCount()) ) {
            throw new IndexOutOfBoundsException( String.valueOf( beforeIndex ) );
        }

        Label item;
        if ( asHTML ) {
            item = new HTML( text );
        } else {
            item = new Label( text );
        }

        item.setWordWrap( false );
        item.addClickListener( this );
        //noinspection GWTStyleCheck
        item.setStyleName( "gwt-TabBarItem" );
        item.addStyleName( "gwt-TabBarItem-NonError" );
        panel.insert( item, beforeIndex + 1 );
    }

    /**
     * Inserts a new tab at the specified index.
     *
     * @param text        the new tab's text
     * @param beforeIndex the index before which this tab will be inserted
     */
    public void insertTab( String text, int beforeIndex ) {
        insertTab( text, false, beforeIndex );
    }

    @Override
    public void onClick( Widget sender ) {
        for ( int i = 1; i < panel.getWidgetCount() - 1; ++i ) {
            if ( panel.getWidget( i ) == sender ) {
                selectTab( i - 1 );
                return;
            }
        }
    }

    /**
     * Removes the tab at the specified index.
     *
     * @param index the index of the tab to be removed
     */
    public void removeTab( int index ) {
        checkTabIndex( index );

        // (index + 1) to account for 'first' placeholder widget.
        Widget toRemove = panel.getWidget( index + 1 );
        if ( toRemove == selectedTab ) {
            selectedTab = null;
        }
        panel.remove( toRemove );
    }

    @Override
    public void removeTabListener( TabListener listener ) {
        if ( tabListeners != null ) {
            tabListeners.remove( listener );
        }
    }

    /**
     * Programmatically selects the specified tab. Use index -1 to specify that no
     * tab should be selected.
     *
     * @param index the index of the tab to be selected.
     *
     * @return <code>true</code> if successful, <code>false</code> if the
     * change is denied by the {@link TabListener}.
     */
    public boolean selectTab( int index ) {
        checkTabIndex( index );

        if ( tabListeners != null ) {
            if ( !tabListeners.fireBeforeTabSelected( this, index ) ) {
                return false;
            }
        }

        // Check for -1.
        setSelectionStyle( selectedTab, false );
        if ( index == -1 ) {
            selectedTab = null;
            return true;
        }

        selectedTab = panel.getWidget( index + 1 );
        setSelectionStyle( selectedTab, true );

        if ( tabListeners != null ) {
            tabListeners.fireTabSelected( this, index );
        }
        return true;
    }

    private void checkTabIndex( int index ) {
        if ( (index < -1) || (index >= getTabCount()) ) {
            throw new IndexOutOfBoundsException( String.valueOf( index ) );
        }
    }

    private void setSelectionStyle( Widget item, boolean selected ) {
        if ( item != null ) {
            if ( selected ) {
                //noinspection GWTStyleCheck
                item.addStyleName( "gwt-TabBarItem-selected" );
            } else {
                //noinspection GWTStyleCheck
                item.removeStyleName( "gwt-TabBarItem-selected" );
            }
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/client/widgets/nonpublic/WidgetTabBar.java

Diff revisions: vs.
Revision Author Commited Message
948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

712 Diff Diff GeorgeS picture GeorgeS Sat 09 Jun, 2012 22:46:04 +0000

Move PAV stuff into LiteSoft

49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

23 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 00:34:32 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000