private int findNext(int pos, String txt, String value) {
    int ix = forwardButton.isSelected() ? txt.indexOf(value, pos) : txt.lastIndexOf(value, pos);

    if (selectedLinesButton.isSelected()
        && (ix < target.getSelectionStart() || ix > target.getSelectionEnd())) {
      ix = -1;
    }

    if (wholeWordCheck.isSelected()) {
      while (ix != -1
          && ((ix > 0 && Character.isLetterOrDigit(txt.charAt(ix - 1)))
              || (ix < txt.length() - value.length() - 1
                  && Character.isLetterOrDigit(txt.charAt(ix + value.length()))))) {
        ix = forwardButton.isSelected() ? ++ix : --ix;
        ix = forwardButton.isSelected() ? txt.indexOf(value, ix) : txt.lastIndexOf(value, ix);
      }
    }

    if (ix == -1 && wrapCheck.isSelected()) {
      if (forwardButton.isSelected() && pos > 0) {
        return findNext(0, txt, value);
      } else if (backwardButton.isSelected() && pos < txt.length() - 1) {
        return findNext(txt.length() - 1, txt, value);
      }
    }

    return ix;
  }
  public void show() {
    if (dialog == null) {
      buildDialog();
    }

    target.getEditComponent().requestFocusInWindow();

    replaceCombo.setEnabled(target.isEditable());
    replaceAllButton.setEnabled(target.isEditable());
    replaceButton.setEnabled(target.isEditable());

    UISupport.showDialog(dialog);
    findCombo.getEditor().selectAll();
    findCombo.requestFocus();
  }
 private int tweakPosition() {
   int pos = target.getCaretPosition();
   if (selectedLinesButton.isSelected()) {
     if (forwardButton.isSelected()) {
       int selstart = target.getSelectionStart();
       if (selstart < pos && selstart != -1) {
         pos = selstart;
       }
     } else {
       int selend = target.getSelectionEnd();
       if (selend > pos && selend != -1) {
         pos = selend;
       }
     }
   } else {
     int selstart = target.getSelectionStart();
     if (selstart < pos && selstart != -1) {
       pos = selstart;
     }
   }
   return pos;
 }
  private void buildDialog() {
    Window window = SwingUtilities.windowForComponent(target.getEditComponent());

    dialog = new JDialog(window, "Find / Replace");
    dialog.setModal(false);

    JPanel panel = new JPanel(new BorderLayout());
    findCombo = new JComboBox();
    findCombo.setEditable(true);
    replaceCombo = new JComboBox();
    replaceCombo.setEditable(true);

    // create inputs
    GridLayout gridLayout = new GridLayout(2, 2);
    gridLayout.setVgap(5);
    JPanel inputPanel = new JPanel(gridLayout);
    inputPanel.add(new JLabel("Find:"));
    inputPanel.add(findCombo);
    inputPanel.add(new JLabel("Replace with:"));
    inputPanel.add(replaceCombo);
    inputPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    // create direction panel
    ButtonGroup directionGroup = new ButtonGroup();
    forwardButton = new JRadioButton("Forward", true);
    forwardButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    directionGroup.add(forwardButton);
    backwardButton = new JRadioButton("Backward");
    backwardButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    directionGroup.add(backwardButton);

    JPanel directionPanel = new JPanel(new GridLayout(2, 1));
    directionPanel.add(forwardButton);
    directionPanel.add(backwardButton);
    directionPanel.setBorder(BorderFactory.createTitledBorder("Direction"));

    // create scope panel
    ButtonGroup scopeGroup = new ButtonGroup();
    allButton = new JRadioButton("All", true);
    allButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    selectedLinesButton = new JRadioButton("Selected Lines");
    selectedLinesButton.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    scopeGroup.add(allButton);
    scopeGroup.add(selectedLinesButton);

    JPanel scopePanel = new JPanel(new GridLayout(2, 1));
    scopePanel.add(allButton);
    scopePanel.add(selectedLinesButton);
    scopePanel.setBorder(BorderFactory.createTitledBorder("Scope"));

    // create options
    caseCheck = new JCheckBox("Case Sensitive");
    caseCheck.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    wholeWordCheck = new JCheckBox("Whole Word");
    wholeWordCheck.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    wrapCheck = new JCheckBox("Wrap Search");
    wrapCheck.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    JPanel optionsPanel = new JPanel(new GridLayout(3, 1));
    optionsPanel.add(caseCheck);
    optionsPanel.add(wholeWordCheck);
    optionsPanel.add(wrapCheck);
    optionsPanel.setBorder(BorderFactory.createTitledBorder("Options"));

    // create panel with options
    JPanel options = new JPanel(new GridLayout(1, 2));

    JPanel radios = new JPanel(new GridLayout(2, 1));
    radios.add(directionPanel);
    radios.add(scopePanel);

    options.add(optionsPanel);
    options.add(radios);
    options.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8));

    // create buttons
    ButtonBarBuilder builder = new ButtonBarBuilder();
    findButton = new JButton(new FindAction());
    builder.addFixed(findButton);
    builder.addRelatedGap();
    replaceButton = new JButton(new ReplaceAction());
    builder.addFixed(replaceButton);
    builder.addRelatedGap();
    replaceAllButton = new JButton(new ReplaceAllAction());
    builder.addFixed(replaceAllButton);
    builder.addUnrelatedGap();
    builder.addFixed(new JButton(new CloseAction()));
    builder.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    // tie it up!
    panel.add(inputPanel, BorderLayout.NORTH);
    panel.add(options, BorderLayout.CENTER);
    panel.add(builder.getPanel(), BorderLayout.SOUTH);

    dialog.getContentPane().add(panel);
    dialog.pack();
    UISupport.initDialogActions(dialog, null, findButton);
  }
 private int tweakLastPosition() {
   return forwardButton.isSelected() ? target.getSelectionEnd() : target.getSelectionStart();
 }