public void resetDocument() {
UndoListener listener = view.getUndoListener();
if (document != null) {
document.removeUndoableEditListener(listener);
}
document = (HTMLDocument) new HTMLEditorKit().createDefaultDocument();
document.addUndoableEditListener(listener);
view.update();
}
}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 View() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
ExceptionHandler.log(e);
}
}
public boolean isHtmlTabSelected() {
if (tabbedPane.getSelectedIndex() == 0) {
return true;
} else
return false;
}
public void selectHtmlTab() {
tabbedPane.setSelectedIndex(0);
this.resetUndo();
}
public void update() {
htmlTextPane.setDocument(controller.getDocument());
}
public void showAbout() {
JOptionPane.showMessageDialog(this, "About", "About", JOptionPane.INFORMATION_MESSAGE);
}
public Controller getController() {
return controller;
}
public void setController(Controller controller) {
this.controller = controller;
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void init() {
initGui();
addWindowListener(new FrameListener(this));
setVisible(true);
}
public void exit() {
controller.exit();
}
public void initMenuBar() {
JMenuBar bar = new JMenuBar();
MenuHelper.initFileMenu(this, bar);
MenuHelper.initEditMenu(this, bar);
MenuHelper.initStyleMenu(this, bar);
MenuHelper.initAlignMenu(this, bar);
MenuHelper.initColorMenu(this, bar);
MenuHelper.initFontMenu(this, bar);
MenuHelper.initHelpMenu(this, bar);
this.getContentPane().add(bar, BorderLayout.NORTH);
}
public void initEditor() {
htmlTextPane.setContentType("text/html");
JScrollPane jScrollPane = new JScrollPane(htmlTextPane);
tabbedPane.addTab("HTML", jScrollPane);
JScrollPane tScrollPane = new JScrollPane(plainTextPane);
tabbedPane.addTab("Текст", tScrollPane);
tabbedPane.setPreferredSize(new Dimension(800, 600));
tabbedPane.addChangeListener(new TabbedPaneChangeListener(this));
this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
public void initGui() {
initMenuBar();
initEditor();
pack();
}
public void selectedTabChanged() {
}
public boolean canUndo() {
return undoManager.canUndo();
}
public boolean canRedo() {
return undoManager.canRedo();
}
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 UndoListener getUndoListener() {
return undoListener;
}
public void resetUndo() {
undoManager.discardAllEdits();
}
}