Anda di halaman 1dari 21

/** * FrameUtama.java * * http://eecchhoo.wordpress.com/ * echo_khannedy@yahoo.co.

id */ package notepad;

import java.awt.BorderLayout; import java.awt.Component; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException;

import javax.swing.Box; import javax.swing.ImageIcon;

import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.WindowConstants; import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditListener; import javax.swing.text.DefaultEditorKit; import javax.swing.undo.CannotRedoException; import javax.swing.undo.CannotUndoException; import javax.swing.undo.UndoManager;

import notepad.dialogform.DialogContainer; import notepad.dialogform.PanelAbout; import notepad.dialogform.PanelFont; import notepad.dialogform.PanelTextArea; import notepad.exception.BadPanelException; import notepad.utility.ArrayTextAction; import notepad.utility.IOUtility;

/** * Class ini merupakan class utama dari aplikasi notepad ini * * @author echo

*/ public class FrameUtama extends javax.swing.JFrame {

/** * Serial */ private static final long serialVersionUID = 112499409108089938L; private JMenu menu_file; private JMenuItem item_open; private JTextArea text_area; private JMenuItem item_about; private JMenu item_help; private JMenuItem item_area; private JMenuItem item_font; private JMenu menu_format; private JMenuItem item_exit; private JMenuItem item_selectall; private JMenuItem item_save; private JMenuItem item_paste; private JMenuItem item_cut; private JMenuItem item_copy; private JMenuItem item_redo; private JMenuItem item_undo; private JMenuItem item_saveas; private JMenu menu_edit; private JMenuBar menubar; private DialogContainer dialog; private PanelFont font;

private ArrayTextAction aksi; private JMenuItem item_new; private UndoManager manager; private File currentFile = null; private PanelTextArea areasetting; private PanelAbout about;

/** * Konstruktor class FrameUtama */ public FrameUtama() { super(); initGUI(); }

/** * Metode ini berisikan semua pendeklarasian komponen SWING */ private void initGUI() { try { this.setIconImage(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/app.png")).getImage()); this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOS E); this.setTitle("Swing NotePad"); { final JScrollPane jScrollPane1 = new JScrollPane(); getContentPane().add(jScrollPane1, BorderLayout.CENTER);

{ text_area = new JTextArea(); jScrollPane1.setViewportView(text_area); { aksi = new ArrayTextAction(text_area); } { manager = new UndoManager(); text_area.getDocument().addUndoableEditListener(new UndoableEditListener() {

public void undoableEditHappened(UndoableEditEvent e) { manager.addEdit(e.getEdit()); item_undo.setEnabled(manager.canUndo()); item_redo.setEnabled(manager.canRedo()); }

}); } { new DropTarget(text_area, new DropTargetListener() {

public void dragEnter(DropTargetDragEvent dtde) { if(!dtde.isDataFlavorSupported(DataFlavor.stringFlavor)){ dtde.rejectDrag(); } }

public void dragExit(DropTargetEvent dte) {

// Do nothing }

public void dragOver(DropTargetDragEvent dtde) { if(!dtde.isDataFlavorSupported(DataFlavor.stringFlavor)){ dtde.rejectDrag(); } }

public void drop(DropTargetDropEvent dtde) {

dtde.acceptDrop(DnDConstants.ACTION_COPY);

Transferable transfer = dtde.getTransferable(); DataFlavor[] flavor = transfer.getTransferDataFlavors();

for(DataFlavor i : flavor){ if(i.equals(DataFlavor.stringFlavor)){ try { String str = (String) transfer.getTransferData(i); text_area.replaceSelection(str); } catch (UnsupportedFlavorException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } }

dtde.dropComplete(true);

public void dropActionChanged(DropTargetDragEvent dtde) { if(!dtde.isDataFlavorSupported(DataFlavor.stringFlavor)){ dtde.rejectDrag(); } } }); } } } { menubar = new JMenuBar(); setJMenuBar(menubar); { menu_file = new JMenu(); menubar.add(menu_file); menu_file.setText("File"); menu_file.setMnemonic('F'); { item_new = new JMenuItem(); menu_file.add(item_new); item_new.setText("New"); item_new.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed N")); item_new.setMnemonic('N');

item_new.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/new.png"))); item_new.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { IOUtility.newFile(FrameUtama.this, text_area, currentFile); currentFile = null; manager.discardAllEdits(); }

}); } { item_open = new JMenuItem(); menu_file.add(item_open); item_open.setText("Open"); item_open.setBounds(31, -3, 484, 20); item_open.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed O")); item_open.setMnemonic('O'); item_open.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/open.png"))); item_open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { File temp = currentFile; String text = text_area.getText();

currentFile = IOUtility.openFile(FrameUtama.this, text_area, currentFile); if (temp == currentFile) { text_area.setText(text); }else{ manager.discardAllEdits(); } }

}); } { item_save = new JMenuItem(); menu_file.add(item_save); item_save.setText("Save"); item_save.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed S")); item_save.setMnemonic('S'); item_save.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/save1.png"))); item_save.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { if (currentFile == null) { currentFile = IOUtility.saveFile(FrameUtama.this, text_area); } else { IOUtility.saveNow(text_area, currentFile); }

}); } { item_saveas = new JMenuItem(); menu_file.add(item_saveas); item_saveas.setText("Save As"); item_saveas.setAccelerator(KeyStroke.getKeyStroke("shift ctrl pressed S")); item_saveas.setMnemonic('A'); item_saveas.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/save2.png"))); item_saveas.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { currentFile = IOUtility.saveFile(FrameUtama.this, text_area); }

}); } { item_exit = new JMenuItem(); menu_file.add(item_exit); item_exit.setText("Exit"); item_exit.setAccelerator(KeyStroke.getKeyStroke("alt pressed F4")); item_exit.setMnemonic('X');

item_exit.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/exit.png"))); item_exit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { if (currentFile == null) { if (text_area.getDocument().getLength() > 0) { int sure = JOptionPane.showConfirmDialog(FrameUtama.this, "Do you want to save it?"); if (sure == JOptionPane.OK_OPTION) { IOUtility.saveFile(FrameUtama.this, text_area); System.exit(0); } else { System.exit(0); } } else { System.exit(0); } } else { int sure = JOptionPane.showConfirmDialog(FrameUtama.this, "Do you want to save it?"); if (sure == JOptionPane.OK_OPTION) { IOUtility.saveNow(text_area, currentFile); System.exit(0); } else { System.exit(0);

} } }

}); } } { menu_edit = new JMenu(); menubar.add(menu_edit); menu_edit.setText("Edit"); menu_edit.setMnemonic('E'); { item_undo = new JMenuItem(); menu_edit.add(item_undo); item_undo.setText("Undo"); item_undo.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed Z")); item_undo.setMnemonic('U'); item_undo.setEnabled(false); item_undo.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/undo.png"))); item_undo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { try { manager.undo(); } catch (CannotUndoException t) {

} finally { item_undo.setEnabled(manager.canUndo()); item_redo.setEnabled(manager.canRedo()); } }

}); } { item_redo = new JMenuItem(); menu_edit.add(item_redo); item_redo.setText("Redo"); item_redo.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed Y")); item_redo.setMnemonic('R'); item_redo.setEnabled(false); item_redo.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/redo.png"))); item_redo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { try { manager.redo(); } catch (CannotRedoException t) { } finally { item_undo.setEnabled(manager.canUndo()); item_redo.setEnabled(manager.canRedo()); }

}); } { final JSeparator jSeparator1 = new JSeparator(); menu_edit.add(jSeparator1); } { item_copy = new JMenuItem(); item_copy.setAction(aksi.getAction(DefaultEditorKit.copyAction)); menu_edit.add(item_copy); item_copy.setText("Copy"); item_copy.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed C")); item_copy.setMnemonic('C'); item_copy.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/copy.png"))); } { item_cut = new JMenuItem(); item_cut.addActionListener(aksi.getAction(DefaultEditorKit.cutActi on)); menu_edit.add(item_cut); item_cut.setText("Cut"); item_cut.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed X")); item_cut.setMnemonic('U');

item_cut.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/cut.png"))); } { item_paste = new JMenuItem(); item_paste.addActionListener(aksi.getAction(DefaultEditorKit.past eAction)); menu_edit.add(item_paste); item_paste.setText("Paste"); item_paste.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed V")); item_paste.setMnemonic('P'); item_paste.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/paste.png"))); } { final JSeparator jSeparator2 = new JSeparator(); menu_edit.add(jSeparator2); } { item_selectall = new JMenuItem(); item_selectall.setAction(aksi.getAction(DefaultEditorKit.selectAllA ction)); menu_edit.add(item_selectall); item_selectall.setText("Select All"); item_selectall.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed A")); item_selectall.setMnemonic('A'); }

} { menu_format = new JMenu(); menubar.add(menu_format); menu_format.setText("Format"); menu_format.setMnemonic('T'); { item_font = new JMenuItem(); menu_format.add(item_font); item_font.setText("Font"); item_font.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed F")); item_font.setMnemonic('F'); item_font.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/font.png"))); item_font.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { showFontDialog(); }

}); } { item_area = new JMenuItem(); menu_format.add(item_area); item_area.setText("Text Area");

item_area.setAccelerator(KeyStroke.getKeyStroke("ctrl pressed T")); item_area.setMnemonic('A'); item_area.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/paragraf.png"))); item_area.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { showAreaDialog(); }

}); } } { Component glue = Box.createHorizontalGlue(); menubar.add(glue); } { item_help = new JMenu(); menubar.add(item_help); item_help.setText("Help"); item_help.setMnemonic('H'); { item_about = new JMenuItem(); item_help.add(item_about); item_about.setText("About"); item_about.setMnemonic('A');

item_about.setAccelerator(KeyStroke.getKeyStroke("shift ctrl alt pressed A")); item_about.setIcon(new ImageIcon(getClass().getClassLoader().getResource( "notepad/image/about.png"))); item_about.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { showAboutDialog(); }

}); } } } { dialog = new DialogContainer(this); { font = new PanelFont(text_area); } { areasetting = new PanelTextArea(text_area); } { about = new PanelAbout(); } } pack(); this.setSize(621, 469);

