Вы находитесь на странице: 1из 6

import javax.swing.*; import java.util.Calendar; import java.io.*; import java.awt.image.BufferedImage; import java.awt.*; import java.awt.event.*; import java.util.

Date; import javax.swing.JFrame; import javax.imageio.ImageIO; import java.io.File; import java.text.*; /** Class CCleaner deletes the unwanted old files in the system * that were modified before the user specified period * @author R.Jeya Sri, S.Gowsalya ,M.Usha Rani ,S.Karthikeyini, B.Lakshmi Priya * @version 2.0 * @since 1.5 */ public class CCleaner extends JFrame { private JFrame frame1=new JFrame("PC CLEANER - List Of Files"); private Date selecteddate= new Date(); private JButton next,exit,list; private JComboBox daycb,loccb; private BufferedImage image,ima; private Icon ic1,ic2,ic3; private JTextField t1; private JLabel l1,lblname,label,lblname1,ll,ll1; private int noofdays,m; private JScrollPane scrollPane; private String period,files,temp; private Object[][] filerec = new Object[200][3]; private JPanel p1,p2; private Date d; private JTable table; private String[] path ={"/home/jeyasri/Desktop","/home/jeyasri/Downloads","/home /jeyasri/Documents"}; private Date mod=new Date(); private Calendar cal = Calendar.getInstance(); /** Constructor of outer class CCleaner. All the buttons , labels and the text f ields are * defined here and the action listeners are set */ public CCleaner() { setSize(500,400); setTitle("PC Cleaner"); try { image = ImageIO.read(new File("/home/jeyasri/Desktop/logo.png")); } catch(IOException ie) { System.out.println("Error:"+ie.getMessage()); } l1=new JLabel(new ImageIcon(image)); l1.setBounds(50,45,400,100); ic1= new ImageIcon("/home/jeyasri/Desktop/deletestart.png"); next = new JButton(ic1);

next.setToolTipText("Delete the available files"); ic2= new ImageIcon("/home/jeyasri/Desktop/close.png"); exit = new JButton(ic2); exit.setToolTipText("Quit"); ic3= new ImageIcon("/home/jeyasri/Desktop/next.png"); list = new JButton(ic3); list.setToolTipText("View the list of files"); daycb = new JComboBox(); daycb.addItem("-select-"); daycb.addItem("1 day"); daycb.addItem("2 days"); daycb.addItem("3 days"); daycb.addItem("1 week"); daycb.addItem("1 month"); daycb.addItem("2 months"); daycb.addItem("3 months"); daycb.addItem("6 months"); daycb.addItem("1 year"); daycb.setBounds(250,210,150,20); loccb=new JComboBox(); loccb.addItem("-select"); loccb.addItem("Documents"); loccb.addItem("Downloads"); loccb.addItem("Desktop"); loccb.setBounds(250,165,150,20); lblname = new JLabel("SELECT THE FILES "); lblname.setBounds(80,160,150,100); lblname1 = new JLabel("MODIFIED BEFORE :"); lblname1.setBounds(80,175,150,100); ll=new JLabel("SELECT LOCATION :"); ll.setBounds(80,125,150,100); ll1=new JLabel("FILES AVAILABLE :"); ll1.setBounds(80,210,150,100); t1=new JTextField(""); t1.setEditable(false); t1.setBounds(250,250,50,20); add(l1); p1 = new JPanel(); p1.setLayout(null); p1.add(ll); p1.add(loccb); p1.add(ll1); p1.add(t1); p1.add(lblname); p1.add(lblname1); p1.add(daycb); add(p1); p2 = new JPanel(); p2.add(next); p2.add(exit); p2.add(list); add(p2,BorderLayout.SOUTH); CCleaner.event ae=new CCleaner.event(); CCleaner.event1 ev=new CCleaner.event1(); next.addActionListener(ev); exit.addActionListener(ev); list.addActionListener(ev); daycb.addActionListener(ae); }

/** Method Modified finds the accurate last modified date required by the user * @param m holds the no of days, flag holds identification number * @return last modified date */ public Date Modified(int m,int flag) { if (flag==0) { cal.add(Calendar.DATE, -m); } if(flag==1) { cal.add(Calendar.MONTH, -m); } if(flag==2) { cal.add(Calendar.YEAR,-m); } return (cal.getTime()); } /** Inner class event is for combo box selection event handling */ public class event implements ActionListener { public void actionPerformed(ActionEvent ae) { daycb=(JComboBox)ae.getSource(); period=(String)daycb.getSelectedItem(); //Determining the selected value in combo box finding the appropriate last modified date required if (period.equals("1 day")) mod=Modified(1,0); if (period.equals("2 days")) mod=Modified(2,0); if (period.equals("3 days")) mod=Modified(3,0); if (period.equals("1 week")) mod=Modified(7,0); if (period.equals("1 month")) mod=Modified(1,1); if (period.equals("2 months")) mod=Modified(2,1); if (period.equals("3 months")) mod=Modified(3,1); if (period.equals("6 months")) mod=Modified(6,1); if (period.equals("1 year")) mod=Modified(1,2); //modified files listing int j=0,k=0; m=0; while (j<3) { File folder = new File(path[j]); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) {

if (listOfFiles[i].isFile()) { files = listOfFiles[i].getName(); long t=listOfFiles[i].lastModified(); Date d=new Date(t); if(mod.compareTo(d)==1|mod.compareTo(d)>1) { ++m; filerec[k][0]=k+1; filerec[k][1]=files; temp=convert(d); filerec[k][2]=temp; k++; } } } j++; } //calculating available files String we=Integer.toString(m); t1.setText(we); } } /** Method convert converts the given date to string format * @param t holds the date to be converted * @return string format of the given date */ public String convert(Date t) { DateFormat formatter ; formatter = new SimpleDateFormat("dd-MMM-yy"); String st = formatter.format(t); return (st); } /** Inner class event1 is for event handling of button selection */ class event1 implements ActionListener { public JFrame frame=new JFrame("Notification"); /** Method actionPerformed overrides the function in the actionListener Interfac e * @param ev holds the button that was clicked */ public void actionPerformed( ActionEvent ev) { Object src=ev.getSource(); if (src==list) { Object columnNames[] = { "S.no", "File Name", "Last Modified" }; table = new JTable(filerec, columnNames); table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); scrollPane = new JScrollPane(table); frame1.add(scrollPane, BorderLayout.CENTER); frame1.setSize(500, 350);

frame1.setVisible(true); } if(src== next) { SwingProgressBar spb= new SwingProgressBar(); int j=0,k=0; while (j<3) { File folder = new File(path[j]); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { files = listOfFiles[i].getName(); long t=listOfFiles[i].lastModified(); Date d=new Date(t); if(mod.compareTo(d)==1|mod.compareTo(d)>1) { listOfFiles[i].delete(); } } } j++; } } if (src== exit) { System.exit(0); } } } /** Inner class SwingProgressBar is for the progress bar during the file * deletion */ public class SwingProgressBar { final static int interval = 200; int i; JProgressBar pb; Timer timer; JButton button; JPanel panel1 = new JPanel(); JFrame frame = new JFrame("PC CLEANER - Progress"); String str = "Files are being deleted..."; /** Constructos of class SwingProgressBar defines the button , timer and * the progress bar required */ public SwingProgressBar() { button = new JButton("Delete"); button.addActionListener(new ButtonListener()); pb = new JProgressBar(0,20); pb.setValue(0); pb.setStringPainted(true); label = new JLabel("");

panel1.add(label, BorderLayout.SOUTH); JPanel panel = new JPanel(); panel.add(button); panel.add(pb); panel1.setLayout(new BorderLayout()); panel1.add(panel, BorderLayout.NORTH); panel1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); frame.setContentPane(panel1); frame.pack(); frame.setVisible(true); //Creating a timer. timer = new Timer(interval, new ActionListener() { public void actionPerformed(ActionEvent evt) { if (i == 20) { Toolkit.getDefaultToolkit().beep(); timer.stop(); button.setEnabled(true); pb.setValue(0); frame.setVisible(false); JOptionPane.showMessageDialog(frame,"The availables files are deleted"); t1.setText("0"); } i = i + 1; pb.setValue(i); } }); } /** Inner class of the inner class SwingProgressBar that performs the progressin g event */ class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent ae) { button.setEnabled(false); i = 0; String str = "Files are being deleted..."; label.setText(str); timer.start(); } } } /** Main function of the outer class CCleaner */ public static void main(String[] args) { CCleaner w1 = new CCleaner(); w1.setVisible(true); w1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

Вам также может понравиться