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
52 * <code>net.sourceforge.phpdt.ui.source.menu</code>).
56 public static final String MENU_ID = "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
58 private PHPEditor fEditor;
60 // private boolean fEditorIsOwner;
61 private IWorkbenchSite fSite;
63 private String fGroupName = IContextMenuConstants.GROUP_SOURCE;
65 private List fRegisteredSelectionListeners;
67 // private AddImportOnSelectionAction fAddImport;
68 // private OverrideMethodsAction fOverrideMethods;
69 // private AddGetterSetterAction fAddGetterSetter;
70 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
71 // private AddJavaDocStubAction fAddJavaDocStub;
72 private AddBookmarkAction fAddBookmark;
74 private AddTaskAction fAddTaskAction;
76 // private ExternalizeStringsAction fExternalizeStrings;
77 // private FindStringsToExternalizeAction fFindStringsToExternalize;
78 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
80 // private OrganizeImportsAction fOrganizeImports;
83 * Note: This constructor is for internal use only. Clients should not call
86 public GenerateActionGroup(PHPEditor editor, String groupName) {
87 fSite = editor.getSite();
89 fGroupName = groupName;
91 ISelectionProvider provider = fSite.getSelectionProvider();
92 ISelection selection = provider.getSelection();
94 // fAddImport= new AddImportOnSelectionAction(editor);
95 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
96 // fAddImport.update();
97 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
99 // fOrganizeImports= new OrganizeImportsAction(editor);
100 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
101 // fOrganizeImports.editorStateChanged();
102 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
104 // fOverrideMethods= new OverrideMethodsAction(editor);
105 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
106 // fOverrideMethods.editorStateChanged();
107 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
109 // fAddGetterSetter= new AddGetterSetterAction(editor);
110 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
111 // fAddGetterSetter.editorStateChanged();
112 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
114 // fAddUnimplementedConstructors= new
115 // AddUnimplementedConstructorsAction(editor);
116 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
117 // fAddUnimplementedConstructors.editorStateChanged();
118 // editor.setAction("AddUnimplementedConstructors",
119 // fAddUnimplementedConstructors); //$NON-NLS-1$
121 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
122 // fAddJavaDocStub.editorStateChanged();
124 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
125 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
126 // fSurroundWithTryCatch.update(selection);
127 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
128 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch);
131 // fExternalizeStrings= new ExternalizeStringsAction(editor);
132 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
133 // fExternalizeStrings.editorStateChanged();
134 // editor.setAction("ExternalizeStrings", fExternalizeStrings);
140 * Creates a new <code>GenerateActionGroup</code>. The group requires
141 * that the selection provided by the page's selection provider is of type
142 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
145 * the page that owns this action group
147 public GenerateActionGroup(Page page) {
148 this(page.getSite());
152 * Creates a new <code>GenerateActionGroup</code>. The group requires
153 * that the selection provided by the part's selection provider is of type
154 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
157 * the view part that owns this action group
159 public GenerateActionGroup(IViewPart part) {
160 this(part.getSite());
163 private GenerateActionGroup(IWorkbenchSite site) {
165 ISelectionProvider provider = fSite.getSelectionProvider();
166 ISelection selection = provider.getSelection();
168 // fOverrideMethods= new OverrideMethodsAction(site);
169 // fAddGetterSetter= new AddGetterSetterAction(site);
170 // fAddUnimplementedConstructors= new
171 // AddUnimplementedConstructorsAction(site);
172 // fAddJavaDocStub= new AddJavaDocStubAction(site);
173 fAddBookmark = new AddBookmarkAction(site.getShell());
174 fAddTaskAction = new AddTaskAction(site);
175 // fExternalizeStrings= new ExternalizeStringsAction(site);
176 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
177 // fOrganizeImports= new OrganizeImportsAction(site);
179 // fOverrideMethods.update(selection);
180 // fAddGetterSetter.update(selection);
181 // fAddUnimplementedConstructors.update(selection);
182 // fAddJavaDocStub.update(selection);
183 // fExternalizeStrings.update(selection);
184 // fFindStringsToExternalize.update(selection);
185 fAddTaskAction.update(selection);
186 // fOrganizeImports.update(selection);
187 if (selection instanceof IStructuredSelection) {
188 IStructuredSelection ss = (IStructuredSelection) selection;
189 fAddBookmark.selectionChanged(ss);
191 fAddBookmark.setEnabled(false);
194 // registerSelectionListener(provider, fOverrideMethods);
195 // registerSelectionListener(provider, fAddGetterSetter);
196 // registerSelectionListener(provider, fAddUnimplementedConstructors);
197 // registerSelectionListener(provider, fAddJavaDocStub);
198 registerSelectionListener(provider, fAddBookmark);
199 // registerSelectionListener(provider, fExternalizeStrings);
200 // registerSelectionListener(provider, fFindStringsToExternalize);
201 // registerSelectionListener(provider, fOrganizeImports);
202 registerSelectionListener(provider, fAddTaskAction);
205 private void registerSelectionListener(ISelectionProvider provider,
206 ISelectionChangedListener listener) {
207 if (fRegisteredSelectionListeners == null)
208 fRegisteredSelectionListeners = new ArrayList(12);
209 provider.addSelectionChangedListener(listener);
210 fRegisteredSelectionListeners.add(listener);
214 * The state of the editor owning this action group has changed. This method
215 * does nothing if the group's owner isn't an editor.
218 * Note: This method is for internal use only. Clients should not call this
221 public void editorStateChanged() {
222 Assert.isTrue(isEditorOwner());
224 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
228 * (non-Javadoc) Method declared in ActionGroup
230 public void fillActionBars(IActionBars actionBar) {
231 super.fillActionBars(actionBar);
232 setGlobalActionHandlers(actionBar);
236 * (non-Javadoc) Method declared in ActionGroup
238 // public void fillContextMenu(IMenuManager menu) {
239 // super.fillContextMenu(menu);
240 // if (fEditorIsOwner) {
241 // IMenuManager subMenu= createEditorSubMenu(menu);
242 // if (subMenu != null)
243 // menu.appendToGroup(fGroupName, subMenu);
245 // // appendToGroup(menu, fOrganizeImports);
246 // // appendToGroup(menu, fOverrideMethods);
247 // // appendToGroup(menu, fAddGetterSetter);
248 // // appendToGroup(menu, fAddUnimplementedConstructors);
249 // // appendToGroup(menu, fAddJavaDocStub);
250 // appendToGroup(menu, fAddBookmark);
254 * (non-Javadoc) Method declared in ActionGroup
256 // public void fillContextMenu(IMenuManager menu) {
257 // super.fillContextMenu(menu);
258 // IMenuManager subMenu= null;
259 // if (isEditorOwner()) {
260 // subMenu= fillEditorSubMenu(menu);
262 // // subMenu= createViewSubMenu(menu);
264 // if (subMenu != null)
265 // menu.appendToGroup(fGroupName, subMenu);
267 public void fillContextMenu(IMenuManager menu) {
268 super.fillContextMenu(menu);
269 String shortCut = null; //$NON-NLS-1$
270 // if (fQuickAccessAction != null) {
271 // shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
273 IMenuManager subMenu = new MenuManager(
274 ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
277 if (isEditorOwner()) {
278 added = fillEditorSubMenu(subMenu);
281 // added= fillViewSubMenu(subMenu);
284 menu.appendToGroup(fGroupName, subMenu);
287 private int fillEditorSubMenu(IMenuManager source) {
288 // IMenuManager result= new
289 // MenuManager(ActionMessages.getString("SourceMenu.label"));
292 added += addEditorAction(source, "Comment"); //$NON-NLS-1$
293 added += addEditorAction(source, "Uncomment"); //$NON-NLS-1$
294 added += addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
295 added += addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
296 added += addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
297 added += addEditorAction(source, "Format"); //$NON-NLS-1$
298 added += addEditorAction(source, "Indent"); //$NON-NLS-1$
299 // result.add(new Separator());
300 // added+= addAction(result, fOrganizeImports);
301 // added+= addAction(result, fAddImport);
302 // result.add(new Separator());
303 // added+= addAction(result, fOverrideMethods);
304 // added+= addAction(result, fAddGetterSetter);
305 // added+= addAction(result, fAddUnimplementedConstructors);
306 // added+= addAction(result, fAddJavaDocStub);
307 // added+= addAction(result, fAddBookmark);
308 // result.add(new Separator());
309 // added+= addAction(result, fSurroundWithTryCatch);
310 // added+= addAction(result, fExternalizeStrings);
317 * (non-Javadoc) Method declared in ActionGroup
319 public void dispose() {
320 if (fRegisteredSelectionListeners != null) {
321 ISelectionProvider provider = fSite.getSelectionProvider();
322 for (Iterator iter = fRegisteredSelectionListeners.iterator(); iter
324 ISelectionChangedListener listener = (ISelectionChangedListener) iter
326 provider.removeSelectionChangedListener(listener);
333 private void setGlobalActionHandlers(IActionBars actionBar) {
334 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT,
336 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH,
337 // fSurroundWithTryCatch);
338 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS,
339 // fOverrideMethods);
340 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER,
341 // fAddGetterSetter);
342 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS,
343 // fAddUnimplementedConstructors);
344 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT,
346 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS,
347 // fExternalizeStrings);
348 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE,
349 // fFindStringsToExternalize);
350 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS,
351 // fOrganizeImports);
352 if (!isEditorOwner()) {
353 // editor provides its own implementation of these actions.
354 actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(),
356 actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(),
361 private int appendToGroup(IMenuManager menu, IAction action) {
362 if (action != null && action.isEnabled()) {
363 menu.appendToGroup(fGroupName, action);
369 private int addAction(IMenuManager menu, IAction action) {
370 if (action != null && action.isEnabled()) {
377 private int addEditorAction(IMenuManager menu, String actionID) {
380 IAction action = fEditor.getAction(actionID);
383 if (action instanceof IUpdate)
384 ((IUpdate) action).update();
385 if (action.isEnabled()) {
392 private boolean isEditorOwner() {
393 return fEditor != null;