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 / ImageStringData.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  7.5 KB  |  245 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. import java.awt.Image;
  35.  
  36. /**
  37.  * This class serves as the workhorse for transferring data from a DataSource to a TableCell.
  38.  * It provides support for transferring String and image information.
  39.  */
  40. public class ImageStringData implements Data {
  41.     StringBuffer        text;
  42.     Image               im;
  43.  
  44.     String              origText;
  45.     boolean             changed = false;
  46.     DataSource          dataSource;
  47.  
  48.     /**
  49.      * Creates a Data element using the specified data source. The string data is set to
  50.      * the emtpy string and the image is set to null.
  51.      */
  52.     public ImageStringData(DataSource ds) {
  53.         this(ds, "", null);
  54.     }
  55.  
  56.     /**
  57.      * Creates a Data element using the specified data source. The string data is set to
  58.      * the value passed as the parameter string and the image is set to null.
  59.      */
  60.     public ImageStringData(DataSource ds, String t) {
  61.         this(ds, t, null);
  62.     }
  63.  
  64.     /**
  65.      * Creates a Data element using the specified data source. The string data is set to
  66.      * the null and the image is set to specifeid paramter value.
  67.      */
  68.     public ImageStringData(DataSource ds, Image i) {
  69.         this(ds, null, i);
  70.     }
  71.  
  72.     /**
  73.      * Creates a Data element using the specified data source. The string data and image
  74.      * data are set to the specified values.
  75.      */
  76.     public ImageStringData(DataSource ds, String t, Image i) {
  77.         dataSource = ds;
  78.         text = new StringBuffer(t);
  79.         origText = t;
  80.         im = i;
  81.     }
  82.  
  83.     /**
  84.      * Gets the value of the type flag to indicate the type of information
  85.      * held by the data object.
  86.      * @return One of STRING, IMAGE, STRING_IMAGE as defined in the Data class.
  87.      */
  88.     public int type() {
  89.         if (text != null && im != null) {
  90.             return IMAGE_STRING;
  91.         } else if (im == null) {
  92.             return STRING;
  93.         }
  94.  
  95.         return IMAGE;
  96.     }
  97.  
  98.     /**
  99.      * Gets whether the data can be modified.
  100.      */
  101.     public boolean isEditable(int row, int col) {
  102.         return dataSource.isDataEditable(row, col);
  103.     }
  104.  
  105.     /**
  106.      * Gets whether the data has been changed since last committed.
  107.      */
  108.     public boolean changed() { return changed; }
  109.  
  110.      /**
  111.      * Requests the value of the data be returned to the last save point.
  112.      */
  113.    public void rollback() {
  114.         text.setLength(0);
  115.         text.append(origText);
  116.         changed = false;
  117.     }
  118.  
  119.     /**
  120.      * Commits the current data as the current value. This method is normally called
  121.      * by the data source when a commit request was issued.
  122.      * @exception TypeNotSupported if the data source does not support the type of
  123.      *          action requested or is not successful
  124.      */
  125.     public void commit() throws TypeNotSupported {
  126.         if (changed) {
  127.             origText = text.toString();
  128.             changed = false;
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * Gets whether the data supports multiple predefined choice data.
  134.      * @return Always return false.
  135.      */
  136.     public boolean supportsChoice() { return false; }
  137.     /**
  138.      * Gets an array of Data objects that define the predefined values associated
  139.      * with a field. This method always throws a TypeNotSupported exception.
  140.      * @exception TypeNotSupported since choices are not supported
  141.      */
  142.     public Data[] getChoices() throws TypeNotSupported {
  143.         throw new TypeNotSupported("ImageStringData does not support choices");
  144.     }
  145.  
  146.     /**
  147.      * Sets a new image for the field.
  148.      */
  149.     public void setImage(Image i) {
  150.         changed = true;
  151.         im = i;
  152.     }
  153.  
  154.     /**
  155.      * Sets the string value for the data.
  156.      */
  157.     public void setText(String t) {
  158.         text.setLength(0);
  159.         text.append(t);
  160. //        origText = t;
  161.         changed = true;
  162.     }
  163.  
  164.     /**
  165.      * Sets the string value for the field using a character.
  166.      */
  167.     public void setText(char c) {
  168.         text.setLength(0);
  169.         text.append(c);
  170.         changed = true;
  171.     }
  172.  
  173.     /**
  174.      * Inserts a character into the string data.
  175.      * @param pos The position in the string to insert the character. Follows the
  176.      *      same rules as the StringBuffer class.
  177.      * @param c The character to insert.
  178.      */
  179.     public void insertChar(int pos, char c) {
  180.         text.insert(pos, c);
  181.         changed = true;
  182.     }
  183.  
  184.     /**
  185.      * Appends a character to the end of the string data.
  186.      */
  187.     public void appendChar(char c) {
  188.         text.append(c);
  189.         changed = true;
  190.     }
  191.  
  192.     /**
  193.      * Gets a substring from the string data for the field.
  194.      */
  195.     public String subString(int spos, int epos) {
  196.         return text.toString().substring(spos, epos);
  197.     }
  198.  
  199.     //pos is space after char to delete
  200.     /**
  201.      * Removes a character from the string data for the field.
  202.      */
  203.     public void deleteChar(int pos) {
  204.         //at beginning so do nothing
  205.         if (pos <= 0)
  206.             return;
  207.  
  208.         String t = text.toString();
  209.         text.setLength(0);
  210.         changed = true;
  211.  
  212.         if (pos >= t.length()) {
  213.             //pos at end so take off last char
  214.             text.append(t.substring(0, t.length()-1));
  215.         } else {
  216.             //take the char before the cursor
  217.             text.append(t.substring(0, pos-1));
  218.             text.append(t.substring(pos, t.length()));
  219.         }
  220.     }
  221.  
  222.     /**
  223.      * Sets the string value to the empty string.
  224.      */
  225.     public void clearText() {
  226.         text.setLength(0);
  227.         changed = true;
  228.     }
  229.  
  230.     /**
  231.      * Gets the current string value for the field.
  232.      */
  233.     public String toString() {
  234.         return text.toString();
  235.     }
  236.  
  237.     /**
  238.      * Gets teh current image value for the field.
  239.      */
  240.     public Image toImage() {
  241.         return im;
  242.     }
  243. }
  244.  
  245.