4 Replies - 6634 Views - Last Post: 14 July 2012 - 08:01 PM Rate Topic: -----

#1 Sachan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-July 12

Gui program to write/get file to/from database using the submit button

Posted 14 July 2012 - 12:49 PM

[/package oplogger.server.ui;

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.Box;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import oplogger.server.OperationalProfileDatabase;
import oplogger.server.OperationalProfileServer;
import oplogger.server.reportgen.OperationalProfileReportGenerator;
import oplogger.server.reportgen.OperationalProfileReportGeneratorFactory;
import oplogger.server.reportgen.SimpleOperationalProfileReportGeneratorFactory;
import javax.swing.SwingConstants;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JSeparator;
import java.awt.Color;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class AdministrationGui extends JFrame implements Runnable{

    /* Constants */
    
//    /*
//     * The menu option the user will choose to produce a profile report.
//     */
//    private static final String PRODUCE_PROFILE_REPORT_MENU_OPTION = "1";
//    
//    /**
//     * The menu option the user will choose to shutdown the server.
//     */
//    private static final String SHUTDOWN_SERVER_MENU_OPTION = "2";
        
//    /**
//     * The menu displayed to the user prompting for their choice.
//     */
//    private static final String MENU_PROMPT =
//        PRODUCE_PROFILE_REPORT_MENU_OPTION + ". Produce Profile Report and write Profile to Database\n" +
//        SHUTDOWN_SERVER_MENU_OPTION + ". Shutdown Server\n" +
//        "Enter Choice: ";
    
//    /**
//     * The prompt displayed to the user requesting the name of a file to save
//     * an operational profile report to.
//     */
//    private static final String REPORT_FILE_NAME_PROMPT = 
//        "Enter Report File Name: ";
    
//    /**
//     * The error message displayed when the user enters an invalid menu option.
//     */
//    private static final String INVALID_MENU_OPTION_ERROR_MESSAGE = 
//        "Invalid menu option";
    
    /**
     * The error message displayed when there was an error saving the profile
     * report to a file.
     */
    private static final String UNABLE_TO_WRITE_PROFILE_REPORT_ERROR_MESSAGE = 
        "Error: Unable to write profile report to file ";
    
    /**
     * The error message displayed when there was an error receiving the user's
     * input from the terminal.
     */
    private static final String UNABLE_TO_READ_FROM_TERMINAL_ERROR_MESSAGE = 
        "Error: Unable to read from terminal";
    
    /* Variables */
    private JPanel contentPane;
    private JLabel label;
    private JTextArea txtNotify;
    private OperationalProfileServer server;
    private OperationalProfileDatabase database;
    private Display display;
    private UserInputManager inputReader;
    Boolean isReportCheckBoxEnabled;
    Boolean isDBCheckBoxEnabled;
    Boolean isReportCheckBoxSelected;
    Boolean isDBCheckBoxSelected;
    String fileChoice = null;
    
    /* Constructors */
    
    /**
     * Constructs a new Gui and associates it with the server
     * receiving operational profiles, the database where the profiles are being
     * stored, the display to display user prompts to, and the source of the
     * user's responses to those prompts. This method creates and defines the visible components of the AdministrationGUI.
     * 
     * @param server the server receiving operational profiles
     * @param database the database of operational profiles
     * @param display the display to display prompts to the user
     * @param inputReader the source of user input
     */
    
	public AdministrationGui(OperationalProfileServer server, OperationalProfileDatabase database, Display display, UserInputManager inputReader) {
		String text = "";
		setServer(server);
		setDisplay(display);
		setInputReader(inputReader);
		text = text + display;// delete this when you solve the notification issue.
		
		//Create the content pane using the GridBagLayout
		setTitle("OPlogger Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 527, 399);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{73, 199, 23, 115, 0};
		gbl_contentPane.rowHeights = new int[]{234, 0, 0, 0, 0, 23, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
				
				//Add a panel to the content pane
				JPanel panel = new JPanel();
				GridBagConstraints gbc_panel = new GridBagConstraints();
				gbc_panel.anchor = GridBagConstraints.NORTHWEST;
				gbc_panel.gridwidth = 4;
				gbc_panel.insets = new Insets(0, 0, 5, 0);
				gbc_panel.gridx = 0;
				gbc_panel.gridy = 0;
				contentPane.add(panel, gbc_panel);
				panel.setAlignmentY(Component.TOP_ALIGNMENT);
				GridBagLayout gbl_panel = new GridBagLayout();
				gbl_panel.columnWidths = new int[]{46, 281, 6, 0};
				gbl_panel.rowHeights = new int[]{14, 133, 20, 0};
				gbl_panel.columnWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
				gbl_panel.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
				panel.setLayout(gbl_panel);
				
				//Set the label for the notifications text area
				JLabel lblNewLabel = new JLabel("Notifications");
				GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
				gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
				gbc_lblNewLabel.gridx = 0;
				gbc_lblNewLabel.gridy = 0;
				panel.add(lblNewLabel, gbc_lblNewLabel);
				lblNewLabel.setLabelFor(panel);
				lblNewLabel.setAlignmentY(Component.TOP_ALIGNMENT);
				lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
				
				//Create the text area that will display the messages previously displayed in the terminal
				JTextArea txtNotify = new JTextArea();
				GridBagConstraints gbc_txtNotify = new GridBagConstraints();
				gbc_txtNotify.gridheight = 2;
				gbc_txtNotify.gridwidth = 3;
				gbc_txtNotify.anchor = GridBagConstraints.NORTHWEST;
				gbc_txtNotify.insets = new Insets(0, 0, 5, 5);
				gbc_txtNotify.gridx = 0;
				gbc_txtNotify.gridy = 1;
				panel.add(txtNotify, gbc_txtNotify);
				txtNotify.setEditable(false);
				txtNotify.append(text);//this is not right... needs to be something dynamic to append any change to the display to the notification box
				
				//Add a label to separate the notifications from the reports selection controls
				JLabel lblGenerateReports = new JLabel("Generate Reports");
				GridBagConstraints gbc_lblGenerateReports = new GridBagConstraints();
				gbc_lblGenerateReports.insets = new Insets(0, 0, 5, 5);
				gbc_lblGenerateReports.gridx = 0;
				gbc_lblGenerateReports.gridy = 2;
				contentPane.add(lblGenerateReports, gbc_lblGenerateReports);
				
				//Add the checkbox to let the user choose to create a text file of the Operation Profile Reports
				JCheckBox ReportCheckBox = new JCheckBox("Send Report to Selected File");
				GridBagConstraints gbcReportCheckBox = new GridBagConstraints();
				gbcReportCheckBox.anchor = GridBagConstraints.WEST;
				gbcReportCheckBox.insets = new Insets(0, 0, 5, 5);
				gbcReportCheckBox.gridx = 1;
				gbcReportCheckBox.gridy = 3;
				contentPane.add(ReportCheckBox, gbcReportCheckBox);
				ReportCheckBox.setHorizontalAlignment(SwingConstants.LEFT);
				ReportCheckBox.setSelected(true);
				ReportCheckBox.setEnabled(false);
			
				isReportCheckBoxEnabled = ReportCheckBox.isEnabled();
				isReportCheckBoxSelected = ReportCheckBox.isSelected();
				
				JButton btnBrowse = new JButton("Browse");
				GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
				gbc_btnBrowse.anchor = GridBagConstraints.WEST;
				gbc_btnBrowse.insets = new Insets(0, 0, 5, 5);
				gbc_btnBrowse.gridx = 2;
				gbc_btnBrowse.gridy = 3;
				contentPane.add(btnBrowse, gbc_btnBrowse);
				btnBrowse.addActionListener(new fileChooserHandler());
				label = new JLabel("Path:");
				GridBagConstraints gbc_label = new GridBagConstraints();
				gbc_label.anchor = GridBagConstraints.WEST;
				gbc_label.insets = new Insets(0, 0, 5, 0);
				gbc_label.gridx = 3;
				gbc_label.gridy = 3;
				contentPane.add(label, gbc_label);
				
				//Create the checkbox to allow the user to write the Operational Profile report to the database.
				JCheckBox DBCheckBox = new JCheckBox("Send Report to Database");
				DBCheckBox.setHorizontalAlignment(SwingConstants.LEFT);
				GridBagConstraints gbc_chckbxNewCheckBox = new GridBagConstraints();
				gbc_chckbxNewCheckBox.anchor = GridBagConstraints.WEST;
				gbc_chckbxNewCheckBox.insets = new Insets(0, 0, 5, 5);
				gbc_chckbxNewCheckBox.gridx = 1;
				gbc_chckbxNewCheckBox.gridy = 4;
				contentPane.add(DBCheckBox, gbc_chckbxNewCheckBox);
				DBCheckBox.setSelected(true);
				
				isDBCheckBoxEnabled = DBCheckBox.isEnabled();
				isDBCheckBoxSelected = DBCheckBox.isSelected();
				
				//Create a button to trigger an event that checks the checkboxes, file path and name and generates the selected reports.
				JButton btnSubmit = new JButton("Submit");
				GridBagConstraints gbc_btnSubmit = new GridBagConstraints();
				gbc_btnSubmit.anchor = GridBagConstraints.WEST;
				gbc_btnSubmit.insets = new Insets(0, 0, 0, 5);
				gbc_btnSubmit.gridx = 1;
				gbc_btnSubmit.gridy = 5;
				contentPane.add(btnSubmit, gbc_btnSubmit);
				btnSubmit.addActionListener(new submitHandler());
				
				JButton btnStopServer = new JButton("Stop Server");
				GridBagConstraints gbc_btnStopServer = new GridBagConstraints();
				gbc_btnStopServer.gridx = 3;
				gbc_btnStopServer.gridy = 5;
				contentPane.add(btnStopServer, gbc_btnStopServer);
				btnStopServer.addActionListener(new serverHandler());
	}
	
	
	public void runGui(){
		AdministrationGui frame = new AdministrationGui(this.server, this.database, this.display, this.inputReader);
        frame.pack();
        frame.setVisible(true);
	}

	
    /**
     * Prompts the user for a filename and saves a formatted report of the 
     * operational profiles to a new file with that name.
     */
    private void produceProfileReport() {
        OperationalProfileReportGeneratorFactory reportGeneratorFactory = 
            SimpleOperationalProfileReportGeneratorFactory.getInstance();
        OperationalProfileReportGenerator reportGenerator = 
            reportGeneratorFactory.newReportGenerator();
        try {
            fileChoice = inputReader.getFilename();
            txtNotify.append("Report will be written to the selected file: " + fileChoice);
            
        } catch (IOException ioe) {
            display.displayMessage(UNABLE_TO_READ_FROM_TERMINAL_ERROR_MESSAGE);
            return;
        } // try...catch (IOException)
        
        try {
            FileWriter writer = new FileWriter(fileChoice);
            writer.write(reportGenerator.generateReport(database));
            writer.flush();
            writer.close();
        } catch (IOException ioe) {
            display.displayMessage(
                UNABLE_TO_WRITE_PROFILE_REPORT_ERROR_MESSAGE + fileChoice);
        } // try...catch (IOException)

    } // void produceProfileReport()

    /**
     * Shuts down the current operational profile server.
     */
    public void shutdownServer() {
        server.stopServer();        
    } // void shutdownServer()

    /**
     * Starts the Gui running as part of a Thread.
     */
    public void run() {
        runGui();
    } // void run()
    
    /**
     * Associates the Gui with the database of operational
     * profiles.
     * 
     * @param database the database of operational profiles
     */
    public void setDatabase(OperationalProfileDatabase database) {
        this.database = database;
    } // void setDatabase(OperationalProfileDatabase)
    
    /**
     * Associates the AdministrationGui with the display that it will use 
     * to display the menu and prompt for the filename of a profile report.
     * 
     * @param display the display to display prompts to the user
     */
    public void setDisplay(Display display) {
        this.display = display;
    } // void setDisplay(Display)
    
    /**
     * Associates the AdministrationGui with the source to read user input
     * from.
     * 
     * @param inputReader the source of user input
     */
    public void setInputReader(UserInputManager inputReader) {
        this.inputReader = inputReader;
    } // void setInputReader(UserInputManager)
    
    /**
     * Associates the AdministrationGui with the server that it 
     * administers.
     * 
     * @param server the server receiving operational profiles
     */
    public void setServer(OperationalProfileServer server) {
        this.server = server;
    } // void setServer(OperationalProfileServer)
    
    public void runFileChooser(){
		JFileChooser chooser = new JFileChooser();
	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
	        "Text Files", "txt");
	    chooser.setFileFilter(filter);
	    Component parent = null;
		int returnVal = chooser.showOpenDialog(parent);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	       fileChoice = new String(chooser.getSelectedFile().getName());
	       isReportCheckBoxEnabled = true;
	    }
    }
    
    /**
     * Opens the FileChooser dialog when the AdministrationGui Browse button is clicked.
     */
	private class fileChooserHandler implements ActionListener {
		
		public void actionPerformed(ActionEvent e){
			runFileChooser();
	}
	}
	
	private class serverHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			shutdownServer();
		    }
		}
	
	private class submitHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			produceProfileReport();
		}
	}
	}
]


