src/java.desktop/windows/classes/sun/awt/windows/WChoicePeer.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.awt.windows;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 import java.awt.event.ItemEvent;
  30 import java.awt.event.WindowEvent;
  31 import java.awt.event.WindowListener;
  32 import java.awt.event.WindowAdapter;


  33 import sun.awt.SunToolkit;
  34 
  35 final class WChoicePeer extends WComponentPeer implements ChoicePeer {
  36 
  37     // WComponentPeer overrides
  38 
  39     @Override
  40     public Dimension getMinimumSize() {
  41         FontMetrics fm = getFontMetrics(((Choice)target).getFont());
  42         Choice c = (Choice)target;
  43         int w = 0;
  44         for (int i = c.getItemCount() ; i-- > 0 ;) {
  45             w = Math.max(fm.stringWidth(c.getItem(i)), w);
  46         }
  47         return new Dimension(28 + w, Math.max(fm.getHeight() + 6, 15));
  48     }
  49     @Override
  50     public boolean isFocusable() {
  51         return true;
  52     }


  77     public void addItem(String item, int index) {
  78         addItems(new String[] {item}, index);
  79     }
  80     public native void addItems(String[] items, int index);
  81 
  82     @Override
  83     public synchronized native void reshape(int x, int y, int width, int height);
  84 
  85     private WindowListener windowListener;
  86 
  87     // Toolkit & peer internals
  88 
  89     WChoicePeer(Choice target) {
  90         super(target);
  91     }
  92 
  93     @Override
  94     native void create(WComponentPeer parent);
  95 
  96     @Override
  97     @SuppressWarnings("deprecation")
  98     void initialize() {
  99         Choice opt = (Choice)target;
 100         int itemCount = opt.getItemCount();
 101         if (itemCount > 0) {
 102             String[] items = new String[itemCount];
 103             for (int i=0; i < itemCount; i++) {
 104                 items[i] = opt.getItem(i);
 105             }
 106             addItems(items, 0);
 107             if (opt.getSelectedIndex() >= 0) {
 108                 select(opt.getSelectedIndex());
 109             }
 110         }
 111 
 112         Window parentWindow = SunToolkit.getContainingWindow((Component)target);
 113         if (parentWindow != null) {
 114             WWindowPeer wpeer = (WWindowPeer)parentWindow.getPeer();

 115             if (wpeer != null) {
 116                 windowListener = new WindowAdapter() {
 117                         @Override
 118                         public void windowIconified(WindowEvent e) {
 119                             closeList();
 120                         }
 121                         @Override
 122                         public void windowClosing(WindowEvent e) {
 123                             closeList();
 124                         }
 125                     };
 126                 wpeer.addWindowListener(windowListener);
 127             }
 128         }
 129         super.initialize();
 130     }
 131 
 132     @Override
 133     @SuppressWarnings("deprecation")
 134     protected void disposeImpl() {
 135         // TODO: we should somehow reset the listener when the choice
 136         // is moved to another toplevel without destroying its peer.
 137         Window parentWindow = SunToolkit.getContainingWindow((Component)target);
 138         if (parentWindow != null) {
 139             WWindowPeer wpeer = (WWindowPeer)parentWindow.getPeer();

 140             if (wpeer != null) {
 141                 wpeer.removeWindowListener(windowListener);
 142             }
 143         }
 144         super.disposeImpl();
 145     }
 146 
 147     // native callbacks
 148 
 149     void handleAction(final int index) {
 150         final Choice c = (Choice)target;
 151         WToolkit.executeOnEventHandlerThread(c, new Runnable() {
 152             @Override
 153             public void run() {
 154                 c.select(index);
 155                 postEvent(new ItemEvent(c, ItemEvent.ITEM_STATE_CHANGED,
 156                                 c.getItem(index), ItemEvent.SELECTED));
 157             }
 158         });
 159     }
   1 /*
   2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
   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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.awt.windows;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 import java.awt.event.ItemEvent;
  30 import java.awt.event.WindowEvent;
  31 import java.awt.event.WindowListener;
  32 import java.awt.event.WindowAdapter;
  33 
  34 import sun.awt.AWTAccessor;
  35 import sun.awt.SunToolkit;
  36 
  37 final class WChoicePeer extends WComponentPeer implements ChoicePeer {
  38 
  39     // WComponentPeer overrides
  40 
  41     @Override
  42     public Dimension getMinimumSize() {
  43         FontMetrics fm = getFontMetrics(((Choice)target).getFont());
  44         Choice c = (Choice)target;
  45         int w = 0;
  46         for (int i = c.getItemCount() ; i-- > 0 ;) {
  47             w = Math.max(fm.stringWidth(c.getItem(i)), w);
  48         }
  49         return new Dimension(28 + w, Math.max(fm.getHeight() + 6, 15));
  50     }
  51     @Override
  52     public boolean isFocusable() {
  53         return true;
  54     }


  79     public void addItem(String item, int index) {
  80         addItems(new String[] {item}, index);
  81     }
  82     public native void addItems(String[] items, int index);
  83 
  84     @Override
  85     public synchronized native void reshape(int x, int y, int width, int height);
  86 
  87     private WindowListener windowListener;
  88 
  89     // Toolkit & peer internals
  90 
  91     WChoicePeer(Choice target) {
  92         super(target);
  93     }
  94 
  95     @Override
  96     native void create(WComponentPeer parent);
  97 
  98     @Override

  99     void initialize() {
 100         Choice opt = (Choice)target;
 101         int itemCount = opt.getItemCount();
 102         if (itemCount > 0) {
 103             String[] items = new String[itemCount];
 104             for (int i=0; i < itemCount; i++) {
 105                 items[i] = opt.getItem(i);
 106             }
 107             addItems(items, 0);
 108             if (opt.getSelectedIndex() >= 0) {
 109                 select(opt.getSelectedIndex());
 110             }
 111         }
 112 
 113         Window parentWindow = SunToolkit.getContainingWindow((Component)target);
 114         if (parentWindow != null) {
 115             final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()
 116                                                  .getPeer(parentWindow);
 117             if (wpeer != null) {
 118                 windowListener = new WindowAdapter() {
 119                         @Override
 120                         public void windowIconified(WindowEvent e) {
 121                             closeList();
 122                         }
 123                         @Override
 124                         public void windowClosing(WindowEvent e) {
 125                             closeList();
 126                         }
 127                     };
 128                 wpeer.addWindowListener(windowListener);
 129             }
 130         }
 131         super.initialize();
 132     }
 133 
 134     @Override

 135     protected void disposeImpl() {
 136         // TODO: we should somehow reset the listener when the choice
 137         // is moved to another toplevel without destroying its peer.
 138         Window parentWindow = SunToolkit.getContainingWindow((Component)target);
 139         if (parentWindow != null) {
 140             final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()
 141                                                 .getPeer(parentWindow);
 142             if (wpeer != null) {
 143                 wpeer.removeWindowListener(windowListener);
 144             }
 145         }
 146         super.disposeImpl();
 147     }
 148 
 149     // native callbacks
 150 
 151     void handleAction(final int index) {
 152         final Choice c = (Choice)target;
 153         WToolkit.executeOnEventHandlerThread(c, new Runnable() {
 154             @Override
 155             public void run() {
 156                 c.select(index);
 157                 postEvent(new ItemEvent(c, ItemEvent.ITEM_STATE_CHANGED,
 158                                 c.getItem(index), ItemEvent.SELECTED));
 159             }
 160         });
 161     }