intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / properties / CssRulePropertySource.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: CssRulePropertySource.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.properties;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import net.sourceforge.phpeclipse.core.model.ISourceReference;
20 import net.sourceforge.phpeclipse.css.core.CssCore;
21 import net.sourceforge.phpeclipse.css.core.model.IDeclaration;
22 import net.sourceforge.phpeclipse.css.core.model.IPropertyInfo;
23 import net.sourceforge.phpeclipse.css.core.model.IRule;
24 import net.sourceforge.phpeclipse.css.core.profiles.IProfile;
25 import net.sourceforge.phpeclipse.css.core.profiles.IProfileManager;
26 import net.sourceforge.phpeclipse.css.ui.CssUI;
27
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.ui.views.properties.IPropertyDescriptor;
31 import org.eclipse.ui.views.properties.IPropertySource;
32 import org.eclipse.ui.views.properties.PropertyDescriptor;
33
34 /**
35  * 
36  */
37 public class CssRulePropertySource implements IPropertySource {
38
39         // Inner Classes -----------------------------------------------------------
40
41         /**
42          * Extended label provider that decorates the property values with specific
43          * icons when a priority is set.
44          */
45         private static class DeclarationLabelProvider extends LabelProvider {
46
47                 /**
48                  * The CSS declaration for which a label should be provided.
49                  */
50                 private IDeclaration declaration;
51
52                 /**
53                  * Constructor.
54                  * 
55                  * @param declaration the declaration for which to provide a label
56                  */
57                 public DeclarationLabelProvider(IDeclaration declaration) {
58                         this.declaration = declaration;
59                 }
60
61                 /*
62                  * @see org.eclipse.jface.viewers.ILabelProvider#getImage(Object)
63                  */
64                 public Image getImage(Object element) {
65                         if (this.declaration.getPriority() != null) {
66                                 CssUI plugin = CssUI.getDefault();
67                                 return plugin.getImageRegistry().get(
68                                         CssUI.ICON_IMPORTANT);
69                         }
70                         return null;
71                 }
72
73                 /*
74                  * @see org.eclipse.jface.viewers.ILabelProvider#getText(Object)
75                  */
76                 public String getText(Object element) {
77                         return this.declaration.getValue().getSource();
78                 }
79
80         }
81
82         // Instance Variables ------------------------------------------------------
83
84         /**
85          * The CSS rule for which properties should be provided.
86          */
87         private IRule rule;
88
89         /**
90          * Cached list of property descriptors (<code>IPropertyDescriptor</code>).
91          */
92         private List descriptors;
93
94         // Constructors ------------------------------------------------------------
95
96         /**
97          * Constructor.
98          * 
99          * @param styleRule the style rule for which to provide properties
100          */
101         public CssRulePropertySource(IRule styleRule) {
102                 this.rule = styleRule;
103         }
104
105         // IPropertySource Implementation ------------------------------------------
106
107         /**
108          * @see IPropertySource#getEditableValue()
109          */
110         public Object getEditableValue() {
111                 return null;
112         }
113
114         /**
115          * @see IPropertySource#getPropertyDescriptors()
116          */
117         public IPropertyDescriptor[] getPropertyDescriptors() {
118                 if (this.descriptors == null) {
119                         IProfileManager mgr = CssCore.getDefault().getProfileManager();
120                         IProfile profile = mgr.getProfile(null);
121                         this.descriptors = new ArrayList();
122                         for (int i = 0; i < this.rule.getDeclarations().length; i++) {
123                                 IDeclaration declaration = this.rule.getDeclarations()[i];
124                                 String property = declaration.getProperty().getSource();
125                                 PropertyDescriptor descriptor = new PropertyDescriptor(
126                                         new Integer(i), property);
127                                 descriptor.setLabelProvider(
128                                         new DeclarationLabelProvider(declaration));
129                                 IPropertyInfo info = profile.getPropertyInfo(property);
130                                 if (info != null) {
131                                         descriptor.setCategory(info.getCategory());
132                                 }
133                                 this.descriptors.add(descriptor);
134                         }
135                 }
136                 return (IPropertyDescriptor[]) this.descriptors.toArray(
137                         new IPropertyDescriptor[this.descriptors.size()]);
138         }
139
140         /**
141          * @see IPropertySource#getPropertyValue(Object)
142          */
143         public Object getPropertyValue(Object id) {
144                 int i = ((Integer) id).intValue();
145                 ISourceReference propertyValue =
146                         this.rule.getDeclarations()[i].getValue();
147                 return propertyValue.getSource();
148         }
149
150         /**
151          * @see IPropertySource#isPropertySet(Object)
152          */
153         public boolean isPropertySet(Object id) {
154                 // read-only property
155                 return false;
156         }
157
158         /**
159          * @see IPropertySource#resetPropertyValue(Object)
160          */
161         public void resetPropertyValue(Object id) {
162                 // read-only property
163         }
164
165         /**
166          * @see IPropertySource#setPropertyValue(Object, Object)
167          */
168         public void setPropertyValue(Object id, Object value) {
169                 // read-only property
170         }
171
172 }