Back to index

wims  3.65+svn20090927
Cell.java
Go to the documentation of this file.
00001 /*
00002  * @(#)Cell.java
00003  *
00004  * $Id: Cell.java,v 1.27 2000/12/05 06:05:18 rkc10 Exp $
00005  *
00006  * Created on October 14, 2000, 6:23 PM
00007  */
00008 package SharpTools; 
00009 import java.util.*;
00010 
00022 public class Cell {
00023 
00025     public static final int TEXT = 0;
00026 
00028     public static final int NUMBER = 1;
00029 
00031     public static final int FORMULA = 2;
00032 
00034     private Object value;
00035     
00040     private Formula expression;
00041 
00046     private TreeSet refs;
00047 
00049     public Cell() {
00050         value = null;
00051         expression = null;
00052         refs = null;
00053     }
00054 
00063     public Cell(Object datum) {
00064         value = datum;
00065         expression = null;
00066         refs = null;
00067     }
00068         
00077     public Cell(Object datum, TreeSet reference) {
00078         value = datum;
00079         expression = null;
00080         refs = reference;
00081     }
00082     
00090     public Cell(Formula thing, Object eVal, TreeSet reference) {
00091         expression = thing;
00092         value = eVal;
00093         refs = reference;
00094     }
00095 
00103     public Formula getFormula() {
00104         return expression;
00105     }
00106 
00107     
00108 
00115     public Object getValue() {
00116         return value;
00117     }
00118 
00119     
00125     public void setValue(Object datum) {
00126         value = datum;
00127     }
00128 
00140     public void setData(Object datum) {
00141         value = datum;
00142         expression = null;
00143     }  
00144 
00153     public void setFormula(Formula form) {
00154         expression = form;
00155     }
00156 
00160     public boolean isEmpty() {
00161        if(value.equals("") && expression == null)
00162            return true;
00163        else
00164            return false;
00165     }
00166         
00172     public boolean hasRefs() {
00173         return (refs != null && !refs.isEmpty());
00174     }
00175 
00183     public TreeSet getRefs() {
00184        return refs;
00185     }
00186 
00192     public void removeRef(CellPoint reference) {
00193        //     System.out.println("remove ref "+reference+" from "+this);
00194        if (refs != null) {
00195            refs.remove(reference);
00196            if (refs.isEmpty()) refs = null;
00197        }
00198     }
00199 
00206     public void addRef(CellPoint reference) {
00207        //     System.out.println("add ref "+reference+" to ("+this);
00208         if (refs == null) {// check if refs is initiated
00209             refs = new TreeSet();
00210         }
00211         refs.add(reference);   
00212     }
00213 
00221     public int getType() {
00222         if (expression != null) return Cell.FORMULA;
00223         if (value instanceof Number) return Cell.NUMBER;
00224         return Cell.TEXT;
00225     }
00226    
00232     public boolean isFormula() {
00233         return (expression != null);
00234     }
00235     
00241     public boolean isErrorCell() {
00242         return ((isFormula()) && (value instanceof ParserException));
00243     }
00244     
00256     public int compare(Cell x, boolean ascending) {
00257         //if this is blank which is lowest value
00258         if (this.value.equals("")) {
00259             //x is not blank so this is less than
00260             if (!x.value.equals("")) {
00261                 if (ascending) {
00262                     return 1;
00263                 } else {
00264                     return -1;
00265                 }
00266             } else {               //otherwise equal
00267                 return 0;
00268             }
00269         } else {
00270             if (x.value.equals("")) {
00271                 if (ascending) {
00272                     return -1;
00273                 }else {
00274                     return 1;
00275                 }
00276             } else {               //both have values
00277                 if (this.isErrorCell()) {
00278                     if (x.isErrorCell()) {
00279                         return 0;
00280                     } else {
00281                         return -1;
00282                     }
00283                 } else {
00284                     if (x.isErrorCell()) {
00285                         return 1;
00286                     } else {
00287                         if (this.getType() == Cell.TEXT) {
00288                             if (x.getType() == Cell.TEXT) {
00289                                 String temp = (String)this.value;
00290                                 return temp.compareToIgnoreCase((String)x.value);
00291                             } else {
00292                                 return -1;
00293                             }
00294                         } else {
00295                             if (x.getType() == Cell.TEXT) {
00296                                 return 1;
00297                             } else {
00298                                 Float first = (Float)this.value;
00299                                 Float second = (Float)x.value;
00300                                 return first.compareTo(second);
00301                             }
00302                         }
00303                     }
00304                 }
00305             }
00306         }
00307     } 
00308     
00309     public String toString() {
00310         
00311         if (expression != null)
00312             return "="+expression.toString();
00313        else
00314            return value.toString();
00315     }
00316 }
00317