1 /* Scilab (http://www.scilab.org/) - This file is part of Scilab
2 * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
4 * This file must be used under the terms of the CeCILL.
5 * This source file is licensed as described in the file COPYING, which
6 * you should have received as part of this distribution. The terms
7 * are also available at
8 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 package org.scilab.modules.scinotes.actions;
14 import java.util.List;
16 import javax.swing.KeyStroke;
17 import javax.swing.text.BadLocationException;
19 import org.scilab.modules.gui.menuitem.MenuItem;
20 import org.scilab.modules.scinotes.SciNotes;
21 import org.scilab.modules.scinotes.ScilabDocument;
22 import org.scilab.modules.scinotes.SearchManager;
26 * @author Antoine ELIAS
29 public final class FindPreviousAction extends DefaultAction {
31 private static final long serialVersionUID = -9017015781643180532L;
35 * @param name the name of the action
36 * @param editor SciNotes
38 public FindPreviousAction(String name, SciNotes editor) {
45 public void doAction() {
47 ScilabDocument doc = (ScilabDocument) getEditor().getTextPane().getDocument();
48 int startPos = getEditor().getTextPane().getSelectionStart();
49 int endPos = getEditor().getTextPane().getSelectionEnd();
50 int startLine = doc.getDefaultRootElement().getElementIndex(startPos);
51 int endLine = doc.getDefaultRootElement().getElementIndex(endPos);
53 //don't manage multiple lines quick find
54 if (startLine != endLine) {
59 if (startPos == endPos) {
61 if (FindAction.getPreviousSearch() == null) {
64 exp = FindAction.getPreviousSearch();
67 exp = doc.getText(startPos, endPos - startPos);
70 //search from current position to end document
71 List<Integer[]> offsets = SearchManager.findWord(doc, exp, 0, doc.getLength(), true, false, false);
72 if (offsets.size() != 0) {
74 for (int i = 0; i < offsets.size(); i++) {
75 if (offsets.get(i)[0] >= startPos) {
82 index = offsets.size() - 1;
85 getEditor().getTextPane().select(offsets.get(index)[0], offsets.get(index)[1]);
87 } catch (BadLocationException e) {
94 * @param label label of the menu
95 * @param editor SciNotes
96 * @param key KeyStroke
99 public static MenuItem createMenu(String label, SciNotes editor, KeyStroke key) {
100 return createMenu(label, null, new FindPreviousAction(label, editor), key);