/*************************************************************************************

This file is part of FragMend.

Written by Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.

Copyright (c) 2006, Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.     
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.       
    * Neither the name of Florian Buchholz, Glenn Henderson, David Horvath, Jeff
      Jones, nor the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*************************************************************************************/

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RangeDlg extends JDialog implements ActionListener {

	private static RangeDlg instance;
	private static RangeParameters return_value = null;
	
	private JButton btn_ok;
	private JButton btn_cancel;
	private static JTextField start_field;
	private static JTextField end_field;
	private static JTextField count_field;
	private static JTextField prefix_field;
	private static JTextField suffix_field;
	private static JTextField directory_field;
	
	private RangeDlg(Frame frame) {
		
		super(frame, "Enter range data", true);

		btn_cancel = new JButton("Cancel");
		btn_cancel.addActionListener(this);
		btn_ok = new JButton("Ok");
		btn_ok.addActionListener(this);
		getRootPane().setDefaultButton(btn_ok);

		JPanel fieldPane = new JPanel();
		fieldPane.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 5, 5, 5);
        
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.PAGE_START;
		JLabel label = new JLabel("Start sector: ", JLabel.TRAILING);
		fieldPane.add(label, c);

		c.gridx = 1;
		start_field = new JTextField(10);
		fieldPane.add(start_field, c);
		
		c.gridx = 0;
		c.gridy++;
		c.anchor = GridBagConstraints.PAGE_START;
		label = new JLabel("End sector: ", JLabel.TRAILING);
		fieldPane.add(label, c);

		c.gridx = 1;
		end_field = new JTextField();
		fieldPane.add(end_field, c);
		
		c.gridx = 0;
		c.gridy++;
		c.anchor = GridBagConstraints.PAGE_START;
		label = new JLabel("Number of sectors to remove: ", JLabel.TRAILING);
		fieldPane.add(label, c);

		c.gridx = 1;
		count_field = new JTextField();
		fieldPane.add(count_field, c);
		
		c.gridx = 0;
		c.gridy++;
		c.anchor = GridBagConstraints.PAGE_START;
		label = new JLabel("Filename prefix: ", JLabel.TRAILING);
		fieldPane.add(label, c);

		c.gridx = 1;
		prefix_field = new JTextField();
		fieldPane.add(prefix_field, c);
		
		c.gridx = 0;
		c.gridy++;
		c.anchor = GridBagConstraints.PAGE_START;
		label = new JLabel("Filename suffix: ", JLabel.TRAILING);
		fieldPane.add(label, c);

		c.gridx = 1;
		suffix_field = new JTextField();
		fieldPane.add(suffix_field, c);
		
		c.gridx = 0;
		c.gridy++;
		c.anchor = GridBagConstraints.PAGE_START;
		label = new JLabel("Directory: ", JLabel.TRAILING);
		fieldPane.add(label, c);

		c.gridx = 1;
		directory_field = new JTextField();
		fieldPane.add(directory_field, c);
		

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 10));
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(btn_ok);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPane.add(btn_cancel);
		buttonPane.add(Box.createHorizontalGlue());

                Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
	        contentPane.add(fieldPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.PAGE_END);
		pack();
		
	}
	
	public static RangeParameters showDialog(Frame frame, int start, int end, String name, String default_suffix) {
		
		if (instance == null)
			instance = new RangeDlg(frame);
		
		return_value = null;
		
		start_field.setText(Integer.toString(start));
		end_field.setText(Integer.toString(end));
		count_field.setText("1");
		prefix_field.setText(name);
		suffix_field.setText(default_suffix);
		directory_field.setText(".");
		
		instance.setVisible(true);
		
		return return_value;
	}
	
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
        
		if(source == btn_ok) {
			
			try {
			
				return_value = new RangeParameters(new Integer(start_field.getText()).intValue(),
							new Integer(end_field.getText()).intValue(), 
							new Integer(count_field.getText()).intValue(),
							directory_field.getText(),
							prefix_field.getText(),
							suffix_field.getText());
			}
			catch (NumberFormatException nfe) {
				return_value = null;
			}
			instance.setVisible(false);
		}
		else if (source == btn_cancel) {
			return_value = null;
			instance.setVisible(false);
		}
	}
	
	public class RangeParameters {
		
		private int start;
		private int end;
		private int count;
		private String directory;
		private String prefix;
		private String suffix;
		
		public RangeParameters(int s, int e, int c,
			String dir, String pre, String suf) {
			
			this.start = s;
			this.end = e;
			this.count = c;
			this.directory = dir;
			this.prefix = pre;
			this.suffix = suf;
			
		}
		
		public int getStart() {
			return start;
		}
		
		public int getEnd() {
			return end;
		}
		
		public int getCount() {
			return count;
		}
		
		public String getDirectory() {
			return directory;
		}
		
		public String getPrefix() {
			return prefix;
		}
		
		public String getSuffix() {
			return suffix;
		}
		
	}
}

