14
14
15
15
package org .eclipse .ui .internal ;
16
16
17
+
17
18
import java .util .regex .Pattern ;
18
19
import java .util .regex .PatternSyntaxException ;
19
20
20
21
import org .eclipse .jface .fieldassist .ControlDecoration ;
21
22
import org .eclipse .jface .fieldassist .FieldDecorationRegistry ;
23
+ import org .eclipse .swt .graphics .GC ;
22
24
import org .eclipse .swt .graphics .Image ;
23
25
24
26
/**
25
27
* This class contains methods to validate and decorate search fields.
26
28
*/
27
29
public class SearchDecoration {
30
+ private static GC gc ;
28
31
29
32
private SearchDecoration () {
30
33
// avoid instantiation
@@ -41,6 +44,8 @@ private SearchDecoration() {
41
44
* the validation.
42
45
*/
43
46
public static boolean validateRegex (String regex , ControlDecoration targetDecoration ) {
47
+ gc = new GC (targetDecoration .getControl ());
48
+
44
49
String errorMessage = getValidationError (regex );
45
50
if (errorMessage .isEmpty ()) {
46
51
targetDecoration .hide ();
@@ -68,14 +73,39 @@ private static String getValidationError(String regex) {
68
73
return "" ; //$NON-NLS-1$
69
74
} catch (PatternSyntaxException e ) {
70
75
String message = e .getLocalizedMessage ();
76
+ StringBuilder sBuilder = new StringBuilder ();
71
77
72
- // Only preserve the first line of the original error message.
73
78
int i = 0 ;
74
79
while (i < message .length () && "\n \r " .indexOf (message .charAt (i )) == -1 ) { //$NON-NLS-1$
75
80
i ++;
76
81
}
82
+ int j = i + 2 ;
83
+ while (j < message .length () && "\n \r " .indexOf (message .charAt (j )) == -1 ) { //$NON-NLS-1$
84
+ j ++;
85
+ }
86
+
87
+ String firstLine = message .substring (0 , i );
88
+
89
+ String indexString = firstLine .replaceAll ("\\ D+" , "" ); //$NON-NLS-1$ //$NON-NLS-2$
90
+ int errorIndex = Integer .parseInt (indexString );
91
+
92
+ sBuilder .append (firstLine );
93
+ sBuilder .append (System .lineSeparator ());
94
+
95
+ String secondLine = message .substring (i + 2 , j ); // the +2 because of the \n\r
96
+ sBuilder .append (secondLine );
97
+ sBuilder .append (System .lineSeparator ());
98
+
99
+ String subsString = secondLine .substring (0 , errorIndex );
100
+ String string = "" ; //$NON-NLS-1$
101
+
102
+ while (gc .stringExtent (string ).x < gc .stringExtent (subsString ).x ) {
103
+ string += " " ; //$NON-NLS-1$
104
+ }
105
+ sBuilder .append (string );
77
106
78
- return message .substring (0 , i );
107
+ sBuilder .append ("^" ); //$NON-NLS-1$
108
+ return sBuilder .toString ();
79
109
}
80
110
}
81
111
0 commit comments