Skip to main content
summaryrefslogtreecommitdiffstats
blob: 026a5d4e854f99dd8e18cb3c21ece142c42c31d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.properties;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.gef.commands.Command;
import org.eclipse.jst.jsf.common.metadata.Entity;
import org.eclipse.jst.jsf.common.metadata.internal.IMetaDataDomainContext;
import org.eclipse.jst.jsf.common.metadata.query.internal.MetaDataQueryContextFactory;
import org.eclipse.jst.jsf.common.metadata.query.internal.MetaDataQueryFactory;
import org.eclipse.jst.jsf.common.metadata.query.internal.taglib.ITaglibDomainMetaDataQuery;
import org.eclipse.jst.jsf.common.runtime.internal.view.model.common.ITagAttribute;
import org.eclipse.jst.jsf.common.runtime.internal.view.model.common.ITagElement;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IStructuredDocumentContextResolverFactory;
import org.eclipse.jst.jsf.context.resolver.structureddocument.ITaglibContextResolver;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IWorkspaceContextResolver;
import org.eclipse.jst.jsf.context.resolver.structureddocument.internal.IStructuredDocumentContextResolverFactory2;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory;
import org.eclipse.jst.jsf.designtime.internal.resolver.ITagElementResolver;
import org.eclipse.jst.jsf.metadataprocessors.MetaDataEnabledProcessingFactory;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.commands.single.ChangeAttributeCommand;
import org.eclipse.jst.pagedesigner.editors.properties.IPropertyPageDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.w3c.dom.Element;

/**
 * 
 * @author mengbo
 */
public class AttributePropertySource implements IPropertySource {
	private IDOMElement _element;

	private IPropertySource _innerSource;

	private Entity _tagEntity;

	private IStructuredDocumentContext _context;

	private IPropertyDescriptor[] _descriptors;

	private ITaglibDomainMetaDataQuery _query;

	/**
	 * Constructor
	 * @param ele
	 * @param source
	 */
	public AttributePropertySource(Element ele, IPropertySource source) {
		_element = (IDOMElement) ele;
		_innerSource = source;
		_tagEntity = getTagEntity();
	}

