/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package moviemanager;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.sql.*;
/**
*
* @author Vishal
*/
public class MovieManager {
private static JButton addDB;
private JFrame menu;
private JPanel left,right,top,center;
private JMenuBar mainmenu;
private static String fileinfodetails = null;
public MovieManager(JFrame menu, JPanel left, JPanel right, JPanel top, JPanel center, JMenuBar mainmenu, JMenu file, JMenuItem seldir, String directoryname, JFileChooser jfc, JList list, String[] filenames, File directory, FilenameFilter filter, DefaultListModel listmodel, JTextArea fileinfo) {
this.menu = menu;
this.left = left;
this.right = right;
this.top = top;
this.center = center;
this.mainmenu = mainmenu;
this.file = file;
this.seldir = seldir;
this.directoryname = directoryname;
this.jfc = jfc;
this.list = list;
this.filenames = filenames;
this.directory = directory;
this.filter = filter;
this.listmodel = listmodel;
this.fileinfo = fileinfo;
}
private JMenu file;
private JMenuItem seldir;
private String directoryname;
private JFileChooser jfc;
private static JList list;
private String filenames[];
private File directory;
private FilenameFilter filter;
private DefaultListModel listmodel;
private static JTextArea fileinfo;
private JScrollPane listjcp;
MovieManager(){
JFrame main = new JFrame("Movie Manager");
main.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
listmodel = new DefaultListModel();
list = new JList(listmodel);
mainmenu = new JMenuBar();
file = new JMenu("File");
seldir = new JMenuItem("Select Directory to scan");
mainmenu.add(file);
file.add(seldir);
seldir.addActionListener(new MenuHandler());
directoryname="F:/TV/How i met your mother/season 1/";
fileinfo = new JTextArea("No File Selected yet",3,1);
fileinfo.setSize(300, 250);
//<<----------------MAIN WINDOW ------------>>
main.setVisible(true);
main.setBounds(340,150,700,500);
main.setResizable(false);
main.setLayout(new BorderLayout());
main.setJMenuBar(mainmenu);
//<----------End Window----------->>
left= new JPanel();
right = new JPanel();
top = new JPanel();
center = new JPanel();
addDB = new JButton("Add To DB");
main.add(center,BorderLayout.CENTER);
center.setLayout(new FlowLayout());
center.add(addDB);
addDB.setEnabled(false);
addDB.addActionListener(new Database());
main.add(left,BorderLayout.WEST);
main.add(right, BorderLayout.EAST);
left.setLayout(new BorderLayout());
main.add(top,BorderLayout.PAGE_START);
top.setLayout(new GridLayout(1,0));
left.add(new JLabel("Files Scanned:"),BorderLayout.NORTH);
right.add(fileinfo, BorderLayout.EAST);
fileinfo.setSize(250, 250);
fileinfo.setLineWrap(true);
left.add(list, BorderLayout.CENTER);
// directory = new File(directoryname);
// filenames = directory.list();
//list = new JList(filenames);
left.add(listjcp = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
listjcp.setPreferredSize(new Dimension(250,500));
list.addListSelectionListener(new ListListener());
}
public static void main(String[] args) {
MovieManager movieManager = new MovieManager();
Database database = new Database();
}
private class MenuHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae){
menu = new JFrame("Select a folder to scan");
menu.setVisible(true);
menu.setBounds(340,150,500,500);
menu.setResizable(false);
jfc = new JFileChooser();
jfc.setMultiSelectionEnabled(false);
menu.add(jfc);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
jfc.addActionListener(new ScannerClass());
} //Emd actionPerformed()
} //End MenuHandler
private class ScannerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae){
String command = ae.getActionCommand();
if(command.equals("ApproveSelection")){
// directoryname = jfc.getSelectedFile().getAbsolutePath();
directory = new File(jfc.getSelectedFile().getAbsolutePath());
filter = new FilenameFilter(){
public boolean accept(File dir, String name) {
if(name.endsWith(".avi")|| name.endsWith(".mp4")|| name.endsWith(".mkv")){
return true;
}
return false;
}
};
String[] filename1 = directory.list(filter);
listmodel.clear();
String elim[] = {".","_","avi","dvdrip","xvid","hdtv","vtv","lol"};
for(int j=0;j<filename1.length;j++){
for (int k=0;k<elim.length;k++){
filename1[j] = filename1[j].replace(elim[k]," ");
} //End nested loop
filename1[j]=filename1[j].trim();
}//End J for loop
for(int i = 0; i< filename1.length;i++){
listmodel.add(i,filename1[i]);
}//END of For
menu.dispose();
list.updateUI();
System.out.println(jfc.getSelectedFile().getAbsolutePath());
}else{
menu.dispose();
listmodel.clear();
} //End if
}
}
public static class Database implements ActionListener{
Connection con;
Statement stmt;
Database(){
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/MovieInfo","root","");
stmt = con.createStatement();
}catch(SQLException se){
System.out.println("Could not connect to DB");
se.printStackTrace();
} //End catch
}//End Database()
//@Override
public void actionPerformed(ActionEvent ae){
if(!list.isSelectionEmpty()){
try {
boolean rs = stmt.execute("INSERT INTO movie(mov_name) VALUES ('"+list.getSelectedValue().toString()+"')") ;
} catch (SQLException ex) {
System.out.println("SQL Error");
}
}
}
} //End of Database
private static class ListListener implements ListSelectionListener {
public ListListener() {
}
@Override
public void valueChanged(ListSelectionEvent e){
if(!list.isSelectionEmpty()){
addDB.setEnabled(true);
fileinfodetails = list.getSelectedValue().toString();
}
else{
addDB.setEnabled(false);
}
fileinfo.setText(fileinfodetails);
}
}
}