import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author izzur
*/
public class nrp5213100087 extends javax.swing.JFrame {
final JFileChooser fc = new JFileChooser();
BufferedReader br;
FileWriter fw;
BufferedWriter bw;
StringReader sr;
File file;
FileFilter ff;
FileNameExtensionFilter textFilter;
int returnVal;
Object[] option = {"Yes, please","No, thanks"};
public nrp5213100087() {
initComponents();
cekWrap.setSelected(false);
textFilter = new FileNameExtensionFilter("Text Files","txt","text");
fc.addChoosableFileFilter(textFilter);
fc.setFileFilter(textFilter);
}
void parseContent() throws IOException {
String temp;
area51.setText("");
try {
br = new BufferedReader(new FileReader(fc.getSelectedFile()));
do {
temp = br.readLine();
if(temp!=null) area51.append(temp+"\\n");
} while (temp!=null);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (NullPointerException e) {
}
br.close();
}
void storeContent() throws IOException {
try {
file = new File(fc.getSelectedFile().getAbsolutePath());
if(!file.exists()) file.createNewFile();
fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
sr = new StringReader(area51.getText());
br = new BufferedReader(sr);
for (String line = br.readLine(); line!=null ; line=br.readLine()) {
bw.write(line);
bw.newLine();
}
bw.close();
fw.close();
sr.close();
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch (NullPointerException e) {
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
area51 = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
newMenu = new javax.swing.JMenuItem();
openMenu = new javax.swing.JMenuItem();
saveMenu = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
exitMenu = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenu4 = new javax.swing.JMenu();
cekWrap = new javax.swing.JCheckBoxMenuItem();
jMenu3 = new javax.swing.JMenu();
lalala = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
area51.setColumns(20);
area51.setRows(5);
area51.setAutoscrolls(false);
jScrollPane1.setViewportView(area51);
jMenu1.setText("File");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
newMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
newMenu.setText("New");
newMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newMenuActionPerformed(evt);
}
});
jMenu1.add(newMenu);
openMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
openMenu.setText("Open...");
openMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openMenuActionPerformed(evt);
}
});
jMenu1.add(openMenu);
saveMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
saveMenu.setText("Save...");
saveMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuActionPerformed(evt);
}
});
jMenu1.add(saveMenu);
jMenu1.add(jSeparator2);
exitMenu.setText("Exit");
exitMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuActionPerformed(evt);
}
});
jMenu1.add(exitMenu);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem3.setText("Cut");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem4.setText("Copy");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem4);
jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem5.setText("Paste");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem5);
jMenuBar1.add(jMenu2);
jMenu4.setText("Format");
cekWrap.setSelected(true);
cekWrap.setText("Word Wrap");
cekWrap.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cekWrapActionPerformed(evt);
}
});
jMenu4.add(cekWrap);
jMenuBar1.add(jMenu4);
jMenu3.setText("Help");
lalala.setText(" About");
lalala.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lalalaActionPerformed(evt);
}
});
jMenu3.add(lalala);
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void newMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(area51.getText()!="") {
int n = JOptionPane.showOptionDialog(this,
"Apakah file ingin disimpan ?",
"Simpan ?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
option, option[0]);
if(n==0) saveMenu.doClick();
if(n==1) {};
}
area51.setText("");
}
private void openMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area51.setEnabled(true);
returnVal = fc.showOpenDialog(this);
try {
parseContent();
} catch (IOException ex) {
Logger.getLogger(nrp5213100087.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void cekWrapActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(cekWrap.isSelected()) {
area51.setLineWrap(true);
area51.setWrapStyleWord(true);
} else {
area51.setLineWrap(false);
area51.setWrapStyleWord(false);
}
}
private void exitMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int n = JOptionPane.showOptionDialog(this,
"Apakah file ingin disimpan ?",
"Simpan ?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
option, option[0]);
if(n==0) saveMenu.doClick();
if(n==1) {};
System.exit(0);
}
private void lalalaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(this, "This note is made by \\nMuhammad Zuhri - 5213100087\\n");
}
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void saveMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
returnVal = fc.showSaveDialog(this);
try {
storeContent();
} catch (IOException ex) {
Logger.getLogger(nrp5213100087.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area51.cut();
}
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area51.copy();
}
private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
area51.paste();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(nrp5213100087.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(nrp5213100087.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(nrp5213100087.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(nrp5213100087.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new nrp5213100087().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextArea area51;
private javax.swing.JCheckBoxMenuItem cekWrap;
private javax.swing.JMenuItem exitMenu;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JMenuItem lalala;
private javax.swing.JMenuItem newMenu;
private javax.swing.JMenuItem openMenu;
private javax.swing.JMenuItem saveMenu;
// End of variables declaration
}