001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.HashMap;
013:
014: import org.eclipse.core.commands.ParameterizedCommand;
015: import org.eclipse.core.expressions.IEvaluationContext;
016: import org.eclipse.jface.dialogs.IMessageProvider;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.SelectionChangedEvent;
020: import org.eclipse.pde.internal.ui.PDEPluginImages;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.custom.SashForm;
024: import org.eclipse.swt.graphics.Image;
025: import org.eclipse.swt.layout.GridData;
026: import org.eclipse.swt.layout.GridLayout;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.ui.IWorkbench;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.commands.ICommandService;
031: import org.eclipse.ui.forms.widgets.FormToolkit;
032: import org.eclipse.ui.forms.widgets.ScrolledForm;
033:
034: /**
035: * This class is meant to be used to integrate the Command Composer into a UI (View, Dialog)
036: * <ul>
037: * <li>setFilterType(int)</li>
038: * <li>setButtonCreator(IDialogButtonCreator)</li>
039: * <li>setPresetCommand(ParameterizedCommand)</li>
040: * <li>setSnapshotContext(IEvaluationContext) - optional</li>
041: * </ul>
042: *
043: * should all be called before creating the actual control:
044: *
045: * Scrolled form = CommandComposerPart#createForm(Composite)
046: * CommandComposerPart#createPartControl(form)
047: *
048: */
049: public class CommandComposerPart implements ISelectionChangedListener {
050:
051: public static final int F_FILTER_NOT_SET = CommandCopyFilter
052: .indexOf(CommandCopyFilter.NONE);
053: public static final int F_HELP_FILTER = CommandCopyFilter
054: .indexOf(CommandCopyFilter.HELP);
055: public static final int F_CHEATSHEET_FILTER = CommandCopyFilter
056: .indexOf(CommandCopyFilter.CHEATSHEET);
057: public static final int F_INTRO_FILTER = CommandCopyFilter
058: .indexOf(CommandCopyFilter.INTRO);
059:
060: private static final ICommandService fCommandService = initCommandService();
061:
062: private final TagManager fTagManager = new TagManager();
063: private FormToolkit fToolkit;
064: private ScrolledForm fScrolledForm;
065: private CommandList fCommandList;
066: private CommandDetails fCommandDetails;
067: private int fFilterType = F_FILTER_NOT_SET;
068: private ParameterizedCommand fPC;
069: private Image fCommandImage;
070: private IEvaluationContext fSnapshotContext;
071:
072: private static ICommandService initCommandService() {
073: IWorkbench workbench = PlatformUI.getWorkbench();
074: Object serviceObject = workbench
075: .getAdapter(ICommandService.class);
076: if (serviceObject instanceof ICommandService)
077: return (ICommandService) serviceObject;
078: return null;
079: }
080:
081: public void setFilterType(int filterType) {
082: fFilterType = filterType;
083: }
084:
085: public int getFilterType() {
086: return fFilterType;
087: }
088:
089: /**
090: * Set a snapshot context to be used by the command details section of this
091: * part.
092: *
093: * @param context
094: * the context to use. May be <code>null</code>.
095: * @since 3.3
096: */
097: public void setSnapshotContext(IEvaluationContext context) {
098: fSnapshotContext = context;
099: }
100:
101: /**
102: * @return
103: */
104: public IEvaluationContext getSnapshotContext() {
105: return fSnapshotContext;
106: }
107:
108: protected void createCC(ScrolledForm form, FormToolkit toolkit,
109: ISelectionChangedListener listener) {
110: fToolkit = toolkit;
111: fScrolledForm = form;
112: fScrolledForm
113: .setText(PDEUIMessages.CommandComposerPart_formTitle);
114: fCommandImage = PDEPluginImages.DESC_BUILD_VAR_OBJ
115: .createImage();
116: fScrolledForm.setImage(fCommandImage);
117: Composite body = fScrolledForm.getBody();
118:
119: GridLayout layout = new GridLayout();
120: layout.marginTop = 10;
121: body.setLayout(layout);
122: body.setLayoutData(new GridData(GridData.FILL_BOTH));
123:
124: SashForm sashForm = new SashForm(body, SWT.HORIZONTAL);
125: sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
126:
127: fCommandList = new CommandList(this , sashForm);
128: if (listener != null)
129: fCommandList.addTreeSelectionListener(listener);
130:
131: fCommandDetails = new CommandDetails(this , sashForm);
132:
133: sashForm.setWeights(new int[] { 4, 5 });
134: fToolkit.adapt(sashForm, true, true);
135:
136: if (fPC != null)
137: fCommandList.setSelection(fPC.getCommand());
138:
139: fPC = null;
140: }
141:
142: protected void setMessage(String message, int newType) {
143: fScrolledForm.getForm().setMessage(message, newType);
144: }
145:
146: public FormToolkit getToolkit() {
147: return fToolkit;
148: }
149:
150: public ICommandService getCommandService() {
151: return fCommandService;
152: }
153:
154: public TagManager getTagManager() {
155: return fTagManager;
156: }
157:
158: public void setFocus() {
159: fCommandList.setFocus();
160: }
161:
162: public void dispose() {
163: fCommandDetails.dispose();
164: if (fCommandImage != null) {
165: fCommandImage.dispose();
166: fCommandImage = null;
167: }
168: }
169:
170: protected String getSelectedCommandName() {
171: return fCommandDetails.getCommandName();
172: }
173:
174: protected String getSelectedSerializedString() {
175: return fCommandDetails.getSerializedString();
176: }
177:
178: protected HashMap getSelectedCommandsParameters() {
179: return fCommandDetails.getParameters();
180: }
181:
182: protected Composite createComposite(Composite parent) {
183: return createComposite(parent, GridData.FILL_BOTH, 1, true, 0);
184: }
185:
186: protected Composite createComposite(Composite parent, int gdStyle,
187: int numCol, boolean colEqual, int margin) {
188: Composite comp = fToolkit.createComposite(parent);
189: GridLayout layout = new GridLayout(numCol, colEqual);
190: layout.marginHeight = layout.marginWidth = margin;
191: comp.setLayout(layout);
192: comp.setLayoutData(new GridData(gdStyle));
193: return comp;
194: }
195:
196: public void selectionChanged(SelectionChangedEvent event) {
197: // Clear the previous error message (if any)
198: // Field input value is lost on selection any way
199: setMessage(null, IMessageProvider.NONE);
200:
201: Object selectionObject = null;
202: // if preselection exists use that
203: if (fPC != null)
204: selectionObject = fPC;
205: else if (event.getSelection() instanceof IStructuredSelection)
206: selectionObject = (((IStructuredSelection) event
207: .getSelection()).getFirstElement());
208: if (selectionObject != null
209: && selectionObject.equals(fCommandDetails.getCommand()))
210: return;
211: fCommandDetails.showDetailsFor(selectionObject);
212: }
213:
214: public ParameterizedCommand getParameterizedCommand() {
215: return fCommandDetails.buildParameterizedCommand();
216: }
217:
218: protected void setPresetCommand(ParameterizedCommand pc) {
219: fPC = pc;
220: }
221:
222: protected ParameterizedCommand getPresetCommand() {
223: return fPC;
224: }
225:
226: /**
227: * @return
228: */
229: public CommandList getCommandList() {
230: return fCommandList;
231: }
232: }
|