001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.viewers;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.swt.widgets.Control;
016:
017: /**
018: * A cell editor that manages a checkbox.
019: * The cell editor's value is a boolean.
020: * <p>
021: * This class may be instantiated; it is not intended to be subclassed.
022: * </p>
023: * <p>
024: * Note that this implementation simply fakes it and does does not create
025: * any new controls. The mere activation of this editor means that the value
026: * of the check box is being toggled by the end users; the listener method
027: * <code>applyEditorValue</code> is immediately called to signal the change.
028: * </p>
029: */
030: public class CheckboxCellEditor extends CellEditor {
031:
032: /**
033: * The checkbox value.
034: */
035: /* package */
036: boolean value = false;
037:
038: /**
039: * Default CheckboxCellEditor style
040: */
041: private static final int defaultStyle = SWT.NONE;
042:
043: /**
044: * Creates a new checkbox cell editor with no control
045: * @since 2.1
046: */
047: public CheckboxCellEditor() {
048: setStyle(defaultStyle);
049: }
050:
051: /**
052: * Creates a new checkbox cell editor parented under the given control.
053: * The cell editor value is a boolean value, which is initially <code>false</code>.
054: * Initially, the cell editor has no cell validator.
055: *
056: * @param parent the parent control
057: */
058: public CheckboxCellEditor(Composite parent) {
059: this (parent, defaultStyle);
060: }
061:
062: /**
063: * Creates a new checkbox cell editor parented under the given control.
064: * The cell editor value is a boolean value, which is initially <code>false</code>.
065: * Initially, the cell editor has no cell validator.
066: *
067: * @param parent the parent control
068: * @param style the style bits
069: * @since 2.1
070: */
071: public CheckboxCellEditor(Composite parent, int style) {
072: super (parent, style);
073: }
074:
075: /**
076: * The <code>CheckboxCellEditor</code> implementation of
077: * this <code>CellEditor</code> framework method simulates
078: * the toggling of the checkbox control and notifies
079: * listeners with <code>ICellEditorListener.applyEditorValue</code>.
080: */
081: public void activate() {
082: value = !value;
083: fireApplyEditorValue();
084: }
085:
086: /**
087: * The <code>CheckboxCellEditor</code> implementation of
088: * this <code>CellEditor</code> framework method does
089: * nothing and returns <code>null</code>.
090: */
091: protected Control createControl(Composite parent) {
092: return null;
093: }
094:
095: /**
096: * The <code>CheckboxCellEditor</code> implementation of
097: * this <code>CellEditor</code> framework method returns
098: * the checkbox setting wrapped as a <code>Boolean</code>.
099: *
100: * @return the Boolean checkbox value
101: */
102: protected Object doGetValue() {
103: return value ? Boolean.TRUE : Boolean.FALSE;
104: }
105:
106: /* (non-Javadoc)
107: * Method declared on CellEditor.
108: */
109: protected void doSetFocus() {
110: // Ignore
111: }
112:
113: /**
114: * The <code>CheckboxCellEditor</code> implementation of
115: * this <code>CellEditor</code> framework method accepts
116: * a value wrapped as a <code>Boolean</code>.
117: *
118: * @param value a Boolean value
119: */
120: protected void doSetValue(Object value) {
121: Assert.isTrue(value instanceof Boolean);
122: this .value = ((Boolean) value).booleanValue();
123: }
124: }
|