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

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.Component;
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 java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FilterDlg extends JDialog
    implements ActionListener, ChangeListener, KeyListener {

    public static final int MODE_FILTER = 0;
    public static final int MODE_SEARCH = 1;
    public static final int MODE_SELECT = 2;

    private static FilterDlg dialog;
    private static FragmentFilter filter = null;
    private Vector<FilterItemDisplay> fields;
    
    private FragmentView frag_view;
    private JButton nextButton, prevButton;
    private int mode;


    public static FragmentFilter showDialog(Component frameComp,
				   Component locationComp,
				   String title) {

	Frame frame = JOptionPane.getFrameForComponent(frameComp);
        dialog = new FilterDlg(frame, locationComp, title,
				 true, MODE_FILTER, null);

        dialog.setVisible(true);
        return filter;

    }

    public FilterDlg(Frame frame,
		       Component locationComp,
		       String title,
		       boolean modal,
		       int m,
		       FragmentView fv) {

        super(frame, title, modal);

	fields = new Vector<FilterItemDisplay>();
	
	mode = m;

	if (mode == MODE_SEARCH)
	    dialog = this;

	frag_view = fv;

        JPanel fieldPane = new JPanel();
        fieldPane.setLayout(new BoxLayout(fieldPane, BoxLayout.PAGE_AXIS));
	
	fields.add(new FilterItemDisplay("Sector #", Fragment.FIELD_ID, FilterItemDisplay.TYPE_RANGE));
	fields.add(new FilterItemDisplay("Fragment name", Fragment.FIELD_NAME, FilterItemDisplay.TYPE_STRING));
	fields.add(new FilterItemDisplay("Printable characters", Fragment.FIELD_PRINT_COUNT, FilterItemDisplay.TYPE_INTEGER));
	fields.add(new FilterItemDisplay("Zero padded", Fragment.FIELD_ZERO_PADDED, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains JPG Header", Fragment.FIELD_JPG_HEADER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains JPG Footer", Fragment.FIELD_JPG_FOOTER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("JPG Markers", Fragment.FIELD_JPG_MARKERS, FilterItemDisplay.TYPE_INTEGER));
	fields.add(new FilterItemDisplay("Contains ZIP Header", Fragment.FIELD_ZIP_HEADER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains ZIP Footer", Fragment.FIELD_ZIP_FOOTER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains HTML Header", Fragment.FIELD_HTML_HEADER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains HTML Footer", Fragment.FIELD_HTML_FOOTER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains Office Header", Fragment.FIELD_OFFICE_HEADER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains Office Footer", Fragment.FIELD_OFFICE_FOOTER, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Contains Office Root Directory", Fragment.FIELD_OFFICE_ROOTDIR, FilterItemDisplay.TYPE_TRUE_FALSE));
	fields.add(new FilterItemDisplay("Bracket count", Fragment.FIELD_BRACKET_COUNT, FilterItemDisplay.TYPE_INTEGER));
	fields.add(new FilterItemDisplay("32-bit int sequence count", Fragment.FIELD_SEQUENCE_COUNT, FilterItemDisplay.TYPE_INTEGER));
	
	for (Enumeration<FilterItemDisplay> elements = fields.elements(); elements.hasMoreElements();)
		fieldPane.add(elements.nextElement());
	
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));

	if (mode == MODE_FILTER) {

	    JButton cancelButton = new JButton("Cancel");
	    cancelButton.addActionListener(this);

	    JButton okButton = new JButton("Filter");
	    okButton.setActionCommand("Filter");
	    okButton.addActionListener(this);

	    getRootPane().setDefaultButton(okButton);
	    buttonPane.add(Box.createHorizontalGlue());
	    buttonPane.add(okButton);
	    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
	    buttonPane.add(cancelButton);
	    buttonPane.add(Box.createHorizontalGlue());

	}

	else if (mode == MODE_SEARCH) {

	    nextButton = new JButton("Next");
	    nextButton.setActionCommand("Next");
	    nextButton.addActionListener(this);

	    prevButton = new JButton("Prev");
	    prevButton.setActionCommand("Prev");
	    prevButton.addActionListener(this);

	    JButton cancelButton = new JButton("Cancel");
	    cancelButton.addActionListener(this);

	    getRootPane().setDefaultButton(nextButton);
	    buttonPane.add(Box.createHorizontalGlue());
	    buttonPane.add(prevButton);
	    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
	    buttonPane.add(nextButton);
	    buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
	    buttonPane.add(cancelButton);
	    buttonPane.add(Box.createHorizontalGlue());
	    
	    

	}

        Container contentPane = getContentPane();
        contentPane.add(fieldPane, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.PAGE_END);

        pack();
	setLocationRelativeTo(locationComp);

    }

    public void actionPerformed(ActionEvent e) {

        if ("Filter".equals(e.getActionCommand())) {
	    filter = buildFilter();
	    FilterDlg.dialog.setVisible(false);
        }	
        else if ("Next".equals(e.getActionCommand())) {
		frag_view.findNextFragment(buildFilter());
	}
        else if ("Prev".equals(e.getActionCommand())) {
		frag_view.findPreviousFragment(buildFilter());
	}
        else {
	    FilterDlg.dialog.setVisible(false);
	}

    } // actionPeformed

    public void stateChanged(ChangeEvent e) {

	if (mode == MODE_SEARCH) {

	    nextButton.setEnabled(true);  
	    prevButton.setEnabled(true);  

	}

    }

    public void keyTyped(KeyEvent e) {

	if (mode == MODE_SEARCH) {

	    nextButton.setEnabled(true);  
	    prevButton.setEnabled(true);  

	}

    }

    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}


    private FragmentFilter buildFilter() {

	    Vector<FilterItem> active_items = new Vector<FilterItem>();
	    
	    for (Enumeration<FilterItemDisplay> elements = fields.elements(); elements.hasMoreElements();){
		    FilterItemDisplay display = elements.nextElement();
		    if (display.isActive())
			    active_items.add(display.getFilterItem());
	    }
	    
	    FilterItem[] items = new FilterItem[0];

	    return new FragmentFilter(active_items.toArray(items));
	
    }

}

