01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.tests.macro;
11:
12: import java.io.InputStream;
13:
14: import org.eclipse.core.resources.IFile;
15: import org.eclipse.core.runtime.CoreException;
16: import org.eclipse.jface.action.IAction;
17: import org.eclipse.jface.viewers.ISelection;
18: import org.eclipse.jface.viewers.IStructuredSelection;
19: import org.eclipse.ui.IWorkbenchWindow;
20: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
21:
22: /**
23: * Our sample action implements workbench action delegate.
24: * The action proxy will be created by the workbench and
25: * shown in the UI. When the user tries to use the action,
26: * this delegate will be created and execution will be
27: * delegated to it.
28: * @see IWorkbenchWindowActionDelegate
29: */
30: public class PlaybackAction implements IWorkbenchWindowActionDelegate {
31: private IWorkbenchWindow window;
32:
33: /**
34: * The constructor.
35: */
36: public PlaybackAction() {
37: }
38:
39: /**
40: * The action has been activated. The argument of the
41: * method represents the 'real' action sitting
42: * in the workbench UI.
43: * @see IWorkbenchWindowActionDelegate#run
44: */
45: public void run(IAction action) {
46: ISelection selection = window.getSelectionService()
47: .getSelection();
48: if (selection instanceof IStructuredSelection) {
49: IStructuredSelection ssel = (IStructuredSelection) selection;
50: Object el = ssel.getFirstElement();
51: if (el instanceof IFile) {
52: action.setEnabled(false);
53: runFile((IFile) el);
54: action.setEnabled(true);
55: }
56: }
57: }
58:
59: private void runFile(IFile file) {
60: try {
61: InputStream is = file.getContents();
62: MacroManager mng = MacroPlugin.getDefault()
63: .getMacroManager();
64: mng.setIndexHandler(new DefaultIndexHandler());
65: mng.play(window.getShell().getDisplay(), window, file
66: .getName(), is);
67: mng.setIndexHandler(null);
68: } catch (CoreException e) {
69: MacroPlugin.logException(e);
70: }
71: }
72:
73: /**
74: * Selection in the workbench has been changed. We
75: * can change the state of the 'real' action here
76: * if we want, but this can only happen after
77: * the delegate has been created.
78: * @see IWorkbenchWindowActionDelegate#selectionChanged
79: */
80: public void selectionChanged(IAction action, ISelection selection) {
81: }
82:
83: /**
84: * We can use this method to dispose of any system
85: * resources we previously allocated.
86: * @see IWorkbenchWindowActionDelegate#dispose
87: */
88: public void dispose() {
89: }
90:
91: /**
92: * We will cache window object in order to
93: * be able to provide parent shell for the message dialog.
94: * @see IWorkbenchWindowActionDelegate#init
95: */
96: public void init(IWorkbenchWindow window) {
97: this.window = window;
98: }
99: }
|