1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2011 - Scilab Enterprises - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 package org.scilab.modules.scinotes.actions;
17 
18 import java.util.regex.Pattern;
19 
20 import javax.swing.KeyStroke;
21 import javax.swing.text.BadLocationException;
22 import javax.swing.text.Element;
23 
24 import org.scilab.modules.gui.menuitem.MenuItem;
25 import org.scilab.modules.scinotes.SciNotes;
26 import org.scilab.modules.scinotes.ScilabDocument;
27 import org.scilab.modules.scinotes.ScilabEditorPane;
28 
29 /**
30  * NextParagraphAction Class
31  * @author Calixte DENIZET
32  */
33 public class NextParagraphAction extends DefaultAction {
34 
35     private static final long serialVersionUID = 1L;
36     private static final Pattern pat = Pattern.compile("[ \\t\\r\\n]*");
37     protected int next = +1;
38 
39     /**
40      * Constructor
41      * @param name the name of the action
42      * @param editor SciNotes
43      */
NextParagraphAction(String name, SciNotes editor)44     public NextParagraphAction(String name, SciNotes editor) {
45         super(name, editor);
46     }
47 
48     /**
49      * doAction
50      */
doAction()51     public void doAction() {
52         ScilabEditorPane sep = getEditor().getTextPane();
53         ScilabDocument doc = (ScilabDocument) sep.getDocument();
54         Element root = doc.getDefaultRootElement();
55         int line = root.getElementIndex(sep.getCaretPosition());
56         boolean empty = isEmptyLine(doc, line);
57         int i = line + next;
58         int stop = next > 0 ? root.getElementCount() : -1;
59         for (; i != stop; i += next) {
60             boolean b = isEmptyLine(doc, i);
61             if (empty && !b) {
62                 empty = false;
63             } else if (!empty && b) {
64                 break;
65             }
66         }
67         if (i == stop) {
68             i = next > 0 ? stop - 1 : 0;
69         }
70 
71         sep.setCaretPosition(root.getElement(i).getStartOffset());
72     }
73 
74     /**
75      * createMenu
76      * @param label label of the menu
77      * @param editor SciNotes
78      * @param key Keystroke
79      * @return MenuItem
80      */
createMenu(String label, final SciNotes editor, KeyStroke key)81     public static MenuItem createMenu(String label, final SciNotes editor, KeyStroke key) {
82         return createMenu(label, null, new NextParagraphAction(label, editor), key);
83     }
84 
85     /**
86      * Test if a lin e is empty
87      * @param doc the document
88      * @param line the line number
89      * @return true if the line is empty
90      */
isEmptyLine(ScilabDocument doc, int line)91     private static final boolean isEmptyLine(ScilabDocument doc, int line) {
92         try {
93             Element e = doc.getDefaultRootElement().getElement(line);
94             int s = e.getStartOffset();
95             String str = doc.getText(s, e.getEndOffset() - s);
96             return pat.matcher(str).matches();
97         } catch (BadLocationException e) { }
98 
99         return false;
100     }
101 }
102