Skip to content

Instantly share code, notes, and snippets.

@ashwinreddy
Created October 18, 2014 05:10
Show Gist options
  • Save ashwinreddy/a05c66e6cba443ce2742 to your computer and use it in GitHub Desktop.
Save ashwinreddy/a05c66e6cba443ce2742 to your computer and use it in GitHub Desktop.
// A simple text editor implemented in Java using JFrame
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
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.JOptionPane;
import javax.swing.JTextArea;
//Text editor class that is a type of JFrame
public class TextEditor extends JFrame implements ActionListener{
//The Text Editor class owns a text area
String Text;
JTextArea area;
File myFile;
boolean writtenToFileAlready = false;
public static void main(String[] args) {
new TextEditor();
}
//When the program is run, a new object is made using this class
//Constructor for a new text editor
int size;
public TextEditor(){
//Tell JFrame's constructor to run
super("Text Editor");
setSize(700, 700);
initialize();
//make the JFrame visible
setVisible(true);
}
private void initialize() {
area = new JTextArea(30,20);
add(area);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu fileMenu = new JMenu("File");
//----------------------
String[] file = {"New","Open","Save","Save as","Quit","Print"};
for(String f: file){
JMenuItem f1 = new JMenuItem(f);
f1.setActionCommand(f);
f1.addActionListener(this);
fileMenu.add(f1);
}
//----------------------
JMenu editMenu = new JMenu("Edit");
String[] edit = {"Undo","Redo","Copy","Cut","Paste", "Find"};
for(String e: edit){
JMenuItem e1 = new JMenuItem(e);
e1.setActionCommand(e);
e1.addActionListener(this);
editMenu.add(e1);
}
//-----------------------
JMenu formatMenu = new JMenu("Format");
String[] format = {"Font","Size","Bold","Italic","Underline","Strikethrough","Superscript", "Subscript","Clear Formatting"};
for(String fo: format){
JMenuItem fo1 = new JMenuItem(fo);
fo1.setActionCommand(fo);
fo1.addActionListener(this);
formatMenu.add(fo1);
}
//Make a filter menu
JMenu filterMenu = new JMenu("Filter");
JMenuItem removeBadWords = new JMenuItem("Remove Bad Words");
removeBadWords.setActionCommand("bad words");
removeBadWords.addActionListener(this);
filterMenu.add(removeBadWords);
JMenuItem lowercase = new JMenuItem("Lowercase");
lowercase.setActionCommand("lowercase");
lowercase.addActionListener(this);
filterMenu.add(lowercase);
JMenuItem uppercase = new JMenuItem("Uppercase");
uppercase.setActionCommand("uppercase");
uppercase.addActionListener(this);
filterMenu.add(uppercase);
JMenuItem shakesperian = new JMenuItem("Shakesperian");
shakesperian.setActionCommand("Shakesperian");
shakesperian.addActionListener(this);
filterMenu.add(shakesperian);
JMenuItem troll = new JMenuItem("Remove Trolling");
troll.setActionCommand("troll");
troll.addActionListener(this);
filterMenu.add(troll);
createMenuItem("Remove Insults");
filterMenu.add("Remove Insults");
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(filterMenu);
}
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);
if(result == JFileChooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
try {
String content = ("");
Scanner s = new Scanner(f);
//While there is another line of input, append a line of input
while(s.hasNextLine()){
//\n adds a new line
content = content + "\n" + s.nextLine();
}
area.setText(content);
} catch (FileNotFoundException e) {
System.out.println("Not a file.");
//e.printStackTrace();
}
}
}
public void saveFile(){
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(this);
if(!writtenToFileAlready){
result = chooser.showSaveDialog(this);
}
if(result == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
// if file doesnt exists, then create it
if (!file.exists()) {
try {
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(area.getText());
bw.close();
writtenToFileAlready = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void saveFileAs(){
JFileChooser chooser = new JFileChooser();
int result = chooser.showSaveDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
// if file doesn't exists, then create it
try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(area.getText());
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
if(e.getActionCommand().equals("Cut")) {
System.out.println("foo");
Cut();
}
if(e.getActionCommand().equals("New")) {
new TextEditor();
}
if(e.getActionCommand().equals("Save as")){
saveFileAs();
}
if(e.getActionCommand().equals("Open")){
openFile();
}
if(e.getActionCommand().equals("Save")){
saveFile();
}
if(e.getActionCommand().equals("Quit")){
quit();
}
if(e.getActionCommand().equals("Print")){
}
/*if(e.getActionCommand().equals("Undo")){
Undo();
}*/
if(e.getActionCommand().equals("Copy")) {
Copy();
}
if(e.getActionCommand().equals("Paste")){
Paste();
}
if(e.getActionCommand().equals("Font")){
Font();
}
if(e.getActionCommand().equals("Size")){
Size();
}
if(e.getActionCommand().equals("bad words")){
removeBadWords();
}
if(e.getActionCommand().equals("lowercase")){
lowercase();
}
if(e.getActionCommand().equals("uppercase")){
uppercase();
}
if(e.getActionCommand().equals("Shakesperian")){
Shakesperian();
}
if(e.getActionCommand().equals("troll")){
Troll();
}
if(e.getActionCommand().equals("Remove Insults")){
Insults();
}
}
private void Cut() {
System.out.println("test");
String content = area.getText();
//System.out.println(area.getText());
//content = area.getSelectedText();
Text = (area.getSelectedText());
if (Text != null && content != null) {
if (content.endsWith(area.getSelectedText())) {
// caret at the back
}
else {
// caret at the front
}
content = content.substring(0, area.getCaretPosition()) + content.substring(area.getCaretPosition() + Text.length());
content = content.replaceAll(area.getSelectedText(),"");
System.out.println(area.getSelectedText());
area.setText(content);
}
}
/*private void Undo() {
}*/
private void Copy() {
Text = (area.getSelectedText());
}
private void Paste() {
area.setText(Text);
}
private void quit() {
WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
private void Insults() {
String content = area.getText();
content = content.replaceAll("\\b(\\w+)\\bis\\b(\\w+)\\b","$1 is not $2");
area.setText(content);
}
private void Font() {
String font = JOptionPane.showInputDialog("What font?");
Font f = new Font(font, 0, size);
area.setFont(f);
}
private void Size() {
String input = JOptionPane.showInputDialog("What size?");
int s = Integer.parseInt(input);
Font f = new Font("Arial", 0, s);
size = s;
area.setFont(f);
}
private void removeBadWords() {
//Get Whatever is in the text area
String content = area.getText();
//Mess with it
//the (?i) makes something case insensitive
content = content.replaceAll("(?i)\\bdarn\\b","dang it");
content = content.replaceAll("(?i)\\bheck\\b ","hoo haa");
content = content.replaceAll("(?i)\\bshoot\\b ","hoot");
//Put it back in
area.setText(content);
}
private void lowercase() {
//Get Whatever is in the text area
String content = area.getText();
//Mess with it
content = content.toLowerCase();
//Put it back in
area.setText(content);
}
private void uppercase() {
//Get Whatever is in the text area
String content = area.getText();
//Mess with it
content = content.toUpperCase();
//Put it back in
area.setText(content);
}
private void Shakesperian() {
//Get Whatever is in the text area
String content = area.getText();
//Mess with it
//the (?i) makes something case insensitive
content = content.replaceAll("(?i)\\byou\\b"," thou ");
content = content.replaceAll("(?i)\\byour\\b "," thy ");
content = content.replaceAll("(?i)\\bdo\\b "," doth ");
content = content.replaceAll("(?i)\\bhas\\b "," hath ");
content = content.replaceAll("(?i)\\bare\\b","art");
//Put it back in
area.setText(content);
}
//content = content.replaceAll("tr(ol)+","go away"); checks if the ol repeats and replaces trol and trolol...
private void Troll() {
//System.out.println("foo");
//Get Whatever is in the text area
String content = area.getText();
//Mess with it
//the (?i) makes something case insensitive
content = content.replaceAll("(?i)\\btr(ol)+\\b","Go away!");
content = content.replaceAll("(?i)\\btroll\\b ","Go away!");
content = content.replaceAll("(?i)\\b(ha)+\\b ","Haha");
content = content.replaceAll("(?i)\\bha+ha+\\b ","Haha");
//Put it back in
area.setText(content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment