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 private PHPEditor fEditor;
54 // private boolean fEditorIsOwner;
55 private IWorkbenchSite fSite;
56 private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
57 private List fRegisteredSelectionListeners;
59 // private AddImportOnSelectionAction fAddImport;
60 // private OverrideMethodsAction fOverrideMethods;
61 // private AddGetterSetterAction fAddGetterSetter;
62 // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
63 // private AddJavaDocStubAction fAddJavaDocStub;
64 private AddBookmarkAction fAddBookmark;
65 private AddTaskAction fAddTaskAction;
66 // private ExternalizeStringsAction fExternalizeStrings;
67 // private FindStringsToExternalizeAction fFindStringsToExternalize;
68 // private SurroundWithTryCatchAction fSurroundWithTryCatch;
70 // private OrganizeImportsAction fOrganizeImports;
72 private ConvertLineDelimitersAction fConvertToWindows;
73 private ConvertLineDelimitersAction fConvertToUNIX;
74 private ConvertLineDelimitersAction fConvertToMac;
77 * Note: This constructor is for internal use only. Clients should not call this constructor.
79 public GenerateActionGroup(PHPEditor editor, String groupName) {
80 fSite= editor.getSite();
82 fGroupName= groupName;
84 ISelectionProvider provider= fSite.getSelectionProvider();
85 ISelection selection= provider.getSelection();
87 // fAddImport= new AddImportOnSelectionAction(editor);
88 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
89 // fAddImport.update();
90 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
92 // fOrganizeImports= new OrganizeImportsAction(editor);
93 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
94 // fOrganizeImports.editorStateChanged();
95 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
97 // fOverrideMethods= new OverrideMethodsAction(editor);
98 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
99 // fOverrideMethods.editorStateChanged();
100 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
102 // fAddGetterSetter= new AddGetterSetterAction(editor);
103 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
104 // fAddGetterSetter.editorStateChanged();
105 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
107 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
108 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
109 // fAddUnimplementedConstructors.editorStateChanged();
110 // editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$
112 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
113 // fAddJavaDocStub.editorStateChanged();
115 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
116 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
117 // fSurroundWithTryCatch.update(selection);
118 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
119 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$
121 // fExternalizeStrings= new ExternalizeStringsAction(editor);
122 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
123 // fExternalizeStrings.editorStateChanged();
124 // editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$
126 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
127 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
128 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$
130 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
131 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
132 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$
134 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
135 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
136 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$
140 * Creates a new <code>GenerateActionGroup</code>. The group
141 * requires that the selection provided by the page's selection provider
142 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
144 * @param page the page that owns this action group
146 public GenerateActionGroup(Page page) {
147 this(page.getSite());
151 * Creates a new <code>GenerateActionGroup</code>. The group
152 * requires that the selection provided by the part's selection provider
153 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
155 * @param part the view part that owns this action group
157 public GenerateActionGroup(IViewPart part) {
158 this(part.getSite());
161 private GenerateActionGroup(IWorkbenchSite site) {
163 ISelectionProvider provider= fSite.getSelectionProvider();
164 ISelection selection= provider.getSelection();
166 // fOverrideMethods= new OverrideMethodsAction(site);
167 // fAddGetterSetter= new AddGetterSetterAction(site);
168 // fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
169 // fAddJavaDocStub= new AddJavaDocStubAction(site);
170 fAddBookmark= new AddBookmarkAction(site.getShell());
171 fAddTaskAction= new AddTaskAction(site);
172 // fExternalizeStrings= new ExternalizeStringsAction(site);
173 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
174 // fOrganizeImports= new OrganizeImportsAction(site);
176 // fOverrideMethods.update(selection);
177 // fAddGetterSetter.update(selection);
178 // fAddUnimplementedConstructors.update(selection);
179 // fAddJavaDocStub.update(selection);
180 // fExternalizeStrings.update(selection);
181 // fFindStringsToExternalize.update(selection);
182 fAddTaskAction.update(selection);
183 // fOrganizeImports.update(selection);
184 if (selection instanceof IStructuredSelection) {
185 IStructuredSelection ss= (IStructuredSelection)selection;
186 fAddBookmark.selectionChanged(ss);
188 fAddBookmark.setEnabled(false);
191 // registerSelectionListener(provider, fOverrideMethods);
192 // registerSelectionListener(provider, fAddGetterSetter);
193 // registerSelectionListener(provider, fAddUnimplementedConstructors);
194 // registerSelectionListener(provider, fAddJavaDocStub);
195 registerSelectionListener(provider, fAddBookmark);
196 // registerSelectionListener(provider, fExternalizeStrings);
197 // registerSelectionListener(provider, fFindStringsToExternalize);
198 // registerSelectionListener(provider, fOrganizeImports);
199 registerSelectionListener(provider, fAddTaskAction);
202 private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
203 if (fRegisteredSelectionListeners == null)
204 fRegisteredSelectionListeners= new ArrayList(12);
205 provider.addSelectionChangedListener(listener);
206 fRegisteredSelectionListeners.add(listener);
210 * The state of the editor owning this action group has changed.
211 * This method does nothing if the group's owner isn't an
215 * Note: This method is for internal use only. Clients should not call this method.
217 public void editorStateChanged() {
218 Assert.isTrue(isEditorOwner());
220 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
221 fConvertToMac.update();
222 fConvertToUNIX.update();
223 fConvertToWindows.update();
227 * Method declared in ActionGroup
229 public void fillActionBars(IActionBars actionBar) {
230 super.fillActionBars(actionBar);
231 setGlobalActionHandlers(actionBar);
235 * Method declared in ActionGroup
237 // public void fillContextMenu(IMenuManager menu) {
238 // super.fillContextMenu(menu);
239 // if (fEditorIsOwner) {
240 // IMenuManager subMenu= createEditorSubMenu(menu);
241 // if (subMenu != null)
242 // menu.appendToGroup(fGroupName, subMenu);
244 //// appendToGroup(menu, fOrganizeImports);
245 //// appendToGroup(menu, fOverrideMethods);
246 //// appendToGroup(menu, fAddGetterSetter);
247 //// appendToGroup(menu, fAddUnimplementedConstructors);
248 //// appendToGroup(menu, fAddJavaDocStub);
249 // appendToGroup(menu, fAddBookmark);
253 * Method declared in ActionGroup
255 public void fillContextMenu(IMenuManager menu) {
256 super.fillContextMenu(menu);
257 IMenuManager subMenu= null;
258 if (isEditorOwner()) {
259 subMenu= createEditorSubMenu(menu);
261 // subMenu= createViewSubMenu(menu);
264 menu.appendToGroup(fGroupName, subMenu);
267 private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
268 IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
270 added+= addEditorAction(result, "Comment"); //$NON-NLS-1$
271 added+= addEditorAction(result, "Uncomment"); //$NON-NLS-1$
272 added+= addEditorAction(result, "Format"); //$NON-NLS-1$
273 // result.add(new Separator());
274 // added+= addAction(result, fOrganizeImports);
275 // added+= addAction(result, fAddImport);
276 // result.add(new Separator());
277 // added+= addAction(result, fOverrideMethods);
278 // added+= addAction(result, fAddGetterSetter);
279 // added+= addAction(result, fAddUnimplementedConstructors);
280 // added+= addAction(result, fAddJavaDocStub);
281 // added+= addAction(result, fAddBookmark);
282 // result.add(new Separator());
283 // added+= addAction(result, fSurroundWithTryCatch);
284 // added+= addAction(result, fExternalizeStrings);
290 * Method declared in ActionGroup
292 public void dispose() {
293 if (fRegisteredSelectionListeners != null) {
294 ISelectionProvider provider= fSite.getSelectionProvider();
295 for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
296 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
297 provider.removeSelectionChangedListener(listener);
304 private void setGlobalActionHandlers(IActionBars actionBar) {
305 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
306 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
307 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
308 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
309 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
310 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
311 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
312 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
313 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
314 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
315 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
316 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
317 if (!isEditorOwner()) {
318 // editor provides its own implementation of these actions.
319 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, fAddBookmark);
320 actionBar.setGlobalActionHandler(IWorkbenchActionConstants.ADD_TASK, fAddTaskAction);
324 private int appendToGroup(IMenuManager menu, IAction action) {
325 if (action != null && action.isEnabled()) {
326 menu.appendToGroup(fGroupName, action);
332 private int addAction(IMenuManager menu, IAction action) {
333 if (action != null && action.isEnabled()) {
340 private int addEditorAction(IMenuManager menu, String actionID) {
343 IAction action= fEditor.getAction(actionID);
346 if (action instanceof IUpdate)
347 ((IUpdate)action).update();
348 if (action.isEnabled()) {
355 private boolean isEditorOwner() {
356 return fEditor != null;