intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.core / src / net / sourceforge / phpeclipse / css / core / internal / model / AbstractRule.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: AbstractRule.java,v 1.1 2004-09-02 18:07:12 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.core.internal.model;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19
20 import net.sourceforge.phpeclipse.core.model.SourceReference;
21 import net.sourceforge.phpeclipse.css.core.model.IDeclaration;
22 import net.sourceforge.phpeclipse.css.core.model.IRule;
23 import net.sourceforge.phpeclipse.css.core.model.IStyleSheet;
24
25 import org.eclipse.core.runtime.Platform;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IRegion;
28
29 /**
30  * 
31  */
32 public abstract class AbstractRule extends SourceReference
33         implements IRule {
34
35         // Instance Variables ------------------------------------------------------
36
37         private IStyleSheet styleSheet;
38
39         private IRule parent;
40
41         private List children = new ArrayList(); 
42
43         private List declarations = new ArrayList();
44
45         // Constructors ------------------------------------------------------------
46
47         /**
48          * Constructor.
49          * 
50          * @param document the document that contains the rule
51          * @param styleSheet the style sheet that contains the rule
52          */
53         public AbstractRule(IDocument document, IStyleSheet styleSheet) {
54                 this(document, styleSheet, null);
55         }
56
57         /**
58          * Constructor.
59          * 
60          * @param document the document that contains the rule
61          * @param styleSheet the style sheet that contains the rule
62          * @param parent the parent rule, or <code>null</code> if the rule is at top
63          *        level
64          */
65         public AbstractRule(IDocument document, IStyleSheet styleSheet,
66                 IRule parent) {
67                 super(document);
68                 this.styleSheet = styleSheet;
69                 this.parent = parent;
70         }
71
72         // IAdaptable Implementation -----------------------------------------------
73
74         /*
75          * @see IAdaptable#getAdapter(Class)
76          */
77         public Object getAdapter(Class adapter) {
78                 return Platform.getAdapterManager().getAdapter(this, adapter);
79         }
80
81         // IStyleRule Implementation -----------------------------------------------
82
83         /*
84          * @see IRule#getStyleSheet()
85          */
86         public final IStyleSheet getStyleSheet() {
87                 return styleSheet;
88         }
89
90         /*
91          * @see IRule#getParent()
92          */
93         public final IRule getParent() {
94                 return parent;
95         }
96
97         /*
98          * @see IRule#getChildren()
99          */
100         public final IRule[] getChildren() {
101                 return (IRule[]) children.toArray(new IRule[children.size()]);
102         }
103
104         /*
105          * @see IRule#getDeclarationAt(int)
106          */
107         public final IDeclaration getDeclarationAt(int offset) {
108                 for (Iterator i = declarations.iterator(); i.hasNext(); ) {
109                         IDeclaration declaration = (IDeclaration) i.next();
110                         IRegion region = declaration.getSourceRegion();
111                         if ((offset > region.getOffset())
112                          && (offset < (region.getOffset() + region.getLength()))) {
113                                 return declaration;
114                         }
115                 }
116                 return null;
117         }
118
119         /*
120          * @see IRule#getDeclarations()
121          */
122         public final IDeclaration[] getDeclarations() {
123                 return (IDeclaration[]) declarations.toArray(
124                         new IDeclaration[declarations.size()]);
125         }
126
127         // Public Methods ----------------------------------------------------------
128
129         public final void addChild(IRule rule) {
130                 children.add(rule);
131         }
132
133         public final void addDeclaration(IDeclaration declaration) {
134                 declarations.add(declaration);
135         }
136
137 }