	private Entity getTagEntity() {
		_context = 
				IStructuredDocumentContextFactory.INSTANCE.getContext(_element.getStructuredDocument(), _element);
		if (_context == null) 
			return null;
		
		IWorkspaceContextResolver wsresolver = 
				IStructuredDocumentContextResolverFactory.INSTANCE.getWorkspaceContextResolver(_context);
		if (wsresolver == null) 
			return null;
		
		ITaglibContextResolver resolver = 
				IStructuredDocumentContextResolverFactory.INSTANCE.getTaglibContextResolver(_context);
		if (resolver == null) return null;
		
		String uri = resolver.getTagURIForNodeName(_element);
		
		//TODO: make below better
		if (uri == null){
			if (_element.getNamespaceURI() != null && _element.getNamespaceURI().equals("http://java.sun.com/JSP/Page")) //$NON-NLS-1$
				uri = "JSP11"; //$NON-NLS-1$
			else
				uri = "HTML"; //$NON-NLS-1$
		}
		final IMetaDataDomainContext context = MetaDataQueryContextFactory.getInstance().createTaglibDomainModelContext((IFile)wsresolver.getResource());
		_query = MetaDataQueryFactory.getInstance().createQuery(context);
		return _query.getQueryHelper().getEntity(uri, _element.getLocalName());		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
	 */
	public Object getEditableValue() {
		return _innerSource.getEditableValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
	 */
	public Object getPropertyValue(Object id) {
		// CR377844: when the attribute in source is "a>b", we would like to
		// display
		// "a>b" in cell editor. But _innerSource.getPropertyValue(id) will
		// return the source
		// of the attribute, so can't use that here.
		// read QTS log for detail.
		// return _innerSource.getPropertyValue(id);
		if (id == null) {
			return null;
		}
		String name = id.toString();
		String value = _element.getAttribute(name);
		if (value == null) {
			value = ""; //$NON-NLS-1$
		}
		return value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
	 */
	public boolean isPropertySet(Object id) {
		return _innerSource.isPropertySet(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
	 */
	public void resetPropertyValue(Object id) {
		_innerSource.resetPropertyValue(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object,
	 *      java.lang.Object)
	 */
	public void setPropertyValue(final Object id, final Object value) {
		Object oldValue = getPropertyValue(id);
		if (oldValue == value || (oldValue != null && oldValue.equals(value))) {
			return;
		}
		Command c = new ChangeAttributeCommand(
				PDPlugin
						.getResourceString("AttributePropertySource.CommandLabel.ChangeAttribute"), _element, (String) id, (String) value); //$NON-NLS-1$
		c.execute();
	}

	/**
	 * the major job of this wrapper is to provide tag attribute property descriptors 
	 */
	public IPropertyDescriptor[] getPropertyDescriptors() {
		if (_descriptors == null) {
			final List result = new ArrayList();
			
			final ITagElementResolver tagregResolver = IStructuredDocumentContextResolverFactory2.INSTANCE
					.getResolver(_context, ITagElementResolver.class);
			
			if (tagregResolver != null) { //relies on getTagEntity() having been called to have setup the _element
				final ITagElement tag = tagregResolver.getTagElement(_element);
				if (tag != null) {
					for (final ITagAttribute attr : tag.getAttributes().values()) {
						final IPropertyDescriptor pd = getAttrPropertyDescriptor(attr
								.getName());
						if (pd != null)
							result.add(new PropertyDescriptorWrapper(_element,
									pd));// ,
							// getStatusLineManager()));
						else {
							final PropertyDescriptor pd1 = new TextPropertyDescriptor(
									attr.getName(), attr.getName());
							pd1.setDescription(attr.getDescription());
							pd1.setCategory(ITabbedPropertiesConstants.OTHER_CATEGORY);
							result.add(new PropertyDescriptorWrapper(_element,
									pd1));// ,
							// getStatusLineManager()));
						}

					}
				}
			}
			
			if (result.size() == 0) {// for JSP and HTML cases
														// this is still
														// necessary
				final IPropertyDescriptor[] descs = _innerSource.getPropertyDescriptors();
				for (int i = 0; i < descs.length; i++) {
					final IPropertyDescriptor pd = getAttrPropertyDescriptor((String) descs[i]
							.getId());
					if (pd != null)
						result.add(new PropertyDescriptorWrapper(_element, pd));// ,
					// getStatusLineManager()));
					else {
						if (descs[i] instanceof PropertyDescriptor)
							((PropertyDescriptor) descs[i])
									.setCategory(ITabbedPropertiesConstants.OTHER_CATEGORY);
						result.add(new PropertyDescriptorWrapper(_element,
								descs[i]));// ,
						// getStatusLineManager()));
					}

				}
			}

			_descriptors = new IPropertyDescriptor[result.size()];
			result.toArray(_descriptors);

		}
		return _descriptors;
	}

//	private IAttributeDescriptor findReferencedAttribute(
//			IElementDescriptor elementDescriptor, IPropertyDescriptor desc) {
//		return null;
//	}

//	private IElementDescriptor getElementDescriptor() {
//		ICMRegistry registry = CMRegistry.getInstance();
//		String uri = CMUtil.getElementNamespaceURI(_element);
//		return registry.getElementDescriptor(uri, _element.getLocalName());
//	}

	private IPropertyDescriptor getAttrPropertyDescriptor(String attrName){
		//Bug 364667 - NullPointerException occurs when I try to display attributes for an HTML element in Web Page Editor
		if (_query != null) {
			Entity attrEntity = _query.findTagAttributeEntity(_tagEntity, attrName);
			List ppds = MetaDataEnabledProcessingFactory.getInstance().getAttributeValueRuntimeTypeFeatureProcessors(IPropertyPageDescriptor.class, _context, attrEntity);
			if (ppds.size() > 0)
				return (IPropertyDescriptor)((IPropertyPageDescriptor)ppds.get(0)).getAdapter(IPropertyDescriptor.class);
		}
		return null;
			
	}
	
//	private IStatusLineManager getStatusLineManager() {
//		_page.getSite().getActionBars().getStatusLineManager();
//	}
}

Back to the top