intial core test module
[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.AddTaskAction;
18 import net.sourceforge.phpdt.ui.IContextMenuConstants;
19 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
20
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;
39
40 /**
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.
43  * 
44  * <p>
45  * This class may be instantiated; it is not intended to be subclassed.
46  * </p>
47  * 
48  * @since 2.0
49  */
50 public class GenerateActionGroup extends ActionGroup {
51         
52         private boolean fEditorIsOwner;
53         private IWorkbenchSite fSite;
54         private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
55         private List fRegisteredSelectionListeners;
56         
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;
67         
68         // private OrganizeImportsAction fOrganizeImports;
69
70         private ConvertLineDelimitersAction fConvertToWindows;
71         private ConvertLineDelimitersAction fConvertToUNIX;
72         private ConvertLineDelimitersAction fConvertToMac;
73         
74         /**
75          * Note: This constructor is for internal use only. Clients should not call this constructor.
76          */
77         public GenerateActionGroup(PHPEditor editor, String groupName) {
78                 fSite= editor.getSite();
79                 fEditorIsOwner= true;
80                 fGroupName= groupName;
81                 ISelectionProvider provider= fSite.getSelectionProvider();
82                 ISelection selection= provider.getSelection();
83
84 //              fAddImport= new AddImportOnSelectionAction(editor);
85 //              fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
86 //              fAddImport.update();
87 //              editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
88                 
89 //              fOrganizeImports= new OrganizeImportsAction(editor);
90 //              fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
91 //              fOrganizeImports.editorStateChanged();
92 //              editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
93
94 //              fOverrideMethods= new OverrideMethodsAction(editor);
95 //              fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
96 //              fOverrideMethods.editorStateChanged();
97 //              editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
98                 
99 //              fAddGetterSetter= new AddGetterSetterAction(editor);
100 //              fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
101 //              fAddGetterSetter.editorStateChanged();
102 //              editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
103                 
104 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
105 //              fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
106 //              fAddUnimplementedConstructors.editorStateChanged();
107 //              editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$          
108                 
109 //              fAddJavaDocStub= new AddJavaDocStubAction(editor);
110 //              fAddJavaDocStub.editorStateChanged();
111 //              
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$          
117 //              
118 //              fExternalizeStrings= new ExternalizeStringsAction(editor);
119 //              fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
120 //              fExternalizeStrings.editorStateChanged();
121 //              editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$              
122                 
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$            
126                 
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$          
130
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$            
134         }
135         
136         /**
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>.
140          * 
141          * @param page the page that owns this action group
142          */
143         public GenerateActionGroup(Page page) {
144                 this(page.getSite());
145         }
146
147         /**
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>.
151          * 
152          * @param part the view part that owns this action group
153          */
154         public GenerateActionGroup(IViewPart part) {
155                 this(part.getSite());
156         }
157         
158         private GenerateActionGroup(IWorkbenchSite site) {
159                 fSite= site;
160                 ISelectionProvider provider= fSite.getSelectionProvider();
161                 ISelection selection= provider.getSelection();
162                 
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);
172 //              
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);
184                 } else {
185                         fAddBookmark.setEnabled(false);
186                 }
187                 
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);
197         }
198         
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);
204         }
205         
206         /*
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
209          * editor.
210          */
211         /**
212          * Note: This method is for internal use only. Clients should not call this method.
213          */
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();
224                 
225                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
226                 fConvertToMac.update();
227                 fConvertToUNIX.update();
228                 fConvertToWindows.update();
229         }
230
231         /* (non-Javadoc)
232          * Method declared in ActionGroup
233          */
234         public void fillActionBars(IActionBars actionBar) {
235                 super.fillActionBars(actionBar);
236                 setGlobalActionHandlers(actionBar);
237         }
238         
239         /* (non-Javadoc)
240          * Method declared in ActionGroup
241          */
242         public void fillContextMenu(IMenuManager menu) {
243                 super.fillContextMenu(menu);
244                 if (fEditorIsOwner) {
245                         IMenuManager subMenu= createEditorSubMenu(menu);
246                         if (subMenu != null)
247                                 menu.appendToGroup(fGroupName, subMenu);
248                 } else {
249 //                      appendToGroup(menu, fOrganizeImports);
250 //                      appendToGroup(menu, fOverrideMethods);
251 //                      appendToGroup(menu, fAddGetterSetter);
252 //                      appendToGroup(menu, fAddUnimplementedConstructors);
253 //                      appendToGroup(menu, fAddJavaDocStub);
254                         appendToGroup(menu, fAddBookmark);
255                 }
256         }
257         
258         private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
259                 IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
260                 int added= 0;
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);
272                 if (added == 0)
273                         result= null;
274                 return result;
275         }
276
277         /* (non-Javadoc)
278          * Method declared in ActionGroup
279          */
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);
286                         }
287                 }
288                 super.dispose();
289         }
290         
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);
308                 }
309         }
310         
311         private int appendToGroup(IMenuManager menu, IAction action) {
312                 if (action != null && action.isEnabled()) {
313                         menu.appendToGroup(fGroupName, action);
314                         return 1;
315                 }
316                 return 0;
317         }       
318
319         private int addAction(IMenuManager menu, IAction action) {
320                 if (action != null && action.isEnabled()) {
321                         menu.add(action);
322                         return 1;
323                 }
324                 return 0;
325         }       
326 }