0N/A/*
6441N/A * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.peer.*;
0N/Aimport java.awt.event.*;
1696N/Aimport sun.util.logging.PlatformLogger;
0N/A
0N/A// FIXME: tab traversal should be disabled when mouse is captured (4816336)
0N/A
0N/A// FIXME: key and mouse events should not be delivered to listeners when the Choice is unfurled. Must override handleNativeKey/MouseEvent (4816336)
0N/A
0N/A// FIXME: test programmatic add/remove/clear/etc
0N/A
0N/A// FIXME: account for unfurling at the edge of the screen
0N/A// Note: can't set x,y on layout(), 'cause moving the top-level to the
0N/A// edge of the screen won't call layout(). Just do it on paint, I guess
0N/A
0N/A// TODO: make painting more efficient (i.e. when down arrow is pressed, only two items should need to be repainted.
0N/A
0N/Apublic class XChoicePeer extends XComponentPeer implements ChoicePeer, ToplevelStateListener {
1696N/A private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XChoicePeer");
0N/A
0N/A private static final int MAX_UNFURLED_ITEMS = 10; // Maximum number of
0N/A // items to be displayed
0N/A // at a time in an
0N/A // unfurled Choice
0N/A // Description of these constants in ListHelper
0N/A public final static int TEXT_SPACE = 1;
0N/A public final static int BORDER_WIDTH = 1;
0N/A public final static int ITEM_MARGIN = 1;
0N/A public final static int SCROLLBAR_WIDTH = 15;
0N/A
0N/A
0N/A // SHARE THESE!
0N/A private static final Insets focusInsets = new Insets(0,0,0,0);
0N/A
0N/A
0N/A static final int WIDGET_OFFSET = 18;
0N/A
0N/A // Stolen from Tiny
0N/A static final int TEXT_XPAD = 8;
0N/A static final int TEXT_YPAD = 6;
0N/A
0N/A // FIXME: Motif uses a different focus color for the item within
0N/A // the unfurled Choice list and for when the Choice itself is focused and
0N/A // popped up.
0N/A static final Color focusColor = Color.black;
0N/A
0N/A // TODO: there is a time value that the mouse is held down. If short
0N/A // enough, the Choice stays popped down. If long enough, Choice
0N/A // is furled when the mouse is released
0N/A
0N/A private boolean unfurled = false; // Choice list is popped down
0N/A
0N/A private boolean dragging = false; // Mouse was pressed and is being
0N/A // dragged over the (unfurled)
0N/A // Choice
0N/A
0N/A private boolean mouseInSB = false; // Mouse is interacting with the
0N/A // scrollbar
0N/A
0N/A private boolean firstPress = false; // mouse was pressed on
0N/A // furled Choice so we
0N/A // not need to furl the
0N/A // Choice when MOUSE_RELEASED occured
0N/A
0N/A // 6425067. Mouse was pressed on furled choice and dropdown list appeared over Choice itself
0N/A // and then there were no mouse movements until MOUSE_RELEASE.
0N/A // This scenario leads to ItemStateChanged as the choice logic uses
0N/A // MouseReleased event to send ItemStateChanged. To prevent it we should
0N/A // use a combination of firstPress and wasDragged variables.
0N/A // The only difference in dragging and wasDragged is: last one will not
0N/A // set to false on mouse ungrab. It become false after MouseRelased() finishes.
0N/A private boolean wasDragged = false;
0N/A private ListHelper helper;
0N/A private UnfurledChoice unfurledChoice;
0N/A
0N/A // TODO: Choice remembers where it was scrolled to when unfurled - it's not
0N/A // always to the currently selected item.
0N/A
0N/A // Indicates whether or not to paint selected item in the choice.
0N/A // Default is to paint
0N/A private boolean drawSelectedItem = true;
0N/A
0N/A // If set, indicates components under which choice popup should be showed.
0N/A // The choice's popup width and location should be adjust to appear
0N/A // under both choice and alignUnder component.
0N/A private Component alignUnder;
0N/A
0N/A // If cursor is outside of an unfurled Choice when the mouse is
0N/A // released, Choice item should NOT be updated. Remember the proper index.
0N/A private int dragStartIdx = -1;
0N/A
0N/A // Holds the listener (XFileDialogPeer) which the processing events from the choice
0N/A // See 6240074 for more information
0N/A private XChoicePeerListener choiceListener;
0N/A
0N/A XChoicePeer(Choice target) {
0N/A super(target);
0N/A }
0N/A
0N/A void preInit(XCreateWindowParams params) {
0N/A super.preInit(params);
0N/A Choice target = (Choice)this.target;
0N/A int numItems = target.getItemCount();
0N/A unfurledChoice = new UnfurledChoice(target);
0N/A getToplevelXWindow().addToplevelStateListener(this);
0N/A helper = new ListHelper(unfurledChoice,
0N/A getGUIcolors(),
0N/A numItems,
0N/A false,
0N/A true,
0N/A false,
0N/A target.getFont(),
0N/A MAX_UNFURLED_ITEMS,
0N/A TEXT_SPACE,
0N/A ITEM_MARGIN,
0N/A BORDER_WIDTH,
0N/A SCROLLBAR_WIDTH);
0N/A }
0N/A
0N/A void postInit(XCreateWindowParams params) {
0N/A super.postInit(params);
0N/A Choice target = (Choice)this.target;
0N/A int numItems = target.getItemCount();
0N/A
0N/A // Add all items
0N/A for (int i = 0; i < numItems; i++) {
0N/A helper.add(target.getItem(i));
0N/A }
0N/A if (!helper.isEmpty()) {
0N/A helper.select(target.getSelectedIndex());
0N/A helper.setFocusedIndex(target.getSelectedIndex());
0N/A }
0N/A helper.updateColors(getGUIcolors());
0N/A updateMotifColors(getPeerBackground());
0N/A }
0N/A
0N/A public boolean isFocusable() { return true; }
0N/A
0N/A // 6399679. check if super.setBounds() actually changes the size of the
0N/A // component and then compare current Choice size with a new one. If
0N/A // they differs then hide dropdown menu
0N/A public void setBounds(int x, int y, int width, int height, int op) {
0N/A int oldX = this.x;
0N/A int oldY = this.y;
0N/A int oldWidth = this.width;
0N/A int oldHeight = this.height;
0N/A super.setBounds(x, y, width, height, op);
0N/A if (unfurled && (oldX != this.x || oldY != this.y || oldWidth != this.width || oldHeight != this.height) ) {
0N/A hidePopdownMenu();
0N/A }
0N/A }
0N/A
0N/A public void focusGained(FocusEvent e) {
0N/A // TODO: only need to paint the focus bit
0N/A super.focusGained(e);
0N/A repaint();
0N/A }
0N/A
0N/A /*
0N/A * Fix for 6246503 : Disabling a choice after selection locks keyboard, mouse and makes the system unusable, Xtoolkit
0N/A * if setEnabled(false) invoked we should close opened choice in
0N/A * order to prevent keyboard/mouse lock.
0N/A */
0N/A public void setEnabled(boolean value) {
0N/A super.setEnabled(value);
0N/A helper.updateColors(getGUIcolors());
0N/A if (!value && unfurled){
0N/A hidePopdownMenu();
0N/A }
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A // TODO: only need to paint the focus bit?
0N/A super.focusLost(e);
0N/A repaint();
0N/A }
0N/A
0N/A void ungrabInputImpl() {
0N/A if (unfurled) {
0N/A unfurled = false;
0N/A dragging = false;
0N/A mouseInSB = false;
0N/A unfurledChoice.setVisible(false);
0N/A }
0N/A
0N/A super.ungrabInputImpl();
0N/A }
0N/A
0N/A void handleJavaKeyEvent(KeyEvent e) {
0N/A if (e.getID() == KeyEvent.KEY_PRESSED) {
0N/A keyPressed(e);
0N/A }
0N/A }
0N/A
0N/A public void keyPressed(KeyEvent e) {
0N/A switch(e.getKeyCode()) {
0N/A // UP & DOWN are same if furled or unfurled
0N/A case KeyEvent.VK_DOWN:
0N/A case KeyEvent.VK_KP_DOWN: {
0N/A if (helper.getItemCount() > 1) {
0N/A helper.down();
0N/A int newIdx = helper.getSelectedIndex();
0N/A
0N/A ((Choice)target).select(newIdx);
0N/A postEvent(new ItemEvent((Choice)target,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A ((Choice)target).getItem(newIdx),
0N/A ItemEvent.SELECTED));
0N/A repaint();
0N/A }
0N/A break;
0N/A }
0N/A case KeyEvent.VK_UP:
0N/A case KeyEvent.VK_KP_UP: {
0N/A if (helper.getItemCount() > 1) {
0N/A helper.up();
0N/A int newIdx = helper.getSelectedIndex();
0N/A
0N/A ((Choice)target).select(newIdx);
0N/A postEvent(new ItemEvent((Choice)target,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A ((Choice)target).getItem(newIdx),
0N/A ItemEvent.SELECTED));
0N/A repaint();
0N/A }
0N/A break;
0N/A }
0N/A case KeyEvent.VK_PAGE_DOWN:
0N/A if (unfurled && !dragging) {
0N/A int oldIdx = helper.getSelectedIndex();
0N/A helper.pageDown();
0N/A int newIdx = helper.getSelectedIndex();
0N/A if (oldIdx != newIdx) {
0N/A ((Choice)target).select(newIdx);
0N/A postEvent(new ItemEvent((Choice)target,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A ((Choice)target).getItem(newIdx),
0N/A ItemEvent.SELECTED));
0N/A repaint();
0N/A }
0N/A }
0N/A break;
0N/A case KeyEvent.VK_PAGE_UP:
0N/A if (unfurled && !dragging) {
0N/A int oldIdx = helper.getSelectedIndex();
0N/A helper.pageUp();
0N/A int newIdx = helper.getSelectedIndex();
0N/A if (oldIdx != newIdx) {
0N/A ((Choice)target).select(newIdx);
0N/A postEvent(new ItemEvent((Choice)target,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A ((Choice)target).getItem(newIdx),
0N/A ItemEvent.SELECTED));
0N/A repaint();
0N/A }
0N/A }
0N/A break;
0N/A case KeyEvent.VK_ESCAPE:
0N/A case KeyEvent.VK_ENTER:
0N/A if (unfurled) {
0N/A if (dragging){
0N/A if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
0N/A //This also happens on
0N/A // - MouseButton2,3, etc. press
0N/A // - ENTER press
0N/A helper.select(dragStartIdx);
0N/A } else { //KeyEvent.VK_ENTER:
0N/A int newIdx = helper.getSelectedIndex();
0N/A ((Choice)target).select(newIdx);
0N/A postEvent(new ItemEvent((Choice)target,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A ((Choice)target).getItem(newIdx),
0N/A ItemEvent.SELECTED));
0N/A }
0N/A }
0N/A hidePopdownMenu();
0N/A dragging = false;
0N/A wasDragged = false;
0N/A mouseInSB = false;
0N/A
0N/A // See 6240074 for more information
0N/A if (choiceListener != null){
0N/A choiceListener.unfurledChoiceClosing();
0N/A }
0N/A }
0N/A break;
0N/A default:
0N/A if (unfurled) {
0N/A Toolkit.getDefaultToolkit().beep();
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A
0N/A public boolean handlesWheelScrolling() { return true; }
0N/A
0N/A void handleJavaMouseWheelEvent(MouseWheelEvent e) {
0N/A if (unfurled && helper.isVSBVisible()) {
0N/A if (ListHelper.doWheelScroll(helper.getVSB(), null, e)) {
0N/A repaint();
0N/A }
0N/A }
0N/A }
0N/A
0N/A void handleJavaMouseEvent(MouseEvent e) {
0N/A super.handleJavaMouseEvent(e);
0N/A int i = e.getID();
0N/A switch (i) {
0N/A case MouseEvent.MOUSE_PRESSED:
0N/A mousePressed(e);
0N/A break;
0N/A case MouseEvent.MOUSE_RELEASED:
0N/A mouseReleased(e);
0N/A break;
0N/A case MouseEvent.MOUSE_DRAGGED:
0N/A mouseDragged(e);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A public void mousePressed(MouseEvent e) {
0N/A /*
0N/A * fix for 5003166: a Choice on XAWT shouldn't react to any
0N/A * mouse button presses except left. This involves presses on
0N/A * Choice but not on opened part of choice.
0N/A */
0N/A if (e.getButton() == MouseEvent.BUTTON1){
0N/A dragStartIdx = helper.getSelectedIndex();
0N/A if (unfurled) {
0N/A //fix 6259328: PIT: Choice scrolls when dragging the parent frame while drop-down is active, XToolkit
0N/A if (! (isMouseEventInChoice(e) ||
0N/A unfurledChoice.isMouseEventInside(e)))
0N/A {
0N/A hidePopdownMenu();
0N/A }
0N/A // Press on unfurled Choice. Highlight the item under the cursor,
0N/A // but don't send item event or set the text on the button yet
0N/A unfurledChoice.trackMouse(e);
0N/A }
0N/A else {
0N/A // Choice is up - unfurl it
0N/A grabInput();
0N/A unfurledChoice.toFront();
0N/A firstPress = true;
0N/A wasDragged = false;
0N/A unfurled = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * helper method for mouseReleased routine
0N/A */
0N/A void hidePopdownMenu(){
0N/A ungrabInput();
0N/A unfurledChoice.setVisible(false);
0N/A unfurled = false;
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A if (unfurled) {
0N/A if (mouseInSB) {
0N/A unfurledChoice.trackMouse(e);
0N/A }
0N/A else {
0N/A // We pressed and dragged onto the Choice, or, this is the
0N/A // second release after clicking to make the Choice "stick"
0N/A // unfurled.
0N/A // This release should ungrab/furl, and set the new item if
0N/A // release was over the unfurled Choice.
0N/A
0N/A // Fix for 6239944 : Choice shouldn't close its
0N/A // pop-down menu if user presses Mouse on Choice's Scrollbar
0N/A // some additional cases like releasing mouse outside
0N/A // of Choice are considered too
0N/A boolean isMouseEventInside = unfurledChoice.isMouseEventInside( e );
0N/A boolean isMouseInListArea = unfurledChoice.isMouseInListArea( e );
0N/A
0N/A // Fixed 6318746: REG: File Selection is failing
0N/A // We shouldn't restore the selected item
0N/A // if the mouse was dragged outside the drop-down choice area
0N/A if (!helper.isEmpty() && !isMouseInListArea && dragging) {
0N/A // Set the selected item back how it was.
0N/A ((Choice)target).select(dragStartIdx);
0N/A }
0N/A
0N/A // Choice must be closed if user releases mouse on
0N/A // pop-down menu on the second click
0N/A if ( !firstPress && isMouseInListArea) {
0N/A hidePopdownMenu();
0N/A }
0N/A // Choice must be closed if user releases mouse
0N/A // outside of Choice's pop-down menu on the second click
0N/A if ( !firstPress && !isMouseEventInside) {
0N/A hidePopdownMenu();
0N/A }
0N/A //if user drags Mouse on pop-down menu, Scrollbar or
0N/A // outside the Choice
0N/A if ( firstPress && dragging) {
0N/A hidePopdownMenu();
0N/A }
0N/A /* this could happen when user has opened a Choice and
0N/A * released mouse button. Then he drags mouse on the
0N/A * Scrollbar and releases mouse again.
0N/A */
0N/A if ( !firstPress && !isMouseInListArea &&
0N/A isMouseEventInside && dragging)
0N/A {
0N/A hidePopdownMenu();
0N/A }
0N/A
0N/A if (!helper.isEmpty()) {
0N/A // Only update the Choice if the mouse button is released
0N/A // over the list of items.
0N/A if (unfurledChoice.isMouseInListArea(e)) {
0N/A int newIdx = helper.getSelectedIndex();
0N/A if (newIdx >= 0) {
0N/A // Update the selected item in the target now that
0N/A // the mouse selection is complete.
0N/A if (newIdx != dragStartIdx) {
0N/A ((Choice)target).select(newIdx);
0N/A // NOTE: We get a repaint when Choice.select()
0N/A // calls our peer.select().
0N/A }
0N/A if (wasDragged && e.getButton() != MouseEvent.BUTTON1){
0N/A ((Choice)target).select(dragStartIdx);
0N/A }
0N/A
0N/A /*fix for 6239941 : Choice triggers ItemEvent when selecting an item with right mouse button, Xtoolkit
0N/A * We should generate ItemEvent if only
0N/A * LeftMouseButton used */
0N/A if (e.getButton() == MouseEvent.BUTTON1 &&
0N/A (!firstPress || wasDragged ))
0N/A {
0N/A postEvent(new ItemEvent((Choice)target,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A ((Choice)target).getItem(newIdx),
0N/A ItemEvent.SELECTED));
0N/A }
0N/A
0N/A // see 6240074 for more information
0N/A if (choiceListener != null) {
0N/A choiceListener.unfurledChoiceClosing();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A // See 6243382 for more information
0N/A unfurledChoice.trackMouse(e);
0N/A }
0N/A }
0N/A
0N/A dragging = false;
0N/A wasDragged = false;
0N/A firstPress = false;
0N/A dragStartIdx = -1;
0N/A }
0N/A
0N/A public void mouseDragged(MouseEvent e) {
0N/A /*
0N/A * fix for 5003166. On Motif user are unable to drag
0N/A * mouse inside opened Choice if he drags the mouse with
0N/A * different from LEFT mouse button ( e.g. RIGHT or MIDDLE).
0N/A * This fix make impossible to drag mouse inside opened choice
0N/A * with other mouse buttons rather then LEFT one.
0N/A */
0N/A if ( e.getModifiers() == MouseEvent.BUTTON1_MASK ){
0N/A dragging = true;
0N/A wasDragged = true;
0N/A unfurledChoice.trackMouse(e);
0N/A }
0N/A }
0N/A
0N/A // Stolen from TinyChoicePeer
0N/A public Dimension getMinimumSize() {
0N/A // TODO: move this impl into ListHelper?
0N/A FontMetrics fm = getFontMetrics(target.getFont());
0N/A Choice c = (Choice)target;
0N/A int w = 0;
0N/A for (int i = c.countItems() ; i-- > 0 ;) {
0N/A w = Math.max(fm.stringWidth(c.getItem(i)), w);
0N/A }
0N/A return new Dimension(w + TEXT_XPAD + WIDGET_OFFSET,
0N/A fm.getMaxAscent() + fm.getMaxDescent() + TEXT_YPAD);
0N/A }
0N/A
0N/A /*
0N/A * Layout the...
0N/A */
0N/A public void layout() {
0N/A /*
0N/A Dimension size = target.getSize();
0N/A Font f = target.getFont();
0N/A FontMetrics fm = target.getFontMetrics(f);
0N/A String text = ((Choice)target).getLabel();
0N/A
0N/A textRect.height = fm.getHeight();
0N/A
0N/A checkBoxSize = getChoiceSize(fm);
0N/A
0N/A // Note - Motif appears to use an left inset that is slightly
0N/A // scaled to the checkbox/font size.
0N/A cbX = borderInsets.left + checkBoxInsetFromText;
0N/A cbY = size.height / 2 - checkBoxSize / 2;
0N/A int minTextX = borderInsets.left + 2 * checkBoxInsetFromText + checkBoxSize;
0N/A // FIXME: will need to account for alignment?
0N/A // FIXME: call layout() on alignment changes
0N/A //textRect.width = fm.stringWidth(text);
0N/A textRect.width = fm.stringWidth(text == null ? "" : text);
0N/A textRect.x = Math.max(minTextX, size.width / 2 - textRect.width / 2);
0N/A textRect.y = size.height / 2 - textRect.height / 2 + borderInsets.top;
0N/A
0N/A focusRect.x = focusInsets.left;
0N/A focusRect.y = focusInsets.top;
0N/A focusRect.width = size.width-(focusInsets.left+focusInsets.right)-1;
0N/A focusRect.height = size.height-(focusInsets.top+focusInsets.bottom)-1;
0N/A
0N/A myCheckMark = AffineTransform.getScaleInstance((double)target.getFont().getSize() / MASTER_SIZE, (double)target.getFont().getSize() / MASTER_SIZE).createTransformedShape(MASTER_CHECKMARK);
0N/A */
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Paint the choice
0N/A */
0N/A public void paint(Graphics g) {
0N/A flush();
0N/A Dimension size = getPeerSize();
0N/A
0N/A // TODO: when mouse is down over button, widget should be drawn depressed
0N/A g.setColor(getPeerBackground());
0N/A g.fillRect(0, 0, width, height);
0N/A
0N/A drawMotif3DRect(g, 1, 1, width-2, height-2, false);
0N/A drawMotif3DRect(g, width - WIDGET_OFFSET, (height / 2) - 3, 12, 6, false);
0N/A
0N/A if (!helper.isEmpty() && helper.getSelectedIndex() != -1) {
0N/A g.setFont(getPeerFont());
0N/A FontMetrics fm = g.getFontMetrics();
0N/A String lbl = helper.getItem(helper.getSelectedIndex());
0N/A if (lbl != null && drawSelectedItem) {
0N/A g.setClip(1, 1, width - WIDGET_OFFSET - 2, height);
0N/A if (isEnabled()) {
0N/A g.setColor(getPeerForeground());
0N/A g.drawString(lbl, 5, (height + fm.getMaxAscent()-fm.getMaxDescent())/2);
0N/A }
0N/A else {
0N/A g.setColor(getPeerBackground().brighter());
0N/A g.drawString(lbl, 5, (height + fm.getMaxAscent()-fm.getMaxDescent())/2);
0N/A g.setColor(getPeerBackground().darker());
0N/A g.drawString(lbl, 4, ((height + fm.getMaxAscent()-fm.getMaxDescent())/2)-1);
0N/A }
0N/A g.setClip(0, 0, width, height);
0N/A }
0N/A }
0N/A if (hasFocus()) {
0N/A paintFocus(g,focusInsets.left,focusInsets.top,size.width-(focusInsets.left+focusInsets.right)-1,size.height-(focusInsets.top+focusInsets.bottom)-1);
0N/A }
0N/A if (unfurled) {
0N/A unfurledChoice.repaint();
0N/A }
0N/A flush();
0N/A }
0N/A
0N/A protected void paintFocus(Graphics g,
0N/A int x, int y, int w, int h) {
0N/A g.setColor(focusColor);
0N/A g.drawRect(x,y,w,h);
0N/A }
0N/A
0N/A
0N/A
0N/A /*
0N/A * ChoicePeer methods stolen from TinyChoicePeer
0N/A */
0N/A
0N/A public void select(int index) {
0N/A helper.select(index);
0N/A helper.setFocusedIndex(index);
0N/A repaint();
0N/A }
0N/A
0N/A public void add(String item, int index) {
0N/A helper.add(item, index);
0N/A repaint();
0N/A }
0N/A
0N/A public void remove(int index) {
0N/A boolean selected = (index == helper.getSelectedIndex());
0N/A boolean visibled = (index >= helper.firstDisplayedIndex() && index <= helper.lastDisplayedIndex());
0N/A helper.remove(index);
0N/A if (selected) {
0N/A if (helper.isEmpty()) {
0N/A helper.select(-1);
0N/A }
0N/A else {
0N/A helper.select(0);
0N/A }
0N/A }
0N/A /*
0N/A * Fix for 6248016
0N/A * After removing the item of the choice we need to reshape unfurled choice
0N/A * in order to keep actual bounds of the choice
0N/A */
0N/A
0N/A /*
0N/A * condition added only for performance
0N/A */
0N/A if (!unfurled) {
0N/A // Fix 6292186: PIT: Choice is not refreshed properly when the last item gets removed, XToolkit
0N/A // We should take into account that there is no 'select' invoking (hence 'repaint')
0N/A // if the choice is empty (see Choice.java method removeNoInvalidate())
0N/A // The condition isn't 'visibled' since it would be cause of the twice repainting
0N/A if (helper.isEmpty()) {
0N/A repaint();
0N/A }
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * condition added only for performance
0N/A * the count of the visible items changed
0N/A */
0N/A if (visibled){
0N/A Rectangle r = unfurledChoice.placeOnScreen();
0N/A unfurledChoice.reshape(r.x, r.y, r.width, r.height);
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * condition added only for performance
0N/A * the structure of visible items changed
0N/A * if removable item is non visible and non selected then there is no repaint
0N/A */
0N/A if (visibled || selected){
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A public void removeAll() {
0N/A helper.removeAll();
0N/A helper.select(-1);
0N/A /*
0N/A * Fix for 6248016
0N/A * After removing the item of the choice we need to reshape unfurled choice
0N/A * in order to keep actual bounds of the choice
0N/A */
0N/A Rectangle r = unfurledChoice.placeOnScreen();
0N/A unfurledChoice.reshape(r.x, r.y, r.width, r.height);
0N/A repaint();
0N/A }
0N/A
0N/A /**
0N/A * DEPRECATED: Replaced by add(String, int).
0N/A */
0N/A public void addItem(String item, int index) {
0N/A add(item, index);
0N/A }
0N/A
0N/A public void setFont(Font font) {
0N/A super.setFont(font);
0N/A helper.setFont(this.font);
0N/A }
0N/A
0N/A public void setForeground(Color c) {
0N/A super.setForeground(c);
0N/A helper.updateColors(getGUIcolors());
0N/A }
0N/A
0N/A public void setBackground(Color c) {
0N/A super.setBackground(c);
0N/A unfurledChoice.setBackground(c);
0N/A helper.updateColors(getGUIcolors());
0N/A updateMotifColors(c);
0N/A }
0N/A
0N/A public void setDrawSelectedItem(boolean value) {
0N/A drawSelectedItem = value;
0N/A }
0N/A
0N/A public void setAlignUnder(Component comp) {
0N/A alignUnder = comp;
0N/A }
0N/A
0N/A // see 6240074 for more information
0N/A public void addXChoicePeerListener(XChoicePeerListener l){
0N/A choiceListener = l;
0N/A }
0N/A
0N/A // see 6240074 for more information
0N/A public void removeXChoicePeerListener(){
0N/A choiceListener = null;
0N/A }
0N/A
0N/A public boolean isUnfurled(){
0N/A return unfurled;
0N/A }
0N/A
0N/A /* fix for 6261352. We should detect if current parent Window (containing a Choice) become iconified and hide pop-down menu with grab release.
0N/A * In this case we should hide pop-down menu.
0N/A */
0N/A //calls from XWindowPeer. Could accept X-styled state events
0N/A public void stateChangedICCCM(int oldState, int newState) {
0N/A if (unfurled && oldState != newState){
0N/A hidePopdownMenu();
0N/A }
0N/A }
0N/A
0N/A //calls from XFramePeer. Could accept Frame's states.
0N/A public void stateChangedJava(int oldState, int newState) {
0N/A if (unfurled && oldState != newState){
0N/A hidePopdownMenu();
0N/A }
0N/A }
0N/A
0N/A /**************************************************************************/
0N/A /* Common functionality between List & Choice
0N/A /**************************************************************************/
0N/A
0N/A /**
0N/A * Inner class for the unfurled Choice list
0N/A * Much, much more docs
0N/A */
0N/A class UnfurledChoice extends XWindow /*implements XScrollbarClient*/ {
0N/A
0N/A // First try - use Choice as the target
0N/A
0N/A public UnfurledChoice(Component target) {
0N/A super(target);
0N/A }
0N/A
0N/A // Override so we can do our own create()
0N/A public void preInit(XCreateWindowParams params) {
0N/A // A parent of this window is the target, at this point: wrong.
0N/A // Remove parent window; in the following preInit() call we'll calculate as a default
0N/A // a correct root window which is the proper parent for override redirect.
0N/A params.delete(PARENT_WINDOW);
0N/A super.preInit(params);
0N/A // Reset bounds(we'll set them later), set overrideRedirect
0N/A params.remove(BOUNDS);
0N/A params.add(OVERRIDE_REDIRECT, Boolean.TRUE);
0N/A }
0N/A
0N/A // Generally, bounds should be:
0N/A // x = target.x
0N/A // y = target.y + target.height
0N/A // w = Max(target.width, getLongestItemWidth) + possible vertScrollbar
0N/A // h = Min(MAX_UNFURLED_ITEMS, target.getItemCount()) * itemHeight
0N/A Rectangle placeOnScreen() {
0N/A int numItemsDisplayed;
0N/A // Motif paints an empty Choice the same size as a single item
0N/A if (helper.isEmpty()) {
0N/A numItemsDisplayed = 1;
0N/A }
0N/A else {
0N/A int numItems = helper.getItemCount();
0N/A numItemsDisplayed = Math.min(MAX_UNFURLED_ITEMS, numItems);
0N/A }
0N/A Point global = XChoicePeer.this.toGlobal(0,0);
0N/A Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
0N/A
0N/A if (alignUnder != null) {
0N/A Rectangle choiceRec = XChoicePeer.this.getBounds();
0N/A choiceRec.setLocation(0, 0);
0N/A choiceRec = XChoicePeer.this.toGlobal(choiceRec);
0N/A Rectangle alignUnderRec = new Rectangle(alignUnder.getLocationOnScreen(), alignUnder.getSize()); // TODO: Security?
0N/A Rectangle result = choiceRec.union(alignUnderRec);
0N/A // we've got the left and width, calculate top and height
0N/A width = result.width;
0N/A x = result.x;
0N/A y = result.y + result.height;
0N/A height = 2*BORDER_WIDTH +
0N/A numItemsDisplayed*(helper.getItemHeight()+2*ITEM_MARGIN);
0N/A } else {
0N/A x = global.x;
0N/A y = global.y + XChoicePeer.this.height;
0N/A width = Math.max(XChoicePeer.this.width,
0N/A helper.getMaxItemWidth() + 2 * (BORDER_WIDTH + ITEM_MARGIN + TEXT_SPACE) + (helper.isVSBVisible() ? SCROLLBAR_WIDTH : 0));
0N/A height = 2*BORDER_WIDTH +
0N/A numItemsDisplayed*(helper.getItemHeight()+2*ITEM_MARGIN);
0N/A }
0N/A // Don't run off the edge of the screen
0N/A if (x < 0) {
0N/A x = 0;
0N/A }
0N/A else if (x + width > screen.width) {
0N/A x = screen.width - width;
0N/A }
0N/A
0N/A if (y < 0) {
0N/A y = 0;
0N/A }
0N/A else if (y + height > screen.height) {
0N/A y = screen.height - height;
0N/A }
0N/A return new Rectangle(x, y, width, height);
0N/A }
0N/A
0N/A public void toFront() {
0N/A // see 6240074 for more information
0N/A if (choiceListener != null)
0N/A choiceListener.unfurledChoiceOpening(helper);
0N/A
0N/A Rectangle r = placeOnScreen();
0N/A reshape(r.x, r.y, r.width, r.height);
0N/A super.toFront();
0N/A setVisible(true);
0N/A }
0N/A
0N/A /*
0N/A * Track a MouseEvent (either a drag or a press) and paint a new
0N/A * selected item, if necessary.
0N/A */
0N/A // FIXME: first unfurl after move is not at edge of the screen onto second monitor doesn't
0N/A // track mouse correctly. Problem is w/ UnfurledChoice coords
0N/A public void trackMouse(MouseEvent e) {
0N/A // Event coords are relative to the button, so translate a bit
0N/A Point local = toLocalCoords(e);
0N/A
0N/A // If x,y is over unfurled Choice,
0N/A // highlight item under cursor
0N/A
0N/A switch (e.getID()) {
0N/A case MouseEvent.MOUSE_PRESSED:
0N/A // FIXME: If the Choice is unfurled and the mouse is pressed
0N/A // outside of the Choice, the mouse should ungrab on the
0N/A // the press, not the release
0N/A if (helper.isInVertSB(getBounds(), local.x, local.y)) {
0N/A mouseInSB = true;
0N/A helper.handleVSBEvent(e, getBounds(), local.x, local.y);
0N/A }
0N/A else {
0N/A trackSelection(local.x, local.y);
0N/A }
0N/A break;
0N/A case MouseEvent.MOUSE_RELEASED:
0N/A if (mouseInSB) {
0N/A mouseInSB = false;
0N/A helper.handleVSBEvent(e, getBounds(), local.x, local.y);
0N/A }else{
0N/A // See 6243382 for more information
0N/A helper.trackMouseReleasedScroll();
0N/A }
0N/A /*
0N/A else {
0N/A trackSelection(local.x, local.y);
0N/A }
0N/A */
0N/A break;
0N/A case MouseEvent.MOUSE_DRAGGED:
0N/A if (mouseInSB) {
0N/A helper.handleVSBEvent(e, getBounds(), local.x, local.y);
0N/A }
0N/A else {
0N/A // See 6243382 for more information
0N/A helper.trackMouseDraggedScroll(local.x, local.y, width, height);
0N/A trackSelection(local.x, local.y);
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A
0N/A private void trackSelection(int transX, int transY) {
0N/A if (!helper.isEmpty()) {
0N/A if (transX > 0 && transX < width &&
0N/A transY > 0 && transY < height) {
0N/A int newIdx = helper.y2index(transY);
1696N/A if (log.isLoggable(PlatformLogger.FINE)) {
0N/A log.fine("transX=" + transX + ", transY=" + transY
0N/A + ",width=" + width + ", height=" + height
0N/A + ", newIdx=" + newIdx + " on " + target);
0N/A }
0N/A if ((newIdx >=0) && (newIdx < helper.getItemCount())
0N/A && (newIdx != helper.getSelectedIndex()))
0N/A {
0N/A helper.select(newIdx);
0N/A unfurledChoice.repaint();
0N/A }
0N/A }
0N/A }
0N/A // FIXME: If dragged off top or bottom, scroll if there's a vsb
0N/A // (ICK - we'll need a timer or our own event or something)
0N/A }
0N/A
0N/A /*
0N/A * fillRect with current Background color on the whole dropdown list.
0N/A */
0N/A public void paintBackground(){
0N/A Graphics g = getGraphics();
0N/A g.setColor(getPeerBackground());
0N/A g.fillRect(0, 0, width, height);
0N/A }
0N/A
0N/A /*
0N/A * 6405689. In some cases we should erase background to eliminate painting
0N/A * artefacts.
0N/A */
0N/A public void repaint() {
0N/A if (!isVisible()) {
0N/A return;
0N/A }
0N/A if (helper.checkVsbVisibilityChangedAndReset()){
0N/A paintBackground();
0N/A }
0N/A super.repaint();
0N/A }
0N/A
0N/A public void paint(Graphics g) {
0N/A //System.out.println("UC.paint()");
0N/A Choice choice = (Choice)target;
0N/A Color colors[] = XChoicePeer.this.getGUIcolors();
0N/A draw3DRect(g, getSystemColors(), 0, 0, width - 1, height - 1, true);
0N/A draw3DRect(g, getSystemColors(), 1, 1, width - 3, height - 3, true);
0N/A
0N/A helper.paintAllItems(g,
0N/A colors,
0N/A getBounds());
0N/A }
0N/A
0N/A public void setVisible(boolean vis) {
0N/A xSetVisible(vis);
0N/A
0N/A if (!vis && alignUnder != null) {
0N/A alignUnder.requestFocusInWindow();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return a MouseEvent's Point in coordinates relative to the
0N/A * UnfurledChoice.
0N/A */
0N/A private Point toLocalCoords(MouseEvent e) {
0N/A // Event coords are relative to the button, so translate a bit
0N/A Point global = e.getLocationOnScreen();
0N/A
0N/A global.x -= x;
0N/A global.y -= y;
0N/A return global;
0N/A }
0N/A
0N/A /* Returns true if the MouseEvent coords (which are based on the Choice)
0N/A * are inside of the UnfurledChoice.
0N/A */
0N/A private boolean isMouseEventInside(MouseEvent e) {
0N/A Point local = toLocalCoords(e);
0N/A if (local.x > 0 && local.x < width &&
0N/A local.y > 0 && local.y < height) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Tests if the mouse cursor is in the Unfurled Choice, yet not
0N/A * in the vertical scrollbar
0N/A */
0N/A private boolean isMouseInListArea(MouseEvent e) {
0N/A if (isMouseEventInside(e)) {
0N/A Point local = toLocalCoords(e);
0N/A Rectangle bounds = getBounds();
0N/A if (!helper.isInVertSB(bounds, local.x, local.y)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Overridden from XWindow() because we don't want to send
0N/A * ComponentEvents
0N/A */
0N/A public void handleConfigureNotifyEvent(XEvent xev) {}
0N/A public void handleMapNotifyEvent(XEvent xev) {}
0N/A public void handleUnmapNotifyEvent(XEvent xev) {}
0N/A } //UnfurledChoice
0N/A
0N/A public void dispose() {
0N/A if (unfurledChoice != null) {
0N/A unfurledChoice.destroy();
0N/A }
0N/A super.dispose();
0N/A }
0N/A
0N/A /*
0N/A * fix for 6239938 : Choice drop-down does not disappear when it loses
0N/A * focus, on XToolkit
0N/A * We are able to handle all _Key_ events received by Choice when
0N/A * it is in opened state without sending it to EventQueue.
0N/A * If Choice is in closed state we should behave like before: send
0N/A * all events to EventQueue.
0N/A * To be compatible with Motif we should handle all KeyEvents in
0N/A * Choice if it is opened. KeyEvents should be sent into Java if Choice is not opened.
0N/A */
0N/A boolean prePostEvent(final AWTEvent e) {
0N/A if (unfurled){
0N/A // fix for 6253211: PIT: MouseWheel events not triggered for Choice drop down in XAWT
0N/A if (e instanceof MouseWheelEvent){
0N/A return super.prePostEvent(e);
0N/A }
0N/A //fix 6252982: PIT: Keyboard FocusTraversal not working when choice's drop-down is visible, on XToolkit
0N/A if (e instanceof KeyEvent){
0N/A // notify XWindow that this event had been already handled and no need to post it again
6441N/A InvocationEvent ev = new InvocationEvent(target, new Runnable() {
6441N/A public void run() {
6441N/A if(target.isFocusable() &&
6441N/A getParentTopLevel().isFocusableWindow() )
6441N/A {
6441N/A handleJavaKeyEvent((KeyEvent)e);
0N/A }
6441N/A }
6441N/A });
6441N/A postEvent(ev);
6441N/A
0N/A return true;
0N/A } else {
0N/A if (e instanceof MouseEvent){
0N/A // Fix for 6240046 : REG:Choice's Drop-down does not disappear when clicking somewhere, after popup menu is disposed
0N/A // if user presses Right Mouse Button on opened (unfurled)
0N/A // Choice then we mustn't open a popup menu. We could filter
0N/A // Mouse Events and handle them in XChoicePeer if Choice
0N/A // currently in opened state.
0N/A MouseEvent me = (MouseEvent)e;
0N/A int eventId = e.getID();
0N/A // fix 6251983: PIT: MouseDragged events not triggered
0N/A // fix 6251988: PIT: Choice consumes MouseReleased, MouseClicked events when clicking it with left button,
0N/A if ((unfurledChoice.isMouseEventInside(me) ||
0N/A (!firstPress && eventId == MouseEvent.MOUSE_DRAGGED)))
0N/A {
0N/A return handleMouseEventByChoice(me);
0N/A }
0N/A // MouseMoved events should be fired in Choice's comp if it's not opened
0N/A // Shouldn't generate Moved Events. CR : 6251995
0N/A if (eventId == MouseEvent.MOUSE_MOVED){
0N/A return handleMouseEventByChoice(me);
0N/A }
0N/A //fix for 6272965: PIT: Choice triggers MousePressed when pressing mouse outside comp while drop-down is active, XTkt
0N/A if ( !firstPress && !( isMouseEventInChoice(me) ||
0N/A unfurledChoice.isMouseEventInside(me)) &&
0N/A ( eventId == MouseEvent.MOUSE_PRESSED ||
0N/A eventId == MouseEvent.MOUSE_RELEASED ||
0N/A eventId == MouseEvent.MOUSE_CLICKED )
0N/A )
0N/A {
0N/A return handleMouseEventByChoice(me);
0N/A }
0N/A }
0N/A }//else KeyEvent
0N/A }//if unfurled
0N/A return super.prePostEvent(e);
0N/A }
0N/A
0N/A //convenient method
0N/A //do not generate this kind of Events
0N/A public boolean handleMouseEventByChoice(final MouseEvent me){
6441N/A InvocationEvent ev = new InvocationEvent(target, new Runnable() {
6441N/A public void run() {
6441N/A handleJavaMouseEvent(me);
6441N/A }
6441N/A });
6441N/A postEvent(ev);
6441N/A
0N/A return true;
0N/A }
0N/A
0N/A /* Returns true if the MouseEvent coords
0N/A * are inside of the Choice itself (it doesnt's depends on
0N/A * if this choice opened or not).
0N/A */
0N/A private boolean isMouseEventInChoice(MouseEvent e) {
0N/A int x = e.getX();
0N/A int y = e.getY();
0N/A Rectangle choiceRect = getBounds();
0N/A
0N/A if (x < 0 || x > choiceRect.width ||
0N/A y < 0 || y > choiceRect.height)
0N/A {
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * The listener interface for receiving "interesting" for XFileDialogPeer
0N/A * choice events (opening, closing).
0N/A * The listener added by means of the method addXChoicePeerListener
0N/A * A opening choice event is generated when the invoking unfurledChoice.toFront()
0N/A * A closing choice event is generated at the time of the processing the mouse releasing
0N/A * and the Enter pressing.
0N/A * see 6240074 for more information
0N/A */
0N/Ainterface XChoicePeerListener{
0N/A public void unfurledChoiceOpening(ListHelper choiceHelper);
0N/A public void unfurledChoiceClosing();
0N/A}