Skip to content

Instantly share code, notes, and snippets.

@keshavsaharia
Created June 27, 2014 16:20
Show Gist options
  • Save keshavsaharia/1676a909ea268132d90b to your computer and use it in GitHub Desktop.
Save keshavsaharia/1676a909ea268132d90b to your computer and use it in GitHub Desktop.
text editor
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
// We made a TextEditor class that is a type
public class TextEditor extends JFrame implements ActionListener {
// The text editor class owns a text area
JTextArea area;
// When I say "run", it should just make a new object
// using this class
public static void main(String[] args) {
new TextEditor();
}
// Constructor for the text editor
public TextEditor() {
// Tell JFrame's constructor to run
super("text editor");
// Set the size
setSize(700, 700);
// Initialize it
initialize();
// Make it visible
setVisible(true);
}
// Create the text area and the menu bar
private void initialize() {
area = new JTextArea(30, 20);
add(area);
JMenuBar bar = new JMenuBar();
// Make a file menu
JMenu fileMenu = new JMenu("file");
JMenuItem newFileItem = new JMenuItem("new");
fileMenu.add(newFileItem);
// Create an item for opening files
JMenuItem openFileItem = new JMenuItem("open");
// Set the command for the ActionEvent to "open"
openFileItem.setActionCommand("open");
// Tell the item to send its action events here
openFileItem.addActionListener(this);
// Add the item to the file menu
fileMenu.add(openFileItem);
JMenuItem saveFileItem = new JMenuItem("save");
fileMenu.add(saveFileItem);
bar.add(fileMenu);
// Make a filter menu
JMenu filterMenu = new JMenu("filter");
JMenuItem removeProfanity = new JMenuItem("remove profanity");
removeProfanity.setActionCommand("swearing");
removeProfanity.addActionListener(this);
filterMenu.add(removeProfanity);
filterMenu.add(createMenuItem("remove insults"));
filterMenu.add(createMenuItem("something else"));
filterMenu.add(createMenuItem("etc"));
bar.add(filterMenu);
setJMenuBar(bar);
}
public JMenuItem createMenuItem(String name) {
JMenuItem item = new JMenuItem(name);
item.setActionCommand(name);
item.addActionListener(this);
return item;
}
public void openFile() {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(this);
// Check if the open dialog returned successfully
if (result == JFileChooser.APPROVE_OPTION) {
// Get my data file
File f = chooser.getSelectedFile();
System.out.println(f.getAbsolutePath());
try {
// Reading it off
String content = "";
Scanner s = new Scanner(f);
// While there's another line of input
while (s.hasNextLine()) {
// Append another line of input
content = content + s.nextLine();
content = content + "\n";
}
// Tell my JTextArea that this is what your text is
area.setText(content);
}
// If I don't find a file
catch (FileNotFoundException e) {
System.out.println("Not a file!");
}
}
}
public void saveFile() {
System.out.println("You think I want to save your stuff?");
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
System.out.println( command );
if (command.equals("open")) {
openFile();
}
if (command.equals("swearing")) {
removeProfanity();
}
if (command.equals("remove troll")) {
removeTrolls();
}
if (command.equals("remove insults")) {
removeInsults();
}
}
private void removeInsults() {
String content = area.getText();
content = content.replaceAll("(\\w+) is a (\\w+)", "$1 is not a $2");
//content = content.replaceAll("\\b(\\w+)\\b", "$1eth");
content = content.replaceAll("(I|you) like (\\w+)", "$1 does not like $2");
area.setText(content);
}
private void removeTrolls() {
// Get whatever is in the text area
String wb = "\\b";
String content = area.getText();
// Mess with it
content = content.replaceAll("troll", "go away!");
content = content.replaceAll("tr(ol)+", "go away!");
content = content.replaceAll(wb + "ha(ha)+" + wb, "haha");
content = content.replaceAll("ha+ha+", "haha");
// Put it back into the text area
area.setText( content );
}
private void removeProfanity() {
// Get whatever is in the text area
String content = area.getText();
// Mess with it
content = content.replaceAll("darn", "dagnabbit");
content = content.replaceAll("\\bheck\\b", "hoo ha");
// Put it back into the text area
area.setText( content );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment