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.Hashtable;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.swt.custom.CCombo;
018: import org.eclipse.swt.custom.CTabFolder;
019: import org.eclipse.swt.custom.CTabItem;
020: import org.eclipse.swt.widgets.Combo;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Display;
023: import org.eclipse.swt.widgets.Event;
024: import org.eclipse.swt.widgets.TabFolder;
025: import org.eclipse.swt.widgets.TabItem;
026: import org.eclipse.swt.widgets.Widget;
027: import org.w3c.dom.Node;
028:
029: public class ChoiceSelectionCommand extends MacroCommand {
030: public static final String TYPE = "choice-select";
031:
032: private String choiceId;
033:
034: public ChoiceSelectionCommand(WidgetIdentifier wid) {
035: super (wid);
036: }
037:
038: public String getType() {
039: return TYPE;
040: }
041:
042: public void processEvent(Event e) {
043: choiceId = computeChoiceId(e.widget, e.item);
044: }
045:
046: private String computeChoiceId(Widget widget, Widget item) {
047: int index = -1;
048:
049: if (widget instanceof Combo) {
050: Combo combo = (Combo) widget;
051: index = combo.getSelectionIndex();
052: } else if (widget instanceof CCombo) {
053: CCombo combo = (CCombo) widget;
054: index = combo.getSelectionIndex();
055: } else {
056: String id = MacroPlugin.getDefault().getMacroManager()
057: .resolveWidget(item);
058: if (id != null)
059: return id;
060: if (widget instanceof TabFolder) {
061: TabFolder tabFolder = (TabFolder) widget;
062: TabItem tabItem = (TabItem) item;
063: index = tabFolder.indexOf(tabItem);
064: } else if (widget instanceof CTabFolder) {
065: CTabFolder tabFolder = (CTabFolder) widget;
066: CTabItem tabItem = (CTabItem) item;
067: index = tabFolder.indexOf(tabItem);
068: }
069: }
070: return (index != -1) ? getPositionId(index) : null;
071: }
072:
073: private String getPositionId(int index) {
074: return "item#" + index;
075: }
076:
077: protected void load(Node node, Hashtable lineTable) {
078: super .load(node, lineTable);
079: choiceId = MacroUtil.getAttribute(node, "choiceId");
080: }
081:
082: public void write(String indent, PrintWriter writer) {
083: writer.print(indent);
084: writer.print("<command type=\"");
085: writer.print(getType());
086: writer.print("\" contextId=\"");
087: writer.print(getWidgetId().getContextId());
088: writer.print("\" widgetId=\"");
089: writer.print(getWidgetId().getWidgetId());
090: writer.print("\"");
091: if (choiceId != null) {
092: writer.print(" choiceId=\"");
093: writer.print(choiceId);
094: writer.print("\"");
095: }
096: writer.println("/>");
097: }
098:
099: public boolean playback(Display display, Composite parent,
100: IProgressMonitor monitor) throws CoreException {
101: CommandTarget target = MacroUtil.locateCommandTarget(parent,
102: getWidgetId(), getStartLine());
103: if (target == null)
104: return false;
105: target.setFocus();
106: Widget widget = target.getWidget();
107: if (widget instanceof TabFolder)
108: doSelect((TabFolder) widget);
109: else if (widget instanceof CTabFolder)
110: doSelect((CTabFolder) widget);
111: else if (widget instanceof Combo)
112: doSelect((Combo) widget);
113: else if (widget instanceof CCombo)
114: doSelect((CCombo) widget);
115: return true;
116: }
117:
118: private void doSelect(TabFolder tabFolder) {
119: TabItem[] items = tabFolder.getItems();
120: for (int i = 0; i < items.length; i++) {
121: TabItem item = items[i];
122: String id = computeChoiceId(tabFolder, item);
123: if (id != null && id.equals(choiceId)) {
124: tabFolder.setSelection(i);
125: break;
126: }
127: }
128: }
129:
130: private void doSelect(CTabFolder tabFolder) {
131: CTabItem[] items = tabFolder.getItems();
132: for (int i = 0; i < items.length; i++) {
133: CTabItem item = items[i];
134: String id = computeChoiceId(tabFolder, item);
135: if (id != null && id.equals(choiceId)) {
136: tabFolder.setSelection(i);
137: break;
138: }
139: }
140: }
141:
142: private void doSelect(Combo combo) {
143: int index = getIndexFromChoice();
144: if (index != -1)
145: combo.select(index);
146: }
147:
148: private void doSelect(CCombo combo) {
149: int index = getIndexFromChoice();
150: if (index != -1)
151: combo.select(index);
152: }
153:
154: private int getIndexFromChoice() {
155: int loc = choiceId.indexOf('#');
156: if (loc == -1)
157: return -1;
158: try {
159: return new Integer(choiceId.substring(loc + 1)).intValue();
160: } catch (NumberFormatException e) {
161: return -1;
162: }
163: }
164: }
|