1/******************************************************************************* 2 * Copyright (c) 2011 Google, Inc. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Google, Inc. - initial API and implementation 10 *******************************************************************************/ 11package org.eclipse.wb.internal.core.model.property.editor; 12 13import java.beans.PropertyDescriptor; 14 15/** 16 * {@link PropertyEditorProvider} that creates editors based on {@link PropertyDescriptor} 17 * attributes, such as "enumerationValues". 18 * 19 * @author scheglov_ke 20 * @coverage core.model.property.editor 21 */ 22public final class PropertyDescriptorEditorProvider extends PropertyEditorProvider { 23 //////////////////////////////////////////////////////////////////////////// 24 // 25 // PropertyEditorProvider 26 // 27 //////////////////////////////////////////////////////////////////////////// 28 @Override 29 public PropertyEditor getEditorForPropertyDescriptor(PropertyDescriptor descriptor) 30 throws Exception { 31 { 32 Object attributeValue = descriptor.getValue("enumerationValues"); 33 if (isEnumerationProperty(descriptor)) { 34 return new EnumerationValuesPropertyEditor(attributeValue); 35 } 36 } 37 return null; 38 } 39 40 //////////////////////////////////////////////////////////////////////////// 41 // 42 // Utils 43 // 44 //////////////////////////////////////////////////////////////////////////// 45 /** 46 * @return <code>true</code> if given {@link PropertyDescriptor} has attribute "enumerationValues" 47 * with valid value structure. 48 */ 49 private static boolean isEnumerationProperty(PropertyDescriptor descriptor) { 50 Object attributeValue = descriptor.getValue("enumerationValues"); 51 // should be Object[] 52 if (!(attributeValue instanceof Object[])) { 53 return false; 54 } 55 Object[] enumElements = (Object[]) attributeValue; 56 // should be multiple 3 57 if (enumElements.length % 3 != 0) { 58 return false; 59 } 60 // elements should be sequence of [String,Object,String] 61 for (int i = 0; i < enumElements.length; i++) { 62 Object element = enumElements[i]; 63 if (i % 3 == 0 && !(element instanceof String)) { 64 return false; 65 } 66 if (i % 3 == 2 && !(element instanceof String)) { 67 return false; 68 } 69 } 70 // OK 71 return true; 72 } 73} 74