001: /*******************************************************************************
002: * Copyright (c) 2005 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.examples.components.actions;
011:
012: import org.eclipse.jface.action.IAction;
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.layout.FillLayout;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Display;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.IWorkbenchPage;
021: import org.eclipse.ui.IWorkbenchWindow;
022: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
023: import org.eclipse.ui.examples.components.ComponentExamplesPlugin;
024: import org.eclipse.ui.examples.components.views.NameTestView;
025: import org.eclipse.ui.internal.components.framework.ComponentException;
026: import org.eclipse.ui.internal.components.framework.ComponentFactory;
027: import org.eclipse.ui.internal.components.framework.ComponentHandle;
028: import org.eclipse.ui.internal.components.framework.Components;
029: import org.eclipse.ui.internal.components.framework.FactoryMap;
030: import org.eclipse.ui.internal.components.framework.IServiceProvider;
031: import org.eclipse.ui.internal.part.ComponentPart;
032: import org.eclipse.ui.internal.part.components.services.INameable;
033: import org.eclipse.ui.internal.part.components.services.IStatusFactory;
034: import org.osgi.framework.Bundle;
035:
036: /**
037: * Demonstrate how to open a part programmatically using the Site class.
038: * This pattern can be used to instantiate views or editors without a workbench,
039: * or to create parts that are neither views nor editors but can be used as
040: * building blocks inside dialogs or other parts.
041: * <p>
042: * Clients should never call the constructor of a component class directly,
043: * even if the component class is API -- otherwise, changing the class dependencies
044: * would be a breaking change. Components need to be instantiated through a container.
045: * For UI-related classes, the Site class is a quick way to instantiate the component.
046: * It creates a container, a composite, instantiates the component, and provides concrete
047: * access to several common adapters.
048: * </p>
049: * <p>
050: * The Site class uses a dispose listener to clean up the container. This means that disposing
051: * the top-level widget is enough to clean up after the part. This example creates the part
052: * in a Shell. Since the shell is disposed automatically when it closes, we don't need
053: * any explicit cleanup code.
054: * </p>
055: * <p>
056: * Note: not all views and editors can be instantiated using this pattern. If
057: * a view or editor permits creation in this manner, it will contain a class
058: * comment saying so and identifying which scope it uses. Most such parts
059: * will be in the global scope, so will have access to a more restricted
060: * set of dependencies (some components like IPartFactory require an IWorkbenchPage,
061: * so cannot exist in the global scope).
062: * </p>
063: */
064: public class ProgrammaticViewCreationExampleAction implements
065: IWorkbenchWindowActionDelegate {
066: private IWorkbenchWindow window;
067:
068: /**
069: * The constructor.
070: */
071: public ProgrammaticViewCreationExampleAction() {
072: }
073:
074: public void run(IAction action) {
075: // Create a shell
076: final Shell tempShell = new Shell(window.getShell(),
077: SWT.DIALOG_TRIM | SWT.RESIZE);
078: tempShell.setLayout(new FillLayout());
079: tempShell.setText("Name test view");
080:
081: Bundle this Plugin = ComponentExamplesPlugin.getDefault()
082: .getBundle();
083:
084: try {
085: // Instantiate the NameTestView part (this line is the whole point of the example)
086: // It demonstrates how the Site class can be used instead of calling NameTestView's constructor.
087: // Note that if we wanted to pass arguments into NameTestView's constructor, we could do so
088: // by attaching them to the ContainerContext.
089: ComponentPart testPart = new ComponentPart(tempShell,
090: new FactoryMap().mapInstance(Bundle.class,
091: this Plugin).mapInstance(
092: IWorkbenchPage.class,
093: window.getActivePage()),
094: new ComponentFactory() {
095: /* (non-Javadoc)
096: * @see org.eclipse.core.components.ComponentFactory#getHandle(org.eclipse.core.components.IComponentProvider)
097: */
098: public ComponentHandle createHandle(
099: IServiceProvider availableServices)
100: throws ComponentException {
101:
102: return new ComponentHandle(
103: new NameTestView(
104: (Composite) Components
105: .queryInterface(
106: availableServices,
107: Composite.class),
108: (INameable) Components
109: .queryInterface(
110: availableServices,
111: INameable.class),
112: (IStatusFactory) Components
113: .queryInterface(
114: availableServices,
115: IStatusFactory.class)));
116: }
117: });
118:
119: } catch (ComponentException e) {
120: tempShell.dispose();
121: MessageDialog.openError(window.getShell(),
122: "Error opening view", e.getMessage());
123: return;
124: }
125:
126: // Open a modal dialog
127: tempShell.open();
128: Display d = tempShell.getDisplay();
129: while (!tempShell.isDisposed()) {
130: if (!d.readAndDispatch())
131: d.sleep();
132: }
133:
134: }
135:
136: public void selectionChanged(IAction action, ISelection selection) {
137: }
138:
139: public void dispose() {
140: }
141:
142: public void init(IWorkbenchWindow window) {
143: this.window = window;
144: }
145: }
|