001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.ui.IPerspectiveDescriptor;
013: import org.eclipse.ui.IPerspectiveListener;
014: import org.eclipse.ui.IPluginContribution;
015: import org.eclipse.ui.IWorkbenchPage;
016: import org.eclipse.ui.PartInitException;
017: import org.eclipse.ui.PlatformUI;
018: import org.eclipse.ui.activities.IIdentifier;
019: import org.eclipse.ui.activities.IIdentifierListener;
020: import org.eclipse.ui.activities.IWorkbenchActivitySupport;
021: import org.eclipse.ui.activities.IdentifierEvent;
022: import org.eclipse.ui.activities.WorkbenchActivityHelper;
023: import org.eclipse.ui.views.IViewDescriptor;
024:
025: /**
026: * Helper methods that the internal layout classes (<code>PageLayout</code> and
027: * <code>FolderLayout</code>) utilize for activities support and view creation.
028: *
029: * @since 3.0
030: */
031: class LayoutHelper {
032:
033: /**
034: * Not intended to be instantiated.
035: */
036: private LayoutHelper() {
037: //no-op
038: }
039:
040: /**
041: * Creates a series of listeners that will activate the provided view on the
042: * provided page layout when <code>IIdenfier</code> enablement changes. The
043: * rules for this activation are as follows: <p>
044: * <ul>
045: * <li> if the identifier becomes enabled and the perspective of the page
046: * layout is the currently active perspective in its window, then activate
047: * the views immediately.
048: * <li> if the identifier becomes enabled and the perspective of the page
049: * layout is not the currently active perspecitve in its window, then add an
050: * <code>IPerspectiveListener</code> to the window and activate the views
051: * when the perspective becomes active.
052: *
053: * @param pageLayout <code>PageLayout</code>.
054: * @param viewId the view id to activate upon <code>IIdentifier</code> enablement.
055: */
056: public static final void addViewActivator(PageLayout pageLayout,
057: final String viewId) {
058: if (viewId == null) {
059: return;
060: }
061:
062: ViewFactory viewFactory = pageLayout.getViewFactory();
063:
064: final IWorkbenchPage partPage = viewFactory.getWorkbenchPage();
065: if (partPage == null) {
066: return;
067: }
068:
069: final IPerspectiveDescriptor partPerspective = pageLayout
070: .getDescriptor();
071:
072: IWorkbenchActivitySupport support = PlatformUI.getWorkbench()
073: .getActivitySupport();
074:
075: IViewDescriptor descriptor = viewFactory.getViewRegistry()
076: .find(viewId);
077: if (!(descriptor instanceof IPluginContribution)) {
078: return;
079: }
080:
081: IIdentifier identifier = support
082: .getActivityManager()
083: .getIdentifier(
084: WorkbenchActivityHelper
085: .createUnifiedId((IPluginContribution) descriptor));
086:
087: identifier.addIdentifierListener(new IIdentifierListener() {
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.ui.activities.IIdentifierListener#identifierChanged(org.eclipse.ui.activities.IdentifierEvent)
091: */
092: public void identifierChanged(
093: IdentifierEvent identifierEvent) {
094: if (identifierEvent.hasEnabledChanged()) {
095: IIdentifier this Identifier = identifierEvent
096: .getIdentifier();
097: if (this Identifier.isEnabled()) {
098: // show view
099: this Identifier.removeIdentifierListener(this );
100: IWorkbenchPage activePage = partPage
101: .getWorkbenchWindow().getActivePage();
102: if (partPage == activePage
103: && partPerspective == activePage
104: .getPerspective()) {
105: // show immediately.
106: try {
107: partPage.showView(viewId);
108: } catch (PartInitException e) {
109: WorkbenchPlugin.log(getClass(),
110: "identifierChanged", e); //$NON-NLS-1$
111: }
112: } else { // show when the perspective becomes active
113: partPage.getWorkbenchWindow()
114: .addPerspectiveListener(
115: new IPerspectiveListener() {
116:
117: /* (non-Javadoc)
118: * @see org.eclipse.ui.IPerspectiveListener#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
119: */
120: public void perspectiveActivated(
121: IWorkbenchPage page,
122: IPerspectiveDescriptor newPerspective) {
123: if (partPerspective == newPerspective) {
124: partPage
125: .getWorkbenchWindow()
126: .removePerspectiveListener(
127: this );
128: try {
129: page
130: .showView(viewId);
131: } catch (PartInitException e) {
132: WorkbenchPlugin
133: .log(
134: getClass(),
135: "perspectiveActivated", e); //$NON-NLS-1$
136: }
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.ui.IPerspectiveListener#perspectiveChanged(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor, java.lang.String)
142: */
143: public void perspectiveChanged(
144: IWorkbenchPage page,
145: IPerspectiveDescriptor perspective,
146: String changeId) {
147: // no-op
148: }
149: });
150: }
151: }
152: }
153: }
154: });
155: }
156:
157: /**
158: * Create the view. If it's already been been created in the provided
159: * factory, return the shared instance.
160: *
161: * @param factory the <code>ViewFactory</code> to use.
162: * @param viewID the view id to use.
163: * @return the new <code>ViewPane</code>.
164: * @throws PartInitException thrown if there is a problem creating the view.
165: */
166: public static final ViewPane createView(ViewFactory factory,
167: String viewId) throws PartInitException {
168: WorkbenchPartReference ref = (WorkbenchPartReference) factory
169: .createView(ViewFactory.extractPrimaryId(viewId),
170: ViewFactory.extractSecondaryId(viewId));
171: ViewPane newPart = (ViewPane) ref.getPane();
172: return newPart;
173: }
174:
175: }
|