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.AddTaskAction;
18 import net.sourceforge.phpdt.ui.IContextMenuConstants;
19 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
21 import org.eclipse.debug.internal.ui.actions.ActionMessages;
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.action.Separator;
26 import org.eclipse.jface.util.Assert;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.IViewPart;
33 import org.eclipse.ui.IWorkbenchActionConstants;
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.part.Page;
38 import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
39 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
40 import org.eclipse.ui.texteditor.IUpdate;
43 * Action group that adds the source and generate actions to a part's context
44 * menu and installs handlers for the corresponding global menu actions.
47 * This class may be instantiated; it is not intended to be subclassed.
52 public class GenerateActionGroup extends ActionGroup {
54 private PHPEditor fEditor;
55 private boolean fEditorIsOwner;
56 private IWorkbenchSite fSite;
57 private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
58 private List fRegisteredSelectionListeners;
60 // private AddImportOnSelectionAction fAddImport;
61 // private OverrideMethodsAction fOverrideMethods;
62 // private AddGetterSetterAction fAddGetterSetter;
63 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
64 // private AddJavaDocStubAction fAddJavaDocStub;
65 private AddBookmarkAction fAddBookmark;
66 private AddTaskAction fAddTaskAction;
67 // private ExternalizeStringsAction fExternalizeStrings;
68 // private FindStringsToExternalizeAction fFindStringsToExternalize;
69 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
71 // private OrganizeImportsAction fOrganizeImports;
73 private ConvertLineDelimitersAction fConvertToWindows;
74 private ConvertLineDelimitersAction fConvertToUNIX;
75 private ConvertLineDelimitersAction fConvertToMac;
78 * Note: This constructor is for internal use only. Clients should not call this constructor.
80 public GenerateActionGroup(PHPEditor editor, String groupName) {
81 fSite= editor.getSite();
84 fGroupName= groupName;
85 ISelectionProvider provider= fSite.getSelectionProvider();
86 ISelection selection= provider.getSelection();
88 // fAddImport= new AddImportOnSelectionAction(editor);
89 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
90 // fAddImport.update();
91 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
93 // fOrganizeImports= new OrganizeImportsAction(editor);
94 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
95 // fOrganizeImports.editorStateChanged();
96 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
98 // fOverrideMethods= new OverrideMethodsAction(editor);
99 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
100 // fOverrideMethods.editorStateChanged();
101 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
103 // fAddGetterSetter= new AddGetterSetterAction(editor);
104 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
105 // fAddGetterSetter.editorStateChanged();
106 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
108 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
109 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
110 // fAddUnimplementedConstructors.editorStateChanged();
111 // editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
113 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
114 // fAddJavaDocStub.editorStateChanged();
116 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
117 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
118 // fSurroundWithTryCatch.update(selection);
119 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
120 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
122 // fExternalizeStrings= new ExternalizeStringsAction(editor);
123 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
124 // fExternalizeStrings.editorStateChanged();
125 // editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
127 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
128 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
129 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$
131 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
132 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
133 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$
135 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
136 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
137 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$
141 * Creates a new <code>GenerateActionGroup</code>. The group
142 * requires that the selection provided by the page's selection provider
143 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
145 * @param page 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
153 * requires that the selection provided by the part's selection provider
154 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
156 * @param part the view part that owns this action group
158 public GenerateActionGroup(IViewPart part) {
159 this(part.getSite());
162 private GenerateActionGroup(IWorkbenchSite site) {
164 ISelectionProvider provider= fSite.getSelectionProvider();
165 ISelection selection= provider.getSelection();
167 // fOverrideMethods= new OverrideMethodsAction(site);
168 // fAddGetterSetter= new AddGetterSetterAction(site);
169 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
170 // fAddJavaDocStub= new AddJavaDocStubAction(site);
171 fAddBookmark= new AddBookmarkAction(site.getShell());
172 fAddTaskAction= new AddTaskAction(site);
173 // fExternalizeStrings= new ExternalizeStringsAction(site);
174 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
175 // fOrganizeImports= new OrganizeImportsAction(site);
177 // fOverrideMethods.update(selection);
178 // fAddGetterSetter.update(selection);
179 // fAddUnimplementedConstructors.update(selection);
180 // fAddJavaDocStub.update(selection);
181 // fExternalizeStrings.update(selection);
182 // fFindStringsToExternalize.update(selection);
183 fAddTaskAction.update(selection);
184 // fOrganizeImports.update(selection);
185 if (selection instanceof IStructuredSelection) {
186 IStructuredSelection ss= (IStructuredSelection)selection;
187 fAddBookmark.selectionChanged(ss);
189 fAddBookmark.setEnabled(false);
192 // registerSelectionListener(provider, fOverrideMethods);
193 // registerSelectionListener(provider, fAddGetterSetter);
194 // registerSelectionListener(provider, fAddUnimplementedConstructors);
195 // registerSelectionListener(provider, fAddJavaDocStub);
196 registerSelectionListener(provider, fAddBookmark);
197 // registerSelectionListener(provider, fExternalizeStrings);
198 // registerSelectionListener(provider, fFindStringsToExternalize);
199 // registerSelectionListener(provider, fOrganizeImports);
200 registerSelectionListener(provider, fAddTaskAction);
203 private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
204 if (fRegisteredSelectionListeners == null)
205 fRegisteredSelectionListeners= new ArrayList(12);
206 provider.addSelectionChangedListener(listener);
207 fRegisteredSelectionListeners.add(listener);
211 * The state of the editor owning this action group has changed.
212 * This method does nothing if the group's owner isn't an
216 * Note: This method is for internal use only. Clients should not call this method.
218 public void editorStateChanged() {
219 Assert.isTrue(fEditorIsOwner);
221 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
222 fConvertToMac.update();
223 fConvertToUNIX.update();
224 fConvertToWindows.update();
228 * Method declared in ActionGroup
230 public void fillActionBars(IActionBars actionBar) {
231 super.fillActionBars(actionBar);
232 setGlobalActionHandlers(actionBar);
236 * Method declared in ActionGroup
238 public void fillContextMenu(IMenuManager menu) {
239 super.fillContextMenu(menu);
240 if (fEditorIsOwner) {
241 IMenuManager subMenu= createEditorSubMenu(menu);
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 private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
255 IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
257 added+= addEditorAction(result, "Comment"); //$NON-NLS-1$
258 added+= addEditorAction(result, "Uncomment"); //$NON-NLS-1$
259 // result.add(new Separator());
260 // added+= addAction(result, fOrganizeImports);
261 // added+= addAction(result, fAddImport);
262 result.add(new Separator());
263 // added+= addAction(result, fOverrideMethods);
264 // added+= addAction(result, fAddGetterSetter);
265 // added+= addAction(result, fAddUnimplementedConstructors);
266 // added+= addAction(result, fAddJavaDocStub);
267 added+= addAction(result, fAddBookmark);
268 // result.add(new Separator());
269 // added+= addAction(result, fSurroundWithTryCatch);
270 // added+= addAction(result, fExternalizeStrings);
276 * Method declared in ActionGroup
278 public void dispose() {
279 if (fRegisteredSelectionListeners != null) {
280 ISelectionProvider provider= fSite.getSelectionProvider();
281 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
282 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
283 provider.removeSelectionChangedListener(listener);
290 private void setGlobalActionHandlers(IActionBars actionBar) {
291 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
292 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
293 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
294 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
295 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
296 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
297 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
298 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
299 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
300 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
301 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
302 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
303 if (!fEditorIsOwner) {
304 // editor provides its own implementation of these actions.
305 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, fAddBookmark);
306 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.ADD_TASK, fAddTaskAction);
310 private int appendToGroup(IMenuManager menu, IAction action) {
311 if (action != null && action.isEnabled()) {
312 menu.appendToGroup(fGroupName, action);
318 private int addAction(IMenuManager menu, IAction action) {
319 if (action != null && action.isEnabled()) {
326 private int addEditorAction(IMenuManager menu, String actionID) {
329 IAction action= fEditor.getAction(actionID);
332 if (action instanceof IUpdate)
333 ((IUpdate)action).update();
334 if (action.isEnabled()) {
341 private boolean isEditorOwner() {
342 return fEditor != null;