posted 18 years ago
Here is my final program
[code}
//Bradley Jordan
//Final Project
import java.awt.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.*;
import java.awt.event.*;
/** this is the class and all of the lovely variables needed for this to run*/
public class FileViewer extends JPanel implements KeyListener
{
private File file;
private JScrollPane scrollPane;
private JTextArea display;
boolean changes = false;
/** method for creating the fileviewer*/
public FileViewer ()
{
setLayout(new GridLayout(1,1));
display = new JTextArea();
display.setFont(new Font("Courier", Font.PLAIN, 12));
display.setEditable(true);
scrollPane = new JScrollPane(display);
add(scrollPane);
}
/** method for setting the new file.*/
public void setFile (File file) throws IOException
{
this.file = file;
refreshContents();
}
/** method for updating the JText Area with the new file.*/
private void refreshContents () throws IOException
{
BufferedReader in = new BufferedReader(new FileReader(file));
StringWriter out = new StringWriter();
PrintWriter pout = new PrintWriter(out);
for (String str = in.readLine();
str != null;
str = in.readLine())
{
pout.println(str);
}
pout.flush();
String contents = out.toString();
display.setText(contents);
pout.close();
out.close();
in.close();
}
/** this is creates the fileview in the in frame see the name. It wouldn't work without this, all the "work" gets done in here*/
public static JFrame createFileViewerInFrame (String filename)
throws IOException
{
final FileViewer viewer = new FileViewer();
viewer.setFile(new File(filename));
final JFrame frame = new JFrame(filename);
frame.getContentPane().setLayout(new GridLayout(1,1));
frame.add(viewer);
// This handles the window's close button.
frame.addWindowListener(new WindowAdapter ()
{
public void windowClosing (WindowEvent event)
{
if (viewer.changes == true)
{
int save = JOptionPane.showConfirmDialog(null,"The data has been changed, save?","Save?",JOptionPane.YES_NO_OPTION);
if(save == JOptionPane.YES_OPTION)
{
try
{
String text = viewer.display.getText();
BufferedWriter out = new BufferedWriter(new FileWriter(viewer.file));
viewer.changes = false;
out.write(text);
out.flush();
}
catch(IOException ioe)
{
JOptionPane.showConfirmDialog(null, "Please try again. An error has occured","Output Error", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
}
}
exit(frame);
}
});
setMenuBar(frame, viewer);
frame.setVisible(true);
frame.setSize(600, 800);
return frame;
}
/** this creates the menubar and than the submenus after that. All of the listners are added in here as well*/
private static void setMenuBar (final JFrame frame, final FileViewer viewer)
{
JMenuBar menuBar = new JMenuBar();
viewer.display.addKeyListener(new KeyAdapter(){public void keyTyped(KeyEvent
event)
{viewer.changes = true;;}});
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
fileMenu.setMnemonic('f');
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
editMenu.setMnemonic('e');
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
helpMenu.setMnemonic('h');
JMenuItem openMenuItem = new JMenuItem("Open");
fileMenu.add(openMenuItem);
openMenuItem.setMnemonic('o');
JMenuItem saveMenuItem = new JMenuItem("Save");
fileMenu.add(saveMenuItem);
saveMenuItem.setMnemonic('s');
JMenuItem exitMenuItem = new JMenuItem("Exit");
fileMenu.add(exitMenuItem);
exitMenuItem.setMnemonic('e');
JMenuItem searchMenuItem = new JMenuItem("Search");
editMenu.add(searchMenuItem);
searchMenuItem.setMnemonic('s');
JMenuItem replaceMenuItem = new JMenuItem("Replace");
editMenu.add(replaceMenuItem);
replaceMenuItem.setMnemonic('r');
JMenuItem contentsMenuItem = new JMenuItem("Contents");
helpMenu.add(contentsMenuItem);
contentsMenuItem.setMnemonic('c');
JMenuItem aboutMenuItem = new JMenuItem("About");
helpMenu.add(aboutMenuItem);
aboutMenuItem.setMnemonic('a');
exitMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
if (viewer.changes == true)
{
int save = JOptionPane.showConfirmDialog(null,"The data has been changed, save?","Save?",JOptionPane.YES_NO_OPTION);
if(save == JOptionPane.YES_OPTION)
{
try
{
String text = viewer.display.getText();
BufferedWriter out = new BufferedWriter(new FileWriter(viewer.file));
viewer.changes = false;
out.write(text);
out.flush();
}
catch(IOException ioe)
{
JOptionPane.showConfirmDialog(null, "Please try again. An error has occured","Output Error", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
}
}
exit(frame);
}
});
contentsMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
JOptionPane.showConfirmDialog(null, "This is a simple text editior"+ "\n" + "Works just like Notepad minus a few options","Instructions", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
});
aboutMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
JOptionPane.showConfirmDialog(null, "Written by Bradley Jordan" + "\n" + "Final Project" + "\n" + "April 26,2007","About", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
});
searchMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
String text = viewer.display.getText();
String a = JOptionPane.showInputDialog(null, "Please enter a character to search for","Search", JOptionPane.QUESTION_MESSAGE);
int index = text.indexOf(a, viewer.display.getCaret().getDot());
if (index > -1)
{
viewer.display.getCaret().setDot(index);
}
}
});
replaceMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
String text = viewer.display.getText();
String a = JOptionPane.showInputDialog(null, "Please enter a character to search for","Search", JOptionPane.QUESTION_MESSAGE);
String b = JOptionPane.showInputDialog(null, "Please enter a character to replace for","Replace", JOptionPane.QUESTION_MESSAGE);
int index = text.indexOf(a, viewer.display.getCaret().getDot());
if (index > -1)
{
int length = b.length();
viewer.display.replaceRange(b,index,index + length );
}
}
});
openMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
try
{
String newFile = JOptionPane.showInputDialog(null, "Please enter a file to open"+ "\n" + "Be sure include the entire location and file name i.e.:" + "\n" + "(C:\\temp\\file_name)","Open", JOptionPane.QUESTION_MESSAGE);
viewer.setFile(new File(newFile));
viewer.changes = false;
}
catch(IOException ioe)
{
JOptionPane.showConfirmDialog(null, "You inputed a file name that does not exist or is in a differnet location","Input Error", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
}
});
saveMenuItem.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
try
{
String text = viewer.display.getText();
BufferedWriter out = new BufferedWriter(new FileWriter(viewer.file));
viewer.changes = false;
out.write(text);
out.flush();
}
catch(IOException ioe)
{
JOptionPane.showConfirmDialog(null, "Please try again. An error has occured","Output Error", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
}
});
frame.setJMenuBar(menuBar);
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
}
/*
What follows here is a rather convoluted mess of multithreading
code that is not actually necessary. This is being done to avoid
calling System.exit() to exit the virtual machine. The only
other way to exit the virtual machine is for all non-daemon
threads to stop. So we have to make sure there is at least one
non-daemon thread (the main thread) AND we have to make sure
there are no other non-daemon threads.
*/
private static boolean exit = false;
private static Object lockObject = new Object();
public static void main (String [] args) throws Exception
{
final String filename = args[0];
Thread t = new Thread ()
{
public void run ()
{
synchronized (lockObject)
{
try
{
/*
This will start the AWT event queue which
we want to be started as a daemon thread.
The only way this can be assured is if
the parent thread (this thread) is also
a daemon thread.
*/
createFileViewerInFrame(filename).setVisible(true);
}
catch (IOException ioe)
{
ioe.printStackTrace();
FileViewer.exit(null);
}
}
}
};
t.setDaemon(true); // Make it a daemon.
synchronized (lockObject)
{
t.start();
while (!exit)
{
try
{
/*
Wait here until notified. Since this thread is
NOT a daemon thread, this keeps the virtual machine
alive.
*/
lockObject.wait();
}
catch (InterruptedException checkAgain) {}
}
}
}
public static void exit (JFrame frame)
{
synchronized (lockObject)
{
if (frame != null)
{
frame.setVisible(false);
/*
We need to dispose of the frame so that the AWT event
queue will not keep the virtual machine around waiting
for GUI events.
*/
frame.dispose();
}
exit = true;
/*
Tell the main thread it is time to stop waiting.
*/
lockObject.notify();
}
}
}
[/code]
thanks for all your help...