gxt, gwt, Java. Validation of textfield without infobubble - java

I have to check some textField with the following rule:
if (the value A != the value B) {
this.A.forceInvalid("");
}
This work really fine, but I want to remove the "info bubble" of the error,on the textfield.
I have tried to simply put a css with red border around it, but it seems that my superior dont want that.
How can I erase that bubble?

First of all, this validation looks wrong. It seems you are using gxt Field which already has built-in validators. Here is an example:
yourField.addValidator(new Validator<String>() {
#Override
public List<EditorError> validate(Editor<String> editor, String value) {
final List<EditorError> errors = new ArrayList<EditorError>();
if (!value.equals("test")) {
errors.add(new DefaultEditorError(yourField, "Value is not \"test\"", value));
}
return errors;
}
});
By default it looks like this:
You can change it by using non-default ErrorHandler on the field. It is not clear what exactly do you want, as far as I can understand you want to get rid of (!) sign and its popup. I think that TitleErrorHandler will suit your needs.
yourField.setErrorSupport(new TitleErrorHandler(yourField));

Related

Enum toString sometimes replacing i with ı

I recently got a report that a few Google Analytics event category names were being recorded with an i character with out a dot on top.
Pageviews and events occurring twice, once without dots over the i.
I had to look to believe it. Sure enough, I had an event called favorite and there was a handful called favorıte. Copy and paste that weird character into a terminal or a monospace font just to see how weird it is. favorıte
My first suspicion is my code where I generate the strings for the category names using toString on an enum.
public enum AnalyticsEvent {
SCREEN_VIEW,
FAVORITE,
UN_FAVORITE,
CLICK_EVENT,
... reduced for brevity;
public String val() {
return this.toString().toLowerCase();
}
}
Example of how that enum is used:
#Override
public void logSearchTag(String type, String value) {
...
logGAEvent(AnalyticsEvent.SEARCH_TAG.val(), type, value);
}
private void logGAEvent(String category, String action, String label) {
... // mGATracker = instance of com.google.android.gms.analytics.Tracker;
mGATracker.send(addCustomDimensions(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label))
.build());
...
}
I am going to solve this by actually assigning a string to the enums and instead return that in the val() function.
Though, I am curious if anyone knows why on a small handful of devices Enum.toString returns the enum name with that weird character replacing the i. I mean small. 8 out 50,000 is the average. Or is it possible that assumption is wrong and the error is on analytics service end somewhere? Really highly doubt that.
The String#toLowerCase method uses the default locale of the system. This use locale specific characters such as ı instead of i. In order to fix this problem call toLowerCase with a locale:
String test = "testString";
test.toLowerCase(java.util.Locale.ENGLISH) // Or your preferred locale

JAVA How to retrieve text content from custom JLabel without the HTML taggings?

How do I retrieve the text from JLabel without the HTML taggings?
E.g.
CustomJLabel:
public CustomJLabel extends JLabel(){
private String text;
public CustomJLabel(String text) {
super("<html><div style='text-align: center;'>"+text+"</div></html>"),
this.text=text;
}
}
Main method:
testCustomLbl = new CustomJLabel("Testing");
System.out.println(testCustomLbl.getText());
Output I got:
<html><div style='text-align: center;'>Testing</div></html>
Desired output:
Testing
There are three options:
You pick your favorite HTML parser and parse HTML; see here for some inspiration. This is by far the most robust and straight forward solution; but of course: costly.
If you are well aware of the exact HTML content that goes into your labels, then you could turn to regular expressions; or other means of string parsing. The problem is: if you don't control those strings, then coming up with your own custom "parsing" is hard. Because each and any change somewhere to the HTML that goes in ... might break your little parser.
You rework your whole design: if having HTML text is such a core thing in your application, you might consider to really "represent" that in your class. For example by creating your own versions of JLabels that take some HtmlString input ... and simply remember which parts are HTML, and which one "pure text".
And whoops; the code you are showing is already suited for option 3. So if you want that getText() returns that original text, you could add a simple
#Override
public void String getText() {
return this.text;
}
to your CustomLabel class.
Edit: alternatively, you could simply add a new method like
public void String getTextWithoutHtmlTags()
or something alike; as overriding that inherited method somehow changes the "contract" of that method. Which (depending on the context) might be ok, or not so ok.
There's no need for complex code or 3rd party JARS / Libraries.
Here's a simple solution using RegEx:
String htmlStr = "<html><h1>Heading</h1> ...... </html>";
String noHtmlStr = htmlStr.replaceAll("\\<.*?\\>", "");
Works great for me.
Hope this helps.

