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 com.javarush.task.task32.task3209.listeners.FrameListener;
import com.javarush.task.task32.task3209.listeners.TabbedPaneChangeListener;
import com.javarush.task.task32.task3209.listeners.UndoListener;
import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class View extends JFrame implements ActionListener {
private Controller controller;
private JTabbedPane tabbedPane = new JTabbedPane();
private JTextPane htmlTextPane = new JTextPane();
private JEditorPane plainTextPane = new JEditorPane();
private UndoManager undoManager = new UndoManager();
private UndoListener undoListener = new UndoListener(undoManager);
public UndoListener getUndoListener() {
return undoListener;
}
public View(){
try {
UIManager.setLookAndFeel((UIManager.getSystemLookAndFeelClassName()));
} catch (ClassNotFoundException e) {
ExceptionHandler.log(e);
} catch (InstantiationException e) {
ExceptionHandler.log(e);
} catch (IllegalAccessException e) {
ExceptionHandler.log(e);
} catch (UnsupportedLookAndFeelException e) {
ExceptionHandler.log(e);
}
}
public void showAbout(){
JOptionPane.showMessageDialog(getContentPane(), "Version 1.0", "About", JOptionPane.INFORMATION_MESSAGE);
}
public void update(){
htmlTextPane.setDocument(controller.getDocument());
}
public void selectHtmlTab(){
tabbedPane.setSelectedIndex(0);
resetUndo();
}
public boolean isHtmlTabSelected(){
return tabbedPane.getSelectedIndex() == 0;
}
public void undo(){
try{
undoManager.undo();
} catch (Exception e){
ExceptionHandler.log(e);
}
}
public void redo(){
try{
undoManager.redo();
} catch (Exception e){
ExceptionHandler.log(e);
}
}
public Controller getController() {
return controller;
}
public void setController(Controller controller) {
this.controller = controller;
}
public void selectedTabChanged(){
}
public void init(){
initGui();
addWindowListener(new FrameListener(this));
setVisible(true);
}
public void exit(){
controller.exit();
}
public void initMenuBar(){
JMenuBar menuBar = new JMenuBar();
MenuHelper.initFileMenu(this, menuBar);
MenuHelper.initEditMenu(this, menuBar);
MenuHelper.initStyleMenu(this, menuBar);
MenuHelper.initAlignMenu(this, menuBar);
MenuHelper.initColorMenu(this, menuBar);
MenuHelper.initFontMenu(this, menuBar);
MenuHelper.initHelpMenu(this, menuBar);
getContentPane().add(menuBar, BorderLayout.NORTH);
}
public void initEditor(){
htmlTextPane.setContentType("text/html");
JScrollPane jScrollPane = new JScrollPane(htmlTextPane);
tabbedPane.addTab("HTML", jScrollPane);
JScrollPane jScrollPane1 = new JScrollPane(plainTextPane);
tabbedPane.addTab("Текст", jScrollPane1);
tabbedPane.setPreferredSize(new Dimension(400, 400));
tabbedPane.addChangeListener(new TabbedPaneChangeListener(this));
getContentPane().add(tabbedPane,BorderLayout.CENTER);
}
public void initGui(){
initMenuBar();
initEditor();
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
}
public boolean canUndo(){
return undoManager.canUndo();
}
public boolean canRedo(){
return undoManager.canRedo();
}
public void resetUndo(){
undoManager.discardAllEdits();
}
}