this.addWindowListener(new WindowAdapter() {

@Override public void windowClosing(WindowEvent e) { if (currentFile == null) { if (text_area.getDocument().getLength() > 0) { int sure = JOptionPane.showConfirmDialog(FrameUtama.this, "Do you want to save it?"); if (sure == JOptionPane.OK_OPTION) { IOUtility.saveFile(FrameUtama.this, text_area); System.exit(0); } else { System.exit(0); } } else { System.exit(0); } } else { int sure = JOptionPane.showConfirmDialog(FrameUtama.this, "Do you want to save it?"); if (sure == JOptionPane.OK_OPTION) { IOUtility.saveNow(text_area, currentFile); System.exit(0); } else { System.exit(0); } } }

}); } catch (Exception e) { e.printStackTrace(); } }

private void showAboutDialog() { try { dialog.showDialog(about); } catch (BadPanelException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE, null); } }

/** * Metode ini digunakan untuk menampilkan Area Dialog yang digunakan untuk * mengedit area text editor */ private void showAreaDialog() { try { dialog.showDialog(areasetting); } catch (BadPanelException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE, null);

} }

/** * Metode ini digunakan untuk menampilkan Font Dialog, yang digunakan untuk * mengedit Font Text */ private void showFontDialog() { try { dialog.showDialog(font); } catch (BadPanelException e) { JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE, null); } }