KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > find > FindAndReplaceDialog


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32
33 package org.antlr.works.find;
34
35 import com.jgoodies.forms.factories.FormFactory;
36 import com.jgoodies.forms.layout.*;
37 import org.antlr.works.IDE;
38 import org.antlr.xjlib.appkit.frame.XJPanel;
39 import org.antlr.xjlib.appkit.utils.XJAlert;
40
41 import javax.swing.*;
42 import java.awt.*;
43 import java.awt.event.ActionEvent JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45 import java.awt.event.KeyEvent JavaDoc;
46
47 public class FindAndReplaceDialog extends XJPanel {
48
49     private FindAndReplace delegate;
50
51     public FindAndReplaceDialog(FindAndReplace delegate) {
52         this.delegate = delegate;
53
54         initComponents();
55
56         setSize(550, 180);
57         setTitle("Find");
58         awake();
59         center();
60
61         getRootPane().setDefaultButton(nextButton);
62         addEscapeHandling();
63         
64         createActions();
65
66         // Default values
67
ignoreCaseButton.setSelected(true);
68         delegate.setIgnoreCase(true);
69     }
70
71     public void setFindText(String JavaDoc text) {
72         if(text == null || text.length() == 0) return;
73         findField.setText(text);
74     }
75
76     public void addEscapeHandling() {
77         KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
78
79         ActionListener JavaDoc cancelAction = new AbstractAction() {
80             public void actionPerformed(ActionEvent JavaDoc ae) {
81                 setVisible(false);
82             }
83         };
84
85         getRootPane().registerKeyboardAction(cancelAction, "CancelAction", ks,
86                 JComponent.WHEN_IN_FOCUSED_WINDOW);
87     }
88
89     public void createActions() {
90         nextButton.addActionListener(new ActionListener JavaDoc() {
91             public void actionPerformed(ActionEvent JavaDoc event) {
92                 delegate.setFindString(findField.getText());
93                 alertEndOfDocument(this, delegate.next());
94             }
95         });
96
97         previousButton.addActionListener(new ActionListener JavaDoc() {
98             public void actionPerformed(ActionEvent JavaDoc event) {
99                 delegate.setFindString(findField.getText());
100                 alertBeginningOfDocument(this, delegate.prev());
101             }
102         });
103
104         replaceButton.addActionListener(new ActionListener JavaDoc() {
105             public void actionPerformed(ActionEvent JavaDoc event) {
106                 delegate.setReplaceString(replaceField.getText());
107                 delegate.replace();
108             }
109         });
110
111         replaceAndFindButton.addActionListener(new ActionListener JavaDoc() {
112             public void actionPerformed(ActionEvent JavaDoc event) {
113                 delegate.setFindString(findField.getText());
114                 delegate.setReplaceString(replaceField.getText());
115                 delegate.replace();
116                 alertEndOfDocument(this, delegate.next());
117             }
118         });
119
120         replaceAllButton.addActionListener(new ActionListener JavaDoc() {
121             public void actionPerformed(ActionEvent JavaDoc event) {
122                 delegate.setFindString(findField.getText());
123                 delegate.setReplaceString(replaceField.getText());
124                 delegate.replaceAll();
125             }
126         });
127
128         ignoreCaseButton.addActionListener(new ActionListener JavaDoc() {
129             public void actionPerformed(ActionEvent JavaDoc event) {
130                 delegate.setIgnoreCase(ignoreCaseButton.isSelected());
131             }
132         });
133
134         regexButton.addActionListener(new ActionListener JavaDoc() {
135             public void actionPerformed(ActionEvent JavaDoc event) {
136                 delegate.setRegex(regexButton.isSelected());
137             }
138         });
139
140         optionsCombo.addActionListener(new ActionListener JavaDoc() {
141             public void actionPerformed(ActionEvent JavaDoc event) {
142                 delegate.setOptions(optionsCombo.getSelectedIndex());
143             }
144         });
145     }
146
147     private void alertEndOfDocument(ActionListener JavaDoc actionListener, boolean result) {
148         if(result) return;
149
150         if(XJAlert.displayAlert(getJavaContainer(), "End of Document", "The end of the document has been reached.",
151                 "Continue", "OK", 0) == 0)
152         {
153             delegate.setPositionToTop();
154             actionListener.actionPerformed(null);
155         }
156     }
157
158     private void alertBeginningOfDocument(ActionListener JavaDoc actionListener, boolean result) {
159         if(result) return;
160
161         if(XJAlert.displayAlert(getJavaContainer(), "Beginning of Document", "The beginning of the document has been reached.",
162                 "Continue", "OK", 0) == 0)
163         {
164             delegate.setPositionToBottom();
165             actionListener.actionPerformed(null);
166         }
167     }
168
169     public boolean shouldDisplayMainMenuBar() {
170         return super.shouldDisplayMainMenuBar() && !IDE.isPlugin();
171     }
172
173     private void initComponents() {
174         // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
175
// Generated using JFormDesigner Open Source Project license - ANTLR (www.antlr.org)
176
label1 = new JLabel();
177         findField = new JTextField();
178         label2 = new JLabel();
179         replaceField = new JTextField();
180         ignoreCaseButton = new JCheckBox();
181         regexButton = new JCheckBox();
182         optionsCombo = new JComboBox();
183         replaceAllButton = new JButton();
184         replaceButton = new JButton();
185         replaceAndFindButton = new JButton();
186         previousButton = new JButton();
187         nextButton = new JButton();
188         CellConstraints cc = new CellConstraints();
189
190         //======== this ========
191
Container contentPane = getContentPane();
192         contentPane.setLayout(new FormLayout(
193             new ColumnSpec[] {
194                 new ColumnSpec(Sizes.DLUX5),
195                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
196                 new ColumnSpec(ColumnSpec.RIGHT, Sizes.DEFAULT, FormSpec.NO_GROW),
197                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
198                 FormFactory.DEFAULT_COLSPEC,
199                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
200                 FormFactory.DEFAULT_COLSPEC,
201                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
202                 new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
203                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
204                 FormFactory.DEFAULT_COLSPEC,
205                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
206                 FormFactory.DEFAULT_COLSPEC,
207                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
208                 new ColumnSpec(Sizes.DLUX5)
209             },
210             new RowSpec[] {
211                 new RowSpec(Sizes.DLUY5),
212                 FormFactory.LINE_GAP_ROWSPEC,
213                 FormFactory.DEFAULT_ROWSPEC,
214                 FormFactory.LINE_GAP_ROWSPEC,
215                 FormFactory.DEFAULT_ROWSPEC,
216                 FormFactory.LINE_GAP_ROWSPEC,
217                 FormFactory.DEFAULT_ROWSPEC,
218                 FormFactory.LINE_GAP_ROWSPEC,
219                 new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
220                 FormFactory.LINE_GAP_ROWSPEC,
221                 FormFactory.DEFAULT_ROWSPEC,
222                 FormFactory.LINE_GAP_ROWSPEC,
223                 new RowSpec(Sizes.DLUY5)
224             }));
225
226         //---- label1 ----
227
label1.setText("Find:");
228         contentPane.add(label1, cc.xy(3, 3));
229         contentPane.add(findField, cc.xywh(5, 3, 9, 1));
230
231         //---- label2 ----
232
label2.setText("Replace by:");
233         contentPane.add(label2, cc.xy(3, 5));
234         contentPane.add(replaceField, cc.xywh(5, 5, 9, 1));
235
236         //---- ignoreCaseButton ----
237
ignoreCaseButton.setText("Ignore case");
238         contentPane.add(ignoreCaseButton, cc.xy(5, 7));
239
240         //---- regexButton ----
241
regexButton.setText("Regular expression");
242         contentPane.add(regexButton, cc.xy(7, 7));
243
244         //---- optionsCombo ----
245
optionsCombo.setModel(new DefaultComboBoxModel(new String JavaDoc[] {
246             "Contains",
247             "Starts with",
248             "Whole words",
249             "Ends with"
250         }));
251         contentPane.add(optionsCombo, cc.xywh(11, 7, 3, 1));
252
253         //---- replaceAllButton ----
254
replaceAllButton.setText("Replace All");
255         contentPane.add(replaceAllButton, cc.xy(3, 11));
256
257         //---- replaceButton ----
258
replaceButton.setText("Replace");
259         contentPane.add(replaceButton, cc.xy(5, 11));
260
261         //---- replaceAndFindButton ----
262
replaceAndFindButton.setText("Replace & Find");
263         contentPane.add(replaceAndFindButton, cc.xy(7, 11));
264
265         //---- previousButton ----
266
previousButton.setText("Previous");
267         contentPane.add(previousButton, cc.xy(11, 11));
268
269         //---- nextButton ----
270
nextButton.setText("Next");
271         contentPane.add(nextButton, cc.xy(13, 11));
272         pack();
273         // JFormDesigner - End of component initialization //GEN-END:initComponents
274
}
275
276     // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
277
// Generated using JFormDesigner Open Source Project license - ANTLR (www.antlr.org)
278
private JLabel label1;
279     private JTextField findField;
280     private JLabel label2;
281     private JTextField replaceField;
282     private JCheckBox ignoreCaseButton;
283     private JCheckBox regexButton;
284     private JComboBox optionsCombo;
285     private JButton replaceAllButton;
286     private JButton replaceButton;
287     private JButton replaceAndFindButton;
288     private JButton previousButton;
289     private JButton nextButton;
290     // JFormDesigner - End of variables declaration //GEN-END:variables
291

292 }
293
Popular Tags