2 * Copyright (c) 2003-2004 Christopher Lenz 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
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssDocumentProvider.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal;
16 import net.sourceforge.phpeclipse.css.core.internal.model.StyleSheet;
17 import net.sourceforge.phpeclipse.css.core.model.IStyleSheet;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IDocumentListener;
22 import org.eclipse.ui.editors.text.TextFileDocumentProvider;
25 * Document provider for CSS files.
27 * TODO This class currently doubles as a model manager which will need to be
28 * moved into core at some point, and would make this class pretty much
31 public class CssDocumentProvider extends TextFileDocumentProvider {
33 // Inner Classes -----------------------------------------------------------
35 private class StyleSheetInfo extends FileInfo {
36 private IStyleSheet styleSheet;
39 // TestFileDocumentProvider Implementation ---------------------------------
42 * @see TextFileDocumentProvider#createEmptyFileInfo()
44 protected FileInfo createEmptyFileInfo() {
45 return new StyleSheetInfo();
49 * @see TextFileDocumentProvider#createFileInfo(Object)
51 protected FileInfo createFileInfo(Object element) throws CoreException {
52 FileInfo fileInfo = super.createFileInfo(element);
53 if (!(fileInfo instanceof StyleSheetInfo)) {
57 IDocument document = fileInfo.fTextFileBuffer.getDocument();
58 IStyleSheet styleSheet = createStyleSheet(document);
59 if (styleSheet instanceof IDocumentListener) {
60 document.addDocumentListener((IDocumentListener) styleSheet);
62 StyleSheetInfo styleSheetInfo = (StyleSheetInfo) fileInfo;
63 styleSheetInfo.styleSheet = styleSheet;
65 return styleSheetInfo;
69 * @see TextFileDocumentProvider#disposeFileInfo(Object, TextFileDocumentProvider.FileInfo)
71 protected void disposeFileInfo(Object element, FileInfo info) {
72 if (info instanceof StyleSheetInfo) {
73 IDocument document = getDocument(element);
74 // TODO Check whether this is really necessary, as the document is
76 if (document != null) {
77 IStyleSheet styleSheet = ((StyleSheetInfo) info).styleSheet;
78 if (styleSheet instanceof IDocumentListener) {
79 document.removeDocumentListener((IDocumentListener)
84 super.disposeFileInfo(element, info);
87 // Public Methods ----------------------------------------------------------
90 * Creates the parsed style sheet object corresponding to the specified
93 * @param document the document to parse
94 * @return the parsed style sheet
96 public IStyleSheet createStyleSheet(IDocument document) {
97 return new StyleSheet(document);
101 * Returns the style sheet associated with the specified element.
103 * @param element the element
104 * @return the style sheet associated with the element
106 public IStyleSheet getStyleSheet(Object element) {
107 FileInfo info = getFileInfo(element);
108 if (info instanceof StyleSheetInfo) {
109 StyleSheetInfo styleSheetInfo = (StyleSheetInfo) info;
110 return styleSheetInfo.styleSheet;