GWT: Querying and translating proxied UI classes into widgets - java

This morning I asked this question and received a really great answer for it. I'm now trying to prototype the design and am running into a similar (but not same) issue. Essentially I'm trying to have a 100% configuration-driven GWT UI, where changes to a DB can produce radically different UIs but without any code changes.
My architecture is simple:
I will use a separate tool (or at first, by hand) to produce a .xul (XUL) file and save it to a database; probably a document database like Mongo or something similar
On the server-side, I will write a XulParser that reads the XUL file out of the database and turns it into a ContainerProxy instance
A ContainerProxy is my "proxy" equivalent to a com.google.gwt.user.client.ui.Panel or something similar; it is just a bean/POJO that contains a list of other widget proxies (see code snippet below)
For instance, I might have a XUL file that defines a Button like the one below
This ButtonProxy would be added to a ContainerProxy (along with any other UI widgets in the same container/view/panel)
On the client-side, I'll query for the ContainerProxy somehow (???), and pull it down from the server.
I'll then have a mechanism that translates each of the ContainerProxy's children (the ButtonProxy, etc.) into actual UI widgets.
This way, at first I can put a certain XUL file into the database and the UI might only contain the "Order Now" button on it. But down the road, I might want to use a totally different UI, so I design a different XUL file, update the document in the database, and - voila - the UI changes for all users without any code changes. Please note: I understand this is not a normal way of doing things: I have a special use case where I need such config-driven UIs.
To help clarify the XUL-parsing process:
<button id="order" label="orderNow" clickHandler="org.myapp.OrderButtonHandler" />
after the XulParser reads the above snippet, we get:
ButtonProxy orderButton = new ButtonProxy("order", "OrderNow");
orderButton.addClickHandler(new OrderButtonHandler());
Anyways, my questions:
What do I need to do on the client-side to query for my ContainerProxy? Can anyone provide pseudo-code to help me visualize it?
Once I have the ContainerProxy on the client-side, what UI mechanisms are available for me to translate the proxied classes into actual UI widgets? Is UIBinder available? Again, can someone provide some pseudo-code?
Thanks in advance for any help here!

Here is a bit of a code idea that would do what you wanted. I haven't tested this, but you can get the idea of how it all could work together from it.
String htmlFragment = "<div><button id='action1' type='button>Action 1<button/><div/>";
HTMLPanel container = new HTMLPanel(htmlFragment);
RootLayoutPanel.get().add(container);
Element button1El = container.getElementById("action1");
if(button1El != null){
Button button1 = Button.wrap(button1El);
button1.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// What you want button 1 to do
}
});
}

Related

How to make JTree to only display strings and not a file structure

I'm currently working on a client-server project where the client sends a request to the server and the server runs a database statement based on the information from the client. As there's no static length for the output I add all the data to a ArrayList<String> and send it back to the client.
The data that the client requests has a certain hierarchy so that one could also display it using an XML file (however the output to a GUI is wanted here instead of file creation). There's several ways of displaying I came up with. One would be using a simple text area, however using this, I need to do applying the layout myself and the option of folding where I can expand and collapse certain items is not possible.
Another possibility of course would be using a simple JList, however I read on Stackoverflow that using a JList to display hierarchical things is way easier done using a JTree.
When I decided trying to implement JTree I also found the Genealogy.java file provided by Oracle in their documentation of JTree. As to be seen in the first examples of this documentation there is always a filestructure being displayed and hence also a filestructure kind of representation (using folder- and file-symbols) is shown. In contrast to this Genealogy.java only shows the names of the persons so I tried to adapt the implementation from Genealogy.java to my case however I'm completely clueless of how to do this as I don't even know where these symbols come from and where I could possibly disable them and also don't really get what some of the methods I need to implement do.
As I'm currently really clueless of how to possibly accomplish that I really need some detailed help (only telling me to come up with my own implementation of JTree or TreeNode doesn't really help me at this point). Is there any simpler way or is the need to implement any of the before mentioned interfaces inevitable and if so, how would it be done?
EDIT:
This is how it currently would be displayed (example taken from the Oracle documentation, showing folder- and file-symbols in front of the string):
And this is how I want it to be displayed (also from the documentation, this time only displaying a string as node):
The answer almost certainly will be found via the (icon of the) TreeCellRenderer used for the tree nodes. See the File Browser GUI for tips. It shows how to set the icons in the FileTreeCellRenderer. Admittedly the point here is almost exactly opposite what it was there, but it still comes down to the same thing - the icons.
The section of the tutorial that covers it is How to Use Trees: Customizing a Tree's Display.

