Description: This program creates a simple Text Editor.
Primary Inputs: ——
Primary Output: Text Editor
Platform Used: JDK 1.5 or higher.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
/*save file as "editor.java"*/ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.text.*; import java.text.*; import java.util.*; import javax.swing.undo.*; import javax.swing.event.*; import java.net.*; public class editor extends JFrame { // ALL THE VARIABLES USED IN THIS APPLICATION JTextArea area = new JTextArea(0, 0); JScrollPane scroller = new JScrollPane(area); public JPopupMenu pop = new JPopupMenu(); JMenu menu = new JMenu("Edit"); JMenuItem menuItem = new JMenuItem("copy"); JMenuItem menucut = new JMenuItem("cut"); JMenuItem menuclear = new JMenuItem("clear"); JMenuItem menupaste = new JMenuItem("paste"); JToolBar toolBar = new JToolBar(); JButton newFile = new JButton(new ImageIcon("new.jpg")); JButton openFile = new JButton(new ImageIcon("open.jpg")); JButton saveFile = new JButton(new ImageIcon("save.jpg")); JButton cutFile = new JButton(new ImageIcon("cut.jpg")); JButton copyFile = new JButton(new ImageIcon("copy.jpg")); JButton pasteFile = new JButton(new ImageIcon("paste.jpg")); JMenuBar menuBar = new JMenuBar(); JMenu FILE = new JMenu("File"); JMenu EDIT = new JMenu("Edit"); JMenuItem NEWFILE = new JMenuItem("New", new ImageIcon("new.jpg")); JMenuItem OPENFILE = new JMenuItem("Open", new ImageIcon("open.jpg")); JMenuItem SAVEFILE = new JMenuItem("Save", new ImageIcon("save.jpg")); JMenuItem SAVEASFILE = new JMenuItem("Save As..."); JMenuItem EXITFILE = new JMenuItem("Exit"); JMenuItem COPYEDIT = new JMenuItem("copy"); JMenuItem CUTEDIT = new JMenuItem("cut"); JMenuItem PASTEDIT = new JMenuItem("paste"); JMenuItem DELETEDIT = new JMenuItem("Delete"); JMenuItem FINDEDIT = new JMenuItem("Find"); JMenuItem FINDNEXTEDIT = new JMenuItem("Find Next"); JMenuItem GOTOEDIT = new JMenuItem("Go To"); JMenuItem SELECTEDIT = new JMenuItem("Select All"); JMenuItem TIMEDIT = new JMenuItem("Time/Date"); String file = null; String fileN; boolean opened = false; JPanel statusPanel = new JPanel(); JLabel statusLabel; JPanel aboutPanel = new JPanel(); int ind = 0; StringBuffer sbufer; String findString; public editor() { // DEFAULT TITLE OF FRAME super("untitled"); // SETS THE SIZE this.setSize(800, 600); // SETS THE LAYOUT this.getContentPane().setLayout(new BorderLayout()); // SETS WORD WRAP TO TRUE AS DEFAULT area.setLineWrap(true); // SETS FOCUS ON THE TEXTAREA area.requestFocus(true); // ADDS THE SCROLLPANE CONTAINING THE TEXTAREA TO THE CONTAINER this.getContentPane().add(scroller, BorderLayout.CENTER); // ADDS THE STATUSPANEL this.getContentPane().add(statusPanel, BorderLayout.SOUTH); // TO ENABLE DRAG MODE area.setDragEnabled(true); // SETS THE TOOLBAR TO BE STATIC I.E DISALLOW THE USER FROM CHANGING THE // DOCKING POSITION toolBar.setFloatable(false); // ADD THE TOOLBAR this.getContentPane().add(toolBar, BorderLayout.NORTH); // ADD A MOUSELISTENER TO RIGHT CLICK FOR THE POPUPLISTENER MouseListener popupListener = new PopupListener(); area.addMouseListener(popupListener); // SETS THE MENUBAR FILE.add(NEWFILE); FILE.add(OPENFILE); FILE.add(SAVEFILE); FILE.add(SAVEASFILE); FILE.addSeparator(); FILE.addSeparator(); FILE.add(EXITFILE); EDIT.add(CUTEDIT); EDIT.add(COPYEDIT); EDIT.add(PASTEDIT); EDIT.add(DELETEDIT); EDIT.addSeparator(); EDIT.add(FINDEDIT); EDIT.add(FINDNEXTEDIT); EDIT.add(GOTOEDIT); EDIT.addSeparator(); EDIT.add(SELECTEDIT); EDIT.add(TIMEDIT); FILE.setMnemonic(KeyEvent.VK_F); menuBar.add(FILE); EDIT.setMnemonic(KeyEvent.VK_E); menuBar.add(EDIT); // ADD MENUBAR TO THE FRAME this.setJMenuBar(menuBar); // ADD ITEMS TO THE POPUP pop.addSeparator(); pop.add(menuItem); pop.add(menucut); pop.add(menupaste); pop.add(menuclear); // VALIDATION newFile.setBorder(null); openFile.setBorder(null); saveFile.setBorder(null); cutFile.setBorder(null); copyFile.setBorder(null); pasteFile.setBorder(null); // ADD TO TOOLBAR toolBar.add(newFile); newFile.setToolTipText("New file"); toolBar.addSeparator(); toolBar.add(openFile); openFile.setToolTipText("Open file"); toolBar.add(saveFile); saveFile.setToolTipText("Save file"); toolBar.addSeparator(); toolBar.add(cutFile); cutFile.setToolTipText("Cut"); toolBar.add(copyFile); copyFile.setToolTipText("Copy"); toolBar.add(pasteFile); pasteFile.setToolTipText("Paste"); // ACTION FOR NEW FILE ON THE TOOLBAR newFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { opened = false; if (area.getText().equals("")) { System.out.print("text is empty"); } else { int confirm = JOptionPane.showConfirmDialog(null, "Would you like to save?", "New File", JOptionPane.YES_NO_CANCEL_OPTION); if (confirm == JOptionPane.YES_OPTION) { saveFile(); area.setText(null); statusPanel.removeAll(); statusPanel.validate(); } else if (confirm == JOptionPane.CANCEL_OPTION) { } else if (confirm == JOptionPane.NO_OPTION) { area.setText(null); statusPanel.removeAll(); statusPanel.validate(); } } } }); // //OPEN BUTTON ON THE TOOLBAR openFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } }); // //SAVE BUTTON ON THE TOOLBAR saveFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveFile(); } }); // ACTION FOR CUT BUTTON ON THE TOOLBAR cutFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.cut(); } }); // ACTION FOR COPY BUTTON ON THE TOOLBAR copyFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.copy(); } }); // ACTION FOR PASTE BUTTON ON THE TOOLBAR pasteFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.paste(); } }); // ACTIONLISTENER FOR OTHER ITEMS ON THE TOOLBAR // COPY BUTTON ON THE TOOLBAR menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.copy(); menupaste.setEnabled(true); pasteFile.setEnabled(true); PASTEDIT.setEnabled(true); } }); // CUT BUTTON ON THE TOOLBAR menucut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.cut(); menupaste.setEnabled(true); pasteFile.setEnabled(true); PASTEDIT.setEnabled(true); } }); // PASTE BUTTON ON THE TOOLBAR menupaste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.paste(); } }); // CLEAR BUTTON ON THE TOOLBAR menuclear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.setText(null); } }); // ACTIONLISTENER FOR ITEMS IN THE MENUBAR // OPEN A NEW FILE NEWFILE.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); NEWFILE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { opened = false; int confirm = JOptionPane.showConfirmDialog(null, "Would you like to save?", "New File", JOptionPane.YES_NO_CANCEL_OPTION); if (confirm == JOptionPane.YES_OPTION) { saveFile(); area.setText(null); statusPanel.removeAll(); statusPanel.validate(); } else if (confirm == JOptionPane.CANCEL_OPTION) { } else { area.setText(null); statusPanel.removeAll(); statusPanel.validate(); } } }); // SAVE OPTION. HAS A VALIDATION CHECK THAT CHECKS WETHER ITS AN OPENED // FILE OR NEW FILE SAVEFILE.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); SAVEFILE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveFile(); } }); // OPTION THAT WILL BRING UP A DIALOG WHICH SAVES THE FILE WITH A NAME // AND FORMAT DESIRED SAVEASFILE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { opened = false; saveFile(); } }); SELECTEDIT.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK)); SELECTEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.selectAll(); } }); // ACTION FOR OPEN BUTTON OPENFILE.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); OPENFILE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } }); // ACTION FOR CUT BUTTON CUTEDIT.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); CUTEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.cut(); } }); // ACTION FOR COPY BUTTON COPYEDIT.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); COPYEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.copy(); } }); // ACTION FOR PASTE BUTTON PASTEDIT.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); PASTEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.paste(); } }); // FONT SELECTOR OPTION // PRINTS THE SYSTEM DATE AND TIME IN THE EDITOR TIMEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Date currentDate; SimpleDateFormat formatter; String dd; formatter = new SimpleDateFormat("KK:mm aa MMMMMMMMM dd yyyy", Locale.getDefault()); currentDate = new java.util.Date(); dd = formatter.format(currentDate); area.insert(dd, area.getCaretPosition()); } }); // FINDS A WORD IN THE EDITOR FINDEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { sbufer = new StringBuffer(area.getText()); findString = JOptionPane.showInputDialog(null, "Find"); ind = sbufer.indexOf(findString); area.setCaretPosition(ind); area.setSelectionStart(ind); area.setSelectionEnd(ind + findString.length()); } catch (IllegalArgumentException npe) { JOptionPane.showMessageDialog(null, "String not found"); } catch (NullPointerException nfe) { } } }); // FINDS A WORD IN THE EDITOR FINDNEXTEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { sbufer = new StringBuffer(area.getText()); findString = JOptionPane.showInputDialog(null, "Find"); ind = sbufer.indexOf(findString, area.getCaretPosition()); area.setCaretPosition(ind); area.setSelectionStart(ind); area.setSelectionEnd(ind + findString.length()); } catch (IllegalArgumentException npe) { JOptionPane.showMessageDialog(null, "String not found"); } catch (NullPointerException nfe) { } } }); // EXITS THE APPLICATION AND CHECKS FOR ANY CHANGES MADE EXITFILE.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK)); EXITFILE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int confirm = JOptionPane.showConfirmDialog(null, "Would you like to save?", "Exit Application", JOptionPane.YES_NO_CANCEL_OPTION); if (confirm == JOptionPane.YES_OPTION) { saveFile(); dispose(); System.exit(0); } else if (confirm == JOptionPane.CANCEL_OPTION) { } else { dispose(); System.exit(0); } } }); // ACTION FOR GO TO OPTION GOTOEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { Element root = area.getDocument().getDefaultRootElement(); Element paragraph = root.getElement(Integer .parseInt(JOptionPane.showInputDialog(null, "Go to line"))); area.setCaretPosition(paragraph.getStartOffset() - 1); } catch (NullPointerException npe) { JOptionPane.showMessageDialog(null, "Invalid line number"); } catch (NumberFormatException nfe) { } } }); // ACTION FOR DELETE OPTION DELETEDIT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.replaceSelection(null); } }); // CLOSES THE WINDOW WHEN THE CLOSE BUTTON IS PRESSED addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int confirm = JOptionPane.showConfirmDialog(null, "Would you like to save?", "Exit Application", JOptionPane.YES_NO_CANCEL_OPTION); if (confirm == JOptionPane.YES_OPTION) { saveFile(); dispose(); System.exit(0); } else if (confirm == JOptionPane.CANCEL_OPTION) { } else { dispose(); System.exit(0); } } }); } // MAIN FUNCTION. public static void main(String args[]) { editor l = new editor(); l.setVisible(true); } // FUNCTION CALLED BY THE SAVE BUTTON public void saveFile() { String line = area.getText(); if (opened == true) { try { FileWriter output = new FileWriter(file); BufferedWriter bufout = new BufferedWriter(output); bufout.write(line, 0, line.length()); JOptionPane.showMessageDialog(null, "Save Successful"); bufout.close(); output.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } else { JFileChooser fc = new JFileChooser(); int result = fc.showSaveDialog(new JPanel()); if (result == JFileChooser.APPROVE_OPTION) { fileN = String.valueOf(fc.getSelectedFile()); try { FileWriter output = new FileWriter(fileN); BufferedWriter bufout = new BufferedWriter(output); bufout.write(line, 0, line.length()); JOptionPane.showMessageDialog(null, "Save Successful"); bufout.close(); output.close(); opened = true; } catch (IOException ioe) { ioe.printStackTrace(); } } } } // FUNCTION TO OPEN THE FILE public void openFile() { statusPanel.removeAll(); statusPanel.validate(); area.setText(null); JFileChooser fc = new JFileChooser(); int result = fc.showOpenDialog(new JPanel()); if (result == JFileChooser.APPROVE_OPTION) { String file = String.valueOf(fc.getSelectedFile()); // String dirn = fc.getDirectory(); File fil = new File(file); newFile.setEnabled(false); // START THIS THREAD WHILE READING FILE Thread loader = new FileLoader(fil, area.getDocument()); loader.start(); statusPanel.removeAll(); statusPanel.revalidate(); } else { } } // CLASS FOR THE MOUSELIsTENER class PopupListener extends MouseAdapter { public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { pop.show(e.getComponent(), e.getX(), e.getY()); } } } /*** Thread to load a file into the text storage model */ class FileLoader extends Thread { JLabel state; FileLoader(File f, Document doc) { setPriority(4); this.f = f; this.doc = doc; } public void run() { try { // initialize the statusbar statusPanel.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum((int) f.length()); statusPanel.add(new JLabel("opened so far ")); statusPanel.add(progress); statusPanel.revalidate(); // try to start reading Reader in = new FileReader(f); char[] buff = new char[4096]; int nch; while ((nch = in.read(buff, 0, buff.length)) != -1) { doc.insertString(doc.getLength(), new String(buff, 0, nch), null); progress.setValue(progress.getValue() + nch); } // we are done... get rid of progressbar statusPanel.removeAll(); statusPanel.revalidate(); } catch (IOException e) { System.err.println(e.toString()); } catch (BadLocationException e) { System.err.println(e.getMessage()); } newFile.setEnabled(true); } Document doc; File f; } // END MAIN CLASS } |