public void totalExport() { File expf = new File("export"); if (expf.exists()) rmrf(expf); expf.mkdirs(); for (int sto = 0; sto < storeLocs.size(); sto++) { try { String sl = storeLocs.get(sto).getAbsolutePath().replaceAll("/", "-").replaceAll("\\\\", "-"); File estore = new File(expf, sl); estore.mkdir(); File log = new File(estore, LIBRARY_NAME); PrintWriter pw = new PrintWriter(log); for (int i = 0; i < store.getRowCount(); i++) if (store.curStore(i) == sto) { File enc = store.locate(i); File dec = sec.prepareMainFile(enc, estore, false); pw.println(dec.getName()); pw.println(store.getValueAt(i, Storage.COL_DATE)); pw.println(store.getValueAt(i, Storage.COL_TAGS)); synchronized (jobs) { jobs.addLast(expJob(enc, dec)); } } pw.close(); } catch (IOException exc) { exc.printStackTrace(); JOptionPane.showMessageDialog(frm, "Exporting Failed"); return; } } JOptionPane.showMessageDialog(frm, "Exporting to:\n " + expf.getAbsolutePath()); }
public static void stringToFile(String file, String string) { try { if (file.lastIndexOf('\\') != -1) { File par = new File(file.substring(0, file.lastIndexOf('\\'))); par.mkdirs(); } FileWriter fw = new FileWriter(file); fw.write(string); fw.flush(); fw.close(); } catch (IOException e) { System.out.println("Failed to save file."); } }
public static void storeImage(BufferedImage image, String s) { String ext; File f; try { ext = s.substring(s.lastIndexOf(".") + 1); f = new File(s); f.mkdirs(); } catch (Exception e) { System.out.println(e); return; } if (!ext.equals("gif") && !ext.equals("jpg") && !ext.equals("png")) { System.out.println("Cannot write to file: Illegal extension " + ext); return; } try { ImageIO.write(image, ext, f); } catch (Exception e) { System.out.println(e); } }
protected void saveToFile() { if (this.fileChooser == null) { this.fileChooser = new JFileChooser(); this.fileChooser.setCurrentDirectory(new File(Configuration.getUserHomeDirectory())); } this.fileChooser.setDialogTitle("Choose Directory to Place Airspaces"); this.fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); this.fileChooser.setMultiSelectionEnabled(false); int status = this.fileChooser.showSaveDialog(null); if (status != JFileChooser.APPROVE_OPTION) return; final File dir = this.fileChooser.getSelectedFile(); if (dir == null) return; if (!dir.exists()) { //noinspection ResultOfMethodCallIgnored dir.mkdirs(); } final Iterable<AirspaceEntry> entries = this.getModel().getEntries(); Thread t = new Thread( new Runnable() { public void run() { try { java.text.DecimalFormat f = new java.text.DecimalFormat("####"); f.setMinimumIntegerDigits(4); int counter = 0; for (AirspaceEntry entry : entries) { Airspace a = entry.getAirspace(); AirspaceAttributes currentAttribs = a.getAttributes(); a.setAttributes(entry.getAttributes()); String xmlString = a.getRestorableState(); if (xmlString != null) { try { PrintWriter of = new PrintWriter( new File( dir, a.getClass().getName() + "-" + entry.getName() + "-" + f.format(counter++) + ".xml")); of.write(xmlString); of.flush(); of.close(); } catch (Exception e) { e.printStackTrace(); } } a.setAttributes(currentAttribs); } } finally { SwingUtilities.invokeLater( new Runnable() { public void run() { setEnabled(true); getApp().setCursor(null); getApp().getWwd().redraw(); } }); } } }); this.setEnabled(false); getApp().setCursor(new Cursor(Cursor.WAIT_CURSOR)); t.start(); }
public Ssys3() { store = new Storage(); tableSorter = new TableRowSorter<Storage>(store); jobs = new LinkedList<String>(); makeGUI(); frm.setSize(800, 600); frm.addWindowListener( new WindowListener() { public void windowActivated(WindowEvent evt) {} public void windowClosed(WindowEvent evt) { try { System.out.println("joining EDT's"); for (EDT edt : encryptDecryptThreads) { edt.weakStop(); try { edt.join(); System.out.println(" - joined"); } catch (InterruptedException e) { System.out.println(" - Not joined"); } } System.out.println("saving storage"); store.saveAll(tempLoc); } catch (IOException e) { e.printStackTrace(); System.err.println( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nFailed to save properly\n\n!!!!!!!!!!!!!!!!!!!!!!!!!"); System.exit(1); } clean(); System.exit(0); } public void windowClosing(WindowEvent evt) { windowClosed(evt); } public void windowDeactivated(WindowEvent evt) {} public void windowDeiconified(WindowEvent evt) {} public void windowIconified(WindowEvent evt) {} public void windowOpened(WindowEvent evt) {} }); ImageIcon ico = new ImageIcon(ICON_NAME); frm.setIconImage(ico.getImage()); frm.setLocationRelativeTo(null); frm.setVisible(true); // load config storeLocs = new ArrayList<File>(); String ossl = "openssl"; int numThreadTemp = 2; boolean priorityDecryptTemp = true; boolean allowExportTemp = false; boolean checkImportTemp = true; try { Scanner sca = new Scanner(CONF_FILE); while (sca.hasNextLine()) { String ln = sca.nextLine(); if (ln.startsWith(CONF_SSL)) { ossl = ln.substring(CONF_SSL.length()); } else if (ln.startsWith(CONF_THREAD)) { try { numThreadTemp = Integer.parseInt(ln.substring(CONF_THREAD.length())); } catch (Exception exc) { // do Nothing } } else if (ln.equals(CONF_STORE)) { while (sca.hasNextLine()) storeLocs.add(new File(sca.nextLine())); } else if (ln.startsWith(CONF_PRIORITY)) { try { priorityDecryptTemp = Boolean.parseBoolean(ln.substring(CONF_PRIORITY.length())); } catch (Exception exc) { // do Nothing } } else if (ln.startsWith(CONF_EXPORT)) { try { allowExportTemp = Boolean.parseBoolean(ln.substring(CONF_EXPORT.length())); } catch (Exception exc) { // do Nothing } } else if (ln.startsWith(CONF_CONFIRM)) { try { checkImportTemp = Boolean.parseBoolean(ln.substring(CONF_CONFIRM.length())); } catch (Exception exc) { // do Nothing } } } sca.close(); } catch (IOException e) { } String osslWorks = OpenSSLCommander.test(ossl); while (osslWorks == null) { ossl = JOptionPane.showInputDialog( frm, "Please input the command used to run open ssl\n We will run \"<command> version\" to confirm\n Previous command: " + ossl, "Find open ssl", JOptionPane.OK_CANCEL_OPTION); if (ossl == null) { System.err.println("Refused to provide openssl executable location"); System.exit(1); } osslWorks = OpenSSLCommander.test(ossl); if (osslWorks == null) JOptionPane.showMessageDialog( frm, "Command " + ossl + " unsuccessful", "Unsuccessful", JOptionPane.ERROR_MESSAGE); } if (storeLocs.size() < 1) JOptionPane.showMessageDialog( frm, "Please select an initial sotrage location\nIf one already exists, or there are more than one, please select it"); while (storeLocs.size() < 1) { JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (jfc.showOpenDialog(frm) != JFileChooser.APPROVE_OPTION) { System.err.println("Refused to provide an initial store folder"); System.exit(1); } File sel = jfc.getSelectedFile(); if (sel.isDirectory()) storeLocs.add(sel); } numThreads = numThreadTemp; priorityExport = priorityDecryptTemp; allowExport = allowExportTemp; checkImports = checkImportTemp; try { PrintWriter pw = new PrintWriter(CONF_FILE); pw.println(CONF_SSL + ossl); pw.println(CONF_THREAD + numThreads); pw.println(CONF_PRIORITY + priorityExport); pw.println(CONF_EXPORT + allowExport); pw.println(CONF_CONFIRM + checkImports); pw.println(CONF_STORE); for (File fi : storeLocs) { pw.println(fi.getAbsolutePath()); } pw.close(); } catch (IOException e) { System.err.println("Failed to save config"); } File chk = null; for (File fi : storeLocs) { File lib = new File(fi, LIBRARY_NAME); if (lib.exists()) { chk = lib; // break; } } char[] pass = null; if (chk == null) { JOptionPane.showMessageDialog( frm, "First time run\n Create your password", "Create Password", JOptionPane.INFORMATION_MESSAGE); char[] p1 = askPassword(); char[] p2 = askPassword(); boolean same = p1.length == p2.length; for (int i = 0; i < Math.min(p1.length, p2.length); i++) { if (p1[i] != p2[i]) same = false; } if (same) { JOptionPane.showMessageDialog( frm, "Password created", "Create Password", JOptionPane.INFORMATION_MESSAGE); pass = p1; } else { JOptionPane.showMessageDialog( frm, "Passwords dont match", "Create Password", JOptionPane.ERROR_MESSAGE); System.exit(1); } } else { pass = askPassword(); } sec = OpenSSLCommander.getCommander(chk, pass, ossl); if (sec == null) { System.err.println("Wrong Password"); System.exit(1); } store.useSecurity(sec); store.useStorage(storeLocs); tempLoc = new File("temp"); if (!tempLoc.exists()) tempLoc.mkdirs(); // load stores try { store.loadAll(tempLoc); store.fireTableDataChanged(); } catch (IOException e) { System.err.println("Storage loading failure"); System.exit(1); } needsSave = false; encryptDecryptThreads = new EDT[numThreads]; for (int i = 0; i < encryptDecryptThreads.length; i++) { encryptDecryptThreads[i] = new EDT(i); encryptDecryptThreads[i].start(); } updateStatus(); txaSearch.grabFocus(); }
/** Runs this thread. */ public void run() { CommonFileChooser file_chooser = new CommonFileChooser(); file_chooser.setDialogTitle("Choose a directory."); file_chooser.setMultiSelectionEnabled(false); file_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (file_chooser.showSaveDialog(pane) == JFileChooser.APPROVE_OPTION) { try { File directory = file_chooser.getSelectedFile(); directory.mkdirs(); // Outputs the variability XML file. String path = FileManager.unitePath(directory.getAbsolutePath(), "package.xml"); File file = new File(path); if (file.exists()) { String message = "Overwrite to " + file.getPath() + " ?"; if (0 != JOptionPane.showConfirmDialog( pane, message, "Confirmation", JOptionPane.YES_NO_OPTION)) { return; } } XmlVariabilityHolder holder = new XmlVariabilityHolder(); Variability[] records = getSelectedRecords(); for (int i = 0; i < records.length; i++) { XmlVariability variability = new XmlVariability(records[i]); holder.addVariability(variability); } holder.write(file); // Copies the report XML document files. Hashtable hash_xml = new Hashtable(); for (int i = 0; i < records.length; i++) { XmlMagRecord[] mag_records = records[i].getMagnitudeRecords(); for (int j = 0; j < mag_records.length; j++) hash_xml.put(mag_records[j].getImageXmlPath(), this); } Vector failed_list = new Vector(); Enumeration keys = hash_xml.keys(); while (keys.hasMoreElements()) { String xml_path = (String) keys.nextElement(); try { File src_file = desktop.getFileManager().newFile(xml_path); File dst_file = new File(FileManager.unitePath(directory.getAbsolutePath(), xml_path)); if (dst_file.exists() == false) FileManager.copy(src_file, dst_file); } catch (Exception exception) { failed_list.addElement(xml_path); } } if (failed_list.size() > 0) { String header = "Failed to copy the following XML files:"; MessagesDialog dialog = new MessagesDialog(header, failed_list); dialog.show(pane, "Error", JOptionPane.ERROR_MESSAGE); } // Copies the image files. failed_list = new Vector(); keys = hash_xml.keys(); while (keys.hasMoreElements()) { path = (String) keys.nextElement(); try { XmlInformation info = XmlReport.readInformation(desktop.getFileManager().newFile(path)); path = info.getImage().getContent(); File src_file = desktop.getFileManager().newFile(path); File dst_file = new File(FileManager.unitePath(directory.getAbsolutePath(), path)); if (dst_file.exists() == false) FileManager.copy(src_file, dst_file); } catch (Exception exception) { failed_list.addElement(path); } } if (failed_list.size() > 0) { String header = "Failed to copy the following image files:"; MessagesDialog dialog = new MessagesDialog(header, failed_list); dialog.show(pane, "Error", JOptionPane.ERROR_MESSAGE); } // Creates the sub catalog database. try { DiskFileSystem file_system = new DiskFileSystem( new File( directory.getAbsolutePath(), net.aerith.misao.pixy.Properties.getDatabaseDirectoryName())); CatalogDBManager new_manager = new GlobalDBManager(file_system).getCatalogDBManager(); Hashtable hash_stars = new Hashtable(); for (int i = 0; i < records.length; i++) { CatalogStar star = records[i].getStar(); CatalogDBReader reader = new CatalogDBReader(desktop.getDBManager().getCatalogDBManager()); StarList list = reader.read(star.getCoor(), 0.5); for (int j = 0; j < list.size(); j++) { CatalogStar s = (CatalogStar) list.elementAt(j); hash_stars.put(s.getOutputString(), s); } } keys = hash_stars.keys(); while (keys.hasMoreElements()) { String string = (String) keys.nextElement(); CatalogStar star = (CatalogStar) hash_stars.get(string); new_manager.addElement(star); } } catch (Exception exception) { String message = "Failed to create sub catalog database."; JOptionPane.showMessageDialog(pane, message, "Error", JOptionPane.ERROR_MESSAGE); } String message = "Completed."; JOptionPane.showMessageDialog(pane, message); } catch (IOException exception) { String message = "Failed."; JOptionPane.showMessageDialog(pane, message, "Error", JOptionPane.ERROR_MESSAGE); } } }