How to download images asynchronously from web server

My application screen looks similar to the image attached. I have multiple rows and each row has a Bitmap image, title and description field. All the information's are getting fetched from our supporting web-server in the form of XML. Now, I have used observer design pattern, which creates a separate thread for connecting to my remote server over HTTP, downloads and parse the XML. The XML includes the URL for image, title and description for each row.
I have tried few approaches so far,
Approach 1:
Created a separate method (drawRow()), which takes care of putting the contents together by specifying the layout.
And, then using the method downloadImage(), I am trying to download the remote URL from the drawRow() method. But it sucks, as it downloads using the same thread and UI gets blocked.
Approach 2:
While searching for the above issue, I came across WebBitmapField in blackberry from coderholic.com.
And, then I am using the below code from my drawRow() method. As I understand the WebBitmapField, here is using observer design pattern and the image is downloading over thread other than UI thread. It works fine when I have limited number of rows like 5 or 10. But when I have more number of rows to be drawn it throws TooManyThreads exception, as it creates a new thread for each row.
I have got this link taskworker-thread-blackberry, but not much clear with how to achieve my requirement.
As I understand, in blackberry an application can create maximum of 16 number of threads. So, now I believe. I may need to create a thread pool specifying max size to 10.
Can anyone please help me to understand and implement the thread pooling for blackberry for my current problem?
Also, I appreciate anyone giving me any other best approach which will fit for my requirement.
Thanks in advance.
You have everything what you need. So:
Create one TaskWorker for your application (use singelton)
Implement Task class from TaskWorker - DownloadImageTask (simply put everything from Runnable.run() to Task.doTask() method)
Instead of new thread creation in Util.getWebData() call TaskWorker.addTask()
There are probably more minor details but you could figure out how to finish it.
And I think it's better to have two method in Callback - success(byte[] data) and error(Throwable error) - to determine end result and escape converting images to String and back.

Java Swing automatically gather data from forms

I have a form in Swing with a lot of textfields receiving data. The idea is when a button is clicked, the app gathers all the data from the textfields at once. Do you know a good practice for this? Or is it necessary to gather the data textfield by textfield?
If you are looking for a framework, try BeansBinding from JDesktop. It is quite well integrated with Netbeans IDE's Matisse GUI builder, and should be sufficient for most needs in a simple program.
If you are using Netbeans, here's a good primer to help you get started.
However, as BeansBinding works by abstracting all the complexity away, what you lose is more granular control. If you are developing a highly complex application that requires more control and tweaks, then I would suggest that roll up your sleeves and prepare to get your hands dirty. Register a class as a listener for each of the editable fields on the form, and save the data to a bean class that stores each of the necessary values.
You will have to read text from each text field/area one at a time.
In order to make your life easier - you'd want to put all the fields you want to read inside a List or a Vector when they get created; in this way, you will be able to read them in a simple for loop. If you need to know which field is which, use a HashMap and name each JTextField with a unique name to serve as key for the HashMap. Conversely, you can use .setName("SomeName") and use a List/Vector and use getName() to see what field it is.
As far as good practices go, unless you have many-many-many fields in your form, you don't need to worry because it won't take too long to do and you can put all the reading code in your listener method. However, if we're talking about many dozens or even hundreds of text fields, you'd want to start a new Thread to do that for you and disable the button so that the user knows something is going and can't press the same button again until the reading is done.

