improved templates
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiSourceViewerConfiguration.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 org.eclipse.core.runtime.NullProgressMonitor;
11
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.graphics.RGB;
14
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.TextAttribute;
17 import org.eclipse.jface.text.contentassist.ContentAssistant;
18 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
19 import org.eclipse.jface.text.contentassist.IContentAssistant;
20 import org.eclipse.jface.text.presentation.IPresentationReconciler;
21 import org.eclipse.jface.text.presentation.PresentationReconciler;
22 import org.eclipse.jface.text.reconciler.IReconciler;
23 import org.eclipse.jface.text.reconciler.MonoReconciler;
24 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
25 import org.eclipse.jface.text.rules.IRule;
26 import org.eclipse.jface.text.rules.IToken;
27 import org.eclipse.jface.text.rules.ITokenScanner;
28 import org.eclipse.jface.text.rules.IWordDetector;
29 import org.eclipse.jface.text.rules.RuleBasedScanner;
30 import org.eclipse.jface.text.rules.SingleLineRule;
31 import org.eclipse.jface.text.rules.Token;
32 import org.eclipse.jface.text.rules.WordRule;
33 import org.eclipse.jface.text.source.IAnnotationHover;
34 import org.eclipse.jface.text.source.ISharedTextColors;
35 import org.eclipse.jface.text.source.ISourceViewer;
36 import org.eclipse.jface.text.source.SourceViewerConfiguration;
37
38 public class WikiSourceViewerConfiguration extends SourceViewerConfiguration {
39
40   /**
41    * A no-op implementation of <code>IAnnotationHover</code> that will trigger the text editor to set up annotation hover support.
42    */
43   private static final class NullHover implements IAnnotationHover {
44
45     /*
46      * @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer, int)
47      */
48     public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
49       return null;
50     }
51   }
52
53   /**
54    * Simple word detector that detects any sequence of non-whitespace as a word.
55    */
56   private static final class SimpleWordDetector implements IWordDetector {
57     /*
58      * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
59      */
60     public boolean isWordStart(char c) {
61       return isWordPart(c);
62     }
63
64     /*
65      * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
66      */
67     public boolean isWordPart(char c) {
68       return !Character.isWhitespace(c);
69     }
70   }
71
72   private static final class SimpleListDetector implements IWordDetector {
73     /*
74      * @see org.eclipse.jface.text.rules.IWordDetector#isWordStart(char)
75      */
76     public boolean isWordStart(char c) {
77       return c == '*' || c == '#';
78     }
79
80     /*
81      * @see org.eclipse.jface.text.rules.IWordDetector#isWordPart(char)
82      */
83     public boolean isWordPart(char c) {
84       return true;
85     }
86   }
87
88   private final ISharedTextColors fColors;
89
90   private WikiEditor fEditor;
91
92   public WikiSourceViewerConfiguration(WikiEditor editor, ISharedTextColors colors) {
93     fEditor = editor;
94     fColors = colors;
95   }
96
97   /*
98    * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
99    */
100   public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
101     PresentationReconciler reconciler = new PresentationReconciler();
102     DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getRecipeScanner());
103     reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
104     reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
105     return reconciler;
106   }
107
108   private ITokenScanner getRecipeScanner() {
109     RuleBasedScanner scanner = new RuleBasedScanner();
110
111     IRule[] rules = new IRule[14];
112     rules[0] = createHeader6Rule();
113     rules[1] = createHeader5Rule();
114     rules[2] = createHeader4Rule();
115     rules[3] = createHeader3Rule();
116     rules[4] = createHeader2Rule();
117     rules[5] = createHeader1Rule();
118     rules[6] = createHRRule();
119     rules[7] = createLinkRule();
120     rules[8] = createExternalHTTPRule();
121     rules[9] = createListRule();
122     rules[10] = createNumberedListRule();
123     rules[11] = createBoldItalicRule();
124     rules[12] = createBoldRule();
125     rules[13] = createItalicRule();
126
127     scanner.setRules(rules);
128     return scanner;
129   }
130
131   //    private IRule createSectionTitleRule() {
132   //            IToken titleToken= new Token(new TextAttribute(null, null, SWT.BOLD));
133   //            WordRule wordRule= new WordRule(new SimpleWordDetector());
134   //            wordRule.addWord(IngredientsSection.TITLE + ":", titleToken);
135   //            wordRule.addWord(PreparationSection.TITLE + ":", titleToken);
136   //            return wordRule;
137   //    }
138
139   private IRule createBoldRule() {
140     IToken boldToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 0)), null, SWT.BOLD));
141     SingleLineRule singleLineRule = new SingleLineRule("'''", "'''", boldToken);
142     return singleLineRule;
143   }
144
145   private IRule createItalicRule() {
146     IToken italicToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 0)), null, SWT.ITALIC));
147     SingleLineRule singleLineRule = new SingleLineRule("''", "''", italicToken);
148     return singleLineRule;
149   }
150
151   private IRule createBoldItalicRule() {
152     IToken boldToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 0)), null, SWT.BOLD | SWT.ITALIC));
153     SingleLineRule singleLineRule = new SingleLineRule("'''''", "'''''", boldToken);
154     return singleLineRule;
155   }
156
157   private IRule createHRRule() {
158     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
159     SingleLineRule singleLineRule = new SingleLineRule("----", "\n", quantityToken);
160     singleLineRule.setColumnConstraint(0);
161     return singleLineRule;
162   }
163
164   private IRule createHeader1Rule() {
165     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
166     SingleLineRule singleLineRule = new SingleLineRule("=", "=", quantityToken);
167     singleLineRule.setColumnConstraint(0);
168     return singleLineRule;
169   }
170
171   private IRule createHeader2Rule() {
172     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
173     SingleLineRule singleLineRule = new SingleLineRule("==", "==", quantityToken);
174     singleLineRule.setColumnConstraint(0);
175     return singleLineRule;
176   }
177
178   private IRule createHeader3Rule() {
179     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
180     SingleLineRule singleLineRule = new SingleLineRule("===", "===", quantityToken);
181     singleLineRule.setColumnConstraint(0);
182     return singleLineRule;
183   }
184
185   private IRule createHeader4Rule() {
186     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
187     SingleLineRule singleLineRule = new SingleLineRule("====", "====", quantityToken);
188     singleLineRule.setColumnConstraint(0);
189     return singleLineRule;
190   }
191
192   private IRule createHeader5Rule() {
193     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
194     SingleLineRule singleLineRule = new SingleLineRule("=====", "=====", quantityToken);
195     singleLineRule.setColumnConstraint(0);
196     return singleLineRule;
197   }
198
199   private IRule createHeader6Rule() {
200     IToken quantityToken = new Token(new TextAttribute(fColors.getColor(new RGB(0, 0, 140)), null, SWT.NONE));
201     SingleLineRule singleLineRule = new SingleLineRule("======", "======", quantityToken);
202     singleLineRule.setColumnConstraint(0);
203     return singleLineRule;
204   }
205
206   //    private IRule createLeadingDashRule() {
207   //            IToken dashToken= new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.BOLD));
208   //            WordRule wordRule= new WordRule(new SimpleWordDetector());
209   //            wordRule.addWord("-", dashToken);
210   //            wordRule.setColumnConstraint(0);
211   //            return wordRule;
212   //    }
213
214   private IRule createListRule() {
215     IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.NONE));
216     //          WordRule wordRule= new WordRule(new SimpleListDetector());
217     //          wordRule.addWord("*", dashToken);
218     SingleLineRule singleLineRule = new SingleLineRule("*", "\n", dashToken);
219     singleLineRule.setColumnConstraint(0);
220     return singleLineRule;
221   }
222
223   private IRule createNumberedListRule() {
224     IToken dashToken = new Token(new TextAttribute(fColors.getColor(new RGB(63, 127, 95)), null, SWT.NONE));
225     //          WordRule wordRule= new WordRule(new SimpleListDetector());
226     //          wordRule.addWord("#", dashToken);
227     SingleLineRule singleLineRule = new SingleLineRule("#", "\n", dashToken);
228     singleLineRule.setColumnConstraint(0);
229     return singleLineRule;
230   }
231
232   private IRule createLinkRule() {
233     IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.NONE));
234     SingleLineRule stepRule = new SingleLineRule("[[", "]]", stepToken);
235     //          stepRule.setColumnConstraint(0);
236     return stepRule;
237   }
238
239   private IRule createExternalHTTPRule() {
240     IToken stepToken = new Token(new TextAttribute(fColors.getColor(new RGB(200, 100, 100)), null, SWT.NONE));
241     SingleLineRule stepRule = new SingleLineRule("[http", "]", stepToken);
242     //          stepRule.setColumnConstraint(0);
243     return stepRule;
244   }
245
246   /*
247    * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
248    */
249   public IReconciler getReconciler(ISourceViewer sourceViewer) {
250     WikiReconcilingStrategy strategy = new WikiReconcilingStrategy(fEditor);
251     MonoReconciler reconciler = new MonoReconciler(strategy, false);
252     reconciler.setProgressMonitor(new NullProgressMonitor());
253     reconciler.setDelay(500);
254
255     return reconciler;
256   }
257
258   /*
259    * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(org.eclipse.jface.text.source.ISourceViewer)
260    */
261   public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
262     ContentAssistant assistant = new ContentAssistant();
263
264     IContentAssistProcessor processor = new WikiCompletionProcessor(fEditor);
265     assistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
266
267     assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
268     assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
269     assistant.enableAutoInsert(true);
270
271     return assistant;
272   }
273
274   /*
275    * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer)
276    */
277   public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
278     return new NullHover();
279   }
280 }