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.tree;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.Rectangle;
026: import java.awt.event.InputEvent;
027: import java.awt.event.KeyEvent;
028: import java.awt.event.MouseEvent;
029: import java.util.EventObject;
030: import javax.swing.BasicSwingTestCase;
031: import javax.swing.BorderFactory;
032: import javax.swing.DefaultCellEditor;
033: import javax.swing.JTextField;
034: import javax.swing.JTree;
035: import javax.swing.UIManager;
036: import javax.swing.border.Border;
037: import javax.swing.event.CellEditorListener;
038: import javax.swing.event.ChangeEvent;
039: import javax.swing.event.TreeSelectionEvent;
040: import javax.swing.plaf.ColorUIResource;
041: import javax.swing.tree.DefaultTreeCellEditor.DefaultTextField;
042:
043: public class DefaultTreeCellEditorTest extends BasicSwingTestCase {
044: private DefaultTreeCellEditor editor;
045:
046: private DefaultTreeCellRenderer renderer;
047:
048: private JTree tree;
049:
050: public DefaultTreeCellEditorTest(final String name) {
051: super (name);
052: }
053:
054: @Override
055: protected void setUp() throws Exception {
056: tree = new JTree();
057: renderer = new DefaultTreeCellRenderer();
058: editor = new DefaultTreeCellEditor(tree, renderer);
059: }
060:
061: @Override
062: protected void tearDown() throws Exception {
063: editor = null;
064: tree = null;
065: renderer = null;
066: }
067:
068: public void testDefaultTextField() throws Exception {
069: editor.getTreeCellEditorComponent(tree, "any", false, true,
070: false, 0);
071: assertTrue(editor.editingComponent instanceof DefaultTreeCellEditor.DefaultTextField);
072: DefaultTreeCellEditor.DefaultTextField defaultTextField = (DefaultTreeCellEditor.DefaultTextField) editor.editingComponent;
073: assertEquals(UIManager.getBorder("Tree.editorBorder"),
074: defaultTextField.border);
075: Border b = BorderFactory.createBevelBorder(0);
076: defaultTextField = editor.new DefaultTextField(b);
077: assertSame(b, defaultTextField.border);
078: }
079:
080: public void testDefaultTextField_getSetBorder() throws Exception {
081: Border b = BorderFactory.createBevelBorder(0);
082: DefaultTreeCellEditor.DefaultTextField defaultTextField = editor.new DefaultTextField(
083: b);
084: assertSame(b, defaultTextField.getBorder());
085: propertyChangeController = new PropertyChangeController();
086: defaultTextField
087: .addPropertyChangeListener(propertyChangeController);
088: b = BorderFactory.createEmptyBorder();
089: defaultTextField.setBorder(b);
090: assertSame(b, defaultTextField.getBorder());
091: assertTrue(propertyChangeController.isChanged("border"));
092: }
093:
094: public void testDefaultTextField_getFont() throws Exception {
095: DefaultTreeCellEditor.DefaultTextField defaultTextField = editor.new DefaultTextField(
096: BorderFactory.createBevelBorder(0));
097: assertEquals(UIManager.getFont("TextField.font"),
098: defaultTextField.getFont());
099: Font f = new Font("font", 0, 30);
100: defaultTextField.setFont(f);
101: assertEquals(f, defaultTextField.getFont());
102: defaultTextField.setFont(null);
103: assertNull(defaultTextField.getFont());
104: editor.setFont(f);
105: assertNull(defaultTextField.getFont());
106: editor.renderer.setFont(f);
107: assertNull(defaultTextField.getFont());
108: editor.tree.setFont(f);
109: assertNull(defaultTextField.getFont());
110: }
111:
112: public void testDefaultTextField_getPreferredSize()
113: throws Exception {
114: DefaultTreeCellEditor.DefaultTextField defaultTextField = editor.new DefaultTextField(
115: BorderFactory.createBevelBorder(0));
116: defaultTextField.setText("value");
117: assertEquals(new Dimension(new JTextField("value")
118: .getPreferredSize().width, 0), defaultTextField
119: .getPreferredSize());
120: editor.renderer.setPreferredSize(new Dimension(100, 200));
121: assertEquals(new Dimension(new JTextField("value")
122: .getPreferredSize().width, 200), defaultTextField
123: .getPreferredSize());
124: }
125:
126: public void testEditorContainer() throws Exception {
127: DefaultTreeCellEditor.EditorContainer container = editor.new EditorContainer();
128: assertEquals(0, container.getComponentCount());
129: assertEquals(new Rectangle(), container.getBounds());
130: }
131:
132: public void testEditorContainer_getPreferredSize() throws Exception {
133: DefaultTreeCellEditor.EditorContainer container = editor.new EditorContainer();
134: assertEquals(new Dimension(), container.getPreferredSize());
135: editor.editingComponent = new JTextField("a");
136: assertEquals(new Dimension(100, editor.editingComponent
137: .getPreferredSize().height), container
138: .getPreferredSize());
139: editor.offset = 1000;
140: assertEquals(new Dimension(1000 + editor.editingComponent
141: .getPreferredSize().width + 5, editor.editingComponent
142: .getPreferredSize().height), container
143: .getPreferredSize());
144: editor.renderer.setPreferredSize(new Dimension(2000, 10));
145: assertEquals(new Dimension(1000 + editor.editingComponent
146: .getPreferredSize().width + 5, editor.editingComponent
147: .getPreferredSize().height), container
148: .getPreferredSize());
149: editor.renderer.setPreferredSize(new Dimension(2000, 30));
150: assertEquals(new Dimension(1000 + editor.editingComponent
151: .getPreferredSize().width + 5, 30), container
152: .getPreferredSize());
153: }
154:
155: public void testEditorContainer_doLayout() throws Exception {
156: DefaultTreeCellEditor.EditorContainer container = editor.new EditorContainer();
157: editor.editingComponent = new JTextField("any");
158: assertEquals(new Rectangle(0, 0, 0, 0), editor.editingComponent
159: .getBounds());
160: container.doLayout();
161: assertEquals(new Rectangle(0, 0, 0, 0), editor.editingComponent
162: .getBounds());
163: editor.offset = 10;
164: container.doLayout();
165: assertEquals(new Rectangle(10, 0, -10, 0),
166: editor.editingComponent.getBounds());
167: container.setBounds(0, 0, 100, 100);
168: container.doLayout();
169: assertEquals(new Rectangle(10, 0, 90, 100),
170: editor.editingComponent.getBounds());
171: }
172:
173: public void testDefaultTreeCellEditor() throws Exception {
174: assertNull(editor.borderSelectionColor);
175: assertFalse(editor.canEdit);
176: assertNull(editor.editingComponent);
177: assertTrue(editor.editingContainer instanceof DefaultTreeCellEditor.EditorContainer);
178: assertNull(editor.editingIcon);
179: assertNull(editor.font);
180: assertNull(editor.lastPath);
181: assertEquals(0, editor.lastRow);
182: assertEquals(0, editor.offset);
183: assertTrue(editor.realEditor instanceof DefaultCellEditor);
184: assertSame(renderer, editor.renderer);
185: assertNull(editor.timer);
186: assertSame(tree, editor.tree);
187: assertEquals(1, ((DefaultCellEditor) editor.realEditor)
188: .getClickCountToStart());
189: DefaultCellEditor realEditor = new DefaultCellEditor(
190: new JTextField());
191: realEditor.setClickCountToStart(4);
192: editor = new DefaultTreeCellEditor(tree, renderer, realEditor);
193: assertSame(realEditor, editor.realEditor);
194: assertEquals(4, ((DefaultCellEditor) editor.realEditor)
195: .getClickCountToStart());
196: }
197:
198: public void testGetSetBorderSelectionColor() throws Exception {
199: assertNull(editor.getBorderSelectionColor());
200: Color c = Color.RED;
201: editor.setBorderSelectionColor(c);
202: assertSame(c, editor.getBorderSelectionColor());
203: c = new ColorUIResource(Color.BLUE);
204: editor.setBorderSelectionColor(c);
205: assertSame(c, editor.getBorderSelectionColor());
206: }
207:
208: public void testGetSetFont() throws Exception {
209: assertNull(editor.getFont());
210: Font font = new Font("font", 0, 20);
211: renderer.setFont(font);
212: assertNull(editor.getFont());
213: assertSame(font, editor.getTreeCellEditorComponent(tree,
214: "value", false, false, false, 0).getFont());
215: font = new Font("font", 0, 50);
216: editor.setFont(font);
217: assertSame(font, editor.getFont());
218: assertSame(font, editor.getTreeCellEditorComponent(tree,
219: "value", false, false, false, 0).getFont());
220: }
221:
222: public void testGetTreeCellEditorComponent() throws Exception {
223: Font font = new Font("font", 0, 30);
224: tree.setFont(font);
225: assertNull(editor.editingComponent);
226: assertSame(editor.editingContainer, editor
227: .getTreeCellEditorComponent(tree, "value", false,
228: false, false, 0));
229: assertEquals(1, editor.editingContainer.getComponentCount());
230: assertSame(font, editor.editingContainer.getFont());
231: assertSame(font, editor.editingComponent.getFont());
232: assertSame(editor.realEditor.getTreeCellEditorComponent(tree,
233: "value", false, false, false, 0),
234: editor.editingContainer.getComponent(0));
235: assertSame(editor.editingContainer.getComponent(0),
236: editor.editingComponent);
237: assertTrue(editor.editingComponent instanceof DefaultTreeCellEditor.DefaultTextField);
238: DefaultTreeCellEditor.DefaultTextField textField = (DefaultTextField) editor.editingComponent;
239: assertEquals("value", textField.getText());
240: assertEquals(tree.getFont(), textField.getFont());
241: }
242:
243: public void testGetCellEditorValue() throws Exception {
244: assertEquals("", editor.getCellEditorValue());
245: editor.editingComponent = new JTextField("any");
246: assertEquals("", editor.getCellEditorValue());
247: editor.realEditor.getTreeCellEditorComponent(tree, "value",
248: false, false, false, 0);
249: assertEquals("value", editor.getCellEditorValue());
250: assertEquals("any", ((JTextField) editor.editingComponent)
251: .getText());
252: editor.getTreeCellEditorComponent(tree, "value2", false, false,
253: false, 0);
254: assertEquals("value2", editor.getCellEditorValue());
255: assertEquals("value2", ((JTextField) editor.editingComponent)
256: .getText());
257: }
258:
259: public void testIsCellEditable() throws Exception {
260: final Marker m = new Marker();
261: editor = new DefaultTreeCellEditor(tree, renderer) {
262: @Override
263: protected void prepareForEditing() {
264: m.setOccurred();
265: }
266: };
267: assertTrue(editor.isCellEditable(null));
268: assertTrue(m.isOccurred());
269: }
270:
271: public void testShouldSelectCell() throws Exception {
272: final Marker m = new Marker();
273: editor = new DefaultTreeCellEditor(tree, renderer) {
274: @Override
275: public boolean shouldSelectCell(final EventObject e) {
276: m.setOccurred();
277: m.setAuxiliary(e);
278: return true;
279: }
280: };
281: EventObject eo = new EventObject(this );
282: assertTrue(editor.shouldSelectCell(eo));
283: assertTrue(m.isOccurred());
284: assertSame(eo, m.getAuxiliary());
285: }
286:
287: public void testStopCellEditing() throws Exception {
288: editor.getTreeCellEditorComponent(tree, "value", false, false,
289: false, 0);
290: assertNotNull(editor.editingComponent);
291: assertEquals(1, editor.editingContainer.getComponentCount());
292: assertTrue(editor.stopCellEditing());
293: assertNull(editor.editingComponent);
294: assertEquals(0, editor.editingContainer.getComponentCount());
295: }
296:
297: public void testCancelCellEditing() throws Exception {
298: editor.getTreeCellEditorComponent(tree, "value", false, false,
299: false, 0);
300: assertNotNull(editor.editingComponent);
301: assertEquals(1, editor.editingContainer.getComponentCount());
302: editor.cancelCellEditing();
303: assertNull(editor.editingComponent);
304: assertEquals(0, editor.editingContainer.getComponentCount());
305: }
306:
307: public void testAddRemoveGetCellEditorListener() throws Exception {
308: assertEquals(0, editor.getCellEditorListeners().length);
309: assertEquals(0, ((DefaultCellEditor) editor.realEditor)
310: .getCellEditorListeners().length);
311: CellEditorListener l = new CellEditorListener() {
312: public void editingStopped(ChangeEvent e) {
313: }
314:
315: public void editingCanceled(ChangeEvent e) {
316: }
317: };
318: editor.addCellEditorListener(l);
319: assertEquals(1, editor.getCellEditorListeners().length);
320: assertEquals(1, ((DefaultCellEditor) editor.realEditor)
321: .getCellEditorListeners().length);
322: editor.removeCellEditorListener(l);
323: assertEquals(0, ((DefaultCellEditor) editor.realEditor)
324: .getCellEditorListeners().length);
325: }
326:
327: public void testValueChanged() throws Exception {
328: TreePath treePath = new TreePath("root");
329: editor.valueChanged(new TreeSelectionEvent(tree, treePath,
330: true, null, treePath));
331: if (isHarmony()) {
332: assertEquals(treePath, editor.lastPath);
333: } else {
334: assertNull(editor.lastPath);
335: }
336: }
337:
338: public void testActionPerformed() throws Exception {
339: editor.lastPath = new TreePath(tree.getModel().getRoot());
340: editor.lastRow = 10;
341: editor.actionPerformed(null);
342: if (isHarmony()) {
343: assertEquals(0, editor.lastRow);
344: } else {
345: assertEquals(10, editor.lastRow);
346: }
347: }
348:
349: public void testSetTree() throws Exception {
350: JTree anotherTree = new JTree();
351: assertEquals(0, anotherTree.getTreeSelectionListeners().length);
352: editor.setTree(anotherTree);
353: assertEquals(1, anotherTree.getTreeSelectionListeners().length);
354: assertSame(anotherTree, editor.tree);
355: }
356:
357: public void testShouldStartEditingTimer() throws Exception {
358: assertFalse(editor.shouldStartEditingTimer(null));
359: assertTrue(editor.shouldStartEditingTimer(new MouseEvent(tree,
360: MouseEvent.MOUSE_PRESSED, 0,
361: InputEvent.BUTTON1_DOWN_MASK, 1, 1, 1, false,
362: MouseEvent.BUTTON1)));
363: assertFalse(editor.shouldStartEditingTimer(new MouseEvent(tree,
364: MouseEvent.MOUSE_PRESSED, 0,
365: InputEvent.BUTTON1_DOWN_MASK, 1, 1, 2, false,
366: MouseEvent.BUTTON1)));
367: }
368:
369: public void testStartEditingTimer() throws Exception {
370: assertNull(editor.timer);
371: editor.startEditingTimer();
372: assertTrue(editor.timer.isRunning());
373: assertEquals(1200, editor.timer.getDelay());
374: }
375:
376: @SuppressWarnings("deprecation")
377: public void testCanEditimmediately() throws Exception {
378: assertTrue(editor.canEditImmediately(null));
379: assertTrue(editor.canEditImmediately(new MouseEvent(tree,
380: MouseEvent.MOUSE_PRESSED, 0, 0, 1, 1, 3, false,
381: MouseEvent.BUTTON1)));
382: assertTrue(editor.canEditImmediately(new MouseEvent(tree,
383: MouseEvent.MOUSE_PRESSED, 0, 0, 1, 1, 4, false,
384: MouseEvent.BUTTON1)));
385: assertFalse(editor.canEditImmediately(new MouseEvent(tree,
386: MouseEvent.MOUSE_PRESSED, 0, 0, 1, 1, 3, false,
387: MouseEvent.BUTTON2)));
388: assertFalse(editor.canEditImmediately(new MouseEvent(tree,
389: MouseEvent.MOUSE_PRESSED, 0, 0, 1, 1, 2, false,
390: MouseEvent.BUTTON1)));
391: assertFalse(editor.canEditImmediately(new KeyEvent(tree,
392: KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_1)));
393: }
394:
395: public void testInHitRegion() throws Exception {
396: assertFalse(editor.inHitRegion(0, 0));
397: assertTrue(editor.inHitRegion(1, 0));
398: if (isHarmony()) {
399: assertFalse(editor.inHitRegion(1000, 1000));
400: } else {
401: assertTrue(editor.inHitRegion(1000, 1000));
402: }
403: editor.offset = 30;
404: assertFalse(editor.inHitRegion(30, 1));
405: assertTrue(editor.inHitRegion(31, 1));
406: }
407:
408: public void testDetermineOffset() throws Exception {
409: assertEquals(0, editor.offset);
410: editor.determineOffset(tree, "value", false, false, false, 0);
411: assertEquals(20, editor.offset);
412: editor.offset = 10;
413: editor.determineOffset(tree, "value", false, false, false, 0);
414: assertEquals(20, editor.offset);
415: }
416:
417: public void testPrepareForEditing() throws Exception {
418: assertEquals(0, editor.editingContainer.getComponentCount());
419: editor.prepareForEditing();
420: assertEquals(0, editor.editingContainer.getComponentCount());
421: editor.editingComponent = new JTextField("any");
422: editor.prepareForEditing();
423: assertEquals(1, editor.editingContainer.getComponentCount());
424: }
425:
426: public void testCreateContainer() throws Exception {
427: assertNotSame(editor.createContainer(), editor
428: .createContainer());
429: assertTrue(editor.createContainer() instanceof DefaultTreeCellEditor.EditorContainer);
430: }
431:
432: public void testCreateTreeCellEditor() throws Exception {
433: assertNotSame(editor.createTreeCellEditor(), editor
434: .createTreeCellEditor());
435: assertTrue(editor.createTreeCellEditor() instanceof DefaultCellEditor);
436: assertTrue(editor.createTreeCellEditor()
437: .getTreeCellEditorComponent(tree, "any", false, true,
438: false, 0) instanceof DefaultTreeCellEditor.DefaultTextField);
439: assertEquals(1, ((DefaultCellEditor) editor
440: .createTreeCellEditor()).getClickCountToStart());
441: }
442: }
|