Skip to content

Commit

Permalink
Implemented the Find function
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanpc committed Sep 16, 2018
1 parent f7f063c commit d12047f
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 37 deletions.
35 changes: 0 additions & 35 deletions src/com/innoveworkshop/monicom/AboutWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,6 @@ private void btWebsiteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
}
}//GEN-LAST:event_btWebsiteActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(AboutWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(AboutWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(AboutWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(AboutWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AboutWindow().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btWebsite;
private javax.swing.JLabel lblAppName;
Expand Down
98 changes: 98 additions & 0 deletions src/com/innoveworkshop/monicom/FindWindow.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="title" type="java.lang.String" value="Find"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="lblWhat" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="txtSearch" min="-2" pref="323" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<Component id="chkMatchCase" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="btCancel" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="btSearch" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="txtSearch" alignment="3" pref="25" max="32767" attributes="0"/>
<Component id="lblWhat" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="btSearch" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="btCancel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="chkMatchCase" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="lblWhat">
<Properties>
<Property name="text" type="java.lang.String" value="Find What: "/>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="txtSearch">
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="txtSearchActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="btSearch">
<Properties>
<Property name="text" type="java.lang.String" value=" Search "/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btSearchActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="btCancel">
<Properties>
<Property name="text" type="java.lang.String" value=" Cancel "/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btCancelActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JCheckBox" name="chkMatchCase">
<Properties>
<Property name="text" type="java.lang.String" value="Match Case"/>
</Properties>
</Component>
</SubComponents>
</Form>
193 changes: 193 additions & 0 deletions src/com/innoveworkshop/monicom/FindWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.innoveworkshop.monicom;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
*
* @author nathanpc
*/
public class FindWindow extends javax.swing.JFrame {
private String needle;
private List<int[]> found;
private int curr_index;
private JTextArea field;

/**
* Creates new form FindWindow
*/
public FindWindow(JTextArea field) {
initComponents();

this.found = new ArrayList();
this.needle = "";
this.curr_index = 0;
this.field = field;
}

/**
* Populates the found list.
*
* @param haystack Text to perform the search on.
*/
private void populateFoundList(String haystack) {
int last_found = -1;
found.clear();

while (haystack.indexOf(needle, last_found + 1) >= 0) {
last_found = haystack.indexOf(needle, last_found + 1);

int indexes[] = { last_found, last_found + needle.length() };
found.add(indexes);
System.out.println("Found: " + indexes[0] + " - " + indexes[1]);
}
}

/**
* Performs a search action.
*/
private void performSearch() {
String text = txtSearch.getText();
String haystack = field.getText();

// Is this case sensitive?
if (!chkMatchCase.isSelected()) {
text = text.toLowerCase();
haystack = haystack.toLowerCase();
}

// Check if it is a new string that the user is searching for.
if (!text.equals(needle)) {
needle = text;
curr_index = 0;
}

// Grab the list of places where we found it.
populateFoundList(haystack);

// Nothing was found.
if (found.isEmpty()) {
Debug.println("FINDING", "No occurrences of " + needle + " found.");
JOptionPane.showMessageDialog(null, "No occurrences of " + needle + " found.",
"Nothing found", JOptionPane.INFORMATION_MESSAGE);

return;
}

// Loop back the indexes if we have reached the end.
if (curr_index == found.size()) {
curr_index = 0;
}

// Go through the findings one at a time.
int indexes[] = found.get(curr_index);
field.select(indexes[0], indexes[1]);
curr_index++;
}

/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

lblWhat = new javax.swing.JLabel();
txtSearch = new javax.swing.JTextField();
btSearch = new javax.swing.JButton();
btCancel = new javax.swing.JButton();
chkMatchCase = new javax.swing.JCheckBox();

setTitle("Find");

lblWhat.setText("Find What: ");

txtSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtSearchActionPerformed(evt);
}
});

btSearch.setText(" Search ");
btSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSearchActionPerformed(evt);
}
});

btCancel.setText(" Cancel ");
btCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCancelActionPerformed(evt);
}
});

chkMatchCase.setText("Match Case");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(lblWhat)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(chkMatchCase)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btCancel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btSearch)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
.addComponent(lblWhat))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btSearch)
.addComponent(btCancel)
.addComponent(chkMatchCase))
.addContainerGap())
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
this.setVisible(false);
}//GEN-LAST:event_btCancelActionPerformed

private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
performSearch();
}//GEN-LAST:event_btSearchActionPerformed

private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed
performSearch();
}//GEN-LAST:event_txtSearchActionPerformed


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btCancel;
private javax.swing.JButton btSearch;
private javax.swing.JCheckBox chkMatchCase;
private javax.swing.JLabel lblWhat;
private javax.swing.JTextField txtSearch;
// End of variables declaration//GEN-END:variables
}
3 changes: 3 additions & 0 deletions src/com/innoveworkshop/monicom/MainWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
</Property>
<Property name="text" type="java.lang.String" value="Find..."/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="mnuFindActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JPopupMenu$Separator" name="jSeparator1">
</MenuItem>
Expand Down
13 changes: 11 additions & 2 deletions src/com/innoveworkshop/monicom/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ private void enableInput(boolean enable) {
*/
private void showErrorDialog(String tag, String title, String message) {
Debug.println(tag, message);
JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_CANCEL_OPTION,
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
}

/**
Expand Down Expand Up @@ -503,6 +502,11 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {

mnuFind.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
mnuFind.setText("Find...");
mnuFind.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnuFindActionPerformed(evt);
}
});
mnuEdit.add(mnuFind);
mnuEdit.add(jSeparator1);

Expand Down Expand Up @@ -852,6 +856,11 @@ private void mnuAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
about.setVisible(true);
}//GEN-LAST:event_mnuAboutActionPerformed

private void mnuFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuFindActionPerformed
FindWindow find = new FindWindow(txtMonitor);
find.setVisible(true);
}//GEN-LAST:event_mnuFindActionPerformed

//<editor-fold defaultstate="collapsed" desc="Main Function">
/**
* @param args the command line arguments
Expand Down

0 comments on commit d12047f

Please sign in to comment.