public void setPlainText(String text){
try {
resetDocument();
StringReader stringReader = new StringReader(text);
new HTMLEditorKit().read(stringReader, document, 0);
} catch (Exception e) {
ExceptionHandler.log(e);
}
}
package com.javarush.task.task32.task3209;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
public class Controller {
private View view;
private HTMLDocument document;
private File currentFile;
public HTMLDocument getDocument() {
return document;
}
public Controller(View view) {
this.view = view;
}
public String getPlainText(){
StringWriter stringWriter = new StringWriter();
try {
if (document != null) new HTMLEditorKit().write(stringWriter, document, 0, document.getLength());
return stringWriter.toString();
} catch (Exception e) {
ExceptionHandler.log(e);
}
return stringWriter.toString();
}
public void setPlainText(String text){
try {
resetDocument();
StringReader stringReader = new StringReader(text);
new HTMLEditorKit().read(stringReader, document, 0);
} catch (Exception e) {
ExceptionHandler.log(e);
}
}
public void resetDocument(){
if (document != null) document.removeUndoableEditListener(view.getUndoListener());
HTMLDocument document = (HTMLDocument) new HTMLEditorKit().createDefaultDocument();
document.addUndoableEditListener(view.getUndoListener());
view.update();
}
public void init(){
}
public void exit(){
System.exit(0);
}
public static void main (String[] args){
View view = new View();
Controller controller = new Controller(view);
view.setController(controller);
view.init();
controller.init();
}
}