aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/properties/BooleanXmlPropertyEditor.java
blob: d6ff4d51d6ff2ad41c03aac6da2736e34b6dded3 (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.properties;

import static com.android.SdkConstants.VALUE_FALSE;
import static com.android.SdkConstants.VALUE_TRUE;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.wb.internal.core.DesignerPlugin;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
import org.eclipse.wb.internal.core.utils.ui.DrawUtils;

/**
 * Handle an XML property which represents booleans.
 *
 * Similar to the WindowBuilder PropertyEditor, but operates on Strings rather
 * than Booleans (which means it is a tri-state boolean: true, false, not set)
 */
public class BooleanXmlPropertyEditor extends XmlPropertyEditor {
    public static final BooleanXmlPropertyEditor INSTANCE = new BooleanXmlPropertyEditor();

    private static final Image mTrueImage = DesignerPlugin.getImage("properties/true.png");
    private static final Image mFalseImage = DesignerPlugin.getImage("properties/false.png");
    private static final Image mNullImage =
            DesignerPlugin.getImage("properties/BooleanNull.png");
    private static final Image mUnknownImage =
            DesignerPlugin.getImage("properties/BooleanUnknown.png");

    private BooleanXmlPropertyEditor() {
    }

    @Override
    public void paint(Property property, GC gc, int x, int y, int width, int height)
            throws Exception {
        Object value = property.getValue();
        assert value == null || value instanceof String;
        if (value == null || value instanceof String) {
            String text = (String) value;
            Image image;
            if (VALUE_TRUE.equals(text)) {
                image = mTrueImage;
            } else if (VALUE_FALSE.equals(text)) {
                image = mFalseImage;
            } else if (text == null) {
                image = mNullImage;
            } else {
                // Probably something like a reference, e.g. @boolean/foo
                image = mUnknownImage;
            }

            // draw image
            DrawUtils.drawImageCV(gc, image, x, y, height);

            // prepare new position/width
            int imageWidth = image.getBounds().width + 2;
            width -= imageWidth;

            // draw text
            if (text != null) {
                x += imageWidth;
                DrawUtils.drawStringCV(gc, text, x, y, width, height);
            }
        }
    }

    @Override
    public boolean activate(PropertyTable propertyTable, Property property, Point location)
            throws Exception {
        // check that user clicked on image
        if (location == null || location.x < mTrueImage.getBounds().width + 2) {
            cycleValue(property);
        }
        // don't activate
        return false;
    }

    @Override
    public void doubleClick(Property property, Point location) throws Exception {
        cycleValue(property);
    }

    /**
     * Cycles through the values
     */
    private void cycleValue(Property property) throws Exception {
        Object value = property.getValue();
        if (value == null || value instanceof String) {
            // Cycle null => true => false => null
            String text = (String) value;
            if (VALUE_TRUE.equals(text)) {
                property.setValue(VALUE_FALSE);
            } else if (VALUE_FALSE.equals(text)) {
                property.setValue(null);
            } else {
                property.setValue(VALUE_TRUE);
            }
        } else {
            assert false;
        }
    }
}