
import javax.swing.*;
import javax.swing.plaf.*;

import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.util.*;

import javax.swing.plaf.metal.*;

import java.sql.*;
import java.io.*;

import javax.swing.event.*;
import javax.swing.table.*;

import java.awt.print.*;

class NepaGeneratorViaDateOriginal extends JFrame {
	JPanel main = new JPanel();
	Container c = getContentPane();
	private JTable table;
	Connection conn;
	Statement st;
	private Home home = new Home();
	String Txtcmb;
	private int option;
	JButton print;
	JButton cancle;
	private String msAccDB;
	private String dbURL;
	private JComboBox<String> cmbDay;
	private ResultSet rs;
	private int i;
	private JTextField txtStartDate;
	private JTextField txtStopDate;
	private String ASC;
	private DefaultTableModel model;
	private String confirm;
	private String confirm1;
	private boolean check;

	NepaGeneratorViaDateOriginal() throws Exception {
			connectiondatabase();

	}

void dialogShow() throws SQLException{
	cmbDay = new JComboBox();
	txtStartDate = new JTextField("01/09/2017");
	
	Object[] message = { "Enter Date", txtStartDate};
	String query = "SELECT * FROM site";
	rs = st.executeQuery(query);
	while (rs.next()) {
		Txtcmb = rs.getString("site").trim();
		cmbDay.addItem(Txtcmb);
	}
	option = JOptionPane.showConfirmDialog(null, message, "",
			JOptionPane.CLOSED_OPTION);
	
	fetchData();
	
}
	void fetchData(){
		
		try{
		boolean val;

		String startDate = txtStartDate.getText();
		String d = (String) cmbDay.getSelectedItem();
		rs = st.executeQuery("Select * from generatorNepa where nepaonofdate like '"
				+ startDate + "'  ");
		
		int row = 0;
		int i = 0;
		

		while (rs.next()) {

			row++;
		}
		model = new DefaultTableModel(new String[] { "Date Generated",
				"Time Generated", "NEPA Time ON", "NEPA Time Of",
				"NEPA Run Hour(s)", "GEN. Time ON", "GEN. Time Of",
				"GEN. Run Hour(s)", "Remark", }, row);

		rs = st.executeQuery("Select * from generatorNepa where nepaonofdate like '"
				+ startDate + "'  ");
 check = rs.next();
if ((!check == false) && (option == JOptionPane.OK_OPTION )) {
	

 while (check) {
			
				model.setValueAt(rs.getString(2).trim(), i, 0);
				model.setValueAt(rs.getString(3).trim(), i, 1);
				model.setValueAt(rs.getString(4).trim(), i, 2);
				model.setValueAt(rs.getString(5).trim(), i, 3);
				model.setValueAt(rs.getString(6).trim(), i, 4);
				model.setValueAt(rs.getString(9).trim(), i, 5);
				model.setValueAt(rs.getString(10).trim(), i, 6);
				model.setValueAt(rs.getString(11).trim(), i, 7);
				model.setValueAt(rs.getString(12).trim(), i, 8);

				i++;
				 setVisibility();
			}
			

			
			
		
		}
 
 else{
JOptionPane.showMessageDialog(null, "Return null value, No data for specified details");	
new Home().setVisible(true);
 }
 }
			catch (Exception e) {
				//e.printStackTrace();
			}}
		
	void setVisibility(){
		//setVisible(false);
		setSize(1320, 520);
		setTitle("Nepa|Generator Operation List  Via Date");
		setLocation(12, 10);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {

			}
		});
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				"images//mainicon.png"));
		c = getContentPane();

		main.setLayout(new BorderLayout());
		main.setBackground(new Color(245, 240, 255));
		JLabel l = new JLabel(
				"<html><font size=6 color=#800080><i>Nepa|Generator Operation List");
		JPanel title = new JPanel() {
			public void paintComponent(Graphics g) {

				Toolkit kit = Toolkit.getDefaultToolkit();
				Image img = kit.getImage("images//Gradien.jpg");

				MediaTracker t = new MediaTracker(this);
				t.addImage(img, 1);
				while (true) {
					try {
						t.waitForID(1);
						break;
					} catch (Exception e) {
					}
				}
				g.drawImage(img, 0, 0, 1350, 50, null);
			}
		};
		title.add(l);
		main.add("North", title);
		Icon prt = new ImageIcon("images//PRINT.png");
		print = new JButton("Print", prt);
		print.setToolTipText("Print");
		cancle = new JButton("Exit");
		cancle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				Home sa = new Home();
				sa.setLocationRelativeTo(null);
				sa.setVisible(true);

			}
		});
		print.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					boolean complete = table.print();
					if (complete) {
						/* show a success message */

						JOptionPane.showMessageDialog(null,
								"Printing was cancelled");

					} else {
						/* show a message indicating that printing was cancelled */
						JOptionPane.showMessageDialog(null,
								"Printing was cancelled");

					}
				} catch (PrinterException pe) {
					/* Printing failed, report to the user */

					JOptionPane.showMessageDialog(null, "printing failed");
				}

			}
		});
		

		cancle.setToolTipText("Exit");
		JPanel butpan = new JPanel();
		butpan.add(print);
		butpan.add(cancle);
		butpan.setBackground(new Color(255, 197, 68));
		c.add("South", butpan);
		
		table = new JTable(model);

		JScrollPane sp = new JScrollPane(table);
		main.add(sp);
		table.setSelectionMode(0);
		table.setFont(new Font("Times New Roman", Font.BOLD, 15));
		table.setForeground(Color.BLACK);
		table.setGridColor(new Color(0, 128, 192));
		table.setRowHeight(35);
		table.getTableHeader().setFont(
				new Font("SansSerif", Font.ITALIC, 14));
		table.setEnabled(false);
		table.getTableHeader().setReorderingAllowed(false);
		c.add(main);
		setVisible(true);
		
		
		
	}
	
	void connectiondatabase() throws Exception {
		 msAccDB = "C://Java_Projects_Workspace//BREM//brem_database.accdb";
			dbURL = "jdbc:ucanaccess://" + msAccDB;
			conn = DriverManager.getConnection(dbURL
					+ ";jackcessOpener=CryptCodecOpener", "", "brem");
			st = conn.createStatement(); 
		dialogShow();
		
	}

public static void main(String args[]) throws Exception {
		NepaGeneratorViaDateOriginal sa1 = new NepaGeneratorViaDateOriginal();

	}

}
