001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.swt.custom.TableTree;
019: import org.eclipse.swt.custom.TableTreeItem;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Display;
022: import org.eclipse.swt.widgets.Event;
023: import org.eclipse.swt.widgets.Item;
024: import org.eclipse.swt.widgets.Table;
025: import org.eclipse.swt.widgets.TableItem;
026: import org.eclipse.swt.widgets.Tree;
027: import org.eclipse.swt.widgets.TreeItem;
028: import org.eclipse.swt.widgets.Widget;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031:
032: public abstract class AbstractStructuredCommand extends MacroCommand {
033: protected ArrayList items;
034:
035: public AbstractStructuredCommand(WidgetIdentifier wid) {
036: super (wid);
037: items = new ArrayList();
038: }
039:
040: public boolean mergeEvent(Event e) {
041: items.clear();
042: processEvent(e);
043: return true;
044: }
045:
046: protected Widget[] getItemsForEvent(Event e) {
047: Widget item = null;
048: if (e.item != null)
049: item = (Widget) e.item;
050: else if (e.widget instanceof Item)
051: item = e.widget;
052: if (item != null)
053: return new Widget[] { item };
054: return null;
055: }
056:
057: public void processEvent(Event event) {
058: Widget[] eventItems = getItemsForEvent(event);
059:
060: if (eventItems != null) {
061: for (int i = 0; i < eventItems.length; i++) {
062: String id = getItemId(eventItems[i]);
063: if (id != null)
064: items.add(id);
065: }
066: }
067: }
068:
069: protected String getItemId(Widget item) {
070: MacroManager recorder = MacroPlugin.getDefault()
071: .getMacroManager();
072: String id = recorder.resolveWidget(item);
073: if (id != null)
074: return id;
075: Object data = item.getData();
076: if (data != null)
077: return data.getClass().getName();
078: return null;
079: }
080:
081: protected void load(Node node, Hashtable lineTable) {
082: super .load(node, lineTable);
083: NodeList children = node.getChildNodes();
084: for (int i = 0; i < children.getLength(); i++) {
085: Node child = children.item(i);
086: if (child.getNodeType() == Node.ELEMENT_NODE
087: && child.getNodeName().equals("item")) {
088: String path = MacroUtil.getAttribute(child, "path");
089: if (path != null)
090: items.add(path);
091: }
092: }
093: }
094:
095: protected void writeAdditionalAttributes(PrintWriter writer) {
096: }
097:
098: public void write(String indent, PrintWriter writer) {
099: writer.print(indent);
100: writer.print("<command type=\"");
101: writer.print(getType());
102: writer.print("\" contextId=\"");
103: writer.print(getWidgetId().getContextId());
104: writer.print("\" widgetId=\"");
105: writer.print(getWidgetId().getWidgetId());
106: writer.print("\"");
107: writeAdditionalAttributes(writer);
108: writer.println(">");
109: String cindent = indent + " ";
110: for (int i = 0; i < items.size(); i++) {
111: writer.print(cindent);
112: writer.print("<item path=\"");
113: writer.print((String) items.get(i));
114: writer.println("\"/>");
115: }
116: writer.println(indent + "</command>");
117: }
118:
119: protected abstract void playTreeCommand(Tree tree,
120: TreeItem[] matches);
121:
122: protected abstract void playTableCommand(Table table,
123: TableItem[] matches);
124:
125: protected abstract void playTableTreeCommand(TableTree tableTree,
126: TableTreeItem[] matches);
127:
128: public final boolean playback(Display display, Composite parent,
129: IProgressMonitor monitor) throws CoreException {
130: CommandTarget target = MacroUtil.locateCommandTarget(parent,
131: getWidgetId(), getStartLine());
132:
133: if (target == null)
134: return false;
135: target.setFocus();
136: MacroUtil.processDisplayEvents(display);
137:
138: Widget widget = target.getWidget();
139:
140: if (widget == null || widget.isDisposed())
141: return false;
142:
143: if (widget instanceof Tree) {
144: TreeItem[] matches = findMatches((Tree) widget);
145: playTreeCommand((Tree) widget, matches);
146: } else if (widget instanceof Table) {
147: TableItem[] matches = findMatches((Table) widget);
148: playTableCommand((Table) widget, matches);
149: } else if (widget instanceof TableTree) {
150: TableTreeItem[] matches = findMatches((TableTree) widget);
151: playTableTreeCommand((TableTree) widget, matches);
152: }
153: return true;
154: }
155:
156: private TreeItem[] findMatches(Tree tree) {
157: TreeItem[] children = tree.getItems();
158: ArrayList matches = new ArrayList();
159: for (int i = 0; i < items.size(); i++) {
160: String itemId = (String) items.get(i);
161: TreeItem item = findTreeItem(children, itemId);
162: if (item != null)
163: matches.add(item);
164: }
165: return (TreeItem[]) matches
166: .toArray(new TreeItem[matches.size()]);
167: }
168:
169: private TableItem[] findMatches(Table table) {
170: TableItem[] elements = table.getItems();
171: ArrayList matches = new ArrayList();
172:
173: for (int i = 0; i < items.size(); i++) {
174: String itemId = (String) items.get(i);
175: TableItem item = findTableItem(elements, itemId);
176: if (item != null)
177: matches.add(item);
178: }
179: return (TableItem[]) matches.toArray(new TableItem[matches
180: .size()]);
181: }
182:
183: private TableTreeItem[] findMatches(TableTree tableTree) {
184: TableTreeItem[] children = tableTree.getItems();
185: ArrayList matches = new ArrayList();
186:
187: for (int i = 0; i < items.size(); i++) {
188: String itemId = (String) items.get(i);
189: TableTreeItem item = findTableTreeItem(children, itemId);
190: if (item != null)
191: matches.add(item);
192: }
193: return (TableTreeItem[]) matches
194: .toArray(new TableTreeItem[matches.size()]);
195: }
196:
197: private TreeItem findTreeItem(TreeItem[] children, String itemId) {
198: for (int i = 0; i < children.length; i++) {
199: TreeItem item = children[i];
200: String id = getItemId(item);
201: //Test the item itself
202: if (id != null && id.equals(itemId))
203: return item;
204: int ccount = item.getItemCount();
205: if (ccount > 0) {
206: //Test the item's children
207: TreeItem citem = findTreeItem(item.getItems(), itemId);
208: if (citem != null)
209: return citem;
210: }
211: }
212: return null;
213: }
214:
215: private TableItem findTableItem(TableItem[] children, String itemId) {
216: for (int i = 0; i < children.length; i++) {
217: TableItem item = children[i];
218: String id = getItemId(item);
219:
220: if (id != null && id.equals(itemId))
221: return item;
222: }
223: return null;
224: }
225:
226: private TableTreeItem findTableTreeItem(TableTreeItem[] children,
227: String itemId) {
228: for (int i = 0; i < children.length; i++) {
229: TableTreeItem item = children[i];
230: String id = getItemId(item);
231: //Test the item itself
232: if (id != null && id.equals(itemId))
233: return item;
234: int ccount = item.getItemCount();
235: if (ccount > 0) {
236: //Test the item's children
237: TableTreeItem citem = findTableTreeItem(
238: item.getItems(), itemId);
239: if (citem != null)
240: return citem;
241: }
242: }
243: return null;
244: }
245: }
|