8014503: AWT Choice implementation should be made consistent across platforms.
Reviewed-by: serb, aghaisas, sveerabhadra
--- a/src/java.desktop/macosx/classes/sun/lwawt/LWChoicePeer.java Fri Oct 05 14:35:24 2018 +0530
+++ b/src/java.desktop/macosx/classes/sun/lwawt/LWChoicePeer.java Tue Oct 09 12:08:59 2018 +0530
@@ -23,16 +23,19 @@
* questions.
*/
-
package sun.lwawt;
-import java.awt.*;
+import java.awt.Point;
+import java.awt.Choice;
+
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.peer.ChoicePeer;
import javax.accessibility.Accessible;
-import javax.swing.*;
+import javax.swing.JComboBox;
+import javax.swing.SwingUtilities;
+import javax.swing.JPopupMenu;
/**
* Lightweight implementation of {@link ChoicePeer}. Delegates most of the work
@@ -146,19 +149,6 @@
return LWChoicePeer.this.getLocationOnScreen();
}
- /**
- * We should post ITEM_STATE_CHANGED event when the same element is
- * reselected.
- */
- @Override
- public void setSelectedItem(final Object anObject) {
- final Object oldSelection = selectedItemReminder;
- if (oldSelection != null && oldSelection.equals(anObject)) {
- selectedItemChanged();
- }
- super.setSelectedItem(anObject);
- }
-
@Override
public void firePopupMenuWillBecomeVisible() {
super.firePopupMenuWillBecomeVisible();
--- a/src/java.desktop/unix/classes/sun/awt/X11/XChoicePeer.java Fri Oct 05 14:35:24 2018 +0530
+++ b/src/java.desktop/unix/classes/sun/awt/X11/XChoicePeer.java Tue Oct 09 12:08:59 2018 +0530
@@ -25,9 +25,29 @@
package sun.awt.X11;
-import java.awt.*;
-import java.awt.peer.*;
-import java.awt.event.*;
+import java.awt.Color;
+import java.awt.Insets;
+import java.awt.Point;
+import java.awt.FontMetrics;
+import java.awt.Dimension;
+import java.awt.Rectangle;
+import java.awt.Choice;
+import java.awt.Toolkit;
+import java.awt.Graphics;
+import java.awt.Component;
+import java.awt.AWTEvent;
+import java.awt.Insets;
+import java.awt.Font;
+
+import java.awt.peer.ChoicePeer;
+
+import java.awt.event.FocusEvent;
+import java.awt.event.InvocationEvent;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseWheelEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.ItemEvent;
+
import sun.util.logging.PlatformLogger;
// FIXME: tab traversal should be disabled when mouse is captured (4816336)
@@ -228,12 +248,14 @@
helper.down();
int newIdx = helper.getSelectedIndex();
- ((Choice)target).select(newIdx);
- postEvent(new ItemEvent((Choice)target,
- ItemEvent.ITEM_STATE_CHANGED,
- ((Choice)target).getItem(newIdx),
- ItemEvent.SELECTED));
- repaint();
+ if (((Choice)target).getSelectedIndex() != newIdx) {
+ ((Choice)target).select(newIdx);
+ postEvent(new ItemEvent((Choice)target,
+ ItemEvent.ITEM_STATE_CHANGED,
+ ((Choice)target).getItem(newIdx),
+ ItemEvent.SELECTED));
+ repaint();
+ }
}
break;
}
@@ -243,12 +265,14 @@
helper.up();
int newIdx = helper.getSelectedIndex();
- ((Choice)target).select(newIdx);
- postEvent(new ItemEvent((Choice)target,
- ItemEvent.ITEM_STATE_CHANGED,
- ((Choice)target).getItem(newIdx),
- ItemEvent.SELECTED));
- repaint();
+ if (((Choice)target).getSelectedIndex() != newIdx) {
+ ((Choice)target).select(newIdx);
+ postEvent(new ItemEvent((Choice)target,
+ ItemEvent.ITEM_STATE_CHANGED,
+ ((Choice)target).getItem(newIdx),
+ ItemEvent.SELECTED));
+ repaint();
+ }
}
break;
}
@@ -293,11 +317,13 @@
helper.select(dragStartIdx);
} else { //KeyEvent.VK_ENTER:
int newIdx = helper.getSelectedIndex();
- ((Choice)target).select(newIdx);
- postEvent(new ItemEvent((Choice)target,
- ItemEvent.ITEM_STATE_CHANGED,
- ((Choice)target).getItem(newIdx),
- ItemEvent.SELECTED));
+ if (newIdx != (((Choice)target).getSelectedIndex())) {
+ ((Choice)target).select(newIdx);
+ postEvent(new ItemEvent((Choice)target,
+ ItemEvent.ITEM_STATE_CHANGED,
+ ((Choice)target).getItem(newIdx),
+ ItemEvent.SELECTED));
+ }
}
}
hidePopdownMenu();
@@ -457,8 +483,10 @@
* We should generate ItemEvent if only
* LeftMouseButton used */
if (e.getButton() == MouseEvent.BUTTON1 &&
- (!firstPress || wasDragged ))
+ (!firstPress || wasDragged ) &&
+ ((Choice)target).getSelectedIndex() != newIdx)
{
+ ((Choice)target).select(newIdx);
postEvent(new ItemEvent((Choice)target,
ItemEvent.ITEM_STATE_CHANGED,
((Choice)target).getItem(newIdx),
--- a/test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java Fri Oct 05 14:35:24 2018 +0530
+++ b/test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java Tue Oct 09 12:08:59 2018 +0530
@@ -108,7 +108,7 @@
robot.delay(1000);
- robot.mouseMove(loc.x + size.width / 2, loc.y + size.height + size.height / 2);
+ robot.mouseMove(loc.x + size.width / 2, loc.y + size.height);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();