home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PropertyEditorSupport.java < prev    next >
Text File  |  1997-05-20  |  8KB  |  247 lines

  1. /*
  2.  * @(#)PropertyEditorSupport.java    1.6 96/12/16  
  3.  * 
  4.  * Copyright (c) 1996 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.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package java.beans;
  24.  
  25. /**
  26.  * This is a support class to help build property editors.
  27.  * <p>
  28.  * It can be used either as a base class or as a delagatee.
  29.  */
  30.  
  31. import java.beans.*;
  32.  
  33. public class PropertyEditorSupport implements PropertyEditor {
  34.  
  35.     /**
  36.      * Constructor for use by derived PropertyEditor classes.
  37.      */
  38.  
  39.     protected PropertyEditorSupport() {
  40.     source = this;
  41.     }
  42.  
  43.     /**
  44.      * Constructor for use when a PropertyEditor is delegating to us.
  45.      * @param source  The source to use for any events we fire.
  46.      */
  47.  
  48.     protected PropertyEditorSupport(Object source) {
  49.     this.source = source;
  50.     }
  51.  
  52.     /**
  53.      * Set (or change) the object that is to be edited.
  54.      * @param value The new target object to be edited.  Note that this
  55.      *     object should not be modified by the PropertyEditor, rather 
  56.      *     the PropertyEditor should create a new object to hold any
  57.      *     modified value.
  58.      */
  59.     public void setValue(Object value) {
  60.     this.value = value;
  61.     firePropertyChange();
  62.     }
  63.  
  64.     /**
  65.      * @return The value of the property.
  66.      */
  67.  
  68.     public Object getValue() {
  69.     return value;
  70.     }
  71.  
  72.     //----------------------------------------------------------------------
  73.  
  74.     /**
  75.      * @return  True if the class will honor the paintValue method.
  76.      */
  77.  
  78.     public boolean isPaintable() {
  79.     return false;
  80.     }
  81.  
  82.     /**
  83.      * Paint a representation of the value into a given area of screen
  84.      * real estate.  Note that the propertyEditor is responsible for doing
  85.      * its own clipping so that it fits into the given rectangle.
  86.      * <p>
  87.      * If the PropertyEditor doesn't honor paint requests (see isPaintable)
  88.      * this method should be a silent noop.
  89.      *
  90.      * @param gfx  Graphics object to paint into.
  91.      * @param box  Rectangle within graphics object into which we should paint.
  92.      */
  93.     public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
  94.     }
  95.  
  96.     //----------------------------------------------------------------------
  97.  
  98.     /**
  99.      * This method is intended for use when generating Java code to set
  100.      * the value of the property.  It should return a fragment of Java code
  101.      * that can be used to initialize a variable with the current property
  102.      * value.
  103.      * <p>
  104.      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
  105.      *
  106.      * @return A fragment of Java code representing an initializer for the
  107.      *       current value.
  108.      */
  109.     public String getJavaInitializationString() {
  110.     return "???";
  111.     }
  112.  
  113.     //----------------------------------------------------------------------
  114.  
  115.     /**
  116.      * @return The property value as a string suitable for presentation
  117.      *       to a human to edit.
  118.      * <p>   Returns "null" is the value can't be expressed as a string.
  119.      * <p>   If a non-null value is returned, then the PropertyEditor should
  120.      *         be prepared to parse that string back in setAsText().
  121.      */
  122.     public String getAsText() {
  123.     if (value instanceof String) {
  124.         return (String)value;
  125.     }
  126.     return ("" + value);
  127.     }
  128.  
  129.     /**
  130.      * Set the property value by parsing a given String.  May raise
  131.      * java.lang.IllegalArgumentException if either the String is
  132.      * badly formatted or if this kind of property can't be expressed
  133.      * as text.
  134.      * @param text  The string to be parsed.
  135.      */
  136.     public void setAsText(String text) throws java.lang.IllegalArgumentException {
  137.     if (value instanceof String) {
  138.         setValue(text);
  139.         return;
  140.     }
  141.     throw new java.lang.IllegalArgumentException(text);
  142.     }
  143.  
  144.     //----------------------------------------------------------------------
  145.  
  146.     /**
  147.      * If the property value must be one of a set of known tagged values, 
  148.      * then this method should return an array of the tag values.  This can
  149.      * be used to represent (for example) enum values.  If a PropertyEditor
  150.      * supports tags, then it should support the use of setAsText with
  151.      * a tag value as a way of setting the value.
  152.      *
  153.      * @return The tag values for this property.  May be null if this 
  154.      *   property cannot be represented as a tagged value.
  155.      *    
  156.      */
  157.     public String[] getTags() {
  158.     return null;
  159.     }
  160.  
  161.     //----------------------------------------------------------------------
  162.  
  163.     /**
  164.      * A PropertyEditor may chose to make available a full custom Component
  165.      * that edits its property value.  It is the responsibility of the
  166.      * PropertyEditor to hook itself up to its editor Component itself and
  167.      * to report property value changes by firing a PropertyChange event.
  168.      * <P>
  169.      * The higher-level code that calls getCustomEditor may either embed
  170.      * the Component in some larger property sheet, or it may put it in
  171.      * its own individual dialog, or ...
  172.      *
  173.      * @return A java.awt.Component that will allow a human to directly
  174.      *      edit the current property value.  May be null if this is
  175.      *        not supported.
  176.      */
  177.  
  178.     public java.awt.Component getCustomEditor() {
  179.     return null;
  180.     }
  181.  
  182.     /**
  183.      * @return  True if the propertyEditor can provide a custom editor.
  184.      */
  185.     public boolean supportsCustomEditor() {
  186.     return false;
  187.     }
  188.   
  189.     //----------------------------------------------------------------------
  190.  
  191.     /**
  192.      * Register a listener for the PropertyChange event.  The class will
  193.      * fire a PropertyChange value whenever the value is updated.
  194.      *
  195.      * @param listener  An object to be invoked when a PropertyChange
  196.      *        event is fired.
  197.      */
  198.     public synchronized void addPropertyChangeListener(
  199.                 PropertyChangeListener listener) {
  200.     if (listeners == null) {
  201.         listeners = new java.util.Vector();
  202.     }
  203.     listeners.addElement(listener);
  204.     }
  205.  
  206.     /**
  207.      * Remove a listener for the PropertyChange event.
  208.      *
  209.      * @param listener  The PropertyChange listener to be removed.
  210.      */
  211.     public synchronized void removePropertyChangeListener(
  212.                 PropertyChangeListener listener) {
  213.     if (listeners == null) {
  214.         return;
  215.     }
  216.     listeners.removeElement(listener);
  217.     }
  218.  
  219.     /**
  220.      * Report that we have been modified to any interested listeners.
  221.      *
  222.      * @param source  The PropertyEditor that caused the event.
  223.      */
  224.     public void firePropertyChange() {
  225.     java.util.Vector targets;
  226.     synchronized (this) {
  227.         if (listeners == null) {
  228.             return;
  229.         }
  230.         targets = (java.util.Vector) listeners.clone();
  231.     }
  232.     // Tell our listeners that "everything" has changed.
  233.         PropertyChangeEvent evt = new PropertyChangeEvent(source, null, null, null);
  234.  
  235.     for (int i = 0; i < targets.size(); i++) {
  236.         PropertyChangeListener target = (PropertyChangeListener)targets.elementAt(i);
  237.         target.propertyChange(evt);
  238.     }
  239.     }
  240.  
  241.     //----------------------------------------------------------------------
  242.  
  243.     private Object value;
  244.     private Object source;
  245.     private java.util.Vector listeners;
  246. }
  247.