import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class FindFrame extends JDialog
{
JPanel buttonPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel replacePanel = new JPanel();
JPanel findPanel = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JComboBox findCombo = new JComboBox();
JLabel jLabel2 = new JLabel();
JComboBox replaceCombo = new JComboBox();
GridBagLayout gridBagLayout1 = new GridBagLayout();
GridBagLayout gridBagLayout2 = new GridBagLayout();
JButton nextButton = new JButton();
JButton previousButton = new JButton();
JButton replaceAllButton = new JButton();
JButton closeButton = new JButton();
JPanel jPanel1 = new JPanel();
JCheckBox startCheckbox = new JCheckBox();
//comment to change UI
DBTable dbTable;
//vector which contains the column number which should be searched
Vector columnVector;
Point lastLoc = new Point(0,1);
boolean find = true;
FindFrame(Frame f,DBTable dbTable)
{
super(f,false);
this.dbTable = dbTable;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//end uncomment
FindFrame()
{
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
dbTable.setFnt(this);
mainPanel.setLayout(borderLayout1);
findPanel.setLayout(gridBagLayout1);
replacePanel.setLayout(gridBagLayout2);
jLabel1.setText(dbTable.getString("TextToFind"));
jLabel1.setLabelFor(findCombo);
dbTable.setFnt(jLabel1);
jLabel2.setLabelFor(replaceCombo);
dbTable.setFnt(jLabel2);
nextButton.setText(dbTable.getString("FindNext"));
getRootPane().setDefaultButton(nextButton);
dbTable.setFnt(nextButton);
previousButton.setText(dbTable.getString("FindPrevious"));
dbTable.setFnt(previousButton);
replaceAllButton.setText(dbTable.getString("ReplaceAll"));
dbTable.setFnt(replaceAllButton);
closeButton.setText(dbTable.getString("Close"));
dbTable.setFnt(closeButton);
startCheckbox.setText(dbTable.getString("FromSelectedRow"));
dbTable.setFnt(startCheckbox);
jLabel2.setText(dbTable.getString("ReplaceWith"));
findCombo.setEditable(true);
replaceCombo.setEditable(true);
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.CENTER);
((FlowLayout)buttonPanel.getLayout()).setVgap(3);
buttonPanel.add(nextButton, null);
buttonPanel.add(previousButton, null);
buttonPanel.add(replaceAllButton, null);
buttonPanel.add(closeButton, null);
this.getContentPane().add(mainPanel, BorderLayout.CENTER);
mainPanel.add(findPanel, BorderLayout.NORTH);
findPanel.add(jLabel1, new SimpleGridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(7, 20, 0, 0), 0, 9));
findPanel.add(findCombo, new SimpleGridBagConstraints(1, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 5, 0, 0), 0, 0));
mainPanel.add(replacePanel, BorderLayout.CENTER);
replacePanel.add(jLabel2, new SimpleGridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 9));
replacePanel.add(replaceCombo, new SimpleGridBagConstraints(1, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 5, 0, 0), 0, 0));
mainPanel.add(jPanel1, BorderLayout.SOUTH);
((FlowLayout)jPanel1.getLayout()).setAlignment(FlowLayout.CENTER);
((FlowLayout)jPanel1.getLayout()).setVgap(3);
jPanel1.add(startCheckbox, null);
FindAction findAction = new FindAction();
nextButton.addActionListener(findAction);
previousButton.addActionListener(findAction);
replaceAllButton.addActionListener(findAction);
closeButton.addActionListener(findAction);
startCheckbox.addActionListener(findAction);
findCombo.addActionListener(findAction);
ChangeListener cl = new ChangeListener();
findCombo.addItemListener(cl);
replaceCombo.addItemListener(cl);
}
void resetFont(Font fnt)
{
setFont(fnt);
jLabel1.setFont(fnt);
nextButton.setFont(fnt);
previousButton.setFont(fnt);
replaceAllButton.setFont(fnt);
closeButton.setFont(fnt);
startCheckbox.setFont(fnt);
jLabel2.setFont(fnt);
}
public void setFind()
{
find = true;
replacePanel.setVisible(false);
nextButton.setText(dbTable.getString("FindNext"));
previousButton.setText(dbTable.getString("FindPrevious"));
replaceAllButton.setVisible(false);
setSize(440,150);
}
public void setReplace()
{
find = false;
replacePanel.setVisible(true);
nextButton.setText(dbTable.getString("ReplaceNext"));
previousButton.setText(dbTable.getString("ReplacePrevious"));
replaceAllButton.setVisible(true);
setSize(440,190);
}
//when row selection is changed in main dbtable
//it can be notified to find frame using this method
public void setRow(int row)
{
if( startCheckbox.isSelected())
{
if( row >= 0 )
lastLoc = new Point(row, -1);
else
lastLoc = new Point(0, -1);
}
}
class ChangeListener implements ItemListener{
public void itemStateChanged(ItemEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
String text =(String)cb.getSelectedItem();
if(text == null || "".equals(text))
return;
boolean exists= false;
for(int i=0; i< cb.getItemCount(); i++)
{
if( text.equals(cb.getItemAt(i)))
{
exists=true;
break;
}
}
if(!exists)
cb.addItem(text);
}
}
void findNext()
{
//if user has not entered the find text show error dialog
if( ! validateInput())
return;
String findText = (String)findCombo.getSelectedItem();
//if the column model is changed, recreate the column Vector
refreshColumnVector(dbTable.getTable());
//move the search pointer to next row/column
if( dbTable.lastLoc == -1 )
{
if( lastLoc.y == columnVector.size()-1)
{
lastLoc.x = lastLoc.x +1;
lastLoc.y = 0;
}
else
lastLoc.y = lastLoc.y + 1;
}
lastLoc = dbTable.find(lastLoc.x, lastLoc.y,findText,columnVector, true);
}
class FindAction implements ActionListener{
public void actionPerformed(ActionEvent e)
{
JTable jt = dbTable.getTable();
if(e.getSource() == closeButton)
{
setVisible(false);
return;
}
else if( e.getSource() == startCheckbox)
{
if( startCheckbox.isSelected() )
{
int startRow = jt.getSelectedRow();
if( startRow >= 0 )
lastLoc = new Point(startRow, -1);
else
lastLoc = new Point(0, -1);
}
else
lastLoc = new Point(0, -1);
return;
}
if( find )
{
if( e.getSource() == nextButton || e.getSource() == findCombo)
{
findNext();
}
else if(e.getSource() == previousButton)
{
//if user has not entered the find text show error dialog
if( ! validateInput())
return;
String findText = (String)findCombo.getSelectedItem();
//if the column model is changed, recreate the column Vector
refreshColumnVector(jt);
//move the search pointer to previous row/column
if( dbTable.lastLoc == -1 )
{
if( lastLoc.y == 0)
{
lastLoc.x = lastLoc.x -1;
lastLoc.y = columnVector.size()-1;
}
else
lastLoc.y = lastLoc.y - 1;
}
lastLoc = dbTable.find(lastLoc.x, lastLoc.y,findText,columnVector, false);
}
}
else
{
if( e.getSource() == nextButton || e.getSource() == findCombo)
{
if( ! validateInput())
return;
String findText = (String)findCombo.getSelectedItem();
String replaceText = (String)replaceCombo.getSelectedItem();
//if the column model is changed, recreate the column Vector
refreshColumnVector(jt);
//move the search pointer to next row/column
if( lastLoc.y == columnVector.size()-1)
{
lastLoc.x = lastLoc.x +1;
lastLoc.y = 0;
}
else
lastLoc.y = lastLoc.y + 1;
setVisible(false);
lastLoc = dbTable.replace(lastLoc.x, lastLoc.y,findText, replaceText, columnVector, true);
}
else if(e.getSource() == previousButton)
{
if( ! validateInput())
return;
String findText = (String)findCombo.getSelectedItem();
String replaceText = (String)replaceCombo.getSelectedItem();
//if the column model is changed, recreate the column Vector
refreshColumnVector(jt);
//move the search pointer to previous row/column
if( lastLoc.y == 0)
{
lastLoc.x = lastLoc.x -1;
lastLoc.y = columnVector.size()-1;
}
else
lastLoc.y = lastLoc.y - 1;
setVisible(false);
lastLoc = dbTable.replace(lastLoc.x, lastLoc.y,findText,replaceText,columnVector, false);
}
else if(e.getSource() == replaceAllButton)
{
//if user has not entered the find text show error dialog
String findText = (String)findCombo.getSelectedItem();
if( findText == null || "".equals(findText))
{
//add code to show dialog
return;
}
String replaceText = (String)replaceCombo.getSelectedItem();
if( replaceText == null || "".equals(replaceText))
{
//add code to show dialog
return;
}
//if the column model is changed, recreate the column Vector
refreshColumnVector(jt);
setVisible(false);
lastLoc = dbTable.replaceAll(0,1,findText, replaceText, columnVector);
}
}
jt.repaint();
}
}
private void refreshColumnVector(JTable jt)
{
int colCount = jt.getColumnCount();
if( columnVector == null)
columnVector = new Vector();
if( colCount != columnVector.size() )
{
columnVector.removeAllElements();
for( int i= 0; i< colCount; i++ )
columnVector.addElement(new Integer(i+1));
}
}
//validate whether user has entered a find or replace text
private boolean validateInput()
{
//if user has not entered the find text show error dialog
String findText = (String)findCombo.getSelectedItem();
if( findText == null || "".equals(findText))
{
//add code to show dialog
return false;
}
if( !find )
{
String replaceText = (String)replaceCombo.getSelectedItem();
if( replaceText == null || "".equals(replaceText))
{
//add code to show dialog
return false;
}
}
return true;
}
public void setVisible(boolean visible)
{
if(visible)
{
if( startCheckbox.isSelected() )
{
int startRow = dbTable.getTable().getSelectedRow();
if( startRow >= 0 )
lastLoc = new Point(startRow, -1);
else
lastLoc = new Point(0, -1);
}
else
lastLoc = new Point(0, -1);
center();
toFront();
findCombo.requestDefaultFocus();
findCombo.requestFocus();
}
else
{
dbTable.lastLoc =-1;
}
super.setVisible(visible);
}
public void center()
{
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dim.width / 2 - getSize().width / 2,
dim.height / 2 - getSize().height / 2);
}
}