Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This file is a part of Examples
- * (c) 2013 TymoonNET/NexT http://tymoon.eu (shinmera@tymoon.eu)
- * Author: Nicolas Hafner <shinmera@tymoon.eu>
- **/
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
- public class BaseEditor extends JFrame implements ActionListener{
- private JTextArea text;
- private JMenuItem mi_save, mi_saveAs, mi_load, mi_quit;
- private File currentDocument;
- public static void main(String[] args){new BaseEditor();}
- public BaseEditor(){
- super();
- setTitle("Basic Editor");
- setSize(300,500);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setupContents();
- pack();
- setVisible(true);
- }
- private void setupContents(){
- text = new JTextArea();
- JScrollPane scroll = new JScrollPane(text);
- scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- this.add(scroll);
- setupMenu();
- }
- private void setupMenu(){
- JMenuBar bar = new JMenuBar();
- JMenu m_file = new JMenu("File");
- m_file.setMnemonic(KeyEvent.VK_F);
- mi_save = new JMenuItem("Save");
- mi_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
- mi_save.addActionListener(this);
- mi_saveAs = new JMenuItem("Save As..");
- mi_saveAs.addActionListener(this);
- mi_load = new JMenuItem("Load..");
- mi_load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
- mi_load.addActionListener(this);
- mi_quit = new JMenuItem("Quit");
- mi_quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
- mi_quit.addActionListener(this);
- m_file.add(mi_save);
- m_file.add(mi_saveAs);
- m_file.add(mi_load);
- m_file.addSeparator();
- m_file.add(mi_quit);
- bar.add(m_file);
- setJMenuBar(bar);
- }
- public void actionPerformed(ActionEvent ev){
- if(ev.getSource() == mi_save) actionSave();
- else if(ev.getSource() == mi_saveAs) actionSaveAs();
- else if(ev.getSource() == mi_load) actionLoad();
- else if(ev.getSource() == mi_quit) actionQuit();
- }
- private void actionSave(){
- if(currentDocument == null){
- actionSaveAs();
- }else{
- if(saveStringToFile(text.getText(), currentDocument)){
- setTitle("Base Editor - "+currentDocument);
- }else{
- JOptionPane.showMessageDialog(null, "Failed to save document to "+currentDocument, "Failed to Save!", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- private void actionSaveAs(){
- JFileChooser chooser = new JFileChooser();
- if(chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
- currentDocument = chooser.getSelectedFile();
- actionSave();
- }
- }
- private void actionLoad(){
- JFileChooser chooser = new JFileChooser();
- if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
- currentDocument = chooser.getSelectedFile();
- text.setText(loadFileToString(currentDocument));
- setTitle("Base Editor - "+currentDocument);
- }
- }
- private void actionQuit(){
- if(JOptionPane.showConfirmDialog(this, "Really quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
- System.exit(0);
- }
- //From NexT.util.Toolkit
- public static boolean saveStringToFile(String s,File f){
- try{
- f.createNewFile();
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f),"UTF-8"));
- pw.print(s);
- pw.flush();
- pw.close();
- }catch(IOException ex){
- return false;
- }
- return true;
- }
- public static String loadFileToString(File f){
- StringBuilder s = new StringBuilder();
- try{
- BufferedReader in = new BufferedReader(new FileReader(f));
- String read = "";
- while ((read = in.readLine()) != null)s.append(read+"\n");
- in.close();
- }catch(IOException e){
- return "";
- }
- return s.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement