3

Background:

I have extensive experience in web applications, but I have very little or no experience with RCP. Presently, I am working with Java Swing API. Everything is good. The only problem is, I don't feel very confident about my design.

Question:

In web application when we implement MVC, we forward the request to a View. The Controller doesn't care about where to show it. Everything is going to be displayed on a page, typically. But in RCP, we need to know where to show this form/table/etc.. I mean which panel, right or left, up or down. That requires a reference to a container component in which we desire to show the thing. How we should design our code to get that reference?

Take:

I am keeping reference to all first level components in the main class -- that has main() method. And then wherever I need some component, I am doing something like,

JPanel formPanel = MainApp.getMainPanel().getFormPanel();
..
JPanel treePanel = MainApp.getMainPanel().getTreePanel();

Is it okay?

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
  • Well, web patterns don't apply well to GUI (I don't like the term RCP much because it reminds me of GUIs like Eclipse or NetBeans, not all GUI applciations have to look like Eclipse...) Just google some of the following terms: MVP, PresentationModel. You should find 2 main references: one from Martin Fowler and one from Karsten Lentzsch (much involved in Swing frameworks and patterns). I strongly advise both of them. – jfpoilpret Apr 29 '11 at 08:04
  • 1
    On a side note, you may want to take a look at my guts-gui (http://kenai.com/projects/guts/pages/Home) project and some examples I have put there. – jfpoilpret Apr 29 '11 at 08:05
  • Thanks for the link to GUTS, jfpoilpret. – Adeel Ansari Apr 29 '11 at 08:13

2 Answers2

2

MVC isn't really as much of a thing in RCP, people talk about it but largely it's bullshit.

For sure having a model of your business objects that's separate and had no dependency on any UI code is important. But the lines between Views and Controllers tend to be more blurred I wouldn't sweat it too much.

User interface elements naturally tend to be organized in a tree like you have, though global variables such as your main panel should be avoided.

Lean heavily on the observer pattern.

Tom
  • 43,583
  • 4
  • 41
  • 61
  • Great advice, Tom. I am using Observer, wherever it seems befitting. But right now, need to show a form in some other panel upon a selection of a tree node. I am using TreeSelectionListener for that. But in listener I think I need the reference to that container where I need to show the form. I hope you would be able to see the picture. – Adeel Ansari Apr 29 '11 at 08:14
  • 2
    Typically a panel would contain the tree on one side and the content you want to update based on selection in the other, it's this panel that would listen to the tree (via a selection listener) and then make the changes to the display panel. – Tom Apr 30 '11 at 04:08
1

A Swing Architecture Overview elaborates on Swing's separable model architecture. I found the concept helpful in understanding commonly used Swing components, as well as popular tools like JFreeChart.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Glad to help; I'm a fan of MVC in Swing. Here's a related [example](http://stackoverflow.com/questions/3066590/gui-problem-after-rewriting-to-mvc/3072979#3072979). – trashgod May 13 '11 at 04:33