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
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssDocumentProvider.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal;
15
16 import net.sourceforge.phpeclipse.css.core.internal.model.StyleSheet;
17 import net.sourceforge.phpeclipse.css.core.model.IStyleSheet;
18
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;
23
24 /**
25  * Document provider for CSS files.
26  * 
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
29  *      useless
30  */
31 public class CssDocumentProvider extends TextFileDocumentProvider {
32
33         // Inner Classes -----------------------------------------------------------
34
35         private class StyleSheetInfo extends FileInfo {
36                 private IStyleSheet styleSheet;
37         }
38
39         // TestFileDocumentProvider Implementation ---------------------------------
40
41         /*
42          * @see TextFileDocumentProvider#createEmptyFileInfo()
43          */
44         protected FileInfo createEmptyFileInfo() {
45                 return new StyleSheetInfo();
46         }
47
48         /*
49          * @see TextFileDocumentProvider#createFileInfo(Object)
50          */
51         protected FileInfo createFileInfo(Object element) throws CoreException {
52                 FileInfo fileInfo = super.createFileInfo(element);
53                 if (!(fileInfo instanceof StyleSheetInfo)) {
54                         return null;
55                 }
56
57                 IDocument document = fileInfo.fTextFileBuffer.getDocument();
58                 IStyleSheet styleSheet = createStyleSheet(document);
59                 if (styleSheet instanceof IDocumentListener) {
60                         document.addDocumentListener((IDocumentListener) styleSheet);
61                 }
62                 StyleSheetInfo styleSheetInfo = (StyleSheetInfo) fileInfo;
63                 styleSheetInfo.styleSheet = styleSheet;
64
65                 return styleSheetInfo;
66         }
67
68         /*
69          * @see TextFileDocumentProvider#disposeFileInfo(Object, TextFileDocumentProvider.FileInfo)
70          */
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
75                         //      always null here
76                         if (document != null) {
77                                 IStyleSheet styleSheet = ((StyleSheetInfo) info).styleSheet;
78                                 if (styleSheet instanceof IDocumentListener) {
79                                         document.removeDocumentListener((IDocumentListener)
80                                                         styleSheet);
81                                 }
82                         }
83                 }
84                 super.disposeFileInfo(element, info);
85         }
86
87         // Public Methods ----------------------------------------------------------
88
89         /**
90          * Creates the parsed style sheet object corresponding to the specified 
91          * document.
92          * 
93          * @param document the document to parse
94          * @return the parsed style sheet
95          */
96         public IStyleSheet createStyleSheet(IDocument document) {
97                 return new StyleSheet(document);
98         }
99
100         /**
101          * Returns the style sheet associated with the specified element.
102          * 
103          * @param element the element
104          * @return the style sheet associated with the element
105          */
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;
111                 }
112                 return null;
113         }
114
115 }