shortcuts for xpad
[scilab.git] / scilab / modules / xpad / src / java / org / scilab / modules / xpad / actions / FindAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Sylvestre KOUMAR
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.xpad.actions;
14
15 import java.awt.Color;
16 import java.awt.Dimension;
17 import java.awt.GridBagConstraints;
18 import java.awt.GridBagLayout;
19 import java.awt.Insets;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.event.KeyEvent;
23 import java.util.Vector;
24
25 import javax.swing.BorderFactory;
26 import javax.swing.ButtonGroup;
27 import javax.swing.JButton;
28 import javax.swing.JCheckBox;
29 import javax.swing.JFrame;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JRadioButton;
33 import javax.swing.JTextField;
34 import javax.swing.KeyStroke;
35 import javax.swing.text.BadLocationException;
36 import javax.swing.text.DefaultHighlighter;
37 import javax.swing.text.Highlighter;
38
39 import org.scilab.modules.xpad.Xpad;
40 import org.scilab.modules.xpad.style.ScilabStyleDocument;
41
42 public class FindAction extends DefaultAction {
43
44         private JFrame frame;
45         private JTextField textfieldFind;
46         private JTextField textfieldReplace;
47         private JRadioButton buttonForward;
48         private JRadioButton buttonBackward;
49         private JRadioButton buttonAll;
50         private JRadioButton buttonSelection;
51         private ButtonGroup groupDirection;
52         private ButtonGroup groupScope;
53         private JCheckBox caseSensitive;
54         private JCheckBox wrap;
55         private JCheckBox wholeWord;
56         private JCheckBox regularExp;
57         private JButton buttonFind;
58         private JButton buttonReplaceFind;
59         private JButton buttonReplace;
60         private JButton buttonReplaceAll;
61         private JButton buttonClose;
62
63         private String oldword;
64         private String newWord;
65
66         private String wordToFind;
67
68
69         public FindAction(Xpad editor) {
70                 super("Find/Replace...", editor);
71                 //setMnemonic('F');
72                 setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
73         }
74
75         public void doAction() {
76
77                 findReplaceBox();
78         }
79
80         public void findReplaceBox() {
81
82                 //Find & Replace Frame
83                 frame = new JFrame();
84                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
85                 frame.setPreferredSize(new Dimension(300, 600));
86                 frame.setTitle("Find/Replace");
87                 frame.pack();
88                 frame.setLocationRelativeTo(null);
89                 frame.setVisible(true);         
90                 JPanel panel = new JPanel();
91                 panel.setLayout(new GridBagLayout());
92                 frame.setContentPane(panel);
93
94                 JPanel direction = new JPanel();
95                 direction.setLayout(new GridBagLayout());
96                 JPanel scope = new JPanel();
97                 scope.setLayout(new GridBagLayout());
98                 JPanel options = new JPanel();
99                 options.setLayout(new GridBagLayout());
100
101                 GridBagConstraints gbc = new GridBagConstraints();
102                 gbc.anchor = GridBagConstraints.WEST;           
103                 gbc.insets = new Insets(10,5,10,5);
104
105                 //Find & Replace label, text field
106                 JLabel labelFind = new JLabel("Find :");
107                 JLabel labelReplace = new JLabel("Replace with :");
108                 textfieldFind = new JTextField();
109                 textfieldFind.setPreferredSize(new Dimension(150, 20));
110                 textfieldReplace = new JTextField();
111                 textfieldReplace.setPreferredSize(new Dimension(150, 20));
112
113                 panel.add(labelFind, gbc);
114                 gbc.gridwidth = GridBagConstraints.REMAINDER;
115                 panel.add(textfieldFind, gbc);
116                 gbc.gridwidth = 1;
117
118                 panel.add(labelReplace, gbc);
119                 gbc.gridwidth = GridBagConstraints.REMAINDER;
120                 panel.add(textfieldReplace, gbc);
121                 gbc.gridwidth = 1;
122
123                 panel.add(direction, gbc);
124                 gbc.gridwidth = GridBagConstraints.REMAINDER;
125                 panel.add(scope, gbc);
126                 gbc.gridwidth = 1;
127                 gbc.gridwidth = GridBagConstraints.REMAINDER;
128                 panel.add(options, gbc);
129
130                 //Find & Replace direction
131                 direction.setBorder(BorderFactory.createTitledBorder("Direction"));
132
133                 buttonForward = new JRadioButton("Forward");
134                 buttonBackward = new JRadioButton("Backward");
135
136                 groupDirection = new ButtonGroup();
137                 groupDirection.add(buttonForward);
138                 groupDirection.add(buttonBackward);
139                 buttonForward.setSelected(true);
140
141                 gbc.gridwidth = GridBagConstraints.REMAINDER;
142                 direction.add(buttonForward, gbc);
143                 gbc.gridwidth = 1;
144
145                 gbc.gridwidth = GridBagConstraints.REMAINDER;
146                 direction.add(buttonBackward, gbc);
147                 gbc.gridwidth = 1;
148
149                 //Find & Replace scope
150                 scope.setBorder(BorderFactory.createTitledBorder("Scope"));
151
152                 buttonAll = new JRadioButton("All");
153                 buttonSelection = new JRadioButton("Selected lines");
154
155                 groupScope = new ButtonGroup();
156                 groupScope.add(buttonAll);
157                 groupScope.add(buttonSelection);
158                 buttonAll.setSelected(true);
159
160                 gbc.gridwidth = GridBagConstraints.REMAINDER;
161                 scope.add(buttonAll, gbc);
162                 gbc.gridwidth = 1;
163
164                 gbc.gridwidth = GridBagConstraints.REMAINDER;
165                 scope.add(buttonSelection, gbc);
166                 gbc.gridwidth = 1;
167
168                 //Find & Replace options
169                 options.setBorder(BorderFactory.createTitledBorder("Options"));
170
171                 caseSensitive = new JCheckBox("Case sensitive");
172                 wrap = new JCheckBox("Wrap search");
173                 wholeWord = new JCheckBox("Whole word");
174                 regularExp = new JCheckBox("Regular expressions");
175
176                 gbc.anchor = GridBagConstraints.WEST;
177
178                 gbc.gridwidth = GridBagConstraints.REMAINDER;
179                 options.add(caseSensitive, gbc);
180                 options.add(wrap, gbc);
181                 gbc.gridwidth = GridBagConstraints.REMAINDER;
182                 options.add(wholeWord, gbc);
183                 options.add(regularExp, gbc);
184
185                 //Find & Replace buttons
186                 buttonFind = new JButton("Find");
187                 buttonReplaceFind = new JButton("Replace/Find");
188                 buttonReplace = new JButton("Replace");
189                 buttonReplaceAll = new JButton("Replace All");
190                 buttonClose = new JButton("Close");
191
192                 buttonFind.setPreferredSize(buttonReplaceFind.getPreferredSize());
193                 buttonReplace.setPreferredSize(buttonReplaceFind.getPreferredSize());
194                 buttonReplaceAll.setPreferredSize(buttonReplaceFind.getPreferredSize());
195                 buttonClose.setPreferredSize(buttonReplaceFind.getPreferredSize());
196
197                 gbc.gridwidth = 1;
198                 panel.add(buttonFind, gbc);
199                 gbc.gridwidth = GridBagConstraints.REMAINDER;
200                 panel.add(buttonReplaceFind, gbc);
201                 gbc.gridwidth = 1;
202
203                 panel.add(buttonReplace, gbc);
204                 gbc.gridwidth = GridBagConstraints.REMAINDER;
205                 panel.add(buttonReplaceAll, gbc);
206                 gbc.gridwidth = 1;
207
208                 gbc.anchor = GridBagConstraints.EAST;
209
210                 gbc.gridwidth = GridBagConstraints.REMAINDER;
211                 panel.add(buttonClose, gbc);
212
213
214                 buttonFind.addActionListener(new ActionListener() {
215                         int click = 0;
216                         Vector<Integer> offsets;
217
218                         public void actionPerformed(ActionEvent e) {
219
220                                 //Get the word we have to find
221                                 wordToFind = textfieldFind.getText();
222
223                                 //Find all matching words and return their starting position into a vector
224                                 offsets = ((ScilabStyleDocument) getEditor().getTextPane().getStyledDocument()).findWord(wordToFind);
225
226                                 Highlighter highlight = getEditor().getTextPane().getHighlighter();
227                                 highlight.removeAllHighlights();
228
229                                 //Here we highlight all the matching words
230                                 for (int i = 0; i < offsets.size(); i++) {
231                                         try {
232                                                 highlight.addHighlight(offsets.elementAt(i), offsets.elementAt(i)+wordToFind.length(), new DefaultHighlighter.DefaultHighlightPainter(Color.green));
233                                         } catch (BadLocationException e1) {
234                                                 e1.printStackTrace();
235                                         }
236                                 }
237
238                                 //Here we highlight matching word one by one
239                                 if (click == offsets.size()) {
240                                         System.out.println("You have reached the end of the document");
241                                         click = 0;
242                                 }
243
244                                 boolean changeHighlight = false;
245                                 Highlighter.Highlight myHighlight = null;
246                                 Highlighter.Highlight[] highlights = getEditor().getTextPane().getHighlighter().getHighlights();
247
248                                 for(int i=0; i < highlights.length; i++) {
249                                         myHighlight = highlights[i];
250                                         //Should equal zero
251                                         if(myHighlight.getStartOffset() == offsets.elementAt(click)) {
252                                                 changeHighlight = true;
253                                                 break;
254                                         }
255                                 }
256
257                                 if(changeHighlight) {
258                                         Highlighter hl = getEditor().getTextPane().getHighlighter();
259                                         try {
260                                                 Xpad editor = getEditor();
261                                                 editor.getTextPane().setCaretPosition(offsets.elementAt(click));
262                                                 editor.getTextPane().select(offsets.elementAt(click), offsets.elementAt(click)+wordToFind.length());
263                                                 highlight.addHighlight(offsets.elementAt(click), offsets.elementAt(click)+wordToFind.length(), DefaultHighlighter.DefaultPainter);
264                                                 hl.changeHighlight(myHighlight, myHighlight.getStartOffset()+wordToFind.length(), myHighlight.getEndOffset());
265                                         } catch (BadLocationException e1) {
266                                                 e1.printStackTrace();
267                                         }
268                                 }
269                                 click++;
270                         }
271                 });
272
273                 buttonReplaceAll.addActionListener(new ActionListener() {
274                         public void actionPerformed(ActionEvent e) {
275                                 oldword = textfieldFind.getText();
276                                 newWord = textfieldReplace.getText();
277
278                                 //Call replace
279                                 
280                         }
281                 });
282
283                 buttonClose.addActionListener(new ActionListener() {
284
285                         public void actionPerformed(ActionEvent e) {
286                                 frame.dispose();
287                         }
288                 });
289         }
290
291 }