package ass2; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.nio.*; import java.nio.channels.FileChannel; //import for Date class import java.util.*; public class JFrameExt extends JFrame { //Saving time value at start of procedure. long startTime = new Date().getTime(); //Saving time value at end of procedure. long endTime = new Date().getTime(); //Calculating time in milliseconds taken by the procedure. double diffTime =(double) (endTime - startTime); JPanel contentPane; BorderLayout BorderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JPanel jPanel2 = new JPanel(); JPanel jPanel3 = new JPanel(); JTextField jtfSource = new JTextField(); JTextField jtfSource2 = new JTextField(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JButton jbtcopy = new JButton(); JButton jbtclr = new JButton(); JComboBox jcboType = new JComboBox(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea jtaMessage = new JTextArea(); public JFrameExt() { try { setDefaultCloseOperation (EXIT_ON_CLOSE); jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } private void jbInit()throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(BorderLayout1); setSize(new Dimension(400,300)); setTitle("Abraham Zepeda's Assignment 2"); //Panel 1 specifications jPanel1.setBackground(Color.red); jPanel1.setPreferredSize(new Dimension (50,30)); jtfSource.setText(""); jtfSource.setColumns(8); jLabel1.setText("From"); jtfSource2.setText(""); jtfSource2.setColumns(8); jLabel2.setText("To"); //Add items to combo box jcboType jcboType.addItem("Binary"); jcboType.addItem("Text"); //Panel 2 specifications jPanel2.setBackground(Color.green); jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jtaMessage.setEditable(false); jtaMessage.setColumns(40); jtaMessage.setRows(10); //Panel 3 specifications jPanel3.setBackground(Color.blue); jPanel3.setPreferredSize(new Dimension(50, 30)); jbtcopy.setText("Copy"); jbtcopy.addActionListener(new JFrameExt_jbtcopy_actionAdapter(this)); jbtclr.setText("Clear"); jbtclr.addActionListener(new JFrameExt_jbtclr_actionAdapter(this)); jcboType.addActionListener(new JFrameExt_jcboType_actionAdapter(this)); //Puting elements togheter in Panel 1 contentPane.add(jPanel1,java.awt.BorderLayout .NORTH); jPanel1.add(jLabel1); jPanel1.add(jtfSource); jPanel1.add(jLabel2); jPanel1.add(jtfSource2); jPanel1.add(jcboType); //Puting elements togheter in Panel 2 contentPane.add(jPanel2,java.awt.BorderLayout .CENTER); jPanel2.add(jtaMessage); jPanel2.add(jScrollPane1); jScrollPane1.getViewport().add(jtaMessage); //Puting elements togheter in Panel 3 contentPane.add(jPanel3,java.awt.BorderLayout .SOUTH); jPanel3.add(jbtcopy); jPanel3.add(jbtclr); } //EVENTS ON JCOMBOBOX public void jcboType_actionPerformed(ActionEvent e) { //Getting Selected Item from combo box jcboType String type = (String)jcboType.getSelectedItem(); String fromFile; String toFile; fromFile =jtfSource.getText(); toFile =jtfSource2.getText(); //TEXT SELECTION ON COMBOBOX if(type.equals("Text")) { try { //TEXT file i/o code //Declaring variables BufferedReader br = null; PrintWriter pw = null; //Creating BufferedReader br for inputting from textfile //Creating PrintWriter pw for outputting to textfile br = new BufferedReader(new FileReader(fromFile)); pw = new PrintWriter(new FileWriter(toFile)); //reading from br a line at a time. //writing to pw the line read. //flushing to pw to write immediately String line; while ((line = br.readLine()) != null) { pw.println(line); pw.flush(); } } catch (Exception ex) { System.err.println("File error"); ex.printStackTrace(); } finally { try { //file close code WORK ON THIS //pw.close(); } catch (Exception ex) { ex.printStackTrace(); } } //write on message screen jtaMessage.append("File Copy Started " + fromFile + "\n"); jtaMessage.append("File Copy Completed " + "\n"); jtaMessage.append("Time in Seconds " + diffTime/1000.0 + "\n"); jtaMessage.paintImmediately(0,0,jtaMessage.getWidth(), jtaMessage.getHeight()); } //DATA SELECTION ON COMBOBOX else if(type.equals("Binary")) { try { //DATA file i/o code //Declaring variables FileInputStream fis=null;; FileOutputStream fos=null; //Creating FileInputStream fis for inputting from binary file //Creating FileOutputStream fos for outputting to binary file fis = new FileInputStream (fromFile); fos = new FileOutputStream (toFile); //reading from fis a byte at a time. //writing to fos the byte read. //flushing to fos to write immediately. int r; while ((r = fis.read()) != -1) { fos.write((byte) r); fos.flush(); } //write on message screen //write on message screen jtaMessage.append("File Copy Started " + fromFile + "\n"); jtaMessage.append("File Copy Completed " + "\n"); jtaMessage.append("Time in Seconds " + diffTime/1000.0 + "\n"); jtaMessage.paintImmediately(0,0,jtaMessage.getWidth(), jtaMessage.getHeight()); } catch (Exception ex) { jtaMessage.setText("File error"); System.err.println("File error"); ex.printStackTrace(); } finally { try { //file close code WORK ON THIS //.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } //EVENTS ON COPY, QUESTION IS HOW TO IMPLEMENT THIS AFTER SELECTING FROM COMBOBOX. public void jbtcopy_actionPerformed(ActionEvent e) { jtaMessage.setText("HOW TO DO THIS, INSTEAD OF FIRING EVEN UPON COMBOBOX SELECTION, DOING IT AFTER COMBOSELECTION AND CLICKING COPY?"); } //EVENTS ON CLEAR public void jbtclr_actionPerformed(ActionEvent e) { jtfSource.setText(""); jtfSource2.setText(""); //clearing JTextArea jtaMessage jtaMessage.setText(""); } } ///Nothing to do here class JFrameExt_jcboType_actionAdapter implements ActionListener { private JFrameExt adaptee; JFrameExt_jcboType_actionAdapter(JFrameExt adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jcboType_actionPerformed(e); } } class JFrameExt_jbtcopy_actionAdapter implements ActionListener { private JFrameExt adaptee; JFrameExt_jbtcopy_actionAdapter(JFrameExt adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jbtcopy_actionPerformed(e); } } class JFrameExt_jbtclr_actionAdapter implements ActionListener { private JFrameExt adaptee; JFrameExt_jbtclr_actionAdapter(JFrameExt adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jbtclr_actionPerformed(e); } }