home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
WDESAMPL.BIN
/
JavaPad.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
12KB
|
405 lines
/*
This simple extension of the java.awt.Frame class
contains all the elements necessary to act as the
main window of an application.
*/
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
public class JavaPad extends Frame
{
void SelectAll_Action(java.awt.event.ActionEvent event) {
// to do: place event handler code here.
textArea1.select(0, textArea1.getText().length());
}
void Paste_Action(java.awt.event.ActionEvent event) {
// to do: place event handler code here.
textArea1. replaceText(m_Clipboard, textArea1.getSelectionStart(), textArea1.getSelectionEnd());
}
void Copy_Action(java.awt.event.ActionEvent event) {
// to do: place event handler code here.
m_Clipboard = textArea1.getSelectedText();
}
void Cut_Action(java.awt.event.ActionEvent event) {
// to do: place event handler code here.
m_Clipboard = textArea1.getSelectedText();
textArea1. replaceText("", textArea1.getSelectionStart(), textArea1.getSelectionEnd());
}
void SaveAs_Action(java.awt.event.ActionEvent event) {
// to do: place event handler code here.
saveFileDialog1.show();
m_fileName = saveFileDialog1.getFile();
m_dirName = saveFileDialog1.getDirectory();
if (m_fileName!=null)
{
try {
FileOutputStream foS = new FileOutputStream( m_dirName + m_fileSeparator + m_fileName );
String text = textArea1.getText();
int size = text.length();
byte[] byteBuffer = new byte[size];
text.getBytes(0, byteBuffer.length, byteBuffer, 0);
foS.write(byteBuffer);
foS.close();
}
catch (IOException e)
{
System.out.print("Error reading file");
}
}
setTitle("JavaPad - " + m_fileName);
}
void New_Action(java.awt.event.ActionEvent event) {
// to do: place event handler code here.
m_fileName = "Untitled";
textArea1.setText("");
setTitle("JavaPad - " + m_fileName);
}
void Save_Action(java.awt.event.ActionEvent event) {
if (m_fileName == "Untitled")
{
// Create a new file and then save it's contents.
saveFileDialog1.show();
m_fileName = saveFileDialog1.getFile();
m_dirName = saveFileDialog1.getDirectory();
}
if (m_fileName!=null)
{
try {
FileOutputStream foS = new FileOutputStream( m_dirName + m_fileSeparator + m_fileName );
String text = textArea1.getText();
int size = text.length();
byte[] byteBuffer = new byte[size];
text.getBytes(0, byteBuffer.length, byteBuffer, 0);
foS.write(byteBuffer);
foS.close();
}
catch (IOException e)
{
System.out.print("Error reading file");
}
}
}
void Open_Action(java.awt.event.ActionEvent event) {
//{{CONNECTION
// Action from Open... Show the OpenFileDialog
openFileDialog1.show();
m_fileName = openFileDialog1.getFile();
m_dirName = openFileDialog1.getDirectory();
if (openFileDialog1.getFile()!=null)
{
try {
FileInputStream fiS = new FileInputStream( m_dirName + m_fileSeparator + m_fileName );
textArea1.setText("");
int notAtEnd = 1;
while (notAtEnd!=-1 && notAtEnd!=0)
{
int size = fiS.available();
byte[] byteBuffer = new byte[size];
notAtEnd = fiS.read(byteBuffer);
// Convert to a String
StringBuffer sb = new StringBuffer(size);
int count = 0;
while (count < size)
{
sb.append((char)byteBuffer[count]);
count++;
}
textArea1.appendText(sb.toString());
}
textArea1.select(0,0);
setTitle("JavaPad - " + m_fileName);
fiS.close();
}
catch (IOException e)
{
System.out.print("Error reading file");
}
}
//}}
}
void About_Action(java.awt.event.ActionEvent event) {
//{{CONNECTION
// Action from About Create and show as modal
(new AboutDialog(this, true)).show();
//}}
}
void Exit_Action(java.awt.event.ActionEvent event) {
//{{CONNECTION
// Action from Exit Create and show as modal
(new QuitDialog(this, true)).show();
//}}
}
public JavaPad() {
Properties prop = new Properties(System.getProperties());
m_fileSeparator = prop.getProperty("file.separator");
//{{INIT_CONTROLS
setLayout(new GridLayout(0,1,0,0));
setVisible(false);
setSize(400,300);
saveFileDialog1 = new java.awt.FileDialog(this);
saveFileDialog1.setMode(FileDialog.SAVE);
saveFileDialog1.setTitle("Save");
//$$ saveFileDialog1.move(0,0);
openFileDialog1 = new java.awt.FileDialog(this);
openFileDialog1.setMode(FileDialog.LOAD);
openFileDialog1.setTitle("Open");
//$$ openFileDialog1.move(36,0);
textArea1 = new java.awt.TextArea();
textArea1.setBounds(0,0,400,300);
add(textArea1);
setTitle("JavaPad - Untitled");
//}}
m_findReplace = new FindReplace("",textArea1);
//{{INIT_MENUS
mainMenuBar = new java.awt.MenuBar();
menu1 = new java.awt.Menu("File");
miNew = new java.awt.MenuItem("New");
menu1.add(miNew);
miOpen = new java.awt.MenuItem("Open...");
menu1.add(miOpen);
miSave = new java.awt.MenuItem("Save");
menu1.add(miSave);
miSaveAs = new java.awt.MenuItem("Save As...");
menu1.add(miSaveAs);
menu1.addSeparator();
miExit = new java.awt.MenuItem("Exit");
menu1.add(miExit);
mainMenuBar.add(menu1);
menu2 = new java.awt.Menu("Edit");
miCut = new java.awt.MenuItem("Cut");
menu2.add(miCut);
miCopy = new java.awt.MenuItem("Copy");
menu2.add(miCopy);
miPaste = new java.awt.MenuItem("Paste");
menu2.add(miPaste);
menu2.addSeparator();
miSelectAll = new java.awt.MenuItem("Select All");
menu2.add(miSelectAll);
mainMenuBar.add(menu2);
menu4 = new java.awt.Menu("Search");
miFind = new java.awt.MenuItem("Find ...");
menu4.add(miFind);
miFindAgain = new java.awt.MenuItem("Find Again");
menu4.add(miFindAgain);
miReplace = new java.awt.MenuItem("Replace ...");
menu4.add(miReplace);
mainMenuBar.add(menu4);
menu3 = new java.awt.Menu("Help");
mainMenuBar.setHelpMenu(menu3);
miAbout = new java.awt.MenuItem("About");
menu3.add(miAbout);
mainMenuBar.add(menu3);
setMenuBar(mainMenuBar);
//$$ mainMenuBar.move(0,0);
//}}
//{{REGISTER_LISTENERS
Action lAction = new Action();
miSelectAll.addActionListener(lAction);
miPaste.addActionListener(lAction);
miCopy.addActionListener(lAction);
miCut.addActionListener(lAction);
miSaveAs.addActionListener(lAction);
miNew.addActionListener(lAction);
miSave.addActionListener(lAction);
miOpen.addActionListener(lAction);
miAbout.addActionListener(lAction);
miExit.addActionListener(lAction);
miFind.addActionListener(lAction);
miFindAgain.addActionListener(lAction);
SymKey lSymKey = new SymKey();
textArea1.addKeyListener(lSymKey);
miReplace.addActionListener(lAction);
SymWindow aSymWindow = new SymWindow();
addWindowListener(aSymWindow);
//}}
}
public JavaPad(String title) {
this();
setTitle(title);
}
public synchronized void show() {
move(50, 50);
super.show();
}
static public void main(String args[]) {
(new JavaPad()).show();
}
String m_fileSeparator;
String m_dirName;
String m_fileName;
String m_Clipboard;
//{{DECLARE_CONTROLS
java.awt.FileDialog saveFileDialog1;
java.awt.FileDialog openFileDialog1;
java.awt.TextArea textArea1;
//}}
FindDialog m_findDialog;
ReplaceDialog m_replaceDialog;
//{{DECLARE_MENUS
java.awt.MenuBar mainMenuBar;
java.awt.Menu menu1;
java.awt.MenuItem miNew;
java.awt.MenuItem miOpen;
java.awt.MenuItem miSave;
java.awt.MenuItem miSaveAs;
java.awt.MenuItem miExit;
java.awt.Menu menu2;
java.awt.MenuItem miCut;
java.awt.MenuItem miCopy;
java.awt.MenuItem miPaste;
java.awt.MenuItem miSelectAll;
java.awt.Menu menu4;
java.awt.MenuItem miFind;
java.awt.MenuItem miFindAgain;
java.awt.MenuItem miReplace;
java.awt.Menu menu3;
java.awt.MenuItem miAbout;
//}}
FindReplace m_findReplace;
class Action implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == miSelectAll)
SelectAll_Action(event);
else if (object == miPaste)
Paste_Action(event);
else if (object == miCopy)
Copy_Action(event);
else if (object == miCut)
Cut_Action(event);
else if (object == miSaveAs)
SaveAs_Action(event);
else if (object == miNew)
New_Action(event);
else if (object == miSave)
Save_Action(event);
else if (object == miOpen)
Open_Action(event);
else if (object == miAbout)
About_Action(event);
else if (object == miExit)
Exit_Action(event);
else if (object == miFind)
miFind_Action(event);
else if (object == miFindAgain)
miFindAgain_Action(event);
else if (object == miReplace)
miReplace_Action(event);
}
}
class SymWindow extends java.awt.event.WindowAdapter
{
public void windowClosing(java.awt.event.WindowEvent event)
{
Object object = event.getSource();
if (object == JavaPad.this)
Frame1_WindowClosing(event);
}
}
void Frame1_WindowClosing(java.awt.event.WindowEvent event)
{
hide(); // hide the Frame
dispose(); // free the system resources
System.exit(0); // close the application
}
void miFind_Action(java.awt.event.ActionEvent event)
{
int select_start;
int select_end;
int select_diff;
int start_index;
String select_body;
//automatically put in selection in FindDialog field
select_start = textArea1.getSelectionStart();
select_end = textArea1.getSelectionEnd();
select_diff = select_end - select_start;
//set find field if selected text not too big
if(select_diff > 0 && select_diff < 20) {
m_findReplace.setFind(textArea1.getSelectedText());
start_index = textArea1.getSelectionStart() + select_diff;
m_findReplace.setIndex(start_index);
}
else {
m_findReplace.setFind("");
m_findReplace.setIndex(textArea1.getSelectionStart());
}
m_findReplace.setOccur(0);
(new FindDialog(this, "Find", true, m_findReplace)).show();
}
void miFindAgain_Action(java.awt.event.ActionEvent event)
{
FindReplaceEngine fre;
fre = new FindReplaceEngine(m_findReplace);
fre.FindIt();
if(!m_findReplace.isFoundIt()) this.getToolkit().beep();
}
class SymKey extends java.awt.event.KeyAdapter
{
public void keyPressed(java.awt.event.KeyEvent event)
{
Object object = event.getSource();
if (object == textArea1)
textArea1_KeyPress(event);
}
}
void textArea1_KeyPress(java.awt.event.KeyEvent event)
{
int keypressed;
keypressed = event.getKeyCode();
//114 is F3 on my keyboard, although java.awt.Event.F3 is 1010
if(keypressed == 114) { miFindAgain_Action(new ActionEvent(this,0,"")); }
}
void miReplace_Action(java.awt.event.ActionEvent event)
{
//(new ReplaceDialog(this, "Replace", true, m_findReplace)).show();
}
}