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 com.google.common.base.Objects;
14
15import org.eclipse.wb.core.controls.CCombo3;
16import org.eclipse.wb.internal.core.model.property.Property;
17
18import java.beans.PropertyDescriptor;
19
20/**
21 * {@link PropertyEditor} for "enumerationValues" attribute of {@link PropertyDescriptor}.
22 * <p>
23 * See http://javadude.com/articles/javabeanattributes.html for attributes.
24 *
25 * @author scheglov_ke
26 * @coverage core.model.property.editor
27 */
28public class EnumerationValuesPropertyEditor extends AbstractComboPropertyEditor
29    implements
30      IValueSourcePropertyEditor {
31  private final String[] m_names;
32  private final Object[] m_values;
33  private final String[] m_sources;
34
35  ////////////////////////////////////////////////////////////////////////////
36  //
37  // Constructor
38  //
39  ////////////////////////////////////////////////////////////////////////////
40  public EnumerationValuesPropertyEditor(Object attributeValue) {
41    Object[] enumElements = (Object[]) attributeValue;
42    int items = enumElements.length / 3;
43    m_names = new String[items];
44    m_values = new Object[items];
45    m_sources = new String[items];
46    for (int i = 0; i < items; i++) {
47      m_names[i] = (String) enumElements[3 * i + 0];
48      m_values[i] = enumElements[3 * i + 1];
49      m_sources[i] = (String) enumElements[3 * i + 2];
50    }
51  }
52
53  ////////////////////////////////////////////////////////////////////////////
54  //
55  // TextDisplayPropertyEditor
56  //
57  ////////////////////////////////////////////////////////////////////////////
58  @Override
59  public String getText(Property property) throws Exception {
60    Object value = property.getValue();
61    // return name for value
62    if (value != Property.UNKNOWN_VALUE) {
63      for (int i = 0; i < m_values.length; i++) {
64        if (Objects.equal(m_values[i], value)) {
65          return m_names[i];
66        }
67      }
68    }
69    // unknown value
70    return null;
71  }
72
73  ////////////////////////////////////////////////////////////////////////////
74  //
75  // IValueSourcePropertyEditor
76  //
77  ////////////////////////////////////////////////////////////////////////////
78  @Override
79public String getValueSource(Object value) throws Exception {
80    if (value != Property.UNKNOWN_VALUE) {
81      for (int i = 0; i < m_values.length; i++) {
82        if (Objects.equal(m_values[i], value)) {
83          return m_sources[i];
84        }
85      }
86    }
87    // unknown value
88    return null;
89  }
90
91  ////////////////////////////////////////////////////////////////////////////
92  //
93  // IClipboardSourceProvider
94  //
95  ////////////////////////////////////////////////////////////////////////////
96//  public String getClipboardSource(GenericProperty property) throws Exception {
97//    Object value = property.getValue();
98//    return getValueSource(value);
99//  }
100
101  ////////////////////////////////////////////////////////////////////////////
102  //
103  // Combo
104  //
105  ////////////////////////////////////////////////////////////////////////////
106  @Override
107  protected void addItems(Property property, CCombo3 combo) throws Exception {
108    for (String title : m_names) {
109      combo.add(title);
110    }
111  }
112
113  @Override
114  protected void selectItem(Property property, CCombo3 combo) throws Exception {
115    combo.setText(getText(property));
116  }
117
118  @Override
119  protected void toPropertyEx(Property property, CCombo3 combo, int index) throws Exception {
120    Object value = m_values[index];
121//    if (property instanceof GenericProperty) {
122//      GenericProperty genericProperty = (GenericProperty) property;
123//      String source = getValueSource(value);
124//      genericProperty.setExpression(source, value);
125//    } else {
126      property.setValue(value);
127//    }
128  }
129}
130