fixed parser bug;
[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   private PHPEditor fEditor;
54 //      private boolean fEditorIsOwner;
55         private IWorkbenchSite fSite;
56         private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
57         private List fRegisteredSelectionListeners;
58         
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;
69         
70         // private OrganizeImportsAction fOrganizeImports;
71
72         private ConvertLineDelimitersAction fConvertToWindows;
73         private ConvertLineDelimitersAction fConvertToUNIX;
74         private ConvertLineDelimitersAction fConvertToMac;
75         
76         /**
77          * Note: This constructor is for internal use only. Clients should not call this constructor.
78          */
79         public GenerateActionGroup(PHPEditor editor, String groupName) {
80                 fSite= editor.getSite();
81                 fEditor= editor;
82                 fGroupName= groupName;
83                                 
84                 ISelectionProvider provider= fSite.getSelectionProvider();
85                 ISelection selection= provider.getSelection();
86
87 //              fAddImport= new AddImportOnSelectionAction(editor);
88 //              fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
89 //              fAddImport.update();
90 //              editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
91                 
92 //              fOrganizeImports= new OrganizeImportsAction(editor);
93 //              fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
94 //              fOrganizeImports.editorStateChanged();
95 //              editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
96
97 //              fOverrideMethods= new OverrideMethodsAction(editor);
98 //              fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
99 //              fOverrideMethods.editorStateChanged();
100 //              editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
101                 
102 //              fAddGetterSetter= new AddGetterSetterAction(editor);
103 //              fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
104 //              fAddGetterSetter.editorStateChanged();
105 //              editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
106                 
107 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
108 //              fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
109 //              fAddUnimplementedConstructors.editorStateChanged();
110 //              editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$          
111                 
112 //              fAddJavaDocStub= new AddJavaDocStubAction(editor);
113 //              fAddJavaDocStub.editorStateChanged();
114 //              
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$          
120 //              
121 //              fExternalizeStrings= new ExternalizeStringsAction(editor);
122 //              fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
123 //              fExternalizeStrings.editorStateChanged();
124 //              editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$              
125                 
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$            
129                 
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$          
133
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$            
137         }
138         
139         /**
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>.
143          * 
144          * @param page the page that owns this action group
145          */
146         public GenerateActionGroup(Page page) {
147                 this(page.getSite());
148         }
149
150         /**
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>.
154          * 
155          * @param part the view part that owns this action group
156          */
157         public GenerateActionGroup(IViewPart part) {
158                 this(part.getSite());
159         }
160         
161         private GenerateActionGroup(IWorkbenchSite site) {
162                 fSite= site;
163                 ISelectionProvider provider= fSite.getSelectionProvider();
164                 ISelection selection= provider.getSelection();
165                 
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);
175 //              
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);
187                 } else {
188                         fAddBookmark.setEnabled(false);
189                 }
190                 
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);
200         }
201         
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);
207         }
208         
209         /*
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
212          * editor.
213          */
214         /**
215          * Note: This method is for internal use only. Clients should not call this method.
216          */
217         public void editorStateChanged() {
218                 Assert.isTrue(isEditorOwner());
219                 
220                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
221                 fConvertToMac.update();
222                 fConvertToUNIX.update();
223                 fConvertToWindows.update();
224         }
225
226         /* (non-Javadoc)
227          * Method declared in ActionGroup
228          */
229         public void fillActionBars(IActionBars actionBar) {
230                 super.fillActionBars(actionBar);
231                 setGlobalActionHandlers(actionBar);
232         }
233         
234         /* (non-Javadoc)
235          * Method declared in ActionGroup
236          */
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);
243 //              } else {
244 ////                    appendToGroup(menu, fOrganizeImports);
245 ////                    appendToGroup(menu, fOverrideMethods);
246 ////                    appendToGroup(menu, fAddGetterSetter);
247 ////                    appendToGroup(menu, fAddUnimplementedConstructors);
248 ////                    appendToGroup(menu, fAddJavaDocStub);
249 //                      appendToGroup(menu, fAddBookmark);
250 //              }
251 //      }
252         /* (non-Javadoc)
253          * Method declared in ActionGroup
254          */
255         public void fillContextMenu(IMenuManager menu) {
256                 super.fillContextMenu(menu);
257                 IMenuManager subMenu= null;
258                 if (isEditorOwner()) {
259                         subMenu= createEditorSubMenu(menu);
260                 } else {
261         //              subMenu= createViewSubMenu(menu);
262                 }
263                 if (subMenu != null)
264                         menu.appendToGroup(fGroupName, subMenu);
265         }
266         
267   private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
268     IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
269     int added= 0;
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);
285     if (added == 0)
286       result= null;
287     return result;
288   }
289         /* (non-Javadoc)
290          * Method declared in ActionGroup
291          */
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);
298                         }
299                 }
300     fEditor= null;
301                 super.dispose();
302         }
303         
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);
321                 }
322         }
323         
324         private int appendToGroup(IMenuManager menu, IAction action) {
325                 if (action != null && action.isEnabled()) {
326                         menu.appendToGroup(fGroupName, action);
327                         return 1;
328                 }
329                 return 0;
330         }       
331
332         private int addAction(IMenuManager menu, IAction action) {
333                 if (action != null && action.isEnabled()) {
334                         menu.add(action);
335                         return 1;
336                 }
337                 return 0;
338         }       
339         
340   private int addEditorAction(IMenuManager menu, String actionID) {
341     if (fEditor == null)
342       return 0;
343     IAction action= fEditor.getAction(actionID);
344     if (action == null)
345       return 0;
346     if (action instanceof IUpdate)
347       ((IUpdate)action).update();
348     if (action.isEnabled()) {
349       menu.add(action);
350       return 1;
351     }
352     return 0;
353   }
354   
355   private boolean isEditorOwner() {
356     return fEditor != null;
357   }     
358 }