public void saveAs() { Vector<RopeFileFilter> filters = new Vector<RopeFileFilter>(); if (fileExt.equals("m") || fileExt.equals("mac")) { filters.add(new RopeFileFilter(new String[] {".m", ".mac"}, "Macro files (*.m *.mac)")); filters.add( new RopeFileFilter( new String[] {".a", ".asm", ".aut", ".s"}, "Assembly files (*.a *.asm *.aut *.s)")); } else { filters.add( new RopeFileFilter( new String[] {".a", ".asm", ".aut", ".s"}, "Assembly files (*.a *.asm *.aut *.s)")); filters.add(new RopeFileFilter(new String[] {".m", ".mac"}, "Macro files (*.m *.mac)")); } filters.add(new RopeFileFilter(new String[] {".txt"}, "Text files (*.txt)")); RopeFileChooser chooser = new RopeFileChooser(selectedPath, null, filters); chooser.setDialogTitle("Save Source File"); String fileName = String.format("%s.%s", baseName, fileExt); chooser.setSelectedFile(new File(selectedPath, fileName)); JTextField field = chooser.getTextField(); field.setSelectionStart(0); field.setSelectionEnd(baseName.length()); File file = chooser.save(ROPE.mainFrame); if (file != null) { selectedPath = file.getParent(); BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(file)); writer.write(sourceArea.getText()); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } }
public boolean loadSourceFile(File file) { boolean result = false; selectedPath = file.getParent(); BufferedReader sourceFile = null; String directoryPath = file.getParent(); String sourceName = file.getName(); int idx = sourceName.lastIndexOf("."); fileExt = idx == -1 ? "" : sourceName.substring(idx + 1); baseName = idx == -1 ? sourceName.substring(0) : sourceName.substring(0, idx); String basePath = directoryPath + File.separator + baseName; DataOptions.directoryPath = directoryPath; sourcePath = file.getPath(); AssemblerOptions.sourcePath = sourcePath; AssemblerOptions.listingPath = basePath + ".lst"; AssemblerOptions.objectPath = basePath + ".cd"; String var = System.getenv("ROPE_MACROS_DIR"); if (var != null && !var.isEmpty()) { File dir = new File(var); if (dir.exists() && dir.isDirectory()) { AssemblerOptions.macroPath = var; } else { AssemblerOptions.macroPath = directoryPath; } } else { AssemblerOptions.macroPath = directoryPath; } DataOptions.inputPath = AssemblerOptions.objectPath; DataOptions.outputPath = basePath + ".out"; DataOptions.readerPath = null; DataOptions.punchPath = basePath + ".pch"; DataOptions.tape1Path = basePath + ".mt1"; DataOptions.tape2Path = basePath + ".mt2"; DataOptions.tape3Path = basePath + ".mt3"; DataOptions.tape4Path = basePath + ".mt4"; DataOptions.tape5Path = basePath + ".mt5"; DataOptions.tape6Path = basePath + ".mt6"; this.setTitle("EDIT: " + sourceName); fileText.setText(sourcePath); if (dialog == null) { dialog = new AssemblerDialog(mainFrame, "Assembler options"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dialogSize = dialog.getSize(); dialog.setLocation( (screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2); } dialog.initialize(); AssemblerOptions.command = dialog.buildCommand(); sourceArea.setText(null); try { sourceFile = new BufferedReader(new FileReader(file)); String line; while ((line = sourceFile.readLine()) != null) { sourceArea.append(line + "\n"); } sourceArea.setCaretPosition(0); optionsButton.setEnabled(true); assembleButton.setEnabled(true); saveButton.setEnabled(true); setSourceChanged(false); undoMgr.discardAllEdits(); result = true; } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (sourceFile != null) { sourceFile.close(); } } catch (IOException ignore) { } } return result; }