How do you make a Swing/JFace/SWT GUI addressable?
I have a "fat" GUI that it getting fairly complex, and I would like to add links from a place to an other, and add back/forward buttons to ease navigation. It seems to me that this would be easier if my application was addressable: each composite could have its URI, and links would use that URI.
Are there design patterns applicable to this problem?
I could just look at the source code for Firefox or Eclipse, but these are huge projects and it would take a good amount of time making sense of it, so I'm asking here. Is there a simpler example somewhere?
Of course it would be simpler if I had build a web app in the first place, but I'm not going to rewrite this huge app from scratch anytime soon.
In Swing, you might use a CardLayout. You can have each "page" be a card, and the name of the card (chosen when adding cards to the layout) would be equivalent to the URI you want.
String PAGE_1_KEY = "page 1"; String PAGE_2_KEY = "page 2"; // as many keys as you need JFrame frame = ...; frame.setLayout(new CardLayout()); frame.add(createPage1(), PAGE_1_KEY); frame.add(createPage2(), PAGE_2_KEY); // etc.
Then in your buttons' action listeners, you would call
((CardLayout)frame.getLayout()).show(frame, PAGE_1_KEY); // or whichever
My solution for doing things like this usually involves the listener pattern. In a nutshell, you reduce coupling by providing a way to send and receive events to and from interested parties (composites in this case). This is fairly easy to implement, even when retrofitting. This way, your events and parties can change without changing the dependent code.
You could build a "global" registry that maps unique IDs to objects (or maybe class names). These objects could be JPanels, for instance. When the user clicks on a link or button, some Controller is notified with the key of the new page to be displayed. This Controller could create that JPanel and place it in the application's frame.
My last approach included global manager and registration of links. Each part of UI was able to name yourself uniquely and register. The global manager knows about each one and then use some dirty work to bring this part visible. The back/forward navigation was made by special undo/redo manager. Each "display" was able to obtain navigation manager and register its "undo" event. It was hard work to make it work, but the resulting feature was quite useful. We discussed about using some simple JNDI service to locate and name UI parts. It may be useful in linking too.