misc changes
[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 import org.eclipse.debug.internal.ui.actions.ActionMessages;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.util.Assert;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionChangedListener;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.IActionBars;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IWorkbenchActionConstants;
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.part.Page;
36 import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
37 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
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         private boolean fEditorIsOwner;
52         private IWorkbenchSite fSite;
53         private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
54         private List fRegisteredSelectionListeners;
55         
56 //      private AddImportOnSelectionAction fAddImport;
57 //      private OverrideMethodsAction fOverrideMethods;
58 //      private AddGetterSetterAction fAddGetterSetter;
59 //      private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
60 //      private AddJavaDocStubAction fAddJavaDocStub;
61         private AddBookmarkAction fAddBookmark;
62         private AddTaskAction fAddTaskAction;
63 //      private ExternalizeStringsAction fExternalizeStrings;
64 //      private FindStringsToExternalizeAction fFindStringsToExternalize;
65 //      private SurroundWithTryCatchAction fSurroundWithTryCatch;
66         
67         // private OrganizeImportsAction fOrganizeImports;
68
69         private ConvertLineDelimitersAction fConvertToWindows;
70         private ConvertLineDelimitersAction fConvertToUNIX;
71         private ConvertLineDelimitersAction fConvertToMac;
72         
73         /**
74          * Note: This constructor is for internal use only. Clients should not call this constructor.
75          */
76         public GenerateActionGroup(PHPEditor editor, String groupName) {
77                 fSite= editor.getSite();
78                 fEditorIsOwner= true;
79                 fGroupName= groupName;
80                 ISelectionProvider provider= fSite.getSelectionProvider();
81                 ISelection selection= provider.getSelection();
82
83 //              fAddImport= new AddImportOnSelectionAction(editor);
84 //              fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
85 //              fAddImport.update();
86 //              editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
87                 
88 //              fOrganizeImports= new OrganizeImportsAction(editor);
89 //              fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
90 //              fOrganizeImports.editorStateChanged();
91 //              editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
92
93 //              fOverrideMethods= new OverrideMethodsAction(editor);
94 //              fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
95 //              fOverrideMethods.editorStateChanged();
96 //              editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
97                 
98 //              fAddGetterSetter= new AddGetterSetterAction(editor);
99 //              fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
100 //              fAddGetterSetter.editorStateChanged();
101 //              editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
102                 
103 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
104 //              fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
105 //              fAddUnimplementedConstructors.editorStateChanged();
106 //              editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$          
107                 
108 //              fAddJavaDocStub= new AddJavaDocStubAction(editor);
109 //              fAddJavaDocStub.editorStateChanged();
110 //              
111 //              fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
112 //              fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
113 //              fSurroundWithTryCatch.update(selection);
114 //              provider.addSelectionChangedListener(fSurroundWithTryCatch);
115 //              editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$          
116 //              
117 //              fExternalizeStrings= new ExternalizeStringsAction(editor);
118 //              fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
119 //              fExternalizeStrings.editorStateChanged();
120 //              editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$              
121                 
122                 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
123                 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
124                 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$            
125                 
126                 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
127                 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
128                 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$          
129
130                 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
131                 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
132                 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$            
133         }
134         
135         /**
136          * Creates a new <code>GenerateActionGroup</code>. The group 
137          * requires that the selection provided by the page's selection provider 
138          * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
139          * 
140          * @param page the page that owns this action group
141          */
142         public GenerateActionGroup(Page page) {
143                 this(page.getSite());
144         }
145
146         /**
147          * Creates a new <code>GenerateActionGroup</code>. The group 
148          * requires that the selection provided by the part's selection provider 
149          * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
150          * 
151          * @param part the view part that owns this action group
152          */
153         public GenerateActionGroup(IViewPart part) {
154                 this(part.getSite());
155         }
156         
157         private GenerateActionGroup(IWorkbenchSite site) {
158                 fSite= site;
159                 ISelectionProvider provider= fSite.getSelectionProvider();
160                 ISelection selection= provider.getSelection();
161                 
162 //              fOverrideMethods= new OverrideMethodsAction(site);
163 //              fAddGetterSetter= new AddGetterSetterAction(site);
164 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
165 //              fAddJavaDocStub= new AddJavaDocStubAction(site);
166                 fAddBookmark= new AddBookmarkAction(site.getShell());
167                 fAddTaskAction= new AddTaskAction(site);
168 //              fExternalizeStrings= new ExternalizeStringsAction(site);
169 //              fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
170 //              fOrganizeImports= new OrganizeImportsAction(site);
171 //              
172 //              fOverrideMethods.update(selection);
173 //              fAddGetterSetter.update(selection);
174 //              fAddUnimplementedConstructors.update(selection);        
175 //              fAddJavaDocStub.update(selection);
176 //              fExternalizeStrings.update(selection);
177 //              fFindStringsToExternalize.update(selection);
178                 fAddTaskAction.update(selection);
179 //              fOrganizeImports.update(selection);
180                 if (selection instanceof IStructuredSelection) {
181                         IStructuredSelection ss= (IStructuredSelection)selection;
182                         fAddBookmark.selectionChanged(ss);
183                 } else {
184                         fAddBookmark.setEnabled(false);
185                 }
186                 
187 //              registerSelectionListener(provider, fOverrideMethods);
188 //              registerSelectionListener(provider, fAddGetterSetter);
189 //              registerSelectionListener(provider, fAddUnimplementedConstructors);
190 //              registerSelectionListener(provider, fAddJavaDocStub);
191                 registerSelectionListener(provider, fAddBookmark);
192 //              registerSelectionListener(provider, fExternalizeStrings);
193 //              registerSelectionListener(provider, fFindStringsToExternalize);
194 //              registerSelectionListener(provider, fOrganizeImports);
195                 registerSelectionListener(provider, fAddTaskAction);
196         }
197         
198         private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
199                 if (fRegisteredSelectionListeners == null)
200                         fRegisteredSelectionListeners= new ArrayList(12);
201                 provider.addSelectionChangedListener(listener);
202                 fRegisteredSelectionListeners.add(listener);
203         }
204         
205         /*
206          * The state of the editor owning this action group has changed. 
207          * This method does nothing if the group's owner isn't an
208          * editor.
209          */
210         /**
211          * Note: This method is for internal use only. Clients should not call this method.
212          */
213         public void editorStateChanged() {
214                 Assert.isTrue(fEditorIsOwner);
215 //              fAddImport.update();
216 //              fExternalizeStrings.editorStateChanged();
217 //              fOrganizeImports.editorStateChanged();
218 //              fOverrideMethods.editorStateChanged();
219 //              fAddUnimplementedConstructors.editorStateChanged();
220 //              fAddJavaDocStub.editorStateChanged();
221 //              fSurroundWithTryCatch.editorStateChanged();
222 //              fAddGetterSetter.editorStateChanged();
223                 
224                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
225                 fConvertToMac.update();
226                 fConvertToUNIX.update();
227                 fConvertToWindows.update();
228         }
229
230         /* (non-Javadoc)
231          * Method declared in ActionGroup
232          */
233         public void fillActionBars(IActionBars actionBar) {
234                 super.fillActionBars(actionBar);
235                 setGlobalActionHandlers(actionBar);
236         }
237         
238         /* (non-Javadoc)
239          * Method declared in ActionGroup
240          */
241         public void fillContextMenu(IMenuManager menu) {
242                 super.fillContextMenu(menu);
243                 if (fEditorIsOwner) {
244                         IMenuManager subMenu= createEditorSubMenu(menu);
245                         if (subMenu != null)
246                                 menu.appendToGroup(fGroupName, subMenu);
247                 } else {
248 //                      appendToGroup(menu, fOrganizeImports);
249 //                      appendToGroup(menu, fOverrideMethods);
250 //                      appendToGroup(menu, fAddGetterSetter);
251 //                      appendToGroup(menu, fAddUnimplementedConstructors);
252 //                      appendToGroup(menu, fAddJavaDocStub);
253                         appendToGroup(menu, fAddBookmark);
254                 }
255         }
256         
257         private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
258                 IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
259                 int added= 0;
260 //              added+= addAction(result, fOrganizeImports);
261 //              added+= addAction(result, fAddImport);
262 //              result.add(new Separator());
263 //              added+= addAction(result, fOverrideMethods);
264 //              added+= addAction(result, fAddGetterSetter);
265 //              added+= addAction(result, fAddUnimplementedConstructors);
266 //              added+= addAction(result, fAddJavaDocStub);
267                 added+= addAction(result, fAddBookmark);
268 //              result.add(new Separator());            
269 //              added+= addAction(result, fSurroundWithTryCatch);
270 //              added+= addAction(result, fExternalizeStrings);
271                 if (added == 0)
272                         result= null;
273                 return result;
274         }
275
276         /* (non-Javadoc)
277          * Method declared in ActionGroup
278          */
279         public void dispose() {
280                 if (fRegisteredSelectionListeners != null) {
281                         ISelectionProvider provider= fSite.getSelectionProvider();
282                         for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
283                                 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
284                                 provider.removeSelectionChangedListener(listener);
285                         }
286                 }
287                 super.dispose();
288         }
289         
290         private void setGlobalActionHandlers(IActionBars actionBar) {
291 //              actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
292 //              actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
293 //              actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
294 //              actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
295 //              actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
296 //              actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
297 //              actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
298 //              actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
299 //              actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
300 //              actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
301 //              actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
302 //              actionBar.setGlobalActionHandler(JdtActionConstants.CONVERT_LINE_DELIMITERS_TO_MAC, fConvertToMac);
303                 if (!fEditorIsOwner) {
304                         // editor provides its own implementation of these actions.
305                         actionBar.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, fAddBookmark);
306                         actionBar.setGlobalActionHandler(IWorkbenchActionConstants.ADD_TASK, fAddTaskAction);
307                 }
308         }
309         
310         private int appendToGroup(IMenuManager menu, IAction action) {
311                 if (action != null && action.isEnabled()) {
312                         menu.appendToGroup(fGroupName, action);
313                         return 1;
314                 }
315                 return 0;
316         }       
317
318         private int addAction(IMenuManager menu, IAction action) {
319                 if (action != null && action.isEnabled()) {
320                         menu.add(action);
321                         return 1;
322                 }
323                 return 0;
324         }       
325 }