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.sample.showcase2.client;
010:
011: import com.google.gwt.core.client.EntryPoint;
012: import com.google.gwt.user.client.History;
013: import com.google.gwt.user.client.HistoryListener;
014: import com.google.gwt.user.client.ui.HTML;
015: import com.google.gwt.user.client.ui.PopupPanel;
016: import com.gwtext.client.core.EventObject;
017: import com.gwtext.client.core.Margins;
018: import com.gwtext.client.core.Position;
019: import com.gwtext.client.core.RegionPosition;
020: import com.gwtext.client.widgets.*;
021: import com.gwtext.client.widgets.event.PanelListenerAdapter;
022: import com.gwtext.client.widgets.event.TabPanelListenerAdapter;
023: import com.gwtext.client.widgets.layout.BorderLayout;
024: import com.gwtext.client.widgets.layout.BorderLayoutData;
025: import com.gwtext.client.widgets.layout.FitLayout;
026: import com.gwtext.client.widgets.menu.BaseItem;
027: import com.gwtext.client.widgets.menu.Item;
028: import com.gwtext.client.widgets.menu.Menu;
029: import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
030: import com.gwtext.client.widgets.tree.TreePanel;
031:
032: public class Showcase2 implements EntryPoint, HistoryListener {
033:
034: private static PopupPanel messagePanel = new PopupPanel(true);
035: private TabPanel centerPanel;
036: private ScreenManager screenManager;
037: private Menu menu;
038:
039: public void onModuleLoad() {
040:
041: //create the main panel and assign it a BorderLayout
042: Panel mainPanel = new Panel();
043: mainPanel.setLayout(new BorderLayout());
044:
045: BorderLayoutData northLayoutData = new BorderLayoutData(
046: RegionPosition.NORTH);
047: northLayoutData.setSplit(false);
048:
049: BorderLayoutData centerLayoutData = new BorderLayoutData(
050: RegionPosition.CENTER);
051: centerLayoutData.setMargins(new Margins(5, 0, 5, 5));
052:
053: Panel centerPanelWrappper = new Panel();
054: centerPanelWrappper.setLayout(new FitLayout());
055: centerPanelWrappper.setBorder(false);
056: centerPanelWrappper.setBodyBorder(false);
057:
058: centerPanel = new TabPanel();
059: centerPanel.setBodyBorder(false);
060: centerPanel.setEnableTabScroll(true);
061: centerPanel.setAutoScroll(true);
062: centerPanel.setAutoDestroy(false);
063: centerPanel.setActiveTab(0);
064:
065: //hide the panel when the tab is closed
066: centerPanel.addListener(new TabPanelListenerAdapter() {
067: public boolean doBeforeTabChange(TabPanel source,
068: Panel newPanel, Panel oldPanel) {
069: WindowMgr.hideAll();
070: return true;
071: }
072:
073: public void onRemove(Container self, Component component) {
074: component.hide();
075: }
076:
077: public void onContextMenu(TabPanel source, Panel tab,
078: EventObject e) {
079: showMenu(tab, e);
080: }
081: });
082:
083: centerPanel.setLayoutOnTabChange(true);
084: centerPanel.setTitle("Main Content");
085:
086: screenManager = new ScreenManager(centerPanel);
087:
088: //setup the west regions layout properties
089: BorderLayoutData westLayoutData = new BorderLayoutData(
090: RegionPosition.WEST);
091: westLayoutData.setMargins(new Margins(5, 5, 0, 5));
092: westLayoutData.setCMargins(new Margins(5, 5, 5, 5));
093: westLayoutData.setMinSize(155);
094: westLayoutData.setMaxSize(350);
095: westLayoutData.setSplit(true);
096:
097: //create the west panel and add it to the main panel applying the west region layout properties
098: Panel westPanel = createWestPanel();
099: mainPanel.add(westPanel, westLayoutData);
100:
101: final Panel introPanel = new Panel();
102: introPanel.setTitle("GWT-Ext 2.0 Showcase");
103: introPanel.setPaddings(10);
104: introPanel.setLayout(new FitLayout());
105: final ShowcaseView showcaseView = new ShowcaseView(
106: screenManager);
107: introPanel.add(showcaseView);
108:
109: centerPanel.add(introPanel, centerLayoutData);
110: centerPanelWrappper.add(centerPanel);
111: mainPanel.add(centerPanelWrappper, centerLayoutData);
112:
113: final String initToken = History.getToken();
114: if (initToken.length() != 0) {
115: mainPanel.addListener(new PanelListenerAdapter() {
116: public void onRender(Component component) {
117: onHistoryChanged(initToken);
118: }
119: });
120: }
121:
122: Viewport viewport = new Viewport(mainPanel);
123:
124: // Add history listener
125: History.addHistoryListener(this );
126: }
127:
128: private Panel createWestPanel() {
129: Panel westPanel = new Panel();
130: westPanel.setId("side-nav");
131: westPanel.setTitle("Showcase Explorer");
132: westPanel.setLayout(new FitLayout());
133: westPanel.setWidth(210);
134: westPanel.setCollapsible(true);
135:
136: Toolbar toolbar = new Toolbar();
137: toolbar.addFill();
138: toolbar.addItem(new ToolbarTextItem("Select Theme "));
139: toolbar.addSpacer();
140: toolbar.addField(new ThemeChanger());
141: westPanel.setTopToolbar(toolbar);
142:
143: TabPanel tabPanel = new TabPanel();
144: tabPanel.setActiveTab(0);
145: tabPanel.setDeferredRender(true);
146: tabPanel.setTabPosition(Position.BOTTOM);
147: TreePanel treePanel = screenManager.getTreeNav();
148:
149: tabPanel.add(treePanel);
150: tabPanel.add(screenManager.getAccordionNav());
151: westPanel.add(tabPanel);
152:
153: return westPanel;
154: }
155:
156: /**
157: * This method is called whenever the application's history changes.
158: *
159: * @param historyToken the histrory token
160: */
161: public void onHistoryChanged(String historyToken) {
162: screenManager.showScreen(historyToken);
163: }
164:
165: private void showMenu(final Panel tab, EventObject e) {
166: if (menu == null) {
167: menu = new Menu();
168: Item close = new Item("Close Tab");
169: close.setId("close-tab-item");
170: close.addListener(new BaseItemListenerAdapter() {
171: public void onClick(BaseItem item, EventObject e) {
172: centerPanel.remove(centerPanel.getActiveTab());
173: }
174: });
175: menu.addItem(close);
176:
177: Item closeOthers = new Item("Close Other Tabs");
178: closeOthers.setId("close-others-item");
179: closeOthers.addListener(new BaseItemListenerAdapter() {
180: public void onClick(BaseItem item, EventObject e) {
181: Component[] items = centerPanel.getItems();
182: for (int i = 0; i < items.length; i++) {
183: Panel panel = (Panel) items[i];
184: if (panel.isClosable()
185: && !panel.getId().equals(
186: centerPanel.getActiveTab()
187: .getId())) {
188: centerPanel.remove(panel);
189: }
190: }
191: }
192: });
193: menu.addItem(closeOthers);
194: }
195:
196: BaseItem close = menu.getItem("close-tab-item");
197: if (!centerPanel.getActiveTab().isClosable()) {
198: close.disable();
199: } else {
200: close.enable();
201: }
202:
203: BaseItem closeOthers = menu.getItem("close-others-item");
204: if (centerPanel.getItems().length == 1) {
205: closeOthers.disable();
206: } else {
207: closeOthers.enable();
208: }
209: menu.showAt(e.getXY());
210: }
211:
212: public static void showMessage(String title, String message) {
213: messagePanel.setPopupPosition(500, 300);
214: messagePanel
215: .setWidget(new HTML(getMessageHtml(title, message)));
216: messagePanel.setWidth("300px");
217: messagePanel.show();
218: }
219:
220: private static native String getMessageHtml(String title,
221: String message) /*-{
222: return ['<div class="msg">',
223: '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
224: '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', title, '</h3>', message, '</div></div></div>',
225: '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
226: '</div>'].join('');
227: }-*/;
228:
229: }
|