summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java
diff options
context:
space:
mode:
Diffstat (limited to 'propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java')
-rw-r--r--propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java
new file mode 100644
index 0000000..a60b698
--- /dev/null
+++ b/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Google, Inc.
+ * 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:
+ * Google, Inc. - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.wb.internal.core.model.property.editor;
+
+import java.beans.PropertyDescriptor;
+
+/**
+ * {@link PropertyEditorProvider} that creates editors based on {@link PropertyDescriptor}
+ * attributes, such as "enumerationValues".
+ *
+ * @author scheglov_ke
+ * @coverage core.model.property.editor
+ */
+public final class PropertyDescriptorEditorProvider extends PropertyEditorProvider {
+ ////////////////////////////////////////////////////////////////////////////
+ //
+ // PropertyEditorProvider
+ //
+ ////////////////////////////////////////////////////////////////////////////
+ @Override
+ public PropertyEditor getEditorForPropertyDescriptor(PropertyDescriptor descriptor)
+ throws Exception {
+ {
+ Object attributeValue = descriptor.getValue("enumerationValues");
+ if (isEnumerationProperty(descriptor)) {
+ return new EnumerationValuesPropertyEditor(attributeValue);
+ }
+ }
+ return null;
+ }
+
+ ////////////////////////////////////////////////////////////////////////////
+ //
+ // Utils
+ //
+ ////////////////////////////////////////////////////////////////////////////
+ /**
+ * @return <code>true</code> if given {@link PropertyDescriptor} has attribute "enumerationValues"
+ * with valid value structure.
+ */
+ private static boolean isEnumerationProperty(PropertyDescriptor descriptor) {
+ Object attributeValue = descriptor.getValue("enumerationValues");
+ // should be Object[]
+ if (!(attributeValue instanceof Object[])) {
+ return false;
+ }
+ Object[] enumElements = (Object[]) attributeValue;
+ // should be multiple 3
+ if (enumElements.length % 3 != 0) {
+ return false;
+ }
+ // elements should be sequence of [String,Object,String]
+ for (int i = 0; i < enumElements.length; i++) {
+ Object element = enumElements[i];
+ if (i % 3 == 0 && !(element instanceof String)) {
+ return false;
+ }
+ if (i % 3 == 2 && !(element instanceof String)) {
+ return false;
+ }
+ }
+ // OK
+ return true;
+ }
+}