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 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.text.BadLocationException;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
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 (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
ExceptionHandler.log(e);
}
}
public UndoListener getUndoListener() {
return undoListener;
}
public Controller getController() {
return controller;
}
public void setController(Controller controller) {
this.controller = controller;
}
@Override
public void actionPerformed(ActionEvent e) {
// String command = e.getActionCommand();
switch (e.getActionCommand()){
case "Новый":
controller.createNewDocument();
break;
case "Открыть":
controller.openDocument();
break;
case "Сохранить":
controller.saveDocument();
break;
case "Сохранить как...":
try {
controller.saveDocumentAs();
} catch (IOException | BadLocationException e1) {
ExceptionHandler.log(e1);
}
break;
case "Выход":
controller.exit();
break;
case "О программе":
showAbout();
}
}
public void init(){
initGui();
FrameListener frameListener = new FrameListener(this);
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 jScrollPane1 = new JScrollPane(htmlTextPane);
tabbedPane.addTab("HTML", jScrollPane1);
JScrollPane jScrollPane2 = new JScrollPane(plainTextPane);
tabbedPane.addTab("Текст", jScrollPane2);
tabbedPane.setPreferredSize(tabbedPane.getPreferredSize());
TabbedPaneChangeListener tabbedPaneChangeListener = new TabbedPaneChangeListener(this);
tabbedPane.addChangeListener(tabbedPaneChangeListener);
this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
public void initGui(){
initMenuBar();
initEditor();
pack();
}
public void selectedTabChanged() {
if(tabbedPane.getSelectedIndex() == 0){
controller.setPlainText(plainTextPane.getText());
}
else {
plainTextPane.setText(controller.getPlainText());
}
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(){
htmlTextPane.setDocument(controller.getDocument());
}
public void showAbout(){
JOptionPane.showMessageDialog(getContentPane(), "Eggs are not supposed to be green.", "myText", JOptionPane.INFORMATION_MESSAGE);
}
}