4378
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
|
4378
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
4378
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
@bug 6823603
|
|
26 |
@summary Generics: JList
|
|
27 |
@author Florian Brunner
|
|
28 |
@run main bug6823603
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.Arrays;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Vector;
|
|
34 |
import java.util.Enumeration;
|
|
35 |
import java.awt.*;
|
|
36 |
import javax.swing.*;
|
|
37 |
|
|
38 |
public class bug6823603 {
|
|
39 |
|
|
40 |
private static final String TEST_ELEMENT = "Test1";
|
|
41 |
|
|
42 |
/**
|
|
43 |
* @param args the command line arguments
|
|
44 |
*/
|
|
45 |
@SuppressWarnings("unchecked")
|
|
46 |
public static void main(String[] args) {
|
|
47 |
testRawSignatures();
|
|
48 |
testGenericSignatures();
|
|
49 |
|
|
50 |
testGetSelectedValuesList(); // new method
|
|
51 |
}
|
|
52 |
|
|
53 |
@SuppressWarnings("unchecked")
|
|
54 |
private static void testRawSignatures() {
|
|
55 |
// Test JList
|
|
56 |
ListModel rawTestModel = new DefaultListModel();
|
|
57 |
|
|
58 |
new JList();
|
|
59 |
new JList(rawTestModel);
|
|
60 |
new JList(new Object[]{TEST_ELEMENT});
|
|
61 |
JList rawTestList = new JList(new Vector());
|
|
62 |
rawTestList.getPrototypeCellValue();
|
|
63 |
rawTestList.setPrototypeCellValue(TEST_ELEMENT);
|
|
64 |
rawTestList.getCellRenderer();
|
|
65 |
rawTestList.setCellRenderer(new DefaultListCellRenderer());
|
|
66 |
rawTestList.getModel();
|
|
67 |
rawTestList.setModel(rawTestModel);
|
|
68 |
rawTestList.setListData(new Object[]{TEST_ELEMENT});
|
|
69 |
rawTestList.setListData(new Vector());
|
|
70 |
|
|
71 |
@SuppressWarnings("deprecation")
|
|
72 |
Object[] selectedValues = rawTestList.getSelectedValues();
|
|
73 |
rawTestList.getSelectedValue();
|
|
74 |
|
|
75 |
// Test ListCellRenderer
|
|
76 |
ListCellRenderer rawTestCellRenderer = new DefaultListCellRenderer();
|
|
77 |
String testEntry = "Test";
|
|
78 |
@SuppressWarnings("unchecked")
|
|
79 |
JList rawJList = new JList(new Object[]{testEntry});
|
|
80 |
|
|
81 |
rawTestCellRenderer.getListCellRendererComponent(rawJList,
|
|
82 |
testEntry, 0, true, true);
|
|
83 |
|
|
84 |
// Test ListModel
|
|
85 |
DefaultListModel testModel = new DefaultListModel();
|
|
86 |
testModel.addElement(TEST_ELEMENT);
|
|
87 |
rawTestModel = testModel;
|
|
88 |
rawTestModel.getElementAt(0);
|
|
89 |
|
|
90 |
// Test DefaultListModel
|
|
91 |
DefaultListModel defaultListModel = new DefaultListModel();
|
|
92 |
|
|
93 |
defaultListModel.addElement(TEST_ELEMENT);
|
|
94 |
defaultListModel.getElementAt(0);
|
|
95 |
defaultListModel.elements();
|
|
96 |
defaultListModel.elementAt(0);
|
|
97 |
defaultListModel.firstElement();
|
|
98 |
defaultListModel.lastElement();
|
|
99 |
|
|
100 |
String testElement2 = "Test2";
|
|
101 |
|
|
102 |
defaultListModel.setElementAt(testElement2, 0);
|
|
103 |
defaultListModel.insertElementAt(TEST_ELEMENT, 0);
|
|
104 |
defaultListModel.get(0);
|
|
105 |
defaultListModel.set(0, testElement2);
|
|
106 |
defaultListModel.add(0, TEST_ELEMENT);
|
|
107 |
defaultListModel.remove(0);
|
|
108 |
|
|
109 |
// Test AbstractListModel
|
|
110 |
@SuppressWarnings("serial")
|
|
111 |
ListModel abstractListModel = new AbstractListModel() {
|
|
112 |
public int getSize() {
|
|
113 |
throw new UnsupportedOperationException("Not supported yet.");
|
|
114 |
}
|
|
115 |
|
|
116 |
public Object getElementAt(int index) {
|
|
117 |
throw new UnsupportedOperationException("Not supported yet.");
|
|
118 |
}
|
|
119 |
};
|
|
120 |
|
|
121 |
// Test DefaultListCellRenderer
|
|
122 |
DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer();
|
|
123 |
|
|
124 |
@SuppressWarnings("unchecked")
|
|
125 |
JList list = new JList(new Object[]{testEntry});
|
|
126 |
|
|
127 |
cellRenderer.getListCellRendererComponent(rawJList, testEntry, 0, true, true);
|
|
128 |
}
|
|
129 |
|
|
130 |
private static <E> void testGenericSignatures() {
|
|
131 |
// Test JList
|
|
132 |
ListModel<String> stringListModel = new DefaultListModel<String>();
|
|
133 |
|
|
134 |
new JList<String>();
|
|
135 |
new JList<String>(stringListModel);
|
|
136 |
new JList<String>(new String[]{TEST_ELEMENT});
|
|
137 |
|
|
138 |
JList<String> stringTestList = new JList<String>(new Vector<String>());
|
|
139 |
|
|
140 |
stringTestList.getPrototypeCellValue();
|
|
141 |
stringTestList.setPrototypeCellValue(TEST_ELEMENT);
|
|
142 |
|
|
143 |
ListCellRenderer<? super String> cellRenderer = stringTestList.getCellRenderer();
|
|
144 |
|
|
145 |
stringTestList.setCellRenderer(new DefaultListCellRenderer());
|
|
146 |
|
|
147 |
ListModel<String> model = stringTestList.getModel();
|
|
148 |
|
|
149 |
stringTestList.setModel(stringListModel);
|
|
150 |
stringTestList.setListData(new String[]{TEST_ELEMENT});
|
|
151 |
stringTestList.setListData(new Vector<String>());
|
|
152 |
|
|
153 |
@SuppressWarnings("deprecation")
|
|
154 |
Object[] selectedValues = stringTestList.getSelectedValues();
|
|
155 |
|
|
156 |
stringTestList.getSelectedValue();
|
|
157 |
|
|
158 |
// Test ListCellRenderer
|
|
159 |
ListCellRenderer<Object> stringTestCellRenderer =
|
|
160 |
new DefaultListCellRenderer();
|
|
161 |
String testEntry = "Test";
|
|
162 |
JList<String> stringJList = new JList<String>(new String[]{testEntry});
|
|
163 |
|
|
164 |
Component listCellRendererComponent2 =
|
|
165 |
stringTestCellRenderer.getListCellRendererComponent(stringJList,
|
|
166 |
testEntry, 0, true, true);
|
|
167 |
|
|
168 |
// Test ListModel
|
|
169 |
DefaultListModel<String> testModel = new DefaultListModel<String>();
|
|
170 |
testModel.addElement(TEST_ELEMENT);
|
|
171 |
stringListModel = testModel;
|
|
172 |
|
|
173 |
String element1 = stringListModel.getElementAt(0);
|
|
174 |
|
|
175 |
// Test DefaultListModel
|
|
176 |
DefaultListModel<String> stringTestModel = new DefaultListModel<String>();
|
|
177 |
|
|
178 |
stringTestModel.addElement(TEST_ELEMENT);
|
|
179 |
element1 = stringTestModel.getElementAt(0);
|
|
180 |
Enumeration<String> elements = stringTestModel.elements();
|
|
181 |
String element2 = stringTestModel.elementAt(0);
|
|
182 |
String firstElement = stringTestModel.firstElement();
|
|
183 |
String lastElement = stringTestModel.lastElement();
|
|
184 |
|
|
185 |
String testElement2 = "Test2";
|
|
186 |
stringTestModel.setElementAt(testElement2, 0);
|
|
187 |
stringTestModel.insertElementAt(TEST_ELEMENT, 0);
|
|
188 |
String element3 = stringTestModel.get(0);
|
|
189 |
String element4 = stringTestModel.set(0, testElement2);
|
|
190 |
stringTestModel.add(0, TEST_ELEMENT);
|
|
191 |
String removedElement = stringTestModel.remove(0);
|
|
192 |
|
|
193 |
// Test AbstractListModel
|
|
194 |
stringListModel = new AbstractListModel<String>() {
|
|
195 |
|
|
196 |
public int getSize() {
|
|
197 |
throw new UnsupportedOperationException("Not supported yet.");
|
|
198 |
}
|
|
199 |
|
|
200 |
public String getElementAt(int index) {
|
|
201 |
throw new UnsupportedOperationException("Not supported yet.");
|
|
202 |
}
|
|
203 |
};
|
|
204 |
|
|
205 |
@SuppressWarnings("serial")
|
|
206 |
ListModel<E> genericTestModel = new AbstractListModel<E>() {
|
|
207 |
|
|
208 |
public int getSize() {
|
|
209 |
throw new UnsupportedOperationException("Not supported yet.");
|
|
210 |
}
|
|
211 |
|
|
212 |
public E getElementAt(int index) {
|
|
213 |
throw new UnsupportedOperationException("Not supported yet.");
|
|
214 |
}
|
|
215 |
};
|
|
216 |
|
|
217 |
// Test DefaultListCellRenderer
|
|
218 |
cellRenderer = new DefaultListCellRenderer();
|
|
219 |
|
|
220 |
stringJList = new JList<String>(new String[]{testEntry});
|
|
221 |
|
|
222 |
listCellRendererComponent2 = cellRenderer.getListCellRendererComponent(stringJList, testEntry, 0, true, true);
|
|
223 |
}
|
|
224 |
|
|
225 |
private static void testGetSelectedValuesList() {
|
|
226 |
Vector<Integer> data = new Vector<Integer>();
|
|
227 |
for (int i = 0; i < 10; i++) {
|
|
228 |
data.add(i);
|
|
229 |
}
|
|
230 |
JList<Integer> list = new JList<Integer>(data);
|
|
231 |
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
|
232 |
list.setSelectedIndices(new int[]{1, 2, 3, 5, 6, 8});
|
|
233 |
|
|
234 |
@SuppressWarnings("deprecation")
|
|
235 |
Object[] expectedSelectedValues = list.getSelectedValues();
|
|
236 |
List<Integer> selectedValuesList = list.getSelectedValuesList();
|
|
237 |
assertEquals(expectedSelectedValues, selectedValuesList.toArray());
|
|
238 |
}
|
|
239 |
|
|
240 |
private static void assertEquals(Object[] expectedArray,
|
|
241 |
Object[] actualArray) {
|
|
242 |
if (!Arrays.equals(expectedArray, actualArray)) {
|
|
243 |
throw new RuntimeException("Expected: " + Arrays.toString(
|
|
244 |
expectedArray) + " but was: " + Arrays.toString(actualArray));
|
|
245 |
}
|
|
246 |
}
|
|
247 |
}
|