*** Edit ***
Please use code tags when posting code
Posted Image

This post has been edited by GunnerInc: 14 July 2012 - 12:52 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Gui program to write/get file to/from database using the submit button

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Gui program to write/get file to/from database using the submit button

Posted 14 July 2012 - 12:52 PM

Please read the "New to Dream.In.Code FAQ" to learn how to post here.

Welcome, but your post is difficult to deal with.
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Gui program to write/get file to/from database using the submit button

Posted 14 July 2012 - 12:52 PM

Is there a problem? Errors? Wrong output/displa? Please help us help you.
Was This Post Helpful? 0
  • +
  • -

#4 Sachan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-July 12

Re: Gui program to write/get file to/from database using the submit button

Posted 14 July 2012 - 12:56 PM

View PostGunnerInc, on 14 July 2012 - 12:52 PM, said:

Is there a problem? Errors? Wrong output/displa? Please help us help you.




When the code is ran the gui comes up you select a file but needing to have that file go to the database when i hit submit. Im very new to this and so confused
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Gui program to write/get file to/from database using the submit button

Posted 14 July 2012 - 08:01 PM

What is your question ?
Don't post it into the Topic Title as it may be truncated.

Anyhow posting 370 lines of code and:
"Gui program to write/get file to/from database using the submit button"
is not a question
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1