001: /*******************************************************************************
002: * Copyright (c) 2000, 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.pde.internal.ui.tests.macro;
011:
012: import java.io.PrintWriter;
013: import java.util.ArrayList;
014: import java.util.Hashtable;
015: import java.util.Stack;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.SubProgressMonitor;
020: import org.eclipse.jface.dialogs.Dialog;
021: import org.eclipse.jface.window.Window;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Display;
025: import org.eclipse.swt.widgets.Event;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.ui.PlatformUI;
028: import org.w3c.dom.Node;
029:
030: public class Macro implements IWritable, IPlayable {
031: private static final String SYNTAX_VERSION = "0.1";
032: private transient IIndexHandler indexHandler;
033: ArrayList shells;
034: private String name;
035: private Stack shellStack;
036:
037: public Macro() {
038: shells = new ArrayList();
039: }
040:
041: public Macro(String name) {
042: this ();
043: this .name = name;
044: }
045:
046: void addShell(Node node, Hashtable lineTable) {
047: String sid = MacroUtil.getAttribute(node, "id");
048: MacroCommandShell shell = new MacroCommandShell(null, sid);
049: shell.load(node, lineTable);
050: shells.add(shell);
051: }
052:
053: public void initializeForRecording(Display display) {
054: shellStack = new Stack();
055: shells.clear();
056: Shell currentShell = display.getActiveShell();
057: MacroCommandShell commandShell = createCommandShell(currentShell);
058: shellStack.push(commandShell);
059: shells.add(commandShell);
060: }
061:
062: private MacroCommandShell createCommandShell(Shell shell) {
063: WidgetIdentifier wi = MacroUtil.getWidgetIdentifier(shell);
064: if (wi == null)
065: return null;
066: return new MacroCommandShell(shell, wi.getWidgetId());
067: }
068:
069: private boolean isCurrent(Shell shell) {
070: if (shellStack.isEmpty())
071: return false;
072: MacroCommandShell cshell = (MacroCommandShell) shellStack
073: .peek();
074: return cshell.tracks(shell);
075: }
076:
077: public void stopRecording() {
078: reset();
079: }
080:
081: public boolean addEvent(Event event) throws Exception {
082: if (isIgnorableEvent(event))
083: return false;
084: try {
085: if (event.widget instanceof Shell) {
086: switch (event.type) {
087: case SWT.Activate:
088: activateShell((Shell) event.widget);
089: break;
090: case SWT.Close:
091: boolean stop = closeShell((Shell) event.widget);
092: if (stop)
093: return true;
094: break;
095: }
096: } else if (getTopShell() != null) {
097: getTopShell().addEvent(event);
098: }
099: } catch (Exception e) {
100: throw e;
101: }
102: return false;
103: }
104:
105: private boolean isIgnorableEvent(Event e) {
106: Shell shell = e.display.getActiveShell();
107: if (shell != null) {
108: Boolean ivalue = (Boolean) shell
109: .getData(MacroManager.IGNORE);
110: if (ivalue != null && ivalue.equals(Boolean.TRUE))
111: return true;
112: }
113: return false;
114: }
115:
116: public void addPause() {
117: getTopShell().addPause();
118: }
119:
120: public void addIndex(String id) {
121: getTopShell().addIndex(id);
122: }
123:
124: public MacroCommandShell getTopShell() {
125: if (shellStack.isEmpty())
126: return null;
127: return (MacroCommandShell) shellStack.peek();
128: }
129:
130: private void activateShell(Shell shell) {
131: Object data = shell.getData();
132: if (data instanceof Dialog) {
133: if (!isCurrent(shell)) {
134: MacroCommandShell commandShell = createCommandShell(shell);
135: getTopShell().addCommandShell(commandShell);
136: shellStack.push(commandShell);
137: }
138: } else if (data instanceof Window) {
139: updateStack();
140: if (!isCurrent(shell)) {
141: // pop the current
142: popStack();
143: MacroCommandShell commandShell = createCommandShell(shell);
144: shellStack.push(commandShell);
145: shells.add(commandShell);
146: }
147: }
148: }
149:
150: private void popStack() {
151: if (shellStack.isEmpty())
152: return;
153: MacroCommandShell top = (MacroCommandShell) shellStack.pop();
154: top.extractExpectedReturnCode();
155: }
156:
157: private boolean closeShell(Shell shell) {
158: if (shellStack.isEmpty())
159: return false;
160: MacroCommandShell top = (MacroCommandShell) shellStack.peek();
161: if (top.tracks(shell))
162: popStack();
163: return shellStack.isEmpty();
164: }
165:
166: private void updateStack() {
167: while (shellStack.size() > 0) {
168: MacroCommandShell top = getTopShell();
169: if (top.isDisposed())
170: popStack();
171: else
172: break;
173: }
174: }
175:
176: private void closeSecondaryShells() {
177: }
178:
179: public String[] getExistingIndices() {
180: ArrayList list = new ArrayList();
181: for (int i = 0; i < shells.size(); i++) {
182: MacroCommandShell shell = (MacroCommandShell) shells.get(i);
183: shell.addExistingIndices(list);
184: }
185: return (String[]) list.toArray(new String[list.size()]);
186: }
187:
188: public boolean playback(Display display, Composite parent,
189: IProgressMonitor monitor) throws CoreException {
190: reset();
191: String mname = name != null ? name : "macro";
192: monitor.beginTask("Executing " + mname + " ...", shells.size());
193: for (int i = 0; i < shells.size(); i++) {
194: MacroCommandShell shell = (MacroCommandShell) shells.get(i);
195: shell.setIndexHandler(getIndexHandler());
196: final Shell[] sh = new Shell[1];
197: display.syncExec(new Runnable() {
198: public void run() {
199: sh[0] = PlatformUI.getWorkbench()
200: .getActiveWorkbenchWindow().getShell();
201: }
202: });
203: try {
204: boolean result = shell.playback(display, sh[0],
205: new SubProgressMonitor(monitor, 1));
206: if (!result)
207: return false;
208: } catch (CoreException e) {
209: closeSecondaryShells();
210: throw e;
211: }
212: }
213: return true;
214: }
215:
216: private void reset() {
217: shellStack = null;
218: }
219:
220: public void write(String indent, PrintWriter writer) {
221: writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
222: writer.print("<macro version=\"");
223: writer.print(SYNTAX_VERSION);
224: writer.println("\">");
225: String cindent = " ";
226: for (int i = 0; i < shells.size(); i++) {
227: MacroCommandShell cshell = (MacroCommandShell) shells
228: .get(i);
229: cshell.write(cindent, writer);
230: }
231: writer.println("</macro>");
232: }
233:
234: public IIndexHandler getIndexHandler() {
235: return indexHandler;
236: }
237:
238: public void setIndexHandler(IIndexHandler indexHandler) {
239: this .indexHandler = indexHandler;
240: }
241:
242: public String getName() {
243: return name;
244: }
245:
246: }
|