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

  1. /*
  2.  * @(#)BasicSpinnerUI.java    1.13 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.basic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.plaf.*;
  28.  
  29. import java.io.Serializable;
  30.  
  31.  
  32. /**
  33.  * BasicSpinner implementation
  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.  * @version 1.13 02/02/98
  43.  * @author Brian Gerhold
  44.  */
  45. public class BasicSpinnerUI extends SpinnerUI implements Serializable
  46. {
  47.   final static Border defaultBorder = BorderFactory.createLoweredBevelBorder();
  48.  
  49.   protected Dimension d;
  50.   protected int ascent;
  51.   protected FontMetrics fm;
  52.  
  53.   // Shared UI object
  54.   protected static SpinnerUI spinnerUI;
  55.  
  56.   public static ComponentUI createUI(JComponent s) 
  57.   {
  58.     if(spinnerUI == null) {
  59.       spinnerUI = new BasicSpinnerUI();
  60.     }
  61.     return spinnerUI;
  62.   }
  63.   
  64.   public void installUI(JComponent c) 
  65.   {
  66.     c.setFont(BasicGraphicsUtils.controlFont);
  67.     if(c.getBorder() == null)
  68.       c.setBorder(defaultBorder);
  69.   }
  70.   
  71.   public void uninstallUI(JComponent c) 
  72.   {
  73.     c.resetKeyboardActions();
  74.     if(c.getBorder() == defaultBorder)
  75.       c.setBorder(null);
  76.   }
  77.     
  78.   public void paint(Graphics g, JComponent c)
  79.   {
  80.     Spinner spinner = ((Spinner)(c));
  81.     //frosty man, frosty
  82.     getMinimumSize(c); //Force computation of dimension
  83.     Insets bi = spinner.getBorder().getBorderInsets(spinner);
  84.     g.setColor(spinner.getBackgroundColor());
  85.     g.fillRect(bi.left,bi.top,d.width-(bi.left+bi.right),d.height-(bi.top+bi.bottom));    
  86.  
  87.     //g.setColor(Color.black);
  88.     //g.drawLine(0,0,0,d.height-1);
  89.     //g.drawLine(1,0,1,d.height-3); 
  90.     //g.drawLine(1,0,d.width,0);
  91.     //g.drawLine(1,1,d.width-2,1);
  92.     //g.setColor(Color.white);
  93.     //g.fillRect(3,3,d.width-5,d.height-5);
  94.  
  95.     if (spinner.hasFocus()) 
  96.       {
  97.        g.setColor(new Color(0,0,150));
  98.        g.fillRect(bi.left+2, bi.top+2, d.width-(bi.left+bi.right+4), d.height-(bi.top+bi.bottom+4));
  99.        g.setColor(Color.white);
  100.       }
  101.     else
  102.       {
  103.     g.setColor(Color.black);
  104.       }
  105.     String s;
  106.     if(spinner instanceof StringSpinner)
  107.       {
  108.     StringSpinner spinner2 = ((StringSpinner)(spinner));
  109.     s = spinner2.getValueName() + (spinner2.getText()!=null ? spinner2.getText() : "");
  110.     if (spinner2.getTypedString()==null)
  111.       {
  112.         g.drawString(s, bi.left+2, ascent + bi.top+2);
  113.       }
  114.     else 
  115.       {
  116.             g.setColor(Color.lightGray);
  117.             g.drawString(s, bi.left+2, ascent+bi.top+2);
  118.             g.setColor(Color.white);
  119.             g.drawString(s.substring(0,spinner2.getTypedString().length()),bi.left+2 , ascent+bi.top+2);
  120.       }
  121.       }
  122.     else
  123.       {
  124.     s = Integer.toString(spinner.getValue());
  125.     if(spinner.getLeadingPad() != -1)
  126.       {
  127.         int j = spinner.getDigits()-s.length();
  128.         String lp = Integer.toString(spinner.getLeadingPad());
  129.         for(int i=0; i<j; i++) { s = lp +s;}
  130.       }
  131.     s += (spinner.getText()!=null ? spinner.getText() : "");
  132.     g.drawString(s, d.width-fm.stringWidth(s) - bi.right-2, ascent + bi.bottom+2);
  133.       }
  134.   }
  135.  
  136.   public Dimension getPreferredSize(JComponent c) 
  137.   {
  138.     return getMinimumSize(c);
  139.   }
  140.   public Dimension getMaximumSize(JComponent c) 
  141.   {
  142.     return getMinimumSize(c);
  143.   }
  144.   public Dimension getMinimumSize(JComponent c) 
  145.   {
  146.     Spinner s = ((Spinner)(c));
  147.     fm = s.getFontMetrics(s.getFont());
  148.     int w=0;
  149.     if(s instanceof StringSpinner)
  150.       {
  151.     StringSpinner s2 = ((StringSpinner)(s));
  152.     String[] names = s2.getNameArray();
  153.     for (int i = names.length; --i>=0; ) 
  154.       {
  155.         int tw = fm.stringWidth(names[i]);
  156.         if (tw>w) w = tw;
  157.       }
  158.       }
  159.     else
  160.       {
  161.     w = fm.stringWidth("0")*s.getDigits();
  162.       }
  163.     if (s.getText()!=null) w+=fm.stringWidth(s.getText());
  164.     Insets bi = s.getBorder().getBorderInsets(s);
  165.     d = new Dimension(w+bi.left+bi.right+4, fm.getHeight()+bi.top+bi.bottom+4);
  166.     ascent = fm.getAscent();
  167.     return d;
  168.   }
  169. }
  170.