Eclipse CDT extend to build an additional language

I would like to extend the CDT language plugin for gnu c, to create some kind of new language based on this.
The new language should have a different visual appearence in editor. I would like to color a method body in gray if there is a special pre-processor directive in front of (like an annotation).
Does anybody know where to extend the GCC language for a modification like this?
EDIT1:
As example I want that the colour of the method body of specialFunction to be gray, as a reason of an example annotation -> #annotation
#annotation
int specialFunction(){
return 1;
}
EDIT2:
What i have tried so far is to build a "extended language". The plan was to highlight a preprocessor position and also save the position so that the method below would be colored. I managed to get the preprocessor keyword colored but not how to work on method bodies color.
public class OwnKeyWords extends GCCLanguage implements ICLanguageKeywords
#Override
public String[] getPreprocessorKeywords() {
//System.out.println("Called keywords" + timesPre++);
return new String[]{
"hide",
"show"
};
}
Example to be colored:
#hide
int specialFunction(){
return 1;
}
In the example above "hide" would be highlighted.
EDIT3:
I tried to implement ISemanticHighlighter and tried a few ways to highlight my code:
CVariable
CFunction
ObjectStyleMacro
...
But non of them where suitable to highlight a method body with a preprocessor directive or anything else on top.
Also the note in ISemanticHighlighter:
* NOTE: Implementors are not allowed to keep a reference on the token or on any object retrieved from the
* token.
Is not what I want to achieve, because I would like to keep reference to the highlighted object for later operations.
Maybe the org.eclipse.cdt.ui.text.folding.DefaultCFoldingStructureProvider would be an option too, there I couldn't colour the instrumentation, I could instead hide it.
This does not sound like a new language so much as semantic highlighting.
CDT has a great extension point called org.eclipse.cdt.ui.semanticHighlighting that allows you to define custom semantic highlighting rules.
Here is an example of the plugin.xml entry:
<extension
point="org.eclipse.cdt.ui.semanticHighlighting">
<semanticHighlighting
class="com.example.SemanticHighlighter"
defaultBold="true"
defaultEnabled="true"
defaultTextColor="35,0,35"
displayName="Example Semantic Highlighter"
id="com.example.SemanticHighlighter"
preferenceKey="com.example.SemanticHighlighter.pref"
priority="5">
</semanticHighlighting>
</extension>
Then in your com.example.SemanticHighlighter you implement the org.eclipse.cdt.ui.text.ISemanticHighlighter interface. There is only one method, consumes that takes a ISemanticToken. Analyse the token to see if it is relevant to your highlighter and return true to have it highlighted.
Here is a trivial implementation of the method:
#Override
public boolean consumes(ISemanticToken token) {
IBinding binding = token.getBinding();
if (binding instanceof IFunction) {
IASTNode node = token.getNode();
if (binding != null && node instanceof IASTName && ((IASTName) node).isReference()) {
String n = binding.getName();
if ("MySpecialFunction".equals(n)) {
return true;
}
}
}
return false;
}
Once implemented, users can modify the colour and applicability via the preference page C/C++ - Editor - Syntax Coloring:
You should be able to use ISemanticHighlighter to do the kind of highlighting you want.
For your example of coloring the body of a function with a particular annotation, it could work like this:
class MyHighlighter implements ISemanticHighlighter {
#Override
public boolean consumes(ISemanticToken token) {
IASTNode node = token.getNode();
// Walk up the AST to determine if 'node' is inside a function body.
// If it's not, return false.
// Navigate the AST some more to examine what comes before the
// function's declaration. If it's the annotation in question,
// return true. Otherwise, return false.
}
}
I left out the details of how to navigate the AST, but CDT has a pretty rich AST API so it's definitely doable. Feel free to ask specific questions if you have them.

