KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > richtext > FindDialog


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22 package salsa.richtext;
23
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import javax.swing.text.*;
28 import javax.swing.border.*;
29
30 public class FindDialog extends JDialog
31 {
32    JFrame _frame;
33
34    JTabbedPane _tab;
35    JTextField _find1Text;
36    JTextField _find2Text;
37    Document _findDoc;
38    Document _replaceDoc;
39    ButtonModel _wordModel;
40    ButtonModel _caseModel;
41    ButtonModel _upModel;
42    ButtonModel _downModel;
43
44    int _searchIndex = -1;
45    boolean _searchUp = false;
46    String JavaDoc _searchData;
47
48
49    public FindDialog( JFrame frame, int index )
50    {
51       super( frame, "Find and Replace", false );
52       _frame = frame;
53
54       // "Find" sheet
55

56       _find1Text = new JTextField();
57       _findDoc = _find1Text.getDocument();
58
59       JPanel findPanel = new JPanel();
60       findPanel.setLayout( new GridLayout( 1, 2 ) );
61       // rows, cols
62
findPanel.setBorder( new EmptyBorder( 8, 5, 8, 0 ) );
63       findPanel.add( new JLabel( "Find what:" ) );
64       findPanel.add( _find1Text );
65
66
67       JCheckBox wordCheck = new JCheckBox( "Whole words only" );
68       wordCheck.setMnemonic( 'w' );
69       _wordModel = wordCheck.getModel();
70
71       JCheckBox caseCheck = new JCheckBox( "Match case" );
72       caseCheck.setMnemonic( 'c' );
73       _caseModel = caseCheck.getModel();
74
75       JRadioButton upRadio = new JRadioButton( "Search up" );
76       upRadio.setMnemonic( 'u' );
77       _upModel = upRadio.getModel();
78
79       JRadioButton downRadio = new JRadioButton( "Search down", true );
80       downRadio.setMnemonic( 'd' );
81       _downModel = downRadio.getModel();
82
83       ButtonGroup bg = new ButtonGroup();
84       bg.add( upRadio );
85       bg.add( downRadio );
86
87       JPanel optionPanel = new JPanel( new GridLayout( 2, 2, 8, 2 ) );
88       optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Options" ) );
89
90       optionPanel.add( wordCheck );
91       optionPanel.add( upRadio );
92       optionPanel.add( caseCheck );
93       optionPanel.add( downRadio );
94
95       JPanel pc1 = new JPanel( new BorderLayout() );
96       pc1.add( findPanel, BorderLayout.CENTER );
97       pc1.add( optionPanel, BorderLayout.SOUTH );
98
99       JButton findButton = new JButton( "Find Next" );
100       findButton.setMnemonic( 'f' );
101       findButton.addActionListener(
102          new ActionListener()
103          {
104             public void actionPerformed( ActionEvent ev )
105             {
106                findNext( false, true );
107             }
108          } );
109
110       ActionListener closeAction =
111          new ActionListener()
112          {
113             public void actionPerformed( ActionEvent ev )
114             {
115                setVisible( false );
116             }
117          };
118
119       JButton closeButton = new JButton( "Close" );
120       closeButton.setDefaultCapable( true );
121       closeButton.addActionListener( closeAction );
122
123       JPanel p01 = new JPanel( new FlowLayout() );
124       JPanel p = new JPanel( new GridLayout( 2, 1, 2, 8 ) );
125
126       p.add( findButton );
127       p.add( closeButton );
128
129       p01.add( p );
130
131       JPanel p1 = new JPanel( new BorderLayout() );
132       p1.add( pc1, BorderLayout.CENTER );
133       p1.add( p01, BorderLayout.EAST );
134
135       // "replace" sheet
136

137       _find2Text = new JTextField();
138       _find2Text.setDocument( _findDoc );
139
140       JTextField replaceText = new JTextField();
141       _replaceDoc = replaceText.getDocument();
142
143       JPanel replacePanel = new JPanel( new GridLayout( 2, 2 ) );
144       // rows, cols
145
replacePanel.setBorder( new EmptyBorder( 8, 5, 8, 0 ) );
146
147       replacePanel.add( new JLabel( "Find what:" ) );
148       replacePanel.add( _find2Text );
149       replacePanel.add( new JLabel( "Replace:" ) );
150       replacePanel.add( replaceText );
151
152       wordCheck = new JCheckBox( "Whole words only" );
153       wordCheck.setMnemonic( 'w' );
154       wordCheck.setModel( _wordModel );
155
156       caseCheck = new JCheckBox( "Match case" );
157       caseCheck.setMnemonic( 'c' );
158       caseCheck.setModel( _caseModel );
159
160       upRadio = new JRadioButton( "Search up" );
161       upRadio.setMnemonic( 'u' );
162       upRadio.setModel( _upModel );
163
164       downRadio = new JRadioButton( "Search down", true );
165       downRadio.setMnemonic( 'd' );
166       downRadio.setModel( _downModel );
167
168       bg = new ButtonGroup();
169       bg.add( upRadio );
170       bg.add( downRadio );
171
172       optionPanel = new JPanel( new GridLayout( 2, 2, 8, 2 ) );
173       optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Options" ) );
174
175       optionPanel.add( wordCheck );
176       optionPanel.add( upRadio );
177       optionPanel.add( caseCheck );
178       optionPanel.add( downRadio );
179
180       JPanel pc2 = new JPanel( new BorderLayout() );
181       pc2.add( replacePanel, BorderLayout.CENTER );
182       pc2.add( optionPanel, BorderLayout.SOUTH );
183
184       JButton replaceButton = new JButton( "Replace" );
185       replaceButton.setMnemonic( 'r' );
186
187       JButton replaceAllButton = new JButton( "Replace All" );
188       replaceAllButton.setMnemonic( 'a' );
189
190       closeButton = new JButton( "Close" );
191       closeButton.setDefaultCapable( true );
192       closeButton.addActionListener( closeAction );
193
194       p = new JPanel( new GridLayout( 3, 1, 2, 8 ) );
195       p.add( replaceButton );
196       p.add( replaceAllButton );
197       p.add( closeButton );
198
199       JPanel p02 = new JPanel( new FlowLayout() );
200       p02.add( p );
201
202       JPanel p2 = new JPanel( new BorderLayout() );
203       p2.add( pc2, BorderLayout.CENTER );
204       p2.add( p02, BorderLayout.EAST );
205
206       p01.setPreferredSize( p02.getPreferredSize() );
207
208       _tab = new JTabbedPane();
209
210       _tab.add( "Find", p1 );
211       _tab.add( "Replace", p2 );
212
213       getContentPane().add( _tab, BorderLayout.CENTER );
214
215       addWindowListener(
216          new WindowAdapter()
217          {
218             public void windowActivated( WindowEvent ev )
219             {
220                _searchIndex = -1;
221                if( _tab.getSelectedIndex() == 0 )
222                   _find1Text.grabFocus();
223                else
224                   _find2Text.grabFocus();
225             }
226
227
228             public void windowDeactivated( WindowEvent ev )
229             {
230                _searchData = null;
231             }
232
233          } );
234
235       pack();
236       setResizable( false );
237    }
238
239
240    public void setSelectedTabIndex( int tabIndex )
241    {
242       _tab.setSelectedIndex( tabIndex );
243       setVisible( true );
244       _searchIndex = -1;
245    }
246
247
248    public int findNext( boolean doReplace, boolean showWarnings )
249    {
250       return 1;
251    }
252 }
253
Popular Tags