001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.KeyEvent;
025: import java.awt.event.MouseEvent;
026: import java.util.Arrays;
027: import javax.swing.event.CellEditorListener;
028: import javax.swing.event.ChangeEvent;
029: import javax.swing.table.BasicSwingTableTestCase;
030:
031: public class DefaultCellEditorTest extends BasicSwingTableTestCase {
032: private DefaultCellEditor editor;
033:
034: public DefaultCellEditorTest(final String name) {
035: super (name);
036: }
037:
038: @Override
039: protected void tearDown() throws Exception {
040: editor = null;
041: }
042:
043: public void testDefaultCellEditor() throws Exception {
044: JTextField field = new JTextField();
045: editor = new DefaultCellEditor(field);
046: assertEquals(field, editor.editorComponent);
047: assertNotNull(editor.delegate);
048: assertEquals(2, editor.clickCountToStart);
049: assertEquals("", editor.delegate.getCellEditorValue());
050: }
051:
052: @SuppressWarnings("deprecation")
053: public void testEditorDelegate_TextField() throws Exception {
054: JTextField field = new JTextField();
055: editor = new DefaultCellEditor(field);
056: assertTrue(Arrays.asList(field.getActionListeners()).contains(
057: editor.delegate));
058: assertEquals(2, editor.getClickCountToStart());
059: assertEquals(field.getText(), editor.delegate
060: .getCellEditorValue());
061: assertEquals(field.getText(), editor.getCellEditorValue());
062: field.setText("text1");
063: assertEquals("text1", editor.delegate.getCellEditorValue());
064: assertEquals("text1", editor.getCellEditorValue());
065: editor.delegate.setValue("text2");
066: assertEquals("text2", editor.delegate.getCellEditorValue());
067: assertEquals("text2", editor.getCellEditorValue());
068: assertEquals(field.getText(), editor.getCellEditorValue());
069: editor.delegate.setValue(new Integer(4));
070: assertEquals("4", editor.delegate.getCellEditorValue());
071: assertEquals("4", editor.getCellEditorValue());
072: assertEquals(field.getText(), editor.getCellEditorValue());
073: assertTrue(editor.delegate.isCellEditable(null));
074: assertTrue(editor.isCellEditable(null));
075: assertTrue(editor.delegate.isCellEditable(new MouseEvent(field,
076: MouseEvent.BUTTON2, 0, 0, 0, 0, 2, true)));
077: assertFalse(editor.delegate.isCellEditable(new MouseEvent(
078: field, MouseEvent.BUTTON2, 0, 0, 0, 0, 1, true)));
079: assertTrue(editor.isCellEditable(null));
080: assertTrue(editor.delegate.isCellEditable(new KeyEvent(field,
081: KeyEvent.KEY_RELEASED, 0, 0, 0)));
082: assertTrue(editor.isCellEditable(new KeyEvent(field,
083: KeyEvent.KEY_TYPED, 0, 0, 0)));
084: assertTrue(editor.delegate.shouldSelectCell(null));
085: assertTrue(editor.shouldSelectCell(null));
086: TestCellEditorListener listener = new TestCellEditorListener();
087: editor.addCellEditorListener(listener);
088: assertTrue(editor.delegate.startCellEditing(null));
089: assertFalse(listener.isOccured());
090: editor.delegate.setValue("any");
091: assertTrue(editor.delegate.stopCellEditing());
092: assertTrue(listener.isOccured(TestCellEditorListener.STOPPPED));
093: assertEquals("any", editor.getCellEditorValue());
094: listener.reset();
095: editor.delegate.setValue("another");
096: editor.delegate.cancelCellEditing();
097: assertTrue(listener.isOccured(TestCellEditorListener.CANCELED));
098: assertEquals("another", editor.getCellEditorValue());
099: listener.reset();
100: field.fireActionPerformed();
101: assertTrue(listener.isOccured(TestCellEditorListener.STOPPPED));
102: }
103:
104: public void testEditorDelegate_CheckBox() throws Exception {
105: JCheckBox check = new JCheckBox();
106: editor = new DefaultCellEditor(check);
107: assertTrue(Arrays.asList(check.getActionListeners()).contains(
108: editor.delegate));
109: assertEquals(1, editor.getClickCountToStart());
110: assertEquals(Boolean.FALSE, editor.delegate
111: .getCellEditorValue());
112: assertEquals(Boolean.FALSE, editor.getCellEditorValue());
113: check.setSelected(true);
114: assertEquals(Boolean.TRUE, editor.delegate.getCellEditorValue());
115: assertEquals(Boolean.TRUE, editor.getCellEditorValue());
116: editor.delegate.setValue("text2");
117: assertEquals(Boolean.FALSE, editor.delegate
118: .getCellEditorValue());
119: editor.delegate.setValue(Boolean.TRUE);
120: assertEquals(Boolean.TRUE, editor.delegate.getCellEditorValue());
121: assertEquals(Boolean.TRUE, editor.getCellEditorValue());
122: assertTrue(check.isSelected());
123: assertTrue(editor.delegate.isCellEditable(null));
124: assertTrue(editor.isCellEditable(null));
125: assertTrue(editor.delegate.isCellEditable(new MouseEvent(check,
126: MouseEvent.BUTTON2, 0, 0, 0, 0, 1, true)));
127: assertFalse(editor.delegate.isCellEditable(new MouseEvent(
128: check, MouseEvent.BUTTON2, 0, 0, 0, 0, 0, true)));
129: assertTrue(editor.delegate.shouldSelectCell(null));
130: assertTrue(editor.shouldSelectCell(null));
131: TestCellEditorListener listener = new TestCellEditorListener();
132: editor.addCellEditorListener(listener);
133: assertTrue(editor.delegate.startCellEditing(null));
134: assertFalse(listener.isOccured());
135: editor.delegate.setValue(Boolean.TRUE);
136: assertTrue(editor.delegate.stopCellEditing());
137: assertTrue(listener.isOccured(TestCellEditorListener.STOPPPED));
138: assertEquals(Boolean.TRUE, editor.getCellEditorValue());
139: listener.reset();
140: editor.delegate.setValue("any");
141: editor.delegate.cancelCellEditing();
142: assertTrue(listener.isOccured(TestCellEditorListener.CANCELED));
143: assertEquals(Boolean.FALSE, editor.getCellEditorValue());
144: listener.reset();
145: check.fireActionPerformed(new ActionEvent(this , 0, "cmd"));
146: assertTrue(listener.isOccured(TestCellEditorListener.STOPPPED));
147: }
148:
149: public void testEditorDelegate_ComboBox() throws Exception {
150: JComboBox cb = new JComboBox();
151: editor = new DefaultCellEditor(cb);
152: assertTrue(Arrays.asList(cb.getActionListeners()).contains(
153: editor.delegate));
154: assertFalse(Arrays.asList(cb.getItemListeners()).contains(
155: editor.delegate));
156: assertEquals(1, editor.getClickCountToStart());
157: assertNull(editor.delegate.getCellEditorValue());
158: assertTrue(editor.delegate.isCellEditable(null));
159: assertTrue(editor.delegate.shouldSelectCell(null));
160: cb.getModel().setSelectedItem("selected");
161: assertEquals("selected", cb.getSelectedItem());
162: assertEquals("selected", editor.delegate.getCellEditorValue());
163: assertEquals("selected", editor.getCellEditorValue());
164: editor.delegate.setValue("any");
165: assertEquals("selected", editor.delegate.getCellEditorValue());
166: ((DefaultComboBoxModel) cb.getModel()).addElement("elem1");
167: ((DefaultComboBoxModel) cb.getModel()).addElement("elem2");
168: assertEquals("selected", editor.delegate.getCellEditorValue());
169: editor.delegate.setValue("elem1");
170: assertEquals("elem1", editor.delegate.getCellEditorValue());
171: assertEquals("elem1", editor.getCellEditorValue());
172: assertTrue(editor.delegate.isCellEditable(null));
173: assertTrue(editor.isCellEditable(null));
174: assertTrue(editor.delegate.isCellEditable(new MouseEvent(cb,
175: MouseEvent.BUTTON2, 0, 0, 0, 0, 1, true)));
176: assertFalse(editor.delegate.isCellEditable(new MouseEvent(cb,
177: MouseEvent.BUTTON2, 0, 0, 0, 0, 0, true)));
178: assertTrue(editor.delegate.shouldSelectCell(null));
179: assertTrue(editor.shouldSelectCell(null));
180: TestCellEditorListener listener = new TestCellEditorListener();
181: editor.addCellEditorListener(listener);
182: assertTrue(editor.delegate.startCellEditing(null));
183: assertFalse(listener.isOccured());
184: editor.delegate.setValue("elem2");
185: assertTrue(editor.delegate.stopCellEditing());
186: assertTrue(listener.isOccured(TestCellEditorListener.STOPPPED));
187: assertEquals("elem2", editor.getCellEditorValue());
188: listener.reset();
189: editor.delegate.setValue("elem1");
190: editor.delegate.cancelCellEditing();
191: assertTrue(listener.isOccured(TestCellEditorListener.CANCELED));
192: assertEquals("elem1", editor.getCellEditorValue());
193: listener.reset();
194: cb.fireActionEvent();
195: assertTrue(listener.isOccured(TestCellEditorListener.STOPPPED));
196: listener.reset();
197: cb.fireItemStateChanged(new ItemEvent(cb, 0, "elem2", 0));
198: assertFalse(listener.isOccured());
199: }
200:
201: public void testGetComponent() throws Exception {
202: JComboBox cb = new JComboBox();
203: editor = new DefaultCellEditor(cb);
204: assertEquals(cb, editor.getComponent());
205: }
206:
207: public void testGetSetClickCountToStart() throws Exception {
208: editor = new DefaultCellEditor(new JTextField());
209: assertEquals(2, editor.getClickCountToStart());
210: editor = new DefaultCellEditor(new JCheckBox());
211: assertEquals(1, editor.getClickCountToStart());
212: editor = new DefaultCellEditor(new JComboBox());
213: assertEquals(1, editor.getClickCountToStart());
214: editor.setClickCountToStart(-5);
215: assertEquals(-5, editor.getClickCountToStart());
216: editor.setClickCountToStart(10);
217: assertEquals(10, editor.getClickCountToStart());
218: }
219:
220: public void testGetTreeCellEditorComponent() throws Exception {
221: editor = new DefaultCellEditor(new JTextField());
222: assertEquals(editor.getComponent(), editor
223: .getTreeCellEditorComponent(new JTree(), "any", false,
224: false, false, 0));
225: assertEquals("any", editor.getCellEditorValue());
226: }
227:
228: public void testGetTableCellEditorComponent() throws Exception {
229: editor = new DefaultCellEditor(new JTextField());
230: assertEquals(editor.getComponent(), editor
231: .getTableCellEditorComponent(new JTable(), "any",
232: false, 0, 0));
233: assertEquals("any", editor.getCellEditorValue());
234: }
235:
236: private class TestCellEditorListener implements CellEditorListener {
237: public static final int CANCELED = 1;
238:
239: public static final int STOPPPED = 2;
240:
241: private ChangeEvent event;
242:
243: private int eventType = -1;
244:
245: public void editingCanceled(final ChangeEvent e) {
246: event = e;
247: eventType = CANCELED;
248: }
249:
250: public void editingStopped(ChangeEvent e) {
251: event = e;
252: eventType = STOPPPED;
253: }
254:
255: public void reset() {
256: event = null;
257: eventType = -1;
258: }
259:
260: public boolean isOccured(final int expectedType) {
261: return isOccured() && eventType == expectedType;
262: }
263:
264: public boolean isOccured() {
265: return event != null;
266: }
267: }
268: }
|