c33d664ab10d97ad185eb44ce2ebb8d3d85fe778
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / CompilationUnitEditorActionContributor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  * 
7  * Contributors: IBM Corporation - initial API and implementation
8  ******************************************************************************/
9 package net.sourceforge.phpeclipse.phpeditor;
10
11 import net.sourceforge.phpdt.ui.actions.PHPdtActionConstants;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.ui.IActionBars;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IFileEditorInput;
22 import org.eclipse.ui.texteditor.AbstractTextEditor;
23 import org.eclipse.ui.texteditor.ITextEditor;
24
25 public class CompilationUnitEditorActionContributor
26                 extends
27                         BasicEditorActionContributor {
28         protected ShowExternalPreviewAction fShowExternalPreviewAction;
29         public CompilationUnitEditorActionContributor() {
30                 super();
31                 fShowExternalPreviewAction = ShowExternalPreviewAction.getInstance();
32         }
33
34         /*
35          * @see IEditorActionBarContributor#setActiveEditor(IEditorPart)
36          */
37         public void setActiveEditor(IEditorPart part) {
38                 super.setActiveEditor(part);
39
40                 ITextEditor textEditor = null;
41                 if (part instanceof ITextEditor)
42                         textEditor = (ITextEditor) part;
43
44                 // Source menu.
45                 IActionBars bars = getActionBars();
46                 bars.setGlobalActionHandler(PHPdtActionConstants.COMMENT, getAction(
47                                 textEditor, "Comment")); //$NON-NLS-1$
48                 bars.setGlobalActionHandler(PHPdtActionConstants.UNCOMMENT, getAction(
49                                 textEditor, "Uncomment")); //$NON-NLS-1$
50                 bars.setGlobalActionHandler(PHPdtActionConstants.TOGGLE_COMMENT,
51                                 getAction(textEditor, "ToggleComment")); //$NON-NLS-1$
52                 bars.setGlobalActionHandler(PHPdtActionConstants.FORMAT, getAction(
53                                 textEditor, "Format")); //$NON-NLS-1$
54                 bars.setGlobalActionHandler(PHPdtActionConstants.ADD_BLOCK_COMMENT,
55                                 getAction(textEditor, "AddBlockComment")); //$NON-NLS-1$
56                 bars.setGlobalActionHandler(PHPdtActionConstants.REMOVE_BLOCK_COMMENT,
57                                 getAction(textEditor, "RemoveBlockComment")); //$NON-NLS-1$
58                 bars.setGlobalActionHandler(PHPdtActionConstants.INDENT, getAction(
59                                 textEditor, "Indent")); //$NON-NLS-1$ //$NON-NLS-2$
60
61                 if (textEditor != null) {
62                         IFile file = null;
63                         IEditorInput editorInput = textEditor.getEditorInput();
64
65                         if (editorInput instanceof IFileEditorInput) {
66                                 file = ((IFileEditorInput) editorInput).getFile();
67                         }
68
69                         PHPeclipsePlugin.getDefault().setLastEditorFile(file);
70
71                         fShowExternalPreviewAction.setEditor(textEditor);
72                         fShowExternalPreviewAction.update();
73                         IPreferenceStore store = PHPeclipsePlugin.getDefault()
74                                         .getPreferenceStore();
75                         IAction a = ShowExternalPreviewAction.getInstance();
76                         if (a != null)
77                                 a.run();
78                 }
79         }
80 }