Java SWT User Input Validation - java

What is the Java convention when it comes to user input validation in SWT? I read that there are FieldEditors which are very convenient fields but sadly only for preferences and dialogue boxes.
I also read that there is an IValidator interface. But it is often used with data binding, which is in my case, most of my inputs do not need any data binding yet. Also, IValidator requires me to write my own validation methods even for simple validations such as integer only, letters only, etc.
Since the FieldEditors cannot be applied in normal usage of input fields, what other convenient way can I use to do validation on user inputs? I am using SWT for my Java GUI.
Thanks!

You can validate the input of your control in a VerifyListener. See this forum thread for further explanation.
Alternatively, you can check out RCP Toolbox which has a built-in easy-to-use validation framework. See this article.

Related

assign data from dto to group of text fields in javafx

I'm new to JavaFX and what I'm trying to do is I have a DTO object with 15 fields that I fetch from backend which I need to show all the fields in screen
for now what I did for each filed will create textfield in fxml file and inject it in the controller using textfield id then set the text for that from the dto for example
#FXML
TextField firstName;
........
firstName.setText(dto.getFirstName)
so is there is any other way than going through each textfield and using setText to set their value
As you state that you are new to JavaFX, I do not recommend that you try to implement the potential approaches in this answer.
Investigate the controlsfx BeanPropertyUtils, PropertySheet and PropertyEditor.
Different potential implementation strategies:
You could use a collection of TextFields, e.g. a list and assign values sequentially based on, for example, position in an sql row set.
Or perform a lookup on a map of strings to TextFields based on a column name key.
Or use reflection on Java Beans (this is how controlsfx works).
But none of them would be worth implementing unless you have a great many fields and need some generic system to handle values. Otherwise I wouldn’t recommend implementing such abstract functionality.
An example of a generic use case would be if you were introspecting on an unknown large database schema.
If you do need to do this, probably your best place to start would be the controlsfx library.
SceneBuilderKit, which was used to build SceneBuilder, has similar functionality but it is not as easily accesible as ControlsFX.
I advise you review the above comments then decide if you really want to do this.
If you do, then choose one potential strategy and implement it (this won’t be done in a StackOverflow answer).
If stuck, provide a complete minimal reproducible example for a concrete example and implementation attempt in code, only for the art of the problem you are stuck on. This allows you to create a more concrete question that is greatly reduced in scope.

Language switching - l10n I18n

How does language switching work in Java/Vaadin? I have a web application and would like to integrate a combo box, that changes the language of every text in this application. Do i need to mark each text that should get translated manually and define its translation? How complex is it to implement this function into an exting project?
Do i need to mark each text that should get translated manually and define its translation?
You should use ResourceBundles to store/read translations of strings.
How does language switching work in Java/Vaadin?
You need to provide a class that implements I18NProvider. Documentation about that can be found here
Once implemented correctly, you will be able to call getTranslation("HelloWorld") on any Component (and therefore on any view since they must extend a component), to receive the translation of the key "HelloWorld" defined in the ResourceBundle-file of the current UI-Locale.
Views that extend LocaleChangeObserver are notified when the Locale is changed, and then you can call getTranslation("HelloWorld") again to find the translation of the freshly set language.
I would like to integrate a combo box, that changes the language of every text in this application.
See this SO answer of mine where I posted example code of a Select component that acts as a language switcher. It is using both ResourceBundle and I18NProvider. (You can use a ComboBox too, but with the downside that you can only display a String for the selected value)
The important part in that code there is that the Select has a ValueChangeListener that sets the Locale of the VaadinSession, which in turn will trigger the localeChange method of the LocaleChangeObserver that your view now should implement. In the localeChange method, you can re-translate the translatable Strings of every component in the view; set new texts in Labels, set new labels and placeholders for TextFields, etc etc.
How complex is it to implement this function into an exting project?
That depends very much on your definition of complex, and how familiar you already are with ResourceBundles. There certainly are less complex topics than this, but I18N is never easy. In my opinion, Vaadin has done a very great job of providing us devs with a way to use I18N in our applications.
Most people use a sort of translation file system for their localization. Basically you make a text file for each language with a key and value system where you name every translated message with a key and a translated value. You can then use these keys (that should be predefined) to get the correct message for the language you want. These files can be anything really, but if you're looking for a simple java implementation then there are pretty simple ways to do it. For an example look here.
Did you look at this section of the documentation? https://vaadin.com/docs/v14/flow/advanced/tutorial-i18n-localization.html

