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