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