home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / progs / java.exe / Java WorkShop / data.3 / JWS / examples / cardfile / CardFile.java < prev    next >
Encoding:
Java Source  |  1997-05-19  |  10.5 KB  |  306 lines

  1. /**
  2.  * This is a template.  You may modify this file.
  3.  *
  4.  * Runtime vendor: SunSoft, Inc.
  5.  * Runtime version: 1.0
  6.  *
  7.  * Visual vendor: SunSoft, Inc.
  8.  * Visual version: 1.0
  9.  */
  10.  
  11.  
  12. import sunsoft.jws.visual.rt.base.*;
  13. import sunsoft.jws.visual.rt.shadow.java.awt.*;
  14. import java.awt.*;
  15.  
  16.  
  17. public class CardFile extends Group {
  18.   private CardFileRoot gui;
  19.  
  20.   /**
  21.    * Sample method call ordering during a group's lifetime:
  22.    *
  23.    * Constructor
  24.    * initRoot
  25.    * initGroup
  26.    * (setOnGroup and getOnGroup may be called at any time in any
  27.    *  order after initGroup has been called)
  28.    * createGroup
  29.    * showGroup/hideGroup + startGroup/stopGroup
  30.    * destroyGroup
  31.    */
  32.  
  33.   /**
  34.    * All the attributes used by the group must be defined in the
  35.    * constructor.  setOnGroup is called at initialization for all
  36.    * the attributes.  If the attribute has not been set prior to
  37.    * initialization, setOnGroup is called with the default value.
  38.    */
  39.   public CardFile() {
  40.     /**
  41.      * Define the group's custom attributes here.
  42.      *
  43.      * For example:
  44.      *
  45.      * attributes.add("customString", "java.lang.String",
  46.      *              "Default String", 0);
  47.      */
  48.  
  49.     /**
  50.      * This method defines the attributes that will be forwarded to
  51.      * the main child (either a window or a panel).  All attributes
  52.      * defined by this method are marked with the FORWARD flag.
  53.      */
  54.     addForwardedAttributes();
  55.   }
  56.  
  57.   /**
  58.    * initRoot must be overridden in group subclasses to initialize
  59.    * the shadow tree.  The return value must be the root of the
  60.    * newly initialized shadow tree.
  61.    */
  62.   protected Root initRoot() {
  63.     /**
  64.      * Initialize the gui components
  65.      */
  66.     gui = new CardFileRoot(this);
  67.  
  68.     /**
  69.      * This method registers an attribute manager with the group, such
  70.      * that attributes marked with the FORWARD flag will be sent to
  71.      * this attribute manager.
  72.      */
  73.     addAttributeForward(gui.getMainChild());
  74.  
  75.     return gui;
  76.   }
  77.  
  78.   /**
  79.    * initGroup is called during initialization.  It is called just after
  80.    * initRoot is called, but before the sub-groups are initialized and
  81.    * before the attributes are sent to the setOnGroup method.
  82.    *
  83.    * initGroup is only called once in the lifetime of the Group.
  84.    * This is because groups cannot be uninitialized.  Anything that
  85.    * needs to be cleaned up should be created in createGroup instead
  86.    * of initGroup, and then can be cleaned up in destroyGroup.
  87.    * createGroup and destroyGroup may be called multiple times during
  88.    * the lifetime of a group.
  89.    */
  90.   protected void initGroup() { }
  91.  
  92.   /**
  93.    * showGroup may be overridden by group subclasses that want
  94.    * to know when the group becomes visible.  It is called just before
  95.    * the group becomes visible.  The group will already be initialized
  96.    * and created at this point.
  97.    */
  98.   protected void showGroup() { }
  99.  
  100.   /**
  101.    * hideGroup may be overridden by group subclasses that want
  102.    * to know when the group becomes non-visible.  It is called just
  103.    * before the group becomes non-visible.
  104.    */
  105.   protected void hideGroup() { }
  106.  
  107.   /**
  108.    * createGroup is called during group creation.  Groups can be
  109.    * created and destroyed multiple times during their lifetime.
  110.    * Anything that is created in createGroup should be cleaned up
  111.    * in destroyGroup.  createGroup is called just after the group
  112.    * has been created.  Anything that needs to be done before the
  113.    * group is created should be done in initGroup.
  114.    */
  115.   protected void createGroup() { }
  116.  
  117.   /**
  118.    * destroyGroup is called during the destroy operation.  Groups can
  119.    * be created and destroyed multiple times during their lifetime.
  120.    * Anything that has been created in createGroup should be cleaned up
  121.    * in destroyGroup.  destroyGroup is called just before the group
  122.    * is destroyed.
  123.    */
  124.   protected void destroyGroup() { }
  125.  
  126.   /**
  127.    * This method may be overridden by group subclasses that want
  128.    * to be informed when the application is starting.  This method is
  129.    * only called after the entire application has been initialized and
  130.    * created.
  131.    *
  132.    * For applets, startGroup is called whenever start is called on the
  133.    * applet.
  134.    */
  135.   protected void startGroup() { }
  136.  
  137.   /**
  138.    * This method may be overridden by group subclasses that want
  139.    * to be informed when the application is stopping.  This method
  140.    * will be called before a destroy is done.
  141.    *
  142.    * For applets, stopGroup is called whenever stop is called on the
  143.    * applet.
  144.    */
  145.   protected void stopGroup() { }
  146.  
  147.   /**
  148.    * "getOnGroup" may be overridden by sub-groups that
  149.    * store attribute values themselves, and do not depend on the
  150.    * group superclass to store them.  This method should be overridden
  151.    * instead of "get".  Any attributes handled in setOnGroup where
  152.    * super.setOnGroup is not called must also be handled in getOnGroup.
  153.    *
  154.    * The default implementation of getOnGroup retrieves the value
  155.    * from the attribute table.
  156.    *
  157.    * The reason that "getOnGroup" should be overridden instead
  158.    * of "get" is that "getOnGroup" is guaranteed not to be called
  159.    * until the group class is initialized.  This means that initRoot
  160.    * will always be called before any calls to getOnGroup are made.
  161.    *
  162.    * Also, this method is only for attributes that are defined in the
  163.    * sub-groups.  It is not called for forwarded attributes.
  164.    */
  165.   protected Object getOnGroup(String key) {
  166.     return super.getOnGroup(key);
  167.   }
  168.  
  169.   /**
  170.    * "setOnGroup" may be overridden by sub-groups that
  171.    * want notification when attributes are changed.  This method
  172.    * should be overridden instead of "set".  Any attributes handled
  173.    * in setOnGroup where super.setOnGroup is not called must also be
  174.    * handled in getOnGroup.
  175.    *
  176.    * The default implementation of setOnGroup puts the value
  177.    * in the attribute table.
  178.    *
  179.    * The reason that "setOnGroup" should be overridden instead
  180.    * of "set" is that "setOnGroup" is guaranteed not to be called
  181.    * until the group class is initialized.  This means that initRoot
  182.    * will always be called before any calls to setOnGroup are made.
  183.    *
  184.    * During initialization, "setOnGroup" will be called for all
  185.    * the group's attributes even if they have not be changed from
  186.    * the default value.  But for attributes that have the DEFAULT
  187.    * flag set, "setOnGroup" will only be called if the value
  188.    * of the attribute has changed from the default.
  189.    *
  190.    * Also, this method is only called when attributes defined in the
  191.    * sub-groups are updated.  It is not called for forwarded attributes.
  192.    */
  193.   protected void setOnGroup(String key, Object value) {
  194.     super.setOnGroup(key, value);
  195.   }
  196.  
  197.   /**
  198.    * handleMessage may be overridden by subclasses that want to act
  199.    * on messages that are sent to the group.  Typically, messages are
  200.    * either AWT events that have been translated to messages, or they
  201.    * are messages that have been sent by other groups.
  202.    * super.handleMessage should be called for any messages that aren't
  203.    * handled.  If super.handleMessage is not called, then handleEvent
  204.    * will not be called.
  205.    *
  206.    * The default implementation of handleMessage returns "true".  This
  207.    * means that no events will be passed up the group tree, unless a
  208.    * subclass overrides this method to return "false".  AWT events are
  209.    * not propagated regardless of the return value from handleEvent.
  210.    *
  211.    * If you want a message to go to the parent group, override
  212.    * handleMessage to return false for that message.
  213.    *
  214.    * If you want an AWT event to go to the parent group, you need to
  215.    * call postMessageToParent() with the event message.
  216.    */
  217.   public boolean handleMessage(Message msg) {
  218.     return super.handleMessage(msg);
  219.   }
  220.  
  221.   /**
  222.    * handleEvent may be overridden by subclasses that want to get
  223.    * notified when AWT events that are sent by the gui components.
  224.    * The return value should be true for handled events, and
  225.    * super.handleEvent should be called for unhandled events.
  226.    * If super.handleEvent is not called, then the specific event
  227.    * handling methods will not be called.
  228.    *
  229.    * The message's target is set to the shadow that sent the event.
  230.    * The event's target is set to the AWT component that sent the event.
  231.    *
  232.    *
  233.    * The following specific event handling methods may also be overridden:
  234.    *
  235.    * public boolean mouseDown(Message msg, Event evt, int x, int y);
  236.    * public boolean mouseDrag(Message msg, Event evt, int x, int y);
  237.    * public boolean mouseUp(Message msg, Event evt, int x, int y);
  238.    * public boolean mouseMove(Message msg, Event evt, int x, int y);
  239.    * public boolean mouseEnter(Message msg, Event evt, int x, int y);
  240.    * public boolean mouseExit(Message msg, Event evt, int x, int y);
  241.    * public boolean keyDown(Message msg, Event evt, int key);
  242.    * public boolean keyUp(Message msg, Event evt, int key);
  243.    * public boolean action(Message msg, Event evt, Object what);
  244.    * public boolean gotFocus(Message msg, Event evt, Object what);
  245.    * public boolean lostFocus(Message msg, Event evt, Object what);
  246.    */
  247.   public boolean handleEvent(Message msg, Event evt) {
  248.     return super.handleEvent(msg, evt);
  249.   }
  250.  
  251.   public void addButtonCallback(Message msg, Event evt) {
  252.     String[] listContents = (String []) gui.namesList.get("items");
  253.     String newItem = (String) gui.nameTextfield.get("text") + " " +
  254.       (String) gui.phoneTextfield.get("text") + " " +
  255.       (String) gui.emailTextfield.get("text");
  256.     int len = 0;
  257.     if (listContents != null)
  258.         listContents.length;
  259.     String[] newContents = new String[len + 1];
  260.     int i = 0;
  261.     for (i=0; i < len; i++) {
  262.       newContents[i] = listContents[i];
  263.     }
  264.     newContents[len] = newItem;
  265.     gui.namesList.set("items",  newContents);
  266.   }
  267.  
  268.   public void findButtonCallback(Message msg, Event evt) {
  269.     List listBody = (List) gui.namesList.getBody();
  270.     int numItems = listBody.countItems();
  271.     String nameToFind = (String) gui.nameTextfield.get("text");
  272.     String currentName = "";
  273.     int i = 0;
  274.     int j = 0;
  275.     int len = 0;
  276.     int nameLen = nameToFind.length();
  277.                           
  278.     for (i=0; i < numItems; i++) {
  279.       currentName = listBody.getItem(i);
  280.       len = currentName.length();
  281.       if (len >= nameLen && 
  282.       len != 0 &&
  283.       nameLen != 0) {
  284.     for (j=0; j < len - nameLen + 1; j++) {
  285.       if (currentName.regionMatches(true,
  286.                     j,
  287.                     nameToFind,
  288.                     0,
  289.                     nameLen) == true) {
  290.         listBody.select(i);
  291.         return;
  292.       }
  293.     }
  294.       }
  295.     }
  296.   }
  297.  
  298.   public void deleteButtonCallback(Message msg, Event evt) {
  299.     List listBody = (List) gui.namesList.getBody();
  300.     int selectedIndex = listBody.getSelectedIndex();
  301.     if (selectedIndex > -1) 
  302.     listBody.delItem(selectedIndex);
  303.   }
  304.  
  305. }
  306.