// Copyright 2004-2014 Jim Voris
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package com.qumasoft.guitools.compare;
import com.qumasoft.guitools.AbstractQVCSCommandDialog;
import javax.swing.JOptionPane;
/**
* Search dialog.
* @author Jim Voris
*/
public class SearchDialog extends AbstractQVCSCommandDialog {
private static final long serialVersionUID = 4382689020724817028L;
private final CompareFrame parentCompareFrame;
SearchDialog(CompareFrame parent) {
super(parent, true);
parentCompareFrame = parent;
initComponents();
center();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents()
{
m_SearchForStringLabel = new javax.swing.JLabel();
m_SearchForStringTextArea = new javax.swing.JTextArea();
m_IgnoreCaseCheckBox = new javax.swing.JCheckBox();
m_FindNextButton = new javax.swing.JButton();
m_FindPreviousButton = new javax.swing.JButton();
m_CloseButton = new javax.swing.JButton();
m_RightBottomAnchor = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Search for string");
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent evt)
{
closeDialog(evt);
}
});
m_SearchForStringLabel.setFont(new java.awt.Font("Arial", 0, 12));
m_SearchForStringLabel.setText("Search for this string:");
m_SearchForStringTextArea.setFont(new java.awt.Font("Arial", 0, 12));
m_SearchForStringTextArea.setToolTipText("Enter string to search for");
m_SearchForStringTextArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
m_SearchForStringTextArea.setMaximumSize(new java.awt.Dimension(2, 17));
m_SearchForStringTextArea.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyReleased(java.awt.event.KeyEvent evt)
{
m_SearchForStringTextAreaKeyReleased(evt);
}
});
m_IgnoreCaseCheckBox.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_IgnoreCaseCheckBox.setText("Ignore case");
m_IgnoreCaseCheckBox.setToolTipText("Match upper or lower case");
m_FindNextButton.setFont(new java.awt.Font("Arial", 0, 12));
m_FindNextButton.setText("Find Next");
m_FindNextButton.setMaximumSize(new java.awt.Dimension(80, 25));
m_FindNextButton.setMinimumSize(new java.awt.Dimension(80, 25));
m_FindNextButton.setPreferredSize(new java.awt.Dimension(80, 25));
m_FindNextButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
m_FindNextButtonActionPerformed(evt);
}
});
m_FindPreviousButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
m_FindPreviousButton.setText("Find Previous");
m_FindPreviousButton.setMaximumSize(new java.awt.Dimension(80, 25));
m_FindPreviousButton.setMinimumSize(new java.awt.Dimension(80, 25));
m_FindPreviousButton.setPreferredSize(new java.awt.Dimension(80, 25));
m_FindPreviousButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
m_FindPreviousButtonActionPerformed(evt);
}
});
m_CloseButton.setFont(new java.awt.Font("Arial", 0, 12));
m_CloseButton.setText("Close");
m_CloseButton.setMaximumSize(new java.awt.Dimension(80, 25));
m_CloseButton.setMinimumSize(new java.awt.Dimension(80, 25));
m_CloseButton.setPreferredSize(new java.awt.Dimension(80, 25));
m_CloseButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
m_CloseButtonActionPerformed(evt);
}
});
m_RightBottomAnchor.setFont(new java.awt.Font("Arial", 0, 12));
m_RightBottomAnchor.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
m_RightBottomAnchor.setText(" ");
m_RightBottomAnchor.setEnabled(false);
m_RightBottomAnchor.setFocusable(false);
m_RightBottomAnchor.setMaximumSize(new java.awt.Dimension(12, 12));
m_RightBottomAnchor.setMinimumSize(new java.awt.Dimension(12, 12));
m_RightBottomAnchor.setPreferredSize(new java.awt.Dimension(12, 12));
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(m_SearchForStringLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 360, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(m_SearchForStringTextArea, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 360, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(m_IgnoreCaseCheckBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 360, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(10, 10, 10)
.add(m_FindNextButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(m_FindPreviousButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 122, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(18, 18, 18)
.add(m_CloseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(370, 370, 370)
.add(m_RightBottomAnchor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(m_SearchForStringLabel)
.add(6, 6, 6)
.add(m_SearchForStringTextArea, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(14, 14, 14)
.add(m_IgnoreCaseCheckBox)
.add(17, 17, 17)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(m_FindNextButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(m_CloseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(5, 5, 5)
.add(m_RightBottomAnchor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(m_FindPreviousButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void m_SearchForStringTextAreaKeyReleased(java.awt.event.KeyEvent evt)//GEN-FIRST:event_m_SearchForStringTextAreaKeyReleased
{//GEN-HEADEREND:event_m_SearchForStringTextAreaKeyReleased
String currentString = m_SearchForStringTextArea.getText();
if (currentString.length() > 0) {
m_FindNextButton.setEnabled(true);
m_FindPreviousButton.setEnabled(true);
} else {
m_FindNextButton.setEnabled(false);
m_FindPreviousButton.setEnabled(false);
}
}//GEN-LAST:event_m_SearchForStringTextAreaKeyReleased
private void m_CloseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_m_CloseButtonActionPerformed
{//GEN-HEADEREND:event_m_CloseButtonActionPerformed
closeDialog(null);
}//GEN-LAST:event_m_CloseButtonActionPerformed
private void closeDialog(java.awt.event.WindowEvent evt)//GEN-FIRST:event_closeDialog
{//GEN-HEADEREND:event_closeDialog
setVisible(false);
}//GEN-LAST:event_closeDialog
@Override
public void dismissDialog() {
m_CloseButtonActionPerformed(null);
}
private void m_FindPreviousButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_m_FindPreviousButtonActionPerformed
{//GEN-HEADEREND:event_m_FindPreviousButtonActionPerformed
FileContentsList currentList = FileContentsList.getLastFocus();
boolean ignoreCase = m_IgnoreCaseCheckBox.isSelected();
if (currentList != null) {
int currentIndex = currentList.getSelectedIndex();
String searchString = m_SearchForStringTextArea.getText();
if (ignoreCase) {
searchString = searchString.toLowerCase();
}
FileContentsListModel listModel = (FileContentsListModel) currentList.getModel();
boolean bFound = false;
for (int i = currentIndex - 1; i >= 0; i--) {
ContentRow row = listModel.elementAt(i);
if (row.getRowType() != ContentRow.ROWTYPE_BLANK) {
String rowContent = row.getText();
if (ignoreCase) {
rowContent = rowContent.toLowerCase();
}
int foundIndex = rowContent.indexOf(searchString, 0);
if (foundIndex >= 0) {
currentList.setSelectedIndex(i);
parentCompareFrame.positionViewPort(i);
bFound = true;
break;
}
}
}
if (bFound == false) {
String message = searchString + " not found.";
JOptionPane.showMessageDialog(parentCompareFrame, message, "Not Found", JOptionPane.INFORMATION_MESSAGE);
}
}
}//GEN-LAST:event_m_FindPreviousButtonActionPerformed
private void m_FindNextButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_m_FindNextButtonActionPerformed
{//GEN-HEADEREND:event_m_FindNextButtonActionPerformed
boolean ignoreCase = m_IgnoreCaseCheckBox.isSelected();
FileContentsList currentList = FileContentsList.getLastFocus();
if (currentList != null) {
int currentIndex = currentList.getSelectedIndex();
String searchString = m_SearchForStringTextArea.getText();
if (ignoreCase) {
searchString = searchString.toLowerCase();
}
FileContentsListModel listModel = (FileContentsListModel) currentList.getModel();
int size = listModel.size();
boolean bFound = false;
for (int i = currentIndex + 1; i < size; i++) {
ContentRow row = listModel.elementAt(i);
if (row.getRowType() != ContentRow.ROWTYPE_BLANK) {
String rowContent = row.getText();
if (ignoreCase) {
rowContent = rowContent.toLowerCase();
}
int foundIndex = rowContent.indexOf(searchString, 0);
if (foundIndex >= 0) {
currentList.setSelectedIndex(i);
parentCompareFrame.positionViewPort(i);
bFound = true;
break;
}
}
}
if (bFound == false) {
String message = searchString + " not found.";
JOptionPane.showMessageDialog(null, message, "Not Found", JOptionPane.INFORMATION_MESSAGE);
}
}
}//GEN-LAST:event_m_FindNextButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton m_CloseButton;
private javax.swing.JButton m_FindNextButton;
private javax.swing.JButton m_FindPreviousButton;
private javax.swing.JCheckBox m_IgnoreCaseCheckBox;
private javax.swing.JLabel m_RightBottomAnchor;
private javax.swing.JLabel m_SearchForStringLabel;
private javax.swing.JTextArea m_SearchForStringTextArea;
// End of variables declaration//GEN-END:variables
}