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;
20 import org.eclipse.debug.internal.ui.actions.ActionMessages;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.util.Assert;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.IActionBars;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IWorkbenchActionConstants;
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.part.Page;
36 import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
37 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
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 private boolean fEditorIsOwner;
52 private IWorkbenchSite fSite;
53 private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
54 private List fRegisteredSelectionListeners;
56 // private AddImportOnSelectionAction fAddImport;
57 // private OverrideMethodsAction fOverrideMethods;
58 // private AddGetterSetterAction fAddGetterSetter;
59 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
60 // private AddJavaDocStubAction fAddJavaDocStub;
61 private AddBookmarkAction fAddBookmark;
62 private AddTaskAction fAddTaskAction;
63 // private ExternalizeStringsAction fExternalizeStrings;
64 // private FindStringsToExternalizeAction fFindStringsToExternalize;
65 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
67 // private OrganizeImportsAction fOrganizeImports;
69 private ConvertLineDelimitersAction fConvertToWindows;
70 private ConvertLineDelimitersAction fConvertToUNIX;
71 private ConvertLineDelimitersAction fConvertToMac;
74 * Note: This constructor is for internal use only. Clients should not call this constructor.
76 public GenerateActionGroup(PHPEditor editor, String groupName) {
77 fSite= editor.getSite();
79 fGroupName= groupName;
80 ISelectionProvider provider= fSite.getSelectionProvider();
81 ISelection selection= provider.getSelection();
83 // fAddImport= new AddImportOnSelectionAction(editor);
84 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
85 // fAddImport.update();
86 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
88 // fOrganizeImports= new OrganizeImportsAction(editor);
89 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
90 // fOrganizeImports.editorStateChanged();
91 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
93 // fOverrideMethods= new OverrideMethodsAction(editor);
94 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
95 // fOverrideMethods.editorStateChanged();
96 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
98 // fAddGetterSetter= new AddGetterSetterAction(editor);
99 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
100 // fAddGetterSetter.editorStateChanged();
101 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
103 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
104 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
105 // fAddUnimplementedConstructors.editorStateChanged();
106 // editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
108 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
109 // fAddJavaDocStub.editorStateChanged();
111 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
112 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
113 // fSurroundWithTryCatch.update(selection);
114 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
115 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
117 // fExternalizeStrings= new ExternalizeStringsAction(editor);
118 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
119 // fExternalizeStrings.editorStateChanged();
120 // editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
122 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
123 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
124 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$
126 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
127 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
128 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$
130 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
131 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
132 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$
136 * Creates a new <code>GenerateActionGroup</code>. The group
137 * requires that the selection provided by the page's selection provider
138 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
140 * @param page the page that owns this action group
142 public GenerateActionGroup(Page page) {
143 this(page.getSite());
147 * Creates a new <code>GenerateActionGroup</code>. The group
148 * requires that the selection provided by the part's selection provider
149 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
151 * @param part the view part that owns this action group
153 public GenerateActionGroup(IViewPart part) {
154 this(part.getSite());
157 private GenerateActionGroup(IWorkbenchSite site) {
159 ISelectionProvider provider= fSite.getSelectionProvider();
160 ISelection selection= provider.getSelection();
162 // fOverrideMethods= new OverrideMethodsAction(site);
163 // fAddGetterSetter= new AddGetterSetterAction(site);
164 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
165 // fAddJavaDocStub= new AddJavaDocStubAction(site);
166 fAddBookmark= new AddBookmarkAction(site.getShell());
167 fAddTaskAction= new AddTaskAction(site);
168 // fExternalizeStrings= new ExternalizeStringsAction(site);
169 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
170 // fOrganizeImports= new OrganizeImportsAction(site);
172 // fOverrideMethods.update(selection);
173 // fAddGetterSetter.update(selection);
174 // fAddUnimplementedConstructors.update(selection);
175 // fAddJavaDocStub.update(selection);
176 // fExternalizeStrings.update(selection);
177 // fFindStringsToExternalize.update(selection);
178 fAddTaskAction.update(selection);
179 // fOrganizeImports.update(selection);
180 if (selection instanceof IStructuredSelection) {
181 IStructuredSelection ss= (IStructuredSelection)selection;
182 fAddBookmark.selectionChanged(ss);
184 fAddBookmark.setEnabled(false);
187 // registerSelectionListener(provider, fOverrideMethods);
188 // registerSelectionListener(provider, fAddGetterSetter);
189 // registerSelectionListener(provider, fAddUnimplementedConstructors);
190 // registerSelectionListener(provider, fAddJavaDocStub);
191 registerSelectionListener(provider, fAddBookmark);
192 // registerSelectionListener(provider, fExternalizeStrings);
193 // registerSelectionListener(provider, fFindStringsToExternalize);
194 // registerSelectionListener(provider, fOrganizeImports);
195 registerSelectionListener(provider, fAddTaskAction);
198 private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
199 if (fRegisteredSelectionListeners == null)
200 fRegisteredSelectionListeners= new ArrayList(12);
201 provider.addSelectionChangedListener(listener);
202 fRegisteredSelectionListeners.add(listener);
206 * The state of the editor owning this action group has changed.
207 * This method does nothing if the group's owner isn't an
211 * Note: This method is for internal use only. Clients should not call this method.
213 public void editorStateChanged() {
214 Assert.isTrue(fEditorIsOwner);
215 // fAddImport.update();
216 // fExternalizeStrings.editorStateChanged();
217 // fOrganizeImports.editorStateChanged();
218 // fOverrideMethods.editorStateChanged();
219 // fAddUnimplementedConstructors.editorStateChanged();
220 // fAddJavaDocStub.editorStateChanged();
221 // fSurroundWithTryCatch.editorStateChanged();
222 // fAddGetterSetter.editorStateChanged();
224 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
225 fConvertToMac.update();
226 fConvertToUNIX.update();
227 fConvertToWindows.update();
231 * Method declared in ActionGroup
233 public void fillActionBars(IActionBars actionBar) {
234 super.fillActionBars(actionBar);
235 setGlobalActionHandlers(actionBar);
239 * Method declared in ActionGroup
241 public void fillContextMenu(IMenuManager menu) {
242 super.fillContextMenu(menu);
243 if (fEditorIsOwner) {
244 IMenuManager subMenu= createEditorSubMenu(menu);
246 menu.appendToGroup(fGroupName, subMenu);
248 // appendToGroup(menu, fOrganizeImports);
249 // appendToGroup(menu, fOverrideMethods);
250 // appendToGroup(menu, fAddGetterSetter);
251 // appendToGroup(menu, fAddUnimplementedConstructors);
252 // appendToGroup(menu, fAddJavaDocStub);
253 appendToGroup(menu, fAddBookmark);
257 private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
258 IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
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);
277 * Method declared in ActionGroup
279 public void dispose() {
280 if (fRegisteredSelectionListeners != null) {
281 ISelectionProvider provider= fSite.getSelectionProvider();
282 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
283 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
284 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(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
301 // actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
302 // actionBar.setGlobalActionHandler(JdtActionConstants.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()) {