Advertisement
Guest User

PositionFileIO.java

a guest
Oct 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package sinalgo.io.positionFile;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.LineNumberReader;
  8. import java.io.PrintStream;
  9.  
  10. import javax.swing.JFileChooser;
  11.  
  12. import sinalgo.configuration.AppConfig;
  13. import sinalgo.configuration.Configuration;
  14. import sinalgo.configuration.WrongConfigurationException;
  15. import sinalgo.io.eps.Exporter.PositionFileFilter;
  16. import sinalgo.io.eps.Exporter.SingleFileFilter;
  17. import sinalgo.nodes.Node;
  18. import sinalgo.nodes.Position;
  19. import sinalgo.tools.Tools;
  20.  
  21. /**
  22.  * @author rflury
  23.  *
  24.  */
  25. public class PositionFileIO {
  26.  
  27.     private static final String separator = "#####----- start of node posiitons -----#####";
  28.    
  29.     /**
  30.      * Creates a file containing a list of the positions
  31.      * of all nodes currently hold by the framework.
  32.      * @param The name of the file, null if the user should be asked using a file-dialog
  33.      * @return true upon success, otherwise false.
  34.      */
  35.     public static boolean printPos(String name) {
  36.         if(name == null) {
  37.             JFileChooser fc = new JFileChooser(AppConfig.getAppConfig().getLastSelectedFileDirectory());
  38.             fc.setDialogTitle("Select destination file");
  39.             SingleFileFilter posFf = new PositionFileFilter();
  40.             fc.setFileFilter(posFf);
  41.             if(fc.showSaveDialog(Tools.getGUI()) == JFileChooser.APPROVE_OPTION){
  42.                 name = fc.getSelectedFile().getAbsolutePath();
  43.                 String p = name;
  44.                 p = p.substring(0, p.length() - fc.getSelectedFile().getName().length()); // remember the selected path
  45.                 AppConfig.getAppConfig().lastSelectedFileDirectory = p;
  46.             } else {
  47.                 return false; // (aborted)
  48.             }
  49.         }
  50.  
  51.         try {
  52.             PrintStream ps = new PrintStream(name);
  53.             // header contains # of nodes and dimension of deployment field
  54.             ps.println("Number of nodes: " + Tools.getNodeList().size());
  55.             Configuration.printConfiguration(ps);
  56.             ps.println(separator);
  57.  
  58.             for(Node n : Tools.getNodeList()) {
  59.                 Position p = n.getPosition();
  60.                 ps.println(p.xCoord + ", " + p.yCoord + ", " + p.zCoord);
  61.             }
  62.             ps.close();
  63.             return true;
  64.         } catch (FileNotFoundException e) {
  65.             Tools.minorError(e.getMessage());
  66.         }
  67.         return false;
  68.     }
  69.    
  70.     /**
  71.      * Opens a file dialog that lets the user select the position file
  72.      * to read from.
  73.      * @param  fileName The name of the file to open, null if a dialog to choose the file should be shown
  74.      * @return A reader instance of the selected file
  75.      * @throws PositionFileException
  76.      */
  77.     public static LineNumberReader getPositionFileReader(String fileName) throws PositionFileException {
  78.         LineNumberReader reader = null;
  79.         String name = null;
  80.        
  81.         if(fileName == null) {
  82.             JFileChooser fc = new JFileChooser(AppConfig.getAppConfig().getLastSelectedFileDirectory());
  83.             fc.setDialogTitle("Select input file");
  84.             SingleFileFilter posFf = new PositionFileFilter();
  85.             fc.setAcceptAllFileFilterUsed(true);
  86.             fc.setFileFilter(posFf);
  87.             if(fc.showOpenDialog(Tools.getGUI()) != JFileChooser.APPROVE_OPTION){
  88.                 throw new PositionFileException("Aborted file selection");
  89.             }
  90.             name = fc.getSelectedFile().getPath();
  91.             String p = name;
  92.             p = p.substring(0, p.length() - fc.getSelectedFile().getName().length()); // remember the selected path
  93.             AppConfig.getAppConfig().lastSelectedFileDirectory = p;
  94.         } else {
  95.             name = fileName;
  96.         }
  97.  
  98.         try {
  99.             reader = new LineNumberReader(new FileReader(new File(name)));
  100.         } catch (FileNotFoundException e) {
  101.             throw new PositionFileException(e.getMessage());
  102.         }
  103.         try {
  104.             // skip the first lines
  105.             String numNodes = reader.readLine();
  106.             while(numNodes != null && !numNodes.equals(separator)) {
  107.                 numNodes = reader.readLine();
  108.             }
  109.         } catch (IOException e) {
  110.             throw new PositionFileException(e.getMessage());
  111.         }
  112.         return reader;
  113.     }
  114.    
  115.     public static Position getNextPosition(LineNumberReader reader) {
  116.         try {
  117.             String line = reader.readLine();
  118.             if(line == null) {
  119.                 throw new PositionFileException("The specified file contains not enough positions");   
  120.             }
  121.             try {
  122.                 String[] parts = line.split(",");
  123.                 if(parts.length < 3) {
  124.                     throw new PositionFileException("Illegal line: expected three doubles, separated by comma. Found \n" + line);  
  125.                 }
  126.                 double x = Double.parseDouble(parts[0]);
  127.                 double y = Double.parseDouble(parts[1]);
  128.                 double z = Double.parseDouble(parts[2]);
  129.                 return new Position(x,y,z);
  130.             } catch(NumberFormatException e) {
  131.                 throw new PositionFileException("Illegal line: expected three doubles, separated by comma. Found \n" + line);
  132.             }
  133.         } catch (IOException e) {
  134.             throw new PositionFileException(e.getMessage());
  135.         }
  136.     }
  137.    
  138.    
  139.     @SuppressWarnings("serial")
  140.     public static class PositionFileException extends WrongConfigurationException { // needs not be caught
  141.         public PositionFileException(String msg) {
  142.             super(msg);
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement