Skip to content

Commit 64292be

Browse files
author
mgricken
committed
Added capability to select multiple documents in flat file mode too,
just like in project mode in the previous commit. The context (right-click) menu will be generated according to the selection. M src/edu/rice/cs/util/docnavigation/JListNavigator.java M src/edu/rice/cs/util/docnavigation/JListSortNavigatorTest.java git-svn-id: file:///tmp/test-svn/trunk@4171 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent c2cccf3 commit 64292be

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

drjava/src/edu/rice/cs/util/docnavigation/JListNavigator.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public JListNavigator() {
9090
private void init(DefaultListModel m) {
9191
_model = m;
9292
setModel(m);
93-
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
93+
setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
9494
addListSelectionListener(new ListSelectionListener() {
9595
/** Called when the list value has changed. Should only run in the event thread.
9696
* @param e the event corresponding to the change
@@ -324,13 +324,19 @@ public boolean selectDocumentAt(final int x, final int y) {
324324
* @param y the y coordinate of the navigator pane
325325
* @return true if the item is currently selected
326326
*/
327-
public boolean isSelectedAt(int x, int y) { return false; }
327+
public boolean isSelectedAt(int x, int y) {
328+
synchronized(_model) {
329+
final int idx = locationToIndex(new java.awt.Point(x,y));
330+
if (idx == -1) return false;
331+
return isSelectedIndex(idx);
332+
}
333+
}
328334

329335
/** @return the renderer for this object. */
330336
public Component getRenderer(){ return _renderer; }
331337

332-
/** @return the number of selected items. Always 1 for JListSortNavigator */
333-
public int getSelectionCount() { return 1; }
338+
/** @return the number of selected items. */
339+
public int getSelectionCount() { return getSelectedIndices().length; }
334340

335341
/** @return true if at least one group of INavigatorItems is selected; always false for JListNavigator */
336342
public boolean isGroupSelected() { return false; }
@@ -344,13 +350,14 @@ public boolean selectDocumentAt(final int x, final int y) {
344350
/** @return true if at least one document is selected; always true for JListNavigator */
345351
public boolean isDocumentSelected() { return true; }
346352

347-
/** @return the number of documents selected. Always 1 for JListSortNavigator */
348-
public int getDocumentSelectedCount() { return 1; }
353+
/** @return the number of documents selected. Same as getSelectionCount for JListSortNavigator. */
354+
public int getDocumentSelectedCount() { return getSelectionCount(); }
349355

350356
/** @return the documents currently selected. Only runs in event thread. */
351357
@SuppressWarnings("unchecked") public java.util.List<ItemT> getSelectedDocuments() {
352-
ArrayList<ItemT> l = new ArrayList<ItemT>(1);
353-
l.add((ItemT)getSelectedValue());
358+
Object[] selected = getSelectedValues();
359+
ArrayList<ItemT> l = new ArrayList<ItemT>(selected.length);
360+
for (Object o: selected) { l.add((ItemT)o); }
354361
return l;
355362
}
356363

drjava/src/edu/rice/cs/util/docnavigation/JListSortNavigatorTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,50 @@ public void testTraversalOps() {
9292
assertTrue("getDocuments test", Arrays.equals(docsArray, new DummyINavigatorItem[] {i1, i2, i3, i4}));
9393
}
9494

95+
/**
96+
* Test of getting the list of selected items.
97+
*/
98+
public void testGetSelectedDocuments() {
99+
list.clearSelection();
100+
list.addSelectionInterval(0, 1);
101+
assertEquals("Two items should be selected", 2, list.getSelectionCount());
102+
assertEquals("Two items should be selected", 2, list.getDocumentSelectedCount());
103+
assertEquals("Zero groups should be selected", 0, list.getGroupSelectedCount());
104+
java.util.List<DummyINavigatorItem> l = list.getSelectedDocuments();
105+
assertEquals("Two items should be selected", 2, l.size());
106+
assertEquals("Wrong item 1", i1, l.get(0));
107+
assertEquals("Wrong item 2", i2, l.get(1));
108+
109+
list.clearSelection();
110+
list.addSelectionInterval(0, 3);
111+
assertEquals("Four items should be selected", 4, list.getSelectionCount());
112+
assertEquals("Four items should be selected", 4, list.getDocumentSelectedCount());
113+
assertEquals("Zero groups should be selected", 0, list.getGroupSelectedCount());
114+
l = list.getSelectedDocuments();
115+
assertEquals("Four items should be selected", 4, l.size());
116+
assertEquals("Wrong item 1", i1, l.get(0));
117+
assertEquals("Wrong item 2", i2, l.get(1));
118+
assertEquals("Wrong item 3", i3, l.get(2));
119+
assertEquals("Wrong item 4", i4, l.get(3));
120+
121+
list.clearSelection();
122+
list.addSelectionInterval(0, 1);
123+
list.addSelectionInterval(2, 3);
124+
assertEquals("Four items should be selected", 4, list.getSelectionCount());
125+
assertEquals("Four items should be selected", 4, list.getDocumentSelectedCount());
126+
assertEquals("Zero groups should be selected", 0, list.getGroupSelectedCount());
127+
l = list.getSelectedDocuments();
128+
assertEquals("Four items should be selected", 4, l.size());
129+
assertEquals("Wrong item 1", i1, l.get(0));
130+
assertEquals("Wrong item 2", i2, l.get(1));
131+
assertEquals("Wrong item 3", i3, l.get(2));
132+
assertEquals("Wrong item 4", i4, l.get(3));
133+
134+
list.clearSelection();
135+
assertEquals("Zero items should be selected", 0, list.getSelectionCount());
136+
assertEquals("Zero items should be selected", 0, list.getDocumentSelectedCount());
137+
assertEquals("Zero groups should be selected", 0, list.getGroupSelectedCount());
138+
l = list.getSelectedDocuments();
139+
assertEquals("Zero items should be selected", 0, l.size());
140+
}
95141
}

0 commit comments

Comments
 (0)