/* * Copyright (c) 2003-2004 Christopher Lenz and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * Christopher Lenz - initial API and implementation * * $Id: CssTextTools.java,v 1.1 2004-09-02 18:11:51 jsurfer Exp $ */ package net.sourceforge.phpeclipse.css.ui.text; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sourceforge.phpeclipse.css.core.CssCore; import net.sourceforge.phpeclipse.css.core.profiles.IProfile; import net.sourceforge.phpeclipse.css.ui.internal.text.CssCodeScanner; import net.sourceforge.phpeclipse.css.ui.internal.text.CssColorManager; import net.sourceforge.phpeclipse.css.ui.internal.text.CssCommentScanner; import net.sourceforge.phpeclipse.css.ui.internal.text.CssPartitionScanner; import net.sourceforge.phpeclipse.css.ui.internal.text.CssStringScanner; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocumentExtension3; import org.eclipse.jface.text.IDocumentPartitioner; import org.eclipse.jface.text.rules.DefaultPartitioner; import org.eclipse.jface.text.rules.IPartitionTokenScanner; import org.eclipse.jface.text.rules.RuleBasedScanner; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; /** * Tools required to configure a CSS text viewer. * *
* The color manager and all scanners exist only one time, i.e. the same * instances are returned to all clients. Thus, clients share those tools. *
*/ public class CssTextTools { // Instance Variables ------------------------------------------------------ /** * The preference store to use. */ private IPreferenceStore store; /** * Listener for changes to the preference store. */ private IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { adaptToPreferenceChange(event); } }; /** * The color manager. */ private IColorManager colorManager; /** * The partition scanner. */ private CssPartitionScanner partitionScanner; /** * Map of the code scanners, keyed by profile ID. */ private Map codeScanners = new HashMap(); /** * The token scanner for syntax highlighting comments in CSS source. */ private CssCommentScanner commentScanner; /** * The token scanner for syntax highlighting string literals in CSS source. */ private CssStringScanner stringScanner; // Constructors ------------------------------------------------------------ /** * Creates a new CSS text tools collection. * * @param store the preference store to initialize the text tools. The text * tools instance installs a listener on the passed preference store * to adapt itself to changes in the preference store. */ public CssTextTools(IPreferenceStore store) { this(store, true); } /** * Creates a new CSS 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. * @param autoDisposeOnDisplayDispose iftrue
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.
*/
public CssTextTools(IPreferenceStore store,
boolean autoDisposeOnDisplayDispose) {
store.addPropertyChangeListener(propertyChangeListener);
this.store = store;
colorManager = new CssColorManager(autoDisposeOnDisplayDispose);
partitionScanner = new CssPartitionScanner();
commentScanner = new CssCommentScanner(store, colorManager);
stringScanner = new CssStringScanner(store, colorManager);
}
// Public Methods ----------------------------------------------------------
/**
* Returns whether the specified change to the preference store would effect
* the presentation of CSS text.
*
* @param event the preference store change event
* @return true
if the specified event affects the presentation
* of CSS text, false
otherwise
*/
public boolean affectsPresentation(PropertyChangeEvent event) {
for (Iterator i = codeScanners.keySet().iterator(); i.hasNext();) {
CssCodeScanner scanner = (CssCodeScanner)
codeScanners.get(i.next());
if (scanner.affectsPresentation(event)) {
return true;
}
}
if (commentScanner.affectsPresentation(event)
|| stringScanner.affectsPresentation(event)) {
return true;
}
return false;
}
/**
* Factory method for creating a Java-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[] types = new String[] {
CssPartitionScanner.CSS_COMMENT,
CssPartitionScanner.CSS_STRING
};
return new DefaultPartitioner(getPartitionScanner(), types);
}
/**
* Disposes all the individual tools of this tools collection.
*/
public void dispose() {
// dispose the scanners
codeScanners.clear();
commentScanner = null;
stringScanner = null;
partitionScanner = null;
// dispose the color manager
if (colorManager != null) {
colorManager.dispose();
colorManager = null;
}
// detach from the preference store
if (store != null) {
store.removePropertyChangeListener(propertyChangeListener);
propertyChangeListener = null;
store = null;
}
}
/**
* Returns the color manager which is used to manage
* any Java-specific colors needed for such things like syntax highlighting.
*
* @return the color manager to be used for Java text viewers
*/
public IColorManager getColorManager() {
return colorManager;
}
/**
* Returns a scanner which is configured to scan CSS source code.
*
* @param profile the profile for which to retrieve the code scanner
* @return a CSS source code scanner
*/
public RuleBasedScanner getCodeScanner(IProfile profile) {
if (profile == null) {
// use the default profile
profile = CssCore.getDefault().getProfileManager().getProfile(null);
}
String profileId = profile.getDescriptor().getId();
RuleBasedScanner codeScanner = (RuleBasedScanner)
codeScanners.get(profileId);
if (codeScanner == null) {
codeScanner = new CssCodeScanner(store, this.colorManager, profile);
codeScanners.put(profileId, codeScanner);
}
return codeScanner;
}
/**
* Returns a scanner which is configured to scan CSS comments.
*
* @return a CSS comment scanner
*/
public RuleBasedScanner getCommentScanner() {
return commentScanner;
}
/**
* Returns a scanner which is configured to scan CSS strings.
*
* @return a CSS string scanner
*/
public RuleBasedScanner getStringScanner() {
return stringScanner;
}
/**
* Returns a scanner which is configured to scan CSS-specific partitions,
* which are comments, strings and regular code.
*
* @return a CSS partition scanner
*/
public IPartitionTokenScanner getPartitionScanner() {
return partitionScanner;
}
/**
* Sets up the given document for the default partitioning.
*
* @param document the document to be set up
*/
public void setupDocument(IDocument document) {
setupDocument(document, IDocumentExtension3.DEFAULT_PARTITIONING);
}
/**
* Sets up the given document for the given partitioning.
*
* @param document the document to be set up
* @param partitioning the document partitioning
*/
public void setupDocument(IDocument document, String partitioning) {
IDocumentPartitioner partitioner = createDocumentPartitioner();
if (document instanceof IDocumentExtension3) {
IDocumentExtension3 extension = (IDocumentExtension3) document;
extension.setDocumentPartitioner(partitioning, partitioner);
} else {
document.setDocumentPartitioner(partitioner);
}
partitioner.connect(document);
}
// Protected Methods -------------------------------------------------------
/**
* Adapts the behavior of the contained components to the change according
* to the given event.
*
* @param event the event to which to adapt
*/
protected void adaptToPreferenceChange(PropertyChangeEvent event) {
for (Iterator i = codeScanners.keySet().iterator(); i.hasNext();) {
CssCodeScanner scanner = (CssCodeScanner)
codeScanners.get(i.next());
if (scanner.affectsPresentation(event)) {
scanner.adaptToPreferenceChange(event);
}
}
if (commentScanner.affectsPresentation(event)) {
commentScanner.adaptToPreferenceChange(event);
}
if (stringScanner.affectsPresentation(event)) {
stringScanner.adaptToPreferenceChange(event);
}
}
}