//Imports small.v5d and sets up specified mappings.

packag
e visad.ss;

import java.rmi.RemoteException;
import v
isad.*;
import java.io.*;

import org.jgap.*;


public class VisadAPI {

   public static SpreadSheet
 mySS;
   //public static BasicSSCell myBasicSS;
   

	public JgapAPI theJgapAPI;  // to call methods in JgapAPI.java

	public DisplayRealType[] mappings;  //array of "mapFrom" fie
lds
   
   
   // constructs a JgapAPI instance and ca
lls method to construct table of mappings
   public VisadAPI(Spr
eadSheet ss) {
   	
   		theJgapAPI = new JgapAPI(4
, 10, 50); //4 cells now since spreadsheet currently shows a 2x2

   		mySS = ss;
   		mappings = new DisplayRealTy
pe[50];
   		setTable();
   	}

	//sets u
p hashtable of mappings from input fields to their corresponding "Dis
playRealType"'s
	public void setTable() {
	
	
	mappings[0] = Display.XAxis;
		mappings[1] = Display.
YAxis;
		mappings[2] = Display.ZAxis;
		mappings[
3] = Display.Latitude;
		mappings[4] = Display.Longitude
;
		mappings[5] = Display.Radius;
		mappings[6]
= Display.CylRadius;
		mappings[7] = Display.CylAzimuth;

		mappings[8] = Display.CylZAxis;
		mappings[9]
= Display.List;
		mappings[10] = Display.Red;
	
	mappings[11] = Display.Green;
		mappings[12] = Displa
y.Blue;
		mappings[13] = Display.RGB;
		mappings[
14] = Display.RGBA;
		mappings[15] = Display.Hue;

		mappings[16] = Display.Saturation;
		mappings[17]
 = Display.Value;
		mappings[18] = Display.HSV;

		mappings[19] = Display.Cyan;
		mappings[20] = Disp
lay.Magenta;
		mappings[21] = Display.Yellow;
	
	mappings[22] = Display.CMY;
		mappings[23] = Display.
Alpha;
		mappings[24] = Display.Animation;
		mapp
ings[25] = Display.SelectValue;
		mappings[26] = Display
.SelectRange;
		mappings[27] = Display.IsoContour;

		mappings[28] = Display.Flow1X;
		mappings[29] = Di
splay.Flow1Y;
		mappings[30] = Display.Flow1Z;
	
	mappings[31] = Display.Flow1Elevation;
		mappings[32]
= Display.Flow1Azimuth;
		mappings[33] = Display.Flow1Ra
dial;
		mappings[34] = Display.Flow2X;
		mappings
[35] = Display.Flow2Y;
		mappings[36] = Display.Flow2Z;

		mappings[37] = Display.Flow2Elevation;
		mappi
ngs[38] = Display.Flow2Azimuth;
		mappings[39] = Display
.Flow2Radial;
		mappings[40] = Display.XAxisOffset;

		mappings[41] = Display.YAxisOffset;
		mappings[42
] = Display.ZAxisOffset;
		mappings[43] = Display.Shape;

		mappings[44] = Display.ShapeScale;		
		map
pings[45] = Display.Text;
		mappings[46] = Display.LineW
idth;
		mappings[47] = Display.PointSize;
		mappi
ngs[48] = Display.LineStyle;
		mappings[49] = Display.Te
xtureEnable;		 				
	}
		

   /
**
    * loads small.v5d into top left spreadsheet cell,

  * called from SpreadSheet.java
    */
   public static vo
id loadMyDataSet(String fileName, int width, int height) throws VisAD
Exception, RemoteException {
      try {
      	mySS.setA
utoDetect(false);
         if (mySS != null) {

		mySS.DisplayCells[width][height].addDataSource(fileName, BasicS
SCell.URL_SOURCE);
         		setMappings();

		//setCellMapping(width, height);  // originally, setMappings()
was called when a DATACHANGE occurred in  ssCellChanged()

   		//but now we can call it right away since J3D is installed

         		
         				//mySS.DisplayCells[w
idth][height].addSSCellListener(this);
	           			//
mySS.DisplayCells[0][0].addDataSource(fileName, BasicSSCell.URL_SOURC
E);
         }
      }
      catch (VisADException exc
) { exc.printStackTrace(); }
      catch (RemoteException exc) {
 exc.printStackTrace(); }
   }
  
  	// sets up Scal
