PHP highlighting colors could now be managed by the new PreferencePage
[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.action.Separator;
26 import org.eclipse.jface.util.Assert;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.IViewPart;
33 import org.eclipse.ui.IWorkbenchActionConstants;
34 import org.eclipse.ui.IWorkbenchSite;
35 import org.eclipse.ui.actions.ActionGroup;
36 import org.eclipse.ui.actions.AddBookmarkAction;
37 import org.eclipse.ui.part.Page;
38 import org.eclipse.ui.texteditor.ConvertLineDelimitersAction;
39 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
40 import org.eclipse.ui.texteditor.IUpdate;
41
42 /**
43  * Action group that adds the source and generate actions to a part's context
44  * menu and installs handlers for the corresponding global menu actions.
45  * 
46  * <p>
47  * This class may be instantiated; it is not intended to be subclassed.
48  * </p>
49  * 
50  * @since 2.0
51  */
52 public class GenerateActionGroup extends ActionGroup {
53         
54   private PHPEditor fEditor;
55         private boolean fEditorIsOwner;
56         private IWorkbenchSite fSite;
57         private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
58         private List fRegisteredSelectionListeners;
59         
60 //      private AddImportOnSelectionAction fAddImport;
61 //      private OverrideMethodsAction fOverrideMethods;
62 //      private AddGetterSetterAction fAddGetterSetter;
63 //      private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
64 //      private AddJavaDocStubAction fAddJavaDocStub;
65         private AddBookmarkAction fAddBookmark;
66         private AddTaskAction fAddTaskAction;
67 //      private ExternalizeStringsAction fExternalizeStrings;
68 //      private FindStringsToExternalizeAction fFindStringsToExternalize;
69 //      private SurroundWithTryCatchAction fSurroundWithTryCatch;
70         
71         // private OrganizeImportsAction fOrganizeImports;
72
73         private ConvertLineDelimitersAction fConvertToWindows;
74         private ConvertLineDelimitersAction fConvertToUNIX;
75         private ConvertLineDelimitersAction fConvertToMac;
76         
77         /**
78          * Note: This constructor is for internal use only. Clients should not call this constructor.
79          */
80         public GenerateActionGroup(PHPEditor editor, String groupName) {
81                 fSite= editor.getSite();
82     fEditor= editor;
83                 fEditorIsOwner= true;
84                 fGroupName= groupName;
85                 ISelectionProvider provider= fSite.getSelectionProvider();
86                 ISelection selection= provider.getSelection();
87
88 //              fAddImport= new AddImportOnSelectionAction(editor);
89 //              fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
90 //              fAddImport.update();
91 //              editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
92                 
93 //              fOrganizeImports= new OrganizeImportsAction(editor);
94 //              fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
95 //              fOrganizeImports.editorStateChanged();
96 //              editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
97
98 //              fOverrideMethods= new OverrideMethodsAction(editor);
99 //              fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
100 //              fOverrideMethods.editorStateChanged();
101 //              editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
102                 
103 //              fAddGetterSetter= new AddGetterSetterAction(editor);
104 //              fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
105 //              fAddGetterSetter.editorStateChanged();
106 //              editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
107                 
108 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(editor);
109 //              fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
110 //              fAddUnimplementedConstructors.editorStateChanged();
111 //              editor.setAction("AddUnimplementedConstructors", fAddUnimplementedConstructors); //$NON-NLS-1$          
112                 
113 //              fAddJavaDocStub= new AddJavaDocStubAction(editor);
114 //              fAddJavaDocStub.editorStateChanged();
115 //              
116 //              fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
117 //              fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
118 //              fSurroundWithTryCatch.update(selection);
119 //              provider.addSelectionChangedListener(fSurroundWithTryCatch);
120 //              editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch); //$NON-NLS-1$          
121 //              
122 //              fExternalizeStrings= new ExternalizeStringsAction(editor);
123 //              fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
124 //              fExternalizeStrings.editorStateChanged();
125 //              editor.setAction("ExternalizeStrings", fExternalizeStrings); //$NON-NLS-1$              
126                 
127                 fConvertToWindows= new ConvertLineDelimitersAction(editor, "\r\n"); //$NON-NLS-1$
128                 fConvertToWindows.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_WINDOWS);
129                 editor.setAction("ConvertLineDelimitersToWindows", fConvertToWindows); //$NON-NLS-1$            
130                 
131                 fConvertToUNIX= new ConvertLineDelimitersAction(editor, "\n"); //$NON-NLS-1$
132                 fConvertToUNIX.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_UNIX);
133                 editor.setAction("ConvertLineDelimitersToUNIX", fConvertToUNIX); //$NON-NLS-1$          
134
135                 fConvertToMac= new ConvertLineDelimitersAction(editor, "\r"); //$NON-NLS-1$
136                 fConvertToMac.setActionDefinitionId( ITextEditorActionDefinitionIds .CONVERT_LINE_DELIMITERS_TO_MAC);
137                 editor.setAction("ConvertLineDelimitersToMac", fConvertToMac); //$NON-NLS-1$            
138         }
139         
140         /**
141          * Creates a new <code>GenerateActionGroup</code>. The group 
142          * requires that the selection provided by the page's selection provider 
143          * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
144          * 
145          * @param page 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 
153          * requires that the selection provided by the part's selection provider 
154          * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
155          * 
156          * @param part the view part that owns this action group
157          */
158         public GenerateActionGroup(IViewPart part) {
159                 this(part.getSite());
160         }
161         
162         private GenerateActionGroup(IWorkbenchSite site) {
163                 fSite= site;
164                 ISelectionProvider provider= fSite.getSelectionProvider();
165                 ISelection selection= provider.getSelection();
166                 
167 //              fOverrideMethods= new OverrideMethodsAction(site);
168 //              fAddGetterSetter= new AddGetterSetterAction(site);
169 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
170 //              fAddJavaDocStub= new AddJavaDocStubAction(site);
171                 fAddBookmark= new AddBookmarkAction(site.getShell());
172                 fAddTaskAction= new AddTaskAction(site);
173 //              fExternalizeStrings= new ExternalizeStringsAction(site);
174 //              fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
175 //              fOrganizeImports= new OrganizeImportsAction(site);
176 //              
177 //              fOverrideMethods.update(selection);
178 //              fAddGetterSetter.update(selection);
179 //              fAddUnimplementedConstructors.update(selection);        
180 //              fAddJavaDocStub.update(selection);
181 //              fExternalizeStrings.update(selection);
182 //              fFindStringsToExternalize.update(selection);
183                 fAddTaskAction.update(selection);
184 //              fOrganizeImports.update(selection);
185                 if (selection instanceof IStructuredSelection) {
186                         IStructuredSelection ss= (IStructuredSelection)selection;
187                         fAddBookmark.selectionChanged(ss);
188                 } else {
189                         fAddBookmark.setEnabled(false);
190                 }
191                 
192 //              registerSelectionListener(provider, fOverrideMethods);
193 //              registerSelectionListener(provider, fAddGetterSetter);
194 //              registerSelectionListener(provider, fAddUnimplementedConstructors);
195 //              registerSelectionListener(provider, fAddJavaDocStub);
196                 registerSelectionListener(provider, fAddBookmark);
197 //              registerSelectionListener(provider, fExternalizeStrings);
198 //              registerSelectionListener(provider, fFindStringsToExternalize);
199 //              registerSelectionListener(provider, fOrganizeImports);
200                 registerSelectionListener(provider, fAddTaskAction);
201         }
202         
203         private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
204                 if (fRegisteredSelectionListeners == null)
205                         fRegisteredSelectionListeners= new ArrayList(12);
206                 provider.addSelectionChangedListener(listener);
207                 fRegisteredSelectionListeners.add(listener);
208         }
209         
210         /*
211          * The state of the editor owning this action group has changed. 
212          * This method does nothing if the group's owner isn't an
213          * editor.
214          */
215         /**
216          * Note: This method is for internal use only. Clients should not call this method.
217          */
218         public void editorStateChanged() {
219                 Assert.isTrue(fEditorIsOwner);
220                 
221                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
222                 fConvertToMac.update();
223                 fConvertToUNIX.update();
224                 fConvertToWindows.update();
225         }
226
227         /* (non-Javadoc)
228          * Method declared in ActionGroup
229          */
230         public void fillActionBars(IActionBars actionBar) {
231                 super.fillActionBars(actionBar);
232                 setGlobalActionHandlers(actionBar);
233         }
234         
235         /* (non-Javadoc)
236          * 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   private IMenuManager createEditorSubMenu(IMenuManager mainMenu) {
255     IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
256     int added= 0;
257     added+= addEditorAction(result, "Comment"); //$NON-NLS-1$
258     added+= addEditorAction(result, "Uncomment"); //$NON-NLS-1$
259 //    result.add(new Separator());
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         /* (non-Javadoc)
276          * Method declared in ActionGroup
277          */
278         public void dispose() {
279                 if (fRegisteredSelectionListeners != null) {
280                         ISelectionProvider provider= fSite.getSelectionProvider();
281                         for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
282                                 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
283                                 provider.removeSelectionChangedListener(listener);
284                         }
285                 }
286     fEditor= null;
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(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_WINDOWS, fConvertToWindows);
301                 actionBar.setGlobalActionHandler(PHPdtActionConstants.CONVERT_LINE_DELIMITERS_TO_UNIX, fConvertToUNIX);
302                 actionBar.setGlobalActionHandler(PHPdtActionConstants.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         
326   private int addEditorAction(IMenuManager menu, String actionID) {
327     if (fEditor == null)
328       return 0;
329     IAction action= fEditor.getAction(actionID);
330     if (action == null)
331       return 0;
332     if (action instanceof IUpdate)
333       ((IUpdate)action).update();
334     if (action.isEnabled()) {
335       menu.add(action);
336       return 1;
337     }
338     return 0;
339   }
340   
341   private boolean isEditorOwner() {
342     return fEditor != null;
343   }     
344 }