57
57
import com .intellij .util .ArrayUtil ;
58
58
import com .intellij .util .Consumer ;
59
59
import org .jetbrains .annotations .NonNls ;
60
+ import org .jetbrains .annotations .Nullable ;
60
61
61
62
import javax .swing .*;
62
63
import javax .swing .text .BadLocationException ;
@@ -106,10 +107,14 @@ class FindDialog extends DialogWrapper {
106
107
private JRadioButton myRbCustomScope ;
107
108
private ScopeChooserCombo myScopeCombo ;
108
109
110
+ private LivePreviewController myLivePreviewController ;
111
+
112
+
109
113
public FindDialog (Project project , FindModel model , Runnable myOkHandler ){
110
114
super (project , true );
111
115
myProject = project ;
112
116
myModel = model ;
117
+
113
118
this .myOkHandler = myOkHandler ;
114
119
115
120
if (myModel .isReplaceState ()){
@@ -133,6 +138,7 @@ public FindDialog(Project project, FindModel model, Runnable myOkHandler){
133
138
setOKButtonIcon (IconLoader .getIcon ("/actions/find.png" ));
134
139
init ();
135
140
initByModel ();
141
+ myLivePreviewController = new LivePreviewController (this , new LivePreview (myProject ), getContentPane ());
136
142
}
137
143
138
144
@ Override
@@ -141,6 +147,9 @@ protected void dispose() {
141
147
e .getKey ().removeDocumentListener (e .getValue ());
142
148
}
143
149
myComboBoxListeners .clear ();
150
+ if (myLivePreviewController != null ) {
151
+ myLivePreviewController .cleanUp ();
152
+ }
144
153
super .dispose ();
145
154
}
146
155
@@ -274,6 +283,22 @@ private void handleComboBoxValueChanged(final ComboBox comboBox) {
274
283
validateFindButton ();
275
284
}
276
285
286
+ public FindModel getModel () {
287
+ return myModel ;
288
+ }
289
+
290
+ @ Nullable
291
+ public FindModel getCurrentModel () {
292
+ FindModel validateModel = (FindModel )myModel .clone ();
293
+ applyTo (validateModel , false );
294
+
295
+
296
+ if (getValidationInfo (validateModel ) == null ) {
297
+ return validateModel ;
298
+ }
299
+ return null ;
300
+ }
301
+
277
302
private static int getCaretPosition (JComboBox comboBox ) {
278
303
Component editorComponent = comboBox .getEditor ().getEditorComponent ();
279
304
if (editorComponent instanceof JTextField ){
@@ -293,7 +318,6 @@ private static void setCaretPosition(JComboBox comboBox, int position) {
293
318
294
319
private void validateFindButton () {
295
320
final String toFind = getStringToFind ();
296
-
297
321
if (toFind == null || toFind .length () == 0 ){
298
322
setOKStatus (false );
299
323
return ;
@@ -416,76 +440,130 @@ public void doOKAction() {
416
440
417
441
private void doOKAction (boolean findAll ) {
418
442
FindModel validateModel = (FindModel )myModel .clone ();
419
- applyTo (validateModel );
420
- validateModel .setFindAll (findAll );
421
- if (validateModel .getDirectoryName () != null ) {
422
- PsiDirectory directory = FindInProjectUtil .getPsiDirectory (validateModel , myProject );
423
- if (directory == null ) {
443
+ applyTo (validateModel , findAll );
444
+
445
+ ValidationInfo validationInfo = getValidationInfo (validateModel );
446
+
447
+ if (validationInfo == null ) {
448
+
449
+ myModel .copyFrom (validateModel );
450
+ updateFindSettings ();
451
+
452
+ super .doOKAction ();
453
+ myOkHandler .run ();
454
+ } else {
455
+ String message = validationInfo .message ;
456
+ if (message != null ) {
424
457
Messages .showMessageDialog (
425
458
myProject ,
426
- FindBundle . message ( "find.directory.not.found.error" , validateModel . getDirectoryName ()) ,
459
+ message ,
427
460
CommonBundle .getErrorTitle (),
428
461
Messages .getErrorIcon ()
429
462
);
430
- return ;
463
+ }
464
+ }
465
+ }
466
+
467
+ private void updateFindSettings () {
468
+ FindSettings findSettings = FindSettings .getInstance ();
469
+ findSettings .setCaseSensitive (myModel .isCaseSensitive ());
470
+ if (myModel .isReplaceState ()) {
471
+ findSettings .setPreserveCase (myModel .isPreserveCase ());
472
+ }
473
+
474
+ findSettings .setWholeWordsOnly (myModel .isWholeWordsOnly ());
475
+ findSettings .setInStringLiteralsOnly (myModel .isInStringLiteralsOnly ());
476
+ findSettings .setInCommentsOnly (myModel .isInCommentsOnly ());
477
+
478
+ findSettings .setRegularExpressions (myModel .isRegularExpressions ());
479
+ if (!myModel .isMultipleFiles ()){
480
+ findSettings .setForward (myModel .isForward ());
481
+ findSettings .setFromCursor (myModel .isFromCursor ());
482
+
483
+ findSettings .setGlobal (myModel .isGlobal ());
484
+ } else {
485
+ String directoryName = myModel .getDirectoryName ();
486
+ if (directoryName != null && !directoryName .isEmpty ()) {
487
+ findSettings .setWithSubdirectories (myModel .isWithSubdirectories ());
488
+ }
489
+ else if (myRbModule .isSelected ()) {
490
+ }
491
+ else if (myRbCustomScope .isSelected ()) {
492
+ SearchScope selectedScope = myScopeCombo .getSelectedScope ();
493
+ String customScopeName = selectedScope == null ? null : selectedScope .getDisplayName ();
494
+ findSettings .setCustomScope (customScopeName );
495
+ }
496
+ }
497
+
498
+ if (myCbToSkipResultsWhenOneUsage != null ){
499
+ findSettings .setSkipResultsWithOneUsage (
500
+ isSkipResultsWhenOneUsage ()
501
+ );
502
+ }
503
+
504
+ findSettings .setFileMask (myModel .getFileFilter ());
505
+ }
506
+
507
+ @ Override
508
+ protected boolean postponeValidation () {
509
+ return true ;
510
+ }
511
+
512
+ private ValidationInfo getValidationInfo (FindModel model ) {
513
+ if (myRbDirectory != null && myRbDirectory .isEnabled () && myRbDirectory .isSelected ()) {
514
+ PsiDirectory directory = FindInProjectUtil .getPsiDirectory (model , myProject );
515
+ if (directory == null ) {
516
+ return new ValidationInfo (FindBundle .message ("find.directory.not.found.error" , getDirectory ()), myDirectoryComboBox );
431
517
}
432
518
}
433
519
434
- if (validateModel .isRegularExpressions ()) {
435
- String toFind = validateModel .getStringToFind ();
520
+ String toFind = (String )myInputComboBox .getSelectedItem ();
521
+ if (toFind != null && toFind .isEmpty ()) {
522
+ return new ValidationInfo ("String to find is empty" , myInputComboBox );
523
+ }
524
+
525
+ if (myCbRegularExpressions != null && myCbRegularExpressions .isEnabled ()) {
436
526
try {
437
- Pattern pattern = Pattern .compile (toFind , validateModel .isCaseSensitive () ? Pattern .MULTILINE : Pattern .MULTILINE | Pattern .CASE_INSENSITIVE );
527
+ boolean isCaseSensitive = myCbCaseSensitive != null && myCbCaseSensitive .isEnabled ();
528
+ Pattern pattern =
529
+ Pattern .compile (toFind , isCaseSensitive ? Pattern .MULTILINE : Pattern .MULTILINE | Pattern .CASE_INSENSITIVE );
438
530
if (pattern .matcher ("" ).matches () && !toFind .endsWith ("$" ) && !toFind .startsWith ("^" )) {
439
- throw new PatternSyntaxException (FindBundle .message ("find.empty.match.regular.expression.error" ),toFind , - 1 );
531
+ return new ValidationInfo (FindBundle .message ("find.empty.match.regular.expression.error" ), myInputComboBox );
440
532
}
441
533
}
442
- catch (PatternSyntaxException e ){
443
- Messages .showMessageDialog (
444
- myProject ,
445
- FindBundle .message ("find.invalid.regular.expression.error" , toFind , e .getDescription ()),
446
- CommonBundle .getErrorTitle (),
447
- Messages .getErrorIcon ()
448
- );
449
- return ;
534
+ catch (PatternSyntaxException e ) {
535
+ return new ValidationInfo (FindBundle .message ("find.invalid.regular.expression.error" , toFind , e .getDescription ()), myInputComboBox );
450
536
}
451
537
}
452
538
453
- validateModel .setFileFilter ( null );
454
- FindSettings .getInstance ().setFileMask (null );
455
-
456
- if (useFileFilter !=null && useFileFilter .isSelected () &&
457
- myFileFilter .getSelectedItem ()!=null
458
- ) {
459
- final String mask = (String )myFileFilter .getSelectedItem ();
539
+ final String mask = myFileFilter == null ? null : (String )myFileFilter .getSelectedItem ();
460
540
461
- if (mask .length () > 0 ) {
541
+ if (mask != null ) {
542
+ if (mask .length () == 0 ) {
543
+ return new ValidationInfo (FindBundle .message ("find.filter.empty.file.mask.error" ), myFileFilter );
544
+ }
545
+ else {
462
546
try {
463
547
FindInProjectUtil .createFileMaskRegExp (mask ); // verify that the regexp compiles
464
- validateModel .setFileFilter (mask );
465
- FindSettings .getInstance ().setFileMask (mask );
466
548
}
467
549
catch (PatternSyntaxException ex ) {
468
- Messages .showMessageDialog (myProject , FindBundle .message ("find.filter.invalid.file.mask.error" , myFileFilter .getSelectedItem ()),
469
- CommonBundle .getErrorTitle (), Messages .getErrorIcon ());
470
- return ;
550
+ return new ValidationInfo (FindBundle .message ("find.filter.invalid.file.mask.error" , mask ), myFileFilter );
471
551
}
472
552
}
473
- else {
474
- Messages .showMessageDialog (myProject , FindBundle .message ("find.filter.empty.file.mask.error" ), CommonBundle .getErrorTitle (),
475
- Messages .getErrorIcon ());
476
- return ;
477
- }
478
553
}
554
+ return null ;
555
+ }
479
556
480
- if (myCbToSkipResultsWhenOneUsage != null ){
481
- FindSettings .getInstance ().setSkipResultsWithOneUsage (
482
- isSkipResultsWhenOneUsage ()
483
- );
484
- }
557
+ @ Override
558
+ protected ValidationInfo doValidate () {
559
+ FindModel validateModel = (FindModel )myModel .clone ();
560
+ applyTo (validateModel , false );
561
+
562
+ ValidationInfo result = getValidationInfo (validateModel );
563
+
564
+ setOKStatus (result == null );
485
565
486
- myModel .copyFrom (validateModel );
487
- super .doOKAction ();
488
- myOkHandler .run ();
566
+ return result ;
489
567
}
490
568
491
569
public void doHelpAction () {
@@ -768,7 +846,7 @@ public void actionPerformed(ActionEvent e) {
768
846
public void consume (final VirtualFile [] files ) {
769
847
if (files .length != 0 ) {
770
848
myDirectoryComboBox .setSelectedItem (files [0 ].getPresentableUrl ());
771
- validateFindButton ();
849
+ // validateFindButton();
772
850
}
773
851
}
774
852
});
@@ -845,10 +923,12 @@ private JPanel createOriginPanel() {
845
923
}
846
924
847
925
private String getStringToFind () {
848
- return (String )myInputComboBox .getEditor ().getItem ();
926
+ String string = (String )myInputComboBox .getEditor ().getItem ();
927
+ return string == null ? "" : string ;
849
928
}
850
929
private String getStringToReplace () {
851
- return (String )myReplaceComboBox .getEditor ().getItem ();
930
+ String item = (String )myReplaceComboBox .getEditor ().getItem ();
931
+ return item == null ? "" : item ;
852
932
}
853
933
854
934
private String getDirectory () {
@@ -886,27 +966,26 @@ private void setDirectories(ArrayList strings, String directoryName) {
886
966
}
887
967
}
888
968
889
- private void applyTo (FindModel model ) {
890
- FindSettings findSettings = FindSettings .getInstance ();
969
+
970
+
971
+ private void applyTo (FindModel model , boolean findAll ) {
972
+
891
973
model .setCaseSensitive (myCbCaseSensitive .isSelected ());
892
- findSettings .setCaseSensitive (myCbCaseSensitive .isSelected ());
893
974
894
975
if (model .isReplaceState ()) {
895
976
model .setPreserveCase (myCbPreserveCase .isSelected ());
896
- findSettings .setPreserveCase (myCbPreserveCase .isSelected ());
897
977
}
898
978
899
979
model .setWholeWordsOnly (myCbWholeWordsOnly .isSelected ());
900
- findSettings .setWholeWordsOnly (myCbWholeWordsOnly .isSelected ());
901
980
model .setInStringLiteralsOnly (myCbInStringLiteralsOnly .isSelected ());
902
- findSettings .setInStringLiteralsOnly (myCbInStringLiteralsOnly .isSelected ());
903
981
904
982
model .setInCommentsOnly (myCbInCommentsOnly .isSelected ());
905
- findSettings .setInCommentsOnly (myCbInCommentsOnly .isSelected ());
906
983
907
984
model .setRegularExpressions (myCbRegularExpressions .isSelected ());
908
- findSettings .setRegularExpressions (myCbRegularExpressions .isSelected ());
909
- model .setStringToFind (getStringToFind ());
985
+ String stringToFind = getStringToFind ();
986
+ if (stringToFind .length () > 0 ) {
987
+ model .setStringToFind (stringToFind );
988
+ }
910
989
911
990
if (model .isReplaceState ()){
912
991
model .setPromptOnReplace (true );
@@ -920,11 +999,8 @@ private void applyTo(FindModel model) {
920
999
921
1000
if (!model .isMultipleFiles ()){
922
1001
model .setForward (myRbForward .isSelected ());
923
- findSettings .setForward (myRbForward .isSelected ());
924
1002
model .setFromCursor (myRbFromCursor .isSelected ());
925
- findSettings .setFromCursor (myRbFromCursor .isSelected ());
926
1003
model .setGlobal (myRbGlobal .isSelected ());
927
- findSettings .setGlobal (myRbGlobal .isSelected ());
928
1004
}
929
1005
else {
930
1006
if (myCbToOpenInNewTab != null ){
@@ -942,7 +1018,6 @@ private void applyTo(FindModel model) {
942
1018
String directory = getDirectory ();
943
1019
model .setDirectoryName (directory == null ? "" : directory );
944
1020
model .setWithSubdirectories (myCbWithSubdirectories .isSelected ());
945
- findSettings .setWithSubdirectories (myCbWithSubdirectories .isSelected ());
946
1021
}
947
1022
else if (myRbModule .isSelected ()) {
948
1023
model .setModuleName ((String )myModuleComboBox .getSelectedItem ());
@@ -953,9 +1028,17 @@ else if (myRbCustomScope.isSelected()) {
953
1028
model .setCustomScopeName (customScopeName );
954
1029
model .setCustomScope (selectedScope == null ? null : selectedScope );
955
1030
model .setCustomScope (true );
956
- findSettings .setCustomScope (customScopeName );
957
1031
}
958
1032
}
1033
+
1034
+ model .setFindAll (findAll );
1035
+
1036
+ String mask = null ;
1037
+ if (useFileFilter !=null && useFileFilter .isSelected ()) {
1038
+ mask = (String )myFileFilter .getSelectedItem ();
1039
+ }
1040
+ model .setFileFilter (mask );
1041
+
959
1042
}
960
1043
961
1044
@@ -1061,7 +1144,7 @@ else if (myModel.getModuleName() != null) {
1061
1144
setStringsToComboBox (FindSettings .getInstance ().getRecentReplaceStrings (), myReplaceComboBox , myModel .getStringToReplace ());
1062
1145
}
1063
1146
updateControls ();
1064
- validateFindButton ();
1147
+
1065
1148
}
1066
1149
}
1067
1150
0 commit comments