1 11 package org.eclipse.ui.internal.misc; 12 13 import org.eclipse.swt.events.SelectionAdapter; 14 import org.eclipse.swt.events.SelectionEvent; 15 import org.eclipse.swt.widgets.*; 16 17 public class Workbook { 18 private TabFolder tabFolder; 19 private TabItem selectedTab; 20 23 public Workbook(Composite parent, int style) { 24 tabFolder = new TabFolder(parent, style); 25 26 tabFolder.addSelectionListener(new SelectionAdapter() { 27 public void widgetSelected(SelectionEvent event) { 28 TabItem newSelectedTab = (TabItem) event.item; 29 if (selectedTab == newSelectedTab) return; 31 32 if (selectedTab != null && (!selectedTab.isDisposed())) { 33 WorkbookPage selectedPage = getWorkbookPage(selectedTab); 34 if (!selectedPage.deactivate()) { 35 tabFolder.setSelection(new TabItem[] {selectedTab}); 36 return; 37 } 38 } 39 40 selectedTab = newSelectedTab; 41 WorkbookPage newSelectedPage = getWorkbookPage(newSelectedTab); 42 newSelectedPage.activate(); 43 44 } 45 }); 46 47 } 48 public WorkbookPage getSelectedPage() { 49 50 int index = tabFolder.getSelectionIndex(); 51 if (index == -1) return null; 53 54 TabItem selectedItem = tabFolder.getItem(index); 55 56 return (WorkbookPage)selectedItem.getData(); 57 } 58 public TabFolder getTabFolder() { 59 60 return tabFolder; 61 62 } 63 protected WorkbookPage getWorkbookPage(TabItem item) { 64 65 try { 66 return (WorkbookPage) item.getData(); 67 } catch (ClassCastException e) { 68 return null; 69 } 70 } 71 public WorkbookPage[] getWorkbookPages() { 72 73 TabItem[] tabItems = tabFolder.getItems(); 74 int nItems = tabItems.length; 75 WorkbookPage[] workbookPages = new WorkbookPage[nItems]; 76 for (int i = 0; i < nItems; i++) 77 workbookPages[i] = getWorkbookPage(tabItems[i]); 78 return workbookPages; 79 } 80 public void setSelectedPage (WorkbookPage workbookPage) 81 { 82 TabItem newSelectedTab = workbookPage.getTabItem(); 83 84 if (selectedTab == newSelectedTab) 85 return; 86 87 selectedTab = newSelectedTab; 88 workbookPage.activate(); 89 tabFolder.setSelection(new TabItem[] {newSelectedTab}); 90 91 } 92 } 93 | Popular Tags |