83396925056af752773aa04f8c0e5e9fa4cb9646
[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.wiki.editor.model.WikipediaSection;
11 import net.sourceforge.phpeclipse.wiki.editor.model.WikipediaText;
12 import net.sourceforge.phpeclipse.wiki.preferences.Util;
13
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.source.ISourceViewer;
17 import org.eclipse.jface.text.source.IVerticalRuler;
18 import org.eclipse.jface.text.source.projection.ProjectionSupport;
19 import org.eclipse.jface.text.source.projection.ProjectionViewer;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
22 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
23 import org.eclipse.ui.texteditor.TextOperationAction;
24 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
25
26 public class WikiEditor extends AbstractDecoratedTextEditor {
27
28   private WikiOutlinePage fOutlinePage;
29
30   private WikipediaSection fSection;
31
32   private ProjectionSupport fProjectionSupport;
33
34   private WikiOccurrencesUpdater fOccurrencesUpdater;
35
36   public WikiEditor() {
37     setSourceViewerConfiguration(new WikiSourceViewerConfiguration(this, getSharedColors()));
38   }
39
40   public WikipediaSection getSection() {
41     return fSection;
42   }
43
44   public void setSection(WikipediaSection section) {
45     fSection = section;
46     if (fOutlinePage != null)
47       fOutlinePage.setWiki(section);
48
49     if (fOccurrencesUpdater != null)
50       fOccurrencesUpdater.update(getSourceViewer());
51   }
52
53   /*
54    * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
55    */
56   public Object getAdapter(Class required) {
57     if (IContentOutlinePage.class.equals(required)) {
58       if (fOutlinePage == null)
59         fOutlinePage = new WikiOutlinePage(this);
60       return fOutlinePage;
61     }
62     if (fProjectionSupport != null) {
63       Object adapter = fProjectionSupport.getAdapter(getSourceViewer(), required);
64       if (adapter != null)
65         return adapter;
66     }
67     return super.getAdapter(required);
68   }
69
70   public void outlinePageClosed() {
71     fOutlinePage = null;
72   }
73
74   /*
75    * @see org.eclipse.ui.texteditor.AbstractTextEditor#createActions()
76    */
77   protected void createActions() {
78     super.createActions();
79
80     IAction action = new TextOperationAction(WikiEditorPlugin.getDefault().getResourceBundle(), "ContentAssistProposal.", this,
81         ISourceViewer.CONTENTASSIST_PROPOSALS);
82     action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
83     setAction("ContentAssist.", action);
84     markAsStateDependentAction("ContentAssist.", true);
85   }
86
87   /*
88    * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
89    */
90   public void createPartControl(Composite parent) {
91     super.createPartControl(parent);
92
93     ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
94     fProjectionSupport = new ProjectionSupport(projectionViewer, getAnnotationAccess(), getSharedColors());
95     fProjectionSupport.install();
96     projectionViewer.doOperation(ProjectionViewer.TOGGLE);
97
98     fOccurrencesUpdater = new WikiOccurrencesUpdater(this);
99   }
100
101   /*
102    * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#createSourceViewer(org.eclipse.swt.widgets.Composite,
103    *      org.eclipse.jface.text.source.IVerticalRuler, int)
104    */
105   protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
106     fAnnotationAccess = createAnnotationAccess();
107     fOverviewRuler = createOverviewRuler(getSharedColors());
108
109     ISourceViewer viewer = new ProjectionViewer(parent, ruler, fOverviewRuler, true, styles);
110     // ensure decoration support has been created and configured:
111     getSourceViewerDecorationSupport(viewer);
112
113     return viewer;
114   }
115
116   /*
117    * @see org.eclipse.ui.IWorkbenchPart#dispose()
118    */
119   public void dispose() {
120     fOccurrencesUpdater.dispose();
121     super.dispose();
122   }
123
124   public IDocument getDocument() {
125     IDocument doc = getDocumentProvider().getDocument(getEditorInput());
126     return doc;
127   }
128
129   /*
130    * (non-Javadoc)
131    * 
132    * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorSaved()
133    */
134   protected void editorSaved() {
135     super.editorSaved();
136     BrowserUtil.refreshBrowserPreview(this);
137   }
138   
139   
140 }