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.util.ArrayList;
13:
14: import org.eclipse.swt.SWT;
15: import org.eclipse.swt.custom.TableTree;
16: import org.eclipse.swt.custom.TableTreeItem;
17: import org.eclipse.swt.widgets.Event;
18: import org.eclipse.swt.widgets.Table;
19: import org.eclipse.swt.widgets.TableItem;
20: import org.eclipse.swt.widgets.Tree;
21: import org.eclipse.swt.widgets.TreeItem;
22: import org.eclipse.swt.widgets.Widget;
23:
24: public class StructuredSelectionCommand extends
25: AbstractStructuredCommand {
26: private String type;
27: public static final String DEFAULT_SELECT = "default-select";
28: public static final String ITEM_SELECT = "item-select";
29:
30: public StructuredSelectionCommand(WidgetIdentifier wid, String type) {
31: super (wid);
32: items = new ArrayList();
33: this .type = type;
34: }
35:
36: public boolean mergeEvent(Event e) {
37: if (e.type == SWT.DefaultSelection) {
38: this .type = DEFAULT_SELECT;
39: }
40: return super .mergeEvent(e);
41: }
42:
43: public String getType() {
44: return type;
45: }
46:
47: protected Widget[] getItemsForEvent(Event event) {
48: if (event.widget instanceof Tree) {
49: return ((Tree) event.widget).getSelection();
50: } else if (event.widget instanceof Table) {
51: return ((Table) event.widget).getSelection();
52: } else if (event.widget instanceof TableTree) {
53: return ((TableTree) event.widget).getSelection();
54: }
55: return super .getItemsForEvent(event);
56: }
57:
58: protected void playTreeCommand(Tree tree, TreeItem[] matches) {
59: tree.setSelection(matches);
60: fireEvent(tree, matches);
61: }
62:
63: private void fireEvent(Widget widget, Widget[] items) {
64: Event e = new Event();
65: e.widget = widget;
66: e.type = type.equals(ITEM_SELECT) ? SWT.Selection
67: : SWT.DefaultSelection;
68: e.item = items.length > 0 ? items[0] : null;
69: widget.notifyListeners(e.type, e);
70: }
71:
72: protected void playTableCommand(Table table, TableItem[] matches) {
73: table.setSelection(matches);
74: fireEvent(table, matches);
75: }
76:
77: protected void playTableTreeCommand(TableTree tableTree,
78: TableTreeItem[] matches) {
79: tableTree.setSelection(matches);
80: fireEvent(tableTree, matches);
81: }
82: }
|