1) Added setting of syntax properties to italic, underline and strike through.
[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 //incastrix
26 //import org.eclipse.jface.text.Assert;
27 import org.eclipse.core.runtime.Assert;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.ISelectionProvider;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.ui.IActionBars;
33 //import org.eclipse.ui.IViewPart;
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.ide.IDEActionFactory;
38 import org.eclipse.ui.part.Page;
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          * Pop-up menu: id of the source sub menu (value
54          * <code>net.sourceforge.phpdt.ui.source.menu</code>).
55          * 
56          * @since 3.0
57          */
58         public static final String MENU_ID = "net.sourceforge.pheclipse.ui.source.menu"; //$NON-NLS-1$
59
60         private PHPEditor fEditor;
61
62         // private boolean fEditorIsOwner;
63         private IWorkbenchSite fSite;
64
65         private String fGroupName = IContextMenuConstants.GROUP_SOURCE;
66
67         private List fRegisteredSelectionListeners;
68
69         // private AddImportOnSelectionAction fAddImport;
70         // private OverrideMethodsAction fOverrideMethods;
71         // private AddGetterSetterAction fAddGetterSetter;
72         // private AddUnimplementedConstructorsAction fAddUnimplementedConstructors;
73         // private AddJavaDocStubAction fAddJavaDocStub;
74         private AddBookmarkAction fAddBookmark;
75
76         private AddTaskAction fAddTaskAction;
77
78         // private ExternalizeStringsAction fExternalizeStrings;
79         // private FindStringsToExternalizeAction fFindStringsToExternalize;
80         // private SurroundWithTryCatchAction fSurroundWithTryCatch;
81
82         // private OrganizeImportsAction fOrganizeImports;
83
84         /**
85          * Note: This constructor is for internal use only. Clients should not call
86          * this constructor.
87          */
88         public GenerateActionGroup(PHPEditor editor, String groupName) {
89                 fSite = editor.getSite();
90                 fEditor = editor;
91                 fGroupName = groupName;
92
93                 //ISelectionProvider provider = fSite.getSelectionProvider();
94                 //ISelection selection = provider.getSelection();
95
96                 // fAddImport= new AddImportOnSelectionAction(editor);
97                 // fAddImport.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_IMPORT);
98                 // fAddImport.update();
99                 // editor.setAction("AddImport", fAddImport); //$NON-NLS-1$
100
101                 // fOrganizeImports= new OrganizeImportsAction(editor);
102                 // fOrganizeImports.setActionDefinitionId(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
103                 // fOrganizeImports.editorStateChanged();
104                 // editor.setAction("OrganizeImports", fOrganizeImports); //$NON-NLS-1$
105
106                 // fOverrideMethods= new OverrideMethodsAction(editor);
107                 // fOverrideMethods.setActionDefinitionId(IJavaEditorActionDefinitionIds.OVERRIDE_METHODS);
108                 // fOverrideMethods.editorStateChanged();
109                 // editor.setAction("OverrideMethods", fOverrideMethods); //$NON-NLS-1$
110
111                 // fAddGetterSetter= new AddGetterSetterAction(editor);
112                 // fAddGetterSetter.setActionDefinitionId(IJavaEditorActionDefinitionIds.CREATE_GETTER_SETTER);
113                 // fAddGetterSetter.editorStateChanged();
114                 // editor.setAction("AddGetterSetter", fAddGetterSetter); //$NON-NLS-1$
115
116                 // fAddUnimplementedConstructors= new
117                 // AddUnimplementedConstructorsAction(editor);
118                 // fAddUnimplementedConstructors.setActionDefinitionId(IJavaEditorActionDefinitionIds.ADD_UNIMPLEMENTED_CONTRUCTORS);
119                 // fAddUnimplementedConstructors.editorStateChanged();
120                 // editor.setAction("AddUnimplementedConstructors",
121                 // fAddUnimplementedConstructors); //$NON-NLS-1$
122
123                 // fAddJavaDocStub= new AddJavaDocStubAction(editor);
124                 // fAddJavaDocStub.editorStateChanged();
125                 //
126                 // fSurroundWithTryCatch= new SurroundWithTryCatchAction(editor);
127                 // fSurroundWithTryCatch.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
128                 // fSurroundWithTryCatch.update(selection);
129                 // provider.addSelectionChangedListener(fSurroundWithTryCatch);
130                 // editor.setAction("SurroundWithTryCatch", fSurroundWithTryCatch);
131                 // //$NON-NLS-1$
132                 //
133                 // fExternalizeStrings= new ExternalizeStringsAction(editor);
134                 // fExternalizeStrings.setActionDefinitionId(IJavaEditorActionDefinitionIds.EXTERNALIZE_STRINGS);
135                 // fExternalizeStrings.editorStateChanged();
136                 // editor.setAction("ExternalizeStrings", fExternalizeStrings);
137                 // //$NON-NLS-1$
138
139         }
140
141         /**
142          * Creates a new <code>GenerateActionGroup</code>. The group requires
143          * that the selection provided by the page's selection provider is of type
144          * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
145          * 
146          * @param page
147          *            the page that owns this action group
148          */
149         public GenerateActionGroup(Page page) {
150                 this(page.getSite());
151         }
152
153         /**
154          * Creates a new <code>GenerateActionGroup</code>. The group requires
155          * that the selection provided by the part's selection provider is of type
156          * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
157          * 
158          * @param part
159          *            the view part that owns this action group
160          */
161 //      public GenerateActionGroup(IViewPart part) {
162 //              this(part.getSite());
163 //      }
164
165         private GenerateActionGroup(IWorkbenchSite site) {
166                 fSite = site;
167                 ISelectionProvider provider = fSite.getSelectionProvider();
168                 ISelection selection = provider.getSelection();
169
170                 // fOverrideMethods= new OverrideMethodsAction(site);
171                 // fAddGetterSetter= new AddGetterSetterAction(site);
172                 // fAddUnimplementedConstructors= new
173                 // AddUnimplementedConstructorsAction(site);
174                 // fAddJavaDocStub= new AddJavaDocStubAction(site);
175                 fAddBookmark = new AddBookmarkAction(site.getShell());
176                 fAddTaskAction = new AddTaskAction(site);
177                 // fExternalizeStrings= new ExternalizeStringsAction(site);
178                 // fFindStringsToExternalize= new FindStringsToExternalizeAction(site);
179                 // fOrganizeImports= new OrganizeImportsAction(site);
180                 //
181                 // fOverrideMethods.update(selection);
182                 // fAddGetterSetter.update(selection);
183                 // fAddUnimplementedConstructors.update(selection);
184                 // fAddJavaDocStub.update(selection);
185                 // fExternalizeStrings.update(selection);
186                 // fFindStringsToExternalize.update(selection);
187                 fAddTaskAction.update(selection);
188                 // fOrganizeImports.update(selection);
189                 if (selection instanceof IStructuredSelection) {
190                         IStructuredSelection ss = (IStructuredSelection) selection;
191                         fAddBookmark.selectionChanged(ss);
192                 } else {
193                         fAddBookmark.setEnabled(false);
194                 }
195
196                 // registerSelectionListener(provider, fOverrideMethods);
197                 // registerSelectionListener(provider, fAddGetterSetter);
198                 // registerSelectionListener(provider, fAddUnimplementedConstructors);
199                 // registerSelectionListener(provider, fAddJavaDocStub);
200                 registerSelectionListener(provider, fAddBookmark);
201                 // registerSelectionListener(provider, fExternalizeStrings);
202                 // registerSelectionListener(provider, fFindStringsToExternalize);
203                 // registerSelectionListener(provider, fOrganizeImports);
204                 registerSelectionListener(provider, fAddTaskAction);
205         }
206
207         private void registerSelectionListener(ISelectionProvider provider,
208                         ISelectionChangedListener listener) {
209                 if (fRegisteredSelectionListeners == null)
210                         fRegisteredSelectionListeners = new ArrayList(12);
211                 provider.addSelectionChangedListener(listener);
212                 fRegisteredSelectionListeners.add(listener);
213         }
214
215         /*
216          * The state of the editor owning this action group has changed. This method
217          * does nothing if the group's owner isn't an editor.
218          */
219         /**
220          * Note: This method is for internal use only. Clients should not call this
221          * method.
222          */
223         public void editorStateChanged() {
224                 Assert.isTrue(isEditorOwner());
225
226                 // http://dev.eclipse.org/bugs/show_bug.cgi?id=17709
227         }
228
229         /*
230          * (non-Javadoc) Method declared in ActionGroup
231          */
232         public void fillActionBars(IActionBars actionBar) {
233                 super.fillActionBars(actionBar);
234                 setGlobalActionHandlers(actionBar);
235         }
236
237         /*
238          * (non-Javadoc) Method declared in ActionGroup
239          */
240         // public void fillContextMenu(IMenuManager menu) {
241         // super.fillContextMenu(menu);
242         // if (fEditorIsOwner) {
243         // IMenuManager subMenu= createEditorSubMenu(menu);
244         // if (subMenu != null)
245         // menu.appendToGroup(fGroupName, subMenu);
246         // } else {
247         // // appendToGroup(menu, fOrganizeImports);
248         // // appendToGroup(menu, fOverrideMethods);
249         // // appendToGroup(menu, fAddGetterSetter);
250         // // appendToGroup(menu, fAddUnimplementedConstructors);
251         // // appendToGroup(menu, fAddJavaDocStub);
252         // appendToGroup(menu, fAddBookmark);
253         // }
254         // }
255         /*
256          * (non-Javadoc) Method declared in ActionGroup
257          */
258         // public void fillContextMenu(IMenuManager menu) {
259         // super.fillContextMenu(menu);
260         // IMenuManager subMenu= null;
261         // if (isEditorOwner()) {
262         // subMenu= fillEditorSubMenu(menu);
263         // } else {
264         // // subMenu= createViewSubMenu(menu);
265         // }
266         // if (subMenu != null)
267         // menu.appendToGroup(fGroupName, subMenu);
268         // }
269         public void fillContextMenu(IMenuManager menu) {
270                 super.fillContextMenu(menu);
271                 String shortCut = null; //$NON-NLS-1$
272                 // if (fQuickAccessAction != null) {
273                 // shortCut= fQuickAccessAction.getShortCutString(); //$NON-NLS-1$
274                 // }
275                 IMenuManager subMenu = new MenuManager(
276                                 ActionMessages.getString("SourceMenu.label") + (shortCut != null ? "\t" + shortCut : ""), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
277                                 MENU_ID);
278                 int added = 0;
279                 if (isEditorOwner()) {
280                         added = fillEditorSubMenu(subMenu);
281                 }
282                 // else {
283                 // added= fillViewSubMenu(subMenu);
284                 // }
285                 if (added > 0)
286                         menu.appendToGroup(fGroupName, subMenu);
287         }
288
289         private int fillEditorSubMenu(IMenuManager source) {
290                 // IMenuManager result= new
291                 // MenuManager(ActionMessages.getString("SourceMenu.label"));
292                 // //$NON-NLS-1$
293                 int added = 0;
294                 added += addEditorAction(source, "Comment"); //$NON-NLS-1$
295                 added += addEditorAction(source, "Uncomment"); //$NON-NLS-1$
296                 added += addEditorAction(source, "ToggleComment"); //$NON-NLS-1$
297                 added += addEditorAction(source, "AddBlockComment"); //$NON-NLS-1$
298                 added += addEditorAction(source, "RemoveBlockComment"); //$NON-NLS-1$
299                 added += addEditorAction(source, "Format"); //$NON-NLS-1$
300                 added += addEditorAction(source, "Indent"); //$NON-NLS-1$
301                 // result.add(new Separator());
302                 // added+= addAction(result, fOrganizeImports);
303                 // added+= addAction(result, fAddImport);
304                 // result.add(new Separator());
305                 // added+= addAction(result, fOverrideMethods);
306                 // added+= addAction(result, fAddGetterSetter);
307                 // added+= addAction(result, fAddUnimplementedConstructors);
308                 // added+= addAction(result, fAddJavaDocStub);
309                 // added+= addAction(result, fAddBookmark);
310                 // result.add(new Separator());
311                 // added+= addAction(result, fSurroundWithTryCatch);
312                 // added+= addAction(result, fExternalizeStrings);
313                 // if (added == 0)
314                 // result= null;
315                 return added;
316         }
317
318         /*
319          * (non-Javadoc) Method declared in ActionGroup
320          */
321         public void dispose() {
322                 if (fRegisteredSelectionListeners != null) {
323                         ISelectionProvider provider = fSite.getSelectionProvider();
324                         for (Iterator iter = fRegisteredSelectionListeners.iterator(); iter
325                                         .hasNext();) {
326                                 ISelectionChangedListener listener = (ISelectionChangedListener) iter
327                                                 .next();
328                                 provider.removeSelectionChangedListener(listener);
329                         }
330                 }
331                 fEditor = null;
332                 super.dispose();
333         }
334
335         private void setGlobalActionHandlers(IActionBars actionBar) {
336                 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_IMPORT,
337                 // fAddImport);
338                 // actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH,
339                 // fSurroundWithTryCatch);
340                 // actionBar.setGlobalActionHandler(JdtActionConstants.OVERRIDE_METHODS,
341                 // fOverrideMethods);
342                 // actionBar.setGlobalActionHandler(JdtActionConstants.GENERATE_GETTER_SETTER,
343                 // fAddGetterSetter);
344                 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_CONSTRUCTOR_FROM_SUPERCLASS,
345                 // fAddUnimplementedConstructors);
346                 // actionBar.setGlobalActionHandler(JdtActionConstants.ADD_JAVA_DOC_COMMENT,
347                 // fAddJavaDocStub);
348                 // actionBar.setGlobalActionHandler(JdtActionConstants.EXTERNALIZE_STRINGS,
349                 // fExternalizeStrings);
350                 // actionBar.setGlobalActionHandler(JdtActionConstants.FIND_STRINGS_TO_EXTERNALIZE,
351                 // fFindStringsToExternalize);
352                 // actionBar.setGlobalActionHandler(JdtActionConstants.ORGANIZE_IMPORTS,
353                 // fOrganizeImports);
354                 if (!isEditorOwner()) {
355                         // editor provides its own implementation of these actions.
356                         actionBar.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(),
357                                         fAddBookmark);
358                         actionBar.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(),
359                                         fAddTaskAction);
360                 }
361         }
362
363 //      private int appendToGroup(IMenuManager menu, IAction action) {
364 //              if (action != null && action.isEnabled()) {
365 //                      menu.appendToGroup(fGroupName, action);
366 //                      return 1;
367 //              }
368 //              return 0;
369 //      }
370
371 //      private int addAction(IMenuManager menu, IAction action) {
372 //              if (action != null && action.isEnabled()) {
373 //                      menu.add(action);
374 //                      return 1;
375 //              }
376 //              return 0;
377 //      }
378
379         private int addEditorAction(IMenuManager menu, String actionID) {
380                 if (fEditor == null)
381                         return 0;
382                 IAction action = fEditor.getAction(actionID);
383                 if (action == null)
384                         return 0;
385                 if (action instanceof IUpdate)
386                         ((IUpdate) action).update();
387                 if (action.isEnabled()) {
388                         menu.add(action);
389                         return 1;
390                 }
391                 return 0;
392         }
393
394         private boolean isEditorOwner() {
395                 return fEditor != null;
396         }
397 }