9cbd2c980a5d9484c845c2d57602841aa9eb5964
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPActionContributor.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
9
10 Contributors:
11     IBM Corporation - Initial implementation
12     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.ResourceBundle;
19
20 import net.sourceforge.phpdt.ui.actions.GotoMatchingBracketAction;
21 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.jface.action.Separator;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.ui.IActionBars;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IWorkbenchActionConstants;
33 import org.eclipse.ui.actions.ActionGroup;
34 import org.eclipse.ui.actions.RetargetAction;
35 import org.eclipse.ui.texteditor.AbstractTextEditor;
36 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
37 import org.eclipse.ui.texteditor.ITextEditor;
38 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
39
40 /**
41  * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
42  */
43 public class PHPActionContributor extends BasicTextEditorActionContributor {
44
45   // protected RetargetTextEditorAction fContentAssistTip;
46   // protected TextEditorAction fTogglePresentation;
47   protected RetargetAction fRetargetContentAssist;
48
49   protected RetargetTextEditorAction fContentAssist;
50   private RetargetTextEditorAction fGotoMatchingBracket;
51   private List fRetargetToolbarActions = new ArrayList();
52
53   protected PHPParserAction parserAction;
54   protected ShowExternalPreviewAction showExternalPreviewAction;
55
56   /**
57    * Default constructor.
58    */
59   public PHPActionContributor() {
60     super();
61
62     ResourceBundle b = PHPEditorMessages.getResourceBundle();
63
64     fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
65     fRetargetContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
66
67     fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
68     fContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
69
70     fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
71     fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
72
73     //  fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
74     //  fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
75
76     // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
77     //  fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
78     //  fTogglePresentation = new PresentationAction();
79
80     parserAction = PHPParserAction.getInstance();
81     if (SWT.getPlatform().equals("win32")) {
82       showExternalPreviewAction = ShowExternalPreviewAction.getInstance();
83     }
84   }
85
86   /*
87    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
88    */
89   //  public void contributeToMenu(IMenuManager menu) {         
90   //    super.contributeToMenu(menu);
91   //    IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
92   //    if (editMenu != null) {
93   //      MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
94   //      structureSelection.add(fStructureSelectEnclosingAction);
95   //      structureSelection.add(fStructureSelectNextAction);
96   //      structureSelection.add(fStructureSelectPreviousAction);
97   //      structureSelection.add(fStructureSelectHistoryAction);
98   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
99   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
100   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
101   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
102
103   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
104   //    }
105   //  }
106   /*
107    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
108    */
109   //  public void contributeToMenu(IMenuManager menu) {
110   //            
111   //      super.contributeToMenu(menu);
112   //            
113   //      IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
114   //      if (editMenu != null) {
115   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
116   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
117   //                    
118   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
119   //      }             
120   //  }
121
122   /*
123    * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
124    */
125   public void contributeToToolBar(IToolBarManager tbm) {
126     tbm.add(new Separator());
127     Iterator e = fRetargetToolbarActions.iterator();
128     while (e.hasNext())
129       tbm.add((IAction) e.next());
130   }
131
132   /*
133    * @see IEditorActionBarContributor#init(IActionBars)
134    */
135   public void init(IActionBars bars) {
136     super.init(bars);
137
138     IMenuManager menuManager = bars.getMenuManager();
139     IMenuManager editMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
140     if (editMenu != null) {
141       editMenu.add(new Separator());
142       editMenu.add(fContentAssist);
143       editMenu.add(fGotoMatchingBracket);
144
145       //   editMenu.add(fContentAssistTip);
146     }
147
148     bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
149     //    IToolBarManager toolBarManager = bars.getToolBarManager();
150     //    if (toolBarManager != null) {
151     //      toolBarManager.add(new Separator());
152     //      toolBarManager.add(fTogglePresentation);
153     //    }
154   }
155
156   private void doSetActiveEditor(IEditorPart part) {
157     super.setActiveEditor(part);
158
159     ITextEditor textEditor = null;
160     if (part instanceof ITextEditor)
161       textEditor = (ITextEditor) part;
162
163     fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
164     //  fContentAssistTip.setAction(getAction(editor, "ContentAssistTip")); //$NON-NLS-1$
165     fGotoMatchingBracket.setAction(getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
166
167     IActionBars bars = getActionBars();
168     bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
169     bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
170     bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
171
172     if (part instanceof PHPEditor) {
173       PHPEditor cuEditor = (PHPEditor) part;
174       ActionGroup group = cuEditor.getActionGroup();
175       if (group != null)
176         group.fillActionBars(bars);
177     }
178     //    fTogglePresentation.setEditor(editor);
179     //    fTogglePresentation.update();
180
181     parserAction.setEditor(textEditor);
182     parserAction.update();
183     if (SWT.getPlatform().equals("win32") &&
184         textEditor instanceof AbstractTextEditor) {
185       showExternalPreviewAction.setEditor(textEditor);
186       showExternalPreviewAction.update();
187       IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
188       if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
189         IAction a = ShowExternalPreviewAction.getInstance();
190         if (a != null)
191           a.run();
192       }
193     }
194   }
195
196   /*
197    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
198    */
199   public void setActiveEditor(IEditorPart part) {
200     doSetActiveEditor(part);
201   }
202
203   /*
204    * @see IEditorActionBarContributor#dispose()
205    */
206   public void dispose() {
207     doSetActiveEditor(null);
208     super.dispose();
209   }
210 }