How to reset back to default css after adding style?

Basically I have changed the css for a text field in javafx by adding a style class like this:
textfield.getStyleClass().add("textfieldstyle");
But then I want to be able to revert it back to its original appearance. But since the original appearance in this case is the default skin for JavaFX, I can't find the original layout for the textfields. I found the textfieldskin properties here, but its a jungle, and I can't find anything about the color of the -fx-control-inner-background, -fx-text-box-border and -fx-focus-color, which is what I want to know.
I've triedtextfield.getStyleClass().remove("textfieldstyle"); and think that does remove the new css, but it doesn't apply the old one again.
Thanks to the comments by #UlukBiy and #varren I solved the issue. System.out.println(textfield.getStyleClass()); was of great use since it allowed me to check which style classes were applied on the text field as default. And as it is pointed out in the comments those where text-input and text-field.
So to restore the text field's css to its default value I just did:
textfield.getStyleClass().clear();
textfield.getStyleClass().addAll("text-field", "text-input");
To reset an element's default styling after setting it using .setStyle("css settings....."); you can simply use -
textfield.setStyle(null);
I'm not sure if this would work on it's own for an element that's had a class applied using .getStyleClass().add("textfieldstyle"); but if not you could do something like -
textfield.getStyleClass().clear();
textfield.setStyle(null);
A short way to effictively remove your style class even in case of duplicates, with a lambda :
textfield.getStyleClass().removeIf(style -> style.equals("textfieldstyle"));
The following test code works for me when adding and removing a class from a control such as Textfield:
import javafx.scene.control.TextInputControl;
public class test
{
protected void setEditable(final TextInputControl toControl, final boolean tlEditable)
{
toControl.setEditable(tlEditable);
if (tlEditable)
{
if (toControl.getStyleClass().contains("non-editable-class"))
{
toControl.getStyleClass().removeAll("non-editable-class");
}
}
else if (!toControl.getStyleClass().contains("non-editable-class"))
{
toControl.getStyleClass().add("non-editable-class");
}
}
}

GWT - CellTable/DataGrid Using ImageCell

I want to add an ImageCell in a CellTable. my code is the following:
Column<Message, String> myColumn = new Column<Message, String>(new ImageCell()) {
#Override
public String getValue(Message details) {
Image image = new Image (ClientResources.of().image1());
return image.getUrl();
}
};
It shows the following warnning when I run it:
[WARN] [adminportal] - Template with variable in URL attribute context: The template code generator cannot guarantee HTML-safety of the template -- please inspect manually or use SafeUri to specify arguments in a URL attribute context
My question is that how I can return the url as string value safely. I saw an example about using imagecell before and the return value is string. I cannot find it now. Could anyone tell me how to fix this.
Note: I just want to know how to fix this if I want to use imagecell in celltable and return value is string. I know how to use imageresourcecell to achieve the same goal. Also, I know I can change the type of getValue() from string to safeHtml to achieve the goal. But i am really wondering how to achieve this by using imagecell and string type of getValue() because I saw an example about this before and I tried it successfully. Just cannot remember what I did wrong here.
There's no way to fix this (remove the warning): using a String as part of a template is inherently unsafe, and GWT warns you about it. If you're absolutely certain of the safety of your values, then you can safely ignore the warnings, but they'll still be emitted.
The only way to not have those warnings is to use a SafeUri, i.e. use the SafeImagecell. It's then up to you to guarantee the safety of your URL, depending on the method you use to construct the SafeUri (have a look at UriUtils)

Categories

Resources