Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Created October 28, 2018 02:58
Show Gist options
  • Save AshleyGrant/54f19b89506bada5fc68969864688b18 to your computer and use it in GitHub Desktop.
Save AshleyGrant/54f19b89506bada5fc68969864688b18 to your computer and use it in GitHub Desktop.
//Ashley Grant
//Project7.java
//A simple text editor
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class Project7
{
public static void main( String[] args )
{
//System.out.println( ) );
TextEditor editor = new TextEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//if more than one argument passed in from command line, open blank file
if( args.length > 1 )
{
JOptionPane.showMessageDialog( editor, "Too Many Arguments, Opening Blank File" );
}
//if a valid filename is passed in to the
//program from the command line, attempt to
//open the file, if the filename passed in
//does not exist, then just leave editor pane
//blank and set the title
if( args.length == 1 )
{
try
{
editor.currentFile = new File( args[0] );
if( editor.currentFile.exists() )
{
FileReader read = new FileReader( editor.currentFile );
editor.editPane.read( read, null );
read.close();
}
}
catch (IOException e)
{
System.out.println( "\nError: IOException\nExiting..." );
System.exit( -1 );
}
//retitle frame with name of file
editor.setTitle( "Ashley's Text Editor: " + editor.currentFile.getName() );
}
editor.setSize( 500, 400 );
editor.show();
}
}
class TextEditor extends JFrame
{
File currentFile = null;
JScrollPane scrollPane;
JEditorPane editPane;
JLabel timeBar = new JLabel();
public TextEditor()
{
setTitle("Ashley's Text Editor: untitled");
//setSize(600, 400 );
editPane = new JEditorPane( "text/plain", "" );
scrollPane = new JScrollPane( editPane );
getContentPane().add( scrollPane );
//create the menu bar
JMenuBar menuBar = new JMenuBar();
//create the "File" menu
JMenu fileMenu = new JMenu("File");
//Populate the "File" menu
Action New = new NewAction();
fileMenu.add( New );
Action Open = new OpenAction();
fileMenu.add( Open );
Action Save = new SaveAction();
fileMenu.add( Save );
Action SaveAs = new SaveAsAction();
fileMenu.add( SaveAs );
fileMenu.addSeparator();
JMenuItem Quit = new JMenuItem( "Quit" );
fileMenu.add( Quit );
//Add "File" menu to the menu Bar
menuBar.add( fileMenu );
//Create "Edit" menu
JMenu editMenu = new JMenu( "Edit" );
//Populate "Edit" menu
Action Copy = new CopyAction();
editMenu.add( Copy );
Action Cut = new CutAction();
editMenu.add( Cut );
Action Paste = new PasteAction();
editMenu.add( Paste );
JMenuItem SelectAll = new JMenuItem( "Select All" );
editMenu.addSeparator();
editMenu.add( SelectAll );
/*
JCheckBoxMenuItem WordWrap = new JCheckBoxMenuItem( "Word Wrap" );
editMenu.add( WordWrap );
*/
//Add "Edit" menu to the menu Bar
menuBar.add( editMenu );
setJMenuBar( menuBar );
//add the action listeners for the three remaining menuitems
Quit.addActionListener( new QuitActionListener() );
SelectAll.addActionListener( new SelectAllActionListener() );
// WordWrap.addActionListener( new ActionLister() {
//create Toolbar
JToolBar toolBar = new JToolBar();
toolBar.setFloatable( false );
toolBar.add( New );
toolBar.add( Open );
toolBar.add( Save );
toolBar.add( SaveAs );
toolBar.addSeparator();
toolBar.add( Copy );
toolBar.add( Cut );
toolBar.add( Paste );
//Add the toolbar to the top of the contentpane
getContentPane().add(toolBar, BorderLayout.NORTH);
//DateFormat df = DateFormat.getTimeInstance();
//JLabel timeBar = new JLabel( df.format( new Date() ) );
//Create the thread needed to update the time on the timeBar
DateThread dt = new DateThread();
//right alighn the time
timeBar.setHorizontalAlignment( JLabel.RIGHT );
//put the time bar in the frame
getContentPane().add( timeBar, BorderLayout.SOUTH);
}
//describes actions for use with "New"
private class NewAction extends AbstractAction
{
public NewAction()
{
putValue(Action.NAME, "New");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/New.gif")) );
putValue(Action.SHORT_DESCRIPTION,
"New" );
}
public void actionPerformed( ActionEvent event )
{
//point currentFile to null and empty the edit pan
//retitle the frame
currentFile = null;
editPane.setText( "" );
setTitle( "Ashley's Text Editor: untitled" );
}
}
private class OpenAction extends AbstractAction
{
public OpenAction()
{
putValue(Action.NAME, "Open");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/Open.gif") ));
putValue(Action.SHORT_DESCRIPTION,
"Open" );
}
public void actionPerformed( ActionEvent event )
{
//create a filechooser and open it as an
//open dialog
JFileChooser fc = new JFileChooser(".");
int returnVal = fc.showOpenDialog( null );
//if the user hits "OK" then try to open the file
//catch any exceptions
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
currentFile = fc.getSelectedFile();
FileReader read = new FileReader( currentFile );
editPane.read( read, null );
read.close();
}
catch (IOException e)
{
System.out.println( "Error: IOException\nExiting..." );
System.exit( -1 );
}
//retitle frame with name of file
setTitle( "Ashley's Text Editor: " + currentFile.getName() );
}
}
}
private class SaveAction extends AbstractAction
{
public SaveAction()
{
putValue(Action.NAME, "Save");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/Save.gif")) );
putValue(Action.SHORT_DESCRIPTION,
"Save" );
}
public void actionPerformed( ActionEvent event )
{
//if file has not been saved yet, then we must use a
//filechooser dialog to name it and pick it's
//position
if( currentFile == null )
{
JFileChooser fc = new JFileChooser(".");
int returnVal = fc.showSaveDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION )
{
//retitle frame and set currentFile to the file
//user has chosen
currentFile = fc.getSelectedFile();
setTitle( "Ashley's Text Editor: " + currentFile.getName() );
}
else
{
return;
}
}
try
{
//try saving the file
FileWriter out = new FileWriter( currentFile );
out.write( editPane.getText() );
out.close();
}
catch (IOException e)
{
System.out.println( "Error: IOException\nExiting" );
System.exit( -1 );
}
}
}
private class SaveAsAction extends AbstractAction
{
public SaveAsAction()
{
putValue(Action.NAME, "Save As");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/SaveAs.gif")) );
putValue(Action.SHORT_DESCRIPTION,
"Save As" );
}
public void actionPerformed( ActionEvent even )
{
//this is the same as above without checking if the
//currentFile is null
JFileChooser fc = new JFileChooser(".");
int returnVal = fc.showSaveDialog(null);
if( returnVal == JFileChooser.APPROVE_OPTION )
{
currentFile = fc.getSelectedFile();
setTitle( "Ashley's Text Editor: " + currentFile.getName() );
}
else
{
return;
}
try
{
FileWriter out = new FileWriter( currentFile );
out.write( editPane.getText() );
out.close();
}
catch (IOException e)
{
System.out.println( "Error: IOException\nExiting" );
System.exit( -1 );
}
}
}
private class QuitActionListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
//quit
System.exit( 0 );
}
}
private class CopyAction extends AbstractAction
{
public CopyAction()
{
putValue(Action.NAME, "Copy");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/Copy.gif")) );
putValue(Action.SHORT_DESCRIPTION,
"Copy" );
}
public void actionPerformed( ActionEvent event )
{
//copy selected text
editPane.copy();
}
}
private class CutAction extends AbstractAction
{
public CutAction()
{
putValue(Action.NAME, "Cut");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/Cut.gif")) );
putValue(Action.SHORT_DESCRIPTION,
"Cut" );
}
public void actionPerformed( ActionEvent event )
{
//cut selected text
editPane.cut();
}
}
private class PasteAction extends AbstractAction
{
public PasteAction()
{
putValue(Action.NAME, "Paste");
putValue(Action.SMALL_ICON, new ImageIcon(getClass().getResource("images/Paste.gif") ));
putValue(Action.SHORT_DESCRIPTION,
"Paste" );
}
public void actionPerformed( ActionEvent event )
{
//paste selected text
editPane.paste();
}
}
private class SelectAllActionListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
//select all of the edit pane
editPane.selectAll();
}
}
//class that allows for the clock in the lower right hand corner to
//update continuously
private class DateThread extends Thread
{
public DateThread()
{
//System.out.println( "Created" );
start();
}
public void run()
{
while( true )
{
try
{
sleep( 1000 );
DateFormat df = DateFormat.getTimeInstance();
timeBar.setText( df.format( new Date() ) );
}
catch (InterruptedException e)
{
}
}
}
}//end class Date Thread
}//end class texteditor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment