KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > commands > QueryByObjectSelection


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.commands;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.commands.Command;
16 import org.eclipse.core.commands.IParameter;
17 import org.eclipse.core.commands.ParameterType;
18 import org.eclipse.core.commands.common.CommandException;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.DisposeEvent;
23 import org.eclipse.swt.events.DisposeListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Group;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.ui.ISelectionListener;
30 import org.eclipse.ui.ISelectionService;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PlatformUI;
34
35 public class QueryByObjectSelection extends QueryControl {
36     
37     private Label fObjectSelectionLabel;
38     private Label fLabel;
39     private SelectionTracker fSelectionTracker;
40     private Object JavaDoc fObjectSelection;
41     
42     public QueryByObjectSelection(CommandComposerPart csp, Composite comp) {
43         super(csp, comp);
44     }
45     
46
47     protected void createGroupContents(Group parent) {
48         Composite comp = fToolkit.createComposite(parent);
49         GridLayout layout = new GridLayout(2, false);
50         layout.marginHeight = layout.marginWidth = 0;
51         comp.setLayout(layout);
52         comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
53         fLabel = fToolkit.createLabel(comp, "selection: "); //$NON-NLS-1$
54
fObjectSelectionLabel = fToolkit.createLabel(comp, "<no selection>", SWT.BORDER); //$NON-NLS-1$
55
fObjectSelectionLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
56         
57         IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
58         if (activeWindow != null) {
59             ISelectionService selectionService = activeWindow.getSelectionService();
60             if (selectionService != null) {
61                 fSelectionTracker = new SelectionTracker(selectionService);
62             }
63         }
64         
65         parent.addDisposeListener(new DisposeListener() {
66             public void widgetDisposed(DisposeEvent e) {
67                 if (fSelectionTracker != null) {
68                     fSelectionTracker.dispose();
69                 }
70             }
71         });
72     }
73
74     protected String JavaDoc getName() {
75         return "Query Commands by selected object"; //$NON-NLS-1$
76
}
77     
78     private class SelectionTracker implements ISelectionListener {
79         
80         private final ISelectionService _selectionService;
81         
82         public SelectionTracker(ISelectionService selectionService) {
83             _selectionService = selectionService;
84             _selectionService.addSelectionListener(this);
85         }
86         
87         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
88             if (selection instanceof IStructuredSelection) {
89                 Object JavaDoc selected = ((IStructuredSelection) selection)
90                         .getFirstElement();
91                 
92                 if (selected != null) {
93                     fObjectSelection = selected;
94                     String JavaDoc typeName = selected.getClass().getName();
95                     fObjectSelectionLabel.setToolTipText(typeName);
96                     
97                     int dotPosition = typeName.lastIndexOf('.');
98                     if (dotPosition != -1) {
99                         typeName = typeName.substring(dotPosition+1);
100                     }
101                     fObjectSelectionLabel.setText(typeName);
102                 }
103             }
104         }
105         
106         public void dispose() {
107             _selectionService.removeSelectionListener(this);
108         }
109     }
110
111     private boolean hasTypedParameterMatch(Command command, Object JavaDoc object) throws CommandException {
112         IParameter[] params = command.getParameters();
113         if (params != null) {
114             for (int i = 0; i < params.length; i++) {
115                 IParameter param = params[i];
116                 ParameterType parameterType = command.getParameterType(param.getId());
117                 if (parameterType != null) {
118                     if (parameterType.isCompatible(object)) return true;
119                 }
120             }
121         }
122         
123         return false;
124     }
125     
126     protected Command[] getCommands() {
127         Object JavaDoc objectSelection = fObjectSelection;
128         if (objectSelection == null)
129             return null;
130         
131         ArrayList JavaDoc hitList = new ArrayList JavaDoc();
132         Command[] commands = getCommandService().getDefinedCommands();
133         for (int i = 0; i < commands.length; i++) {
134             Command command = commands[i];
135             try {
136                 if (hasTypedParameterMatch(command, objectSelection))
137                     hitList.add(command);
138             }
139             catch (CommandException ex) {
140             }
141         }
142         
143         return (Command[]) hitList.toArray(new Command[hitList.size()]);
144     }
145     
146     protected void enable(boolean enable) {
147         fGroup.setEnabled(enable);
148         fLabel.setEnabled(enable);
149         fObjectSelectionLabel.setEnabled(enable);
150     }
151 }
152
Popular Tags