/** * */ package io; // imports import java.io.*; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import model.color.ColorConst; /** * @author John H. Krantz * Allows saving of text output files * @version 0.1 */ public class SaveFile { JFileChooser fileChooser = new JFileChooser(); // flags that affect saving the file private boolean flipVert = true; // directory information private String dir = null; /** Saves all the text in this TextPanel to a file. Set 'path' to "" to display a save as dialog. Returns 'false' if the user cancels the save as dialog.*/ public String saveAs(double [][][] data, String summary, String directory, String ext) { if (dir == null) { dir = directory; } double [][][] modelData = data; // create the output filename JFrame chooserFrame = new JFrame("Save Data File"); fileChooser.setDialogTitle("Save Model Data File"); fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES); if (dir != null) fileChooser.setCurrentDirectory(new File(dir)); File dflt = new File("*"+ext); fileChooser.setSelectedFile(dflt); int result = fileChooser.showSaveDialog(chooserFrame); chooserFrame.setVisible(true); chooserFrame.setVisible(false); if (result == JFileChooser.CANCEL_OPTION){ } else { // get file and see if it has the proper extension File holdFile = fileChooser.getSelectedFile(); String fName = holdFile.getName(); if (fName.endsWith(ext)){ fName = holdFile.getAbsolutePath(); } else { // if not have extention add it. fName = holdFile.getAbsolutePath()+ext; } // create the file to be saved with the extension File saveFile = new File(fName); dir = saveFile.getAbsolutePath(); String outDir = saveFile.getAbsolutePath()+File.separator; // now create the string that has to be written DataOutputStream out; try{ out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outDir))); PrintWriter pw = new PrintWriter(out); double value; // print summary pw.print(summary); int yPos; for (int a = 0; a < modelData.length; a ++){ for (int y = 0; y <= modelData[0][0].length-1; y ++){ for (int x = 0; x < modelData[0].length ; x ++){ yPos = y; if (flipVert) yPos = modelData[0][0].length-1-y; value = modelData[a][x][yPos]; pw.print(value); if (x < modelData[0].length-1) pw.print(" "); } // end for x = columns pw.println(); } // end for y rows }// end for a angle pw.close(); out.close(); } // end try catch (IOException e) { JOptionPane.showMessageDialog(null, "Error Saving File:\n"+ e.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE); } // end catch ioexception }// end else a file has been selected return dir; }// end saveAs /** Saves all the text in this TextPanel to a file. Set 'path' to "" to display a save as dialog. Returns 'false' if the user cancels the save as dialog.*/ public String saveGabor(double [][][] data, String summary, String directory, String ext) { if (dir == null) { dir = directory; } double [][][] modelData = data; // create the output filename JFrame chooserFrame = new JFrame("Save Gabor File"); fileChooser.setDialogTitle("Save Model Gabor File"); fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES); if (dir != null) fileChooser.setCurrentDirectory(new File(dir)); File dflt = new File("*"+ext); fileChooser.setSelectedFile(dflt); int result = fileChooser.showSaveDialog(chooserFrame); chooserFrame.setVisible(true); chooserFrame.setVisible(false); if (result == JFileChooser.CANCEL_OPTION){ } else { // get file and see if it has the proper extension File holdFile = fileChooser.getSelectedFile(); String fName = holdFile.getName(); if (fName.endsWith(ext)){ fName = holdFile.getAbsolutePath(); } else { // if not have extention add it. fName = holdFile.getAbsolutePath()+ext; } // create the file to be saved with the extension File saveFile = new File(fName); dir = saveFile.getAbsolutePath(); String outDir = saveFile.getAbsolutePath()+File.separator; // now create the string that has to be written DataOutputStream out; try{ out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outDir))); PrintWriter pw = new PrintWriter(out); double value; // print summary pw.print(summary); int yPos; double aMult = 180.0/(double)modelData[0][0].length; for (int a = 0; a < modelData[0][0].length; a ++){ pw.println("Angle= "+((double)a*aMult)); for (int y = 0; y <= modelData.length-1; y ++){ for (int x = 0; x < modelData[0].length ; x ++){ yPos = y; if (flipVert) yPos = modelData[0][0].length-1-y; value = modelData[yPos][x][a]; pw.print(value); if (x < modelData[0].length-1) pw.print(" "); } // end for x = columns pw.println(); } // end for y rows }// end for a angle pw.close(); out.close(); } // end try catch (IOException e) { JOptionPane.showMessageDialog(null, "Error Saving File:\n"+ e.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE); } // end catch ioexception }// end else a file has been selected return dir; }// end saveGabor /** Saves all the text in this TextPanel to a file. Set 'path' to "" to display a save as dialog. Returns 'false' if the user cancels the save as dialog.*/ public String saveDOG(double [][][] data, String summary, String directory) { String ext = ".dog"; if (dir == null) { dir = directory; } double [][][] modelData = data; // create the output filename JFrame chooserFrame = new JFrame("Save DOG File"); fileChooser.setDialogTitle("Save Model DOG File"); fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES); if (dir != null) fileChooser.setCurrentDirectory(new File(dir)); File dflt = new File("*"+ext); fileChooser.setSelectedFile(dflt); int result = fileChooser.showSaveDialog(chooserFrame); chooserFrame.setVisible(true); chooserFrame.setVisible(false); if (result == JFileChooser.CANCEL_OPTION){ } else { // get file and see if it has the proper extension File holdFile = fileChooser.getSelectedFile(); String fName = holdFile.getName(); if (fName.endsWith(ext)){ fName = holdFile.getAbsolutePath(); } else { // if not have extention add it. fName = holdFile.getAbsolutePath()+ext; } // create the file to be saved with the extension File saveFile = new File(fName); dir = saveFile.getAbsolutePath(); String outDir = saveFile.getAbsolutePath()+File.separator; // now create the string that has to be written DataOutputStream out; try{ out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outDir))); PrintWriter pw = new PrintWriter(out); double value; // print summary pw.print(summary); int yPos; for (int c = 0; c < modelData[0][0].length; c ++){ pw.println("Color Opponent Channel "+ ColorConst.OPPONENT_CHANNELS[c]); for (int y = 0; y <= modelData.length-1; y ++){ for (int x = 0; x < modelData[0].length ; x ++){ yPos = y; if (flipVert) yPos = modelData[0][0].length-1-y; value = modelData[yPos][x][c]; pw.print(value); if (x < modelData[0].length-1) pw.print(" "); } // end for x = columns pw.println(); } // end for y rows }// end for a angle pw.close(); out.close(); } // end try catch (IOException e) { JOptionPane.showMessageDialog(null, "Error Saving File:\n"+ e.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE); } // end catch ioexception }// end else a file has been selected return dir; }// end saveDOG /** Saves all the text in this TextPanel to a file. Set 'path' to "" to display a save as dialog. Returns 'false' if the user cancels the save as dialog.*/ public String saveDiff(double [][] diffs, String summary, String directory) { String ext = ".diff"; if (dir == null) { dir = directory; } double [][] data = diffs; // create the output filename JFrame chooserFrame = new JFrame("Save Difference File"); fileChooser.setDialogTitle("Save Model Difference File"); fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES); if (dir != null) fileChooser.setCurrentDirectory(new File(dir)); File dflt = new File("*"+ext); fileChooser.setSelectedFile(dflt); int result = fileChooser.showSaveDialog(chooserFrame); chooserFrame.setVisible(true); chooserFrame.setVisible(false); if (result == JFileChooser.CANCEL_OPTION){ } else { // get file and see if it has the proper extension File holdFile = fileChooser.getSelectedFile(); String fName = holdFile.getName(); if (fName.endsWith(ext)){ fName = holdFile.getAbsolutePath(); } else { // if not have extention add it. fName = holdFile.getAbsolutePath()+ext; } // create the file to be saved with the extension File saveFile = new File(fName); dir = saveFile.getAbsolutePath(); String outDir = saveFile.getAbsolutePath()+File.separator; // now create the string that has to be written DataOutputStream out; try{ out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outDir))); PrintWriter pw = new PrintWriter(out); double value; // print summary pw.print(summary); int yPos; for (int y = 0; y <= data.length-1; y ++){ for (int x = 0; x < data[0].length ; x ++){ yPos = y; if (flipVert) yPos = data[0].length-1-y; value = data[yPos][x]; pw.print(value); if (x < data[0].length-1) pw.print(" "); } // end for x = columns pw.println(); } // end for y rows pw.close(); out.close(); } // end try catch (IOException e) { JOptionPane.showMessageDialog(null, "Error Saving File:\n"+ e.getMessage(), "Save Error", JOptionPane.ERROR_MESSAGE); } // end catch ioexception }// end else a file has been selected return dir; }// end saveDiff // set methods public void setFlipVertical(boolean b){ flipVert = b; } }