public void saveDocumentAs() {
view.selectHtmlTab();
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileFilter(new HTMLFileFilter());
JOptionPane.showMessageDialog(view.getContentPane(), "Make your choice...", "Save File", JOptionPane.INFORMATION_MESSAGE);
// Если пользователь подтвердит выбор файла
if (jFileChooser.showSaveDialog(view) == JFileChooser.APPROVE_OPTION) {
currentFile = jFileChooser.getSelectedFile();
view.setTitle(currentFile.getName());
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(currentFile);
new HTMLEditorKit().write(fileWriter, document, 0, document.getLength());
fileWriter.close();
} catch (IOException | BadLocationException e) {
ExceptionHandler.log(e);
}
}
}
package com.javarush.task.task32.task3209;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.*;
public class Controller {
private View view;
private HTMLDocument document;
private File currentFile;
public Controller(View view) {
this.view = view;
}
public HTMLDocument getDocument() {
return document;
}
public void init(){
createNewDocument();
}
public void exit(){
System.exit(0);
}
public void resetDocument(){
if (document != null){
document.removeUndoableEditListener(view.getUndoListener());
}
document = (HTMLDocument) new HTMLEditorKit().createDefaultDocument();
document.addUndoableEditListener(view.getUndoListener());
view.update();
}
public void setPlainText(String text) {
resetDocument();
StringReader stringReader = new StringReader(text);
try {
new HTMLEditorKit().read(stringReader, document, 0);
} catch (Exception e) {
ExceptionHandler.log(e);
}
}
public String getPlainText(){
StringWriter stringWriter = new StringWriter();
try {
new HTMLEditorKit().write(stringWriter, document, 0, document.getLength());
} catch (IOException | BadLocationException e) {
ExceptionHandler.log(e);
}
return stringWriter.toString();
}
public static void main(String args[]){
View view = new View();
Controller controller = new Controller(view);
view.setController(controller);
view.init();
controller.init();
}
public void createNewDocument() {
view.selectHtmlTab();
resetDocument();
view.setTitle("HTML редактор");
view.resetUndo();
currentFile = null;
}
public void openDocument() {
}
public void saveDocument() {
}
public void saveDocumentAs() throws IOException, BadLocationException {
view.selectHtmlTab();
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileFilter(new HTMLFileFilter());
JOptionPane.showMessageDialog(view.getContentPane(), "Make your choice...", "Save File", JOptionPane.INFORMATION_MESSAGE);
// Если пользователь подтвердит выбор файла
if (jFileChooser.showSaveDialog(view) == JFileChooser.APPROVE_OPTION) {
currentFile = jFileChooser.getSelectedFile();
view.setTitle(currentFile.getName());
FileWriter fileWriter = null;
fileWriter = new FileWriter(currentFile);
new HTMLEditorKit().write(fileWriter, document, 0, document.getLength());
fileWriter.close();
}
}
}