KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > Workbook


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
21  * Workbook constructor comment.
22  */

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) // Do nothing if the selection did not change.
30
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) // When can this be -1
52
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 JavaDoc 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