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.PrintWriter;
13:
14: import org.eclipse.core.runtime.CoreException;
15: import org.eclipse.core.runtime.IProgressMonitor;
16: import org.eclipse.swt.widgets.Composite;
17: import org.eclipse.swt.widgets.Display;
18: import org.eclipse.swt.widgets.Event;
19: import org.eclipse.ui.PlatformUI;
20: import org.eclipse.ui.commands.ExecutionException;
21: import org.eclipse.ui.commands.ICommand;
22: import org.eclipse.ui.commands.IWorkbenchCommandSupport;
23: import org.eclipse.ui.commands.NotHandledException;
24:
25: public class KeyCommand extends MacroCommand {
26: public static final String TYPE = "key-binding";
27: private String commandId;
28:
29: /**
30: * @param widgetId
31: */
32: public KeyCommand(WidgetIdentifier widgetId, String commandId) {
33: super (widgetId);
34: this .commandId = commandId;
35: }
36:
37: /* (non-Javadoc)
38: * @see org.eclipse.ui.internal.macro.MacroCommand#getType()
39: */
40: public String getType() {
41: return TYPE;
42: }
43:
44: /* (non-Javadoc)
45: * @see org.eclipse.ui.internal.macro.MacroCommand#processEvent(org.eclipse.swt.widgets.Event)
46: */
47: public void processEvent(Event e) {
48: }
49:
50: /* (non-Javadoc)
51: * @see org.eclipse.ui.internal.macro.IWritable#write(java.lang.String, java.io.PrintWriter)
52: */
53: public void write(String indent, PrintWriter writer) {
54: writer.print(indent);
55: writer.print("<command type=\"");
56: writer.print(getType());
57: writer.print("\" contextId=\"");
58: writer.print(getWidgetId().getContextId());
59: writer.print("\" widgetId=\"");
60: writer.print(getWidgetId().getWidgetId());
61: writer.print("\" commandId=\"");
62: writer.print(commandId);
63: writer.println("\"/>");
64: }
65:
66: /* (non-Javadoc)
67: * @see org.eclipse.ui.internal.macro.IPlayable#playback(org.eclipse.swt.widgets.Display, org.eclipse.swt.widgets.Composite, org.eclipse.core.runtime.IProgressMonitor)
68: */
69: public boolean playback(Display display, Composite parent,
70: IProgressMonitor monitor) throws CoreException {
71: CommandTarget target = MacroUtil.locateCommandTarget(parent,
72: getWidgetId(), getStartLine());
73: if (target == null)
74: return false;
75: IWorkbenchCommandSupport csupport = PlatformUI.getWorkbench()
76: .getCommandSupport();
77: ICommand command = csupport.getCommandManager().getCommand(
78: commandId);
79: if (command != null) {
80: try {
81: command.execute(null);
82: return true;
83: } catch (ExecutionException e) {
84: MacroPlugin.logException(e);
85: } catch (NotHandledException e) {
86: MacroPlugin.logException(e);
87: }
88: }
89: return false;
90: }
91:
92: }
|