NikosTek
NikosTek

Reputation: 53

FindPrevious word in a text editor

Hello the following code is a text editor with some utilities but i can't find out why my doPre() method doesn't find the previous word i searched like the doNext() method which finds the next word i searched. What is wrong so it will work properly? Thanks in advance!

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

public class MyEditor extends JFrame {

public MyEditor() {
    super("MyEditor");
    initComponents();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void initComponents() {
    p = new JPanel(new BorderLayout());
    ta = new JTextArea(10, 50);

    sc = new JScrollPane(ta);
    p.add(sc);

    tb = new JToolBar();
    tb.setFloatable(false);
    newButton = new JButton("new");
    newButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doNew();
        }
    });
    newButton.setToolTipText("New file...");
    newButton.setIcon(new ImageIcon(getClass().getResource("/images/New16.png")));
    openButton = new JButton("open");
    openButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doOpen();
        }
    });
    openButton.setToolTipText("Open a file...");
    openButton.setIcon(new ImageIcon(getClass().getResource("/images/Open16.gif")));

    saveButton = new JButton("save");
    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doSave();
        }
    });
    saveButton.setToolTipText("Save a file...");
    saveButton.setIcon(new ImageIcon(getClass().getResource("/images/Save16.gif")));
    findButton = new JButton("find");
    findButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doFind();
        }
    });
    findButton.setToolTipText("Find...");
    findButton.setIcon(new ImageIcon(getClass().getResource("/images/Search16.png")));
    nextButton = new JButton("next");
    nextButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doNext();
        }
    });
    nextButton.setToolTipText("Next...");
    nextButton.setEnabled(false);
    previousButton = new JButton("pre");
    previousButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doPre();
        }
    });
    previousButton.setToolTipText("P...");
    previousButton.setEnabled(true);
    aboutButton = new JButton("about");
    aboutButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doAbout();
        }
    });
    aboutButton.setToolTipText("About...");
    aboutButton.setIcon(new ImageIcon(getClass().getResource("/images/Info16.png")));
    tb.add(newButton);
    tb.add(openButton);
    tb.add(saveButton);
    tb.add(findButton);
    tb.add(nextButton);
    tb.add(previousButton);
    tb.add(aboutButton);
    p.add(tb, BorderLayout.NORTH);
    add(p);
    fc = new JFileChooser();
    pack();
}

private void doOpen() {
    fc.showOpenDialog(this);
    theFile = fc.getSelectedFile();
    if (theFile == null) {
        return;
    }

    ta.setText(null);

    FileReader myFile = null;
    BufferedReader buff = null;

    try {
        myFile = new FileReader(theFile);
        buff = new BufferedReader(myFile);

        while (true) {
            String line = buff.readLine();
            if (line == null) {   //EOF
                break;
            }
            ta.append(line + "\n");
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            if (buff != null) {
                buff.close();
            }
            if (myFile != null) {
                myFile.close();
            }
        } catch (IOException e1) {
            System.out.println(e1.getMessage());
        }
    }
}

private void doSave() {
    fc.showSaveDialog(this);
    theFile = fc.getSelectedFile();
    if (theFile == null) {
        return;
    }

    FileWriter myFile = null;
    BufferedWriter buff = null;

    try {
        myFile = new FileWriter(theFile);
        buff = new BufferedWriter(myFile);
        buff.write(ta.getText());

        System.out.println("File writing is complete");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            if (buff != null) {
                buff.flush();
                buff.close();
            }
            if (myFile != null) {
                myFile.close();
            }
        } catch (IOException e1) {
            System.out.println(e1.getMessage());
        }
    }

}

private void doNew() {
    ta.setText(null);
}

private void doFind() {
    findString = JOptionPane.showInputDialog(this, "Find What", "Find", JOptionPane.INFORMATION_MESSAGE);
    ta.requestFocusInWindow();
    if (findString != null && findString.length() > 0) {
        String txt = ta.getText();
        int findLength = findString.length();
        findLocation = txt.indexOf(findString);
        if (findLocation > -1) {
            ta.setCaretPosition(findLocation);
            ta.moveCaretPosition(findLocation + findLength);
            ta.getCaret().setSelectionVisible(true);
            nextButton.setEnabled(true);
        }
    }
}

private void doNext() {
    String txt = ta.getText();
    int findLength = findString.length();
    if (findLocation > -1) {
        findLocation = txt.indexOf(findString, findLocation + 1);
        if (findLocation > -1) {
            ta.setCaretPosition(findLocation);
            ta.moveCaretPosition(findLocation + findLength);
            ta.getCaret().setSelectionVisible(true);
        } else {
            nextButton.setEnabled(false);
        }
    }

}
private void doPre() {
    String txt = ta.getText();
    int findLength = findString.length();
    if (findLocation > -1) {
        findLocation = txt.indexOf(findString, (findLocation-1) + 1);
        if (findLocation > -1) {
            ta.setCaretPosition(findLocation);
            ta.moveCaretPosition((findLocation-(findLocation+1)) + findLength +1);
            ta.getCaret().setSelectionVisible(true);
        } else {
            nextButton.setEnabled(false);
        }
    }
private void doAbout() {
    JOptionPane.showMessageDialog(this, "TEI Java Editor\nSummer Semester 2015", "About", JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MyEditor().setVisible(true);
        }
    });
}

private JPanel p;
private JTextArea ta;
private JScrollPane sc;
private JToolBar tb;
private JButton newButton, openButton, saveButton, findButton, aboutButton, nextButton, previousButton;
private JFileChooser fc;
private File theFile;
private String findString;
private int findLocation;
}

As far i managed to make it go to the previous word but i can't figure out how to make it go top. For example if we got 4 times the same word in the text editor like:

findLocation
findLocation
findLocation
findLocation

and my current state marked the 3rd word it will jumpand mark the 2nd word and if i press again the previous button it won't jump and mark the 1st word.Any ideas?

private void doPre() {
    String txt = ta.getText();
    int findLength = findString.length();
    if (findLocation > -1) {
        findLocation = txt.lastIndexOf(findString, findLocation + 1);
        if (findLocation > -1) {
            ta.setCaretPosition(findLocation);
            ta.moveCaretPosition(findLocation - findLength - 1);
            ta.getCaret().setSelectionVisible(true);
        } else {
            previousButton.setEnabled(false);
        }
    }

Upvotes: 2

Views: 65

Answers (2)

Patrick
Patrick

Reputation: 731

I noticed in your doPre method you have this

findLocation = txt.indexOf(findString, (findLocation-1) + 1);

It seems findLocation would be exactly the same if you subtract one then add one again. Try just taking out the +1;

findLocation = txt.indexOf(findString, findLocation - 1);

It would also be the same with this line:

ta.moveCaretPosition((findLocation-(findLocation+1)) + findLength +1);

here, findLocation - findLocation + 1 will give you just 1, that is redundant.

Upvotes: 4

maskacovnik
maskacovnik

Reputation: 3084

I don't know what does it mean "doesn't work correctly".
Only what I see is this:

ta.moveCaretPosition((findLocation-(findLocation+1))+findLength+1);

simplify:

( findLocation - ( findLocation + 1 ) ) + findLength +1

to:

( findLocation - findLocation - 1) + findLength + 1 =
( -1 ) + findLength + 1 =
findLength

so it means this:

ta.moveCaretPosition(findLength);

Upvotes: 1

Related Questions