001: /*******************************************************************************
002: * Copyright (c) 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.commands;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.commands.Command;
015: import org.eclipse.core.commands.IParameter;
016: import org.eclipse.core.commands.ParameterType;
017: import org.eclipse.core.commands.common.CommandException;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.events.DisposeEvent;
022: import org.eclipse.swt.events.DisposeListener;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Group;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.ui.ISelectionListener;
029: import org.eclipse.ui.ISelectionService;
030: import org.eclipse.ui.IWorkbenchPart;
031: import org.eclipse.ui.IWorkbenchWindow;
032: import org.eclipse.ui.PlatformUI;
033:
034: public class QueryByObjectSelection extends QueryControl {
035:
036: private Label fObjectSelectionLabel;
037: private Label fLabel;
038: private SelectionTracker fSelectionTracker;
039: private Object fObjectSelection;
040:
041: public QueryByObjectSelection(CommandComposerPart csp,
042: Composite comp) {
043: super (csp, comp);
044: }
045:
046: protected void createGroupContents(Group parent) {
047: Composite comp = fToolkit.createComposite(parent);
048: GridLayout layout = new GridLayout(2, false);
049: layout.marginHeight = layout.marginWidth = 0;
050: comp.setLayout(layout);
051: comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
052: fLabel = fToolkit.createLabel(comp, "selection: "); //$NON-NLS-1$
053: fObjectSelectionLabel = fToolkit.createLabel(comp,
054: "<no selection>", SWT.BORDER); //$NON-NLS-1$
055: fObjectSelectionLabel.setLayoutData(new GridData(
056: GridData.FILL_HORIZONTAL));
057:
058: IWorkbenchWindow activeWindow = PlatformUI.getWorkbench()
059: .getActiveWorkbenchWindow();
060: if (activeWindow != null) {
061: ISelectionService selectionService = activeWindow
062: .getSelectionService();
063: if (selectionService != null) {
064: fSelectionTracker = new SelectionTracker(
065: selectionService);
066: }
067: }
068:
069: parent.addDisposeListener(new DisposeListener() {
070: public void widgetDisposed(DisposeEvent e) {
071: if (fSelectionTracker != null) {
072: fSelectionTracker.dispose();
073: }
074: }
075: });
076: }
077:
078: protected String getName() {
079: return "Query Commands by selected object"; //$NON-NLS-1$
080: }
081:
082: private class SelectionTracker implements ISelectionListener {
083:
084: private final ISelectionService _selectionService;
085:
086: public SelectionTracker(ISelectionService selectionService) {
087: _selectionService = selectionService;
088: _selectionService.addSelectionListener(this );
089: }
090:
091: public void selectionChanged(IWorkbenchPart part,
092: ISelection selection) {
093: if (selection instanceof IStructuredSelection) {
094: Object selected = ((IStructuredSelection) selection)
095: .getFirstElement();
096:
097: if (selected != null) {
098: fObjectSelection = selected;
099: String typeName = selected.getClass().getName();
100: fObjectSelectionLabel.setToolTipText(typeName);
101:
102: int dotPosition = typeName.lastIndexOf('.');
103: if (dotPosition != -1) {
104: typeName = typeName.substring(dotPosition + 1);
105: }
106: fObjectSelectionLabel.setText(typeName);
107: }
108: }
109: }
110:
111: public void dispose() {
112: _selectionService.removeSelectionListener(this );
113: }
114: }
115:
116: private boolean hasTypedParameterMatch(Command command,
117: Object object) throws CommandException {
118: IParameter[] params = command.getParameters();
119: if (params != null) {
120: for (int i = 0; i < params.length; i++) {
121: IParameter param = params[i];
122: ParameterType parameterType = command
123: .getParameterType(param.getId());
124: if (parameterType != null) {
125: if (parameterType.isCompatible(object))
126: return true;
127: }
128: }
129: }
130:
131: return false;
132: }
133:
134: protected Command[] getCommands() {
135: Object objectSelection = fObjectSelection;
136: if (objectSelection == null)
137: return null;
138:
139: ArrayList hitList = new ArrayList();
140: Command[] commands = getCommandService().getDefinedCommands();
141: for (int i = 0; i < commands.length; i++) {
142: Command command = commands[i];
143: try {
144: if (hasTypedParameterMatch(command, objectSelection))
145: hitList.add(command);
146: } catch (CommandException ex) {
147: }
148: }
149:
150: return (Command[]) hitList.toArray(new Command[hitList.size()]);
151: }
152:
153: protected void enable(boolean enable) {
154: fGroup.setEnabled(enable);
155: fLabel.setEnabled(enable);
156: fObjectSelectionLabel.setEnabled(enable);
157: }
158: }
|