Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.io.*;
- import java.util.*;
- public class Gui extends JFrame{
- private File file;
- private JLabel label;
- private JButton openFile;
- private JButton save;
- private JTextArea textArea;
- public Gui(){
- super("Frost Text Editor");
- FlowLayout layout = new FlowLayout();
- layout.setAlignment(FlowLayout.LEFT);
- setLayout(layout);
- openFile = new JButton("Open\\Create");
- openFile.setToolTipText("Open or Create the file you want to edit.");
- add(openFile);
- openFile.addActionListener(
- new ActionListener(){
- public void actionPerformed(ActionEvent event){
- JFileChooser chooser = new JFileChooser();
- int returnVal = chooser.showOpenDialog(getContentPane());
- file = chooser.getSelectedFile();
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- if(file.exists()){
- label.setText("Opened file: " +
- file.getName());
- label.setToolTipText(file.getAbsolutePath());
- textArea.setText(readFile(file));
- } else{
- JOptionPane.showMessageDialog(null, "You tried to open a non exist file.");
- if(JOptionPane.showConfirmDialog(null, "Would you want to create that file?") == 0){
- if(createFile(file)){
- autoComplete(file.getName());
- label.setText("Opened file: " +
- file.getName());
- label.setToolTipText(file.getAbsolutePath());
- }
- }
- }
- }
- }
- }
- );
- save = new JButton("Save");
- add(save);
- save.addActionListener(
- new ActionListener(){
- public void actionPerformed(ActionEvent event){
- if(file != null){
- try {
- Formatter f = new Formatter(file.getAbsolutePath());
- f.format("%s", textArea.getText());
- f.close();
- label.setText("The file: " + file.getName() + " has been saved succesfly");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } else {
- JOptionPane.showMessageDialog(null, "You must open\\create a file first");
- }
- }
- }
- );
- label = new JLabel();
- add(label);
- textArea = new JTextArea("I think Frost Text Editor is the best editor!", 25, 50);
- textArea.setFont(new Font("Arial", Font.PLAIN, 14));
- JScrollPane scrollPane = new JScrollPane(textArea);
- textArea.setEditable(true);
- add(scrollPane);
- }
- private boolean createFile(File f){
- if(!f.exists()){
- try{
- Formatter x = new Formatter(f.getAbsolutePath());
- JOptionPane.showMessageDialog(null, "File created succesfly");
- return true;
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null, "Couldn't create the file.");
- return false;
- }
- }
- return false;
- }
- private String readFile(File f){
- try{
- String fileContains = "";
- Scanner in = new Scanner(f);
- while(in.hasNextLine()){
- String line = in.nextLine();
- fileContains += line + "\n";
- }
- in.close();
- return fileContains;
- }
- catch(Exception e){
- return "";
- }
- }
- private void autoComplete(String name){
- int num = name.lastIndexOf('.');
- String start = name.substring(0, num);
- String end = name.substring(num + 1, name.length());
- switch(end){
- case "html":
- 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>";
- textArea.setText(text);
- break;
- case "java":
- String text1 = "public class " + start + " {\n";
- text1 += " public static void main(String[] args) {\n\n\n\n\t}\n}";
- textArea.setText(text1);
- break;
- case "php":
- textArea.setText("<?php\n\n\n\n?>");
- break;
- case "cs":
- String text2 = "public class Test\n{\n\tpublic static void Main()\n\t{\n\t\t// your code goes here\n\t}\n}";
- textArea.setText(text2);
- break;
- case "py":
- textArea.setText("# your code goes here");
- break;
- case "pl":
- textArea.setText("#!/usr/bin/perl\n# your code goes here");
- break;
- case "js":
- textArea.setText("// your code goes here");
- break;
- case "rb":
- textArea.setText("# your code goes here");
- break;
- case "c":
- String text3 = "#include <stdio.h>\n\nint main(void) {\n\t// your code goes here\n\treturn 0;\n}";
- textArea.setText(text3);
- break;
- case "cpp":
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement