Skip to content

Commit 4500818

Browse files
committed
Factor out interface from SelectionListModel
1 parent 7ebc956 commit 4500818

File tree

3 files changed

+102
-48
lines changed

3 files changed

+102
-48
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* OpenSlide, a library for reading whole slide image files
3+
*
4+
* Copyright (c) 2007-2010 Carnegie Mellon University
5+
* All rights reserved.
6+
*
7+
* OpenSlide is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, version 2.1.
10+
*
11+
* OpenSlide is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with OpenSlide. If not, see
18+
* <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
package edu.cmu.cs.openslide.gui;
23+
24+
import java.util.ArrayList;
25+
import java.util.Iterator;
26+
import java.util.List;
27+
28+
import javax.swing.AbstractListModel;
29+
30+
public class DefaultSelectionListModel extends AbstractListModel implements
31+
SelectionListModel {
32+
33+
private final List<Annotation> list = new ArrayList<Annotation>();
34+
35+
@Override
36+
public Annotation getElementAt(int index) {
37+
return list.get(index);
38+
}
39+
40+
@Override
41+
public int getSize() {
42+
return list.size();
43+
}
44+
45+
@Override
46+
public void add(Annotation s) {
47+
list.add(s);
48+
int i = list.size() - 1;
49+
fireIntervalAdded(this, i, i);
50+
}
51+
52+
@Override
53+
public void add(int index, Annotation s) {
54+
list.add(index, s);
55+
fireIntervalAdded(this, index, index);
56+
}
57+
58+
@Override
59+
public void remove(int index) {
60+
list.remove(index);
61+
fireIntervalRemoved(this, index, index);
62+
}
63+
64+
@Override
65+
public void replace(int index, Annotation annotation) {
66+
list.remove(index);
67+
list.add(index, annotation);
68+
fireContentsChanged(this, index, index);
69+
}
70+
71+
@Override
72+
public void clear() {
73+
int oldSize = list.size();
74+
list.clear();
75+
fireIntervalRemoved(this, 0, oldSize - 1);
76+
}
77+
78+
@Override
79+
public boolean isEmpty() {
80+
return list.isEmpty();
81+
}
82+
83+
@Override
84+
public Annotation get(int index) {
85+
return list.get(index);
86+
}
87+
88+
@Override
89+
public Iterator<Annotation> iterator() {
90+
return list.iterator();
91+
}
92+
}

src/edu/cmu/cs/openslide/gui/OpenSlideView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class OpenSlideView extends JPanel {
6060

6161
private OpenSlideView otherView;
6262

63-
private SelectionListModel selections = new SelectionListModel();
63+
private SelectionListModel selections = new DefaultSelectionListModel();
6464

6565
private Shape selectionBeingDrawn;
6666

src/edu/cmu/cs/openslide/gui/SelectionListModel.java

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,21 @@
2121

2222
package edu.cmu.cs.openslide.gui;
2323

24-
import java.util.ArrayList;
25-
import java.util.Iterator;
26-
import java.util.List;
24+
import javax.swing.ListModel;
2725

28-
import javax.swing.AbstractListModel;
26+
public interface SelectionListModel extends ListModel, Iterable<Annotation> {
2927

30-
public class SelectionListModel extends AbstractListModel implements
31-
Iterable<Annotation> {
28+
void add(Annotation annotation);
3229

33-
private final List<Annotation> list = new ArrayList<Annotation>();
30+
boolean isEmpty();
3431

35-
@Override
36-
public Annotation getElementAt(int index) {
37-
return list.get(index);
38-
}
32+
Annotation get(int i);
3933

40-
@Override
41-
public int getSize() {
42-
return list.size();
43-
}
34+
void add(int index, Annotation annotation);
4435

45-
public void add(Annotation s) {
46-
list.add(s);
47-
int i = list.size() - 1;
48-
fireIntervalAdded(this, i, i);
49-
}
36+
void remove(int index);
5037

51-
public void add(int index, Annotation s) {
52-
list.add(index, s);
53-
fireIntervalAdded(this, index, index);
54-
}
38+
void replace(int index, Annotation annotation);
5539

56-
public void remove(int index) {
57-
list.remove(index);
58-
fireIntervalRemoved(this, index, index);
59-
}
60-
61-
public void clear() {
62-
int oldSize = list.size();
63-
list.clear();
64-
fireIntervalRemoved(this, 0, oldSize - 1);
65-
}
66-
67-
public boolean isEmpty() {
68-
return list.isEmpty();
69-
}
70-
71-
public Annotation get(int index) {
72-
return list.get(index);
73-
}
74-
75-
@Override
76-
public Iterator<Annotation> iterator() {
77-
return list.iterator();
78-
}
40+
void clear();
7941
}

0 commit comments

Comments
 (0)