Refactory: externaltools plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / ui / actions / GenerateActionGroup.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
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;
21
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.IWorkbenchSite;
33 import org.eclipse.ui.actions.ActionGroup;
34 import org.eclipse.ui.actions.AddBookmarkAction;
35 import org.eclipse.ui.ide.IDEActionFactory;
36 import org.eclipse.ui.part.Page;
37 import org.eclipse.ui.texteditor.IUpdate;
38
39 /**
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.
42  * 
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  * 
47  * @since 2.0
48  */
49 public class GenerateActionGroup extends ActionGroup {
50         /**
51          * Pop-up menu: id of the source sub menu (value
52          * <code>net.sourceforge.phpdt.ui.source.menu</code>).
53          * 
54          * @since 3.0
55          */
56         public static final String MENU_ID = "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
57
58         private PHPEditor fEditor;
59
60         // private boolean fEditorIsOwner;
61         private IWorkbenchSite fSite;
62
63         private String fGroupName = IContextMenuConstants.GROUP_SOURCE;
64
65         private List fRegisteredSelectionListeners;
66
67         // private AddImportOnSelectionAction fAddImport;
68         // private OverrideMethodsAction fOverrideMethods;
69         // private AddGetterSetterAction fAddGetterSetter;
70         // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
71         // private AddJavaDocStubAction fAddJavaDocStub;
72         private AddBookmarkAction fAddBookmark;
73
74         private AddTaskAction fAddTaskAction;
75
76         // private ExternalizeStringsAction fExternalizeStrings;
77         // private FindStringsToExternalizeAction fFindStringsToExternalize;
78         // private SurroundWithTryCatchAction fSurroundWithTryCatch;
79
80         // private OrganizeImportsAction fOrganizeImports;
81
82         /**
83          * Note: This constructor is for internal use only. Clients should not call
84          * this constructor.
85          */
86         public GenerateActionGroup(PHPEditor editor, String groupName) {
87                 fSite = editor.getSite();
88                 fEditor = editor;
89                 fGroupName = groupName;
90
91                 ISelectionProvider provider = fSite.getSelectionProvider();
92                 ISelection selection = provider.getSelection();
93
94                 // fAddImport= new AddImportOnSelectionAction(editor);
95                 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
96                 // fAddImport.update();
97                 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
98
99                 // fOrganizeImports= new OrganizeImportsAction(editor);
100                 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
101                 // fOrganizeImports.editorStateChanged();
102                 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
103
104                 // fOverrideMethods= new OverrideMethodsAction(editor);
105                 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
106                 // fOverrideMethods.editorStateChanged();
107                 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
108
109                 // fAddGetterSetter= new AddGetterSetterAction(editor);
110                 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
111                 // fAddGetterSetter.editorStateChanged();
112                 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
113
114                 // fAddUnimplementedConstructors= new
115                 // AddUnimplementedConstructorsAction(editor);
116                 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
117                 // fAddUnimplementedConstructors.editorStateChanged();
118                 // editor.setAction("AddUnimplementedConstructors",
119                 // fAddUnimplementedConstructors); //$NON-NLS-1$
120
121                 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
122                 // fAddJavaDocStub.editorStateChanged();
123                 //
124                 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
125                 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
126                 // fSurroundWithTryCatch.update(selection);
127                 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
128                 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch);
129                 // //$NON-NLS-1$
130                 //
131                 // fExternalizeStrings= new ExternalizeStringsAction(editor);
132                 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
133                 // fExternalizeStrings.editorStateChanged();
134                 // editor.setAction("ExternalizeStrings", fExternalizeStrings);
135                 // //$NON-NLS-1$
136
137         }
138
139         /**
140          * Creates a new <code>GenerateActionGroup</code>. The group requires
141          * that the selection provided by the page's selection provider is of type
142          * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
143          * 
144          * @param page
145          *            the page that owns this action group
146          */
147         public GenerateActionGroup(Page page) {
148                 this(page.getSite());
149         }
150
151         /**
152          * Creates a new <code>GenerateActionGroup</code>. The group requires
153          * that the selection provided by the part's selection provider is of type
154          * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
155          * 
156          * @param part
157          *            the view part that owns this action group
158          */
159         public GenerateActionGroup(IViewPart part) {
160                 this(part.getSite());
161         }
162
163         private GenerateActionGroup(IWorkbenchSite site) {
164                 fSite = site;
165                 ISelectionProvider provider = fSite.getSelectionProvider();
166                 ISelection selection = provider.getSelection();
167
168                 // fOverrideMethods= new OverrideMethodsAction(site);
169                 // fAddGetterSetter= new AddGetterSetterAction(site);
170                 // fAddUnimplementedConstructors= new
171                 // AddUnimplementedConstructorsAction(site);
172                 // fAddJavaDocStub= new AddJavaDocStubAction(site);
173                 fAddBookmark = new AddBookmarkAction(site.getShell());
174                 fAddTaskAction = new AddTaskAction(site);
175                 // fExternalizeStrings= new ExternalizeStringsAction(site);
176                 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
177                 // fOrganizeImports= new OrganizeImportsAction(site);
178                 //
179                 // fOverrideMethods.update(selection);
180                 // fAddGetterSetter.update(selection);
181                 // fAddUnimplementedConstructors.update(selection);
182                 // fAddJavaDocStub.update(selection);
183                 // fExternalizeStrings.update(selection);
184                 // fFindStringsToExternalize.update(selection);
185                 fAddTaskAction.update(selection);
186                 // fOrganizeImports.update(selection);
187                 if (selection instanceof IStructuredSelection) {
188                         IStructuredSelection ss = (IStructuredSelection) selection;
189                         fAddBookmark.selectionChanged(ss);
190                 } else {
191                         fAddBookmark.setEnabled(false);
192                 }
193
194                 // registerSelectionListener(provider, fOverrideMethods);
195                 // registerSelectionListener(provider, fAddGetterSetter);
196                 // registerSelectionListener(provider, fAddUnimplementedConstructors);
197                 // registerSelectionListener(provider, fAddJavaDocStub);
198                 registerSelectionListener(provider, fAddBookmark);
199                 // registerSelectionListener(provider, fExternalizeStrings);
200                 // registerSelectionListener(provider, fFindStringsToExternalize);
201                 // registerSelectionListener(provider, fOrganizeImports);
202                 registerSelectionListener(provider, fAddTaskAction);
203         }
204
205         private void registerSelectionListener(ISelectionProvider provider,
206                         ISelectionChangedListener listener) {
207                 if (fRegisteredSelectionListeners == null)
208                         fRegisteredSelectionListeners = new ArrayList(12);
209                 provider.addSelectionChangedListener(listener);
210                 fRegisteredSelectionListeners.add(listener);
211         }
212
213         /*
214          * The state of the editor owning this action group has changed. This method
215          * does nothing if the group's owner isn't an editor.
216          */
217         /**
218          * Note: This method is for internal use only. Clients should not call this
219          * method.
220          */
221         public void editorStateChanged() {
222                 Assert.isTrue(isEditorOwner());
223
224                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
225         }
226
227         /*
228          * (non-Javadoc) Method declared in ActionGroup
229          */
230         public void fillActionBars(IActionBars actionBar) {
231                 super.fillActionBars(actionBar);
232                 setGlobalActionHandlers(actionBar);
233         }
234
235         /*
236          * (non-Javadoc) Method declared in ActionGroup
237          */
238         // public void fillContextMenu(IMenuManager menu) {
239         // super.fillContextMenu(menu);
240         // if (fEditorIsOwner) {
241         // IMenuManager subMenu= createEditorSubMenu(menu);
242         // if (subMenu != null)
243         // menu.appendToGroup(fGroupName, subMenu);
244         // } else {
245         // // appendToGroup(menu, fOrganizeImports);
246         // // appendToGroup(menu, fOverrideMethods);
247         // // appendToGroup(menu, fAddGetterSetter);
248         // // appendToGroup(menu, fAddUnimplementedConstructors);
249         // // appendToGroup(menu, fAddJavaDocStub);
250         // appendToGroup(menu, fAddBookmark);
251         // }
252         // }
253         /*
254          * (non-Javadoc) Method declared in ActionGroup
255          */
256         // public void fillContextMenu(IMenuManager menu) {
257         // super.fillContextMenu(menu);
258         // IMenuManager subMenu= null;
259         // if (isEditorOwner()) {
260         // subMenu= fillEditorSubMenu(menu);
261         // } else {
262         // // subMenu= createViewSubMenu(menu);
263         // }
264         // if (subMenu != null)
265         // menu.appendToGroup(fGroupName, subMenu);
266         // }
267         public void fillContextMenu(IMenuManager menu) {
268                 super.fillContextMenu(menu);
269                 String shortCut = null; //$NON-NLS-1$
270                 // if (fQuickAccessAction != null) {
271                 // shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
272                 // }
273                 IMenuManager subMenu = new MenuManager(
274                                 ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
275                                 MENU_ID);
276                 int added = 0;
277                 if (isEditorOwner()) {
278                         added = fillEditorSubMenu(subMenu);
279                 }
280                 // else {
281                 // added= fillViewSubMenu(subMenu);
282                 // }
283                 if (added > 0)
284                         menu.appendToGroup(fGroupName, subMenu);
285         }
286
287         private int fillEditorSubMenu(IMenuManager source) {
288                 // IMenuManager result= new
289                 // MenuManager(ActionMessages.getString("SourceMenu.label"));
290                 // //$NON-NLS-1$
291                 int added = 0;
292                 added += addEditorAction(source, "Comment"); //$NON-NLS-1$
293                 added += addEditorAction(source, "Uncomment"); //$NON-NLS-1$
294                 added += addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
295                 added += addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
296                 added += addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
297                 added += addEditorAction(source, "Format"); //$NON-NLS-1$
298                 added += addEditorAction(source, "Indent"); //$NON-NLS-1$
299                 // result.add(new Separator());
300                 // added+= addAction(result, fOrganizeImports);
301                 // added+= addAction(result, fAddImport);
302                 // result.add(new Separator());
303                 // added+= addAction(result, fOverrideMethods);
304                 // added+= addAction(result, fAddGetterSetter);
305                 // added+= addAction(result, fAddUnimplementedConstructors);
306                 // added+= addAction(result, fAddJavaDocStub);
307                 // added+= addAction(result, fAddBookmark);
308                 // result.add(new Separator());
309                 // added+= addAction(result, fSurroundWithTryCatch);
310                 // added+= addAction(result, fExternalizeStrings);
311                 // if (added == 0)
312                 // result= null;
313                 return added;
314         }
315
316         /*
317          * (non-Javadoc) Method declared in ActionGroup
318          */
319         public void dispose() {
320                 if (fRegisteredSelectionListeners != null) {
321                         ISelectionProvider provider = fSite.getSelectionProvider();
322                         for (Iterator iter = fRegisteredSelectionListeners.iterator(); iter
323                                         .hasNext();) {
324                                 ISelectionChangedListener listener = (ISelectionChangedListener) iter
325                                                 .next();
326                                 provider.removeSelectionChangedListener(listener);
327                         }
328                 }
329                 fEditor = null;
330                 super.dispose();
331         }
332
333         private void setGlobalActionHandlers(IActionBars actionBar) {
334                 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT,
335                 // fAddImport);
336                 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH,
337                 // fSurroundWithTryCatch);
338                 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS,
339                 // fOverrideMethods);
340                 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER,
341                 // fAddGetterSetter);
342                 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS,
343                 // fAddUnimplementedConstructors);
344                 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT,
345                 // fAddJavaDocStub);
346                 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS,
347                 // fExternalizeStrings);
348                 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE,
349                 // fFindStringsToExternalize);
350                 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS,
351                 // fOrganizeImports);
352                 if (!isEditorOwner()) {
353                         // editor provides its own implementation of these actions.
354                         actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(),
355                                         fAddBookmark);
356                         actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(),
357                                         fAddTaskAction);
358                 }
359         }
360
361         private int appendToGroup(IMenuManager menu, IAction action) {
362                 if (action != null && action.isEnabled()) {
363                         menu.appendToGroup(fGroupName, action);
364                         return 1;
365                 }
366                 return 0;
367         }
368
369         private int addAction(IMenuManager menu, IAction action) {
370                 if (action != null && action.isEnabled()) {
371                         menu.add(action);
372                         return 1;
373                 }
374                 return 0;
375         }
376
377         private int addEditorAction(IMenuManager menu, String actionID) {
378                 if (fEditor == null)
379                         return 0;
380                 IAction action = fEditor.getAction(actionID);
381                 if (action == null)
382                         return 0;
383                 if (action instanceof IUpdate)
384                         ((IUpdate) action).update();
385                 if (action.isEnabled()) {
386                         menu.add(action);
387                         return 1;
388                 }
389                 return 0;
390         }
391
392         private boolean isEditorOwner() {
393                 return fEditor != null;
394         }
395 }