import java.awt.*; import java.awt.event.*; import java.io.*; /** * Who are you? * What section is this? * When are you turning this in? * * An encrypting text editor * CS 170 Homework * */ public class JPad extends GUIApp implements ActionListener { // Constructor ------------------------------------- public JPad(String fname) { // Construct superclass, initialize fields super("JPad - " + fname); this.fname = fname; // Set layout & size ---------------------- setLayout(new BorderLayout(5, 5)); Toolkit tk = Toolkit.getDefaultToolkit(); int w = tk.getScreenSize().width / 2; int h = tk.getScreenSize().height / 2; setBounds(0, 0, w, h); // Initialize & place TextArea ---------------- add(ta, "Center"); ta.setFont(new Font("Courier", Font.PLAIN, 12)); // Initialize & place status bar, toolbar, menu bar status.setBackground(Color.lightGray); add(status, "South"); add(toolBar(), "North"); setMenuBar(menuBar()); // Display initial file -------------------------- readFile(); show(); } /** * Creates ToolBar object */ Panel toolBar() { Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.LEFT)); String[] btns = {"New", "Open", "Save", "Save As..."}; for (int i = 0; i < btns.length; i++) { Button b = new Button(btns[i]); b.setActionCommand(btns[i]); b.addActionListener(this); p.add(b); } p.setBackground(Color.lightGray); return p; } /** * Creates MenuBar */ MenuBar menuBar() { MenuBar mb = new MenuBar(); Menu fm = new Menu("File"); fm.add("New"); fm.add("Open"); fm.add("Save"); fm.add("Save As..."); fm.add("-"); fm.add("Exit"); fm.addActionListener(this); mb.add(fm); return mb; } /** * Menu, Button handler */ public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand().equals("New")) newFile(); else if (ae.getActionCommand().equals("Open")) openFile(); else if (ae.getActionCommand().equals("Save")) saveFile(); else if (ae.getActionCommand().equals("Save As...")) saveFileAs(); else if (ae.getActionCommand().equals("Exit")) quitApp(); } /** * Clears TextArea * Sets fname to Untitled */ private void newFile() { System.out.println("New File"); /* 1. Set fname to "Untitled" 2. Clear ta 3. Set focus to ta */ } /** * Uses FileDialog to get FileName * Sets fname to directory + filename * Calls readFile() */ private void openFile() { System.out.println("Open File"); /* 1. Open a FileDialog using *.jpad as the mask 2. If use cancels then print a message and return 3. Otherwise, set fname to directory+filename, 4. Call readFile() */ } /** * Check if file is "Untitled" * Yes? Calls SaveFileAs() * No? Calls writeFile() */ private void saveFile() { System.out.println("Save File"); /* 1. If fname equals "Untitled" call saveFileAs() 2. Otherwise call writeFile() */ } /** * Use FileDialog to get name * Sets fname variable * Calls writeFile() */ private void saveFileAs() { System.out.println("Save File As"); /* 1. Open a FileDialog using *.jpad as the mask 2. If user cancels then just display message in status line 3. Otherwise, set fname to chosen name [use directory] 4. Call writeFile(); */ } /** * Reads current file from fname * Store result in TextArea * Success/Failure displayed in status */ private void readFile() { /* 1. If fname equals "Untitled" clear ta, return 2. Else Create a FileInputStream using fname 3. Read the file 4. Store the results in ta 5. Display message in status line */ } /** * Save current contents of TextArea * to the file named fname. * Success/Failure displayed in status */ private void writeFile() { /* 1. Create a FileOutputStream using fname 2. Write the contents of the file to disk 3. Display message in status line */ } private void quitApp() { dispose(); System.exit(0); } // Main Method ------------------------------------- public static void main(String[] args) { if (args.length > 0) new JPad(args[0]); else new JPad("Untitled"); } // Attributes -------------------------------------- private TextArea ta = new TextArea(); private String fname = ""; private Label status = new Label("Ready", Label.LEFT); }