Home | History | Annotate | Download | only in properties
      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 
     17 package com.android.ide.eclipse.adt.internal.editors.layout.properties;
     18 
     19 import static com.android.SdkConstants.VALUE_FALSE;
     20 import static com.android.SdkConstants.VALUE_TRUE;
     21 
     22 import org.eclipse.swt.graphics.GC;
     23 import org.eclipse.swt.graphics.Image;
     24 import org.eclipse.swt.graphics.Point;
     25 import org.eclipse.wb.internal.core.DesignerPlugin;
     26 import org.eclipse.wb.internal.core.model.property.Property;
     27 import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
     28 import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
     29 
     30 /**
     31  * Handle an XML property which represents booleans.
     32  *
     33  * Similar to the WindowBuilder PropertyEditor, but operates on Strings rather
     34  * than Booleans (which means it is a tri-state boolean: true, false, not set)
     35  */
     36 public class BooleanXmlPropertyEditor extends XmlPropertyEditor {
     37     public static final BooleanXmlPropertyEditor INSTANCE = new BooleanXmlPropertyEditor();
     38 
     39     private static final Image mTrueImage = DesignerPlugin.getImage("properties/true.png");
     40     private static final Image mFalseImage = DesignerPlugin.getImage("properties/false.png");
     41     private static final Image mNullImage =
     42             DesignerPlugin.getImage("properties/BooleanNull.png");
     43     private static final Image mUnknownImage =
     44             DesignerPlugin.getImage("properties/BooleanUnknown.png");
     45 
     46     private BooleanXmlPropertyEditor() {
     47     }
     48 
     49     @Override
     50     public void paint(Property property, GC gc, int x, int y, int width, int height)
     51             throws Exception {
     52         Object value = property.getValue();
     53         assert value == null || value instanceof String;
     54         if (value == null || value instanceof String) {
     55             String text = (String) value;
     56             Image image;
     57             if (VALUE_TRUE.equals(text)) {
     58                 image = mTrueImage;
     59             } else if (VALUE_FALSE.equals(text)) {
     60                 image = mFalseImage;
     61             } else if (text == null) {
     62                 image = mNullImage;
     63             } else {
     64                 // Probably something like a reference, e.g. @boolean/foo
     65                 image = mUnknownImage;
     66             }
     67 
     68             // draw image
     69             DrawUtils.drawImageCV(gc, image, x, y, height);
     70 
     71             // prepare new position/width
     72             int imageWidth = image.getBounds().width + 2;
     73             width -= imageWidth;
     74 
     75             // draw text
     76             if (text != null) {
     77                 x += imageWidth;
     78                 DrawUtils.drawStringCV(gc, text, x, y, width, height);
     79             }
     80         }
     81     }
     82 
     83     @Override
     84     public boolean activate(PropertyTable propertyTable, Property property, Point location)
     85             throws Exception {
     86         // check that user clicked on image
     87         if (location == null || location.x < mTrueImage.getBounds().width + 2) {
     88             cycleValue(property);
     89         }
     90         // don't activate
     91         return false;
     92     }
     93 
     94     @Override
     95     public void doubleClick(Property property, Point location) throws Exception {
     96         cycleValue(property);
     97     }
     98 
     99     /**
    100      * Cycles through the values
    101      */
    102     private void cycleValue(Property property) throws Exception {
    103         Object value = property.getValue();
    104         if (value == null || value instanceof String) {
    105             // Cycle null => true => false => null
    106             String text = (String) value;
    107             if (VALUE_TRUE.equals(text)) {
    108                 property.setValue(VALUE_FALSE);
    109             } else if (VALUE_FALSE.equals(text)) {
    110                 property.setValue(null);
    111             } else {
    112                 property.setValue(VALUE_TRUE);
    113             }
    114         } else {
    115             assert false;
    116         }
    117     }
    118 }
    119