mirror of
https://github.com/openjdk/jdk.git
synced 2026-04-15 01:18:40 +00:00
Implement LWListPeer.setMultipleMode
This commit is contained in:
parent
d453d49e52
commit
27e073cf81
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011, 2026, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -46,7 +46,6 @@ import javax.swing.JScrollPane;
|
||||
import javax.swing.JViewport;
|
||||
import javax.swing.ListCellRenderer;
|
||||
import javax.swing.ListModel;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
@ -54,6 +53,9 @@ import static java.awt.event.ItemEvent.DESELECTED;
|
||||
import static java.awt.event.ItemEvent.ITEM_STATE_CHANGED;
|
||||
import static java.awt.event.ItemEvent.SELECTED;
|
||||
|
||||
import static javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
|
||||
import static javax.swing.ListSelectionModel.SINGLE_SELECTION;
|
||||
|
||||
/**
|
||||
* Lightweight implementation of {@link ListPeer}. Delegates most of the work to
|
||||
* the {@link JList}, which is placed inside {@link JScrollPane}.
|
||||
@ -192,9 +194,23 @@ final class LWListPeer extends LWComponentPeer<List, LWListPeer.ScrollableJList>
|
||||
@Override
|
||||
public void setMultipleMode(final boolean m) {
|
||||
synchronized (getDelegateLock()) {
|
||||
getDelegate().getView().setSelectionMode(m ?
|
||||
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
|
||||
: ListSelectionModel.SINGLE_SELECTION);
|
||||
JList<String> view = getDelegate().getView();
|
||||
int newMode = m ? MULTIPLE_INTERVAL_SELECTION : SINGLE_SELECTION;
|
||||
if (view.getSelectionMode() == newMode) {
|
||||
return;
|
||||
}
|
||||
int lead = view.getLeadSelectionIndex();
|
||||
boolean wasSelected = lead != -1 && view.isSelectedIndex(lead);
|
||||
getDelegate().setSkipStateChangedEvent(true);
|
||||
try {
|
||||
view.clearSelection();
|
||||
view.setSelectionMode(newMode);
|
||||
if (wasSelected) {
|
||||
view.setSelectedIndex(lead);
|
||||
}
|
||||
} finally {
|
||||
getDelegate().setSkipStateChangedEvent(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -151,7 +151,7 @@ java/awt/grab/EmbeddedFrameTest1/EmbeddedFrameTest1.java 7080150 macosx-all
|
||||
java/awt/event/InputEvent/EventWhenTest/EventWhenTest.java 8168646 generic-all
|
||||
java/awt/List/KeyEventsTest/KeyEventsTest.java 8201307 linux-all
|
||||
java/awt/List/NoEvents/ProgrammaticChange.java 8201307 linux-all
|
||||
java/awt/List/SelectInvalid.java 8369455 linux-all
|
||||
java/awt/List/ListSelection/SelectInvalidTest.java 8369455 linux-all
|
||||
java/awt/Paint/ListRepaint.java 8201307 linux-all
|
||||
java/awt/Mixing/AWT_Mixing/OpaqueOverlapping.java 8370584 windows-x64
|
||||
java/awt/Mixing/AWT_Mixing/OpaqueOverlappingChoice.java 8048171 generic-all
|
||||
@ -811,3 +811,4 @@ tools/sincechecker/modules/jdk.management.jfr/JdkManagementJfrCheckSince.java 83
|
||||
# in either implementation or test code.
|
||||
|
||||
#############################################################################
|
||||
|
||||
|
||||
@ -26,20 +26,18 @@ import java.awt.List;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8369327
|
||||
* @summary Test awt list deselection methods
|
||||
* @key headful
|
||||
*/
|
||||
public final class DeselectionUnitTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// non-displayable list
|
||||
testSingleMode(null);
|
||||
testMultipleMode(null);
|
||||
testInvalidDeselection(null);
|
||||
testEmptyListDeselection(null);
|
||||
testNonDisplayable(DeselectionUnitTest::testSingleMode);
|
||||
testNonDisplayable(DeselectionUnitTest::testMultipleMode);
|
||||
testNonDisplayable(DeselectionUnitTest::testInvalidDeselection);
|
||||
testNonDisplayable(DeselectionUnitTest::testEmptyListDeselection);
|
||||
|
||||
// displayable list
|
||||
testDisplayable(DeselectionUnitTest::testSingleMode);
|
||||
testDisplayable(DeselectionUnitTest::testMultipleMode);
|
||||
testDisplayable(DeselectionUnitTest::testInvalidDeselection);
|
||||
@ -50,6 +48,10 @@ public final class DeselectionUnitTest {
|
||||
void execute(Frame frame);
|
||||
}
|
||||
|
||||
private static void testNonDisplayable(Test test) {
|
||||
test.execute(null);
|
||||
}
|
||||
|
||||
private static void testDisplayable(Test test) {
|
||||
Frame frame = new Frame();
|
||||
try {
|
||||
@ -62,14 +64,19 @@ public final class DeselectionUnitTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void testSingleMode(Frame frame) {
|
||||
List list = new List(4, false);
|
||||
private static List createList(Frame frame, boolean multi) {
|
||||
List list = new List(4, multi);
|
||||
if (frame != null) {
|
||||
frame.add(list);
|
||||
}
|
||||
list.add("Item1");
|
||||
list.add("Item2");
|
||||
list.add("Item3");
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void testSingleMode(Frame frame) {
|
||||
List list = createList(frame, false);
|
||||
|
||||
// Select and deselect single item
|
||||
list.select(1);
|
||||
@ -91,13 +98,7 @@ public final class DeselectionUnitTest {
|
||||
}
|
||||
|
||||
private static void testMultipleMode(Frame frame) {
|
||||
List list = new List(4, true);
|
||||
if (frame != null) {
|
||||
frame.add(list);
|
||||
}
|
||||
list.add("Item1");
|
||||
list.add("Item2");
|
||||
list.add("Item3");
|
||||
List list = createList(frame, true);
|
||||
|
||||
// Select multiple items and deselect one
|
||||
list.select(0);
|
||||
@ -122,13 +123,7 @@ public final class DeselectionUnitTest {
|
||||
}
|
||||
|
||||
private static void testInvalidDeselection(Frame frame) {
|
||||
List list = new List(4, false);
|
||||
if (frame != null) {
|
||||
frame.add(list);
|
||||
}
|
||||
list.add("Item1");
|
||||
list.add("Item2");
|
||||
list.add("Item3");
|
||||
List list = createList(frame, false);
|
||||
|
||||
// Deselect invalid indices (should be no-op)
|
||||
list.select(0);
|
||||
@ -156,9 +151,8 @@ public final class DeselectionUnitTest {
|
||||
|
||||
private static void assertEquals(int expected, int actual) {
|
||||
if (expected != actual) {
|
||||
System.err.println("Expected: " + expected);
|
||||
System.err.println("Actual: " + actual);
|
||||
throw new RuntimeException("Values are not equal");
|
||||
throw new RuntimeException(
|
||||
"Expected %d, got %d".formatted(expected, actual));
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,12 +27,12 @@ import jdk.test.lib.Platform;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8369327
|
||||
* @library /java/awt/regtesthelpers /test/lib
|
||||
* @summary Test awt list selection of invalid indexes
|
||||
* @key headful
|
||||
* @library /test/lib
|
||||
*/
|
||||
public final class SelectInvalid {
|
||||
public final class SelectInvalidTest {
|
||||
|
||||
/**
|
||||
* A special index on windows, selects or deselects all elements.
|
||||
@ -49,12 +49,12 @@ public final class SelectInvalid {
|
||||
public static void main(String[] args) {
|
||||
for (int i : INVALID) {
|
||||
if (Platform.isWindows() && i == WINDOWS_INVALID) {
|
||||
testDisplayable(SelectInvalid::testWinDeselectAllSingleMode, i);
|
||||
testDisplayable(SelectInvalid::testWinSelectAllMultipleMode, i);
|
||||
testDisplayable(SelectInvalidTest::testWinDeselectAllSingleMode, i);
|
||||
testDisplayable(SelectInvalidTest::testWinSelectAllMultipleMode, i);
|
||||
} else {
|
||||
testDisplayable(SelectInvalid::testSingleMode, i);
|
||||
testDisplayable(SelectInvalid::testMultipleMode, i);
|
||||
testDisplayable(SelectInvalid::testEmptySelection, i);
|
||||
testDisplayable(SelectInvalidTest::testSingleMode, i);
|
||||
testDisplayable(SelectInvalidTest::testMultipleMode, i);
|
||||
testDisplayable(SelectInvalidTest::testEmptySelection, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -152,9 +152,8 @@ public final class SelectInvalid {
|
||||
|
||||
private static void assertEquals(int expected, int actual) {
|
||||
if (expected != actual) {
|
||||
System.err.println("Expected: " + expected);
|
||||
System.err.println("Actual: " + actual);
|
||||
throw new RuntimeException("Values are not equal");
|
||||
throw new RuntimeException(
|
||||
"Expected %d, got %d".formatted(expected, actual));
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,19 +26,17 @@ import java.awt.List;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @key headful
|
||||
* @bug 8369327
|
||||
* @summary Test awt list selection methods
|
||||
* @key headful
|
||||
*/
|
||||
public final class SelectionUnitTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// non-displayable list
|
||||
testSingleMode(null);
|
||||
testMultipleMode(null);
|
||||
testEmptySelection(null);
|
||||
testNonDisplayable(SelectionUnitTest::testSingleMode);
|
||||
testNonDisplayable(SelectionUnitTest::testMultipleMode);
|
||||
testNonDisplayable(SelectionUnitTest::testEmptySelection);
|
||||
|
||||
// displayable list
|
||||
testDisplayable(SelectionUnitTest::testSingleMode);
|
||||
testDisplayable(SelectionUnitTest::testMultipleMode);
|
||||
testDisplayable(SelectionUnitTest::testEmptySelection);
|
||||
@ -48,6 +46,10 @@ public final class SelectionUnitTest {
|
||||
void execute(Frame frame);
|
||||
}
|
||||
|
||||
private static void testNonDisplayable(Test test) {
|
||||
test.execute(null);
|
||||
}
|
||||
|
||||
private static void testDisplayable(Test test) {
|
||||
Frame frame = new Frame();
|
||||
try {
|
||||
@ -60,14 +62,19 @@ public final class SelectionUnitTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void testSingleMode(Frame frame) {
|
||||
List list = new List(4, false);
|
||||
private static List createList(Frame frame, boolean multi) {
|
||||
List list = new List(4, multi);
|
||||
if (frame != null) {
|
||||
frame.add(list);
|
||||
}
|
||||
list.add("Item1");
|
||||
list.add("Item2");
|
||||
list.add("Item3");
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void testSingleMode(Frame frame) {
|
||||
List list = createList(frame, false);
|
||||
|
||||
// Test initial state
|
||||
assertEquals(-1, list.getSelectedIndex());
|
||||
@ -96,13 +103,7 @@ public final class SelectionUnitTest {
|
||||
}
|
||||
|
||||
private static void testMultipleMode(Frame frame) {
|
||||
List list = new List(4, true);
|
||||
if (frame != null) {
|
||||
frame.add(list);
|
||||
}
|
||||
list.add("Item1");
|
||||
list.add("Item2");
|
||||
list.add("Item3");
|
||||
List list = createList(frame, true);
|
||||
|
||||
// Test multiple selections
|
||||
list.select(0);
|
||||
@ -137,9 +138,8 @@ public final class SelectionUnitTest {
|
||||
|
||||
private static void assertEquals(int expected, int actual) {
|
||||
if (expected != actual) {
|
||||
System.err.println("Expected: " + expected);
|
||||
System.err.println("Actual: " + actual);
|
||||
throw new RuntimeException("Values are not equal");
|
||||
throw new RuntimeException(
|
||||
"Expected %d, got %d".formatted(expected, actual));
|
||||
}
|
||||
}
|
||||
|
||||
133
test/jdk/java/awt/List/ListSelection/SetMultipleModeTest.java
Normal file
133
test/jdk/java/awt/List/ListSelection/SetMultipleModeTest.java
Normal file
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.awt.Frame;
|
||||
import java.awt.List;
|
||||
import java.awt.Toolkit;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8369327
|
||||
* @summary Test awt list setMultipleMode selection
|
||||
* @key headful
|
||||
*/
|
||||
public final class SetMultipleModeTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Non-displayable list
|
||||
// test(null); Does not work per the spec
|
||||
|
||||
// Displayable list
|
||||
Frame frame = new Frame();
|
||||
try {
|
||||
test(frame);
|
||||
} finally {
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(Frame frame) {
|
||||
List list = new List();
|
||||
list.add("Item1");
|
||||
list.add("Item2");
|
||||
list.add("Item3");
|
||||
if (frame != null) {
|
||||
frame.add(list);
|
||||
frame.pack();
|
||||
}
|
||||
|
||||
// Empty: mode switch preserves empty
|
||||
list.setMultipleMode(true);
|
||||
check(list);
|
||||
list.deselect(0);
|
||||
check(list);
|
||||
list.setMultipleMode(false);
|
||||
check(list);
|
||||
|
||||
// Single to multi preserves selection
|
||||
list.select(1);
|
||||
list.setMultipleMode(true);
|
||||
check(list, 1);
|
||||
|
||||
// Multi to multi is no-op
|
||||
list.select(0);
|
||||
list.select(2);
|
||||
check(list, 0, 1, 2);
|
||||
list.setMultipleMode(true);
|
||||
check(list, 0, 1, 2);
|
||||
|
||||
// Multi to single keeps lead
|
||||
list.setMultipleMode(false);
|
||||
check(list, 2);
|
||||
|
||||
// Single to single is no-op
|
||||
list.setMultipleMode(false);
|
||||
check(list, 2);
|
||||
|
||||
// Round-trip
|
||||
list.setMultipleMode(true);
|
||||
check(list, 2);
|
||||
list.setMultipleMode(false);
|
||||
check(list, 2);
|
||||
|
||||
// XAWT does not move the focus cursor on deselect(),
|
||||
// so multi->single keeps the focused item, skip on XAWT
|
||||
boolean isXToolkit = "sun.awt.X11.XToolkit".equals(
|
||||
Toolkit.getDefaultToolkit().getClass().getName());
|
||||
if (!isXToolkit) {
|
||||
// Deselect lead in multi, switch to single
|
||||
list.setMultipleMode(true);
|
||||
list.select(0);
|
||||
list.select(1);
|
||||
check(list, 0, 1, 2);
|
||||
list.deselect(2);
|
||||
check(list, 0, 1);
|
||||
list.setMultipleMode(false);
|
||||
check(list);
|
||||
|
||||
// Deselect non-selected in multi, no-op
|
||||
list.setMultipleMode(true);
|
||||
list.select(0);
|
||||
check(list, 0);
|
||||
list.deselect(2);
|
||||
check(list, 0);
|
||||
list.setMultipleMode(false);
|
||||
check(list);
|
||||
}
|
||||
|
||||
if (frame != null) {
|
||||
frame.remove(list);
|
||||
}
|
||||
}
|
||||
|
||||
private static void check(List list, int... expected) {
|
||||
int[] actual = list.getSelectedIndexes();
|
||||
Arrays.sort(actual);
|
||||
if (!Arrays.equals(expected, actual)) {
|
||||
throw new RuntimeException(
|
||||
"Expected %s, got %s".formatted(Arrays.toString(expected),
|
||||
Arrays.toString(actual)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user