removed ColorManager, not used anywhere
[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.IWorkbenchSite;
33 import org.eclipse.ui.actions.ActionGroup;
34 import org.eclipse.ui.actions.AddBookmarkAction;
35 import org.eclipse.ui.ide.IDEActionFactory;
36 import org.eclipse.ui.part.Page;
37 import org.eclipse.ui.texteditor.IUpdate;
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          * Pop-up menu: id of the source sub menu (value <code>net.sourceforge.phpdt.ui.source.menu</code>).
52          *
53          * @since 3.0
54          */
55         public static final String MENU_ID= "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
56
57   private PHPEditor fEditor;
58 //      private boolean fEditorIsOwner;
59         private IWorkbenchSite fSite;
60         private String fGroupName= IContextMenuConstants.GROUP_SOURCE;
61         private List fRegisteredSelectionListeners;
62
63 //      private AddImportOnSelectionAction fAddImport;
64 //      private OverrideMethodsAction fOverrideMethods;
65 //      private AddGetterSetterAction fAddGetterSetter;
66 //      private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
67 //      private AddJavaDocStubAction fAddJavaDocStub;
68         private AddBookmarkAction fAddBookmark;
69         private AddTaskAction fAddTaskAction;
70 //      private ExternalizeStringsAction fExternalizeStrings;
71 //      private FindStringsToExternalizeAction fFindStringsToExternalize;
72 //      private SurroundWithTryCatchAction fSurroundWithTryCatch;
73
74         // private OrganizeImportsAction fOrganizeImports;
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         }
127
128         /**
129          * Creates a new <code>GenerateActionGroup</code>. The group
130          * requires that the selection provided by the page's selection provider
131          * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
132          *
133          * @param page the page that owns this action group
134          */
135         public GenerateActionGroup(Page page) {
136                 this(page.getSite());
137         }
138
139         /**
140          * Creates a new <code>GenerateActionGroup</code>. The group
141          * requires that the selection provided by the part's selection provider
142          * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
143          *
144          * @param part the view part that owns this action group
145          */
146         public GenerateActionGroup(IViewPart part) {
147                 this(part.getSite());
148         }
149
150         private GenerateActionGroup(IWorkbenchSite site) {
151                 fSite= site;
152                 ISelectionProvider provider= fSite.getSelectionProvider();
153                 ISelection selection= provider.getSelection();
154
155 //              fOverrideMethods= new OverrideMethodsAction(site);
156 //              fAddGetterSetter= new AddGetterSetterAction(site);
157 //              fAddUnimplementedConstructors= new AddUnimplementedConstructorsAction(site);
158 //              fAddJavaDocStub= new AddJavaDocStubAction(site);
159                 fAddBookmark= new AddBookmarkAction(site.getShell());
160                 fAddTaskAction= new AddTaskAction(site);
161 //              fExternalizeStrings= new ExternalizeStringsAction(site);
162 //              fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
163 //              fOrganizeImports= new OrganizeImportsAction(site);
164 //
165 //              fOverrideMethods.update(selection);
166 //              fAddGetterSetter.update(selection);
167 //              fAddUnimplementedConstructors.update(selection);
168 //              fAddJavaDocStub.update(selection);
169 //              fExternalizeStrings.update(selection);
170 //              fFindStringsToExternalize.update(selection);
171                 fAddTaskAction.update(selection);
172 //              fOrganizeImports.update(selection);
173                 if (selection instanceof IStructuredSelection) {
174                         IStructuredSelection ss= (IStructuredSelection)selection;
175                         fAddBookmark.selectionChanged(ss);
176                 } else {
177                         fAddBookmark.setEnabled(false);
178                 }
179
180 //              registerSelectionListener(provider, fOverrideMethods);
181 //              registerSelectionListener(provider, fAddGetterSetter);
182 //              registerSelectionListener(provider, fAddUnimplementedConstructors);
183 //              registerSelectionListener(provider, fAddJavaDocStub);
184                 registerSelectionListener(provider, fAddBookmark);
185 //              registerSelectionListener(provider, fExternalizeStrings);
186 //              registerSelectionListener(provider, fFindStringsToExternalize);
187 //              registerSelectionListener(provider, fOrganizeImports);
188                 registerSelectionListener(provider, fAddTaskAction);
189         }
190
191         private void registerSelectionListener(ISelectionProvider provider, ISelectionChangedListener listener) {
192                 if (fRegisteredSelectionListeners == null)
193                         fRegisteredSelectionListeners= new ArrayList(12);
194                 provider.addSelectionChangedListener(listener);
195                 fRegisteredSelectionListeners.add(listener);
196         }
197
198         /*
199          * The state of the editor owning this action group has changed.
200          * This method does nothing if the group's owner isn't an
201          * editor.
202          */
203         /**
204          * Note: This method is for internal use only. Clients should not call this method.
205          */
206         public void editorStateChanged() {
207                 Assert.isTrue(isEditorOwner());
208
209                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
210         }
211
212         /* (non-Javadoc)
213          * Method declared in ActionGroup
214          */
215         public void fillActionBars(IActionBars actionBar) {
216                 super.fillActionBars(actionBar);
217                 setGlobalActionHandlers(actionBar);
218         }
219
220         /* (non-Javadoc)
221          * Method declared in ActionGroup
222          */
223 //      public void fillContextMenu(IMenuManager menu) {
224 //              super.fillContextMenu(menu);
225 //              if (fEditorIsOwner) {
226 //                      IMenuManager subMenu= createEditorSubMenu(menu);
227 //                      if (subMenu != null)
228 //                              menu.appendToGroup(fGroupName, subMenu);
229 //              } else {
230 ////                    appendToGroup(menu, fOrganizeImports);
231 ////                    appendToGroup(menu, fOverrideMethods);
232 ////                    appendToGroup(menu, fAddGetterSetter);
233 ////                    appendToGroup(menu, fAddUnimplementedConstructors);
234 ////                    appendToGroup(menu, fAddJavaDocStub);
235 //                      appendToGroup(menu, fAddBookmark);
236 //              }
237 //      }
238         /* (non-Javadoc)
239          * Method declared in ActionGroup
240          */
241 //      public void fillContextMenu(IMenuManager menu) {
242 //              super.fillContextMenu(menu);
243 //              IMenuManager subMenu= null;
244 //              if (isEditorOwner()) {
245 //                      subMenu= fillEditorSubMenu(menu);
246 //              } else {
247 //      //              subMenu= createViewSubMenu(menu);
248 //              }
249 //              if (subMenu != null)
250 //                      menu.appendToGroup(fGroupName, subMenu);
251 //      }
252         public void fillContextMenu(IMenuManager menu) {
253                 super.fillContextMenu(menu);
254                 String shortCut= null; //$NON-NLS-1$
255 //              if (fQuickAccessAction != null) {
256 //                      shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
257 //              }
258                 IMenuManager subMenu= new MenuManager(
259                         ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
260                         MENU_ID);
261                 int added= 0;
262                 if (isEditorOwner()) {
263                         added= fillEditorSubMenu(subMenu);
264                 }
265 //              else {
266 //                      added= fillViewSubMenu(subMenu);
267 //              }
268                 if (added > 0)
269                         menu.appendToGroup(fGroupName, subMenu);
270         }
271   private int fillEditorSubMenu(IMenuManager source) {
272 //    IMenuManager result= new MenuManager(ActionMessages.getString("SourceMenu.label")); //$NON-NLS-1$
273     int added= 0;
274     added+= addEditorAction(source, "Comment"); //$NON-NLS-1$
275     added+= addEditorAction(source, "Uncomment"); //$NON-NLS-1$
276     added+= addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
277         added+= addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
278         added+= addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
279         added+= addEditorAction(source, "Format"); //$NON-NLS-1$
280         added+= addEditorAction(source, "Indent"); //$NON-NLS-1$
281 //    result.add(new Separator());
282 //    added+= addAction(result, fOrganizeImports);
283 //    added+= addAction(result, fAddImport);
284 //    result.add(new Separator());
285 //    added+= addAction(result, fOverrideMethods);
286 //    added+= addAction(result, fAddGetterSetter);
287 //    added+= addAction(result, fAddUnimplementedConstructors);
288 //    added+= addAction(result, fAddJavaDocStub);
289 //    added+= addAction(result, fAddBookmark);
290 //    result.add(new Separator());
291 //    added+= addAction(result, fSurroundWithTryCatch);
292 //    added+= addAction(result, fExternalizeStrings);
293 //    if (added == 0)
294 //      result= null;
295     return added;
296   }
297         /* (non-Javadoc)
298          * Method declared in ActionGroup
299          */
300         public void dispose() {
301                 if (fRegisteredSelectionListeners != null) {
302                         ISelectionProvider provider= fSite.getSelectionProvider();
303                         for (Iterator iter= fRegisteredSelectionListeners.iterator(); iter.hasNext();) {
304                                 ISelectionChangedListener listener= (ISelectionChangedListener) iter.next();
305                                 provider.removeSelectionChangedListener(listener);
306                         }
307                 }
308     fEditor= null;
309                 super.dispose();
310         }
311
312         private void setGlobalActionHandlers(IActionBars actionBar) {
313 //              actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT, fAddImport);
314 //              actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatch);
315 //              actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS, fOverrideMethods);
316 //              actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER, fAddGetterSetter);
317 //              actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS, fAddUnimplementedConstructors);
318 //              actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT, fAddJavaDocStub);
319 //              actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS, fExternalizeStrings);
320 //              actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE, fFindStringsToExternalize);
321 //              actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS, fOrganizeImports);
322                 if (!isEditorOwner()) {
323                         // editor provides its own implementation of these actions.
324                         actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), fAddBookmark);
325                         actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), fAddTaskAction);
326                 }
327         }
328
329         private int appendToGroup(IMenuManager menu, IAction action) {
330                 if (action != null && action.isEnabled()) {
331                         menu.appendToGroup(fGroupName, action);
332                         return 1;
333                 }
334                 return 0;
335         }
336
337         private int addAction(IMenuManager menu, IAction action) {
338                 if (action != null && action.isEnabled()) {
339                         menu.add(action);
340                         return 1;
341                 }
342                 return 0;
343         }
344
345   private int addEditorAction(IMenuManager menu, String actionID) {
346     if (fEditor == null)
347       return 0;
348     IAction action= fEditor.getAction(actionID);
349     if (action == null)
350       return 0;
351     if (action instanceof IUpdate)
352       ((IUpdate)action).update();
353     if (action.isEnabled()) {
354       menu.add(action);
355       return 1;
356     }
357     return 0;
358   }
359
360   private boolean isEditorOwner() {
361     return fEditor != null;
362   }
363 }