I'm trying to do the OOP approach in all my xPages. As expected I'm facing several issues, but also have tons of advantages doing so.
My question is related to Views (Repeat controls). I am loading a List<myCustomBean> for my repeat controls that contains all available objects of type myCustomBean and display each myCustomBean the way I want in a Bootstrap table row. That works all fine.
I'm able to sort my List with URL parameter sortedBy=MySortColumn with my own method. - Problem 1 solved.
How would I approach a Categorization in my Repeat Control? So I could easily sort the beans by the Cotegory, but how would I display it, incl. expandable and collapsible twisties? Maybe there is a Custom control that I can use? Or a Control of the Extension Library?
Or do I have to build everything from scratch myself?
Any advice is much appreciated.
The Data View control is probably the best. Like the View Panel or Data View, it's a extension of the Repeat Control. But it has much more flexibility that the View Panel and allows much more configurable layout than the Data View. It has a categoryColumn property, but that's designed for binding to a dominoView datasource. But there is also the categoryRow facet which can be used.
Essentially, using a dominoView component is already using OOP programming. Your repeat is using List<myCustomBean>, dominoView returns List<DominoViewEntry>. Properties on the dominoView are used to interrogate the underlying View object within the database and return only those ViewEntry objects from the ViewNavigator or ViewEntryCollection that are required. It wraps the ViewEntry as a DominoViewEntry object for just a selection of those, based on the rows property of whatever uses the DominoView.
As someone who built a subset of that functionality for use from Vaadin (see my XPages to Web App blog series http://www.intec.co.uk/tag/xpages-to-web-app-tutorial/), within XPages I typipcally use the dominoView object unless I'm extracting a small subset of ViewEntries / Documents. When I use ViewEntryCollection / DocumentCollection, I rarely wrap, preferring to let XPages optimise retrieval rather than re-develop that optimisation myself.
Related
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.
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
If you would like more details please let me know, or refer to the last lines of this question. I've already read a lot and I feel I'm turning something simple into something complicated and I still get stuck here and there, so maybe you can help me in those very specific points.
I'm using Netbeans IDE 7 and JDK 7, and no frameworks. The first Window is a JFrame and all other windows are JDialogs with modal=true.
Questions:
How do I correctly implement the MVC pattern with swing?
From the ideas bellow Which one is better: (A) or (B)? Or maybe another one... Why is it better?
(A)
Main:
MyModel model
MyView view(model)
MyView:
MyController(this, model)
(B)
Main:
MyModel model
MyView View
MyController controller(view, model)
when I click jbutton1 in MainFrame I need it to open the SettingsFrame for editing settings. where should I instantiate the View, the Model and the Controller of the SettingsFrame? In MainFrame Controller?
In terms of MVC organization and implementation, how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)? Should I create empty classes for them?
a. The implementation of a TrayIcon
b. A URL connection class (an HttpsUrlConnection which will update data in the main jframe and also upload/download files)
c. A Directory Monitor (which will update data in the main jframe and also use the urlconnection to download a file)
d. My own implementation of TableModel
e. json
How to correctly keep and use an object with settings through the whole application? I will need it's information in different places (Views, Models, Controllers) but it might be altered by user during the runtime). Is it a good idea to make this model a singleton?
What should I do when:
a. View needs some data from the Model?
What I'm doing: using the reference of Model which I keep in the View
b. View needs some data from the Controller?
What I'm doing: using the reference of Controller which I keep in the View
c. Model needs some data from the Controller?
Still didn't happen but I have no idea how to do correctly
d. Model needs some data from the View?
What I'm doing: pulling all my hair from my head...
e. Controller needs some data from the View?
What I'm doing: using the reference of the View which I keep in the Controller
f. Controller needs some data from the Model?
What I'm doing: using the reference of the Model which I keep in the Controller
g. One of FooModel, FooView or FooController needs data from one of BarModel, BarView or BarController?
What I'm doing: thinking of jumping from the highest building...
Any hints on how to know if I implemented MVC correctly? Should I process massive data in Model or Controller?
I'm also using a DAO, what I'm doing is: my model has a
ArrayList MyModel load()
method which creates an instance of the DAO and returns the ArrayList of Models returned by the DAO, and then sometimes I process this ArrayList of Models in the Model and sometimes I allow the Controller to process it. Is this a good practice or is there a better way? By Process I mean: iterate through the ArrayList and get the data from the models.
I Have a PasswordCheck jDialog to restrict access to some Views. How can I reuse it in terms of MVC, so that I can use the same PasswordCheck dialog for allowing/restricting access to different Views without doing a mess in the code?
Any other tips, hints, ideas, suggestions?
Context:
I'm required to develop a Java Swing MVC software in a short time, although by default I'm not a Java developer and not so used to implement the MVC pattern, specially in Java (I get the idea but sometimes it lacks me knowledge to implement the relationship between classes).
The applications is basically a monitor for local/online files with a JTable in the main frame to show this data. I'm using the new WatchService API to keep track of the local files and saving their info in a h2 database using a DAO, and them reload this data in the main frame jtable. I must also notify the user about new files (which for I'm using TrayIcon). For the online files monitoring/uploading/downloading I'm using HttpsUrlConnection and json. It may also allow Settings customization.
Thanks in advance for you time and help.
Have a look at Sun's (Oracle's) suggestions.
As one simplification, you could have each component (model, view, controller) register with a top-level application component to provide a single point of reference rather than individual references between each component (your A or B). The article I cite provides ideas for push and pull design; I'd recommend push as a more popular modern approach. Disclosure: I have experience with Java and MVC but not MVC in Swing per se.
where should I instantiate the View, the Model and the Controller of
the SettingsFrame?
Sure, yes, or in a top-level application component.
how should I handle more specific features that (apparently) lacks one
or two of the MVC "legs" (either Model or View or Controller)?
I would implement GUI-only pieces as your own GUI library. And purely algorithm/service pieces as a services library.
Should I process massive data in Model or Controller?
Data processing algorithms would fit nicely in a controller or even service library; your model should not do much processing at all beyond possibly data type conversion or validation.
How to correctly keep and use an object with settings through the whole application?
See my note on registration; a singleton may be appropriate.
In C#, there are observable collections. In Java how do I represent a list a models? Suppose I have a model Item. I want to show users a list of Items. Do I create a ItemsModel (notice plural, encapsulating an ArrayList<Item>) then in my ItemsView bind (listen to property change events) to that? This model will also need to listen to PropertyChange events from its underlying Item. Is this the right way of implementing this? Is there a better way?
A possible solution could be the Eclipse Databinding. It let's you create a binding from your POJOs to certain SWT Controls. For your special case with Items, you could use the Master-Detail Binding mechanism, for an example see http://wiki.eclipse.org/JFace_Data_Binding/Snippets#Master_Detail
Depending on the source of your domain model, you could consider changing it to use Eclipse Modeling Framework (EMF). EMF have complete a notification framework for all changes made to the model, which makes it easy to use and bind to. Eclipse Databinding - as mentioned by Tom Seidel - also bind to EMF.
My Java application is not very big, but I would like to do it the "right way". Looking for suggestions to not make a big mess...
I am using Swing, the Swing Application Framework and H2 database.
It is a single frame application with different tabs. Most tabs will reflect some aspects of the data in the database. Some of them will be able to change it. Some tabs will have buttons or actions that will redirect to another tab... that's basically it.
I would like to keep things clean, rather portable and flexible (can enable disable some tabs for example or add some easily).
Here are the questions :
1) How to have tabs "refreshed" when the database content changes ? Implement some Listener interface ? Should I define that interface or there is something already existing I can reuse ?
2) How to "link" the different tabs together so that it is easy to jump from one to another (and may have to carry some argument to setup the destination tab to the right "view")
3) If I set some preference with some filters in another part of the application, how do I share such settings with the whole application ?
And when I ask "how to", I really mean "what is the best way"... I can get that working already... just looking for the right tool to use, design pattern or such.
There are different strategies of gui application building. Here some article about.
If using swing, you can use tabbed pane to have a tabular layout.
Implement a listener (as you are planning). Have a look at observable interface in Java.
You can implement a singleton(eg PreferenceManager class) to hold the preference and access it across the application to access properties.
Do you really want a push model where the server has to maintain a connection to every client and then update them all when anything changes? This is feasible with a handful of clients, but quickly becomes problematic. It's probably better to use a pull model, where the client manually (periodically?) asks for updates. This is also a good way to use optimistic locking with a last_modified field in the DB. Attempt an update, if it's changed since you last reloaded, warn the user and allow them to overwrite / merge.
As for Swing in general, best practice is to factor everything out that's not GUI related into core classes ('business logic') that fire off events. Register all your Swing classes as the appropriate event listener and update the gui appropriately (SwingUtilities.invokeLater() is your friend here).