KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.HashMap JavaDoc;
14
15 import org.eclipse.core.commands.ParameterizedCommand;
16 import org.eclipse.core.expressions.IEvaluationContext;
17 import org.eclipse.jface.dialogs.IMessageProvider;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.pde.internal.ui.PDEPluginImages;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.SashForm;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.commands.ICommandService;
32 import org.eclipse.ui.forms.widgets.FormToolkit;
33 import org.eclipse.ui.forms.widgets.ScrolledForm;
34
35 /**
36  * This class is meant to be used to integrate the Command Composer into a UI (View, Dialog)
37  * <ul>
38  * <li>setFilterType(int)</li>
39  * <li>setButtonCreator(IDialogButtonCreator)</li>
40  * <li>setPresetCommand(ParameterizedCommand)</li>
41  * <li>setSnapshotContext(IEvaluationContext) - optional</li>
42  * </ul>
43  *
44  * should all be called before creating the actual control:
45  *
46  * Scrolled form = CommandComposerPart#createForm(Composite)
47  * CommandComposerPart#createPartControl(form)
48  *
49  */

50 public class CommandComposerPart implements ISelectionChangedListener {
51     
52     public static final int F_FILTER_NOT_SET = CommandCopyFilter.indexOf(CommandCopyFilter.NONE);
53     public static final int F_HELP_FILTER = CommandCopyFilter.indexOf(CommandCopyFilter.HELP);
54     public static final int F_CHEATSHEET_FILTER = CommandCopyFilter.indexOf(CommandCopyFilter.CHEATSHEET);
55     public static final int F_INTRO_FILTER = CommandCopyFilter.indexOf(CommandCopyFilter.INTRO);
56     
57     private static final ICommandService fCommandService = initCommandService();
58     
59     private final TagManager fTagManager = new TagManager();
60     private FormToolkit fToolkit;
61     private ScrolledForm fScrolledForm;
62     private CommandList fCommandList;
63     private CommandDetails fCommandDetails;
64     private int fFilterType = F_FILTER_NOT_SET;
65     private ParameterizedCommand fPC;
66     private Image fCommandImage;
67     private IEvaluationContext fSnapshotContext;
68     
69     private static ICommandService initCommandService() {
70         IWorkbench workbench = PlatformUI.getWorkbench();
71         Object JavaDoc serviceObject = workbench.getAdapter(ICommandService.class);
72         if (serviceObject instanceof ICommandService)
73             return (ICommandService) serviceObject;
74         return null;
75     }
76     
77     public void setFilterType(int filterType) {
78         fFilterType = filterType;
79     }
80     
81     public int getFilterType() {
82         return fFilterType;
83     }
84     
85     /**
86      * Set a snapshot context to be used by the command details section of this
87      * part.
88      *
89      * @param context
90      * the context to use. May be <code>null</code>.
91      * @since 3.3
92      */

93     public void setSnapshotContext(IEvaluationContext context) {
94         fSnapshotContext = context;
95     }
96     
97     /**
98      * @return
99      */

100     public IEvaluationContext getSnapshotContext() {
101         return fSnapshotContext;
102     }
103     
104     protected void createCC(ScrolledForm form, FormToolkit toolkit, ISelectionChangedListener listener) {
105         fToolkit = toolkit;
106         fScrolledForm = form;
107         fScrolledForm.setText(PDEUIMessages.CommandComposerPart_formTitle);
108         fCommandImage = PDEPluginImages.DESC_BUILD_VAR_OBJ.createImage();
109         fScrolledForm.setImage(fCommandImage);
110         Composite body = fScrolledForm.getBody();
111         
112         GridLayout layout = new GridLayout();
113         layout.marginTop = 10;
114         body.setLayout(layout);
115         body.setLayoutData(new GridData(GridData.FILL_BOTH));
116         
117         SashForm sashForm = new SashForm(body, SWT.HORIZONTAL);
118         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
119         
120         fCommandList = new CommandList(this, sashForm);
121         if (listener != null)
122             fCommandList.addTreeSelectionListener(listener);
123         
124         fCommandDetails = new CommandDetails(this, sashForm);
125         
126         sashForm.setWeights(new int[] {4,5});
127         fToolkit.adapt(sashForm, true, true);
128         
129         if (fPC != null)
130             fCommandList.setSelection(fPC.getCommand());
131         
132         fPC = null;
133     }
134     
135     protected void setMessage(String JavaDoc message, int newType) {
136         fScrolledForm.getForm().setMessage(message, newType);
137     }
138     
139     public FormToolkit getToolkit() {
140         return fToolkit;
141     }
142     
143     public ICommandService getCommandService() {
144         return fCommandService;
145     }
146     
147     public TagManager getTagManager() {
148         return fTagManager;
149     }
150     
151     public void setFocus() {
152         fCommandList.setFocus();
153     }
154     
155     public void dispose() {
156         fCommandDetails.dispose();
157         if (fCommandImage!=null) {
158             fCommandImage.dispose();
159             fCommandImage=null;
160         }
161     }
162     
163     protected String JavaDoc getSelectedCommandName() {
164         return fCommandDetails.getCommandName();
165     }
166     protected String JavaDoc getSelectedSerializedString() {
167         return fCommandDetails.getSerializedString();
168     }
169     protected HashMap JavaDoc getSelectedCommandsParameters() {
170         return fCommandDetails.getParameters();
171     }
172     
173     protected Composite createComposite(Composite parent) {
174         return createComposite(parent, GridData.FILL_BOTH, 1, true, 0);
175     }
176     
177     protected Composite createComposite(Composite parent, int gdStyle, int numCol, boolean colEqual, int margin) {
178         Composite comp = fToolkit.createComposite(parent);
179         GridLayout layout = new GridLayout(numCol, colEqual);
180         layout.marginHeight = layout.marginWidth = margin;
181         comp.setLayout(layout);
182         comp.setLayoutData(new GridData(gdStyle));
183         return comp;
184     }
185
186     public void selectionChanged(SelectionChangedEvent event) {
187         // Clear the previous error message (if any)
188
// Field input value is lost on selection any way
189
setMessage(null, IMessageProvider.NONE);
190         
191         Object JavaDoc selectionObject = null;
192         // if preselection exists use that
193
if (fPC != null)
194             selectionObject = fPC;
195         else if (event.getSelection() instanceof IStructuredSelection)
196             selectionObject = (((IStructuredSelection)event.getSelection()).getFirstElement());
197         if (selectionObject != null &&
198                 selectionObject.equals(fCommandDetails.getCommand()))
199             return;
200         fCommandDetails.showDetailsFor(selectionObject);
201     }
202
203     public ParameterizedCommand getParameterizedCommand() {
204         return fCommandDetails.buildParameterizedCommand();
205     }
206     
207     protected void setPresetCommand(ParameterizedCommand pc) {
208         fPC = pc;
209     }
210     
211     protected ParameterizedCommand getPresetCommand() {
212         return fPC;
213     }
214
215     /**
216      * @return
217      */

218     public CommandList getCommandList() {
219         return fCommandList;
220     }
221 }
222
Popular Tags