home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / OrganicComboBoxUI.java < prev    next >
Text File  |  1998-02-26  |  7KB  |  225 lines

  1. /*
  2.  * @(#)OrganicComboBoxUI.java    1.5 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.plaf.*;
  27. import com.sun.java.swing.border.*;
  28. import java.io.Serializable;
  29. import com.sun.java.swing.plaf.basic.BasicComboBoxUI;
  30.  
  31.  
  32. /**
  33.  * Organic UI for JComboBox
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @see OrganicComboBoxListCellRenderer
  43.  * @see OrganicPopupMenuBorder
  44.  * @version 1.5 02/02/98
  45.  * @author Tom Santos
  46.  */
  47. public class OrganicComboBoxUI extends BasicComboBoxUI  {
  48.     protected OrganicComboBoxButton button;
  49.     protected JButton arrowCache;
  50.  
  51.     private static Border emptyBorder = new BorderUIResource (
  52.                         new EmptyBorder(0,0,0,0));
  53.  
  54.     public static ComponentUI createUI(JComponent c) {
  55.         return new OrganicComboBoxUI();
  56.     }
  57.  
  58.     public void installUI(JComponent c) {
  59.         super.installUI(c);
  60.  
  61.     editablePropertyChanged();
  62.     }
  63.  
  64.     public void uninstallUI( JComponent c ) {
  65.     if ( button != null ) {
  66.         comboBox.remove( button );
  67.         button = null;
  68.     }
  69.  
  70.         super.uninstallUI( c );       
  71.     }
  72.  
  73.     public void paint(Graphics g, JComponent c) {
  74.     }
  75.  
  76.     protected JButton createArrowButton() {
  77.     return new OrganicComboBoxButton( comboBox, new OrganicComboBoxIcon(), true );
  78.     }
  79.  
  80.     public void layoutContainer(Container parent) {
  81.         if ( comboBox.isEditable() ) {
  82.         super.layoutContainer( parent );
  83.     }
  84.     else {
  85.         if ( button != null ) {
  86.             button.setSize( comboBox.getSize().width, comboBox.getSize().height );
  87.         }
  88.     }
  89.     }
  90.  
  91.     public void editablePropertyChanged() {
  92.         if ( comboBox.isEditable() ) {
  93.         if ( button != null ) {
  94.             comboBox.remove( button );
  95.         button = null;
  96.         }
  97.  
  98.         if ( arrowCache != null ) {
  99.             comboBox.add( arrowCache );
  100.         }
  101.  
  102.         super.editablePropertyChanged();
  103.  
  104.         comboBox.setBorder(emptyBorder);
  105.     }
  106.     else {
  107.         removeEditor();
  108.         if ( arrowButton != null ) {
  109.             comboBox.remove( arrowButton );
  110.         arrowCache = arrowButton;
  111.         arrowButton = null;
  112.         }
  113.  
  114.         if ( button == null ) {
  115.             Insets insets = comboBox.getInsets();
  116.             button = new OrganicComboBoxButton( comboBox, new OrganicComboBoxIcon() );
  117.         button.setLocation( 0, 0 );
  118.         button.setSize( comboBox.getSize().width - 2,
  119.                 comboBox.getSize().height );
  120.         }
  121.         comboBox.add( button );
  122.     }
  123.     }
  124.  
  125.     protected void setupListBox(JList listBox, JComboBox comboBox) {
  126.     listBox.setFont(comboBox.getFont());
  127.         listBox.setForeground(UIManager.getColor("ComboBox.listForeground"));
  128.         listBox.setBackground(UIManager.getColor("ComboBox.listBackground"));
  129.         listBox.setSelectionForeground(UIManager.getColor("ComboBox.selectedForeground"));
  130.         listBox.setSelectionBackground(UIManager.getColor("ComboBox.selectedBackground"));
  131.     }
  132.  
  133.     public void focusGained(FocusEvent e) {
  134.         super.focusGained( e );
  135.     if ( button != null ) {
  136.         button.forceDrawFocus( true );
  137.         button.repaint();
  138.     }
  139.     }
  140.  
  141.     public void focusLost(FocusEvent e) {
  142.         super.focusLost( e );
  143.     if ( button != null ) {
  144.         button.forceDrawFocus( false );
  145.         button.repaint();
  146.     }
  147.     }
  148.  
  149.     public CellRendererPane getCurrentValuePane() {
  150.         return currentValuePane;
  151.     }
  152.  
  153.     public void addArrowButton() {
  154.         arrowButton = createArrowButton();
  155.         arrowButton.setRequestFocusEnabled(true);
  156.         arrowButton.resetKeyboardActions();
  157.         comboBox.add(arrowButton);
  158.     }
  159.  
  160.     public void removeArrowButton() {
  161.         if(arrowButton != null) {
  162.             comboBox.remove(arrowButton);
  163.             arrowButton = null;
  164.         }
  165.     }
  166.  
  167.     public void mouseReleased( MouseEvent anEvent ) {
  168.         super.mouseReleased( anEvent );
  169.  
  170.     if ( anEvent.getSource() == listBox && button != null ) {
  171.         button.getModel().setPressed( false );
  172.     }
  173.     else if ( anEvent.getSource() == listBox && arrowButton != null ) {
  174.         arrowButton.getModel().setPressed( false );
  175.     }
  176.     }
  177.  
  178.     public void doMouseDragged( MouseEvent anEvent ) {
  179.         if ( popupIsVisible() ) {
  180.         Window sourceWindow;
  181.         MouseEvent tmp = convertEventToListBox(anEvent);
  182.         updateListBoxSelectionForEvent(tmp,true);
  183.         lastMouseLocation = SwingUtilities.convertPoint((Component)anEvent.getSource(),anEvent.getPoint(),null);
  184.         sourceWindow = SwingUtilities.windowForComponent((Component)anEvent.getSource());
  185.         lastMouseLocation.x += sourceWindow.getBounds().x;
  186.         lastMouseLocation.y += sourceWindow.getBounds().y;
  187.         startAutoscrolling();        
  188.     }
  189.     }
  190.  
  191.     public Dimension getMaximumSize(JComponent c) {
  192.         Dimension basicSize = super.getMaximumSize( c );
  193.     
  194.     if ( !comboBox.isEditable() ) {
  195.         basicSize.height += 4;
  196.     }
  197.  
  198.         return basicSize;
  199.     }
  200.  
  201.  
  202.     public void doMouseReleased( MouseEvent anEvent ) {
  203.         stopAutoscrolling();
  204.     if ( popupIsVisible() ) {
  205.         Object sv;
  206.         MouseEvent tmp = convertEventToListBox(anEvent);
  207.         updateListBoxSelectionForEvent(tmp,true);
  208.         sv = listBox.getSelectedValue();
  209.         if(sv != null)
  210.             comboBox.getModel().setSelectedItem(sv);
  211.         hidePopup();
  212.     }
  213.     }
  214.  
  215.     public void enablePropertyChanged() {
  216.         if ( button != null ) {
  217.         button.setEnabled( comboBox.isEnabled() );
  218.     }
  219.     if ( arrowCache != null ) {
  220.         arrowCache.setEnabled( comboBox.isEnabled() );
  221.     }
  222.     super.enablePropertyChanged();
  223.     }
  224. }
  225.