home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / DataState.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  8.3 KB  |  274 lines

  1. /*
  2.  * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
  3.  *
  4.  * www.krumel.com - controls@krumel.com
  5.  *
  6.  * Permission is given to the buyer of this package for one software
  7.  * developer to use this software on one CPU (one workstation) and to make
  8.  * one backup copy.  You may uitilize and/or modify this class for use in your
  9.  * projects.  You may distribute or sell any executable which results from
  10.  * using this code in yur application, except a utility or class of similar
  11.  * nature to this product.  You may distribute this product in compiled
  12.  * form only, but soley to be used with your cmpiled executable product
  13.  * for the puposes of dynamic loading. You may NOT redistribute the source
  14.  * code in any form or make it accessible through a network or other
  15.  * distribution media to others. Please refer to the file "copyright.html"
  16.  * for further important copyright and licensing information.
  17.  *
  18.  * The source code is the confidential and proprietary information
  19.  * of Krumel & Associates, Inc. ("Confidential Information").  You shall
  20.  * not disclose such Confidential Information and shall use it only in
  21.  * accordance with the terms of the license agreement you entered into
  22.  * with Krumel & Associates, Inc..
  23.  
  24.  * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  25.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
  26.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
  28.  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  29.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  30.  */
  31.  
  32. package symantec.itools.db.awt;
  33.  
  34. /**
  35.  * Class represents the possible states for a piece of data.
  36.  */
  37. public class DataState {
  38.     int state = NEW;
  39.  
  40.     DataState link;
  41.     /**
  42.      * Data has been created and not updated or saved.
  43.      */
  44.     public static final int NEW = 0;
  45.     /**
  46.      * Data has been previously saved and not modified.
  47.      */
  48.     public static final int CLEAN = 1;
  49.     /**
  50.      * Base value for having been modified.
  51.      */
  52.     static final int MODIFIED = 2;
  53.     /**
  54.      * Data has not been previously saved but has been modified since creation.
  55.      */
  56.     public static final int NEW_MODIFIED = 2;
  57.     /**
  58.      * Data has been previously saved and modified.
  59.      */
  60.     public static final int CLEAN_MODIFIED = 3;
  61.     /**
  62.      * Base value for deletion.
  63.      */
  64.     static final int DELETED = 4;
  65.     /**
  66.      * Data has not been previously saved or modified, but has been marked for deletion.
  67.      */
  68.     public static final int NEW_DELETED = 4;
  69.     /**
  70.      * Data has been previously saved and marked for deletion, but not modified.
  71.      */
  72.     public static final int CLEAN_DELETED = 5;
  73.     /**
  74.      * Data has not been previously saved, but has been modified and marked for deletion.
  75.      */
  76.     public static final int NEW_MOD_DELETED = 6;
  77.     /**
  78.      * Data has been previously save, changed, and marked for deletion.
  79.      */
  80.     public static final int CLEAN_MOD_DELETED = 7;
  81.  
  82.  
  83.     static final int MASK = (1 << 31)-1;
  84.  
  85.     /**
  86.      * Constructs a state of NEW.
  87.      */
  88.     public DataState() {}
  89.  
  90.     /**
  91.      * Creates a state of either NEW or CLEAN.
  92.      */
  93.     public DataState(int initialState) {
  94.         state = (initialState == NEW)  ? NEW  :CLEAN;
  95.     }
  96.  
  97.     /**
  98.      * Gets whether the specified state is a delete state.
  99.      */
  100.     public static boolean deleteState(int state) {
  101.         return (state & DELETED) != 0;
  102.     }
  103.  
  104.     /**
  105.      * Gets whether the object has been marked for deletion.
  106.      */
  107.     public boolean deleted() {
  108.         return (state & DELETED) != 0;
  109.     }
  110.  
  111.     /**
  112.      * Gets whether the object has been modified since creation or the last save.
  113.      */
  114.     public boolean modified() {
  115.         return (state & MODIFIED) != 0;
  116.     }
  117.  
  118.     /**
  119.      * Gets whether the object has been modified since creation or the last save.
  120.      */
  121.     public boolean isModified() {
  122.         return (state & MODIFIED) != 0;
  123.     }
  124.  
  125.     /**
  126.      * Gets whether the object has not been previously saved.
  127.      */
  128.     public boolean isNew() {
  129.         return (state & 1) == 0;
  130.     }
  131.  
  132.     /**
  133.      * Gets whether the object has not been changed since last save.
  134.      */
  135.     public boolean isClean() {
  136.         return state == CLEAN;
  137.     }
  138.  
  139.     /**
  140.      * Gets whether the object has been marked for deletion.
  141.      */
  142.     public boolean isDeleted() {
  143.         return (state & DELETED) != 0;
  144.     }
  145.  
  146.     /**
  147.      * Gets whether the object has been modified or or marked for deletion.
  148.      */
  149.     public boolean changedState() {
  150.         return deleted() || modified();
  151.     }
  152.  
  153.     /**
  154.      * Links this object to the state of another object. If this object's state is modified
  155.      * the link's state will be modified accordingly.
  156.      */
  157.     public void link(DataState l) { link = l; }
  158.  
  159.     /**
  160.      * Gets the state value.
  161.      */
  162.     public int state() { return state; }
  163.  
  164.     /**
  165.      * Marks the object as new object. This method is a noop and is included for completeness.
  166.      * Objects can only be marked as new when they are created.
  167.      */
  168.     public void markNew() {
  169.         //can only mark new if already new - this method is a noop
  170.     }
  171.  
  172.     /**
  173.      * Marks the object as ready for deletion.
  174.      */
  175.     public void markDeleted() {
  176.         state = DELETED | state;
  177.         if (link != null) {
  178.             link.markDeleted();
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Resets the state of the object to the state before it was marked for deletion.
  184.      */
  185.     public void markUndelete() {
  186.         state &= (DELETED ^ MASK);
  187.         if (link != null) {
  188.             link.markUndelete();
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Sets the state to clean (has been saved and not modified).
  194.      */
  195.     public void markClean() {
  196.         state = state | (CLEAN ^ (state>>2));
  197.         if (link != null) {
  198.             link.markClean();
  199.         }
  200.     }
  201.  
  202.     /**
  203.      * Marks the object as having been modified.
  204.      */
  205.     public void markModified() {
  206.         state = state | MODIFIED;
  207.         if (link != null) {
  208.            link.markModified();
  209.         }
  210.  
  211.     }
  212.  
  213.     /**
  214.      * Marks this object as CLEAN but does not make a change in the state link. This
  215.      * is handy when sub objects are being saved before the owner object is saved.
  216.      */
  217.     public void updated() {
  218.         //do not want to update link b/c some action could still depend on
  219.         //master link's state
  220.         state = CLEAN;
  221.     }
  222.  
  223.     /**
  224.      * Prints the state information to the standard output stream.
  225.      */
  226.     public void printState() {
  227.         switch(state) {
  228.             case NEW: System.out.println("state=NEW"); break;
  229.             case CLEAN: System.out.println("state=CLEAN"); break;
  230.             case NEW_MODIFIED: System.out.println("state=NEW_MODIFIED"); break;
  231.             case CLEAN_MODIFIED: System.out.println("state=CLEAN_MODIFIED"); break;
  232.             case NEW_DELETED: System.out.println("state=NEW_DELETED"); break;
  233.             case CLEAN_DELETED: System.out.println("state=CLEAN_DELETED"); break;
  234.             case NEW_MOD_DELETED:
  235.                 System.out.println("state=NEW_MOD_DELETED");
  236.                 break;
  237.             case CLEAN_MOD_DELETED:
  238.                 System.out.println("state=CLEAN_MOD_DELETED");
  239.                 break;
  240.             default: System.out.println("in illegal state - " + state);
  241.         }
  242.     }
  243.  
  244. /*
  245.     public static void main(String a[]) throws Exception {
  246.         DataState ds = new DataState();
  247.         ds.printState();
  248.         System.out.println(ds.isNew());
  249.         ds.markModified();
  250.         System.out.println(ds.isNew());
  251.         ds.printState();
  252.         ds.markDeleted();
  253.         System.out.println(ds.isNew());
  254.         ds.printState();
  255.         ds.markModified();
  256.         ds.printState();
  257.         ds.markUndelete();
  258.         ds.printState();
  259.         ds.markClean();
  260.         ds.printState();
  261.         ds.updated();
  262.         System.out.println(ds.isNew());
  263.         ds.printState();
  264.         ds.markModified();
  265.         ds.printState();
  266.         ds.markDeleted();
  267.         ds.printState();
  268.         ds.markUndelete();
  269.         ds.printState();
  270.  
  271.         System.in.read();
  272.     }
  273. */
  274. }