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.wb.internal.core.DesignerPlugin; 14 import org.eclipse.wb.internal.core.model.property.Property; 15 import org.eclipse.wb.internal.core.model.property.table.PropertyTable; 16 import org.eclipse.wb.internal.core.utils.ui.DrawUtils; 17 18 import org.eclipse.swt.graphics.GC; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.graphics.Point; 21 22 /** 23 * The {@link PropertyEditor} for <code>Boolean</code>. 24 * 25 * @author scheglov_ke 26 * @coverage core.model.property.editor 27 */ 28 public final class BooleanObjectPropertyEditor extends PropertyEditor { 29 private static final Image m_nullImage = DesignerPlugin.getImage("properties/BooleanNull.png"); 30 private static final Image m_trueImage = DesignerPlugin.getImage("properties/true.png"); 31 private static final Image m_falseImage = DesignerPlugin.getImage("properties/false.png"); 32 //////////////////////////////////////////////////////////////////////////// 33 // 34 // Instance 35 // 36 //////////////////////////////////////////////////////////////////////////// 37 public static final PropertyEditor INSTANCE = new BooleanObjectPropertyEditor(); 38 39 private BooleanObjectPropertyEditor() { 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, text, image); 55 } 56 if (value == null) { 57 Image image = m_nullImage; 58 String text = "null"; 59 paint(gc, x, y, width, height, text, image); 60 } 61 } 62 63 private void paint(GC gc, int x, int y, int width, int height, String text, Image image) { 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 { 74 DrawUtils.drawStringCV(gc, text, x, y, width, height); 75 } 76 } 77 78 //////////////////////////////////////////////////////////////////////////// 79 // 80 // Editing 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 < m_trueImage.getBounds().width + 2) { 88 invertValue(property); 89 } 90 // don't activate 91 return false; 92 } 93 94 @Override 95 public void doubleClick(Property property, Point location) throws Exception { 96 invertValue(property); 97 } 98 99 /** 100 * Inverts the value of given boolean {@link Property}. 101 */ 102 private void invertValue(Property property) throws Exception { 103 Object value = property.getValue(); 104 // null 105 if (value == null) { 106 property.setValue(true); 107 return; 108 } 109 // boolean 110 if (value instanceof Boolean) { 111 boolean booleanValue = ((Boolean) value).booleanValue(); 112 property.setValue(!booleanValue); 113 return; 114 } 115 // unknown 116 property.setValue(true); 117 } 118 }