Advertisement
Black_Spider

FTE Gui.java

Apr 10th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. public class Gui extends JFrame{
  11.     private File file;
  12.     private JLabel label;
  13.     private JButton openFile;
  14.     private JButton save;
  15.     private JTextArea textArea;
  16.    
  17.     public Gui(){
  18.         super("Frost Text Editor");
  19.         FlowLayout layout = new FlowLayout();
  20.         layout.setAlignment(FlowLayout.LEFT);
  21.         setLayout(layout);
  22.        
  23.        
  24.         openFile = new JButton("Open\\Create");
  25.         openFile.setToolTipText("Open or Create the file you want to edit.");
  26.         add(openFile);
  27.         openFile.addActionListener(
  28.                 new ActionListener(){
  29.                     public void actionPerformed(ActionEvent event){
  30.                         JFileChooser chooser = new JFileChooser();
  31.                         int returnVal = chooser.showOpenDialog(getContentPane());
  32.                         file = chooser.getSelectedFile();
  33.                         if(returnVal == JFileChooser.APPROVE_OPTION) {
  34.                             if(file.exists()){
  35.                            label.setText("Opened file: " +
  36.                                 file.getName());
  37.                            label.setToolTipText(file.getAbsolutePath());
  38.                            textArea.setText(readFile(file));
  39.                             } else{
  40.                                 JOptionPane.showMessageDialog(null, "You tried to open a non exist file.");
  41.                                 if(JOptionPane.showConfirmDialog(null, "Would you want to create that file?") == 0){
  42.                                     if(createFile(file)){
  43.                                         autoComplete(file.getName());
  44.                                         label.setText("Opened file: " +
  45.                                                 file.getName());
  46.                                         label.setToolTipText(file.getAbsolutePath());
  47.                                     }
  48.                                 }
  49.                             }
  50.                         }
  51.                      
  52.                     }
  53.                 }
  54.                
  55.         );
  56.        
  57.         save = new JButton("Save");
  58.         add(save);
  59.         save.addActionListener(
  60.         new ActionListener(){
  61.             public void actionPerformed(ActionEvent event){
  62.                 if(file != null){
  63.                     try {
  64.                         Formatter f = new Formatter(file.getAbsolutePath());
  65.                         f.format("%s", textArea.getText());
  66.                         f.close();
  67.                         label.setText("The file: " + file.getName() + " has been saved succesfly");
  68.                     } catch (FileNotFoundException e) {
  69.  
  70.                         e.printStackTrace();
  71.                     }
  72.                 } else {
  73.                     JOptionPane.showMessageDialog(null, "You must open\\create a file first");
  74.                 }
  75.                
  76.             }
  77.         }
  78.                
  79.         );
  80.        
  81.         label = new JLabel();
  82.         add(label);
  83.        
  84.        
  85.         textArea = new JTextArea("I think Frost Text Editor is the best editor!", 25, 50);
  86.         textArea.setFont(new Font("Arial", Font.PLAIN, 14));
  87.         JScrollPane scrollPane = new JScrollPane(textArea);
  88.         textArea.setEditable(true);
  89.         add(scrollPane);
  90.        
  91.        
  92.        
  93.     }
  94.    
  95.     private boolean createFile(File f){
  96.         if(!f.exists()){
  97.             try{
  98.                 Formatter x = new Formatter(f.getAbsolutePath());
  99.                 JOptionPane.showMessageDialog(null, "File created succesfly");
  100.                 return true;
  101.                
  102.             }
  103.             catch(Exception e){
  104.                 JOptionPane.showMessageDialog(null, "Couldn't create the file.");
  105.                 return false;
  106.             }
  107.         }
  108.         return false;
  109.     }
  110.    
  111.     private String readFile(File f){
  112.         try{
  113.             String fileContains = "";
  114.             Scanner in = new Scanner(f);
  115.             while(in.hasNextLine()){
  116.                 String line = in.nextLine();
  117.                 fileContains += line + "\n";
  118.             }
  119.             in.close();
  120.             return fileContains;
  121.         }
  122.         catch(Exception e){
  123.             return "";
  124.         }
  125.     }
  126.    
  127.     private void autoComplete(String name){
  128.         int num = name.lastIndexOf('.');
  129.         String start = name.substring(0, num);
  130.         String end = name.substring(num + 1, name.length());
  131.         switch(end){
  132.             case "html":
  133.                 String text = "<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<title></title>\n\t</head>\n\t<body>\n\t\t\n\t</body>\n</html>";
  134.                 textArea.setText(text);
  135.                 break;
  136.             case "java":
  137.                 String text1 = "public class " + start + " {\n";
  138.                 text1 += "  public static void main(String[] args) {\n\n\n\n\t}\n}";
  139.                 textArea.setText(text1);
  140.                 break;
  141.             case "php":
  142.                 textArea.setText("<?php\n\n\n\n?>");
  143.                 break;
  144.             case "cs":
  145.                 String text2 = "public class Test\n{\n\tpublic static void Main()\n\t{\n\t\t// your code goes here\n\t}\n}";
  146.                 textArea.setText(text2);
  147.                 break;
  148.             case "py":
  149.                 textArea.setText("# your code goes here");
  150.                 break;
  151.             case "pl":
  152.                 textArea.setText("#!/usr/bin/perl\n# your code goes here");
  153.                 break;
  154.             case "js":
  155.                 textArea.setText("// your code goes here");
  156.                 break;
  157.             case "rb":
  158.                 textArea.setText("# your code goes here");
  159.                 break;
  160.             case "c":
  161.                 String text3 = "#include <stdio.h>\n\nint main(void) {\n\t// your code goes here\n\treturn 0;\n}";
  162.                 textArea.setText(text3);
  163.                 break;
  164.             case "cpp":
  165.                
  166.                 break;
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement