home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / plaf / basic / BasicComboPopup.class (.txt) < prev    next >
Encoding:
Java Class File  |  1979-12-31  |  12.3 KB  |  423 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Dimension;
  8. import java.awt.Point;
  9. import java.awt.Rectangle;
  10. import java.awt.Toolkit;
  11. import java.awt.event.InputEvent;
  12. import java.awt.event.ItemListener;
  13. import java.awt.event.KeyListener;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.event.MouseListener;
  16. import java.awt.event.MouseMotionListener;
  17. import java.beans.PropertyChangeListener;
  18. import java.util.EventObject;
  19. import javax.swing.BorderFactory;
  20. import javax.swing.BoxLayout;
  21. import javax.swing.ComboBoxModel;
  22. import javax.swing.JComboBox;
  23. import javax.swing.JComponent;
  24. import javax.swing.JList;
  25. import javax.swing.JPopupMenu;
  26. import javax.swing.JScrollPane;
  27. import javax.swing.KeyStroke;
  28. import javax.swing.ListCellRenderer;
  29. import javax.swing.ListModel;
  30. import javax.swing.MenuElement;
  31. import javax.swing.MenuSelectionManager;
  32. import javax.swing.SwingUtilities;
  33. import javax.swing.Timer;
  34. import javax.swing.UIManager;
  35. import javax.swing.border.Border;
  36. import javax.swing.event.ListDataListener;
  37. import javax.swing.event.ListSelectionListener;
  38.  
  39. public class BasicComboPopup extends JPopupMenu implements ComboPopup {
  40.    static final ListModel EmptyListModel = new 1();
  41.    protected JComboBox comboBox;
  42.    protected JList list;
  43.    protected JScrollPane scroller;
  44.    protected boolean valueIsAdjusting = false;
  45.    protected MouseMotionListener mouseMotionListener;
  46.    protected MouseListener mouseListener;
  47.    protected KeyListener keyListener;
  48.    protected ListSelectionListener listSelectionListener;
  49.    protected ListDataListener listDataListener;
  50.    protected MouseListener listMouseListener;
  51.    protected MouseMotionListener listMouseMotionListener;
  52.    protected PropertyChangeListener propertyChangeListener;
  53.    protected ItemListener itemListener;
  54.    protected Timer autoscrollTimer;
  55.    protected boolean hasEntered = false;
  56.    protected boolean isAutoScrolling = false;
  57.    protected int scrollDirection = 0;
  58.    protected static final int SCROLL_UP = 0;
  59.    protected static final int SCROLL_DOWN = 1;
  60.    private boolean lightNav = false;
  61.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION = "JComboBox.lightweightKeyboardNavigation";
  62.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION_ON = "Lightweight";
  63.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION_OFF = "Heavyweight";
  64.  
  65.    public void show() {
  66.       Dimension var1 = this.comboBox.getSize();
  67.       var1.setSize(var1.width, this.getPopupHeightForRowCount(this.comboBox.getMaximumRowCount()));
  68.       Rectangle var2 = this.computePopupBounds(0, this.comboBox.getBounds().height, var1.width, var1.height);
  69.       this.scroller.setMaximumSize(var2.getSize());
  70.       this.scroller.setPreferredSize(var2.getSize());
  71.       this.scroller.setMinimumSize(var2.getSize());
  72.       this.list.invalidate();
  73.       this.syncListSelectionWithComboBoxSelection();
  74.       this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
  75.       ((JPopupMenu)this).setLightWeightPopupEnabled(this.comboBox.isLightWeightPopupEnabled());
  76.       ((JPopupMenu)this).show(this.comboBox, var2.x, var2.y);
  77.    }
  78.  
  79.    public void hide() {
  80.       MenuSelectionManager var1 = MenuSelectionManager.defaultManager();
  81.       MenuElement[] var2 = var1.getSelectedPath();
  82.  
  83.       for(int var3 = 0; var3 < var2.length; ++var3) {
  84.          if (var2[var3] == this) {
  85.             var1.clearSelectedPath();
  86.             break;
  87.          }
  88.       }
  89.  
  90.       this.comboBox.repaint();
  91.    }
  92.  
  93.    public JList getList() {
  94.       return this.list;
  95.    }
  96.  
  97.    public MouseListener getMouseListener() {
  98.       return this.mouseListener;
  99.    }
  100.  
  101.    public MouseMotionListener getMouseMotionListener() {
  102.       return this.mouseMotionListener;
  103.    }
  104.  
  105.    public KeyListener getKeyListener() {
  106.       return this.keyListener;
  107.    }
  108.  
  109.    public void uninstallingUI() {
  110.       this.comboBox.removePropertyChangeListener(this.propertyChangeListener);
  111.       this.comboBox.removeItemListener(this.itemListener);
  112.       this.uninstallComboBoxModelListeners(this.comboBox.getModel());
  113.       this.uninstallKeyboardActions();
  114.       this.uninstallListListeners();
  115.       this.list.setModel(EmptyListModel);
  116.    }
  117.  
  118.    protected void uninstallComboBoxModelListeners(ComboBoxModel var1) {
  119.       if (var1 != null) {
  120.          var1.removeListDataListener(this.listDataListener);
  121.       }
  122.  
  123.    }
  124.  
  125.    protected void uninstallKeyboardActions() {
  126.       this.comboBox.unregisterKeyboardAction(KeyStroke.getKeyStroke(10, 0));
  127.    }
  128.  
  129.    public BasicComboPopup(JComboBox var1) {
  130.       this.comboBox = var1;
  131.       Object var2 = ((JComponent)var1).getClientProperty("JComboBox.lightweightKeyboardNavigation");
  132.       if (var2 != null) {
  133.          if (var2.equals("Lightweight")) {
  134.             this.lightNav = true;
  135.          } else if (var2.equals("Heavyweight")) {
  136.             this.lightNav = false;
  137.          }
  138.       }
  139.  
  140.       this.mouseListener = this.createMouseListener();
  141.       this.mouseMotionListener = this.createMouseMotionListener();
  142.       this.keyListener = this.createKeyListener();
  143.       this.listSelectionListener = this.createListSelectionListener();
  144.       this.listDataListener = this.createListDataListener();
  145.       this.listMouseListener = this.createListMouseListener();
  146.       this.listMouseMotionListener = this.createListMouseMotionListener();
  147.       this.propertyChangeListener = this.createPropertyChangeListener();
  148.       this.itemListener = this.createItemListener();
  149.       this.list = this.createList();
  150.       this.configureList();
  151.       this.scroller = this.createScroller();
  152.       this.configureScroller();
  153.       this.configurePopup();
  154.       this.installComboBoxListeners();
  155.       this.installKeyboardActions();
  156.    }
  157.  
  158.    protected MouseListener createMouseListener() {
  159.       return new InvocationMouseHandler(this);
  160.    }
  161.  
  162.    protected MouseMotionListener createMouseMotionListener() {
  163.       return new InvocationMouseMotionHandler(this);
  164.    }
  165.  
  166.    protected KeyListener createKeyListener() {
  167.       return new InvocationKeyHandler(this);
  168.    }
  169.  
  170.    protected ListSelectionListener createListSelectionListener() {
  171.       return new ListSelectionHandler(this);
  172.    }
  173.  
  174.    protected ListDataListener createListDataListener() {
  175.       return new ListDataHandler(this);
  176.    }
  177.  
  178.    protected MouseListener createListMouseListener() {
  179.       return new ListMouseHandler(this);
  180.    }
  181.  
  182.    protected MouseMotionListener createListMouseMotionListener() {
  183.       return new ListMouseMotionHandler(this);
  184.    }
  185.  
  186.    protected PropertyChangeListener createPropertyChangeListener() {
  187.       return new PropertyChangeHandler(this);
  188.    }
  189.  
  190.    protected ItemListener createItemListener() {
  191.       return new ItemHandler(this);
  192.    }
  193.  
  194.    protected JList createList() {
  195.       return new JList(this.comboBox.getModel());
  196.    }
  197.  
  198.    protected void configureList() {
  199.       this.list.setFont(this.comboBox.getFont());
  200.       this.list.setForeground(this.comboBox.getForeground());
  201.       this.list.setBackground(this.comboBox.getBackground());
  202.       this.list.setSelectionForeground(UIManager.getColor("ComboBox.selectionForeground"));
  203.       this.list.setSelectionBackground(UIManager.getColor("ComboBox.selectionBackground"));
  204.       this.list.setBorder((Border)null);
  205.       this.list.setCellRenderer(this.comboBox.getRenderer());
  206.       this.list.setRequestFocusEnabled(false);
  207.       this.syncListSelectionWithComboBoxSelection();
  208.       this.list.setSelectionMode(0);
  209.       this.installListListeners();
  210.    }
  211.  
  212.    protected void installListListeners() {
  213.       this.list.addListSelectionListener(this.listSelectionListener);
  214.       this.list.addMouseMotionListener(this.listMouseMotionListener);
  215.       this.list.addMouseListener(this.listMouseListener);
  216.    }
  217.  
  218.    void uninstallListListeners() {
  219.       this.list.removeListSelectionListener(this.listSelectionListener);
  220.       this.list.removeMouseMotionListener(this.listMouseMotionListener);
  221.       this.list.removeMouseListener(this.listMouseListener);
  222.    }
  223.  
  224.    protected JScrollPane createScroller() {
  225.       return new JScrollPane(this.list, 20, 31);
  226.    }
  227.  
  228.    protected void configureScroller() {
  229.       this.scroller.setRequestFocusEnabled(false);
  230.       this.scroller.getVerticalScrollBar().setRequestFocusEnabled(false);
  231.       this.scroller.setBorder((Border)null);
  232.    }
  233.  
  234.    protected void configurePopup() {
  235.       ((Container)this).setLayout(new BoxLayout(this, 1));
  236.       ((JPopupMenu)this).setBorderPainted(true);
  237.       ((JComponent)this).setBorder(BorderFactory.createLineBorder(Color.black));
  238.       ((JComponent)this).setOpaque(false);
  239.       ((Container)this).add(this.scroller);
  240.       ((JComponent)this).setDoubleBuffered(true);
  241.       ((JComponent)this).setRequestFocusEnabled(false);
  242.    }
  243.  
  244.    protected void installComboBoxListeners() {
  245.       this.comboBox.addPropertyChangeListener(this.propertyChangeListener);
  246.       this.comboBox.addItemListener(this.itemListener);
  247.       this.installComboBoxModelListeners(this.comboBox.getModel());
  248.    }
  249.  
  250.    protected void installComboBoxModelListeners(ComboBoxModel var1) {
  251.       if (var1 != null) {
  252.          var1.addListDataListener(this.listDataListener);
  253.       }
  254.  
  255.    }
  256.  
  257.    protected void installKeyboardActions() {
  258.       2 var1 = new 2(this);
  259.       this.comboBox.registerKeyboardAction(var1, KeyStroke.getKeyStroke(10, 0), 1);
  260.    }
  261.  
  262.    public boolean isFocusTraversable() {
  263.       return false;
  264.    }
  265.  
  266.    protected void startAutoScrolling(int var1) {
  267.       if (this.isAutoScrolling) {
  268.          this.autoscrollTimer.stop();
  269.       }
  270.  
  271.       this.isAutoScrolling = true;
  272.       if (var1 == 0) {
  273.          this.scrollDirection = 0;
  274.          Point var2 = SwingUtilities.convertPoint(this.scroller, new Point(1, 1), this.list);
  275.          int var3 = this.list.locationToIndex(var2);
  276.          this.valueIsAdjusting = true;
  277.          this.list.setSelectedIndex(var3);
  278.          this.valueIsAdjusting = false;
  279.          3 var4 = new 3(this);
  280.          this.autoscrollTimer = new Timer(100, var4);
  281.       } else if (var1 == 1) {
  282.          this.scrollDirection = 1;
  283.          Dimension var6 = this.scroller.getSize();
  284.          Point var7 = SwingUtilities.convertPoint(this.scroller, new Point(1, var6.height - 1 - 2), this.list);
  285.          int var8 = this.list.locationToIndex(var7);
  286.          this.valueIsAdjusting = true;
  287.          this.list.setSelectedIndex(var8);
  288.          this.valueIsAdjusting = false;
  289.          4 var5 = new 4(this);
  290.          this.autoscrollTimer = new Timer(100, var5);
  291.       }
  292.  
  293.       this.autoscrollTimer.start();
  294.    }
  295.  
  296.    protected void stopAutoScrolling() {
  297.       this.isAutoScrolling = false;
  298.       if (this.autoscrollTimer != null) {
  299.          this.autoscrollTimer.stop();
  300.          this.autoscrollTimer = null;
  301.       }
  302.  
  303.    }
  304.  
  305.    protected void autoScrollUp() {
  306.       int var1 = this.list.getSelectedIndex();
  307.       if (var1 > 0) {
  308.          this.valueIsAdjusting = true;
  309.          this.list.setSelectedIndex(var1 - 1);
  310.          this.valueIsAdjusting = false;
  311.          this.list.ensureIndexIsVisible(var1 - 1);
  312.       }
  313.  
  314.    }
  315.  
  316.    protected void autoScrollDown() {
  317.       int var1 = this.list.getSelectedIndex();
  318.       int var2 = this.list.getModel().getSize() - 1;
  319.       if (var1 < var2) {
  320.          this.valueIsAdjusting = true;
  321.          this.list.setSelectedIndex(var1 + 1);
  322.          this.valueIsAdjusting = false;
  323.          this.list.ensureIndexIsVisible(var1 + 1);
  324.       }
  325.  
  326.    }
  327.  
  328.    protected void delegateFocus(MouseEvent var1) {
  329.       if (this.comboBox.isEditable()) {
  330.          this.comboBox.getEditor().getEditorComponent().requestFocus();
  331.       } else {
  332.          this.comboBox.requestFocus();
  333.       }
  334.  
  335.    }
  336.  
  337.    protected void togglePopup() {
  338.       if (((JPopupMenu)this).isVisible()) {
  339.          this.hide();
  340.       } else {
  341.          this.show();
  342.       }
  343.  
  344.    }
  345.  
  346.    void syncListSelectionWithComboBoxSelection() {
  347.       int var1 = this.comboBox.getSelectedIndex();
  348.       if (var1 == -1) {
  349.          this.list.clearSelection();
  350.       } else {
  351.          this.list.setSelectedIndex(var1);
  352.       }
  353.  
  354.    }
  355.  
  356.    protected MouseEvent convertMouseEvent(MouseEvent var1) {
  357.       Point var2 = SwingUtilities.convertPoint((Component)((EventObject)var1).getSource(), var1.getPoint(), this.list);
  358.       MouseEvent var3 = new MouseEvent((Component)((EventObject)var1).getSource(), ((AWTEvent)var1).getID(), ((InputEvent)var1).getWhen(), ((InputEvent)var1).getModifiers(), var2.x, var2.y, ((InputEvent)var1).getModifiers(), var1.isPopupTrigger());
  359.       return var3;
  360.    }
  361.  
  362.    protected int getPopupHeightForRowCount(int var1) {
  363.       int var2 = this.comboBox.getModel().getSize();
  364.       int var3 = Math.min(var1, var2);
  365.       int var4 = 0;
  366.       ListCellRenderer var5 = this.list.getCellRenderer();
  367.       Object var6 = null;
  368.  
  369.       for(int var7 = 0; var7 < var3; ++var7) {
  370.          var6 = this.list.getModel().getElementAt(var7);
  371.          Component var8 = var5.getListCellRendererComponent(this.list, var6, var7, false, false);
  372.          var4 += var8.getPreferredSize().height;
  373.       }
  374.  
  375.       return var4 == 0 ? 100 : var4;
  376.    }
  377.  
  378.    protected Rectangle computePopupBounds(int var1, int var2, int var3, int var4) {
  379.       Rectangle var5 = new Rectangle();
  380.       Rectangle var6 = new Rectangle(var1, var2, var3, var4);
  381.       Point var7 = new Point(0, 0);
  382.       Dimension var8 = Toolkit.getDefaultToolkit().getScreenSize();
  383.       SwingUtilities.convertPointFromScreen(var7, this.comboBox);
  384.       var5.x = var7.x;
  385.       var5.y = var7.y;
  386.       var5.width = var8.width;
  387.       var5.height = var8.height;
  388.       return SwingUtilities.isRectangleContainingRectangle(var5, var6) ? var6 : new Rectangle(0, -var6.height, var6.width, var6.height);
  389.    }
  390.  
  391.    protected void updateListBoxSelectionForEvent(MouseEvent var1, boolean var2) {
  392.       Point var3 = var1.getPoint();
  393.       if (this.list != null) {
  394.          int var4 = this.list.locationToIndex(var3);
  395.          if (var4 == -1) {
  396.             if (var3.y < 0) {
  397.                var4 = 0;
  398.             } else {
  399.                var4 = this.comboBox.getModel().getSize() - 1;
  400.             }
  401.          }
  402.  
  403.          if (this.list.getSelectedIndex() != var4) {
  404.             this.list.setSelectedIndex(var4);
  405.             if (var2) {
  406.                this.list.ensureIndexIsVisible(var4);
  407.             }
  408.          }
  409.  
  410.       }
  411.    }
  412.  
  413.    // $FF: synthetic method
  414.    static boolean access$000(BasicComboPopup var0) {
  415.       return var0.lightNav;
  416.    }
  417.  
  418.    // $FF: synthetic method
  419.    static boolean access$002(BasicComboPopup var0, boolean var1) {
  420.       return var0.lightNav = var1;
  421.    }
  422. }
  423.