summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/PropertyDescriptorEditorProvider.java
blob: a60b6985e9443988391e018590e1df71e7458367 (plain)
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
/*******************************************************************************
 * 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;
  }
}