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