29edcb121a0d7d9701935d909f944c7a7be7772e
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiEditorContributor.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: IBM Corporation - initial API and implementation
7  **********************************************************************************************************************************/
8
9 package net.sourceforge.phpeclipse.wiki.editor;
10
11 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
12 import net.sourceforge.phpeclipse.wiki.preferences.Util;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.ui.IActionBars;
17 import org.eclipse.ui.IEditorInput;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IFileEditorInput;
20 import org.eclipse.ui.IViewPart;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
23 import org.eclipse.ui.texteditor.ITextEditor;
24 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
25 import org.eclipse.ui.texteditor.RetargetTextEditorAction;
26
27 public class WikiEditorContributor extends BasicTextEditorActionContributor {
28
29   private static final String CONTENTASSIST_ACTION = "net.sourceforge.phpeclipse.wiki.editor.ContentAssist";
30
31   private RetargetTextEditorAction fContentAssist;
32   public WikiEditorContributor() {
33     super();
34     fContentAssist = new RetargetTextEditorAction(WikiEditorPlugin.getDefault().getResourceBundle(), "ContentAssistProposal.");
35     fContentAssist.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
36   }
37
38   public void setActiveEditor(IEditorPart part) {
39     super.setActiveEditor(part);
40     ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null;
41     fContentAssist.setAction(getAction(editor, CONTENTASSIST_ACTION));
42     // jsurfer
43     setBrowserPreview(editor);
44   }
45
46   public void setBrowserPreview(ITextEditor editor) {
47     IWorkbenchPage page = WikiEditorPlugin.getDefault().getActivePage();
48     try {
49       IViewPart part = page.findView(BrowserView.ID_BROWSER);
50       if (part == null) {
51         part = page.showView(BrowserView.ID_BROWSER);
52       } else {
53         //                if (bringToTopPreview) {
54         //                  page.bringToTop(part);
55         //                }
56       }
57       IEditorInput editorInput = null;
58       if (editor != null) {
59         editorInput = editor.getEditorInput();
60       }
61       if (editorInput instanceof IFileEditorInput) {
62         IFile file = ((IFileEditorInput) editorInput).getFile();
63         String srcBasePath = Util.getWikiTextsPath(file);
64         String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH);
65         String htmlName = Util.getHTMLFileName(file, binBasePath, srcBasePath);
66         if (htmlName!=null) {
67           ((BrowserView) part).setUrl(htmlName);
68         }
69       }
70     } catch (Exception e) {
71     }
72   }
73
74   public void contributeToMenu(IMenuManager menu) {
75         
76         super.contributeToMenu(menu);
77         
78 }
79   
80   public void init(IActionBars bars, IWorkbenchPage page) {
81     super.init(bars, page);
82     bars.setGlobalActionHandler(CONTENTASSIST_ACTION, fContentAssist);
83     
84   }
85         
86   public void dispose() {
87     setActiveEditor(null);
88     super.dispose();
89   }
90 }