misc parser changes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / text / JavaTextTools.java
index 6d4f394..4bad7d2 100644 (file)
@@ -10,10 +10,13 @@ import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCodeScanner;
 import net.sourceforge.phpeclipse.IPreferenceConstants;
 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCodeScanner;
+import net.sourceforge.phpeclipse.phpeditor.php.IPHPPartitionScannerConstants;
 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
+import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
 
+import org.eclipse.core.runtime.Preferences;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.text.IDocumentPartitioner;
 import org.eclipse.jface.text.rules.DefaultPartitioner;
@@ -40,10 +43,35 @@ import org.eclipse.jface.util.PropertyChangeEvent;
  */
 public class JavaTextTools {
 
-  private class PreferenceListener implements IPropertyChangeListener {
+  private static PHPPartitionScanner HTML_PARTITION_SCANNER = null;
+
+  private static PHPPartitionScanner PHP_PARTITION_SCANNER = null;
+  private static PHPPartitionScanner SMARTY_PARTITION_SCANNER = null;
+
+  // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC, PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
+  private final static String[] TYPES =
+    new String[] {
+      IPHPPartitionScannerConstants.PHP,
+      IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
+      IPHPPartitionScannerConstants.HTML,
+      IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
+      IPHPPartitionScannerConstants.JAVASCRIPT,
+      IPHPPartitionScannerConstants.CSS,
+      IPHPPartitionScannerConstants.SMARTY,
+      IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT };
+  private static PHPPartitionScanner XML_PARTITION_SCANNER = null;
+
+  /**
+   * This tools' preference listener. 
+   */
+  private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
     public void propertyChange(PropertyChangeEvent event) {
       adaptToPreferenceChange(event);
     }
+    public void propertyChange(Preferences.PropertyChangeEvent event) {
+      adaptToPreferenceChange(
+        new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue()));
+    }
   };
 
   /** The color manager */
