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(); } } } }
@Override public void approveSelection() { File file = getSelectedFile(); if (file != null) { FileFilter filter = getFileFilter(); if (filter != null && filter instanceof FileNameExtensionFilter) { String[] extensions = ((FileNameExtensionFilter) filter).getExtensions(); boolean goodExt = false; for (String ext : extensions) { if (file.getName().toLowerCase().endsWith("." + ext.toLowerCase())) { goodExt = true; break; } } if (!goodExt) { file = new File(file.getParent(), file.getName() + "." + extensions[0]); } } if (file.exists()) { String okStr = Messages.FILE_CHOOSER_FILE_EXISTS_OK_OPTION; String cancelStr = Messages.FILE_CHOOSER_FILE_EXISTS_CANCEL_OPTION; int ret = JOptionPane.showOptionDialog( this, Resources.format(Messages.FILE_CHOOSER_FILE_EXISTS_MESSAGE, file.getName()), Messages.FILE_CHOOSER_FILE_EXISTS_TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new Object[] {okStr, cancelStr}, okStr); if (ret != JOptionPane.OK_OPTION) { return; } } setSelectedFile(file); } super.approveSelection(); }
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; }
private void init(EditorPatternButton imgBtn, JLabel msgApplied) { _imgBtn = imgBtn; JLabel lblPath = new JLabel(_I("lblPath")); JLabel lblFilename = new JLabel(_I("lblFilename")); String filename = _imgBtn.getFilename(); File f = new File(filename); String fullpath = f.getParent(); filename = getFilenameWithoutExt(f); _oldFilename = filename; BufferedImage thumb = _imgBtn.createThumbnailImage(THUMB_MAX_HEIGHT); Border border = LineBorder.createGrayLineBorder(); JLabel lblThumb = new JLabel(new ImageIcon(thumb)); lblThumb.setBorder(border); _txtPath = new JTextField(fullpath, TXT_FILENAME_LENGTH); _txtPath.setEditable(false); _txtPath.setEnabled(false); String[] candidates = new String[] {filename}; // <editor-fold defaultstate="collapsed" desc="OCR --- not used"> /* String ocrText = getFilenameFromImage(thumb); if(ocrText.length()>0 && !ocrText.equals(filename)) candidates = new String[] {filename, ocrText}; */ // </editor-fold> _txtFilename = new AutoCompleteCombo(candidates); _txtFileExt = new JTextField(getFileExt(f), TXT_FILE_EXT_LENGTH); _txtFileExt.setEditable(false); _txtFileExt.setEnabled(false); GridBagConstraints c = new GridBagConstraints(); c.gridy = 0; c.insets = new Insets(100, 0, 0, 0); this.add(new JLabel(""), c); c = new GridBagConstraints(); c.fill = 0; c.gridwidth = 3; c.gridy = 1; c.insets = new Insets(0, 10, 20, 10); this.add(lblThumb, c); c = new GridBagConstraints(); c.fill = 1; c.gridy = 2; this.add(lblPath, c); c.gridx = 1; c.gridwidth = 2; this.add(_txtPath, c); c = new GridBagConstraints(); c.gridy = 3; c.fill = 0; this.add(lblFilename, c); this.add(_txtFilename, c); this.add(_txtFileExt, c); c = new GridBagConstraints(); c.gridy = 4; c.gridx = 1; c.insets = new Insets(200, 0, 0, 0); this.add(msgApplied, c); }