1 /*******************************************************************************
2 * Copyright (c) 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpdt.internal.ui.actions.ActionMessages;
18 import net.sourceforge.phpdt.internal.ui.actions.AddTaskAction;
19 import net.sourceforge.phpdt.ui.IContextMenuConstants;
20 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.MenuManager;
25 import org.eclipse.jface.util.Assert;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.ui.IActionBars;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbenchSite;
33 import org.eclipse.ui.actions.ActionGroup;
34 import org.eclipse.ui.actions.AddBookmarkAction;
35 import org.eclipse.ui.ide.IDEActionFactory;
36 import org.eclipse.ui.part.Page;
37 import org.eclipse.ui.texteditor.IUpdate;
40 * Action group that adds the source and generate actions to a part's context
41 * menu and installs handlers for the corresponding global menu actions.
44 * This class may be instantiated; it is not intended to be subclassed.
49 public class GenerateActionGroup extends ActionGroup {
51 * Pop-up menu: id of the source sub menu (value <code>net.sourceforge.phpdt.ui.source.menu</code>).
55 public static final String MENU_ID= "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
57 private PHPEditor fEditor;
58 // private boolean fEditorIsOwner;
59 private IWorkbenchSite fSite;
60 private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
61 private List fRegisteredSelectionListeners;
63 // private AddImportOnSelectionAction fAddImport;
64 // private OverrideMethodsAction fOverrideMethods;
65 // private AddGetterSetterAction fAddGetterSetter;
66 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
67 // private AddJavaDocStubAction fAddJavaDocStub;
68 private AddBookmarkAction fAddBookmark;
69 private AddTaskAction fAddTaskAction;
70 // private ExternalizeStringsAction fExternalizeStrings;
71 // private FindStringsToExternalizeAction fFindStringsToExternalize;
72 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
74 // private OrganizeImportsAction fOrganizeImports;
77 * Note: This constructor is for internal use only. Clients should not call this constructor.
79 public GenerateActionGroup(PHPEditor editor, String groupName) {
80 fSite= editor.getSite();
82 fGroupName= groupName;
84 ISelectionProvider provider= fSite.getSelectionProvider();
85 ISelection selection= provider.getSelection();
87 // fAddImport= new AddImportOnSelectionAction(editor);
88 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
89 // fAddImport.update();
90 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
92 // fOrganizeImports= new OrganizeImportsAction(editor);
93 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
94 // fOrganizeImports.editorStateChanged();
95 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
97 // fOverrideMethods= new OverrideMethodsAction(editor);
98 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
99 // fOverrideMethods.editorStateChanged();
100 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
102 // fAddGetterSetter= new AddGetterSetterAction(editor);
103 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
104 // fAddGetterSetter.editorStateChanged();
105 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
107 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
108 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
109 // fAddUnimplementedConstructors.editorStateChanged();
110 // editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
112 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
113 // fAddJavaDocStub.editorStateChanged();
115 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
116 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
117 // fSurroundWithTryCatch.update(selection);
118 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
119 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
121 // fExternalizeStrings= new ExternalizeStringsAction(editor);
122 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
123 // fExternalizeStrings.editorStateChanged();
124 // editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
129 * Creates a new <code>GenerateActionGroup</code>. The group
130 * requires that the selection provided by the page's selection provider
131 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
133 * @param page the page that owns this action group
135 public GenerateActionGroup(Page page) {
136 this(page.getSite());
140 * Creates a new <code>GenerateActionGroup</code>. The group
141 * requires that the selection provided by the part's selection provider
142 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
144 * @param part the view part that owns this action group
146 public GenerateActionGroup(IViewPart part) {
147 this(part.getSite());
150 private GenerateActionGroup(IWorkbenchSite site) {
152 ISelectionProvider provider= fSite.getSelectionProvider();
153 ISelection selection= provider.getSelection();
155 // fOverrideMethods= new OverrideMethodsAction(site);
156 // fAddGetterSetter= new AddGetterSetterAction(site);
157 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
158 // fAddJavaDocStub= new AddJavaDocStubAction(site);
159 fAddBookmark= new AddBookmarkAction(site.getShell());
160 fAddTaskAction= new AddTaskAction(site);
161 // fExternalizeStrings= new ExternalizeStringsAction(site);
162 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
163 // fOrganizeImports= new OrganizeImportsAction(site);
165 // fOverrideMethods.update(selection);
166 // fAddGetterSetter.update(selection);
167 // fAddUnimplementedConstructors.update(selection);
168 // fAddJavaDocStub.update(selection);
169 // fExternalizeStrings.update(selection);
170 // fFindStringsToExternalize.update(selection);
171 fAddTaskAction.update(selection);
172 // fOrganizeImports.update(selection);
173 if (selection instanceof IStructuredSelection) {
174 IStructuredSelection ss= (IStructuredSelection)selection;
175 fAddBookmark.selectionChanged(ss);
177 fAddBookmark.setEnabled(false);
180 // registerSelectionListener(provider, fOverrideMethods);
181 // registerSelectionListener(provider, fAddGetterSetter);
182 // registerSelectionListener(provider, fAddUnimplementedConstructors);
183 // registerSelectionListener(provider, fAddJavaDocStub);
184 registerSelectionListener(provider, fAddBookmark);
185 // registerSelectionListener(provider, fExternalizeStrings);
186 // registerSelectionListener(provider, fFindStringsToExternalize);
187 // registerSelectionListener(provider, fOrganizeImports);
188 registerSelectionListener(provider, fAddTaskAction);
191 private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
192 if (fRegisteredSelectionListeners == null)
193 fRegisteredSelectionListeners= new ArrayList(12);
194 provider.addSelectionChangedListener(listener);
195 fRegisteredSelectionListeners.add(listener);
199 * The state of the editor owning this action group has changed.
200 * This method does nothing if the group's owner isn't an
204 * Note: This method is for internal use only. Clients should not call this method.
206 public void editorStateChanged() {
207 Assert.isTrue(isEditorOwner());
209 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
213 * Method declared in ActionGroup
215 public void fillActionBars(IActionBars actionBar) {
216 super.fillActionBars(actionBar);
217 setGlobalActionHandlers(actionBar);
221 * Method declared in ActionGroup
223 // public void fillContextMenu(IMenuManager menu) {
224 // super.fillContextMenu(menu);
225 // if (fEditorIsOwner) {
226 // IMenuManager subMenu= createEditorSubMenu(menu);
227 // if (subMenu != null)
228 // menu.appendToGroup(fGroupName, subMenu);
230 //// appendToGroup(menu, fOrganizeImports);
231 //// appendToGroup(menu, fOverrideMethods);
232 //// appendToGroup(menu, fAddGetterSetter);
233 //// appendToGroup(menu, fAddUnimplementedConstructors);
234 //// appendToGroup(menu, fAddJavaDocStub);
235 // appendToGroup(menu, fAddBookmark);
239 * Method declared in ActionGroup
241 // public void fillContextMenu(IMenuManager menu) {
242 // super.fillContextMenu(menu);
243 // IMenuManager subMenu= null;
244 // if (isEditorOwner()) {
245 // subMenu= fillEditorSubMenu(menu);
247 // // subMenu= createViewSubMenu(menu);
249 // if (subMenu != null)
250 // menu.appendToGroup(fGroupName, subMenu);
252 public void fillContextMenu(IMenuManager menu) {
253 super.fillContextMenu(menu);
254 String shortCut= null; //$NON-NLS-1$
255 // if (fQuickAccessAction != null) {
256 // shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
258 IMenuManager subMenu= new MenuManager(
259 ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
262 if (isEditorOwner()) {
263 added= fillEditorSubMenu(subMenu);
266 // added= fillViewSubMenu(subMenu);
269 menu.appendToGroup(fGroupName, subMenu);
271 private int fillEditorSubMenu(IMenuManager source) {
272 // IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
274 added+= addEditorAction(source, "Comment"); //$NON-NLS-1$
275 added+= addEditorAction(source, "Uncomment"); //$NON-NLS-1$
276 added+= addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
277 added+= addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
278 added+= addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
279 added+= addEditorAction(source, "Format"); //$NON-NLS-1$
280 added+= addEditorAction(source, "Indent"); //$NON-NLS-1$
281 // result.add(new Separator());
282 // added+= addAction(result, fOrganizeImports);
283 // added+= addAction(result, fAddImport);
284 // result.add(new Separator());
285 // added+= addAction(result, fOverrideMethods);
286 // added+= addAction(result, fAddGetterSetter);
287 // added+= addAction(result, fAddUnimplementedConstructors);
288 // added+= addAction(result, fAddJavaDocStub);
289 // added+= addAction(result, fAddBookmark);
290 // result.add(new Separator());
291 // added+= addAction(result, fSurroundWithTryCatch);
292 // added+= addAction(result, fExternalizeStrings);
298 * Method declared in ActionGroup
300 public void dispose() {
301 if (fRegisteredSelectionListeners != null) {
302 ISelectionProvider provider= fSite.getSelectionProvider();
303 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
304 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
305 provider.removeSelectionChangedListener(listener);
312 private void setGlobalActionHandlers(IActionBars actionBar) {
313 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
314 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
315 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
316 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
317 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
318 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
319 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
320 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
321 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
322 if (!isEditorOwner()) {
323 // editor provides its own implementation of these actions.
324 actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), fAddBookmark);
325 actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), fAddTaskAction);
329 private int appendToGroup(IMenuManager menu, IAction action) {
330 if (action != null && action.isEnabled()) {
331 menu.appendToGroup(fGroupName, action);
337 private int addAction(IMenuManager menu, IAction action) {
338 if (action != null && action.isEnabled()) {
345 private int addEditorAction(IMenuManager menu, String actionID) {
348 IAction action= fEditor.getAction(actionID);
351 if (action instanceof IUpdate)
352 ((IUpdate)action).update();
353 if (action.isEnabled()) {
360 private boolean isEditorOwner() {
361 return fEditor != null;