home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / ActiveX / JCalendar / JCalendarYearEditor.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  8.4 KB  |  299 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. //  JCalendarYearEditor.java
  4. //
  5. //  This class is an editor for editing the calendar year. It implements
  6. //  PropertyEditor inerface required of all property editors.
  7. //
  8. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  9. //
  10. ///////////////////////////////////////////////////////////////////////////
  11.  
  12. import java.beans.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15.  
  16. public class JCalendarYearEditor extends Panel implements PropertyEditor, ActionListener, KeyListener //, IUIAdjustmentListener
  17. {
  18.     private TextField text;                         // The text field for entering the year
  19.     private Integer year = new Integer(1);          // the year which this class is out to modify
  20.     private PropertyChangeSupport helper ;          // Helps to fire property change events.
  21.  
  22.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  23.     // The constructor of this editor
  24.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  25.     public JCalendarYearEditor() 
  26.     {   
  27.         helper = new PropertyChangeSupport(this);
  28.  
  29.         // I don't need a layout manger
  30.         setLayout(null);
  31.  
  32.         // now create a text field
  33.         text = new TextField("1",6);
  34.  
  35.         // now register myself as a listener on this text field
  36.         text.addActionListener(this);
  37.         text.addKeyListener(this);
  38.  
  39.         // add this text field
  40.         add(text);
  41.         text.setBounds(4,4, 100, 30);
  42.  
  43.         // now size myself correctly.
  44.         setSize(128, 38);
  45.         
  46.     }
  47.  
  48.     /**
  49.      * Returns the size of this component
  50.      * @return  Dimension   the size
  51.      */
  52.     public Dimension getPreferredSize()
  53.     {
  54.         return new Dimension(108,72);
  55.     }
  56.  
  57.     /**
  58.      * Returns the size of this component
  59.      * @return  Dimension   the size
  60.      */
  61.     public Dimension getMinimumSize()
  62.     {
  63.         return new Dimension(108,72);
  64.     }
  65.  
  66.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  67.     // Event handling
  68.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  69.  
  70.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  71.     // Interface ActionListener requires the following methods.
  72.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  73.     /**
  74.      * Invoked by the text field when the user enters a new value
  75.      * @param   ActionEvent The event object
  76.      */
  77.     public void actionPerformed(ActionEvent event) 
  78.     {
  79.         Object source = event.getSource();
  80.         if ( source == text)
  81.         {
  82.             try 
  83.             {
  84.                 setAsText(text.getText());
  85.             }
  86.             catch(IllegalArgumentException e)
  87.             {
  88.                 text.setText(getAsText());
  89.             }
  90.         }       
  91.     }
  92.  
  93.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  94.     // Interface KeyListener requires the following methods.
  95.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  96.     
  97.     public void keyPressed(KeyEvent event)
  98.     {        
  99.         Object source = event.getSource();
  100.         if ( source == text)
  101.         {
  102.             try 
  103.             {
  104.                 setAsText(text.getText());
  105.             }
  106.             catch(IllegalArgumentException e)
  107.             {
  108.                 text.setText(getAsText());
  109.             }
  110.         }
  111.     }
  112.  
  113.     public void keyReleased(KeyEvent event)
  114.     {
  115.         Object source = event.getSource();
  116.         if ( source == text)
  117.         {
  118.             try 
  119.             {
  120.                 setAsText(text.getText());
  121.             }
  122.             catch(IllegalArgumentException e)
  123.             {
  124.                 text.setText(getAsText());
  125.             }
  126.         }
  127.     }
  128.  
  129.     public void keyTyped(KeyEvent event) 
  130.     {
  131.         Object source = event.getSource();
  132.         if ( source == text)
  133.         {
  134.             try 
  135.             {
  136.                 setAsText(text.getText());
  137.             }
  138.             catch(IllegalArgumentException e)
  139.             {
  140.                 text.setText(getAsText());
  141.             }
  142.         }
  143.     }
  144.  
  145.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  146.     // Interface PropertyEditor requires the following functions to be 
  147.     // defined.
  148.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  149.  
  150.     /**
  151.      * Returns a string representation of the property suitable for
  152.      * inclusion in java code. Note year being an int will be wrapped in an Integer.
  153.      *
  154.      * @return  String  The java representation of the year
  155.      */
  156.     public String getJavaInitializationString()
  157.     {
  158.        return getAsText();
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Sets the value of the year
  164.      * @param   Object  The year wrapped in an Integer object.
  165.      */
  166.     public void setValue(Object yearObject)
  167.     {
  168.         // make a copy of the old year
  169.         Integer oldYear = year;
  170.  
  171.         // now update the year
  172.         year = (Integer) yearObject;
  173.  
  174.         // update the text box only if its different
  175.         if( ! (text.getText()).equals(getAsText()))
  176.             text.setText(getAsText());
  177.  
  178.         // now fire the property change event
  179.         helper.firePropertyChange("year", oldYear, year);
  180.     }
  181.  
  182.     /**
  183.      * Get the value of the year
  184.      * @return  Object  The year wrapped in an Integer object
  185.      */
  186.     public Object getValue()
  187.     {
  188.         return (Object) year;
  189.     }
  190.  
  191.     /**
  192.      * Tell the calling function that this property has a customized
  193.      * editor.
  194.      *
  195.      * @return  boolean True
  196.      */
  197.     public boolean supportsCustomEditor()
  198.     {
  199.         return true;
  200.     }
  201.  
  202.     /**
  203.      * Returns the component which is responsible for editing this property
  204.      *
  205.      * @return  Component   The component which actually edits the property.
  206.      */
  207.     public Component getCustomEditor()
  208.     {
  209.         return this;
  210.     }
  211.  
  212.  
  213.      
  214.     /**
  215.      * This editor is capable of drawing itself 
  216.      * 
  217.      * @return  boolean true
  218.      */
  219.     public boolean isPaintable()
  220.     {
  221.         return true;
  222.     }
  223.  
  224.     /**
  225.      * Draws a representation of the year in a rectangle
  226.      *
  227.      * @param   Graphics    The graphics context
  228.      * @param   Rectangle   The rectangle in which to draw itself
  229.      */
  230.     public void paintValue(Graphics gr, Rectangle rect)
  231.     {
  232.         // Get the font metrics to figure out the string widths
  233.         FontMetrics fm = gr.getFontMetrics();
  234.  
  235.         // Center the year in the middle of the rectangle.
  236.         gr.drawString(getAsText(), rect.x + rect.width/2 - fm.stringWidth(getAsText())/2, 
  237.             rect.y + rect.height/2 + fm.getAscent()/2 );
  238.     }
  239.  
  240.     /**
  241.      * Return a string representation of the property suitable for display
  242.      *
  243.      * @return  String  The year
  244.      */
  245.     public String getAsText()
  246.     {
  247.         return ((Integer) getValue()).toString();
  248.     }
  249.  
  250.     /**
  251.      * Set the value of the year given as a string. Throws an Illegal Argument exception
  252.      * if something is wrong with the provided string.
  253.      *
  254.      * @param   String  The month name
  255.      */
  256.     public void setAsText(String yearString) throws IllegalArgumentException
  257.     {
  258.         try
  259.         {
  260.             setValue( new Integer(Integer.parseInt(yearString)) );    
  261.         }
  262.         catch (Exception e)
  263.         {
  264.             throw new IllegalArgumentException(yearString);
  265.         }
  266.     }
  267.  
  268.  
  269.     /**
  270.      * Returns the possible string values supported with this property
  271.      * @return  String[]    The array of possible string values supported.
  272.      */
  273.     public String[] getTags()
  274.     {
  275.         String s[] = {"10", "20"};
  276.         return s;
  277.            
  278.     }
  279.  
  280.     /**
  281.      * Add a listener for changes in this property
  282.      * @param   PropertyChangeListener  The object which should be informed of a change
  283.      */
  284.     public void addPropertyChangeListener(PropertyChangeListener listener)
  285.     {
  286.         helper.addPropertyChangeListener(listener);
  287.     }
  288.  
  289.     /**
  290.      * Remove a listener of property changes
  291.      * @param   PropertyChangeListener  The object who no longer wants to listen to changes
  292.      */
  293.     public void removePropertyChangeListener(PropertyChangeListener listener) 
  294.     {
  295.         helper.removePropertyChangeListener(listener);
  296.     }
  297.  
  298. }
  299.