tk.elevenk.restfulrobot.gui.EditorWindow.java Source code

Java tutorial

Introduction

Here is the source code for tk.elevenk.restfulrobot.gui.EditorWindow.java

Source

/* This source code file is part of RESTfulRobot
Copyright (C) 2014 John Krause (Eleven-K Software)
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package tk.elevenk.restfulrobot.gui;

import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class EditorWindow extends JPanel {

    private static final long serialVersionUID = 2L;
    private static Logger logger = LogManager.getLogger(EditorWindow.class.getName());
    private JEditorPane editor;
    private JScrollPane editorScrollPane;
    private boolean fileOpen;
    private File currentFile;

    public EditorWindow() {
        super();
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        fileOpen = false;
        editor = new JEditorPane();
        editor.setText("");
        editorScrollPane = new JScrollPane(editor);
        editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.setMaximumSize(new Dimension(800, 600));
        this.setMinimumSize(new Dimension(320, 240));
        this.add(editorScrollPane);
    }

    public void openFile(File file) {
        if (fileOpen) {
            saveFile();
        }
        currentFile = file;
        try {
            editor.read(new FileInputStream(file), new String("Javascript"));
        } catch (FileNotFoundException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        } catch (IOException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
        fileOpen = true;
    }

    public void saveFile() {
        try {
            editor.write(new FileWriter(currentFile));
        } catch (IOException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
    }
}