Home | History | Annotate | Download | only in editor
      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  *******************************************************************************/
     11 package org.eclipse.wb.internal.core.model.property.editor;
     12 
     13 import org.eclipse.swt.graphics.GC;
     14 import org.eclipse.swt.graphics.Image;
     15 import org.eclipse.swt.graphics.Point;
     16 import org.eclipse.wb.internal.core.DesignerPlugin;
     17 import org.eclipse.wb.internal.core.model.property.Property;
     18 import org.eclipse.wb.internal.core.model.property.table.PropertyTable;
     19 import org.eclipse.wb.internal.core.utils.ui.DrawUtils;
     20 
     21 /**
     22  * The {@link PropertyEditor} for <code>boolean</code>.
     23  *
     24  * @author scheglov_ke
     25  * @coverage core.model.property.editor
     26  */
     27 public final class BooleanPropertyEditor extends PropertyEditor {
     28   private static final Image m_trueImage = DesignerPlugin.getImage("properties/true.png");
     29   private static final Image m_falseImage = DesignerPlugin.getImage("properties/false.png");
     30   private static final Image m_unknownImage =
     31           DesignerPlugin.getImage("properties/BooleanUnknown.png");
     32   ////////////////////////////////////////////////////////////////////////////
     33   //
     34   // Instance
     35   //
     36   ////////////////////////////////////////////////////////////////////////////
     37   public static final PropertyEditor INSTANCE = new BooleanPropertyEditor();
     38 
     39   private BooleanPropertyEditor() {
     40   }
     41 
     42   ////////////////////////////////////////////////////////////////////////////
     43   //
     44   // Presentation
     45   //
     46   ////////////////////////////////////////////////////////////////////////////
     47   @Override
     48   public void paint(Property property, GC gc, int x, int y, int width, int height) throws Exception {
     49     Object value = property.getValue();
     50     if (value instanceof Boolean) {
     51       boolean booleanValue = ((Boolean) value).booleanValue();
     52       Image image = booleanValue ? m_trueImage : m_falseImage;
     53       String text = Boolean.toString(booleanValue);
     54       paint(gc, x, y, width, height, image, text);
     55     } else {
     56       paint(gc, x, y, width, height, m_unknownImage, "unknown");
     57     }
     58   }
     59 
     60   /**
     61    * Paints {@link Image} and text.
     62    */
     63   private void paint(GC gc, int x, int y, int width, int height, Image image, String text) {
     64     // draw image
     65     {
     66       DrawUtils.drawImageCV(gc, image, x, y, height);
     67       // prepare new position/width
     68       int imageWidth = image.getBounds().width + 2;
     69       x += imageWidth;
     70       width -= imageWidth;
     71     }
     72     // draw text
     73     DrawUtils.drawStringCV(gc, text, x, y, width, height);
     74   }
     75 
     76   ////////////////////////////////////////////////////////////////////////////
     77   //
     78   // Editing
     79   //
     80   ////////////////////////////////////////////////////////////////////////////
     81   @Override
     82   public boolean activate(PropertyTable propertyTable, Property property, Point location)
     83       throws Exception {
     84     // check that user clicked on image
     85     if (location == null || location.x < m_trueImage.getBounds().width + 2) {
     86       invertValue(property);
     87     }
     88     // don't activate
     89     return false;
     90   }
     91 
     92   @Override
     93   public void doubleClick(Property property, Point location) throws Exception {
     94     invertValue(property);
     95   }
     96 
     97   /**
     98    * Inverts the value of given boolean {@link Property}.
     99    */
    100   private void invertValue(Property property) throws Exception {
    101     // prepare current boolean value
    102     boolean booleanValue = false;
    103     {
    104       Object value = property.getValue();
    105       if (value instanceof Boolean) {
    106         booleanValue = ((Boolean) value).booleanValue();
    107       }
    108     }
    109     // set inverted value
    110     property.setValue(!booleanValue);
    111   }
    112 }