@@ -69,6 +97,11 @@ public class JavaTextTools {
 
   /** The preference store */
   private IPreferenceStore fPreferenceStore;
+  /**
+   * The core preference store.
+   * @since 2.1
+   */
+  private Preferences fCorePreferenceStore;
   /** The preference change listener */
   private PreferenceListener fPreferenceListener = new PreferenceListener();
 
@@ -100,6 +133,42 @@ public class JavaTextTools {
   }
 
   /**
+   * Creates a new Java text tools collection.
+   * 
+   * @param store the preference store to initialize the text tools. The text tool
+   *                   instance installs a listener on the passed preference store to adapt itself to 
+   *                   changes in the preference store. In general <code>PreferenceConstants.
+   *                   getPreferenceStore()</code> shoould be used to initialize the text tools.
+   * @param coreStore optional preference store to initialize the text tools. The text tool
+   *                   instance installs a listener on the passed preference store to adapt itself to 
+   *                   changes in the preference store.
+   * @param autoDisposeOnDisplayDispose        if <code>true</code>  the color manager
+   *                   automatically disposes all managed colors when the current display gets disposed
+   *                   and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
+   * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
+   * @since 2.1
+   */
+  public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
+    fPreferenceStore = store;
+    fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
+
+    fCorePreferenceStore = coreStore;
+    if (fCorePreferenceStore != null)
+      fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
+
+    fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
+    fCodeScanner = new PHPCodeScanner(fColorManager, store);
+    fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
+    fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
+    fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
+    fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
+    fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
+    fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
+    fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
+    fPartitionScanner = new FastJavaPartitionScanner();
+  }
+
+  /**
    * Disposes all the individual tools of this tools collection.
    */
   public void dispose() {
@@ -119,6 +188,12 @@ public class JavaTextTools {
     if (fPreferenceStore != null) {
       fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
       fPreferenceStore = null;
+
+      if (fCorePreferenceStore != null) {
+        fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
+        fCorePreferenceStore = null;
+      }
+
       fPreferenceListener = null;
     }
   }
@@ -230,22 +305,60 @@ public class JavaTextTools {
   }
 
   /**
-   * Factory method for creating a Java-specific document partitioner
+   * Factory method for creating a PHP-specific document partitioner
    * using this object's partitions scanner. This method is a 
    * convenience method.
    *
    * @return a newly created Java document partitioner
    */
   public IDocumentPartitioner createDocumentPartitioner() {
+    return createDocumentPartitioner(".php");
+  }
 
-    String[] types =
-      new String[] {
-        FastJavaPartitionScanner.JAVA_DOC,
-        FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
-        FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
-        FastJavaPartitionScanner.JAVA_STRING };
+  /**
+   * Factory method for creating a PHP-specific document partitioner
+   * using this object's partitions scanner. This method is a 
+   * convenience method.
+   *
+   * @return a newly created Java document partitioner
+   */
+  public IDocumentPartitioner createDocumentPartitioner(String extension) {
 
-    return new DefaultPartitioner(getPartitionScanner(), types);
+    //    String[] types =
+    //      new String[] {
+    //        FastJavaPartitionScanner.JAVA_DOC,
+    //        FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
+    //        FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
+    //        FastJavaPartitionScanner.JAVA_STRING };
+    //
+    //    return new DefaultPartitioner(getPartitionScanner(), types);
+    IDocumentPartitioner partitioner = null;
+    // System.out.println(extension);
+    if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
+      // html
+      partitioner = createHTMLPartitioner();
+    } else if (extension.equalsIgnoreCase(".xml")) {
+      // xml
+      partitioner = createXMLPartitioner();
+    } else if (extension.equalsIgnoreCase(".js")) {
+      // javascript
+      partitioner = createJavaScriptPartitioner();
+    } else if (extension.equalsIgnoreCase(".css")) {
+      // cascading style sheets
+      partitioner = createCSSPartitioner();
+    } else if (extension.equalsIgnoreCase(".tpl")) {
+      // smarty ?
+      partitioner = createSmartyPartitioner();
+    } else if (extension.equalsIgnoreCase(".inc")) {
+      // php include files ?
+      partitioner = createIncludePartitioner();
+    }
+
+    if (partitioner == null) {
+      partitioner = createPHPPartitioner();
+    }
+
+    return partitioner;
   }
 
   /**
@@ -297,4 +410,73 @@ public class JavaTextTools {
     if (fPHPDocScanner.affectsBehavior(event))
       fPHPDocScanner.adaptToPreferenceChange(event);
   }
+
+  /**
+        * Return a partitioner for .html files.
+        */
+  private static IDocumentPartitioner createHTMLPartitioner() {
+    return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
+  }
+
+  private static IDocumentPartitioner createIncludePartitioner() {
+    return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
+  }
+
+  private static IDocumentPartitioner createJavaScriptPartitioner() {
+    return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
+  }
+
+  /**
+       * Return a partitioner for .php files.
+       */
+  private static IDocumentPartitioner createPHPPartitioner() {
+    return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
+  }
+
+  private static IDocumentPartitioner createSmartyPartitioner() {
+    return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
+  }
+
+  private static IDocumentPartitioner createXMLPartitioner() {
+    return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
+  }
+
+  private static IDocumentPartitioner createCSSPartitioner() {
+    return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
+  }
+  /**
+       * Return a scanner for creating html partitions.
+       */
+  private static PHPPartitionScanner getHTMLPartitionScanner() {
+    if (HTML_PARTITION_SCANNER == null)
+      HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.HTML_FILE);
+    return HTML_PARTITION_SCANNER;
+  }
+  /**
+       * Return a scanner for creating php partitions.
+       */
+  private static PHPPartitionScanner getPHPPartitionScanner() {
+    if (PHP_PARTITION_SCANNER == null)
+      PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.PHP_FILE);
+    return PHP_PARTITION_SCANNER;
+  }
+
+  /**
+       * Return a scanner for creating smarty partitions.
+       */
+  private static PHPPartitionScanner getSmartyPartitionScanner() {
+    if (SMARTY_PARTITION_SCANNER == null)
+      SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.SMARTY_FILE);
+    return SMARTY_PARTITION_SCANNER;
+  }
+
+  /**
+       * Return a scanner for creating xml partitions.
+       */
+  private static PHPPartitionScanner getXMLPartitionScanner() {
+    if (XML_PARTITION_SCANNER == null)
+      XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.XML_FILE);
+    return XML_PARTITION_SCANNER;
+  }
+
 }
\ No newline at end of file