/*
 * @(#)FindPanel.java  1.0  2002-02-11
 *
 * Copyright (c) 2002 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.view;

import javax.swing.*;
import java.awt.*;

/**
 * A panel for input of a search string and search options.
 *
 * @author  Werner Randelshofer
 * @version 1.0 2002-02-11
 */
public class FindPanel extends JComponent {
    /**
     * Return value if cancel (close) is chosen.
     */
    public static final int CANCEL_OPTION = 1;

    /**
     * Return value if approve (yes, ok) is chosen.
     */
    public static final int APPROVE_OPTION = 0;
    
    /**
     * The return value from the last invocation
     * of show dialog.
     */
    private int returnValue = CANCEL_OPTION;
    
    /**
     * The dialog panel used for the show dialog method.
     */
    private JDialog dialog;
    
    /** Creates new form TextSearchPanel */
    public FindPanel() {
        initComponents();
    }
    
    public String getFindString() {
        return findTextField.getText();
    }
    public void setFindString(String newValue) {
         findTextField.setText(newValue);
    }
    public void setMatchCase(boolean b) {
        caseCheckBox.setSelected(b);
    }
    public boolean isMatchCase() {
        return caseCheckBox.isSelected();
    }
    public void setMatchWord(boolean b) {
        wordCheckBox.setSelected(b);
    }
    public boolean isMatchWord() {
        return wordCheckBox.isSelected();
    }
    
    /**
     * Pops up a find dialog.
     *
     * @param   title The title of the dialog.
     * @return  the return state of the file chooser on popdown:
     *             CANCEL_OPTION, APPROVE_OPTION
     */
    public int showDialog(Component parent, String title) {
        returnValue = CANCEL_OPTION;
        
        Frame frame = parent instanceof Frame ? (Frame) parent
              : (Frame)SwingUtilities.getAncestorOfClass(Frame.class, parent);

        dialog = new JDialog(frame, title, true);
        Container contentPane = dialog.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(this, BorderLayout.CENTER);
        findTextField.selectAll();
        dialog.getRootPane().setDefaultButton(findButton);
 
        dialog.pack();
        dialog.setLocationRelativeTo(parent);

        dialog.setVisible(true);
	return returnValue;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        findTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        caseCheckBox = new javax.swing.JCheckBox();
        wordCheckBox = new javax.swing.JCheckBox();
        jPanel1 = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();
        findButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(12, 12, 12, 12)));
        jLabel1.setText("Find:");
        jLabel1.setLabelFor(findTextField);
        jLabel1.setRequestFocusEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabel1, gridBagConstraints);

        findTextField.setColumns(20);
        findTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                find(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0);
        add(findTextField, gridBagConstraints);

        jLabel2.setText("Options:");
        jLabel2.setRequestFocusEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(3, 0, 0, 0);
        add(jLabel2, gridBagConstraints);

        caseCheckBox.setText("Match Case");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 0, 0);
        add(caseCheckBox, gridBagConstraints);

        wordCheckBox.setText("Match Whole Words Only");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 12, 0, 0);
        add(wordCheckBox, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        closeButton.setText("Close");
        closeButton.setDefaultCapable(false);
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(closeButton, gridBagConstraints);

        findButton.setText("Find");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                find(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(findButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jPanel1, gridBagConstraints);

    }//GEN-END:initComponents

    private void close(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close
	returnValue = CANCEL_OPTION;
	if(dialog != null) {
	    dialog.setVisible(false);
	}
    }//GEN-LAST:event_close

    private void find(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_find
	returnValue = APPROVE_OPTION;
	if(dialog != null) {
	    dialog.setVisible(false);
	}
        }//GEN-LAST:event_find
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton findButton;
    private javax.swing.JCheckBox caseCheckBox;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField findTextField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JCheckBox wordCheckBox;
    // End of variables declaration//GEN-END:variables
    
}
