Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b19078e74db6df3108e435d9b4acfde88c2dc6c7 (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
/*******************************************************************************
 * Copyright (c) 2004, 2011 John Krasnay 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:
 *     John Krasnay    - initial API and implementation
 *     Florian Thienel - namespace support (bug 253753)
 *******************************************************************************/
package org.eclipse.vex.ui.internal.property;

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

import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySheetEntry;
import org.eclipse.ui.views.properties.IPropertySource2;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;
import org.eclipse.vex.core.internal.dom.Namespace;
import org.eclipse.vex.core.provisional.dom.AttributeDefinition;
import org.eclipse.vex.core.provisional.dom.DocumentValidationException;
import org.eclipse.vex.core.provisional.dom.IAttribute;
import org.eclipse.vex.core.provisional.dom.IElement;
import org.eclipse.vex.core.provisional.dom.IValidator;
import org.eclipse.vex.ui.internal.editor.Messages;

public class ElementPropertySource implements IPropertySource2 {

	private static final String ATTR_ID = "id"; //$NON-NLS-1$

	private static final String ELEMENT_NAME_PROPERTY = "elementName"; //$NON-NLS-1$
	private static final String ELEMENT_NAMESPACE_URI_PROPERTY = "elementNsUri"; //$NON-NLS-1$
	private static final String ELEMENT_NAMESPACE_PREFIX_PROPERTY = "elementNsPrefix"; //$NON-NLS-1$

	private static final String ELEMENT_CATEGORY = "Element";
	private static final String ATTRIBUTES_CATEGORY = "Attributes";
	private static final String NAMESPACES_CATEGORY = "Namespaces";

	private final IElement element;
	private final IValidator validator;
	private final boolean multipleElementsSelected;

	public ElementPropertySource(final IElement element, final IValidator validator, final boolean multipleElementsSelected) {
		this.element = element;
		this.validator = validator;
		this.multipleElementsSelected = multipleElementsSelected;
	}

	public Object getEditableValue() {
		return null;
	}

	public IPropertyDescriptor[] getPropertyDescriptors() {
		final List<IPropertyDescriptor> result = new ArrayList<IPropertyDescriptor>();

		result.add(createExpertPropertyDescriptor(ELEMENT_NAME_PROPERTY, "Element Name", ELEMENT_CATEGORY));

		if (element.getQualifiedName().getQualifier() != null) {
			result.add(createExpertPropertyDescriptor(ELEMENT_NAMESPACE_URI_PROPERTY, "Namespace URI", ELEMENT_CATEGORY));
			result.add(createExpertPropertyDescriptor(ELEMENT_NAMESPACE_PREFIX_PROPERTY, "Namespace Prefix", ELEMENT_CATEGORY));
		}

		/*
		 * Note that elements from DocumentFragments don't have access to their original document, so we get it from the
		 * VexWidget.
		 */
		final List<AttributeDefinition> attributeDefinitions = validator.getAttributeDefinitions(element);
		for (final AttributeDefinition attributeDefinition : attributeDefinitions) {
			final PropertyDescriptor propertyDescriptor;
			if (multipleElementsSelected && attributeDefinition.getName().equals(ATTR_ID)) {
				propertyDescriptor = new PropertyDescriptor(attributeDefinition, attributeDefinition.getName());
			} else if (attributeDefinition.isFixed()) {
				propertyDescriptor = new PropertyDescriptor(attributeDefinition, attributeDefinition.getName());
			} else if (attributeDefinition.getType() == AttributeDefinition.Type.ENUMERATION) {
				propertyDescriptor = new ComboBoxPropertyDescriptor(attributeDefinition, attributeDefinition.getName(), getEnumValues(attributeDefinition));
			} else {
				propertyDescriptor = new TextPropertyDescriptor(attributeDefinition, attributeDefinition.getName());
			}
			propertyDescriptor.setCategory(ATTRIBUTES_CATEGORY);
			result.add(propertyDescriptor);
		}

		for (final String namespacePrefix : element.getNamespacePrefixes()) {
			final String namespaceUri = element.getNamespaceURI(namespacePrefix);
			final NamespaceUri namespaceDeclaration = new NamespaceUri(namespaceUri);
			result.add(createExpertPropertyDescriptor(namespaceDeclaration, Namespace.XMLNS_NAMESPACE_PREFIX + ":" + namespacePrefix, NAMESPACES_CATEGORY));
		}

		final String defaultNamespaceUri = element.getDefaultNamespaceURI();
		if (defaultNamespaceUri != null) {
			final NamespaceUri namespaceDeclaration = new NamespaceUri(defaultNamespaceUri);
			result.add(createExpertPropertyDescriptor(namespaceDeclaration, Namespace.XMLNS_NAMESPACE_PREFIX, NAMESPACES_CATEGORY));
		}

		return result.toArray(new IPropertyDescriptor[result.size()]);
	}

	private IPropertyDescriptor createExpertPropertyDescriptor(final Object id, final String displayName, final String category) {
		final PropertyDescriptor propertyDescriptor = new PropertyDescriptor(id, displayName);
		propertyDescriptor.setCategory(category);
		propertyDescriptor.setFilterFlags(new String[] { IPropertySheetEntry.FILTER_ID_EXPERT });
		return propertyDescriptor;
	}

	public Object getPropertyValue(final Object id) {
		if (id == ELEMENT_NAME_PROPERTY) {
			return element.getLocalName();
		}
		if (id == ELEMENT_NAMESPACE_URI_PROPERTY) {
			return element.getQualifiedName().getQualifier();
		}
		if (id == ELEMENT_NAMESPACE_PREFIX_PROPERTY) {
			return element.getNamespacePrefix(element.getQualifiedName().getQualifier());
		}

		if (id instanceof AttributeDefinition) {
			final AttributeDefinition attributeDefinition = (AttributeDefinition) id;
			if (multipleElementsSelected && id.equals(ATTR_ID)) {
				return Messages.getString("ElementPropertySource.multiple"); //$NON-NLS-1$
			}

			final IAttribute attribute = element.getAttribute(attributeDefinition.getName());
			final String value;
			if (attribute != null) {
				value = attribute.getValue();
			} else {
				value = nullToEmpty(attributeDefinition.getDefaultValue());
			}

			if (attributeDefinition.getType() == AttributeDefinition.Type.ENUMERATION) {
				final String[] values = getEnumValues(attributeDefinition);
				for (int i = 0; i < values.length; i++) {
					if (values[i].equals(value)) {
						return Integer.valueOf(i);
					}
				}
				return Integer.valueOf(0);
				// TODO: If the actual value is not in the list, we should probably add it.
			}
			return value;
		}

		if (id instanceof NamespaceUri) {
			return ((NamespaceUri) id).uri;
		}

		return "";
	}

	private static String nullToEmpty(final String string) {
		if (string == null) {
			return "";
		}
		return string;
	}

	public boolean isPropertySet(final Object id) {
		if (id == ELEMENT_NAME_PROPERTY) {
			return true;
		}
		if (id == ELEMENT_NAMESPACE_URI_PROPERTY) {
			return true;
		}
		if (id == ELEMENT_NAMESPACE_PREFIX_PROPERTY) {
			return true;
		}

		if (id instanceof AttributeDefinition) {
			final AttributeDefinition attributeDefinition = (AttributeDefinition) id;
			final IAttribute attribute = element.getAttribute(attributeDefinition.getName());
			if (attribute == null) {
				return false;
			}
			return true;
		}

		if (id instanceof NamespaceUri) {
			return true;
		}

		return false;
	}

	public void resetPropertyValue(final Object id) {
		if (!(id instanceof AttributeDefinition)) {
			return;
		}
		final AttributeDefinition attributeDefinition = (AttributeDefinition) id;
		element.removeAttribute(attributeDefinition.getName());
	}

	public void setPropertyValue(final Object id, final Object value) {
		if (!(id instanceof AttributeDefinition)) {
			return;
		}
		final AttributeDefinition attributeDefinition = (AttributeDefinition) id;

		try {
			if (attributeDefinition.getType() == AttributeDefinition.Type.ENUMERATION) {
				final int i = ((Integer) value).intValue();
				final String enumValue = getEnumValues(attributeDefinition)[i];
				if (!attributeDefinition.isRequired() && enumValue.equals("")) {
					element.removeAttribute(attributeDefinition.getName());
				} else {
					element.setAttribute(attributeDefinition.getName(), enumValue);
				}
			} else {
				final String s = (String) value;
				if (s.equals("")) {
					element.removeAttribute(attributeDefinition.getName());
				} else {
					element.setAttribute(attributeDefinition.getName(), s);
				}
			}
		} catch (final DocumentValidationException e) {
		}
	}

	private static String[] getEnumValues(final AttributeDefinition attributeDefinition) {
		String[] values = attributeDefinition.getValues();
		if (attributeDefinition.isRequired()) {
			return values;
		} else {
			if (values == null) {
				values = new String[1];
				values[0] = "";
			}
			final String[] values2 = new String[values.length + 1];
			values2[0] = ""; //$NON-NLS-1$
			System.arraycopy(values, 0, values2, 1, values.length);
			return values2;
		}
	}

	public boolean isPropertyResettable(final Object id) {
		if (!(id instanceof AttributeDefinition)) {
			return false;
		}
		return true;
	}

	private static class NamespaceUri {
		public final String uri;

		public NamespaceUri(final String uri) {
			this.uri = uri;
		}
	}

}

Back to the top