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