Java document state pattern?

I am a new to programming with Java and I would like to know if there is some common practice for managing state of opened document (is current state saved or dirty) , saving document, opening, creating new document and so on. How do you approach this?
Right now I have my little Swing application and have actions for opening and closing document and creating new one, but I don't know how to manage if user has saved file or not (I need this to check if user wants to create new one or open existing while working on current.)
Is there some pattern for this? All advices are very welcome since I am still learning how to swim with Java.
As far as I know Swing does not have mechanisms for managing document state. You have to do that yourself. But then, it is not that much code that has to be written and if you have several different documents in your app you can put that stuff in an abstract base class.
The basic approach has been outlined already: just have a "dirty" flag in your document data structure. You should put some thought into writing down which of your operations like "create", "open", "save", "close" should modify and evaluate this flag. I would suggest a state chart (not necessarily the UML state machine variant) as a tool to specify this.
If you need more complex functionality, especially undo/redo, take a look at the Memento pattern. Most of the code that has to be written when you use this pattern is specific to the application and its data structures (i.e. the types you create for managing documents) so it would be hard to impossible to effectively generalize this and put it into a framework like Swing or RCP.
You have a boolean variable named isDirty which starts at false.
Every time a change is made to the document it is set to true by the code.
All other program functions (Open,save,new menus e.t.c) check the status of this boolean
before doing anything else.
This way they also present the familiar dialogs: Are you sure you want to exit, Discard
your changes e.t.c
I have used this several times on real world Swing Apps
You may think about working with temporary versions of your document (i.e. you open main document, but when you edit it then temp document is created). In this case another user who opens the same document will see original doc. As I know it's common practice..
But I'm not sure that you want to maintain so complex behavior..

Fast search in java swing applications?

I'm wandering myself what component is the best for displaying fast search results in swing. I want to create something like this, make a text field where user can enter some text, during his entering I'll improve in back end fast search on database, and I want to show data bellow the text box, and he will be able to browse the results and on pres enter result will be displayed in table. So my question is is there any component which already have this logic for displaying?
Or is it's not, what is the best way to implement that.
This search will be something what ajax gives me on web, same logic same look and feel, if it's possible on desktop application.
Are you looking for something like an AutoComplete component for Java Swing?
SwingX has such a component. See here for the JavaDoc. It has a lot of utility methods to do various things, i.e. auto-completing a text box from the contents of a JList.
I strongly, strongly recommend that you take a look at Glazed Lists - this is one of the finer open source Java libraries out there, and it makes the bulk of what you are asking about super easy.
You will have to first attach a listener to the JTextFields Document to be notified whenever the user types in the field (or changes it).
From there, you can fire off any server-side code you need. The results of that can be used to update a listbox.
A few things to keep in mind:
The code to do the search against the backend must be in another thread
The code that updates the list box should update the list box's model
You will need to manage all your backend search results so that you only update the listbox with the most recent result (e.g. user types 'A', backenf searches for that. Meanwhile, user has typed 'C', kicking off a backend search for 'AC'. You need to ensure the results from the 'A' search dont' make it to the listbox if the 'AC' search results are available).
Use Hibernate Search.
The SwingHack (http://oreilly.com/catalog/9780596009076/) book has an example of this.
In the interest of killing two birds with one stone: have a separate indexing thread. This will:
Improve the speed of searches whenever they are executed.
Improve the responsiveness of the UI since indexing is happening in a separate thread.
Of course, exactly how you perform the indexing will vary widely depending on your particular application. Here is a good place to start researching: Search Indexing. And please, ignore the reference to Web 3.0 [sic].
It is possible of course. It is simple too. For drop down list of terms just use popup menu. This is simple. The background processing of entered text is simple too. Enjoy!

Categories

Resources