1414
1515package org .eclipse .ui .internal ;
1616
17+
1718import java .util .regex .Pattern ;
1819import java .util .regex .PatternSyntaxException ;
1920
2021import org .eclipse .jface .fieldassist .ControlDecoration ;
2122import org .eclipse .jface .fieldassist .FieldDecorationRegistry ;
23+ import org .eclipse .swt .graphics .GC ;
2224import org .eclipse .swt .graphics .Image ;
2325
2426/**
2527 * This class contains methods to validate and decorate search fields.
2628 */
2729public class SearchDecoration {
30+ private static GC gc ;
2831
2932 private SearchDecoration () {
3033 // avoid instantiation
@@ -41,6 +44,8 @@ private SearchDecoration() {
4144 * the validation.
4245 */
4346 public static boolean validateRegex (String regex , ControlDecoration targetDecoration ) {
47+ gc = new GC (targetDecoration .getControl ());
48+
4449 String errorMessage = getValidationError (regex );
4550 if (errorMessage .isEmpty ()) {
4651 targetDecoration .hide ();
@@ -68,14 +73,39 @@ private static String getValidationError(String regex) {
6873 return "" ; //$NON-NLS-1$
6974 } catch (PatternSyntaxException e ) {
7075 String message = e .getLocalizedMessage ();
76+ StringBuilder sBuilder = new StringBuilder ();
7177
72- // Only preserve the first line of the original error message.
7378 int i = 0 ;
7479 while (i < message .length () && "\n \r " .indexOf (message .charAt (i )) == -1 ) { //$NON-NLS-1$
7580 i ++;
7681 }
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 );
77106
78- return message .substring (0 , i );
107+ sBuilder .append ("^" ); //$NON-NLS-1$
108+ return sBuilder .toString ();
79109 }
80110 }
81111
0 commit comments