//File | Open action performed public void jMenuFileOpen_actionPerformed(ActionEvent e) throws IOException{ String inFilename; openFile.show(); try{ inFilename = openFile.getDirectory()+openFile.getFile(); this.filename.setText(inFilename); FileInputStream inFile = new FileInputStream(inFilename); DataInputStream inStream = new DataInputStream(inFile); textArea.setText(""); String line; while((line=inStream.readLine())!=null) textArea.append(line+"\n"); inStream.close(); inFile.close(); }catch(IOException x){ this.filename.setText("Error reading "+ this.filename.getText()); } }
//File | Save action performed public void jMenuFileSave_actionPerformed(ActionEvent e) throws IOException{ String outFilename; saveFile.show(); try{ outFilename = saveFile.getDirectory()+saveFile.getFile(); this.filename.setText(outFilename); FileOutputStream outFile = new FileOutputStream(outFilename); DataOutputStream outStream = new DataOutputStream(outFile); outStream.writeBytes(matchTextArea.getText()); outStream.close(); }catch(IOException x){ this.filename.setText("Error writing "+ this.filename.getText()); } }
//File | Exit action performed public void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); }
//Filter | None action performed public void jMenuFilterNone_actionPerformed(ActionEvent e) { matchTextArea.setText(textArea.getText()); }
//Filter | Grep action performed public void jMenuFilterGrep_actionPerformed(ActionEvent e) throws IOException{ String phrase = JOptionPane.showInputDialog(this, "Enter phrase to grep"); StringBufferInputStream inString = new StringBufferInputStream(textArea.getText()); DataInputStream inStream = new DataInputStream(inString); String line; int idx; matchTextArea.setText(""); while((line=inStream.readLine())!=null){ if((idx=line.indexOf(phrase))<0){ ; } else{ matchTextArea.append(line+"\n"); } } }
//Help | About action performed public void jMenuHelpAbout_actionPerformed(ActionEvent e) { }
//Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { jMenuFileExit_actionPerformed(null); } } }
class WinGrepFrame_jMenuFileOpen_ActionAdapter implements ActionListener { WinGrepFrame adaptee;
WinGrepFrame_jMenuFileOpen_ActionAdapter(WinGrepFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { try{ adaptee.jMenuFileOpen_actionPerformed(e); } catch (IOException x){ adaptee.textArea.append("cannot open the file\n"); } } }
class WinGrepFrame_jMenuFileSave_ActionAdapter implements ActionListener { WinGrepFrame adaptee;
WinGrepFrame_jMenuFileSave_ActionAdapter(WinGrepFrame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { try{ adaptee.jMenuFileSave_actionPerformed(e); } catch (IOException x){ adaptee.textArea.append("cannot open the file\n"); } } }
class WinGrepFrame_jMenuFileExit_ActionAdapter implements ActionListener { WinGrepFrame adaptee;