added requirement for JRE 1.4.x or higher
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPSourceViewerConfiguration.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.util.Vector;
15
16 import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference;
17 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
18 import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy;
19 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
21 import net.sourceforge.phpdt.ui.text.JavaTextTools;
22 import net.sourceforge.phpeclipse.PHPCore;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy;
25 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
26 import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
27 import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy;
28 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
29 import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector;
30 import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider;
31
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
34 import org.eclipse.jface.text.IAutoIndentStrategy;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.ITextDoubleClickStrategy;
37 import org.eclipse.jface.text.ITextHover;
38 import org.eclipse.jface.text.TextAttribute;
39 import org.eclipse.jface.text.contentassist.ContentAssistant;
40 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
41 import org.eclipse.jface.text.contentassist.IContentAssistant;
42 import org.eclipse.jface.text.formatter.ContentFormatter;
43 import org.eclipse.jface.text.formatter.IContentFormatter;
44 import org.eclipse.jface.text.formatter.IFormattingStrategy;
45 import org.eclipse.jface.text.presentation.IPresentationReconciler;
46 import org.eclipse.jface.text.presentation.PresentationReconciler;
47 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
48 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
49 import org.eclipse.jface.text.rules.DefaultPartitioner;
50 import org.eclipse.jface.text.rules.RuleBasedScanner;
51 import org.eclipse.jface.text.rules.Token;
52 import org.eclipse.jface.text.source.IAnnotationHover;
53 import org.eclipse.jface.text.source.ISourceViewer;
54 import org.eclipse.jface.text.source.SourceViewerConfiguration;
55
56 /**
57  * Configuration for an <code>SourceViewer</code> which shows PHP code. 
58  */
59 public class PHPSourceViewerConfiguration extends SourceViewerConfiguration
60 {
61
62     /** 
63      * Preference key used to look up display tab width.
64      * 
65      * @since 2.0
66      */
67     public final static String PREFERENCE_TAB_WIDTH =
68         PreferenceConstants.EDITOR_TAB_WIDTH;
69     /** 
70      * Preference key for inserting spaces rather than tabs.
71      * 
72      * @since 2.0
73      */
74     public final static String SPACES_FOR_TABS =
75         PreferenceConstants.EDITOR_SPACES_FOR_TABS;
76
77     //  public static final String HTML_DEFAULT = IPHPPartitionScannerConstants.HTML;
78     //IDocument.DEFAULT_CONTENT_TYPE;
79
80     private JavaTextTools fJavaTextTools;
81     private PHPEditor fEditor;
82
83     private ContentFormatter fFormatter;
84     private HTMLFormattingStrategy fFormattingStrategy;
85     /**
86      * Single token scanner.
87      */
88     static class SingleTokenScanner extends BufferedRuleBasedScanner
89     {
90         public SingleTokenScanner(TextAttribute attribute)
91         {
92             setDefaultReturnToken(new Token(attribute));
93         }
94     };
95
96     /**
97      * Default constructor.
98      */
99     public PHPSourceViewerConfiguration(
100         JavaTextTools textTools,
101         PHPEditor editor)
102     {
103         fJavaTextTools = textTools;
104         fEditor = editor;
105     }
106
107     /*
108      * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
109      */
110     public IContentFormatter getContentFormatter(ISourceViewer sourceViewer)
111     {
112         //    if (fFormatter == null) {
113         //      fFormatter = new ContentFormatter();
114         //      fFormattingStrategy = new HTMLFormattingStrategy(this, sourceViewer);
115         //      fFormatter.setFormattingStrategy(fFormattingStrategy, HTML_DEFAULT);
116         //      fFormatter.enablePartitionAwareFormatting(false);
117         //      fFormatter.setPartitionManagingPositionCategories(getConfiguredContentTypes(null));
118         //    }
119         //    return fFormatter;
120
121         if (fFormatter == null)
122         {
123             //ContentFormatter 
124             fFormatter = new ContentFormatter();
125             IFormattingStrategy strategy =
126                 new JavaFormattingStrategy(sourceViewer);
127
128             fFormatter.setFormattingStrategy(
129                 strategy,
130                 IDocument.DEFAULT_CONTENT_TYPE);
131             fFormatter.enablePartitionAwareFormatting(false);
132             fFormatter.setPartitionManagingPositionCategories(
133                 getPartitionManagingPositionCategories());
134         }
135         return fFormatter;
136     }
137
138     /**
139      * Returns the names of the document position categories used by the document
140      * partitioners created by this object to manage their partition information.
141      * If the partitioners don't use document position categories, the returned
142      * result is <code>null</code>.
143      *
144      * @return the partition managing position categories or <code>null</code> 
145      *                  if there is none
146      */
147     public String[] getPartitionManagingPositionCategories()
148     {
149         return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
150     }
151     //  /** 
152     //   * Returns the names of the document position categories used by the document
153     //   * partitioners created by this object to manage their partition information.
154     //   * If the partitioners don't use document position categories, the returned
155     //   * result is <code>null</code>.
156     //   *
157     //   * @return the partition managing position categories or <code>null</code> 
158     //   *                      if there is none
159     //   */
160     //  private String[] getPartitionManagingPositionCategories() {
161     //    return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
162     //  } 
163
164     public PHPEditor getEditor()
165     {
166         return fEditor;
167     }
168
169     /**
170      * Returns the preference store used by this configuration to initialize
171      * the individual bits and pieces.
172      * 
173      * @return the preference store used to initialize this configuration
174      * 
175      * @since 2.0
176      */
177     protected IPreferenceStore getPreferenceStore()
178     {
179         return PHPeclipsePlugin.getDefault().getPreferenceStore();
180     }
181
182     /* (non-Javadoc)
183      * Method declared on SourceViewerConfiguration
184      */
185     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer)
186     {
187         return new PHPAnnotationHover();
188     }
189
190     /* (non-Javadoc)
191      * Method declared on SourceViewerConfiguration
192      */
193     public IAutoIndentStrategy getAutoIndentStrategy(
194         ISourceViewer sourceViewer,
195         String contentType)
196     {
197         return (
198             IPHPPartitionScannerConstants.PHP.equals(contentType)
199                 ? new PHPAutoIndentStrategy()
200                 : new DefaultAutoIndentStrategy());
201     }
202
203     /**
204      * Returns the PHP source code scanner for this configuration.
205      *
206      * @return the PHP source code scanner
207      */
208     protected RuleBasedScanner getCodeScanner()
209     {
210         return fJavaTextTools.getCodeScanner();
211     }
212
213     /**
214      * Returns the HTML source code scanner for this configuration.
215      *
216      * @return the HTML source code scanner
217      */
218     protected RuleBasedScanner getHTMLScanner()
219     {
220         return fJavaTextTools.getHTMLScanner();
221     }
222
223     /**
224      * Returns the PHPDoc source code scanner for this configuration.
225      *
226      * @return the PHPDoc source code scanner
227      */
228     protected RuleBasedScanner getPHPDocScanner()
229     {
230         return fJavaTextTools.getJavaDocScanner();
231     }
232
233     /* (non-Javadoc)
234      * Method declared on SourceViewerConfiguration
235      */
236     public String[] getConfiguredContentTypes(ISourceViewer sourceViewer)
237     {
238         return new String[] {
239             IPHPPartitionScannerConstants.HTML,
240             IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
241             IPHPPartitionScannerConstants.PHP,
242             IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
243             IPHPPartitionScannerConstants.CSS,
244             IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT,
245             IPHPPartitionScannerConstants.JAVASCRIPT,
246             IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT,
247             IDocument.DEFAULT_CONTENT_TYPE };
248     }
249
250     /* (non-Javadoc) 
251      * Method declared on SourceViewerConfiguration
252      */
253     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer)
254     {
255
256         ContentAssistant assistant = new ContentAssistant();
257         IContentAssistProcessor processor = new HTMLCompletionProcessor();
258         assistant.setContentAssistProcessor(
259             processor,
260             IPHPPartitionScannerConstants.HTML);
261         assistant.setContentAssistProcessor(
262             processor,
263             IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
264         assistant.setContentAssistProcessor(
265             processor,
266             IDocument.DEFAULT_CONTENT_TYPE);
267         assistant.setContentAssistProcessor(
268             processor,
269             IPHPPartitionScannerConstants.CSS);
270         assistant.setContentAssistProcessor(
271             processor,
272             IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
273         assistant.setContentAssistProcessor(
274             processor,
275             IPHPPartitionScannerConstants.JAVASCRIPT);
276         assistant.setContentAssistProcessor(
277             processor,
278             IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
279
280         assistant.setContentAssistProcessor(
281             new PHPCompletionProcessor(),
282             IPHPPartitionScannerConstants.PHP);
283
284         assistant.setContentAssistProcessor(
285             new PHPDocCompletionProcessor(),
286             IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
287
288         //    assistant.enableAutoActivation(true);
289         //    assistant.setAutoActivationDelay(500);
290         //    assistant.setProposalPopupOrientation(ContentAssistant.PROPOSAL_OVERLAY);
291         //    ContentAssistPreference.configure(assistant, getPreferenceStore());
292         //    assistant.setContextInformationPopupOrientation(
293         //      ContentAssistant.CONTEXT_INFO_ABOVE);
294         //    assistant.setContextInformationPopupBackground(
295         //      PHPEditorEnvironment.getPHPColorProvider().getColor(
296         //        new RGB(150, 150, 0)));
297         ContentAssistPreference.configure(assistant, getPreferenceStore());
298
299         assistant.setContextInformationPopupOrientation(
300             ContentAssistant.CONTEXT_INFO_ABOVE);
301         assistant.setInformationControlCreator(
302             getInformationControlCreator(sourceViewer));
303
304         return assistant;
305     }
306
307     /* (non-Javadoc)
308      * Method declared on SourceViewerConfiguration
309      */
310     //  public String getDefaultPrefix(ISourceViewer sourceViewer, String contentType) {
311     //    return (PHPPartitionScanner.PHP.equals(contentType) ? "//" : null); //$NON-NLS-1$
312     //    // return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
313     //  }
314
315     /*
316      * @see SourceViewerConfiguration#getDefaultPrefix(ISourceViewer, String)
317      * @since 2.0
318      */
319     public String[] getDefaultPrefixes(
320         ISourceViewer sourceViewer,
321         String contentType)
322     {
323         return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
324     }
325
326     /* (non-Javadoc)
327      * Method declared on SourceViewerConfiguration
328      */
329     public ITextDoubleClickStrategy getDoubleClickStrategy(
330         ISourceViewer sourceViewer,
331         String contentType)
332     {
333         return new PHPDoubleClickSelector();
334     }
335
336     /*
337      * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
338      */
339     public String[] getIndentPrefixes(
340         ISourceViewer sourceViewer,
341         String contentType)
342     {
343
344         Vector vector = new Vector();
345
346         // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
347
348         final IPreferenceStore preferences =
349             PHPeclipsePlugin.getDefault().getPreferenceStore();
350         int tabWidth = preferences.getInt(PHPCore.FORMATTER_TAB_SIZE);
351         boolean useSpaces = getPreferenceStore().getBoolean(SPACES_FOR_TABS);
352
353         for (int i = 0; i <= tabWidth; i++)
354         {
355             StringBuffer prefix = new StringBuffer();
356
357             if (useSpaces)
358             {
359                 for (int j = 0; j + i < tabWidth; j++)
360                     prefix.append(' ');
361
362                 if (i != 0)
363                     prefix.append('\t');
364             }
365             else
366             {
367                 for (int j = 0; j < i; j++)
368                     prefix.append(' ');
369
370                 if (i != tabWidth)
371                     prefix.append('\t');
372             }
373
374             vector.add(prefix.toString());
375         }
376
377         vector.add(""); //$NON-NLS-1$
378
379         return (String[]) vector.toArray(new String[vector.size()]);
380     }
381     /* (non-Javadoc)
382      * Method declared on SourceViewerConfiguration
383      */
384     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer)
385     {
386         //  PHPColorProvider provider = PHPEditorEnvironment.getPHPColorProvider();
387         JavaColorManager provider = PHPEditorEnvironment.getPHPColorProvider();
388         PresentationReconciler reconciler = new PresentationReconciler();
389
390         DefaultDamagerRepairer dr =
391             new DefaultDamagerRepairer(getHTMLScanner());
392         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
393         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
394
395         dr = new DefaultDamagerRepairer(getHTMLScanner());
396         reconciler.setDamager(dr, IPHPPartitionScannerConstants.HTML);
397         reconciler.setRepairer(dr, IPHPPartitionScannerConstants.HTML);
398         dr = new DefaultDamagerRepairer(getHTMLScanner());
399         reconciler.setDamager(dr, IPHPPartitionScannerConstants.CSS);
400         reconciler.setRepairer(dr, IPHPPartitionScannerConstants.CSS);
401         dr = new DefaultDamagerRepairer(getHTMLScanner());
402         reconciler.setDamager(
403             dr,
404             IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
405         reconciler.setRepairer(
406             dr,
407             IPHPPartitionScannerConstants.CSS_MULTILINE_COMMENT);
408         dr = new DefaultDamagerRepairer(getHTMLScanner());
409         reconciler.setDamager(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
410         reconciler.setRepairer(dr, IPHPPartitionScannerConstants.JAVASCRIPT);
411         dr = new DefaultDamagerRepairer(getHTMLScanner());
412         reconciler.setDamager(
413             dr,
414             IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
415         reconciler.setRepairer(
416             dr,
417             IPHPPartitionScannerConstants.JS_MULTILINE_COMMENT);
418         dr = new DefaultDamagerRepairer(
419                 new SingleTokenScanner(
420                     new TextAttribute(
421                         fJavaTextTools.getColorManager().getColor(
422                             PHPColorProvider.MULTI_LINE_COMMENT))));
423         reconciler.setDamager(
424             dr,
425             IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
426         reconciler.setRepairer(
427             dr,
428             IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT);
429
430         dr = new DefaultDamagerRepairer(getCodeScanner());
431         reconciler.setDamager(dr, IPHPPartitionScannerConstants.PHP);
432         reconciler.setRepairer(dr, IPHPPartitionScannerConstants.PHP);
433
434         dr = new DefaultDamagerRepairer(getPHPDocScanner());
435         reconciler.setDamager(
436             dr,
437             IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
438         reconciler.setRepairer(
439             dr,
440             IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT);
441
442         return reconciler;
443     }
444
445     /* (non-Javadoc)
446      * Method declared on SourceViewerConfiguration
447      */
448     public int getTabWidth(ISourceViewer sourceViewer)
449     {
450         return getPreferenceStore().getInt(PREFERENCE_TAB_WIDTH);
451     }
452
453     /* (non-Javadoc)
454      * Method declared on SourceViewerConfiguration
455      */
456     public ITextHover getTextHover(
457         ISourceViewer sourceViewer,
458         String contentType)
459     {
460         return new PHPTextHover();
461     }
462 }