Something similar to GenericDialog/ADM

I'm looking for something similar to GenericDialog used in ImageJ or Adobe Dialog Manager. The goal is to create a template of a dialog (number of fields, types etc - info that is needed, not how it is presented) and send it to view-class. This view may be Swing based GUI or simple console UI. User fills the fields and values are visible for the source of the dialog template.
Is there some library for Java that implements this?
I know there is GenericDialog, but I'm not sure if I can use it in my project (as it is a part of ImageJ). What is more, I feel it's to 'heavy' for me.
The ImageJ2 project provides almost exactly what you describe: a flexible mechanism for executing runnable operations (called commands) with typed input and output parameters.
You define the command's inputs and outputs by labeling them with the #Parameter annotation. ImageJ automatically takes care of filling in the inputs (typically by prompting the user for input using a dialog box), as well as displaying the outputs after the command has run.
How the inputs are harvested from the user depends on which (if any) user interface is associated with the ImageJ context. We have implemented full widget support for Swing, as well as proof-of-concept implementations in "pure" AWT (i.e., java.awt widgets), Apache Pivot and Eclipse SWT.
Some examples:
GradientImage.java: A simple example command
ParameterTester.java: A command exercising many types of parameters
All of the code is BSD-2 licensed. The ij-core JAR containing the framework is ~334KB as of this writing. The ij-ui-swing JAR containing the Swing widgets is ~150KB, but it contains other things as well which could be stripped out (for comparison, the ij-ui-pivot JAR is only ~30KB).
All of that said, if you think ImageJ 1.x's GenericDialog is "too heavy" then you will very likely feel the same about ImageJ2's command framework (or really any other solution to this problem; I don't think it can get much "lighter" than GenericDialog). But in that case, perhaps the ImageJ2 implementation will give you some ideas on how to roll your own.
Generic dialog in ImageJ extends java.awt.Component so doesn't this contradict your requirement that the template for the dialog be decoupled from the view?
Java's collections would allow you to encapsulate the info, types etc.
java.util.LinkedHashMap<String,Type> dialog = new java.util.LinkedHashMap<String,Type>;
where the key is the name of the field and the value is the type of the field.
To create a swing based dialog see
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Can Java self-modify via user input?

I'm interested in an executed script allowing user input to modify the process and corresponding source.
What precedents exist to implement such a structure?
Yes, depending on what is meant.
Consider such projects as ObjectWeb ASM (see the the ASM 2.0 tutorial for a general rundown).
Trying to emit the-would-need-to-be-decompiled Java source code is another story: if this was the goal then perhaps the source should be edited, re-compiled, and somehow loaded in/over. (This is possible as well, consider tools like JRebel.)
Happy coding.
You should not be able to modify existing classes. But if you implement a ClassLoader then you can dynamically load classes from non-traditional sources: network, XML file, user input, random number generator, etc.
There are probably other, better ways.
Maybe the Java scripting API is what you're looking for:
http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html
http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html
I wrote an app once that used reflection to allow tests to be driven by a text file. For instance, if you had a class like this:
class Tuner(String Channel) {
tune(){...
play(){...
stop(){...
}
You could execute methods via code like:
tuner=Channel 1
tune tuner
play tuner
stop tuner
It had some more capabilities (You could pass objects into other objects, etc), but mostly I used it to drive tests on a cable box where a full write/build/deploy in order to test took on the order of a half hour.
You could create a few reusable classes and tie them together with this test language to make some very complex and easy to create tests.
THAT is a DSL, not monkeying around with your loose-syntax language by eliminating parenthesis and adding underscores and dots in random locations to make it look like some strange semi-English.

Is there a Java web framework that allows the user to create custom fields (like in Mantis bugtracker)?

Is there a Java web framework that allows
the user to create custom fields, like in Mantis bugtracker?
http://www.mantisbt.org/wiki/doku.php/mantisbt:features
Here's what I actually want to do:
Allow the user to create a template / form that has several custom fields
(textboxes, checkboxes, comboboxes, etc.)
Once a template has been created (of which several others can also be made),
he can use the template, and save data inputted into the fields.
I am planning to use Spring and Hibernate along with this,
though other suggestions are also appreciated.
Like comments suggests, it is not a framework duty to provide you that kind of fields, it is your duty, as an application developper, and depending upon your requirements, to provide such a feature.

Categories

Resources