5050@ SuppressWarnings ("serial" )
5151public class FindReplace extends JFrame implements ActionListener {
5252
53- static final int EDGE = OSUtils .isMacOS () ? 20 : 13 ;
54- static final int SMALL = 6 ;
55- static final int BUTTONGAP = 12 ; // 12 is correct for Mac, other numbers may be required for other platofrms
53+ private Editor editor ;
5654
57- Editor editor ;
55+ private JTextField findField ;
56+ private JTextField replaceField ;
57+ private static String findString ;
58+ private static String replaceString ;
5859
59- JTextField findField ;
60- JTextField replaceField ;
61- static String findString ;
62- static String replaceString ;
60+ private JButton replaceButton ;
61+ private JButton replaceAllButton ;
62+ private JButton replaceFindButton ;
63+ private JButton previousButton ;
64+ private JButton findButton ;
6365
64- JButton replaceButton ;
65- JButton replaceAllButton ;
66- JButton replaceFindButton ;
67- JButton previousButton ;
68- JButton findButton ;
66+ private JCheckBox ignoreCaseBox ;
67+ private static boolean ignoreCase = true ;
6968
70- JCheckBox ignoreCaseBox ;
71- static boolean ignoreCase = true ;
69+ private JCheckBox wrapAroundBox ;
70+ private static boolean wrapAround = true ;
7271
73- JCheckBox wrapAroundBox ;
74- static boolean wrapAround = true ;
75-
76- JCheckBox searchAllFilesBox ;
77- static boolean searchAllFiles = false ;
72+ private JCheckBox searchAllFilesBox ;
73+ private static boolean searchAllFiles = false ;
7874
7975 public FindReplace (Editor editor ) {
80- super ("Find" );
81- setResizable (false );
76+ super (_ ("Find" ));
8277 this .editor = editor ;
8378
84- FlowLayout searchLayout = new FlowLayout (FlowLayout .RIGHT ,5 ,0 );
85- Container pane = getContentPane ();
86- pane .setLayout (searchLayout );
87-
8879 JLabel findLabel = new JLabel (_ ("Find:" ));
80+ findField = new JTextField (20 );
8981 JLabel replaceLabel = new JLabel (_ ("Replace with:" ));
90- Dimension labelDimension = replaceLabel .getPreferredSize ();
91-
92- JPanel find = new JPanel ();
93- find .add (findLabel );
94- find .add (findField = new JTextField (20 ));
95- pane .add (find );
96-
97- JPanel replace = new JPanel ();
98- replace .add (replaceLabel );
99- replace .add (replaceField = new JTextField (20 ));
100- pane .add (replace );
101-
102- int fieldHeight = findField .getPreferredSize ().height ;
82+ replaceField =new JTextField (20 );
10383
104- JPanel checkbox = new JPanel ();
105-
10684 // Fill the findString with selected text if no previous value
10785 if (editor .getSelectedText () != null &&
10886 editor .getSelectedText ().length () > 0 )
10987 findString = editor .getSelectedText ();
11088
111- if (findString != null ) findField . setText ( findString );
112- if ( replaceString != null ) replaceField .setText (replaceString );
113- //System.out.println("setting find str to " + findString);
114- //findField.requestFocusInWindow( );
115-
89+ if (findString != null )
90+ findField .setText (findString );
91+ if ( replaceString != null )
92+ replaceField . setText ( replaceString );
93+
11694 ignoreCaseBox = new JCheckBox (_ ("Ignore Case" ));
11795 ignoreCaseBox .addActionListener (new ActionListener () {
11896 public void actionPerformed (ActionEvent e ) {
11997 ignoreCase = ignoreCaseBox .isSelected ();
12098 }
12199 });
122100 ignoreCaseBox .setSelected (ignoreCase );
123- checkbox .add (ignoreCaseBox );
124101
125102 wrapAroundBox = new JCheckBox (_ ("Wrap Around" ));
126103 wrapAroundBox .addActionListener (new ActionListener () {
@@ -129,128 +106,111 @@ public void actionPerformed(ActionEvent e) {
129106 }
130107 });
131108 wrapAroundBox .setSelected (wrapAround );
132- checkbox .add (wrapAroundBox );
133-
109+
134110 searchAllFilesBox = new JCheckBox (_ ("Search all Sketch Tabs" ));
135111 searchAllFilesBox .addActionListener (new ActionListener () {
136112 public void actionPerformed (ActionEvent e ) {
137113 searchAllFiles = searchAllFilesBox .isSelected ();
138114 }
139115 });
140116 searchAllFilesBox .setSelected (searchAllFiles );
141- checkbox .add (searchAllFilesBox );
142117
143- pane .add (checkbox );
144-
145- JPanel buttons = new JPanel ();
146- buttons .setLayout (new FlowLayout (FlowLayout .CENTER , BUTTONGAP , 0 ));
118+ JPanel checkboxPanel = new JPanel ();
119+ checkboxPanel .setLayout (new BoxLayout (checkboxPanel , BoxLayout .LINE_AXIS ));
120+ checkboxPanel .add (ignoreCaseBox );
121+ checkboxPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
122+ checkboxPanel .add (wrapAroundBox );
123+ checkboxPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
124+ checkboxPanel .add (searchAllFilesBox );
125+
126+ replaceAllButton = new JButton (_ ("Replace All" ));
127+ replaceAllButton .addActionListener (this );
128+ replaceButton = new JButton (_ ("Replace" ));
129+ replaceButton .addActionListener (this );
130+ replaceFindButton = new JButton (_ ("Replace & Find" ));
131+ replaceFindButton .addActionListener (this );
132+ previousButton = new JButton (_ ("Previous" ));
133+ previousButton .addActionListener (this );
134+ findButton = new JButton (_ ("Find" ));
135+ findButton .addActionListener (this );
147136
148- // ordering is different on mac versus pc
137+ JPanel buttonPanel = new JPanel ();
138+ buttonPanel .setLayout (new BoxLayout (buttonPanel , BoxLayout .LINE_AXIS ));
139+ // ordering of buttons is different on mac versus pc
149140 if (OSUtils .isMacOS ()) {
150- buttons .add (replaceAllButton = new JButton (_ ("Replace All" )));
151- buttons .add (replaceButton = new JButton (_ ("Replace" )));
152- buttons .add (replaceFindButton = new JButton (_ ("Replace & Find" )));
153- buttons .add (previousButton = new JButton (_ ("Previous" )));
154- buttons .add (findButton = new JButton (_ ("Find" )));
141+ buttonPanel .add (replaceAllButton );
142+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
143+ buttonPanel .add (replaceButton );
144+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
145+ buttonPanel .add (replaceFindButton );
146+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
147+ buttonPanel .add (previousButton );
148+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
149+ buttonPanel .add (findButton );
155150
156151 } else {
157- buttons .add (findButton = new JButton (_ ("Find" )));
158- buttons .add (previousButton = new JButton (_ ("Previous" ))); // is this the right position for non-Mac?
159- buttons .add (replaceFindButton = new JButton (_ ("Replace & Find" )));
160- buttons .add (replaceButton = new JButton (_ ("Replace" )));
161- buttons .add (replaceAllButton = new JButton (_ ("Replace All" )));
152+ buttonPanel .add (findButton );
153+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
154+ buttonPanel .add (previousButton ); // is this the right position for non-Mac?
155+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
156+ buttonPanel .add (replaceFindButton );
157+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
158+ buttonPanel .add (replaceButton );
159+ buttonPanel .add (Box .createRigidArea (new Dimension (8 ,0 )));
160+ buttonPanel .add (replaceAllButton );
162161 }
163- pane .add (buttons );
164-
162+
165163 // to fix ugliness.. normally macosx java 1.3 puts an
166164 // ugly white border around this object, so turn it off.
167165 if (OSUtils .isMacOS ()) {
168- buttons .setBorder (null );
166+ buttonPanel .setBorder (null );
169167 }
170168
171- /*
172- findField.addFocusListener(new FocusListener() {
173- public void focusGained(FocusEvent e) {
174- System.out.println("Focus gained " + e.getOppositeComponent());
175- }
176-
177- public void focusLost(FocusEvent e) {
178- System.out.println("Focus lost "); // + e.getOppositeComponent());
179- if (e.getOppositeComponent() == null) {
180- requestFocusInWindow();
181- }
182- }
183- });
184- */
185-
186- Dimension buttonsDimension = buttons .getPreferredSize ();
187- int visibleButtonWidth = buttonsDimension .width - 2 * BUTTONGAP ;
188- int fieldWidth = visibleButtonWidth - (labelDimension .width + SMALL );
189-
190- // +1 since it's better to tend downwards
191- int yoff = (1 + fieldHeight - labelDimension .height ) / 2 ;
192-
193- int ypos = EDGE ;
194-
195- int labelWidth = findLabel .getPreferredSize ().width ;
196- findLabel .setBounds (EDGE + (labelDimension .width -labelWidth ), ypos + yoff , // + yoff was added to the wrong field
197- labelWidth , labelDimension .height );
198- findField .setBounds (EDGE + labelDimension .width + SMALL , ypos ,
199- fieldWidth , fieldHeight );
200-
201- ypos += fieldHeight + SMALL ;
202-
203- labelWidth = replaceLabel .getPreferredSize ().width ;
204- replaceLabel .setBounds (EDGE + (labelDimension .width -labelWidth ), ypos + yoff ,
205- labelWidth , labelDimension .height );
206- replaceField .setBounds (EDGE + labelDimension .width + SMALL , ypos ,
207- fieldWidth , fieldHeight );
208-
209- ypos += fieldHeight + SMALL ;
210-
211- ignoreCaseBox .setBounds (EDGE + labelDimension .width + SMALL ,
212- ypos ,
213- (fieldWidth -SMALL )/4 , fieldHeight );
214-
215- wrapAroundBox .setBounds (EDGE + labelDimension .width + SMALL + (fieldWidth -SMALL )/4 + SMALL ,
216- ypos ,
217- (fieldWidth -SMALL )/4 , fieldHeight );
218-
219- searchAllFilesBox .setBounds (EDGE + labelDimension .width + SMALL + (int )((fieldWidth -SMALL )/1.9 ) + SMALL ,
220- ypos ,
221- (fieldWidth -SMALL )/2 , fieldHeight );
222-
223- ypos += fieldHeight + SMALL ;
224-
225- buttons .setBounds (EDGE -BUTTONGAP , ypos ,
226- buttonsDimension .width , buttonsDimension .height );
227-
228- ypos += buttonsDimension .height ;
229-
230- // Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
231-
232- int wide = visibleButtonWidth + EDGE *2 ;
233- int high = ypos ; // butt.y + butt.height + EDGE*2 + SMALL;
169+ //Put all components onto the dialog window
170+ GridBagLayout searchLayout =new GridBagLayout ();
171+ GridBagConstraints gbc =new GridBagConstraints ();
172+ Container pane = getContentPane ();
173+ pane .setLayout (searchLayout );
174+
175+ gbc .insets =new Insets (4 ,4 ,4 ,4 );
176+ gbc .gridx =0 ;
177+ gbc .weightx =0.0 ;
178+ gbc .weighty =0.0 ;
179+ gbc .fill =GridBagConstraints .NONE ;
180+ gbc .anchor =GridBagConstraints .LINE_END ;
181+ pane .add (findLabel ,gbc );
182+ gbc .gridx =1 ;
183+ gbc .weightx =1.0 ;
184+ gbc .fill =GridBagConstraints .HORIZONTAL ;
185+ gbc .anchor =GridBagConstraints .LINE_START ;
186+ pane .add (findField ,gbc );
187+ gbc .gridx =0 ;
188+ gbc .gridy =1 ;
189+ gbc .weightx =0.0 ;
190+ gbc .fill =GridBagConstraints .NONE ;
191+ gbc .anchor =GridBagConstraints .LINE_END ;
192+ pane .add (replaceLabel ,gbc );
193+ gbc .gridx =1 ;
194+ gbc .weightx =1.0 ;
195+ gbc .fill =GridBagConstraints .HORIZONTAL ;
196+ gbc .anchor =GridBagConstraints .LINE_START ;
197+ pane .add (replaceField ,gbc );
198+ gbc .gridx =1 ;
199+ gbc .gridy =2 ;
200+ gbc .weighty =0.0 ;
201+ gbc .fill =GridBagConstraints .NONE ;
202+ pane .add (checkboxPanel ,gbc );
203+ gbc .anchor =GridBagConstraints .CENTER ;
204+ gbc .gridwidth =2 ;
205+ gbc .gridx =0 ;
206+ gbc .gridy =3 ;
207+ gbc .insets =new Insets (12 ,4 ,4 ,4 );
208+ pane .add (buttonPanel ,gbc );
234209
235210 pack ();
236- Insets insets = getInsets ();
237- //System.out.println("Insets = " + insets);
238- setSize (wide + insets .left + insets .right ,high + insets .top + insets .bottom );
239-
240- setLocationRelativeTo ( null ); // center
241- // setBounds((screen.width - wide) / 2, (screen.height - high) / 2, wide, high);
242-
243- replaceButton .addActionListener (this );
244- replaceAllButton .addActionListener (this );
245- replaceFindButton .addActionListener (this );
246- findButton .addActionListener (this );
247- previousButton .addActionListener (this );
248-
249- // you mustn't replace what you haven't found, my son
250- // semantics of replace are "replace the current selection with the replace field"
251- // so whether we have found before or not is irrelevent
252- // replaceButton.setEnabled(false);
253- // replaceFindButton.setEnabled(false);
211+ setResizable (false );
212+ //centers the dialog on thew screen
213+ setLocationRelativeTo ( null );
254214
255215 // make the find button the blinky default
256216 getRootPane ().setDefaultButton (findButton );
0 commit comments