public void actionPerformed(ActionEvent actionEvent) {
String s = actionEvent.getActionCommand();
switch (s) {
case "New":
controller.createNewDocument();
break;
case "Open":
controller.openDocument();
break;
case "Save":
controller.saveDocument();
break;
case "Save As...":
controller.saveDocumentAs();
break;
case "Exit":
controller.exit();
break;
case "About":
showAbout();
}
}
package com.codegym.task.task32.task3209;
import com.codegym.task.task32.task3209.listeners.TabbedPaneChangeListener;
import com.codegym.task.task32.task3209.listeners.UndoListener;
import javax.swing.*;
import javax.swing.text.Document;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static com.codegym.task.task32.task3209.ExceptionHandler.log;
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 (ClassNotFoundException e) {
log(e);
} catch (InstantiationException e) {
log(e);
} catch (IllegalAccessException e) {
log(e);
} catch (UnsupportedLookAndFeelException e) {
log(e);
}
}
public Controller getController() {
return controller;
}
public void setController(Controller controller) {
this.controller = controller;
}
public UndoListener getUndoListener() {
return undoListener;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
String s = actionEvent.getActionCommand();
switch (s) {
case "New":
controller.createNewDocument();
break;
case "Open":
controller.openDocument();
break;
case "Save":
controller.saveDocument();
break;
case "Save As...":
controller.saveDocumentAs();
break;
case "Exit":
controller.exit();
break;
case "About":
showAbout();
}
}
public void init(){
initGui();
FrameListener frameListener = new FrameListener();
addWindowListener(frameListener);
setVisible(true);
}
public void exit(){
controller.exit();
}
public void initMenuBar(){
JMenuBar jMenuBar = new JMenuBar();
MenuHelper.initFileMenu(this, jMenuBar);
MenuHelper.initEditMenu(this, jMenuBar);
MenuHelper.initStyleMenu(this, jMenuBar);
MenuHelper.initAlignMenu(this, jMenuBar);
MenuHelper.initColorMenu(this, jMenuBar);
MenuHelper.initFontMenu(this, jMenuBar);
MenuHelper.initHelpMenu(this, jMenuBar);
getContentPane().add(jMenuBar, 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("Text", jScrollPane1);
tabbedPane.setPreferredSize(new Dimension(200,200));
tabbedPane.addChangeListener(new TabbedPaneChangeListener());
getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
public void initGui(){
initMenuBar();
initEditor();
pack();
}
public void selectedTabChanged(){
int selIndex = tabbedPane.getSelectedIndex();
if(selIndex == 0){
String s = plainTextPane.getText();
controller.setPlainText(s);
}
else if (selIndex == 1){
String st = controller.getPlainText();
plainTextPane.setText(st);
}
resetUndo();
}
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 void resetUndo(){
undoManager.discardAllEdits();
}
public boolean isHtmlTabSelected(){
return tabbedPane.getSelectedIndex() == 0;
}
public void selectHtmlTab(){
tabbedPane.setSelectedIndex(0);
resetUndo();
}
public void update(){
Document document = controller.getDocument();
htmlTextPane.setDocument(document);
}
public void showAbout(){
JOptionPane.showMessageDialog(tabbedPane,
"Eggs are not supposed to be green.",
"Insane custom dialog",
JOptionPane.INFORMATION_MESSAGE);
}
}