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 org.eclipse.wb.core.controls.CCombo3;
14import org.eclipse.wb.internal.core.model.property.Property;
15import org.eclipse.wb.internal.core.utils.check.Assert;
16
17import java.util.List;
18import java.util.Map;
19
20/**
21 * The {@link PropertyEditor} for selecting single expression from given set.
22 *
23 * @author sablin_aa
24 * @coverage core.model.property.editor
25 */
26public abstract class AbstractListPropertyEditor extends AbstractComboPropertyEditor
27    implements
28      IValueSourcePropertyEditor {
29  ////////////////////////////////////////////////////////////////////////////
30  //
31  // TextDisplayPropertyEditor
32  //
33  ////////////////////////////////////////////////////////////////////////////
34  @Override
35  public String getText(Property property) throws Exception {
36    // return title for value
37    Object value = property.getValue();
38    if (value != Property.UNKNOWN_VALUE) {
39      int index = getValueIndex(value);
40      if (index >= 0) {
41        return getTitle(index);
42      } else {
43        if (value instanceof String) {
44          return (String) value;
45        }
46      }
47    }
48    // unknown value
49    return null;
50  }
51
52  ////////////////////////////////////////////////////////////////////////////
53  //
54  // IValueSourcePropertyEditor
55  //
56  ////////////////////////////////////////////////////////////////////////////
57  @Override
58public String getValueSource(Object value) throws Exception {
59    // return expression for value
60    if (value != Property.UNKNOWN_VALUE) {
61      int index = getValueIndex(value);
62      if (index >= 0) {
63        return getExpression(index);
64      }
65    }
66    // unknown value
67    return null;
68  }
69
70//  ////////////////////////////////////////////////////////////////////////////
71//  //
72//  // IClipboardSourceProvider
73//  //
74//  ////////////////////////////////////////////////////////////////////////////
75//  @Override
76//public String getClipboardSource(GenericProperty property) throws Exception {
77//    Object value = property.getValue();
78//    return getValueSource(value);
79//  }
80
81  ////////////////////////////////////////////////////////////////////////////
82  //
83  // Combo
84  //
85  ////////////////////////////////////////////////////////////////////////////
86  @Override
87  protected void addItems(Property property, CCombo3 combo) throws Exception {
88    for (int i = 0; i < getCount(); i++) {
89      combo.add(getTitle(i));
90    }
91  }
92
93  @Override
94  protected void selectItem(Property property, CCombo3 combo) throws Exception {
95    combo.setText(getText(property));
96  }
97
98  @Override
99  protected void toPropertyEx(Property property, CCombo3 combo, int index) throws Exception {
100//    if (property instanceof GenericProperty) {
101//      GenericProperty genericProperty = (GenericProperty) property;
102//      String expression = getExpression(index);
103//      Object evaluatedExpression = evaluateExpression(genericProperty, expression);
104//      // apply expression
105//      genericProperty.setExpression(expression, evaluatedExpression);
106//    } else {
107      toPropertyEx_simpleProperty(property, combo, index);
108//    }
109  }
110
111//  private static Object evaluateExpression(final GenericProperty genericProperty,
112//      final String expression) {
113//    return ExecutionUtils.runObjectIgnore(new RunnableObjectEx<Object>() {
114//      public Object runObject() throws Exception {
115//        JavaInfo javaInfo = genericProperty.getJavaInfo();
116//        ClassLoader classLoader = JavaInfoUtils.getClassLoader(javaInfo);
117//        return ScriptUtils.evaluate(classLoader, expression);
118//      }
119//    }, Property.UNKNOWN_VALUE);
120//      System.out.println("HACK 1234");
121//      return Property.UNKNOWN_VALUE;
122//  }
123
124  /**
125   * Sets value of simple {@link Property}, not {@link GenericProperty}.
126   */
127  protected void toPropertyEx_simpleProperty(Property property, CCombo3 combo, int index)
128      throws Exception {
129  }
130
131  ////////////////////////////////////////////////////////////////////////////
132  //
133  // Access to list items
134  //
135  ////////////////////////////////////////////////////////////////////////////
136  abstract protected int getCount();
137
138  abstract protected int getValueIndex(Object value);
139
140  abstract protected String getTitle(int index);
141
142  abstract protected String getExpression(int index) throws Exception;
143
144  ////////////////////////////////////////////////////////////////////////////
145  //
146  // Utils
147  //
148  ////////////////////////////////////////////////////////////////////////////
149  /**
150   * Extract string array from parameters.
151   */
152  protected static String[] getParameterAsArray(Map<String, Object> parameters, String name) {
153    return getParameterAsArray(parameters, name, false);
154  }
155
156  @SuppressWarnings("unchecked")
157  protected static String[] getParameterAsArray(Map<String, Object> parameters,
158      String name,
159      boolean noAssert) {
160    String[] values = null;
161    if (parameters.containsKey(name)) {
162      List<String> list = (List<String>) parameters.get(name);
163      values = list.toArray(new String[list.size()]);
164    } else {
165      if (noAssert) {
166        values = null;
167      } else {
168        Assert.fail(String.format("No parameter %s in %s.", name, parameters));
169      }
170    }
171    return values;
172  }
173}