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;
26 //import org.eclipse.jface.text.Assert;
27 import org.eclipse.core.runtime.Assert;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.IActionBars;
33 //import org.eclipse.ui.IViewPart;
34 import org.eclipse.ui.IWorkbenchSite;
35 import org.eclipse.ui.actions.ActionGroup;
36 import org.eclipse.ui.actions.AddBookmarkAction;
37 import org.eclipse.ui.ide.IDEActionFactory;
38 import org.eclipse.ui.part.Page;
39 import org.eclipse.ui.texteditor.IUpdate;
42 * Action group that adds the source and generate actions to a part's context
43 * menu and installs handlers for the corresponding global menu actions.
46 * This class may be instantiated; it is not intended to be subclassed.
51 public class GenerateActionGroup extends ActionGroup {
53 * Pop-up menu: id of the source sub menu (value
54 * <code>net.sourceforge.phpdt.ui.source.menu</code>).
58 public static final String MENU_ID = "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
60 private PHPEditor fEditor;
62 // private boolean fEditorIsOwner;
63 private IWorkbenchSite fSite;
65 private String fGroupName = IContextMenuConstants.GROUP_SOURCE;
67 private List fRegisteredSelectionListeners;
69 // private AddImportOnSelectionAction fAddImport;
70 // private OverrideMethodsAction fOverrideMethods;
71 // private AddGetterSetterAction fAddGetterSetter;
72 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
73 // private AddJavaDocStubAction fAddJavaDocStub;
74 private AddBookmarkAction fAddBookmark;
76 private AddTaskAction fAddTaskAction;
78 // private ExternalizeStringsAction fExternalizeStrings;
79 // private FindStringsToExternalizeAction fFindStringsToExternalize;
80 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
82 // private OrganizeImportsAction fOrganizeImports;
85 * Note: This constructor is for internal use only. Clients should not call
88 public GenerateActionGroup(PHPEditor editor, String groupName) {
89 fSite = editor.getSite();
91 fGroupName = groupName;
93 //ISelectionProvider provider = fSite.getSelectionProvider();
94 //ISelection selection = provider.getSelection();
96 // fAddImport= new AddImportOnSelectionAction(editor);
97 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
98 // fAddImport.update();
99 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
101 // fOrganizeImports= new OrganizeImportsAction(editor);
102 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
103 // fOrganizeImports.editorStateChanged();
104 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
106 // fOverrideMethods= new OverrideMethodsAction(editor);
107 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
108 // fOverrideMethods.editorStateChanged();
109 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
111 // fAddGetterSetter= new AddGetterSetterAction(editor);
112 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
113 // fAddGetterSetter.editorStateChanged();
114 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
116 // fAddUnimplementedConstructors= new
117 // AddUnimplementedConstructorsAction(editor);
118 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
119 // fAddUnimplementedConstructors.editorStateChanged();
120 // editor.setAction("AddUnimplementedConstructors",
121 // fAddUnimplementedConstructors); //$NON-NLS-1$
123 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
124 // fAddJavaDocStub.editorStateChanged();
126 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
127 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
128 // fSurroundWithTryCatch.update(selection);
129 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
130 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch);
133 // fExternalizeStrings= new ExternalizeStringsAction(editor);
134 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
135 // fExternalizeStrings.editorStateChanged();
136 // editor.setAction("ExternalizeStrings", fExternalizeStrings);
142 * Creates a new <code>GenerateActionGroup</code>. The group requires
143 * that the selection provided by the page's selection provider is of type
144 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
147 * the page that owns this action group
149 public GenerateActionGroup(Page page) {
150 this(page.getSite());
154 * Creates a new <code>GenerateActionGroup</code>. The group requires
155 * that the selection provided by the part's selection provider is of type
156 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
159 * the view part that owns this action group
161 // public GenerateActionGroup(IViewPart part) {
162 // this(part.getSite());
165 private GenerateActionGroup(IWorkbenchSite site) {
167 ISelectionProvider provider = fSite.getSelectionProvider();
168 ISelection selection = provider.getSelection();
170 // fOverrideMethods= new OverrideMethodsAction(site);
171 // fAddGetterSetter= new AddGetterSetterAction(site);
172 // fAddUnimplementedConstructors= new
173 // AddUnimplementedConstructorsAction(site);
174 // fAddJavaDocStub= new AddJavaDocStubAction(site);
175 fAddBookmark = new AddBookmarkAction(site.getShell());
176 fAddTaskAction = new AddTaskAction(site);
177 // fExternalizeStrings= new ExternalizeStringsAction(site);
178 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
179 // fOrganizeImports= new OrganizeImportsAction(site);
181 // fOverrideMethods.update(selection);
182 // fAddGetterSetter.update(selection);
183 // fAddUnimplementedConstructors.update(selection);
184 // fAddJavaDocStub.update(selection);
185 // fExternalizeStrings.update(selection);
186 // fFindStringsToExternalize.update(selection);
187 fAddTaskAction.update(selection);
188 // fOrganizeImports.update(selection);
189 if (selection instanceof IStructuredSelection) {
190 IStructuredSelection ss = (IStructuredSelection) selection;
191 fAddBookmark.selectionChanged(ss);
193 fAddBookmark.setEnabled(false);
196 // registerSelectionListener(provider, fOverrideMethods);
197 // registerSelectionListener(provider, fAddGetterSetter);
198 // registerSelectionListener(provider, fAddUnimplementedConstructors);
199 // registerSelectionListener(provider, fAddJavaDocStub);
200 registerSelectionListener(provider, fAddBookmark);
201 // registerSelectionListener(provider, fExternalizeStrings);
202 // registerSelectionListener(provider, fFindStringsToExternalize);
203 // registerSelectionListener(provider, fOrganizeImports);
204 registerSelectionListener(provider, fAddTaskAction);
207 private void registerSelectionListener(ISelectionProvider provider,
208 ISelectionChangedListener listener) {
209 if (fRegisteredSelectionListeners == null)
210 fRegisteredSelectionListeners = new ArrayList(12);
211 provider.addSelectionChangedListener(listener);
212 fRegisteredSelectionListeners.add(listener);
216 * The state of the editor owning this action group has changed. This method
217 * does nothing if the group's owner isn't an editor.
220 * Note: This method is for internal use only. Clients should not call this
223 public void editorStateChanged() {
224 Assert.isTrue(isEditorOwner());
226 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
230 * (non-Javadoc) Method declared in ActionGroup
232 public void fillActionBars(IActionBars actionBar) {
233 super.fillActionBars(actionBar);
234 setGlobalActionHandlers(actionBar);
238 * (non-Javadoc) Method declared in ActionGroup
240 // public void fillContextMenu(IMenuManager menu) {
241 // super.fillContextMenu(menu);
242 // if (fEditorIsOwner) {
243 // IMenuManager subMenu= createEditorSubMenu(menu);
244 // if (subMenu != null)
245 // menu.appendToGroup(fGroupName, subMenu);
247 // // appendToGroup(menu, fOrganizeImports);
248 // // appendToGroup(menu, fOverrideMethods);
249 // // appendToGroup(menu, fAddGetterSetter);
250 // // appendToGroup(menu, fAddUnimplementedConstructors);
251 // // appendToGroup(menu, fAddJavaDocStub);
252 // appendToGroup(menu, fAddBookmark);
256 * (non-Javadoc) Method declared in ActionGroup
258 // public void fillContextMenu(IMenuManager menu) {
259 // super.fillContextMenu(menu);
260 // IMenuManager subMenu= null;
261 // if (isEditorOwner()) {
262 // subMenu= fillEditorSubMenu(menu);
264 // // subMenu= createViewSubMenu(menu);
266 // if (subMenu != null)
267 // menu.appendToGroup(fGroupName, subMenu);
269 public void fillContextMenu(IMenuManager menu) {
270 super.fillContextMenu(menu);
271 String shortCut = null; //$NON-NLS-1$
272 // if (fQuickAccessAction != null) {
273 // shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
275 IMenuManager subMenu = new MenuManager(
276 ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
279 if (isEditorOwner()) {
280 added = fillEditorSubMenu(subMenu);
283 // added= fillViewSubMenu(subMenu);
286 menu.appendToGroup(fGroupName, subMenu);
289 private int fillEditorSubMenu(IMenuManager source) {
290 // IMenuManager result= new
291 // MenuManager(ActionMessages.getString("SourceMenu.label"));
294 added += addEditorAction(source, "Comment"); //$NON-NLS-1$
295 added += addEditorAction(source, "Uncomment"); //$NON-NLS-1$
296 added += addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
297 added += addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
298 added += addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
299 added += addEditorAction(source, "Format"); //$NON-NLS-1$
300 added += addEditorAction(source, "Indent"); //$NON-NLS-1$
301 // result.add(new Separator());
302 // added+= addAction(result, fOrganizeImports);
303 // added+= addAction(result, fAddImport);
304 // result.add(new Separator());
305 // added+= addAction(result, fOverrideMethods);
306 // added+= addAction(result, fAddGetterSetter);
307 // added+= addAction(result, fAddUnimplementedConstructors);
308 // added+= addAction(result, fAddJavaDocStub);
309 // added+= addAction(result, fAddBookmark);
310 // result.add(new Separator());
311 // added+= addAction(result, fSurroundWithTryCatch);
312 // added+= addAction(result, fExternalizeStrings);
319 * (non-Javadoc) Method declared in ActionGroup
321 public void dispose() {
322 if (fRegisteredSelectionListeners != null) {
323 ISelectionProvider provider = fSite.getSelectionProvider();
324 for (Iterator iter = fRegisteredSelectionListeners.iterator(); iter
326 ISelectionChangedListener listener = (ISelectionChangedListener) iter
328 provider.removeSelectionChangedListener(listener);
335 private void setGlobalActionHandlers(IActionBars actionBar) {
336 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT,
338 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH,
339 // fSurroundWithTryCatch);
340 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS,
341 // fOverrideMethods);
342 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER,
343 // fAddGetterSetter);
344 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS,
345 // fAddUnimplementedConstructors);
346 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT,
348 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS,
349 // fExternalizeStrings);
350 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE,
351 // fFindStringsToExternalize);
352 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS,
353 // fOrganizeImports);
354 if (!isEditorOwner()) {
355 // editor provides its own implementation of these actions.
356 actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(),
358 actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(),
363 // private int appendToGroup(IMenuManager menu, IAction action) {
364 // if (action != null && action.isEnabled()) {
365 // menu.appendToGroup(fGroupName, action);
371 // private int addAction(IMenuManager menu, IAction action) {
372 // if (action != null && action.isEnabled()) {
379 private int addEditorAction(IMenuManager menu, String actionID) {
382 IAction action = fEditor.getAction(actionID);
385 if (action instanceof IUpdate)
386 ((IUpdate) action).update();
387 if (action.isEnabled()) {
394 private boolean isEditorOwner() {
395 return fEditor != null;