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.jface.action.ContributionItem;
019: import org.eclipse.jface.action.MenuManager;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.widgets.Button;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Display;
024: import org.eclipse.swt.widgets.Event;
025: import org.eclipse.swt.widgets.MenuItem;
026: import org.eclipse.swt.widgets.ToolItem;
027: import org.eclipse.swt.widgets.Widget;
028: import org.w3c.dom.Node;
029: import org.w3c.dom.NodeList;
030:
031: public class BooleanSelectionCommand extends MacroCommand {
032: public static final String TYPE = "select";
033: private Boolean selection;
034: private ArrayList path;
035:
036: public BooleanSelectionCommand(WidgetIdentifier wid) {
037: super (wid);
038: }
039:
040: public String getType() {
041: return TYPE;
042: }
043:
044: public void processEvent(Event e) {
045: selection = getSelection(e.widget);
046: if (e.widget instanceof MenuItem) {
047: //System.out.println("Item="+e.widget+" data = "+e.widget.getData());
048: path = getPath((MenuItem) e.widget);
049: }
050: }
051:
052: private Boolean getSelection(Widget widget) {
053: if ((widget.getStyle() & (SWT.CHECK | SWT.RADIO)) == 0)
054: return null;
055: if (widget instanceof Button)
056: return new Boolean(((Button) widget).getSelection());
057: if (widget instanceof ToolItem)
058: return new Boolean(((ToolItem) widget).getSelection());
059: if (widget instanceof MenuItem)
060: return new Boolean(((MenuItem) widget).getSelection());
061: return null;
062: }
063:
064: private ArrayList getPath(MenuItem item) {
065: ArrayList segments = new ArrayList();
066: Object data = item.getData();
067:
068: if (data instanceof ContributionItem) {
069: ContributionItem aitem = (ContributionItem) data;
070: MenuManager manager = (MenuManager) aitem.getParent();
071: while (manager != null) {
072: String id = manager.getId();
073: if (id == null)
074: break;
075: segments.add(0, id);
076: manager = (MenuManager) manager.getParent();
077: }
078: }
079: return segments.size() > 0 ? segments : null;
080: }
081:
082: protected void load(Node node, Hashtable lineTable) {
083: super .load(node, lineTable);
084: String sel = MacroUtil.getAttribute(node, "selection");
085: if (sel != null) {
086: selection = sel.equals("true") ? Boolean.TRUE
087: : Boolean.FALSE;
088: }
089: NodeList children = node.getChildNodes();
090: for (int i = 0; i < children.getLength(); i++) {
091: Node child = children.item(i);
092: if (child.getNodeType() == Node.ELEMENT_NODE
093: && child.getNodeName().equals("parent")) {
094: if (path == null)
095: path = new ArrayList();
096: path.add(MacroUtil.getAttribute(child, "widgetId"));
097: }
098: }
099: }
100:
101: public void write(String indent, PrintWriter writer) {
102: writer.print(indent);
103: writer.print("<command type=\"");
104: writer.print(getType());
105: writer.print("\" contextId=\"");
106: writer.print(getWidgetId().getContextId());
107: writer.print("\" widgetId=\"");
108: writer.print(getWidgetId().getWidgetId());
109: writer.print("\"");
110: if (selection != null) {
111: writer.print(" selection=\"");
112: writer.print(selection.equals(Boolean.TRUE) ? "true"
113: : "false");
114: writer.print("\"");
115: }
116: if (path != null) {
117: writer.println(">");
118: String pindent = indent + " ";
119: for (int i = 0; i < path.size(); i++) {
120: writer.print(pindent);
121: writer.print("<parent widgetId=\"");
122: writer.print((String) path.get(i));
123: writer.println("\"/>");
124: }
125: writer.print(indent);
126: writer.println("</command>");
127: } else
128: writer.println("/>");
129: }
130:
131: public boolean playback(Display display, Composite parent,
132: IProgressMonitor monitor) throws CoreException {
133: CommandTarget target = MacroUtil.locateCommandTarget(parent,
134: getWidgetId(), path, getStartLine());
135: if (target == null)
136: return false;
137: target.setFocus();
138: Widget widget = target.getWidget();
139:
140: if ((widget.getStyle() & (SWT.CHECK | SWT.RADIO)) == 0) {
141: doClick(widget);
142: } else if (selection != null)
143: doSelect(widget);
144: return true;
145: }
146:
147: private void doClick(Widget widget) throws CoreException {
148: Event e = new Event();
149: e.type = SWT.Selection;
150: e.widget = widget;
151: widget.notifyListeners(e.type, e);
152: }
153:
154: protected Event createMouseEvent(Widget widget, int type) {
155: Event e = new Event();
156: e.type = type;
157: e.button = 1;
158: e.widget = widget;
159: return e;
160: }
161:
162: private void doSelect(Widget widget) throws CoreException {
163: if (widget instanceof Button)
164: ((Button) widget).setSelection(selection.booleanValue());
165: else if (widget instanceof ToolItem)
166: ((ToolItem) widget).setSelection(selection.booleanValue());
167: else if (widget instanceof MenuItem)
168: ((MenuItem) widget).setSelection(selection.booleanValue());
169: }
170: }
|