OSPE_LoadLocalObjectsCat.java 5.32 KB
package osp.ui;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

//import javafx.scene.layout.Priority;
import osp.FieldOfView;
import osp.LocalCatObject;

public class OSPE_LoadLocalObjectsCat 
{
	/** Parent Main Frame */
	private OSPE_MainFrame pater;
	/** Field of view */
	private FieldOfView fov;

OSPE_LoadLocalObjectsCat(OSPE_MainFrame p, FieldOfView fov) 
{	
	pater = p;
	this.fov = fov;
}

public boolean loadLocalObjectsCat(File catFile)
{
	boolean noError=false;
	ArrayList<LocalCatObject> localObjectsList = new ArrayList<LocalCatObject>();
	Point2D.Double position;
	String prop="";
	
	 try 
     { 
         // Prepare to read from the file, using a Scanner object
         Scanner in = new Scanner(catFile);

         String idObj="";

         String ra = null;
         String dec = null;
         String tab[]= new String[5];
         String line;
         int prior=0;
         int i=0;
          String temp="";
          
         // Read each line until end of file is reached
         while (in.hasNextLine())
         {
        	 i=0;
             // Read an entire line, which contains all the details for 1 account
              line = in.nextLine();
             
             // Make a Scanner object to break up this line into parts
			 Scanner lineBreaker = new Scanner(line);
             String firstChar = null;
             

             	if (lineBreaker.hasNext())
             	{
             		firstChar = lineBreaker.next();
               		idObj=firstChar;
             	}
                            
              if ((!firstChar.isEmpty()) && (!firstChar.equals("#")))
              {
             // 1st part is the account number
            	  idObj=firstChar;
            	  
            	  try 
            	  {  
            		  LocalCatObject obj;

            		  // 2nd part is RA
            		  if (lineBreaker.hasNext())
            			  ra = lineBreaker.next();
            	 
            		  // 3nd part is Dec
            		  if (lineBreaker.hasNext())
            			  dec = lineBreaker.next();
            		  
            		  // Test if 4th field - if is number then is priority, if not, is from  
            		  if (lineBreaker.hasNext())
            		  {
            			 String field = lineBreaker.next();
            			 if (!("".equals(field)))
            			 {
            				 if (isInt(field))
            					 prior=Integer.parseInt(field);
            				 else 
            				 {
            					 tab[i] = field;
            					 temp=temp+" "+tab[i];
            					 i++;
            				 }
            			 }
            			  
            		  }
            		  
            		  // Read the rest of the file
            		
            		   while (lineBreaker.hasNext() && i<5)
            		   {
            			tab[i]  = lineBreaker.next();
            			temp=temp+"\\t"+tab[i];
            			i++;
            		   }
            		  
            		  obj=new LocalCatObject(idObj,ra,dec,prior,temp);
            		  temp="";
            		  
            		  /*
            		  position = obj.getPosition();
            		  if (nbObj==0) 
            		  {
            			  posMin = new Point2D.Double(position.x,position.y);
            			  posMax = new Point2D.Double(position.x,position.y);      			  
            		  }
            		  // set the min and max of the objects imported. 
            		  if (position.x< posMin.x)
            			  posMin.x=position.x;
            		  if (position.y< posMin.y)
            			  posMin.y=position.y;
            		  if (position.x> posMax.x)
            			  posMax.x=position.x;
            		  if (position.y> posMax.y)
            			  posMax.y=position.y;
            		  */
            		  localObjectsList.add(obj);
            		  ra=null;
            		  dec=null;
            		  prior=0;
            	  }
              
            	  catch (InputMismatchException e)
            	  {
            		  System.out.println("File not found.");
            	  }
             
            	  catch (NoSuchElementException e)
            	  {
            		  System.out.println("File not found");

            	  }
              }
              lineBreaker.close(); 
         } // End while
         in.close();
     }
     catch (FileNotFoundException e)
     {
         System.out.println("File not found");

     }   // Make an ArrayList to store all the accounts we will make
	
	int objNb= localObjectsList.size();

	String idObj ="";

	int nbCat = pater.getVisuPanel().getOspeControl().getNbLoadCat();
	nbCat++;
	pater.getVisuPanel().getOspeControl().setNbLoadCat(nbCat);
	
	if (objNb >0) 
	{
		for(int i=0;i<objNb;i++)
		{
			//idObj =Integer.parseInt(localObjectsList.get(i).getId());
			idObj=localObjectsList.get(i).getId();
			position=localObjectsList.get(i).getPosition();
			//String [] prop=localObjectsList.get(i).getProperties();
			//String temp="";
			int prior = localObjectsList.get(i).getPriority();				
			prop=localObjectsList.get(i).getProperty();	 
		    pater.getVisuPanel().addObject(idObj,nbCat, prop, position,prior);
		
		}
		fov.update();
	}
	 
	return noError;
}


public boolean isInt(String ch) {
	try {
		Integer.parseInt(ch);
	} catch (NumberFormatException e){
		return false;
	}

	return true;
}
}