KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > LayoutHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.ui.internal;
12
13 import org.eclipse.ui.IPerspectiveDescriptor;
14 import org.eclipse.ui.IPerspectiveListener;
15 import org.eclipse.ui.IPluginContribution;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.activities.IIdentifier;
20 import org.eclipse.ui.activities.IIdentifierListener;
21 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
22 import org.eclipse.ui.activities.IdentifierEvent;
23 import org.eclipse.ui.activities.WorkbenchActivityHelper;
24 import org.eclipse.ui.views.IViewDescriptor;
25
26 /**
27  * Helper methods that the internal layout classes (<code>PageLayout</code> and
28  * <code>FolderLayout</code>) utilize for activities support and view creation.
29  *
30  * @since 3.0
31  */

32 class LayoutHelper {
33
34     /**
35      * Not intended to be instantiated.
36      */

37     private LayoutHelper() {
38         //no-op
39
}
40
41     /**
42      * Creates a series of listeners that will activate the provided view on the
43      * provided page layout when <code>IIdenfier</code> enablement changes. The
44      * rules for this activation are as follows: <p>
45      * <ul>
46      * <li> if the identifier becomes enabled and the perspective of the page
47      * layout is the currently active perspective in its window, then activate
48      * the views immediately.
49      * <li> if the identifier becomes enabled and the perspective of the page
50      * layout is not the currently active perspecitve in its window, then add an
51      * <code>IPerspectiveListener</code> to the window and activate the views
52      * when the perspective becomes active.
53      *
54      * @param pageLayout <code>PageLayout</code>.
55      * @param viewId the view id to activate upon <code>IIdentifier</code> enablement.
56      */

57     public static final void addViewActivator(PageLayout pageLayout,
58             final String JavaDoc viewId) {
59         if (viewId == null) {
60             return;
61         }
62
63         ViewFactory viewFactory = pageLayout.getViewFactory();
64
65         final IWorkbenchPage partPage = viewFactory.getWorkbenchPage();
66         if (partPage == null) {
67             return;
68         }
69
70         final IPerspectiveDescriptor partPerspective = pageLayout
71                 .getDescriptor();
72
73         IWorkbenchActivitySupport support = PlatformUI.getWorkbench()
74                 .getActivitySupport();
75
76         IViewDescriptor descriptor = viewFactory.getViewRegistry().find(viewId);
77         if (!(descriptor instanceof IPluginContribution)) {
78             return;
79         }
80
81         IIdentifier identifier = support.getActivityManager().getIdentifier(
82                 WorkbenchActivityHelper
83                         .createUnifiedId((IPluginContribution) descriptor));
84
85         identifier.addIdentifierListener(new IIdentifierListener() {
86
87             /* (non-Javadoc)
88              * @see org.eclipse.ui.activities.IIdentifierListener#identifierChanged(org.eclipse.ui.activities.IdentifierEvent)
89              */

90             public void identifierChanged(IdentifierEvent identifierEvent) {
91                 if (identifierEvent.hasEnabledChanged()) {
92                     IIdentifier thisIdentifier = identifierEvent
93                             .getIdentifier();
94                     if (thisIdentifier.isEnabled()) {
95                         // show view
96
thisIdentifier.removeIdentifierListener(this);
97                         IWorkbenchPage activePage = partPage
98                                 .getWorkbenchWindow().getActivePage();
99                         if (partPage == activePage
100                                 && partPerspective == activePage
101                                         .getPerspective()) {
102                             // show immediately.
103
try {
104                                 partPage.showView(viewId);
105                             } catch (PartInitException e) {
106                                 WorkbenchPlugin.log(getClass(), "identifierChanged", e); //$NON-NLS-1$
107
}
108                         } else { // show when the perspective becomes active
109
partPage.getWorkbenchWindow()
110                                     .addPerspectiveListener(
111                                             new IPerspectiveListener() {
112
113                                                 /* (non-Javadoc)
114                                                  * @see org.eclipse.ui.IPerspectiveListener#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
115                                                  */

116                                                 public void perspectiveActivated(
117                                                         IWorkbenchPage page,
118                                                         IPerspectiveDescriptor newPerspective) {
119                                                     if (partPerspective == newPerspective) {
120                                                         partPage
121                                                                 .getWorkbenchWindow()
122                                                                 .removePerspectiveListener(
123                                                                         this);
124                                                         try {
125                                                             page
126                                                                     .showView(viewId);
127                                                         } catch (PartInitException e) {
128                                                             WorkbenchPlugin.log(getClass(), "perspectiveActivated", e); //$NON-NLS-1$
129
}
130                                                     }
131                                                 }
132
133                                                 /* (non-Javadoc)
134                                                  * @see org.eclipse.ui.IPerspectiveListener#perspectiveChanged(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor, java.lang.String)
135                                                  */

136                                                 public void perspectiveChanged(
137                                                         IWorkbenchPage page,
138                                                         IPerspectiveDescriptor perspective,
139                                                         String JavaDoc changeId) {
140                                                     // no-op
141
}
142                                             });
143                         }
144                     }
145                 }
146             }
147         });
148     }
149
150     /**
151      * Create the view. If it's already been been created in the provided
152      * factory, return the shared instance.
153      *
154      * @param factory the <code>ViewFactory</code> to use.
155      * @param viewID the view id to use.
156      * @return the new <code>ViewPane</code>.
157      * @throws PartInitException thrown if there is a problem creating the view.
158      */

159     public static final ViewPane createView(ViewFactory factory, String JavaDoc viewId)
160             throws PartInitException {
161         WorkbenchPartReference ref = (WorkbenchPartReference) factory
162                 .createView(ViewFactory.extractPrimaryId(viewId), ViewFactory
163                         .extractSecondaryId(viewId));
164         ViewPane newPart = (ViewPane) ref.getPane();
165         return newPart;
166     }
167
168 }
169
Popular Tags