KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > PDEMultiPagePropertySheet


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

11 package org.eclipse.pde.internal.ui.editor;
12
13 import org.eclipse.ui.*;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.viewers.*;
16 import org.eclipse.ui.part.*;
17 import org.eclipse.swt.widgets.*;
18 import org.eclipse.ui.views.properties.*;
19 import org.eclipse.swt.*;
20 import java.util.*;
21
22 public class PDEMultiPagePropertySheet implements IPropertySheetPage {
23     private PageBook pagebook;
24     private Hashtable recMap = new Hashtable();
25     private PropertySheetPage defaultPage;
26     private IActionBars actionBars;
27     private IPropertySheetPage currentPage;
28     private boolean disposed = false;
29
30     class PageRec {
31         IPropertySheetPage page;
32         SubActionBars bars;
33         void setBarsActive(boolean active) {
34             if (active)
35                 bars.activate();
36             else
37                 bars.deactivate();
38         }
39         void dispose() {
40             if (bars!=null) {
41                 bars.dispose();
42                 bars = null;
43             }
44             if (page!=null) {
45                 page.dispose();
46                 page = null;
47             }
48         }
49     }
50
51     public PDEMultiPagePropertySheet() {
52         defaultPage = new PropertySheetPage();
53     }
54
55     public void createControl(Composite parent) {
56         disposed = false;
57         pagebook = new PageBook(parent, SWT.NULL);
58         defaultPage.createControl(pagebook);
59         if (currentPage != null)
60             setPageActive(currentPage);
61     }
62     private PageRec createPageRec(IPropertySheetPage page) {
63         if (actionBars == null)
64             return null;
65         PageRec rec = new PageRec();
66         rec.page = page;
67
68         rec.bars = new SubActionBars(actionBars);
69         getPageControl(page);
70
71         page.setActionBars(rec.bars);
72         recMap.put(page, rec);
73         return rec;
74     }
75     public void dispose() {
76         updateActionBars();
77         
78         for (Enumeration enm=recMap.elements(); enm.hasMoreElements();) {
79             PageRec rec = (PageRec)enm.nextElement();
80             rec.dispose();
81         }
82         recMap.clear();
83
84         if (pagebook != null && !pagebook.isDisposed()) {
85             pagebook.dispose();
86         }
87         if (defaultPage!=null) {
88             defaultPage.dispose();
89             defaultPage=null;
90         }
91         pagebook = null;
92         disposed = true;
93     }
94
95     public boolean isDisposed() {
96         return disposed;
97     }
98
99     public Control getControl() {
100         return pagebook;
101     }
102     
103     public void closeActiveEditor() {
104         if (currentPage==null) return;
105     }
106     
107     private Control getPageControl(IPropertySheetPage page) {
108         Control control = page.getControl();
109         if (control == null || control.isDisposed()) {
110             // first time
111
page.createControl(pagebook);
112             control = page.getControl();
113         }
114         return control;
115     }
116     public void selectionChanged(IWorkbenchPart part, ISelection sel) {
117         if (currentPage != null)
118             currentPage.selectionChanged(part, sel);
119     }
120     public void setActionBars(IActionBars bars) {
121         this.actionBars = bars;
122
123         createPageRec(defaultPage);
124
125         if (currentPage != null) {
126             PageRec rec = createPageRec(currentPage);
127             setPageActive(rec);
128             updateActionBars();
129         }
130     }
131     public void setDefaultPageActive() {
132         setPageActive(defaultPage);
133     }
134     public void setFocus() {
135         if (currentPage != null)
136             currentPage.setFocus();
137     }
138     private void setPageActive(PageRec pageRec) {
139         IPropertySheetPage page = pageRec.page;
140         Control control = getPageControl(page);
141         pagebook.showPage(control);
142         pageRec.setBarsActive(true);
143     }
144     public void setPageActive(IPropertySheetPage page) {
145         IPropertySheetPage oldPage = currentPage;
146         this.currentPage = page;
147         if (pagebook == null) {
148             // still not being made
149
return;
150         }
151         if (oldPage != null) {
152             PageRec oldRec = (PageRec) recMap.get(oldPage);
153             if (oldRec != null) {
154                 oldRec.setBarsActive(false);
155             }
156         }
157         PageRec rec = (PageRec) recMap.get(page);
158         if (rec == null) {
159             rec = createPageRec(page);
160         }
161         if (rec != null) {
162             setPageActive(rec);
163             updateActionBars();
164         }
165     }
166     private void updateActionBars() {
167         refreshGlobalActionHandlers();
168         actionBars.updateActionBars();
169     }
170     private void refreshGlobalActionHandlers() {
171         // Clear old actions.
172
actionBars.clearGlobalActionHandlers();
173
174         // Set new actions.
175
if (recMap == null || currentPage == null)
176             return;
177         PageRec activeRec = (PageRec) recMap.get(currentPage);
178         Map newActionHandlers = activeRec.bars.getGlobalActionHandlers();
179         if (newActionHandlers != null) {
180             Set keys = newActionHandlers.entrySet();
181             Iterator iter = keys.iterator();
182             while (iter.hasNext()) {
183                 Map.Entry entry = (Map.Entry) iter.next();
184                 actionBars.setGlobalActionHandler(
185                     (String JavaDoc) entry.getKey(),
186                     (IAction) entry.getValue());
187             }
188         }
189     }
190 }
191
Popular Tags