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.IWorkbenchActionConstants;
33 import org.eclipse.ui.IWorkbenchSite;
34 import org.eclipse.ui.actions.ActionGroup;
35 import org.eclipse.ui.actions.AddBookmarkAction;
36 import org.eclipse.ui.part.Page;
37 import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
38 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
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 <code>net.sourceforge.phpdt.ui.source.menu</code>).
57 public static final String MENU_ID= "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
59 private PHPEditor fEditor;
60 // private boolean fEditorIsOwner;
61 private IWorkbenchSite fSite;
62 private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
63 private List fRegisteredSelectionListeners;
65 // private AddImportOnSelectionAction fAddImport;
66 // private OverrideMethodsAction fOverrideMethods;
67 // private AddGetterSetterAction fAddGetterSetter;
68 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
69 // private AddJavaDocStubAction fAddJavaDocStub;
70 private AddBookmarkAction fAddBookmark;
71 private AddTaskAction fAddTaskAction;
72 // private ExternalizeStringsAction fExternalizeStrings;
73 // private FindStringsToExternalizeAction fFindStringsToExternalize;
74 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
76 // private OrganizeImportsAction fOrganizeImports;
78 private ConvertLineDelimitersAction fConvertToWindows;
79 private ConvertLineDelimitersAction fConvertToUNIX;
80 private ConvertLineDelimitersAction fConvertToMac;
83 * Note: This constructor is for internal use only. Clients should not call this constructor.
85 public GenerateActionGroup(PHPEditor editor, String groupName) {
86 fSite= editor.getSite();
88 fGroupName= groupName;
90 ISelectionProvider provider= fSite.getSelectionProvider();
91 ISelection selection= provider.getSelection();
93 // fAddImport= new AddImportOnSelectionAction(editor);
94 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
95 // fAddImport.update();
96 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
98 // fOrganizeImports= new OrganizeImportsAction(editor);
99 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
100 // fOrganizeImports.editorStateChanged();
101 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
103 // fOverrideMethods= new OverrideMethodsAction(editor);
104 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
105 // fOverrideMethods.editorStateChanged();
106 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
108 // fAddGetterSetter= new AddGetterSetterAction(editor);
109 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
110 // fAddGetterSetter.editorStateChanged();
111 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
113 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
114 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
115 // fAddUnimplementedConstructors.editorStateChanged();
116 // editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
118 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
119 // fAddJavaDocStub.editorStateChanged();
121 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
122 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
123 // fSurroundWithTryCatch.update(selection);
124 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
125 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
127 // fExternalizeStrings= new ExternalizeStringsAction(editor);
128 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
129 // fExternalizeStrings.editorStateChanged();
130 // editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
132 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
133 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
134 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$
136 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
137 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
138 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$
140 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
141 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
142 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$
146 * Creates a new <code>GenerateActionGroup</code>. The group
147 * requires that the selection provided by the page's selection provider
148 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
150 * @param page the page that owns this action group
152 public GenerateActionGroup(Page page) {
153 this(page.getSite());
157 * Creates a new <code>GenerateActionGroup</code>. The group
158 * requires that the selection provided by the part's selection provider
159 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
161 * @param part the view part that owns this action group
163 public GenerateActionGroup(IViewPart part) {
164 this(part.getSite());
167 private GenerateActionGroup(IWorkbenchSite site) {
169 ISelectionProvider provider= fSite.getSelectionProvider();
170 ISelection selection= provider.getSelection();
172 // fOverrideMethods= new OverrideMethodsAction(site);
173 // fAddGetterSetter= new AddGetterSetterAction(site);
174 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
175 // fAddJavaDocStub= new AddJavaDocStubAction(site);
176 fAddBookmark= new AddBookmarkAction(site.getShell());
177 fAddTaskAction= new AddTaskAction(site);
178 // fExternalizeStrings= new ExternalizeStringsAction(site);
179 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
180 // fOrganizeImports= new OrganizeImportsAction(site);
182 // fOverrideMethods.update(selection);
183 // fAddGetterSetter.update(selection);
184 // fAddUnimplementedConstructors.update(selection);
185 // fAddJavaDocStub.update(selection);
186 // fExternalizeStrings.update(selection);
187 // fFindStringsToExternalize.update(selection);
188 fAddTaskAction.update(selection);
189 // fOrganizeImports.update(selection);
190 if (selection instanceof IStructuredSelection) {
191 IStructuredSelection ss= (IStructuredSelection)selection;
192 fAddBookmark.selectionChanged(ss);
194 fAddBookmark.setEnabled(false);
197 // registerSelectionListener(provider, fOverrideMethods);
198 // registerSelectionListener(provider, fAddGetterSetter);
199 // registerSelectionListener(provider, fAddUnimplementedConstructors);
200 // registerSelectionListener(provider, fAddJavaDocStub);
201 registerSelectionListener(provider, fAddBookmark);
202 // registerSelectionListener(provider, fExternalizeStrings);
203 // registerSelectionListener(provider, fFindStringsToExternalize);
204 // registerSelectionListener(provider, fOrganizeImports);
205 registerSelectionListener(provider, fAddTaskAction);
208 private void registerSelectionListener(ISelectionProvider provider, 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.
217 * This method does nothing if the group's owner isn't an
221 * Note: This method is for internal use only. Clients should not call this method.
223 public void editorStateChanged() {
224 Assert.isTrue(isEditorOwner());
226 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
227 fConvertToMac.update();
228 fConvertToUNIX.update();
229 fConvertToWindows.update();
233 * Method declared in ActionGroup
235 public void fillActionBars(IActionBars actionBar) {
236 super.fillActionBars(actionBar);
237 setGlobalActionHandlers(actionBar);
241 * Method declared in ActionGroup
243 // public void fillContextMenu(IMenuManager menu) {
244 // super.fillContextMenu(menu);
245 // if (fEditorIsOwner) {
246 // IMenuManager subMenu= createEditorSubMenu(menu);
247 // if (subMenu != null)
248 // menu.appendToGroup(fGroupName, subMenu);
250 //// appendToGroup(menu, fOrganizeImports);
251 //// appendToGroup(menu, fOverrideMethods);
252 //// appendToGroup(menu, fAddGetterSetter);
253 //// appendToGroup(menu, fAddUnimplementedConstructors);
254 //// appendToGroup(menu, fAddJavaDocStub);
255 // appendToGroup(menu, fAddBookmark);
259 * Method declared in ActionGroup
261 // public void fillContextMenu(IMenuManager menu) {
262 // super.fillContextMenu(menu);
263 // IMenuManager subMenu= null;
264 // if (isEditorOwner()) {
265 // subMenu= fillEditorSubMenu(menu);
267 // // subMenu= createViewSubMenu(menu);
269 // if (subMenu != null)
270 // menu.appendToGroup(fGroupName, subMenu);
272 public void fillContextMenu(IMenuManager menu) {
273 super.fillContextMenu(menu);
274 String shortCut= null; //$NON-NLS-1$
275 // if (fQuickAccessAction != null) {
276 // shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
278 IMenuManager subMenu= new MenuManager(
279 ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
282 if (isEditorOwner()) {
283 added= fillEditorSubMenu(subMenu);
286 // added= fillViewSubMenu(subMenu);
289 menu.appendToGroup(fGroupName, subMenu);
291 private int fillEditorSubMenu(IMenuManager source) {
292 // IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
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);
318 * Method declared in ActionGroup
320 public void dispose() {
321 if (fRegisteredSelectionListeners != null) {
322 ISelectionProvider provider= fSite.getSelectionProvider();
323 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
324 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
325 provider.removeSelectionChangedListener(listener);
332 private void setGlobalActionHandlers(IActionBars actionBar) {
333 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
334 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
335 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
336 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
337 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
338 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
339 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
340 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
341 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
342 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
343 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
344 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
345 if (!isEditorOwner()) {
346 // editor provides its own implementation of these actions.
347 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, fAddBookmark);
348 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.ADD_TASK, fAddTaskAction);
352 private int appendToGroup(IMenuManager menu, IAction action) {
353 if (action != null && action.isEnabled()) {
354 menu.appendToGroup(fGroupName, action);
360 private int addAction(IMenuManager menu, IAction action) {
361 if (action != null && action.isEnabled()) {
368 private int addEditorAction(IMenuManager menu, String actionID) {
371 IAction action= fEditor.getAction(actionID);
374 if (action instanceof IUpdate)
375 ((IUpdate)action).update();
376 if (action.isEnabled()) {
383 private boolean isEditorOwner() {
384 return fEditor != null;