001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.google.gwt.user.client.Element;
013: import com.gwtext.client.core.Position;
014: import com.gwtext.client.util.JavaScriptObjectHelper;
015: import com.gwtext.client.widgets.event.TabPanelListener;
016: import com.gwtext.client.widgets.layout.ContainerLayout;
017:
018: /**
019: * A lightweight tab container.
020: */
021: public class TabPanel extends Panel {
022:
023: private static JavaScriptObject configPrototype;
024:
025: static {
026: init();
027: }
028:
029: private static native void init()/*-{
030: var c = new $wnd.Ext.TabPanel();
031: @com.gwtext.client.widgets.TabPanel::configPrototype = c.initialConfig;
032: }-*/;
033:
034: protected JavaScriptObject getConfigPrototype() {
035: return configPrototype;
036: }
037:
038: public String getXType() {
039: return "tabpanel";
040: }
041:
042: public TabPanel() {
043: //by default enable this as its too painful to track down this commonly applicable setting
044: setLayoutOnTabChange(true);
045: setActiveTab(0);
046: }
047:
048: public TabPanel(JavaScriptObject jsObj) {
049: super (jsObj);
050: }
051:
052: private static TabPanel instance(JavaScriptObject jsObj) {
053: return new TabPanel(jsObj);
054: }
055:
056: protected native JavaScriptObject create(JavaScriptObject config) /*-{
057: return new $wnd.Ext.TabPanel(config);
058: }-*/;
059:
060: /**
061: * Return the min tab width.
062: *
063: * @return the min tab width
064: */
065: public native int getMinTabWidth() /*-{
066: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
067: return tp.minTabWidth;
068: }-*/;
069:
070: /**
071: * Set the min tab width.
072: *
073: * @param minTabWidth the min tab width
074: */
075: private native void setMinTabWidthRendered(int minTabWidth) /*-{
076: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
077: tp.minTabWidth = minTabWidth;
078: }-*/;
079:
080: public native boolean isResizeTabs() /*-{
081: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
082: return tp.resizeTabs;
083: }-*/;
084:
085: public native void setResizeTabsRendered(boolean resizeTabs) /*-{
086: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
087: tp.resizeTabs = resizeTabs;
088: }-*/;
089:
090: public native String getTabPosition() /*-{
091: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
092: return tp.tabPosition;
093: }-*/;
094:
095: public void setActiveItemID(String activeItem) {
096: if (isRendered()) {
097: activate(activeItem);
098: } else {
099: super .setActiveItemID(activeItem);
100: }
101: }
102:
103: public void setActiveItem(int activeItem) {
104: if (isRendered()) {
105: activate(activeItem);
106: } else {
107: super .setActiveItem(activeItem);
108: }
109: }
110:
111: public void setLayout(ContainerLayout layout) {
112: throw new IllegalArgumentException(
113: "The layout of TabPanel should not be changed.");
114: }
115:
116: /**
117: * Activates a tab panel. The currently active one will be deactivated.
118: *
119: * @param tabID the tab id
120: */
121: public void activate(String tabID) {
122: if (isRendered()) {
123: activateRendered(tabID);
124: } else {
125: setActiveTab(tabID);
126: }
127: }
128:
129: private native void activateRendered(String tabID) /*-{
130: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
131: tp.activate(tabID);
132: }-*/;
133:
134: /**
135: * Activates a tab panel. The currently active one will be deactivated.
136: *
137: * @param tabIndex the tab index
138: */
139: public void activate(int tabIndex) {
140: if (isRendered()) {
141: activateRendered(tabIndex);
142: } else {
143: setActiveTab(tabIndex);
144: }
145: }
146:
147: private native void activateRendered(int tabIndex) /*-{
148: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
149: tp.activate(tabIndex);
150: }-*/;
151:
152: /**
153: * Disables tab resizing while tabs are being added (if resizeTabs is false this does nothing).
154: */
155: public native void beginUpdate() /*-{
156: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
157: tp.beginUpdate();
158: }-*/;
159:
160: /**
161: * Stops an update and resizes the tabs (if resizeTabs is false this does nothing).
162: */
163: public native void endUpdate() /*-{
164: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
165: tp.endUpdate();
166: }-*/;
167:
168: /**
169: * Gets the currently active tab.
170: *
171: * @return the active tab
172: */
173: public native Panel getActiveTab() /*-{
174: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
175: var p = tp.getActiveTab();
176: return p == null || p === undefined ? null : @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(p);
177: }-*/;
178:
179: /**
180: * Gets the specified tab by id.
181: *
182: * @param id the tab id
183: * @return the specified tab
184: */
185: public native Panel getItem(String id) /*-{
186: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
187: var p = tp.getItem(id);
188: return p == null || p === undefined ? null : @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(p);
189: }-*/;
190:
191: /**
192: * Gets the DOM element for tab strip item which activates the child panel with the specified ID. Access this to
193: * change the visual treatment of the item, for example by changing the CSS class name.
194: *
195: * @param panel the panel
196: * @return the DOM node
197: */
198: public native Element getTabEl(Panel panel) /*-{
199: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
200: var panelJS = panel.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
201: return tp.getTabEl(panelJS);
202: }-*/;
203:
204: /**
205: * Hides the tab strip item for the passed tab.
206: *
207: * @param id the tab id
208: */
209: public native void hideTabStripItem(String id) /*-{
210: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
211: tp.hideTabStripItem(id);
212: }-*/;
213:
214: /**
215: * Hides the tab strip item for the passed tab.
216: *
217: * @param index the tab index
218: */
219: public native void hideTabStripItem(int index) /*-{
220: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
221: tp.hideTabStripItem(index);
222: }-*/;
223:
224: /**
225: * Hides the tab strip item for the passed tab.
226: *
227: * @param panel the tab panel
228: */
229: public native void hideTabStripItem(Panel panel) /*-{
230: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
231: var panelJS = panel.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
232: tp.hideTabStripItem(panelJS);
233: }-*/;
234:
235: /**
236: * Scrolls to a particular tab if tab scrolling is enabled.
237: *
238: * @param panel the panel to scroll to
239: * @param animate true to animate
240: */
241: public native void scrollToTab(Panel panel, boolean animate) /*-{
242: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
243: var panelJS = panel.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
244: tp.scrollToTab(panelJS, animate);
245: }-*/;
246:
247: /**
248: * Unhides the tab strip item for the passed tab.
249: *
250: * @param id the tab id
251: */
252: public native void unhideTabStripItem(String id) /*-{
253: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
254: tp.unhideTabStripItem(id);
255: }-*/;
256:
257: /**
258: * Unhides the tab strip item for the passed tab.
259: *
260: * @param index the tab index
261: */
262: public native void unhideTabStripItem(int index) /*-{
263: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
264: tp.unhideTabStripItem(index);
265: }-*/;
266:
267: /**
268: * Unhides the tab strip item for the passed tab.
269: *
270: * @param panel the tab panel
271: */
272: public native void unhideTabStripItem(Panel panel) /*-{
273: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
274: var panelJS = panel.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
275: tp.unhideTabStripItem(panelJS);
276: }-*/;
277:
278: /**
279: * Check if the TabPanel has the specified tab.
280: *
281: * @param id the tab id
282: * @return true if tab present
283: */
284: public native boolean hasItem(String id) /*-{
285: var tp = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
286: var p = tp.getItem(id);
287: return p != null && p !== undefined;
288: }-*/;
289:
290: /**
291: * Add a TabPanel listner.
292: *
293: * @param listener the listener
294: */
295: public native void addListener(TabPanelListener listener) /*-{
296: this.@com.gwtext.client.widgets.Panel::addListener(Lcom/gwtext/client/widgets/event/PanelListener;)(listener);
297: var tabPanelJ = this;
298:
299: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('beforetabchange',
300: function(source, newPanel, oldPanel) {
301: var newPanelJ = @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(newPanel);
302: var oldPanelJ = oldPanel == null || oldPanel === undefined ? null : @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(oldPanel);
303: return listener.@com.gwtext.client.widgets.event.TabPanelListener::doBeforeTabChange(Lcom/gwtext/client/widgets/TabPanel;Lcom/gwtext/client/widgets/Panel;Lcom/gwtext/client/widgets/Panel;)(tabPanelJ, newPanelJ, oldPanelJ);
304: }
305: );
306:
307: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('contextmenu',
308: function(source, tab, event) {
309: var tabJ = @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(tab);
310: var e = @com.gwtext.client.core.EventObject::instance(Lcom/google/gwt/core/client/JavaScriptObject;)(event);
311: listener.@com.gwtext.client.widgets.event.TabPanelListener::onContextMenu(Lcom/gwtext/client/widgets/TabPanel;Lcom/gwtext/client/widgets/Panel;Lcom/gwtext/client/core/EventObject;)(tabPanelJ, tabJ, e);
312: }
313: );
314:
315: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('tabchange',
316: function(source, tab) {
317: var tabJ = @com.gwtext.client.widgets.ComponentFactory::getComponent(Lcom/google/gwt/core/client/JavaScriptObject;)(tab);
318: listener.@com.gwtext.client.widgets.event.TabPanelListener::onTabChange(Lcom/gwtext/client/widgets/TabPanel;Lcom/gwtext/client/widgets/Panel;)(tabPanelJ, tabJ);
319: }
320: );
321:
322: }-*/;
323:
324: // --- config properties ---
325:
326: /**
327: * The numeric index of the tab that should be initially
328: * activated on render (defaults to none).
329: *
330: * @param activeTab the active tab index
331: */
332: public void setActiveTab(int activeTab) {
333: if (!isRendered()) {
334: setAttribute("activeTab", activeTab, true);
335: } else {
336: activate(activeTab);
337: }
338: }
339:
340: /**
341: * The id of the tab that should be initially activated on render (defaults to none).
342: *
343: * @param activeTab the active tab ID
344: */
345: public void setActiveTab(String activeTab) {
346: if (!isRendered()) {
347: setAttribute("activeTab", activeTab, true);
348: } else {
349: activate(activeTab);
350: }
351: }
352:
353: /**
354: * True to animate tab scrolling so that hidden tabs slide smoothly into view (defaults to true).
355: * Only applies when enableTabScroll = true.
356: *
357: * @param animScroll true to animate tab scrolling
358: * @see #setEnableTabScroll(boolean)
359: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
360: */
361: public void setAnimScroll(boolean animScroll)
362: throws IllegalStateException {
363: setAttribute("animScroll", animScroll, true);
364: }
365:
366: /**
367: * True to animate tab scrolling so that hidden tabs slide smoothly into view (defaults to true).
368: * Only applies when enableTabScroll = true.
369: *
370: * @return true if animate scroll
371: */
372: public boolean isAnimScroll() {
373: return JavaScriptObjectHelper.getAttributeAsBoolean(config,
374: "animScroll");
375: }
376:
377: /**
378: * Internally, the TabPanel uses a CardLayout to manage its tabs. This property will be
379: * passed on to the layout as its CardLayout.deferredRender config value, determining whether
380: * or not each tab is rendered only when first accessed (defaults to true).
381: *
382: * @param deferredRender true to defer rendering
383: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
384: */
385: public void setDeferredRender(boolean deferredRender)
386: throws IllegalStateException {
387: setAttribute("deferredRender", deferredRender, true);
388: }
389:
390: /**
391: * Internally, the TabPanel uses a CardLayout to manage its tabs. This property will be
392: * passed on to the layout as its CardLayout.deferredRender config value, determining whether
393: * or not each tab is rendered only when first accessed (defaults to true).
394: *
395: * @return true if deferred render
396: */
397: public boolean isDeferredRender() {
398: return JavaScriptObjectHelper.getAttributeAsBoolean(config,
399: "deferredRender");
400: }
401:
402: /**
403: * True to enable scrolling to tabs that may be invisible due to overflowing the overall TabPanel width.
404: * Only available with tabs on top. (defaults to false).
405: *
406: * @param enableTabScroll true to enable tab scroll
407: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
408: */
409: public void setEnableTabScroll(boolean enableTabScroll)
410: throws IllegalStateException {
411: setAttribute("enableTabScroll", enableTabScroll, true);
412: }
413:
414: /**
415: * True to enable scrolling to tabs that may be invisible due to overflowing the overall TabPanel width.
416: * Only available with tabs on top. (defaults to false).
417: *
418: * @return true if tab scrolling enabled
419: */
420: public boolean isEnableTabScroll() {
421: return JavaScriptObjectHelper.getAttributeAsBoolean(config,
422: "enableTabScroll");
423: }
424:
425: /**
426: * Set to true to do a layout of tab items as tabs are changed.
427: *
428: * @param layoutOnTabChange true to do a layout of tab items as tabs are changed (default true)
429: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
430: */
431: public void setLayoutOnTabChange(boolean layoutOnTabChange)
432: throws IllegalStateException {
433: setAttribute("layoutOnTabChange", layoutOnTabChange, true);
434: }
435:
436: /**
437: * True to do a layout of tab items as tabs are changed.
438: *
439: * @return true to do a layout of tab items as tabs are changed
440: */
441: public boolean isLayoutOnTabChange() {
442: return JavaScriptObjectHelper.getAttributeAsBoolean(config,
443: "layoutOnTabChange");
444: }
445:
446: /**
447: * The minimum width in pixels for each tab when resizeTabs = true (defaults to 30).
448: *
449: * @param minTabWidth the min tab width
450: */
451: public void setMinTabWidth(int minTabWidth) {
452: if (!isRendered()) {
453: setAttribute("minTabWidth", minTabWidth, true);
454: } else {
455: setMinTabWidthRendered(minTabWidth);
456: }
457: }
458:
459: /**
460: * True to render the tab strip without a background container image (defaults to false).
461: *
462: * @param plain true for plain tabs
463: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
464: */
465: public void setPlain(boolean plain) throws IllegalStateException {
466: setAttribute("plain", plain, true);
467: }
468:
469: /**
470: * True to render the tab strip without a background container image (defaults to false).
471: *
472: * @return true to render the tab strip without a background container image (defaults to false).
473: */
474: public boolean isPlain() {
475: return JavaScriptObjectHelper.getAttributeAsBoolean(config,
476: "plain");
477: }
478:
479: /**
480: * True to automatically resize each tab so that the tabs will completely fill the tab strip (defaults to false).
481: * Setting this to true may cause specific widths that might be set per tab to be overridden in order to fit them all
482: * into view (although minTabWidth will always be honored).
483: *
484: * @param resizeTabs true to resize tabs
485: */
486: public void setResizeTabs(boolean resizeTabs) {
487: if (!isRendered()) {
488: setAttribute("resizeTabs", resizeTabs, true);
489: } else {
490: setResizeTabsRendered(resizeTabs);
491: }
492: }
493:
494: /**
495: * The number of milliseconds that each scroll animation should last (defaults to .35). Only applies when
496: * animScroll = true.
497: *
498: *
499: * @param scrollDuration scroll duration
500: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
501: */
502: public void setScrollDuration(int scrollDuration)
503: throws IllegalStateException {
504: setAttribute("scrollDuration", scrollDuration, true);
505: }
506:
507: /**
508: * The number of milliseconds that each scroll animation should last (defaults to .35). Only applies when
509: * animScroll = true.
510: *
511: * @return the scroll duration
512: */
513: public int getScrollDuration() {
514: return JavaScriptObjectHelper.getAttributeAsInt(config,
515: "scrollDuration");
516: }
517:
518: /**
519: * The number of pixels to scroll each time a tab scroll button is pressed (defaults to 100, or if resizeTabs = true, the calculated tab width).
520: * Only applies when enableTabScroll = true.
521: *
522: * @param scrollIncrement the scroll increment
523: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
524: */
525: public void setScrollIncrement(int scrollIncrement)
526: throws IllegalStateException {
527: setAttribute("scrollIncrement", scrollIncrement, true);
528: }
529:
530: /**
531: * The number of pixels to scroll each time a tab scroll button is pressed (defaults to 100, or if resizeTabs = true, the calculated tab width).
532: * Only applies when enableTabScroll = true.
533: *
534: * @return the scroll increment
535: */
536: public int getScrollIncrement() {
537: return JavaScriptObjectHelper.getAttributeAsInt(config,
538: "scrollIncrement");
539: }
540:
541: /**
542: * Number of milliseconds between each scroll while a tab scroll button is continuously pressed (defaults to 400).
543: *
544: * @param scrollRepeatInterval the scroll repeat interval
545: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
546: */
547: public void setScrollRepeatInterval(int scrollRepeatInterval)
548: throws IllegalStateException {
549: setAttribute("scrollRepeatInterval", scrollRepeatInterval, true);
550: }
551:
552: /**
553: * Number of milliseconds between each scroll while a tab scroll button is continuously pressed (defaults to 400).
554: *
555: * @return the scroll repeat interval
556: */
557: public int getScrollRepeatInterval() {
558: return JavaScriptObjectHelper.getAttributeAsInt(config,
559: "scrollRepeatInterval");
560: }
561:
562: /**
563: * The number of pixels of space to calculate into the sizing and scrolling of tabs. If you change the margin in CSS,
564: * you will need to update this value so calculations are correct with either resizeTabs or scrolling tabs. (defaults to 2).
565: *
566: * @param tabMargin the tab margin
567: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
568: */
569: public void setTabMargin(int tabMargin)
570: throws IllegalStateException {
571: setAttribute("tabMargin", tabMargin, true);
572: }
573:
574: /**
575: * The number of pixels of space to calculate into the sizing and scrolling of tabs. If you change the margin in CSS,
576: * you will need to update this value so calculations are correct with either resizeTabs or scrolling tabs. (defaults to 2).
577: *
578: * @return the tab margin
579: */
580: public int getTabMargin() {
581: return JavaScriptObjectHelper.getAttributeAsInt(config,
582: "tabMargin");
583: }
584:
585: /**
586: * The position where the tab strip should be rendered (defaults to 'top'). The only other supported value is 'bottom'.
587: * Note that tab scrolling is only supported for position 'top'.
588: *
589: * @param tabPosition the tab position
590: * @see com.gwtext.client.core.Position#TOP
591: * @see com.gwtext.client.core.Position#BOTTOM
592: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
593: */
594: public void setTabPosition(Position tabPosition)
595: throws IllegalStateException {
596: setAttribute("tabPosition", tabPosition.getPosition(), true);
597: }
598:
599: /**
600: * The initial width in pixels of each new tab (defaults to 120).
601: *
602: * @param tabWidth the tab width
603: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
604: */
605: public void setTabWidth(int tabWidth) throws IllegalStateException {
606: setAttribute("tabWidth", tabWidth, true);
607: }
608:
609: /**
610: * The initial width in pixels of each new tab (defaults to 120).
611: *
612: * @return the initial tab width
613: */
614: public int getTabWidth() {
615: return JavaScriptObjectHelper.getAttributeAsInt(config,
616: "tabWidth");
617: }
618:
619: /**
620: * For scrolling tabs, the number of pixels to increment on mouse wheel scrolling (defaults to 20).
621: *
622: * @param wheelIncrement the wheel increment
623: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
624: */
625: public void setWheelIncrement(int wheelIncrement)
626: throws IllegalStateException {
627: setAttribute("wheelIncrement", wheelIncrement, true);
628: }
629:
630: /**
631: * For scrolling tabs, the number of pixels to increment on mouse wheel scrolling (defaults to 20).
632: *
633: * @return the scroll wheel increment
634: */
635: public int getWheelIncrement() {
636: return JavaScriptObjectHelper.getAttributeAsInt(config,
637: "wheelIncrement");
638: }
639: }
|