arMap[] array to pass into setMaps. sets up mapping for each gene for
 cell at (i,j)
	  	public void setCellMapping(int width, int
 height) {
  			
  			MappingDialog mapDialog
= mySS.DisplayCells[width][height].mapDialog;
  			int f
ields = theJgapAPI.field_count;            //used as arg to getMapp
ingForCell()
  			int cell_number = width * height;
        //used as arg to getMappingForCell()
  			

			ScalarMap[] theMaps = new ScalarMap[fields];   //array tha
t will be passed into setMaps()
  			String[] mapsFrom
= mapDialog.getMapsFrom();  //the array of input fields for given f
ile	
  			
  			for (int i = 0; i < fields;
 i++) {
  				int assignment = theJgapAPI.getMappingFo
rCell(cell_number, i);
  				DisplayRealType mapTo = m
appings[assignment];
  				String name = mapsFrom[i];
//string name of input field i for this file, small.v5d for now

  				RealType mapFrom = new RealType(name);  //construc
ts a RealType object from a given String name
  		

				theMaps[i] = new ScalarMap(mapFrom, mapTo);

			}
  		mySS.DisplayCells[width][height].setMaps(theM
aps);  		
  }
  
  /*
  	//sets the mapping
s for every cell in spreadsheet
  	public void setAllMappings(
) {
  		for (int i = 0; i < mySS.NumVisX; i++) {

			for (int j = 0; j < mySS.NumVisY; j++) {
  		
		setCellMapping(i, j);
  			}
 	 	}

	}
  	*/	
  	
  	//calls setPop, calls setCh
oices, 
  	public void updateGeneticCells() {
  		

  		//find method or write a method to delete all cells fro
m previous generation!!!
  		theJgapAPI.setChoices(promptUse
r());//set up array of user's choices, figure out how mouse click sel
ects cells)
  		theJgapAPI.setPop();  //evolves new generati
on with user's selections
  		deleteAllCells();   //clear ce
lls from previous generation 
  		//setAllMappings();  	//
assigns new set of mappings based on what setPop assigned		

  	}


 	// deletes all cells in spreadsheet wi
thout asking user if it's ok to do so, method does not
 	// de
pend on # of rows or columns 
  	public void deleteAllCells()
{
  		
  		int rowCells = mySS.NumVisY;

		int columnCells = mySS.NumVisX;
 	  		for (int i
= 0; i < columnCells; i++) {
  				for (int j = 0; j
 < rowCells; j++) {
  					try {
   			
			mySS.DisplayCells[i][j].clearCell();
    				}

    			catch (VisADException exc) {
      		
		displayErrorMessage("Cannot clear display mappings", exc, "VisA
D SpreadSheet error");
    			}
    			catch (R
emoteException exc) {
      				displayErrorMessage("Can
not clear display mappings", exc, "VisAD SpreadSheet error");

  			}
    		}
    	}
    	refreshFormul
aBar();
    	refreshMenuCommands();
  }

/*

   public VisadAPI(SpreadSheet ss, BasicSSCell basic) {

    mySS = ss;
      //myBasicSS = basic;
      myBasic
SS.addSSCellListener(this);
   }
*/

    // this
is intended to be called by main for testing purposes only in the

    // command line!
	public int[] promptUser() throws I
OException {
	    
	    int[] myChoices = new int[2];

		System.out.println("Enter cell numbers you want for the n
ext generation");
		BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
		char[] cbuf = new ch
ar[2]; 
		int charsRead = in.read(cbuf, 0, 2);
 	
	for (int i = 0; i < 2; i++) {
			myChoices[i] = Int
eger.parseInt(String.valueOf(cbuf[i]));
		}
		retur
n myChoices;
	}


   /** sets up default mappin
gs when image is first loaded in. **/
   public static void setM
appings() throws VisADException, RemoteException {
      try {

         ScalarMap Alt_rad = new ScalarMap(RealType.Altitude,
 Display.Radius);
         ScalarMap Alt_green = new ScalarMap
(RealType.Altitude, Display.Green);
         ScalarMap Lat_blue
= new ScalarMap(RealType.Latitude, Display.Blue);
         Sca
larMap Long_red = new ScalarMap(RealType.Longitude, Display.Red);

         ScalarMap Long_long = new ScalarMap(RealType.Longitu
de, Display.Longitude);
         ScalarMap Lat_lat = new Scala
rMap(RealType.Latitude, Display.Latitude);
         myBasicSS.se
tMaps(new ScalarMap[] {
            Alt_rad, Alt_green, Lat_blue
, Long_red, Long_long, Lat_lat
         });
      }

    catch (VisADException exc) { exc.printStackTrace(); }

catch (RemoteException exc) { exc.printStackTrace(); }
   }

  

   /** called each time myBasicSS changes 
   p
ublic void ssCellChanged(SSCellChangeEvent e) {
      if (e.getC
hangeType() != SSCellChangeEvent.DATA_CHANGE) return;
      if
 (e.getSSCell() != myBasicSS) return;
      String source
= myBasicSS.getDataSource("A1d1");
      if (source == nul
l || !source.endsWith("small.v5d")) return;
      try {

      setMappings();
      }
      catch (VisADException ex
c) { exc.printStackTrace(); }
      catch (RemoteException exc)
{ exc.printStackTrace(); }
   }
  
  */
  


}

