001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.docnavigation;
038:
039: import edu.rice.cs.util.UnexpectedException;
040: import edu.rice.cs.util.swing.Utilities;
041: import edu.rice.cs.drjava.DrJavaTestCase;
042:
043: import java.util.Enumeration;
044: import javax.swing.tree.DefaultMutableTreeNode;
045: import javax.swing.tree.TreePath;
046: import java.io.File;
047: import java.util.HashSet;
048:
049: public class JTreeSortNavigatorTest extends DrJavaTestCase {
050:
051: protected JTreeSortNavigator<DummyINavigatorItem> tree;
052: protected DefaultMutableTreeNode root;
053: protected DefaultMutableTreeNode source;
054: protected DefaultMutableTreeNode folder1;
055: protected DefaultMutableTreeNode folder2;
056: protected String projName;
057: protected DummyINavigatorItem i1, i2, i3, i4;
058:
059: protected DefaultMutableTreeNode aux;
060: protected DefaultMutableTreeNode auxFolder1;
061: protected DefaultMutableTreeNode auxFolder2;
062: protected DummyINavigatorItem auxi1, auxi2, auxi3, auxi4, auxi5;
063:
064: protected final String SOURCE_BIN_NAME = "[ Source Files ]";
065: protected final String EXTERNAL_BIN_NAME = "[ External Files ]";
066:
067: public void setUp() throws Exception {
068: super .setUp();
069:
070: Utilities.invokeAndWait(new Runnable() {
071: public void run() {
072:
073: try {
074: File f = File.createTempFile("project-", ".pjt")
075: .getCanonicalFile();
076: tree = new JTreeSortNavigator<DummyINavigatorItem>(
077: f.getCanonicalPath());
078:
079: tree.addTopLevelGroup(SOURCE_BIN_NAME,
080: new INavigatorItemFilter<INavigatorItem>() {
081: public boolean accept(INavigatorItem n) {
082: return !n.getName().startsWith(
083: "aux");
084: }
085: });
086: tree.addTopLevelGroup(EXTERNAL_BIN_NAME,
087: new INavigatorItemFilter<INavigatorItem>() {
088: public boolean accept(INavigatorItem n) {
089: return n.getName()
090: .startsWith("aux");
091: }
092: });
093:
094: i1 = new DummyINavigatorItem("item11");
095: i2 = new DummyINavigatorItem("item12");
096: i3 = new DummyINavigatorItem("item21");
097: i4 = new DummyINavigatorItem("item22");
098: tree.addDocument(i1, "folder1");
099: tree.addDocument(i2, "folder1");
100: tree.addDocument(i3, "folder2");
101: tree.addDocument(i4, "folder2");
102:
103: auxi1 = new DummyINavigatorItem("auxitem11");
104: auxi2 = new DummyINavigatorItem("auxitem12");
105: auxi3 = new DummyINavigatorItem("auxitem21");
106: auxi4 = new DummyINavigatorItem("auxitem22");
107: auxi5 = new DummyINavigatorItem("auxitem23");
108: tree.addDocument(auxi1, "auxfolder1");
109: tree.addDocument(auxi2, "auxfolder1");
110: tree.addDocument(auxi3, "auxfolder2");
111: tree.addDocument(auxi4, "auxfolder2");
112: tree.addDocument(auxi5, "auxfolder2");
113:
114: root = (DefaultMutableTreeNode) tree.getModel()
115: .getRoot();
116: source = (DefaultMutableTreeNode) root
117: .getChildAt(0);
118: folder1 = (DefaultMutableTreeNode) source
119: .getChildAt(0);
120: folder2 = (DefaultMutableTreeNode) source
121: .getChildAt(1);
122: aux = (DefaultMutableTreeNode) root.getChildAt(1);
123: auxFolder1 = (DefaultMutableTreeNode) aux
124: .getChildAt(0);
125: auxFolder2 = (DefaultMutableTreeNode) aux
126: .getChildAt(1);
127:
128: projName = root.toString();
129: } catch (Exception e) {
130: throw new UnexpectedException(e);
131: }
132: }
133: });
134: }
135:
136: public void testTraversalOps() {
137: assertEquals("doc count test", 9, tree.getDocumentCount());
138: assertSame("getFirst test", i1, tree.getFirst());
139: assertSame("getLast test", auxi5, tree.getLast());
140:
141: Utilities.invokeAndWait(new Runnable() {
142: public void run() {
143: tree.setNextChangeModelInitiated(true);
144: tree.setActiveDoc(i1);
145: }
146: });
147: assertSame("getCurrent test", i1, tree.getCurrent());
148: assertSame("getNext test 1", i2, tree.getNext(i1));
149: assertSame("getNext test 2", i3, tree.getNext(i2));
150: assertSame("getNext test 3", i4, tree.getNext(i3));
151:
152: assertSame("getPrevious test 1", i3, tree.getPrevious(i4));
153: assertSame("getPrevious test 2", i2, tree.getPrevious(i3));
154: assertSame("getPrevious test 3", i1, tree.getPrevious(i2));
155: }
156:
157: public void testGeneratePathString() {
158: TreePath tp = new TreePath(root.getPath());
159: assertEquals("Path String for Root", "./", tree
160: .generatePathString(tp));
161:
162: tp = new TreePath(source.getPath());
163: assertEquals("Path String for source", "./[ Source Files ]/",
164: tree.generatePathString(tp));
165:
166: tp = new TreePath(folder1.getPath());
167: // System.out.println(tree.generatePathString(tp));
168: assertEquals("Path String for folder1",
169: "./[ Source Files ]/folder1/", tree
170: .generatePathString(tp));
171:
172: tp = new TreePath(folder2.getPath());
173: assertEquals("Path String for folder2",
174: "./[ Source Files ]/folder2/", tree
175: .generatePathString(tp));
176: }
177:
178: public void testCollapsedPaths() {
179: HashSet<String> set = new HashSet<String>();
180: set.add("./[ Source Files ]/folder1/");
181: set.add("./[ Source Files ]/");
182: // folder2 & root should not be collapsed
183:
184: TreePath tp1 = new TreePath(source.getPath());
185: TreePath tp2 = new TreePath(folder1.getPath());
186: TreePath tp3 = new TreePath(folder2.getPath());
187:
188: tree.collapsePaths(set);
189: assertTrue("Source should be collapsed.", tree.isCollapsed(tp1));
190: assertTrue("Source InnerNode should say it is collapsed.",
191: ((InnerNode<?, ?>) tp1.getLastPathComponent())
192: .isCollapsed());
193: assertTrue("Folder1 should be collapsed.", tree
194: .isCollapsed(tp2));
195: // System.out.println(((InnerNode)tp2.getLastPathComponent()).isCollapsed());
196: // System.out.println(tree.isCollapsed(tp3));
197: assertTrue("folder1 InnerNode should say it is collapsed.",
198: ((InnerNode<?, ?>) tp2.getLastPathComponent())
199: .isCollapsed());
200: assertTrue("Tree should say Folder2 is collapsed.", tree
201: .isCollapsed(tp3));
202: assertFalse(
203: "folder2 InnerNode should not say it is collapsed.",
204: ((InnerNode<?, ?>) tp3.getLastPathComponent())
205: .isCollapsed());
206:
207: HashSet<String> tmp = new HashSet<String>();
208: for (String s : tree.getCollapsedPaths()) {
209: tmp.add(s);
210: }
211: assertEquals(
212: "Collapsed paths given should be collapsed paths received.",
213: set, tmp);
214:
215: }
216:
217: // private String _name;
218: /**
219: * When the node is the only child of its parent and it is refreshed it should not
220: * try to delete the parent.
221: */
222: public void testRenameDocument() {
223: final String name = "MyTest.dj0";
224: final String newName = "MyTest.dj0*";
225: final DummyINavigatorItem item = new DummyINavigatorItem(name);
226: final DummyINavigatorItem newItem = new DummyINavigatorItem(
227: newName);
228: Utilities.invokeAndWait(new Runnable() {
229: public void run() {
230: tree.addDocument(item, "folder3");
231: }
232: });
233: InnerNode folder3 = (InnerNode) source.getChildAt(2);
234: assertEquals("folder3 should have 1 children", 1, folder3
235: .getChildCount());
236: Utilities.invokeAndWait(new Runnable() {
237: public void run() {
238: tree.refreshDocument(item, "folder3");
239: }
240: });
241: assertEquals("folder3 should have 1 children", 1, folder3
242: .getChildCount());
243: LeafNode<?> node = (LeafNode<?>) folder3.getChildAt(0);
244: assertEquals("node should have correct name", name, node
245: .toString());
246: Utilities.invokeAndWait(new Runnable() {
247: public void run() {
248: tree.removeDocument(item);
249: tree.addDocument(newItem, "folder3");
250: }
251: });
252: folder3 = (InnerNode) source.getChildAt(2);
253: LeafNode<?> newNode = (LeafNode<?>) folder3.getChildAt(0);
254:
255: // tree.refreshDocument(newItem, "folder3")
256: assertEquals("should have been renamed", newName, newNode
257: .toString());
258: assertEquals("node should have same parent", folder3, newNode
259: .getParent());
260: Utilities.invokeAndWait(new Runnable() {
261: public void run() {
262: tree.removeDocument(newItem);
263: }
264: });
265: }
266:
267: /**
268: * Test the enumeration of items based on top-level bins.
269: */
270: public void testGetDocumentsInBin() {
271: Enumeration<DummyINavigatorItem> e = tree
272: .getDocumentsInBin(SOURCE_BIN_NAME);
273: DummyINavigatorItem d;
274: assertTrue(SOURCE_BIN_NAME + " bin should not have 0 items", e
275: .hasMoreElements());
276: d = e.nextElement();
277: assertEquals("Wrong item 1", i1, d);
278: assertTrue(SOURCE_BIN_NAME + " bin should not have 1 item", e
279: .hasMoreElements());
280: d = e.nextElement();
281: assertEquals("Wrong item 2", i2, d);
282: assertTrue(SOURCE_BIN_NAME + " bin should not have 2 items", e
283: .hasMoreElements());
284: d = e.nextElement();
285: assertEquals("Wrong item 3", i3, d);
286: assertTrue(SOURCE_BIN_NAME + " bin should not have 3 items", e
287: .hasMoreElements());
288: d = e.nextElement();
289: assertEquals("Wrong item 4", i4, d);
290: assertFalse(SOURCE_BIN_NAME + " bin should not have 4 items", e
291: .hasMoreElements());
292:
293: e = tree.getDocumentsInBin(EXTERNAL_BIN_NAME);
294: assertTrue(EXTERNAL_BIN_NAME + " bin should not have 0 items",
295: e.hasMoreElements());
296: d = e.nextElement();
297: assertEquals("Wrong item 1", auxi1, d);
298: assertTrue(EXTERNAL_BIN_NAME + " bin should not have 1 item", e
299: .hasMoreElements());
300: d = e.nextElement();
301: assertEquals("Wrong item 2", auxi2, d);
302: assertTrue(EXTERNAL_BIN_NAME + " bin should not have 2 items",
303: e.hasMoreElements());
304: d = e.nextElement();
305: assertEquals("Wrong item 3", auxi3, d);
306: assertTrue(EXTERNAL_BIN_NAME + " bin should not have 3 items",
307: e.hasMoreElements());
308: d = e.nextElement();
309: assertEquals("Wrong item 4", auxi4, d);
310: assertTrue(EXTERNAL_BIN_NAME + " bin should not have 4 items",
311: e.hasMoreElements());
312: d = e.nextElement();
313: assertEquals("Wrong item 5", auxi5, d);
314: assertFalse(EXTERNAL_BIN_NAME + " bin should not have 5 items",
315: e.hasMoreElements());
316: }
317:
318: /**
319: * Test of getting the list of selected items.
320: */
321: public void testGetSelectedDocuments() {
322: tree.clearSelection();
323: tree.addSelectionRows(new int[] { 3, 4 });
324: java.util.List<DummyINavigatorItem> l = tree
325: .getSelectedDocuments();
326: assertEquals("Two items should be selected", 2, l.size());
327: assertEquals("Wrong item 1", i1, l.get(0));
328: assertEquals("Wrong item 2", i2, l.get(1));
329:
330: tree.clearSelection();
331: tree.addSelectionRows(new int[] { 3, 4, 6, 7 });
332: l = tree.getSelectedDocuments();
333: assertEquals("Four items should be selected", 4, l.size());
334: assertEquals("Wrong item 1", i1, l.get(0));
335: assertEquals("Wrong item 2", i2, l.get(1));
336: assertEquals("Wrong item 3", i3, l.get(2));
337: assertEquals("Wrong item 4", i4, l.get(3));
338:
339: tree.clearSelection();
340: tree.addSelectionRows(new int[] { 1, 2, 3, 4, 5, 6, 7 });
341: l = tree.getSelectedDocuments();
342: assertEquals("Four items should be selected", 4, l.size());
343: assertEquals("Wrong item 1", i1, l.get(0));
344: assertEquals("Wrong item 2", i2, l.get(1));
345: assertEquals("Wrong item 3", i3, l.get(2));
346: assertEquals("Wrong item 4", i4, l.get(3));
347:
348: tree.clearSelection();
349: tree.addSelectionRows(new int[] { 1, 2, 3, 5, 6 });
350: l = tree.getSelectedDocuments();
351: assertEquals("Two items should be selected", 2, l.size());
352: assertEquals("Wrong item 1", i1, l.get(0));
353: assertEquals("Wrong item 2", i3, l.get(1));
354:
355: tree.clearSelection();
356: tree.addSelectionRows(new int[] { 10, 11 });
357: l = tree.getSelectedDocuments();
358: assertEquals("Two items should be selected", 2, l.size());
359: assertEquals("Wrong item 1", auxi1, l.get(0));
360: assertEquals("Wrong item 2", auxi2, l.get(1));
361:
362: tree.clearSelection();
363: tree.addSelectionRows(new int[] { 10, 11, 13, 14, 15 });
364: l = tree.getSelectedDocuments();
365: assertEquals("Five items should be selected", 5, l.size());
366: assertEquals("Wrong item 1", auxi1, l.get(0));
367: assertEquals("Wrong item 2", auxi2, l.get(1));
368: assertEquals("Wrong item 3", auxi3, l.get(2));
369: assertEquals("Wrong item 4", auxi4, l.get(3));
370: assertEquals("Wrong item 5", auxi5, l.get(4));
371:
372: tree.clearSelection();
373: tree
374: .addSelectionRows(new int[] { 8, 9, 10, 11, 12, 13, 14,
375: 15 });
376: l = tree.getSelectedDocuments();
377: assertEquals("Five items should be selected", 5, l.size());
378: assertEquals("Wrong item 1", auxi1, l.get(0));
379: assertEquals("Wrong item 2", auxi2, l.get(1));
380: assertEquals("Wrong item 3", auxi3, l.get(2));
381: assertEquals("Wrong item 4", auxi4, l.get(3));
382: assertEquals("Wrong item 5", auxi5, l.get(4));
383:
384: tree.clearSelection();
385: tree.addSelectionRows(new int[] { 8, 9, 10, 12, 13 });
386: l = tree.getSelectedDocuments();
387: assertEquals("Two items should be selected", 2, l.size());
388: assertEquals("Wrong item 1", auxi1, l.get(0));
389: assertEquals("Wrong item 2", auxi3, l.get(1));
390: }
391:
392: /**
393: * Test of getting the list of selected items.
394: */
395: public void testGetNamesOfSelectedTopLevelGroup() {
396: java.util.Set<String> s = null;
397:
398: tree.clearSelection();
399: tree.addSelectionRows(new int[] { 3, 4 });
400: try {
401: s = tree.getNamesOfSelectedTopLevelGroup();
402: } catch (GroupNotSelectedException e) {
403: fail("getNamesOfSelectedTopLevelGroup threw " + e);
404: }
405: assertEquals("Only one top-level group should be selected", 1,
406: s.size());
407: assertTrue("Wrong top-level group", s.contains(SOURCE_BIN_NAME));
408:
409: tree.clearSelection();
410: tree.addSelectionRows(new int[] { 3, 4, 6, 7 });
411: try {
412: s = tree.getNamesOfSelectedTopLevelGroup();
413: } catch (GroupNotSelectedException e) {
414: fail("getNamesOfSelectedTopLevelGroup threw " + e);
415: }
416: assertEquals("Only one top-level group should be selected", 1,
417: s.size());
418: assertTrue("Wrong top-level group", s.contains(SOURCE_BIN_NAME));
419:
420: tree.clearSelection();
421: tree.addSelectionRows(new int[] { 1, 2, 3, 4, 5, 6, 7 });
422: try {
423: s = tree.getNamesOfSelectedTopLevelGroup();
424: } catch (GroupNotSelectedException e) {
425: fail("getNamesOfSelectedTopLevelGroup threw " + e);
426: }
427: assertEquals("Only one top-level group should be selected", 1,
428: s.size());
429: assertTrue("Wrong top-level group", s.contains(SOURCE_BIN_NAME));
430:
431: tree.clearSelection();
432: tree.addSelectionRows(new int[] { 1, 2, 3, 5, 6 });
433: try {
434: s = tree.getNamesOfSelectedTopLevelGroup();
435: } catch (GroupNotSelectedException e) {
436: fail("getNamesOfSelectedTopLevelGroup threw " + e);
437: }
438: assertEquals("Only one top-level group should be selected", 1,
439: s.size());
440: assertTrue("Wrong top-level group", s.contains(SOURCE_BIN_NAME));
441:
442: tree.clearSelection();
443: tree.addSelectionRows(new int[] { 10, 11 });
444: try {
445: s = tree.getNamesOfSelectedTopLevelGroup();
446: } catch (GroupNotSelectedException e) {
447: fail("getNamesOfSelectedTopLevelGroup threw " + e);
448: }
449: assertEquals("Only one top-level group should be selected", 1,
450: s.size());
451: assertEquals("Wrong top-level group 1", EXTERNAL_BIN_NAME, s
452: .toArray()[0]);
453:
454: tree.clearSelection();
455: tree.addSelectionRows(new int[] { 10, 11, 13, 14, 15 });
456: try {
457: s = tree.getNamesOfSelectedTopLevelGroup();
458: } catch (GroupNotSelectedException e) {
459: fail("getNamesOfSelectedTopLevelGroup threw " + e);
460: }
461: assertEquals("Only one top-level group should be selected", 1,
462: s.size());
463: assertEquals("Wrong top-level group 1", EXTERNAL_BIN_NAME, s
464: .toArray()[0]);
465:
466: tree.clearSelection();
467: tree
468: .addSelectionRows(new int[] { 8, 9, 10, 11, 12, 13, 14,
469: 15 });
470: try {
471: s = tree.getNamesOfSelectedTopLevelGroup();
472: } catch (GroupNotSelectedException e) {
473: fail("getNamesOfSelectedTopLevelGroup threw " + e);
474: }
475: assertEquals("Only one top-level group should be selected", 1,
476: s.size());
477: assertEquals("Wrong top-level group 1", EXTERNAL_BIN_NAME, s
478: .toArray()[0]);
479:
480: tree.clearSelection();
481: tree.addSelectionRows(new int[] { 8, 9, 10, 12, 13 });
482: try {
483: s = tree.getNamesOfSelectedTopLevelGroup();
484: } catch (GroupNotSelectedException e) {
485: fail("getNamesOfSelectedTopLevelGroup threw " + e);
486: }
487: assertEquals("Only one top-level group should be selected", 1,
488: s.size());
489: assertEquals("Wrong top-level group 1", EXTERNAL_BIN_NAME, s
490: .toArray()[0]);
491:
492: tree.clearSelection();
493: tree.addSelectionRows(new int[] { 3, 13 });
494: try {
495: s = tree.getNamesOfSelectedTopLevelGroup();
496: } catch (GroupNotSelectedException e) {
497: fail("getNamesOfSelectedTopLevelGroup threw " + e);
498: }
499: assertEquals("Two top-level groups should be selected", 2, s
500: .size());
501: assertTrue("Wrong top-level group", s.contains(SOURCE_BIN_NAME));
502: assertTrue("Wrong top-level group", s
503: .contains(EXTERNAL_BIN_NAME));
504:
505: tree.clearSelection();
506: try {
507: s = tree.getNamesOfSelectedTopLevelGroup();
508: fail("Didn't through");
509: } catch (Exception e) {
510: assertEquals("Exception isn't a GroupNotSelectedException",
511: GroupNotSelectedException.class, e.getClass());
512: }
513: }
514: }
|