public void saveDocumentAs(){
view.selectHtmlTab();
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileFilter(new HTMLFileFilter());
int index = jFileChooser.showDialog(view, "Save File");
if(index == JFileChooser.APPROVE_OPTION){
currentFile = jFileChooser.getSelectedFile();
try(FileOutputStream fileOutputStream = new FileOutputStream(currentFile)) {
fileOutputStream.write(getDocument().getText(0, getDocument().getLength()).getBytes());
} catch (FileNotFoundException e) {
ExceptionHandler.log(e);
} catch (IOException e) {
ExceptionHandler.log(e);
} catch (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.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
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() throws HeadlessException {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
ExceptionHandler.log(new HeadlessException());
}
}
public UndoListener getUndoListener() {
return undoListener;
}
public Controller getController() {
return controller;
}
public void setController(Controller controller) {
this.controller = controller;
}
public void init(){
initGui();
FrameListener frameListener = new FrameListener(this);
addWindowListener(frameListener);
setVisible(true);
}
public void exit(){
controller.exit();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command){
case "Новый":
controller.createNewDocument();
break;
case "Открыть":
controller.openDocument();
break;
case "Сохранить":
controller.saveDocument();
break;
case "Сохранить как...":
controller.saveDocumentAs();
break;
case "Выход":
controller.exit();
break;
case "О программе":
this.showAbout();
break;
}
}
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);
Container container = getContentPane();
container.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("Текст",jScrollPane1);
tabbedPane.setPreferredSize(new Dimension(1000,800));
TabbedPaneChangeListener tabbedPaneChangeListener = new TabbedPaneChangeListener(this);
tabbedPane.addChangeListener(tabbedPaneChangeListener);
Container container = getContentPane();
container.add(tabbedPane, BorderLayout.CENTER);
}
public void initGui(){
initMenuBar();
initEditor();
pack();
}
public void selectedTabChanged(){
int index = tabbedPane.getSelectedIndex();
if(index == 0){
controller.setPlainText(plainTextPane.getText());
}else if(index ==1){
plainTextPane.setText(controller.getPlainText());
}
this.resetUndo();
}
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 boolean isHtmlTabSelected(){
return tabbedPane.getSelectedIndex()==0;
}
public void selectHtmlTab(){
tabbedPane.setSelectedIndex(0);
resetUndo();
}
public void update(){
HTMLDocument htmlDocument = controller.getDocument();
htmlTextPane.setDocument(htmlDocument);
}
public void showAbout(){
JOptionPane.showMessageDialog(getContentPane(),"Сообщение о программе","Титул",JOptionPane.INFORMATION_MESSAGE);
}
public void resetUndo(){
undoManager.discardAllEdits();
}
public boolean canUndo(){
return undoManager.canUndo();
}
public boolean canRedo(){
return undoManager.canRedo();
}
}