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 java.text.MessageFormat;
013:
014: import org.eclipse.jface.action.IAction;
015: import org.eclipse.jface.dialogs.MessageDialog;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.layout.FillLayout;
020: import org.eclipse.swt.widgets.Display;
021: import org.eclipse.swt.widgets.Shell;
022: import org.eclipse.ui.IPageLayout;
023: import org.eclipse.ui.IWorkbenchPage;
024: import org.eclipse.ui.IWorkbenchWindow;
025: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
026: import org.eclipse.ui.internal.WorkbenchPage;
027: import org.eclipse.ui.internal.components.framework.ComponentException;
028: import org.eclipse.ui.internal.components.framework.FactoryMap;
029: import org.eclipse.ui.internal.part.components.services.ISelectionHandler;
030: import org.eclipse.ui.internal.part.components.services.IWorkbenchPartFactory;
031:
032: /**
033: * Demonstrate how to open a view by its ID from an IWorkbenchPage.
034: */
035: public class CreateViewByIdAction2 implements
036: IWorkbenchWindowActionDelegate {
037: private IWorkbenchWindow window;
038:
039: /**
040: * The constructor.
041: */
042: public CreateViewByIdAction2() {
043: }
044:
045: public void run(IAction action) {
046: IWorkbenchPage page = window.getActivePage();
047:
048: if (page == null) {
049: MessageDialog.openInformation(window.getShell(),
050: "Open view", "No workbench page open");
051: return;
052: }
053:
054: final Shell tempShell = new Shell(window.getShell(),
055: SWT.DIALOG_TRIM | SWT.RESIZE);
056:
057: tempShell.setLayout(new FillLayout());
058: tempShell.setText("Problems");
059:
060: IWorkbenchPartFactory factory = ((WorkbenchPage) page)
061: .getPartFactory();
062: try {
063: FactoryMap context = new FactoryMap();
064:
065: // Add an ISelectionHandler to the view's context. Whenever the view changes its selection,
066: // we'll update the shell title to display the selection size
067: context.mapInstance(ISelectionHandler.class,
068: new ISelectionHandler() {
069: /* (non-Javadoc)
070: * @see org.eclipse.ui.internal.part.components.services.ISelectionHandler#setSelection(org.eclipse.jface.viewers.ISelection)
071: */
072: public void setSelection(ISelection newSelection) {
073: if (newSelection instanceof IStructuredSelection) {
074: IStructuredSelection sel = (IStructuredSelection) newSelection;
075: int selectionSize = sel.size();
076:
077: tempShell
078: .setText(MessageFormat
079: .format(
080: "Problems ({0} problems selected)",
081: new String[] { Integer
082: .toString(selectionSize) }));
083: }
084: }
085: });
086:
087: factory.createView(IPageLayout.ID_PROBLEM_VIEW, tempShell,
088: null, context);
089: } catch (ComponentException e) {
090: tempShell.dispose();
091: MessageDialog.openError(window.getShell(),
092: "Error opening view", e.getMessage());
093: }
094:
095: // Open the dialog
096: tempShell.open();
097: Display d = tempShell.getDisplay();
098: while (!tempShell.isDisposed()) {
099: if (!d.readAndDispatch())
100: d.sleep();
101: }
102:
103: }
104:
105: public void selectionChanged(IAction action, ISelection selection) {
106: }
107:
108: public void dispose() {
109: }
110:
111: public void init(IWorkbenchWindow window) {
112: this.window = window;
113: }
114: }
|