home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / plaf / basic / BasicComboBoxUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  14.6 KB  |  584 lines

  1. package javax.swing.plaf.basic;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Insets;
  8. import java.awt.LayoutManager;
  9. import java.awt.Rectangle;
  10. import java.awt.event.FocusListener;
  11. import java.awt.event.ItemListener;
  12. import java.awt.event.KeyListener;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.beans.PropertyChangeListener;
  16. import javax.accessibility.Accessible;
  17. import javax.accessibility.AccessibleContext;
  18. import javax.swing.CellRendererPane;
  19. import javax.swing.ComboBoxEditor;
  20. import javax.swing.ComboBoxModel;
  21. import javax.swing.JButton;
  22. import javax.swing.JComboBox;
  23. import javax.swing.JComponent;
  24. import javax.swing.JList;
  25. import javax.swing.JPopupMenu;
  26. import javax.swing.KeyStroke;
  27. import javax.swing.ListCellRenderer;
  28. import javax.swing.LookAndFeel;
  29. import javax.swing.SwingUtilities;
  30. import javax.swing.UIManager;
  31. import javax.swing.event.ListDataListener;
  32. import javax.swing.plaf.ComboBoxUI;
  33. import javax.swing.plaf.ComponentUI;
  34. import javax.swing.plaf.UIResource;
  35.  
  36. public class BasicComboBoxUI extends ComboBoxUI {
  37.    protected JComboBox comboBox;
  38.    protected boolean hasFocus = false;
  39.    private boolean lightNav = false;
  40.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION = "JComboBox.lightweightKeyboardNavigation";
  41.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION_ON = "Lightweight";
  42.    private static final String LIGHTWEIGHT_KEYBOARD_NAVIGATION_OFF = "Heavyweight";
  43.    protected JList listBox;
  44.    protected CellRendererPane currentValuePane = new CellRendererPane();
  45.    protected ComboPopup popup;
  46.    protected Component editor;
  47.    protected JButton arrowButton;
  48.    protected KeyListener keyListener;
  49.    protected FocusListener focusListener;
  50.    protected ItemListener itemListener;
  51.    protected PropertyChangeListener propertyChangeListener;
  52.    protected MouseListener popupMouseListener;
  53.    protected MouseMotionListener popupMouseMotionListener;
  54.    protected KeyListener popupKeyListener;
  55.    protected ListDataListener listDataListener;
  56.    protected boolean isMinimumSizeDirty = true;
  57.    protected Dimension cachedMinimumSize = new Dimension(0, 0);
  58.    Dimension cachedDisplaySize = new Dimension(0, 0);
  59.  
  60.    // $FF: synthetic method
  61.    static boolean access$0(BasicComboBoxUI var0) {
  62.       return var0.lightNav;
  63.    }
  64.  
  65.    // $FF: synthetic method
  66.    static void access$1(BasicComboBoxUI var0, boolean var1) {
  67.       var0.lightNav = var1;
  68.    }
  69.  
  70.    public void addEditor() {
  71.       this.removeEditor();
  72.       this.editor = this.comboBox.getEditor().getEditorComponent();
  73.       this.comboBox.add(this.editor);
  74.    }
  75.  
  76.    public void configureArrowButton() {
  77.       if (this.arrowButton != null) {
  78.          this.arrowButton.setEnabled(this.comboBox.isEnabled());
  79.          this.arrowButton.setRequestFocusEnabled(false);
  80.          if (this.popupMouseListener != null) {
  81.             this.arrowButton.addMouseListener(this.popupMouseListener);
  82.          }
  83.  
  84.          if (this.popupMouseMotionListener != null) {
  85.             this.arrowButton.addMouseMotionListener(this.popupMouseMotionListener);
  86.          }
  87.  
  88.          this.arrowButton.resetKeyboardActions();
  89.       }
  90.  
  91.    }
  92.  
  93.    protected void configureEditor() {
  94.       this.editor.setFont(this.comboBox.getFont());
  95.       if (this.popupKeyListener != null) {
  96.          this.editor.addKeyListener(this.popupKeyListener);
  97.       }
  98.  
  99.       if (this.editor instanceof Accessible) {
  100.          AccessibleContext var1 = ((Accessible)this.editor).getAccessibleContext();
  101.          if (var1 != null) {
  102.             var1.setAccessibleParent(this.comboBox);
  103.          }
  104.       }
  105.  
  106.       this.comboBox.configureEditor(this.comboBox.getEditor(), this.comboBox.getSelectedItem());
  107.    }
  108.  
  109.    protected JButton createArrowButton() {
  110.       return new BasicArrowButton(5);
  111.    }
  112.  
  113.    protected ComboBoxEditor createEditor() {
  114.       return new BasicComboBoxEditor.UIResource();
  115.    }
  116.  
  117.    protected FocusListener createFocusListener() {
  118.       return new FocusHandler(this);
  119.    }
  120.  
  121.    protected ItemListener createItemListener() {
  122.       return new ItemHandler(this);
  123.    }
  124.  
  125.    protected KeyListener createKeyListener() {
  126.       return new KeyHandler(this);
  127.    }
  128.  
  129.    protected LayoutManager createLayoutManager() {
  130.       return new ComboBoxLayoutManager(this);
  131.    }
  132.  
  133.    protected ListDataListener createListDataListener() {
  134.       return new ListDataHandler(this);
  135.    }
  136.  
  137.    protected ComboPopup createPopup() {
  138.       BasicComboPopup var1 = new BasicComboPopup(this.comboBox);
  139.       ((JPopupMenu)var1).getAccessibleContext().setAccessibleParent(this.comboBox);
  140.       return var1;
  141.    }
  142.  
  143.    protected PropertyChangeListener createPropertyChangeListener() {
  144.       return new PropertyChangeHandler(this);
  145.    }
  146.  
  147.    protected ListCellRenderer createRenderer() {
  148.       return new BasicComboBoxRenderer.UIResource();
  149.    }
  150.  
  151.    public static ComponentUI createUI(JComponent var0) {
  152.       return new BasicComboBoxUI();
  153.    }
  154.  
  155.    public Accessible getAccessibleChild(JComponent var1, int var2) {
  156.       switch (var2) {
  157.          case 0:
  158.             if (this.popup instanceof Accessible) {
  159.                AccessibleContext var4 = ((Accessible)this.popup).getAccessibleContext();
  160.                var4.setAccessibleParent(this.comboBox);
  161.                return (Accessible)this.popup;
  162.             }
  163.             break;
  164.          case 1:
  165.             if (this.comboBox.isEditable() && this.editor instanceof Accessible) {
  166.                AccessibleContext var3 = ((Accessible)this.editor).getAccessibleContext();
  167.                var3.setAccessibleParent(this.comboBox);
  168.                return (Accessible)this.editor;
  169.             }
  170.       }
  171.  
  172.       return null;
  173.    }
  174.  
  175.    public int getAccessibleChildrenCount(JComponent var1) {
  176.       return this.comboBox.isEditable() ? 2 : 1;
  177.    }
  178.  
  179.    protected Dimension getDefaultSize() {
  180.       return new Dimension(100, 20);
  181.    }
  182.  
  183.    protected Dimension getDisplaySize() {
  184.       Dimension var3 = new Dimension();
  185.       ListCellRenderer var4 = this.comboBox.getRenderer();
  186.       ComboBoxModel var5 = this.comboBox.getModel();
  187.       if (var4 != null && var5.getSize() > 0) {
  188.          int var1 = 0;
  189.  
  190.          for(int var2 = var5.getSize(); var1 < var2; ++var1) {
  191.             Component var6 = var4.getListCellRendererComponent(this.listBox, var5.getElementAt(var1), -1, false, false);
  192.             this.currentValuePane.add(var6);
  193.             var6.setFont(this.comboBox.getFont());
  194.             Dimension var7 = var6.getPreferredSize();
  195.             this.currentValuePane.remove(var6);
  196.             var3.width = Math.max(var3.width, var7.width);
  197.             var3.height = Math.max(var3.height, var7.height);
  198.          }
  199.  
  200.          if (this.comboBox.isEditable()) {
  201.             Dimension var8 = this.editor.getPreferredSize();
  202.             var3.width = Math.max(var3.width, var8.width);
  203.             var3.height = Math.max(var3.height, var8.height);
  204.          }
  205.  
  206.          this.cachedDisplaySize.setSize(var3.width, var3.height);
  207.          return var3;
  208.       } else {
  209.          return this.getDefaultSize();
  210.       }
  211.    }
  212.  
  213.    protected Insets getInsets() {
  214.       return this.comboBox.getInsets();
  215.    }
  216.  
  217.    public Dimension getMaximumSize(JComponent var1) {
  218.       Dimension var2 = this.getPreferredSize(var1);
  219.       var2.width = 32767;
  220.       return var2;
  221.    }
  222.  
  223.    public Dimension getMinimumSize(JComponent var1) {
  224.       if (!this.isMinimumSizeDirty) {
  225.          return new Dimension(this.cachedMinimumSize);
  226.       } else {
  227.          Insets var3 = this.getInsets();
  228.          Dimension var2 = this.getDisplaySize();
  229.          var2.height += var3.top + var3.bottom;
  230.          int var4 = var2.height - (var3.top + var3.bottom);
  231.          var2.width += var3.left + var3.right + var4;
  232.          this.cachedMinimumSize.setSize(var2.width, var2.height);
  233.          this.isMinimumSizeDirty = false;
  234.          return var2;
  235.       }
  236.    }
  237.  
  238.    public Dimension getPreferredSize(JComponent var1) {
  239.       Dimension var2 = this.getMinimumSize(var1);
  240.       var2.width += 4;
  241.       return var2;
  242.    }
  243.  
  244.    protected void installComponents() {
  245.       this.arrowButton = this.createArrowButton();
  246.       this.comboBox.add(this.arrowButton);
  247.       if (this.comboBox.isEditable()) {
  248.          this.addEditor();
  249.       }
  250.  
  251.       this.comboBox.add(this.currentValuePane);
  252.    }
  253.  
  254.    protected void installDefaults() {
  255.       LookAndFeel.installColorsAndFont(this.comboBox, "ComboBox.background", "ComboBox.foreground", "ComboBox.font");
  256.       LookAndFeel.installBorder(this.comboBox, "ComboBox.border");
  257.    }
  258.  
  259.    protected void installKeyboardActions() {
  260.       2 var1 = new 2(this);
  261.       this.comboBox.registerKeyboardAction(var1, KeyStroke.getKeyStroke(27, 0), 1);
  262.    }
  263.  
  264.    protected void installListeners() {
  265.       if ((this.itemListener = this.createItemListener()) != null) {
  266.          this.comboBox.addItemListener(this.itemListener);
  267.       }
  268.  
  269.       if ((this.propertyChangeListener = this.createPropertyChangeListener()) != null) {
  270.          this.comboBox.addPropertyChangeListener(this.propertyChangeListener);
  271.       }
  272.  
  273.       if ((this.keyListener = this.createKeyListener()) != null) {
  274.          this.comboBox.addKeyListener(this.keyListener);
  275.       }
  276.  
  277.       if ((this.focusListener = this.createFocusListener()) != null) {
  278.          this.comboBox.addFocusListener(this.focusListener);
  279.       }
  280.  
  281.       if ((this.popupMouseListener = this.popup.getMouseListener()) != null) {
  282.          this.comboBox.addMouseListener(this.popupMouseListener);
  283.       }
  284.  
  285.       if ((this.popupMouseMotionListener = this.popup.getMouseMotionListener()) != null) {
  286.          this.comboBox.addMouseMotionListener(this.popupMouseMotionListener);
  287.       }
  288.  
  289.       if ((this.popupKeyListener = this.popup.getKeyListener()) != null) {
  290.          this.comboBox.addKeyListener(this.popupKeyListener);
  291.       }
  292.  
  293.       if (this.comboBox.getModel() != null && (this.listDataListener = this.createListDataListener()) != null) {
  294.          this.comboBox.getModel().addListDataListener(this.listDataListener);
  295.       }
  296.  
  297.    }
  298.  
  299.    public void installUI(JComponent var1) {
  300.       this.isMinimumSizeDirty = true;
  301.       this.comboBox = (JComboBox)var1;
  302.       this.installDefaults();
  303.       this.popup = this.createPopup();
  304.       this.listBox = this.popup.getList();
  305.       Object var2 = var1.getClientProperty("JComboBox.lightweightKeyboardNavigation");
  306.       if (var2 != null) {
  307.          if (var2.equals("Lightweight")) {
  308.             this.lightNav = true;
  309.          } else if (var2.equals("Heavyweight")) {
  310.             this.lightNav = false;
  311.          }
  312.       }
  313.  
  314.       if (this.comboBox.getRenderer() == null || this.comboBox.getRenderer() instanceof UIResource) {
  315.          this.comboBox.setRenderer(this.createRenderer());
  316.       }
  317.  
  318.       if (this.comboBox.getEditor() == null || this.comboBox.getEditor() instanceof UIResource) {
  319.          this.comboBox.setEditor(this.createEditor());
  320.       }
  321.  
  322.       this.installComponents();
  323.       this.installListeners();
  324.       if (this.arrowButton != null) {
  325.          this.configureArrowButton();
  326.       }
  327.  
  328.       if (this.editor != null) {
  329.          this.configureEditor();
  330.       }
  331.  
  332.       this.comboBox.setLayout(this.createLayoutManager());
  333.       this.comboBox.setRequestFocusEnabled(true);
  334.       1 var3 = new 1(this);
  335.       SwingUtilities.invokeLater(var3);
  336.    }
  337.  
  338.    public boolean isFocusTraversable(JComboBox var1) {
  339.       return this.comboBox.isEditable() ^ true;
  340.    }
  341.  
  342.    protected boolean isNavigationKey(int var1) {
  343.       return var1 == 38 || var1 == 40;
  344.    }
  345.  
  346.    public boolean isPopupVisible(JComboBox var1) {
  347.       return this.popup.isVisible();
  348.    }
  349.  
  350.    public void paint(Graphics var1, JComponent var2) {
  351.       this.hasFocus = this.comboBox.hasFocus();
  352.       if (!this.comboBox.isEditable()) {
  353.          Rectangle var3 = this.rectangleForCurrentValue();
  354.          this.paintCurrentValueBackground(var1, var3, this.hasFocus);
  355.          this.paintCurrentValue(var1, var3, this.hasFocus);
  356.       }
  357.  
  358.    }
  359.  
  360.    public void paintCurrentValue(Graphics var1, Rectangle var2, boolean var3) {
  361.       ListCellRenderer var4 = this.comboBox.getRenderer();
  362.       if (this.comboBox.getSelectedIndex() != -1) {
  363.          Component var5;
  364.          if (var3 && !this.isPopupVisible(this.comboBox)) {
  365.             var5 = var4.getListCellRendererComponent(this.listBox, this.comboBox.getSelectedItem(), -1, true, false);
  366.          } else {
  367.             var5 = var4.getListCellRendererComponent(this.listBox, this.comboBox.getSelectedItem(), -1, false, false);
  368.             var5.setBackground(UIManager.getColor("ComboBox.background"));
  369.          }
  370.  
  371.          var5.setFont(this.comboBox.getFont());
  372.          if (var3 && !this.isPopupVisible(this.comboBox)) {
  373.             var5.setForeground(this.listBox.getSelectionForeground());
  374.             var5.setBackground(this.listBox.getSelectionBackground());
  375.          } else if (this.comboBox.isEnabled()) {
  376.             var5.setForeground(this.comboBox.getForeground());
  377.             var5.setBackground(this.comboBox.getBackground());
  378.          } else {
  379.             var5.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
  380.             var5.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
  381.          }
  382.  
  383.          this.currentValuePane.paintComponent(var1, var5, this.comboBox, var2.x, var2.y, var2.width, var2.height);
  384.       }
  385.    }
  386.  
  387.    public void paintCurrentValueBackground(Graphics var1, Rectangle var2, boolean var3) {
  388.       Color var4 = var1.getColor();
  389.       if (this.comboBox.isEnabled()) {
  390.          var1.setColor(UIManager.getColor("ComboBox.background"));
  391.       } else {
  392.          var1.setColor(UIManager.getColor("ComboBox.disabledBackground"));
  393.       }
  394.  
  395.       var1.fillRect(var2.x, var2.y, var2.width, var2.height);
  396.       var1.setColor(var4);
  397.    }
  398.  
  399.    protected Rectangle rectangleForCurrentValue() {
  400.       int var1 = this.comboBox.getWidth();
  401.       int var2 = this.comboBox.getHeight();
  402.       Insets var3 = this.getInsets();
  403.       int var4 = var2 - (var3.top + var3.bottom);
  404.       if (this.arrowButton != null) {
  405.          var4 = this.arrowButton.getWidth();
  406.       }
  407.  
  408.       return new Rectangle(var3.left, var3.top, var1 - (var3.left + var3.right + var4), var2 - (var3.top + var3.bottom));
  409.    }
  410.  
  411.    public void removeEditor() {
  412.       if (this.editor != null) {
  413.          this.unconfigureEditor();
  414.          this.comboBox.remove(this.editor);
  415.       }
  416.  
  417.    }
  418.  
  419.    void repaintCurrentValue() {
  420.       Rectangle var1 = this.rectangleForCurrentValue();
  421.       this.comboBox.repaint(var1.x, var1.y, var1.width, var1.height);
  422.    }
  423.  
  424.    protected void selectNextPossibleValue() {
  425.       int var1;
  426.       if (this.lightNav) {
  427.          var1 = this.listBox.getSelectedIndex();
  428.       } else {
  429.          var1 = this.comboBox.getSelectedIndex();
  430.       }
  431.  
  432.       if (var1 < this.comboBox.getModel().getSize() - 1) {
  433.          if (this.lightNav) {
  434.             this.listBox.setSelectedIndex(var1 + 1);
  435.             this.listBox.ensureIndexIsVisible(var1 + 1);
  436.          } else {
  437.             this.comboBox.setSelectedIndex(var1 + 1);
  438.          }
  439.  
  440.          this.comboBox.repaint();
  441.       }
  442.  
  443.    }
  444.  
  445.    protected void selectPreviousPossibleValue() {
  446.       int var1;
  447.       if (this.lightNav) {
  448.          var1 = this.listBox.getSelectedIndex();
  449.       } else {
  450.          var1 = this.comboBox.getSelectedIndex();
  451.       }
  452.  
  453.       if (var1 > 0) {
  454.          if (this.lightNav) {
  455.             this.listBox.setSelectedIndex(var1 - 1);
  456.             this.listBox.ensureIndexIsVisible(var1 - 1);
  457.          } else {
  458.             this.comboBox.setSelectedIndex(var1 - 1);
  459.          }
  460.  
  461.          this.comboBox.repaint();
  462.       }
  463.  
  464.    }
  465.  
  466.    public void setPopupVisible(JComboBox var1, boolean var2) {
  467.       if (var2) {
  468.          this.popup.show();
  469.       } else {
  470.          this.popup.hide();
  471.       }
  472.  
  473.    }
  474.  
  475.    protected void toggleOpenClose() {
  476.       this.setPopupVisible(this.comboBox, this.isPopupVisible(this.comboBox) ^ true);
  477.    }
  478.  
  479.    public void unconfigureArrowButton() {
  480.       if (this.arrowButton != null) {
  481.          if (this.popupMouseListener != null) {
  482.             this.arrowButton.removeMouseListener(this.popupMouseListener);
  483.          }
  484.  
  485.          if (this.popupMouseMotionListener != null) {
  486.             this.arrowButton.removeMouseMotionListener(this.popupMouseMotionListener);
  487.          }
  488.       }
  489.  
  490.    }
  491.  
  492.    protected void unconfigureEditor() {
  493.       if (this.popupKeyListener != null) {
  494.          this.editor.removeKeyListener(this.popupKeyListener);
  495.       }
  496.  
  497.    }
  498.  
  499.    protected void uninstallComponents() {
  500.       if (this.arrowButton != null) {
  501.          this.unconfigureArrowButton();
  502.       }
  503.  
  504.       if (this.editor != null) {
  505.          this.unconfigureEditor();
  506.       }
  507.  
  508.       this.comboBox.removeAll();
  509.       this.arrowButton = null;
  510.    }
  511.  
  512.    protected void uninstallDefaults() {
  513.       LookAndFeel.installColorsAndFont(this.comboBox, "ComboBox.background", "ComboBox.foreground", "ComboBox.font");
  514.       LookAndFeel.uninstallBorder(this.comboBox);
  515.    }
  516.  
  517.    protected void uninstallKeyboardActions() {
  518.       this.comboBox.unregisterKeyboardAction(KeyStroke.getKeyStroke(27, 0));
  519.    }
  520.  
  521.    protected void uninstallListeners() {
  522.       if (this.keyListener != null) {
  523.          this.comboBox.removeKeyListener(this.keyListener);
  524.       }
  525.  
  526.       if (this.itemListener != null) {
  527.          this.comboBox.removeItemListener(this.itemListener);
  528.       }
  529.  
  530.       if (this.propertyChangeListener != null) {
  531.          this.comboBox.removePropertyChangeListener(this.propertyChangeListener);
  532.       }
  533.  
  534.       if (this.focusListener != null) {
  535.          this.comboBox.removeFocusListener(this.focusListener);
  536.       }
  537.  
  538.       if (this.popupMouseListener != null) {
  539.          this.comboBox.removeMouseListener(this.popupMouseListener);
  540.       }
  541.  
  542.       if (this.popupMouseMotionListener != null) {
  543.          this.comboBox.removeMouseMotionListener(this.popupMouseMotionListener);
  544.       }
  545.  
  546.       if (this.popupKeyListener != null) {
  547.          this.comboBox.removeKeyListener(this.popupKeyListener);
  548.       }
  549.  
  550.       if (this.comboBox.getModel() != null && this.listDataListener != null) {
  551.          this.comboBox.getModel().removeListDataListener(this.listDataListener);
  552.       }
  553.  
  554.    }
  555.  
  556.    public void uninstallUI(JComponent var1) {
  557.       this.setPopupVisible(this.comboBox, false);
  558.       this.popup.uninstallingUI();
  559.       this.uninstallKeyboardActions();
  560.       this.comboBox.setLayout((LayoutManager)null);
  561.       this.uninstallComponents();
  562.       this.uninstallListeners();
  563.       this.uninstallDefaults();
  564.       if (this.comboBox.getRenderer() == null || this.comboBox.getRenderer() instanceof UIResource) {
  565.          this.comboBox.setRenderer((ListCellRenderer)null);
  566.       }
  567.  
  568.       if (this.comboBox.getEditor() == null || this.comboBox.getEditor() instanceof UIResource) {
  569.          this.comboBox.setEditor((ComboBoxEditor)null);
  570.       }
  571.  
  572.       this.keyListener = null;
  573.       this.focusListener = null;
  574.       this.listDataListener = null;
  575.       this.popupKeyListener = null;
  576.       this.popupMouseListener = null;
  577.       this.popupMouseMotionListener = null;
  578.       this.propertyChangeListener = null;
  579.       this.popup = null;
  580.       this.listBox = null;
  581.       this.comboBox = null;
  582.    }
  583. }
  584.