Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cdbdd6fa4a3bd802621e185e8adfafc22043b0e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 *  Copyright (c) 2006, 2012 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.ui.commands;

import java.util.HashMap;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.*;
import org.eclipse.pde.internal.ui.PDEPluginImages;
import org.eclipse.pde.internal.ui.PDEUIMessages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * This class is meant to be used to integrate the Command Composer into a UI (View, Dialog)
 * <ul>
 * <li>setFilterType(int)</li>
 * <li>setButtonCreator(IDialogButtonCreator)</li>
 * <li>setPresetCommand(ParameterizedCommand)</li>
 * <li>setSnapshotContext(IEvaluationContext) - optional</li>
 * </ul>
 * 
 * should all be called before creating the actual control:
 * 
 * Scrolled form = CommandComposerPart#createForm(Composite)
 * CommandComposerPart#createPartControl(form)
 * 
 */
public class CommandComposerPart implements ISelectionChangedListener {

	public static final int F_FILTER_NOT_SET = CommandCopyFilter.indexOf(CommandCopyFilter.NONE);
	public static final int F_HELP_FILTER = CommandCopyFilter.indexOf(CommandCopyFilter.HELP);
	public static final int F_CHEATSHEET_FILTER = CommandCopyFilter.indexOf(CommandCopyFilter.CHEATSHEET);
	public static final int F_INTRO_FILTER = CommandCopyFilter.indexOf(CommandCopyFilter.INTRO);

	private static final ICommandService fCommandService = initCommandService();

	private final TagManager fTagManager = new TagManager();
	private FormToolkit fToolkit;
	private ScrolledForm fScrolledForm;
	private CommandList fCommandList;
	private CommandDetails fCommandDetails;
	private int fFilterType = F_FILTER_NOT_SET;
	private ParameterizedCommand fPC;
	private Image fCommandImage;
	private IEvaluationContext fSnapshotContext;

	private static ICommandService initCommandService() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		Object serviceObject = workbench.getAdapter(ICommandService.class);
		if (serviceObject instanceof ICommandService)
			return (ICommandService) serviceObject;
		return null;
	}

	public void setFilterType(int filterType) {
		fFilterType = filterType;
	}

	public int getFilterType() {
		return fFilterType;
	}

	/**
	 * Set a snapshot context to be used by the command details section of this
	 * part.
	 * 
	 * @param context
	 *            the context to use. May be <code>null</code>.
	 * @since 3.3
	 */
	public void setSnapshotContext(IEvaluationContext context) {
		fSnapshotContext = context;
	}

	public IEvaluationContext getSnapshotContext() {
		return fSnapshotContext;
	}

	protected void createCC(ScrolledForm form, FormToolkit toolkit, ISelectionChangedListener listener) {
		fToolkit = toolkit;
		fScrolledForm = form;
		fScrolledForm.setText(PDEUIMessages.CommandComposerPart_formTitle);
		fCommandImage = PDEPluginImages.DESC_BUILD_VAR_OBJ.createImage();
		fScrolledForm.setImage(fCommandImage);
		Composite body = fScrolledForm.getBody();

		GridLayout layout = new GridLayout();
		layout.marginTop = 10;
		body.setLayout(layout);
		body.setLayoutData(new GridData(GridData.FILL_BOTH));

		SashForm sashForm = new SashForm(body, SWT.HORIZONTAL);
		sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));

		fCommandList = new CommandList(this, sashForm);
		if (listener != null)
			fCommandList.addTreeSelectionListener(listener);

		fCommandDetails = new CommandDetails(this, sashForm);

		sashForm.setWeights(new int[] {4, 5});
		fToolkit.adapt(sashForm, true, true);

		if (fPC != null)
			fCommandList.setSelection(fPC.getCommand());

		fPC = null;
	}

	protected void setMessage(String message, int newType) {
		fScrolledForm.getForm().setMessage(message, newType);
	}

	public FormToolkit getToolkit() {
		return fToolkit;
	}

	public ICommandService getCommandService() {
		return fCommandService;
	}

	public TagManager getTagManager() {
		return fTagManager;
	}

	public void setFocus() {
		fCommandList.setFocus();
	}

	public void dispose() {
		fCommandDetails.dispose();
		if (fCommandImage != null) {
			fCommandImage.dispose();
			fCommandImage = null;
		}
	}

	protected String getSelectedCommandName() {
		return fCommandDetails.getCommandName();
	}

	protected String getSelectedSerializedString() {
		return fCommandDetails.getSerializedString();
	}

	protected HashMap<?, ?> getSelectedCommandsParameters() {
		return fCommandDetails.getParameters();
	}

	protected Composite createComposite(Composite parent) {
		return createComposite(parent, GridData.FILL_BOTH, 1, true, 0);
	}

	protected Composite createComposite(Composite parent, int gdStyle, int numCol, boolean colEqual, int margin) {
		Composite comp = fToolkit.createComposite(parent);
		GridLayout layout = new GridLayout(numCol, colEqual);
		layout.marginHeight = layout.marginWidth = margin;
		comp.setLayout(layout);
		comp.setLayoutData(new GridData(gdStyle));
		return comp;
	}

	public void selectionChanged(SelectionChangedEvent event) {
		// Clear the previous error message (if any)
		// Field input value is lost on selection any way
		setMessage(null, IMessageProvider.NONE);

		Object selectionObject = null;
		// if preselection exists use that
		if (fPC != null)
			selectionObject = fPC;
		else if (event.getSelection() instanceof IStructuredSelection)
			selectionObject = (((IStructuredSelection) event.getSelection()).getFirstElement());
		if (selectionObject != null && selectionObject.equals(fCommandDetails.getCommand()))
			return;
		fCommandDetails.showDetailsFor(selectionObject);
	}

	public ParameterizedCommand getParameterizedCommand() {
		return fCommandDetails.buildParameterizedCommand();
	}

	protected void setPresetCommand(ParameterizedCommand pc) {
		fPC = pc;
	}

	protected ParameterizedCommand getPresetCommand() {
		return fPC;
	}

	public CommandList getCommandList() {
		return fCommandList;
	}
}

Back to the top