Fixed "textEditor==null" bug
[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.core.resources.IFile;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.Separator;
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.ui.IActionBars;
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IWorkbenchActionConstants;
36 import org.eclipse.ui.actions.ActionGroup;
37 import org.eclipse.ui.actions.RetargetAction;
38 import org.eclipse.ui.texteditor.AbstractTextEditor;
39 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
40 import org.eclipse.ui.texteditor.ITextEditor;
41 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
42
43 /**
44  * Contributes interesting PHP actions to the desktop's Edit menu and the toolbar.
45  */
46 public class PHPActionContributor extends BasicTextEditorActionContributor {
47
48   // protected RetargetTextEditorAction fContentAssistTip;
49   // protected TextEditorAction fTogglePresentation;
50   protected RetargetAction fRetargetContentAssist;
51
52   protected RetargetTextEditorAction fContentAssist;
53   private RetargetTextEditorAction fGotoMatchingBracket;
54   private List fRetargetToolbarActions = new ArrayList();
55
56   protected PHPParserAction fParserAction;
57   protected ShowExternalPreviewAction fShowExternalPreviewAction;
58
59   /**
60    * Default constructor.
61    */
62   public PHPActionContributor() {
63     super();
64
65     ResourceBundle b = PHPEditorMessages.getResourceBundle();
66
67     fRetargetContentAssist = new RetargetAction(PHPdtActionConstants.CONTENT_ASSIST, PHPEditorMessages.getString("ContentAssistProposal.label")); //$NON-NLS-1$
68     fRetargetContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
69
70     fContentAssist = new RetargetTextEditorAction(b, "ContentAssistProposal."); //$NON-NLS-1$
71     fContentAssist.setActionDefinitionId(PHPEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
72
73     fGotoMatchingBracket = new RetargetTextEditorAction(b, "GotoMatchingBracket."); //$NON-NLS-1$
74     fGotoMatchingBracket.setActionDefinitionId(PHPEditorActionDefinitionIds.GOTO_MATCHING_BRACKET);
75
76     //  fContentAssist.setImageDescriptor(JavaPluginImages.DESC_CLCL_CODE_ASSIST);
77     //  fContentAssist.setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CODE_ASSIST);
78
79     // fContentAssist = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistProposal."); //$NON-NLS-1$
80     //  fContentAssistTip = new RetargetTextEditorAction(PHPEditorMessages.getResourceBundle(), "ContentAssistTip."); //$NON-NLS-1$
81     //  fTogglePresentation = new PresentationAction();
82
83     fParserAction = PHPParserAction.getInstance();
84
85     if (SWT.getPlatform().equals("win32")) {
86       fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
87     }
88   }
89
90   /*
91    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
92    */
93   //  public void contributeToMenu(IMenuManager menu) {         
94   //    super.contributeToMenu(menu);
95   //    IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
96   //    if (editMenu != null) {
97   //      MenuManager structureSelection= new MenuManager(JavaEditorMessages.getString("ExpandSelectionMenu.label")); //$NON-NLS-1$
98   //      structureSelection.add(fStructureSelectEnclosingAction);
99   //      structureSelection.add(fStructureSelectNextAction);
100   //      structureSelection.add(fStructureSelectPreviousAction);
101   //      structureSelection.add(fStructureSelectHistoryAction);
102   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, structureSelection);
103   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoPreviousMemberAction);
104   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoNextMemberAction);
105   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoMatchingBracket);
106
107   //      editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fShowOutline);
108   //    }
109   //  }
110   /*
111    * @see EditorActionBarContributor#contributeToMenu(IMenuManager)
112    */
113   //  public void contributeToMenu(IMenuManager menu) {
114   //            
115   //      super.contributeToMenu(menu);
116   //            
117   //      IMenuManager editMenu= menu.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
118   //      if (editMenu != null) {
119   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_OPEN));
120   //              editMenu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
121   //                    
122   //              editMenu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, fRetargetContentAssist);
123   //      }             
124   //  }
125
126   /*
127    * @see EditorActionBarContributor#contributeToToolBar(IToolBarManager)
128    */
129   public void contributeToToolBar(IToolBarManager tbm) {
130     tbm.add(new Separator());
131     Iterator e = fRetargetToolbarActions.iterator();
132     while (e.hasNext())
133       tbm.add((IAction) e.next());
134   }
135
136   /*
137    * @see IEditorActionBarContributor#init(IActionBars)
138    */
139   public void init(IActionBars bars) {
140     super.init(bars);
141
142     IMenuManager menuManager = bars.getMenuManager();
143     IMenuManager editMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_EDIT);
144     if (editMenu != null) {
145       editMenu.add(new Separator());
146       editMenu.add(fContentAssist);
147       editMenu.add(fGotoMatchingBracket);
148
149       //   editMenu.add(fContentAssistTip);
150     }
151
152     bars.setGlobalActionHandler(PHPdtActionConstants.CONTENT_ASSIST, fContentAssist);
153     //    IToolBarManager toolBarManager = bars.getToolBarManager();
154     //    if (toolBarManager != null) {
155     //      toolBarManager.add(new Separator());
156     //      toolBarManager.add(fTogglePresentation);
157     //    }
158   }
159
160   private void doSetActiveEditor(IEditorPart part) {
161     super.setActiveEditor(part);
162
163     ITextEditor textEditor = null;
164     if (part instanceof ITextEditor)
165       textEditor = (ITextEditor) part;
166
167     fContentAssist.setAction(getAction(textEditor, "ContentAssistProposal")); //$NON-NLS-1$
168     //  fContentAssistTip.setAction(getAction(editor, "ContentAssistTip")); //$NON-NLS-1$
169     fGotoMatchingBracket.setAction(getAction(textEditor, GotoMatchingBracketAction.GOTO_MATCHING_BRACKET));
170
171     IActionBars bars = getActionBars();
172     bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(textEditor, "Comment"));
173     bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(textEditor, "Uncomment"));
174     bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(textEditor, "Format"));
175
176     if (part instanceof PHPEditor) {
177       PHPEditor cuEditor = (PHPEditor) part;
178       ActionGroup group = cuEditor.getActionGroup();
179       if (group != null)
180         group.fillActionBars(bars);
181     }
182     //    fTogglePresentation.setEditor(editor);
183     //    fTogglePresentation.update();
184
185     if (textEditor != null) {
186
187       IFile file = null;
188       IEditorInput editorInput = textEditor.getEditorInput();
189
190       if (editorInput instanceof IFileEditorInput) {
191         file = ((IFileEditorInput) editorInput).getFile();
192       }
193
194       PHPeclipsePlugin.getDefault().setLastEditorFile(file);
195       fParserAction.setEditor(textEditor);
196       fParserAction.update();
197       if (SWT.getPlatform().equals("win32") && textEditor instanceof AbstractTextEditor) {
198         fShowExternalPreviewAction.setEditor(textEditor);
199         fShowExternalPreviewAction.update();
200         IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
201         if (store.getBoolean(PHPeclipsePlugin.SHOW_EXTERNAL_PREVIEW_PREF)) {
202           IAction a = ShowExternalPreviewAction.getInstance();
203           if (a != null)
204             a.run();
205         }
206       }
207     }
208     
209   }
210
211   /*
212    * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
213    */
214   public void setActiveEditor(IEditorPart part) {
215     doSetActiveEditor(part);
216   }
217
218   /*
219    * @see IEditorActionBarContributor#dispose()
220    */
221   public void dispose() {
222     doSetActiveEditor(null);
223     super.dispose();
224   }
225 }