misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPActionContributor.java
1 package net.sourceforge.phpeclipse.phpeditor;
2 /*******************************************************************************
3  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
4  * program and the accompanying materials are made available under the terms of
5  * the Common Public License v1.0 which accompanies this distribution, and is
6  * available at http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors: IBM Corporation - Initial implementation Klaus Hartlage -
9  * www.eclipseproject.de
10  ******************************************************************************/
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.ResourceBundle;
15
16 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
17 import net.sourceforge.phpdt.ui.IContextMenuConstants;
18 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.IStatusLineManager;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.action.Separator;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IFileEditorInput;
31 import org.eclipse.ui.IWorkbenchActionConstants;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.actions.RetargetAction;
34 import org.eclipse.ui.editors.text.EncodingActionGroup;
35 import org.eclipse.ui.ide.IDEActionFactory;
36 import org.eclipse.ui.texteditor.AbstractTextEditor;
37 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
38 import org.eclipse.ui.texteditor.ITextEditor;
39 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
40 /**
41  * Contributes interesting PHP actions to the desktop's Edit menu and the
42  * toolbar.
43  */
44 public class PHPActionContributor extends BasicJavaEditorActionContributor {
45 //  protected RetargetTextEditorAction fContentAssistTip;
46   // protected TextEditorAction fTogglePresentation;
47   protected RetargetAction fRetargetContentAssist;
48   protected RetargetTextEditorAction fContentAssist;
49   protected RetargetTextEditorAction fContextInformation;
50   private RetargetTextEditorAction fGotoMatchingBracket;
51   private List fRetargetToolbarActions = new ArrayList();
52 //  private List fPartListeners = new ArrayList();
53   protected PHPParserAction fParserAction;
54   protected ShowExternalPreviewAction fShowExternalPreviewAction;
55   private EncodingActionGroup fEncodingActionGroup;
56   /**
57    * Default constructor.
58    */
59   public PHPActionContributor() {
60     super();
61     ResourceBundle b = PHPEditorMessages.getResourceBundle();
62     
63 //    fRetargetContentAssist = new RetargetAction(
64 //        PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages
65 //            .getString("ContentAssistProposal.label")); //$NON-NLS-1$
66 //    fRetargetContentAssist
67 //        .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
68 //    markAsPartListener(fRetargetContentAssist);
69 //    
70 //    fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
71 //    fContentAssist
72 //        .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
73     
74     fRetargetContentAssist= new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
75         fRetargetContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
76         markAsPartListener(fRetargetContentAssist);
77         
78         fContentAssist= new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
79         fContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 
80         fContentAssist.setImageDescriptor(PHPUiImages.DESC_CLCL_CODE_ASSIST);
81         fContentAssist.setDisabledImageDescriptor(PHPUiImages.DESC_DLCL_CODE_ASSIST);
82         
83         fContextInformation= new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistContextInformation."); //$NON-NLS-1$
84         fContextInformation.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
85         
86 //      fCorrectionAssist= new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "CorrectionAssistProposal."); //$NON-NLS-1$
87 //      fCorrectionAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CORRECTION_ASSIST_PROPOSALS);
88         
89         // character encoding
90         fEncodingActionGroup= new EncodingActionGroup();
91
92     fParserAction = PHPParserAction.getInstance();
93     fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
94   }
95   
96   /*
97    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
98    */
99   //  public void contributeToMenu(IMenuManager menu) {
100   //    super.contributeToMenu(menu);
101   //    IMenuManager editMenu=
102   // menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
103   //    if (editMenu != null) {
104   //      MenuManager structureSelection= new
105   // MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label"));
106   // //$NON-NLS-1$
107   //      structureSelection.add(fStructureSelectEnclosingAction);
108   //      structureSelection.add(fStructureSelectNextAction);
109   //      structureSelection.add(fStructureSelectPreviousAction);
110   //      structureSelection.add(fStructureSelectHistoryAction);
111   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
112   // structureSelection);
113   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
114   // fGotoPreviousMemberAction);
115   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
116   // fGotoNextMemberAction);
117   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN,
118   // fGotoMatchingBracket);
119   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE,
120   // fShowOutline);
121   //    }
122   //  }
123   /*
124    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
125    */
126   //  public void contributeToMenu(IMenuManager menu) {
127   //            
128   //      super.contributeToMenu(menu);
129   //            
130   //      IMenuManager editMenu=
131   // menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
132   //      if (editMenu != null) {
133   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
134   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
135   //                    
136   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE,
137   // fRetargetContentAssist);
138   //      }
139   //  }
140   /*
141    * @see org.eclipse.ui.part.EditorActionBarContributor#contributeToMenu(org.eclipse.jface.action.IMenuManager)
142    */
143   public void contributeToMenu(IMenuManager menu) {
144     super.contributeToMenu(menu);
145         
146         IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
147         if (editMenu != null) {
148                 editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
149 //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fCorrectionAssist);                        
150                 editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fContextInformation);
151         }       
152   }
153 //  /*
154 //   * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
155 //   */
156 //  public void contributeToToolBar(IToolBarManager tbm) {
157 //    tbm.add(new Separator());
158 //    Iterator e = fRetargetToolbarActions.iterator();
159 //    while (e.hasNext())
160 //      tbm.add((IAction) e.next());
161 //  }
162   /*
163    * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
164    */
165 //  public void init(IActionBars bars, IWorkbenchPage page) {
166 //    Iterator e = fPartListeners.iterator();
167 //    while (e.hasNext())
168 //      page.addPartListener((RetargetAction) e.next());
169 //    // character encoding
170 //    fEncodingActionGroup.fillActionBars(bars);
171 //    super.init(bars, page);
172 //  }
173
174         /*
175          * @see IEditorActionBarContributor#init(IActionBars, IWorkbenchPage)
176          */
177         public void init(IActionBars bars, IWorkbenchPage page) {
178                 super.init(bars, page);
179                 
180                 // register actions that have a dynamic editor. 
181                 bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
182                 // character encoding
183                 fEncodingActionGroup.fillActionBars(bars);
184         }       
185   //  /*
186 //   * @see IEditorActionBarContributor#init(IActionBars)
187 //   */
188 //  public void init(IActionBars bars) {
189 //    super.init(bars);
190 //    IMenuManager menuManager = bars.getMenuManager();
191 //    IMenuManager editMenu = menuManager
192 //        .findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
193 //    if (editMenu != null) {
194 //      editMenu.add(new Separator());
195 //      editMenu.add(fContentAssist);
196 //      // editMenu.add(fGotoMatchingBracket);
197 //      //   editMenu.add(fContentAssistTip);
198 //    }
199 //    bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST,
200 //        fContentAssist);
201 //    //    IToolBarManager toolBarManager = bars.getToolBarManager();
202 //    //    if (toolBarManager != null) {
203 //    //      toolBarManager.add(new Separator());
204 //    //      toolBarManager.add(fTogglePresentation);
205 //    //    }
206 //  }
207   
208
209   /*
210    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
211    */
212   public void setActiveEditor(IEditorPart part) {
213     super.setActiveEditor(part);
214     IActionBars bars = getActionBars();
215     IStatusLineManager manager = bars.getStatusLineManager();
216     manager.setMessage(null);
217     manager.setErrorMessage(null);
218     
219     ITextEditor textEditor = null;    
220     if (part instanceof ITextEditor)
221       textEditor = (ITextEditor) part;
222         
223         fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
224         fContextInformation.setAction(getAction(textEditor, "ContentAssistContextInformation")); //$NON-NLS-1$
225 //      fCorrectionAssist.setAction(getAction(textEditor, "CorrectionAssistProposal")); //$NON-NLS-1$
226         
227         IActionBars actionBars= getActionBars();
228         actionBars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_RIGHT, getAction(textEditor, "ShiftRight")); //$NON-NLS-1$
229         actionBars.setGlobalActionHandler(PHPdtActionConstants.SHIFT_LEFT, getAction(textEditor, "ShiftLeft")); //$NON-NLS-1$
230
231         actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), getAction(textEditor, IDEActionFactory.ADD_TASK.getId())); //$NON-NLS-1$
232         actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), getAction(textEditor, IDEActionFactory.BOOKMARK.getId())); //$NON-NLS-1$
233         
234         // character encoding
235         fEncodingActionGroup.retarget(textEditor);
236     
237     if (textEditor != null) {
238       IFile file = null;
239       IEditorInput editorInput = textEditor.getEditorInput();
240       if (editorInput instanceof IFileEditorInput) {
241         file = ((IFileEditorInput) editorInput).getFile();
242       }
243       PHPeclipsePlugin.getDefault().setLastEditorFile(file);
244       fParserAction.setEditor(textEditor);
245       fParserAction.update();
246       if (textEditor instanceof AbstractTextEditor) {
247         fShowExternalPreviewAction.setEditor(textEditor);
248         fShowExternalPreviewAction.update();
249         IAction a = ShowExternalPreviewAction.getInstance();
250         if (a != null)
251           a.run();
252       }
253     }
254   }
255   
256 }