KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > CheckboxCellEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.viewers;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17
18 /**
19  * A cell editor that manages a checkbox.
20  * The cell editor's value is a boolean.
21  * <p>
22  * This class may be instantiated; it is not intended to be subclassed.
23  * </p>
24  * <p>
25  * Note that this implementation simply fakes it and does does not create
26  * any new controls. The mere activation of this editor means that the value
27  * of the check box is being toggled by the end users; the listener method
28  * <code>applyEditorValue</code> is immediately called to signal the change.
29  * </p>
30  */

31 public class CheckboxCellEditor extends CellEditor {
32
33     /**
34      * The checkbox value.
35      */

36     /* package */
37     boolean value = false;
38
39     /**
40      * Default CheckboxCellEditor style
41      */

42     private static final int defaultStyle = SWT.NONE;
43
44     /**
45      * Creates a new checkbox cell editor with no control
46      * @since 2.1
47      */

48     public CheckboxCellEditor() {
49         setStyle(defaultStyle);
50     }
51
52     /**
53      * Creates a new checkbox cell editor parented under the given control.
54      * The cell editor value is a boolean value, which is initially <code>false</code>.
55      * Initially, the cell editor has no cell validator.
56      *
57      * @param parent the parent control
58      */

59     public CheckboxCellEditor(Composite parent) {
60         this(parent, defaultStyle);
61     }
62
63     /**
64      * Creates a new checkbox cell editor parented under the given control.
65      * The cell editor value is a boolean value, which is initially <code>false</code>.
66      * Initially, the cell editor has no cell validator.
67      *
68      * @param parent the parent control
69      * @param style the style bits
70      * @since 2.1
71      */

72     public CheckboxCellEditor(Composite parent, int style) {
73         super(parent, style);
74     }
75
76     /**
77      * The <code>CheckboxCellEditor</code> implementation of
78      * this <code>CellEditor</code> framework method simulates
79      * the toggling of the checkbox control and notifies
80      * listeners with <code>ICellEditorListener.applyEditorValue</code>.
81      */

82     public void activate() {
83         value = !value;
84         fireApplyEditorValue();
85     }
86
87     /**
88      * The <code>CheckboxCellEditor</code> implementation of
89      * this <code>CellEditor</code> framework method does
90      * nothing and returns <code>null</code>.
91      */

92     protected Control createControl(Composite parent) {
93         return null;
94     }
95
96     /**
97      * The <code>CheckboxCellEditor</code> implementation of
98      * this <code>CellEditor</code> framework method returns
99      * the checkbox setting wrapped as a <code>Boolean</code>.
100      *
101      * @return the Boolean checkbox value
102      */

103     protected Object JavaDoc doGetValue() {
104         return value ? Boolean.TRUE : Boolean.FALSE;
105     }
106
107     /* (non-Javadoc)
108      * Method declared on CellEditor.
109      */

110     protected void doSetFocus() {
111         // Ignore
112
}
113
114     /**
115      * The <code>CheckboxCellEditor</code> implementation of
116      * this <code>CellEditor</code> framework method accepts
117      * a value wrapped as a <code>Boolean</code>.
118      *
119      * @param value a Boolean value
120      */

121     protected void doSetValue(Object JavaDoc value) {
122         Assert.isTrue(value instanceof Boolean JavaDoc);
123         this.value = ((Boolean JavaDoc) value).booleanValue();
124     }
125     
126     public void activate(ColumnViewerEditorActivationEvent activationEvent) {
127         if (activationEvent.eventType != ColumnViewerEditorActivationEvent.TRAVERSAL) {
128             super.activate(activationEvent);
129         }
130     }
131 }
132
Popular Tags