initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiEditor.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 package net.sourceforge.phpeclipse.wiki.editor;
9
10 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
11 import net.sourceforge.phpeclipse.wiki.builder.CreatePageAction;
12 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaSection;
13 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
14 import net.sourceforge.phpeclipse.wiki.preferences.Util;
15
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.source.ISourceViewer;
19 import org.eclipse.jface.text.source.IVerticalRuler;
20 import org.eclipse.jface.text.source.projection.ProjectionSupport;
21 import org.eclipse.jface.text.source.projection.ProjectionViewer;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IFileEditorInput;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
28 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
29 import org.eclipse.ui.texteditor.TextOperationAction;
30 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
31
32 public class WikiEditor extends AbstractDecoratedTextEditor {
33
34   private WikiOutlinePage fOutlinePage;
35
36   private WikipediaSection fSection;
37
38   private ProjectionSupport fProjectionSupport;
39
40   private WikiOccurrencesUpdater fOccurrencesUpdater;
41
42   public WikiEditor() {
43     setSourceViewerConfiguration(new WikiSourceViewerConfiguration(this, getSharedColors()));
44   }
45
46   public WikipediaSection getSection() {
47     return fSection;
48   }
49
50   public void setSection(WikipediaSection section) {
51     fSection = section;
52     if (fOutlinePage != null)
53       fOutlinePage.setWiki(section);
54
55     if (fOccurrencesUpdater != null)
56       fOccurrencesUpdater.update(getSourceViewer());
57   }
58
59   /*
60    * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
61    */
62   public Object getAdapter(Class required) {
63     if (IContentOutlinePage.class.equals(required)) {
64       if (fOutlinePage == null)
65         fOutlinePage = new WikiOutlinePage(this);
66       return fOutlinePage;
67     }
68     if (fProjectionSupport != null) {
69       Object adapter = fProjectionSupport.getAdapter(getSourceViewer(), required);
70       if (adapter != null)
71         return adapter;
72     }
73     return super.getAdapter(required);
74   }
75
76   public void outlinePageClosed() {
77     fOutlinePage = null;
78   }
79
80   /*
81    * @see org.eclipse.ui.texteditor.AbstractTextEditor#createActions()
82    */
83   protected void createActions() {
84     super.createActions();
85
86     IAction action = new TextOperationAction(WikiEditorPlugin.getDefault().getResourceBundle(), "ContentAssistProposal.", this,
87         ISourceViewer.CONTENTASSIST_PROPOSALS);
88     action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
89     setAction("ContentAssist.", action);
90     markAsStateDependentAction("ContentAssist.", true);
91   }
92
93   /*
94    * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
95    */
96   public void createPartControl(Composite parent) {
97     super.createPartControl(parent);
98
99     ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
100     fProjectionSupport = new ProjectionSupport(projectionViewer, getAnnotationAccess(), getSharedColors());
101     fProjectionSupport.install();
102     projectionViewer.doOperation(ProjectionViewer.TOGGLE);
103
104     fOccurrencesUpdater = new WikiOccurrencesUpdater(this);
105   }
106
107   /*
108    * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createSourceViewer(org.eclipse.swt.widgets.Composite,
109    *      org.eclipse.jface.text.source.IVerticalRuler, int)
110    */
111   protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
112     fAnnotationAccess = createAnnotationAccess();
113     fOverviewRuler = createOverviewRuler(getSharedColors());
114
115     ISourceViewer viewer = new ProjectionViewer(parent, ruler, fOverviewRuler, true, styles);
116     // ensure decoration support has been created and configured:
117     getSourceViewerDecorationSupport(viewer);
118
119     return viewer;
120   }
121
122   /*
123    * @see org.eclipse.ui.IWorkbenchPart#dispose()
124    */
125   public void dispose() {
126     fOccurrencesUpdater.dispose();
127     super.dispose();
128   }
129
130   public IDocument getDocument() {
131     IDocument doc = getDocumentProvider().getDocument(getEditorInput());
132     return doc;
133   }
134
135   /*
136    * (non-Javadoc)
137    * 
138    * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
139    */
140   protected void editorSaved() {
141     super.editorSaved();
142     // doesn't work here, wikibuilder has to be finished with generating html page
143     IWorkbenchPage page = WikiEditorPlugin.getDefault().getActivePage();
144     try {
145       IViewPart part = page.findView(BrowserView.ID_BROWSER);
146       if (part == null) {
147         part = page.showView(BrowserView.ID_BROWSER);
148       } else {
149         //                if (bringToTopPreview) {
150         //                  page.bringToTop(part);
151         //                }
152       }
153       IEditorInput editorInput = null;
154       editorInput = this.getEditorInput();
155       if (editorInput instanceof IFileEditorInput) {
156         CreatePageAction.createPage(((IFileEditorInput) editorInput).getFile());
157         ((BrowserView) part).refresh();
158       }
159     } catch (Exception e) {
160     }
161   }
162   
163   
164 }