Advertisement
Shinmera

Base Editor

Aug 16th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. /**
  2.  * This file is a part of Examples
  3.  * (c) 2013 TymoonNET/NexT http://tymoon.eu (shinmera@tymoon.eu)
  4.  * Author: Nicolas Hafner <shinmera@tymoon.eu>
  5.  **/
  6.  
  7. import javax.swing.*;
  8. import java.awt.event.*;
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class BaseEditor extends JFrame implements ActionListener{
  13.    
  14.     private JTextArea text;
  15.     private JMenuItem mi_save, mi_saveAs, mi_load, mi_quit;
  16.     private File currentDocument;
  17.  
  18.     public static void main(String[] args){new BaseEditor();}
  19.  
  20.     public BaseEditor(){
  21.         super();
  22.         setTitle("Basic Editor");
  23.         setSize(300,500);
  24.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.  
  26.         setupContents();
  27.  
  28.         pack();
  29.         setVisible(true);
  30.     }
  31.  
  32.     private void setupContents(){
  33.         text = new JTextArea();
  34.         JScrollPane scroll = new JScrollPane(text);
  35.         scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  36.         this.add(scroll);
  37.        
  38.         setupMenu();
  39.     }
  40.  
  41.     private void setupMenu(){
  42.         JMenuBar bar = new JMenuBar();
  43.        
  44.         JMenu m_file = new JMenu("File");
  45.         m_file.setMnemonic(KeyEvent.VK_F);
  46.        
  47.         mi_save = new JMenuItem("Save");
  48.         mi_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
  49.         mi_save.addActionListener(this);
  50.         mi_saveAs = new JMenuItem("Save As..");
  51.         mi_saveAs.addActionListener(this);
  52.         mi_load = new JMenuItem("Load..");
  53.         mi_load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
  54.         mi_load.addActionListener(this);
  55.         mi_quit = new JMenuItem("Quit");
  56.         mi_quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
  57.         mi_quit.addActionListener(this);
  58.  
  59.         m_file.add(mi_save);
  60.         m_file.add(mi_saveAs);
  61.         m_file.add(mi_load);
  62.         m_file.addSeparator();
  63.         m_file.add(mi_quit);
  64.        
  65.         bar.add(m_file);
  66.  
  67.         setJMenuBar(bar);
  68.     }
  69.  
  70.     public void actionPerformed(ActionEvent ev){
  71.         if(ev.getSource() == mi_save)        actionSave();
  72.         else if(ev.getSource() == mi_saveAs) actionSaveAs();
  73.         else if(ev.getSource() == mi_load)   actionLoad();
  74.         else if(ev.getSource() == mi_quit)   actionQuit();
  75.     }
  76.  
  77.     private void actionSave(){
  78.         if(currentDocument == null){
  79.             actionSaveAs();
  80.         }else{
  81.             if(saveStringToFile(text.getText(), currentDocument)){
  82.                 setTitle("Base Editor - "+currentDocument);
  83.             }else{
  84.                 JOptionPane.showMessageDialog(null, "Failed to save document to "+currentDocument, "Failed to Save!", JOptionPane.ERROR_MESSAGE);
  85.             }
  86.         }
  87.     }
  88.  
  89.     private void actionSaveAs(){
  90.         JFileChooser chooser = new JFileChooser();
  91.         if(chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
  92.             currentDocument = chooser.getSelectedFile();
  93.             actionSave();
  94.         }
  95.     }
  96.  
  97.     private void actionLoad(){
  98.         JFileChooser chooser = new JFileChooser();
  99.         if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
  100.             currentDocument = chooser.getSelectedFile();
  101.             text.setText(loadFileToString(currentDocument));
  102.             setTitle("Base Editor - "+currentDocument);
  103.         }
  104.     }
  105.  
  106.     private void actionQuit(){
  107.         if(JOptionPane.showConfirmDialog(this, "Really quit?", "Quit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
  108.             System.exit(0);
  109.     }
  110.  
  111.     //From NexT.util.Toolkit
  112.     public static boolean saveStringToFile(String s,File f){
  113.         try{
  114.             f.createNewFile();
  115.             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f),"UTF-8"));
  116.             pw.print(s);
  117.             pw.flush();
  118.             pw.close();
  119.         }catch(IOException ex){
  120.             return false;
  121.         }
  122.         return true;
  123.     }
  124.  
  125.     public static String loadFileToString(File f){
  126.         StringBuilder s = new StringBuilder();
  127.         try{
  128.             BufferedReader in = new BufferedReader(new FileReader(f));
  129.             String read = "";
  130.             while ((read = in.readLine()) != null)s.append(read+"\n");
  131.             in.close();
  132.         }catch(IOException e){
  133.             return "";
  134.         }
  135.         return s.toString();
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement