home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / plaf / metal / MetalComboBoxUI.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  7.1 KB  |  242 lines

  1. package com.sun.java.swing.plaf.metal;
  2.  
  3. import com.sun.java.swing.CellRendererPane;
  4. import com.sun.java.swing.JButton;
  5. import com.sun.java.swing.JComboBox;
  6. import com.sun.java.swing.JComponent;
  7. import com.sun.java.swing.JList;
  8. import com.sun.java.swing.KeyStroke;
  9. import com.sun.java.swing.SwingUtilities;
  10. import com.sun.java.swing.UIManager;
  11. import com.sun.java.swing.border.Border;
  12. import com.sun.java.swing.border.EmptyBorder;
  13. import com.sun.java.swing.plaf.BorderUIResource;
  14. import com.sun.java.swing.plaf.ComponentUI;
  15. import com.sun.java.swing.plaf.basic.BasicComboBoxUI;
  16. import java.awt.Component;
  17. import java.awt.Container;
  18. import java.awt.Dimension;
  19. import java.awt.Font;
  20. import java.awt.Graphics;
  21. import java.awt.Point;
  22. import java.awt.Window;
  23. import java.awt.event.FocusEvent;
  24. import java.awt.event.MouseEvent;
  25. import java.util.EventObject;
  26.  
  27. public class MetalComboBoxUI extends BasicComboBoxUI {
  28.    protected MetalComboBoxButton button;
  29.    protected MetalComboBoxButton arrowCache;
  30.    private static Border emptyBorder = new BorderUIResource(new EmptyBorder(0, 0, 0, 0));
  31.  
  32.    public static ComponentUI createUI(JComponent var0) {
  33.       return new MetalComboBoxUI();
  34.    }
  35.  
  36.    public void installUI(JComponent var1) {
  37.       super.installUI(var1);
  38.       this.removeArrowButton();
  39.       this.addArrowButton();
  40.       if (super.editor != null) {
  41.          Font var2 = ((Component)var1).getFont();
  42.          super.editor.setFont(new Font(var2.getName(), var2.getStyle(), var2.getSize()));
  43.       }
  44.  
  45.    }
  46.  
  47.    public void uninstallUI(JComponent var1) {
  48.       if (this.button != null) {
  49.          super.comboBox.remove(this.button);
  50.          this.button = null;
  51.       }
  52.  
  53.       JComboBox var2 = super.comboBox;
  54.       super.uninstallUI(var1);
  55.       ((Container)var2).removeAll();
  56.    }
  57.  
  58.    public void paint(Graphics var1, JComponent var2) {
  59.    }
  60.  
  61.    protected JButton createArrowButton() {
  62.       MetalComboBoxButton var1 = new MetalComboBoxButton(super.comboBox, new MetalComboBoxIcon(), true);
  63.       if (super.comboBox.isEditable()) {
  64.          this.arrowCache = var1;
  65.       }
  66.  
  67.       return var1;
  68.    }
  69.  
  70.    public void layoutContainer(Container var1) {
  71.       if (super.comboBox.isEditable()) {
  72.          super.layoutContainer(var1);
  73.       } else {
  74.          if (this.button != null) {
  75.             this.button.setSize(super.comboBox.getSize().width, super.comboBox.getSize().height);
  76.          }
  77.  
  78.       }
  79.    }
  80.  
  81.    public void editablePropertyChanged() {
  82.       if (super.comboBox.isEditable()) {
  83.          if (this.button != null) {
  84.             super.comboBox.remove(this.button);
  85.             this.button = null;
  86.          }
  87.  
  88.          super.editablePropertyChanged();
  89.          if (this.arrowCache != null) {
  90.             super.comboBox.add(this.arrowCache);
  91.             super.comboBox.repaint();
  92.          } else if (super.arrowButton == null) {
  93.             this.addArrowButton();
  94.          }
  95.  
  96.          this.arrowCache = (MetalComboBoxButton)super.arrowButton;
  97.          super.comboBox.setBorder(emptyBorder);
  98.       } else {
  99.          ((BasicComboBoxUI)this).removeEditor();
  100.          if (super.arrowButton != null) {
  101.             super.comboBox.remove(super.arrowButton);
  102.             this.arrowCache = (MetalComboBoxButton)super.arrowButton;
  103.             super.arrowButton = null;
  104.          }
  105.  
  106.          if (this.button == null) {
  107.             super.comboBox.getInsets();
  108.             this.button = new MetalComboBoxButton(super.comboBox, new MetalComboBoxIcon());
  109.             this.button.setLocation(0, 0);
  110.             this.button.setSize(super.comboBox.getSize().width - 2, super.comboBox.getSize().height);
  111.          }
  112.  
  113.          super.comboBox.add(this.button);
  114.       }
  115.    }
  116.  
  117.    protected void setupListBox(JList var1, JComboBox var2) {
  118.       ((Component)var1).setFont(((Component)var2).getFont());
  119.       ((Component)var1).setForeground(UIManager.getColor("ComboBox.listForeground"));
  120.       ((Component)var1).setBackground(UIManager.getColor("ComboBox.listBackground"));
  121.       var1.setSelectionForeground(UIManager.getColor("ComboBox.selectedForeground"));
  122.       var1.setSelectionBackground(UIManager.getColor("ComboBox.selectedBackground"));
  123.    }
  124.  
  125.    public void hidePopup() {
  126.       super.hidePopup();
  127.       if (this.arrowCache != null) {
  128.          this.arrowCache.getModel().setPressed(false);
  129.          this.arrowCache.repaint();
  130.       }
  131.  
  132.    }
  133.  
  134.    public void focusGained(FocusEvent var1) {
  135.       super.focusGained(var1);
  136.       if (this.button != null) {
  137.          this.button.forceDrawFocus(true);
  138.          this.button.repaint();
  139.       }
  140.  
  141.    }
  142.  
  143.    public void focusLost(FocusEvent var1) {
  144.       super.focusLost(var1);
  145.       if (this.button != null) {
  146.          this.button.forceDrawFocus(false);
  147.          this.button.repaint();
  148.       }
  149.  
  150.    }
  151.  
  152.    public CellRendererPane getCurrentValuePane() {
  153.       return super.currentValuePane;
  154.    }
  155.  
  156.    public void addArrowButton() {
  157.       super.arrowButton = this.createArrowButton();
  158.       super.arrowButton.setRequestFocusEnabled(true);
  159.       super.comboBox.add(super.arrowButton);
  160.       super.comboBox.repaint();
  161.    }
  162.  
  163.    public void removeArrowButton() {
  164.       if (super.arrowButton != null) {
  165.          super.comboBox.remove(super.arrowButton);
  166.          super.arrowButton = null;
  167.       }
  168.  
  169.    }
  170.  
  171.    public void mouseReleased(MouseEvent var1) {
  172.       super.mouseReleased(var1);
  173.       if (((EventObject)var1).getSource() == super.listBox && this.button != null) {
  174.          this.button.getModel().setPressed(false);
  175.       } else {
  176.          if (((EventObject)var1).getSource() == super.listBox && super.arrowButton != null) {
  177.             super.arrowButton.getModel().setPressed(false);
  178.          }
  179.  
  180.       }
  181.    }
  182.  
  183.    public void doMouseDragged(MouseEvent var1) {
  184.       if (((BasicComboBoxUI)this).popupIsVisible()) {
  185.          MouseEvent var3 = ((BasicComboBoxUI)this).convertEventToListBox(var1);
  186.          ((BasicComboBoxUI)this).updateListBoxSelectionForEvent(var3, true);
  187.          super.lastMouseLocation = SwingUtilities.convertPoint((Component)((EventObject)var1).getSource(), var1.getPoint(), (Component)null);
  188.          Window var2 = SwingUtilities.windowForComponent((Component)((EventObject)var1).getSource());
  189.          Point var10000 = super.lastMouseLocation;
  190.          var10000.x += ((Component)var2).getBounds().x;
  191.          var10000 = super.lastMouseLocation;
  192.          var10000.y += ((Component)var2).getBounds().y;
  193.          ((BasicComboBoxUI)this).startAutoscrolling();
  194.       }
  195.  
  196.    }
  197.  
  198.    public Dimension getMaximumSize(JComponent var1) {
  199.       Dimension var2 = super.getMaximumSize(var1);
  200.       var2.height += 4;
  201.       return var2;
  202.    }
  203.  
  204.    public void doMouseReleased(MouseEvent var1) {
  205.       ((BasicComboBoxUI)this).stopAutoscrolling();
  206.       if (((BasicComboBoxUI)this).popupIsVisible()) {
  207.          MouseEvent var3 = ((BasicComboBoxUI)this).convertEventToListBox(var1);
  208.          ((BasicComboBoxUI)this).updateListBoxSelectionForEvent(var3, true);
  209.          Object var2 = super.listBox.getSelectedValue();
  210.          if (var2 != null) {
  211.             super.comboBox.getModel().setSelectedItem(var2);
  212.          }
  213.  
  214.          this.hidePopup();
  215.       }
  216.  
  217.    }
  218.  
  219.    public void enablePropertyChanged() {
  220.       if (this.button != null) {
  221.          this.button.setEnabled(super.comboBox.isEnabled());
  222.       }
  223.  
  224.       if (this.arrowCache != null) {
  225.          this.arrowCache.setEnabled(super.comboBox.isEnabled());
  226.       }
  227.  
  228.       super.enablePropertyChanged();
  229.    }
  230.  
  231.    protected void addKeyAccelerators(JComponent var1) {
  232.       JComboBox var2 = super.comboBox;
  233.       super.addKeyAccelerators(var1);
  234.       super.comboBox.registerKeyboardAction(new 1(var2, this), KeyStroke.getKeyStroke(32, 0), 0);
  235.    }
  236.  
  237.    protected void removeKeyAccelerators(JComponent var1) {
  238.       super.removeKeyAccelerators(var1);
  239.       super.comboBox.unregisterKeyboardAction(KeyStroke.getKeyStroke(32, 0));
  240.    }
  241. }
  242.