String currFileName = null; // path plus filename. null means new / untitled boolean dirty = false; // true means modified text
textArea1.setText(new String(data, 0, chars_read));
// Cache the currently opened filename for use at save time... this.currFileName = fileName; // ...and mark the edit session as being clean. this.dirty = false;
/** * Save current file; handle not yet having a filename; report to statusBar. * @return false if save did not occur. */ boolean saveFile() { // Handle the case where we don't have a file name yet. if (currFileName == null) { return saveAsFile(); } try { // Open a file of the current name. File file = new File (currFileName); // Create an output writer that will write to that file. // FileWriter handles international characters encoding conversions. FileWriter out = new FileWriter(file); String text = textArea1.getText(); out.write(text); out.close(); this.dirty = false; return true; } catch (IOException e) { statusBar.setText("Error saving "+currFileName); } return false; }
/** * Save current file, asking user for new destination name. * Report to statuBar. * @return false means user cancelled the SaveAs */ boolean saveAsFile() { // Filer makes use of a java.awt.FileDialog, so its // mode property uses the same values as those of FileDialog. filer1.setMode(FileDialog.SAVE); // Use the SAVE version of the dialog. // Make the dialog visible as a modal (default) dialog box. filer1.show(); // Upon return, getFile() will be null if user cancelled the dialog. if (filer1.getFile() != null) { // Non-null file property after return implies user // selected a filename to save to. // Set the current file name to the user's selection, // then do a regular saveFile. currFileName = filer1.getDirectory()+filer1.getFile(); return saveFile(); } else { return false; } }
void menuItem3_actionPerformed(ActionEvent e) { saveFile(); }
void menuItem4_actionPerformed(ActionEvent e) { saveAsFile(); }