1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.eclipse.org/org/documents/epl-v10.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ide.eclipse.adt.internal.editors.layout.properties;
18
19import com.android.ide.eclipse.adt.internal.editors.descriptors.AttributeDescriptor;
20import com.android.ide.eclipse.adt.internal.editors.descriptors.ListAttributeDescriptor;
21
22import org.eclipse.wb.core.controls.CCombo3;
23import org.eclipse.wb.internal.core.model.property.Property;
24import org.eclipse.wb.internal.core.model.property.editor.AbstractComboPropertyEditor;
25import org.eclipse.wb.internal.core.model.property.editor.ITextValuePropertyEditor;
26
27class EnumXmlPropertyEditor extends AbstractComboPropertyEditor implements
28        ITextValuePropertyEditor {
29    public static final EnumXmlPropertyEditor INSTANCE = new EnumXmlPropertyEditor();
30
31    private EnumXmlPropertyEditor() {
32    }
33
34    @Override
35    protected String getText(Property property) throws Exception {
36        Object value = property.getValue();
37        if (value == null) {
38            return "";
39        } else if (value instanceof String) {
40            return (String) value;
41        } else if (value == Property.UNKNOWN_VALUE) {
42            return "<varies>";
43        } else {
44            return "";
45        }
46    }
47
48    private String[] getItems(Property property) {
49        XmlProperty xmlProperty = (XmlProperty) property;
50        AttributeDescriptor descriptor = xmlProperty.getDescriptor();
51        assert descriptor instanceof ListAttributeDescriptor;
52        ListAttributeDescriptor list = (ListAttributeDescriptor) descriptor;
53        return list.getValues();
54    }
55
56    @Override
57    protected void addItems(Property property, CCombo3 combo) throws Exception {
58        for (String item : getItems(property)) {
59            combo.add(item);
60        }
61    }
62
63    @Override
64    protected void selectItem(Property property, CCombo3 combo) throws Exception {
65        combo.setText(getText(property));
66    }
67
68    @Override
69    protected void toPropertyEx(Property property, CCombo3 combo, int index) throws Exception {
70        property.setValue(getItems(property)[index]);
71    }
72
73    @Override
74    public void setText(Property property, String text) throws Exception {
75        property.setValue(text);
76    }
77}
78