intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / CssDocumentProvider.java
diff --git a/archive/net.sourceforge.phpeclipse.css.ui/src/net/sourceforge/phpeclipse/css/ui/internal/CssDocumentProvider.java b/archive/net.sourceforge.phpeclipse.css.ui/src/net/sourceforge/phpeclipse/css/ui/internal/CssDocumentProvider.java
new file mode 100644 (file)
index 0000000..d43169f
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * 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: CssDocumentProvider.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
+ */
+
+package net.sourceforge.phpeclipse.css.ui.internal;
+
+import net.sourceforge.phpeclipse.css.core.internal.model.StyleSheet;
+import net.sourceforge.phpeclipse.css.core.model.IStyleSheet;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IDocumentListener;
+import org.eclipse.ui.editors.text.TextFileDocumentProvider;
+
+/**
+ * Document provider for CSS files.
+ * 
+ * TODO This class currently doubles as a model manager which will need to be 
+ *      moved into core at some point, and would make this class pretty much
+ *      useless
+ */
+public class CssDocumentProvider extends TextFileDocumentProvider {
+
+       // Inner Classes -----------------------------------------------------------
+
+       private class StyleSheetInfo extends FileInfo {
+               private IStyleSheet styleSheet;
+       }
+
+       // TestFileDocumentProvider Implementation ---------------------------------
+
+       /*
+        * @see TextFileDocumentProvider#createEmptyFileInfo()
+        */
+       protected FileInfo createEmptyFileInfo() {
+               return new StyleSheetInfo();
+       }
+
+       /*
+        * @see TextFileDocumentProvider#createFileInfo(Object)
+        */
+       protected FileInfo createFileInfo(Object element) throws CoreException {
+               FileInfo fileInfo = super.createFileInfo(element);
+               if (!(fileInfo instanceof StyleSheetInfo)) {
+                       return null;
+               }
+
+               IDocument document = fileInfo.fTextFileBuffer.getDocument();
+               IStyleSheet styleSheet = createStyleSheet(document);
+               if (styleSheet instanceof IDocumentListener) {
+                       document.addDocumentListener((IDocumentListener) styleSheet);
+               }
+               StyleSheetInfo styleSheetInfo = (StyleSheetInfo) fileInfo;
+               styleSheetInfo.styleSheet = styleSheet;
+
+               return styleSheetInfo;
+       }
+
+       /*
+        * @see TextFileDocumentProvider#disposeFileInfo(Object, TextFileDocumentProvider.FileInfo)
+        */
+       protected void disposeFileInfo(Object element, FileInfo info) {
+               if (info instanceof StyleSheetInfo) {
+                       IDocument document = getDocument(element);
+                       // TODO Check whether this is really necessary, as the document is
+                       //      always null here
+                       if (document != null) {
+                               IStyleSheet styleSheet = ((StyleSheetInfo) info).styleSheet;
+                               if (styleSheet instanceof IDocumentListener) {
+                                       document.removeDocumentListener((IDocumentListener)
+                                                       styleSheet);
+                               }
+                       }
+               }
+               super.disposeFileInfo(element, info);
+       }
+
+       // Public Methods ----------------------------------------------------------
+
+       /**
+        * Creates the parsed style sheet object corresponding to the specified 
+        * document.
+        * 
+        * @param document the document to parse
+        * @return the parsed style sheet
+        */
+       public IStyleSheet createStyleSheet(IDocument document) {
+               return new StyleSheet(document);
+       }
+
+       /**
+        * Returns the style sheet associated with the specified element.
+        * 
+        * @param element the element
+        * @return the style sheet associated with the element
+        */
+       public IStyleSheet getStyleSheet(Object element) {
+               FileInfo info = getFileInfo(element);
+               if (info instanceof StyleSheetInfo) {
+                       StyleSheetInfo styleSheetInfo = (StyleSheetInfo) info;
+                       return styleSheetInfo.styleSheet;
+               }
+               return null;
+       }
+
+}