-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the functionality of the regex search with control decorations
- Loading branch information
1 parent
090a18a
commit 91762a5
Showing
5 changed files
with
141 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
bundles/org.eclipse.ui/src/org/eclipse/ui/internal/SearchDecoration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Vector Informatik GmbH and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Vector Informatik GmbH - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.ui.internal; | ||
|
||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
import org.eclipse.jface.fieldassist.ControlDecoration; | ||
import org.eclipse.jface.fieldassist.FieldDecorationRegistry; | ||
import org.eclipse.swt.graphics.Image; | ||
|
||
/** | ||
* This class contains methods to validate and decorate search fields. | ||
*/ | ||
public class SearchDecoration { | ||
|
||
private SearchDecoration() { | ||
// avoid instantiation | ||
} | ||
|
||
/** | ||
* Validate the given regular expression and change the control decoration | ||
* accordingly. If the expression is invalid then the decoration will show an | ||
* error icon and a message and if the expression is valid then the decoration | ||
* will be hidden. | ||
* | ||
* @param regex The regular expression to be validated. | ||
* @param targetDecoration The control decoration that will show the result of | ||
* the validation. | ||
*/ | ||
public static void validateRegex(String regex, ControlDecoration targetDecoration) { | ||
String errorMessage = getValidationError(regex); | ||
if (errorMessage.isEmpty()) { | ||
targetDecoration.hide(); | ||
return; | ||
|
||
} | ||
|
||
Image decorationImage = FieldDecorationRegistry.getDefault() | ||
.getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).getImage(); | ||
targetDecoration.setImage(decorationImage); | ||
targetDecoration.setDescriptionText(errorMessage); | ||
targetDecoration.show(); | ||
} | ||
|
||
/** | ||
* Validate a regular expression. | ||
* | ||
* @return The appropriate error message if the regex is invalid or an empty | ||
* string if the regex is valid. | ||
*/ | ||
private static String getValidationError(String regex) { | ||
try { | ||
Pattern.compile(regex); | ||
return ""; //$NON-NLS-1$ | ||
} catch (PatternSyntaxException e) { | ||
String message = e.getLocalizedMessage(); | ||
|
||
// Only preserve the first line of the original error message. | ||
int i = 0; | ||
while (i < message.length() && "\n\r".indexOf(message.charAt(i)) == -1) { //$NON-NLS-1$ | ||
i++; | ||
} | ||
|
||
return message.substring(0, i); | ||
} | ||
} | ||
|
||
} |