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.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;
41 * Action group that adds the source and generate actions to a part's context
42 * menu and installs handlers for the corresponding global menu actions.
45 * This class may be instantiated; it is not intended to be subclassed.
50 public class GenerateActionGroup extends ActionGroup {
52 private boolean fEditorIsOwner;
53 private IWorkbenchSite fSite;
54 private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
55 private List fRegisteredSelectionListeners;
57 // private AddImportOnSelectionAction fAddImport;
58 // private OverrideMethodsAction fOverrideMethods;
59 // private AddGetterSetterAction fAddGetterSetter;
60 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
61 // private AddJavaDocStubAction fAddJavaDocStub;
62 private AddBookmarkAction fAddBookmark;
63 private AddTaskAction fAddTaskAction;
64 // private ExternalizeStringsAction fExternalizeStrings;
65 // private FindStringsToExternalizeAction fFindStringsToExternalize;
66 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
68 // private OrganizeImportsAction fOrganizeImports;
70 private ConvertLineDelimitersAction fConvertToWindows;
71 private ConvertLineDelimitersAction fConvertToUNIX;
72 private ConvertLineDelimitersAction fConvertToMac;
75 * Note: This constructor is for internal use only. Clients should not call this constructor.
77 public GenerateActionGroup(PHPEditor editor, String groupName) {
78 fSite= editor.getSite();
80 fGroupName= groupName;
81 ISelectionProvider provider= fSite.getSelectionProvider();
82 ISelection selection= provider.getSelection();
84 // fAddImport= new AddImportOnSelectionAction(editor);
85 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
86 // fAddImport.update();
87 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
89 // fOrganizeImports= new OrganizeImportsAction(editor);
90 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
91 // fOrganizeImports.editorStateChanged();
92 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
94 // fOverrideMethods= new OverrideMethodsAction(editor);
95 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
96 // fOverrideMethods.editorStateChanged();
97 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
99 // fAddGetterSetter= new AddGetterSetterAction(editor);
100 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
101 // fAddGetterSetter.editorStateChanged();
102 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
104 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
105 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
106 // fAddUnimplementedConstructors.editorStateChanged();
107 // editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
109 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
110 // fAddJavaDocStub.editorStateChanged();
112 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
113 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
114 // fSurroundWithTryCatch.update(selection);
115 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
116 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
118 // fExternalizeStrings= new ExternalizeStringsAction(editor);
119 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
120 // fExternalizeStrings.editorStateChanged();
121 // editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
123 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
124 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
125 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$
127 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
128 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
129 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$
131 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
132 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
133 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$
137 * Creates a new <code>GenerateActionGroup</code>. The group
138 * requires that the selection provided by the page's selection provider
139 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
141 * @param page the page that owns this action group
143 public GenerateActionGroup(Page page) {
144 this(page.getSite());
148 * Creates a new <code>GenerateActionGroup</code>. The group
149 * requires that the selection provided by the part's selection provider
150 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
152 * @param part the view part that owns this action group
154 public GenerateActionGroup(IViewPart part) {
155 this(part.getSite());
158 private GenerateActionGroup(IWorkbenchSite site) {
160 ISelectionProvider provider= fSite.getSelectionProvider();
161 ISelection selection= provider.getSelection();
163 // fOverrideMethods= new OverrideMethodsAction(site);
164 // fAddGetterSetter= new AddGetterSetterAction(site);
165 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
166 // fAddJavaDocStub= new AddJavaDocStubAction(site);
167 fAddBookmark= new AddBookmarkAction(site.getShell());
168 fAddTaskAction= new AddTaskAction(site);
169 // fExternalizeStrings= new ExternalizeStringsAction(site);
170 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
171 // fOrganizeImports= new OrganizeImportsAction(site);
173 // fOverrideMethods.update(selection);
174 // fAddGetterSetter.update(selection);
175 // fAddUnimplementedConstructors.update(selection);
176 // fAddJavaDocStub.update(selection);
177 // fExternalizeStrings.update(selection);
178 // fFindStringsToExternalize.update(selection);
179 fAddTaskAction.update(selection);
180 // fOrganizeImports.update(selection);
181 if (selection instanceof IStructuredSelection) {
182 IStructuredSelection ss= (IStructuredSelection)selection;
183 fAddBookmark.selectionChanged(ss);
185 fAddBookmark.setEnabled(false);
188 // registerSelectionListener(provider, fOverrideMethods);
189 // registerSelectionListener(provider, fAddGetterSetter);
190 // registerSelectionListener(provider, fAddUnimplementedConstructors);
191 // registerSelectionListener(provider, fAddJavaDocStub);
192 registerSelectionListener(provider, fAddBookmark);
193 // registerSelectionListener(provider, fExternalizeStrings);
194 // registerSelectionListener(provider, fFindStringsToExternalize);
195 // registerSelectionListener(provider, fOrganizeImports);
196 registerSelectionListener(provider, fAddTaskAction);
199 private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
200 if (fRegisteredSelectionListeners == null)
201 fRegisteredSelectionListeners= new ArrayList(12);
202 provider.addSelectionChangedListener(listener);
203 fRegisteredSelectionListeners.add(listener);
207 * The state of the editor owning this action group has changed.
208 * This method does nothing if the group's owner isn't an
212 * Note: This method is for internal use only. Clients should not call this method.
214 public void editorStateChanged() {
215 Assert.isTrue(fEditorIsOwner);
216 // fAddImport.update();
217 // fExternalizeStrings.editorStateChanged();
218 // fOrganizeImports.editorStateChanged();
219 // fOverrideMethods.editorStateChanged();
220 // fAddUnimplementedConstructors.editorStateChanged();
221 // fAddJavaDocStub.editorStateChanged();
222 // fSurroundWithTryCatch.editorStateChanged();
223 // fAddGetterSetter.editorStateChanged();
225 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
226 fConvertToMac.update();
227 fConvertToUNIX.update();
228 fConvertToWindows.update();
232 * Method declared in ActionGroup
234 public void fillActionBars(IActionBars actionBar) {
235 super.fillActionBars(actionBar);
236 setGlobalActionHandlers(actionBar);
240 * Method declared in ActionGroup
242 public void fillContextMenu(IMenuManager menu) {
243 super.fillContextMenu(menu);
244 if (fEditorIsOwner) {
245 IMenuManager subMenu= createEditorSubMenu(menu);
247 menu.appendToGroup(fGroupName, subMenu);
249 // appendToGroup(menu, fOrganizeImports);
250 // appendToGroup(menu, fOverrideMethods);
251 // appendToGroup(menu, fAddGetterSetter);
252 // appendToGroup(menu, fAddUnimplementedConstructors);
253 // appendToGroup(menu, fAddJavaDocStub);
254 appendToGroup(menu, fAddBookmark);
258 private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
259 IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
261 // added+= addAction(result, fOrganizeImports);
262 // added+= addAction(result, fAddImport);
263 // result.add(new Separator());
264 // added+= addAction(result, fOverrideMethods);
265 // added+= addAction(result, fAddGetterSetter);
266 // added+= addAction(result, fAddUnimplementedConstructors);
267 // added+= addAction(result, fAddJavaDocStub);
268 added+= addAction(result, fAddBookmark);
269 // result.add(new Separator());
270 // added+= addAction(result, fSurroundWithTryCatch);
271 // added+= addAction(result, fExternalizeStrings);
278 * Method declared in ActionGroup
280 public void dispose() {
281 if (fRegisteredSelectionListeners != null) {
282 ISelectionProvider provider= fSite.getSelectionProvider();
283 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
284 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
285 provider.removeSelectionChangedListener(listener);
291 private void setGlobalActionHandlers(IActionBars actionBar) {
292 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
293 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
294 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
295 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
296 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
297 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
298 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
299 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
300 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
301 // actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
302 // actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
303 // actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
304 if (!fEditorIsOwner) {
305 // editor provides its own implementation of these actions.
306 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, fAddBookmark);
307 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.ADD_TASK, fAddTaskAction);
311 private int appendToGroup(IMenuManager menu, IAction action) {
312 if (action != null && action.isEnabled()) {
313 menu.appendToGroup(fGroupName, action);
319 private int addAction(IMenuManager menu, IAction action) {
320 if (action != null && action.isEnabled()) {