1 package net.sourceforge.phpdt.ui.text;
 
   4  * (c) Copyright IBM Corp. 2000, 2001.
 
   8 import net.sourceforge.phpdt.internal.ui.text.FastJavaPartitionScanner;
 
   9 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
 
  10 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCodeScanner;
 
  11 import net.sourceforge.phpeclipse.IPreferenceConstants;
 
  12 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCodeScanner;
 
  13 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
 
  15 import org.eclipse.jface.preference.IPreferenceStore;
 
  16 import org.eclipse.jface.text.IDocumentPartitioner;
 
  17 import org.eclipse.jface.text.rules.DefaultPartitioner;
 
  18 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
 
  19 import org.eclipse.jface.text.rules.RuleBasedScanner;
 
  20 import org.eclipse.jface.util.IPropertyChangeListener;
 
  21 import org.eclipse.jface.util.PropertyChangeEvent;
 
  23 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
 
  24 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
 
  25 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
 
  26 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
 
  27 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
 
  28 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
 
  32  * Tools required to configure a Java text viewer. 
 
  33  * The color manager and all scanner exist only one time, i.e.
 
  34  * the same instances are returned to all clients. Thus, clients
 
  37  * This class may be instantiated; it is not intended to be subclassed.
 
  40 public class JavaTextTools {
 
  42         private class PreferenceListener implements IPropertyChangeListener {
 
  43                 public void propertyChange(PropertyChangeEvent event) {
 
  44                         adaptToPreferenceChange(event);
 
  48         /** The color manager */
 
  49         private JavaColorManager fColorManager;
 
  50         /** The PHP source code scanner */
 
  51         private PHPCodeScanner fCodeScanner;
 
  52         /** The PHP multiline comment scanner */
 
  53         private SingleTokenPHPScanner fMultilineCommentScanner;
 
  54         /** The Java singleline comment scanner */
 
  55         private SingleTokenPHPScanner fSinglelineCommentScanner;
 
  56         /** The Java string scanner */
 
  57         private SingleTokenPHPScanner fStringScanner;
 
  58         /** The PHPDoc scanner */
 
  59         private PHPDocCodeScanner fJavaDocScanner;
 
  60   /** The HTML scanner */
 
  61   private HTMLCodeScanner fHTMLScanner;
 
  62         /** The Java partitions scanner */
 
  63         private FastJavaPartitionScanner fPartitionScanner;     
 
  65         /** The preference store */
 
  66         private IPreferenceStore fPreferenceStore;
 
  67         /** The preference change listener */
 
  68         private PreferenceListener fPreferenceListener= new PreferenceListener();
 
  72          * Creates a new Java text tools collection.
 
  74          * @param store the preference store to initialize the text tools. The text tool
 
  75          * instance installs a listener on the passed preference store to adapt itself to 
 
  76          * changes in the preference store. In general <code>PreferenceConstants.
 
  77          * getPreferenceStore()</code> shoould be used to initialize the text tools.
 
  79          * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
 
  82         public JavaTextTools(IPreferenceStore store) {
 
  83                 fPreferenceStore= store;
 
  84                 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
 
  86                 fColorManager= new JavaColorManager();
 
  87                 fCodeScanner= new PHPCodeScanner(fColorManager, store);
 
  88                 fMultilineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
 
  89                 fSinglelineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
 
  90                 fStringScanner= new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
 
  91                 fJavaDocScanner= new PHPDocCodeScanner(fColorManager, store);
 
  92     fHTMLScanner= new HTMLCodeScanner(fColorManager, store);
 
  93                 fPartitionScanner= new FastJavaPartitionScanner();
 
  97          * Disposes all the individual tools of this tools collection.
 
  99         public void dispose() {
 
 102                 fMultilineCommentScanner= null;
 
 103                 fSinglelineCommentScanner= null;
 
 104                 fStringScanner= null;
 
 105                 fJavaDocScanner= null;
 
 106                 fPartitionScanner= null;
 
 108                 if (fColorManager != null) {
 
 109                         fColorManager.dispose();
 
 113                 if (fPreferenceStore != null) {
 
 114                         fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
 
 115                         fPreferenceStore= null;
 
 116                         fPreferenceListener= null;
 
 121          * Returns the color manager which is used to manage
 
 122          * any Java-specific colors needed for such things like syntax highlighting.
 
 124          * @return the color manager to be used for Java text viewers
 
 126         public JavaColorManager getColorManager() {
 
 127                 return fColorManager;
 
 131          * Returns a scanner which is configured to scan Java source code.
 
 133          * @return a Java source code scanner
 
 135         public RuleBasedScanner getCodeScanner() {
 
 140          * Returns a scanner which is configured to scan Java multiline comments.
 
 142          * @return a Java multiline comment scanner
 
 146         public RuleBasedScanner getMultilineCommentScanner() {
 
 147                 return fMultilineCommentScanner;
 
 151    * Returns a scanner which is configured to scan HTML code.
 
 153    * @return a HTML scanner
 
 157   public RuleBasedScanner getHTMLScanner() {
 
 162          * Returns a scanner which is configured to scan Java singleline comments.
 
 164          * @return a Java singleline comment scanner
 
 168         public RuleBasedScanner getSinglelineCommentScanner() {
 
 169                 return fSinglelineCommentScanner;
 
 173          * Returns a scanner which is configured to scan Java strings.
 
 175          * @return a Java string scanner
 
 179         public RuleBasedScanner getStringScanner() {
 
 180                 return fStringScanner;
 
 184          * Returns a scanner which is configured to scan JavaDoc compliant comments.
 
 185          * Notes that the start sequence "/**" and the corresponding end sequence
 
 186          * are part of the JavaDoc comment.
 
 188          * @return a JavaDoc scanner
 
 190         public RuleBasedScanner getJavaDocScanner() {
 
 191                 return fJavaDocScanner;
 
 195          * Returns a scanner which is configured to scan 
 
 196          * Java-specific partitions, which are multi-line comments,
 
 197          * JavaDoc comments, and regular Java source code.
 
 199          * @return a Java partition scanner
 
 201         public IPartitionTokenScanner getPartitionScanner() {
 
 202                 return fPartitionScanner;
 
 206          * Factory method for creating a Java-specific document partitioner
 
 207          * using this object's partitions scanner. This method is a 
 
 208          * convenience method.
 
 210          * @return a newly created Java document partitioner
 
 212         public IDocumentPartitioner createDocumentPartitioner() {
 
 214                 String[] types= new String[] {
 
 215       FastJavaPartitionScanner.JAVA_DOC,
 
 216       FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
 
 217       FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
 
 218       FastJavaPartitionScanner.JAVA_STRING
 
 221                 return new DefaultPartitioner(getPartitionScanner(), types);
 
 225          * Returns the names of the document position categories used by the document
 
 226          * partitioners created by this object to manage their partition information.
 
 227          * If the partitioners don't use document position categories, the returned
 
 228          * result is <code>null</code>.
 
 230          * @return the partition managing position categories or <code>null</code> 
 
 233         public String[] getPartitionManagingPositionCategories() {
 
 234                 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
 
 238          * Determines whether the preference change encoded by the given event
 
 239          * changes the behavior of one its contained components.
 
 241          * @param event the event to be investigated
 
 242          * @return <code>true</code> if event causes a behavioral change
 
 246         public boolean affectsBehavior(PropertyChangeEvent event) {
 
 247                 return fCodeScanner.affectsBehavior(event) ||
 
 248                                         fMultilineCommentScanner.affectsBehavior(event) ||
 
 249                                         fSinglelineCommentScanner.affectsBehavior(event) ||
 
 250                                         fStringScanner.affectsBehavior(event) ||
 
 251                                         fJavaDocScanner.affectsBehavior(event);
 
 255          * Adapts the behavior of the contained components to the change
 
 256          * encoded in the given event.
 
 258          * @param event the event to which to adapt
 
 261         protected void adaptToPreferenceChange(PropertyChangeEvent event) {
 
 262                 if (fCodeScanner.affectsBehavior(event))
 
 263                         fCodeScanner.adaptToPreferenceChange(event);
 
 264                 if (fMultilineCommentScanner.affectsBehavior(event))
 
 265                         fMultilineCommentScanner.adaptToPreferenceChange(event);
 
 266                 if (fSinglelineCommentScanner.affectsBehavior(event))
 
 267                         fSinglelineCommentScanner.adaptToPreferenceChange(event);
 
 268                 if (fStringScanner.affectsBehavior(event))
 
 269                         fStringScanner.adaptToPreferenceChange(event);
 
 270                 if (fJavaDocScanner.affectsBehavior(event))
 
 271                         fJavaDocScanner.adaptToPreferenceChange(event);