private boolean checkForSave() { // build warning message String message; if (file == null) { message = "File has been modified. Save changes?"; } else { message = "File \"" + file.getName() + "\" has been modified. Save changes?"; } // show confirm dialog int r = JOptionPane.showConfirmDialog( this, new JLabel(message), "Warning!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (r == JOptionPane.YES_OPTION) { // Save File if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // write the file physWriteTextFile(fileChooser.getSelectedFile(), textView.getText()); } else { // user cancelled save after all return false; } } return r != JOptionPane.CANCEL_OPTION; }
public void saveFileAs() { // Force user to enter new file name if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } else { // user cancelled save after all return; } // file selected, so write it. physWriteTextFile(file, textView.getText()); // update status statusView.setText(" Saved file \"" + file.getName() + "\"."); // reset dirty bit dirty = false; }
public void openFile() { if (!dirty || checkForSave()) { if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } else { // user cancelled open after all return; } // load file into text view textView.setText(physReadTextFile(file)); // update status statusView.setText(" Loaded file \"" + file.getName() + "\"."); // reset dirty bit dirty = false; } }