01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.nls;
11:
12: import org.eclipse.core.runtime.Assert;
13:
14: import org.eclipse.swt.widgets.Composite;
15: import org.eclipse.swt.widgets.Control;
16:
17: import org.eclipse.jface.viewers.CellEditor;
18:
19: public class MultiStateCellEditor extends CellEditor {
20:
21: private int fValue;
22: private final int fStateCount;
23:
24: /**
25: * @param stateCount must be > 1
26: * @param initialValue initialValue
27: */
28: public MultiStateCellEditor(Composite parent, int stateCount,
29: int initialValue) {
30: super (parent);
31: Assert.isTrue(stateCount > 1, "incorrect state count"); //$NON-NLS-1$
32: fStateCount = stateCount;
33:
34: Assert.isTrue(initialValue >= 0 && initialValue < stateCount,
35: "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,
52: "incorrect initial value"); //$NON-NLS-1$
53: return (currentValue + 1) % stateCount;
54: }
55:
56: /*
57: * @see org.eclipse.jface.viewers.CellEditor#createControl(org.eclipse.swt.widgets.Composite)
58: */
59: protected Control createControl(Composite parent) {
60: return null;
61: }
62:
63: /*
64: * @see org.eclipse.jface.viewers.CellEditor#doGetValue()
65: * @return the Integer value
66: */
67: protected Object doGetValue() {
68: return new Integer(fValue);
69: }
70:
71: /*
72: * @see org.eclipse.jface.viewers.CellEditor#doSetFocus()
73: */
74: protected void doSetFocus() {
75: // Ignore
76: }
77:
78: /*
79: * @see org.eclipse.jface.viewers.CellEditor#doSetValue(java.lang.Object)
80: * @param value an Integer value
81: * must be >=0 and < stateCount (value passed in the constructor)
82: */
83: protected void doSetValue(Object value) {
84: Assert
85: .isTrue(value instanceof Integer,
86: "value must be Integer"); //$NON-NLS-1$
87: fValue = ((Integer) value).intValue();
88: Assert.isTrue(fValue >= 0 && fValue < fStateCount,
89: "invalid value"); //$NON-NLS-1$
90: }
91: }
|