KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > MultiStateCellEditor


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.jdt.internal.ui.refactoring.nls;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17
18 import org.eclipse.jface.viewers.CellEditor;
19
20
21 public class MultiStateCellEditor extends CellEditor {
22     
23     private int fValue;
24     private final int fStateCount;
25     
26     /**
27      * @param stateCount must be > 1
28      * @param initialValue initialValue
29      */

30     public MultiStateCellEditor(Composite parent, int stateCount, int initialValue) {
31         super(parent);
32         Assert.isTrue(stateCount > 1, "incorrect state count"); //$NON-NLS-1$
33
fStateCount= stateCount;
34         
35         Assert.isTrue(initialValue >= 0 && initialValue < stateCount, "incorrect initial value"); //$NON-NLS-1$
36
fValue= initialValue;
37         
38         setValueValid(true);
39     }
40
41     /*
42      * @see org.eclipse.jface.viewers.CellEditor#activate()
43      */

44     public void activate() {
45         fValue= getNextValue(fStateCount, fValue);
46         fireApplyEditorValue();
47     }
48     
49     public static int getNextValue(int stateCount, int currentValue){
50         Assert.isTrue(stateCount > 1, "incorrect state count"); //$NON-NLS-1$
51
Assert.isTrue(currentValue >= 0 && currentValue < stateCount, "incorrect initial value"); //$NON-NLS-1$
52
return (currentValue + 1) % stateCount;
53     }
54
55     /*
56      * @see org.eclipse.jface.viewers.CellEditor#createControl(org.eclipse.swt.widgets.Composite)
57      */

58     protected Control createControl(Composite parent) {
59         return null;
60     }
61
62     /*
63      * @see org.eclipse.jface.viewers.CellEditor#doGetValue()
64      * @return the Integer value
65      */

66     protected Object JavaDoc doGetValue() {
67         return new Integer JavaDoc(fValue);
68     }
69
70     /*
71      * @see org.eclipse.jface.viewers.CellEditor#doSetFocus()
72      */

73     protected void doSetFocus() {
74         // Ignore
75
}
76
77     /*
78      * @see org.eclipse.jface.viewers.CellEditor#doSetValue(java.lang.Object)
79      * @param value an Integer value
80      * must be >=0 and < stateCount (value passed in the constructor)
81      */

82     protected void doSetValue(Object JavaDoc value) {
83         Assert.isTrue(value instanceof Integer JavaDoc, "value must be Integer"); //$NON-NLS-1$
84
fValue = ((Integer JavaDoc) value).intValue();
85         Assert.isTrue(fValue >= 0 && fValue < fStateCount, "invalid value"); //$NON-NLS-1$
86
}
87 }
88
Popular Tags