summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/model/property/editor/AbstractListPropertyEditor.java
blob: ba341035fa424b2da1ec20dcf2439943b4c341fc (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * 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 org.eclipse.wb.core.controls.CCombo3;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.utils.check.Assert;

import java.util.List;
import java.util.Map;

/**
 * The {@link PropertyEditor} for selecting single expression from given set.
 *
 * @author sablin_aa
 * @coverage core.model.property.editor
 */
public abstract class AbstractListPropertyEditor extends AbstractComboPropertyEditor
    implements
      IValueSourcePropertyEditor {
  ////////////////////////////////////////////////////////////////////////////
  //
  // TextDisplayPropertyEditor
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  public String getText(Property property) throws Exception {
    // return title for value
    Object value = property.getValue();
    if (value != Property.UNKNOWN_VALUE) {
      int index = getValueIndex(value);
      if (index >= 0) {
        return getTitle(index);
      } else {
        if (value instanceof String) {
          return (String) value;
        }
      }
    }
    // unknown value
    return null;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // IValueSourcePropertyEditor
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
public String getValueSource(Object value) throws Exception {
    // return expression for value
    if (value != Property.UNKNOWN_VALUE) {
      int index = getValueIndex(value);
      if (index >= 0) {
        return getExpression(index);
      }
    }
    // unknown value
    return null;
  }

//  ////////////////////////////////////////////////////////////////////////////
//  //
//  // IClipboardSourceProvider
//  //
//  ////////////////////////////////////////////////////////////////////////////
//  @Override
//public String getClipboardSource(GenericProperty property) throws Exception {
//    Object value = property.getValue();
//    return getValueSource(value);
//  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Combo
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  protected void addItems(Property property, CCombo3 combo) throws Exception {
    for (int i = 0; i < getCount(); i++) {
      combo.add(getTitle(i));
    }
  }

  @Override
  protected void selectItem(Property property, CCombo3 combo) throws Exception {
    combo.setText(getText(property));
  }

  @Override
  protected void toPropertyEx(Property property, CCombo3 combo, int index) throws Exception {
//    if (property instanceof GenericProperty) {
//      GenericProperty genericProperty = (GenericProperty) property;
//      String expression = getExpression(index);
//      Object evaluatedExpression = evaluateExpression(genericProperty, expression);
//      // apply expression
//      genericProperty.setExpression(expression, evaluatedExpression);
//    } else {
      toPropertyEx_simpleProperty(property, combo, index);
//    }
  }

//  private static Object evaluateExpression(final GenericProperty genericProperty,
//      final String expression) {
//    return ExecutionUtils.runObjectIgnore(new RunnableObjectEx<Object>() {
//      public Object runObject() throws Exception {
//        JavaInfo javaInfo = genericProperty.getJavaInfo();
//        ClassLoader classLoader = JavaInfoUtils.getClassLoader(javaInfo);
//        return ScriptUtils.evaluate(classLoader, expression);
//      }
//    }, Property.UNKNOWN_VALUE);
//      System.out.println("HACK 1234");
//      return Property.UNKNOWN_VALUE;
//  }

  /**
   * Sets value of simple {@link Property}, not {@link GenericProperty}.
   */
  protected void toPropertyEx_simpleProperty(Property property, CCombo3 combo, int index)
      throws Exception {
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access to list items
  //
  ////////////////////////////////////////////////////////////////////////////
  abstract protected int getCount();

  abstract protected int getValueIndex(Object value);

  abstract protected String getTitle(int index);

  abstract protected String getExpression(int index) throws Exception;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Utils
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Extract string array from parameters.
   */
  protected static String[] getParameterAsArray(Map<String, Object> parameters, String name) {
    return getParameterAsArray(parameters, name, false);
  }

  @SuppressWarnings("unchecked")
  protected static String[] getParameterAsArray(Map<String, Object> parameters,
      String name,
      boolean noAssert) {
    String[] values = null;
    if (parameters.containsKey(name)) {
      List<String> list = (List<String>) parameters.get(name);
      values = list.toArray(new String[list.size()]);
    } else {
      if (noAssert) {
        values = null;
      } else {
        Assert.fail(String.format("No parameter %s in %s.", name, parameters));
      }
    }
    return values;
  }
}