3

Can you explain me how a Controller can call another Controller method in a simple but still correct way?

Please provide some code!

background: I Have 2 different Controllers-View-Model and 2 libraries and they need to communicate between each other:

  1. SettingsWindow/SettingsController/SettingsModel: Responsible for the app settings. The model is a singleton because I need the settings data all over the app;

  2. A Library for Monitoring a directory which creates an event every time a file is created in a specific directory. The monitored dir path is defined in the SettingsModel; I'm using the Java 7 WatchService API for that;

  3. A Library for Monitoring a webserver and download new files. The webserver address and the save directory are both defined in the SettingsModel; I'm using HttpsUrlConnection and a timer for that;

  4. MainWindow/MainController/MainModel: Responsible for the main app window, which has a table that must be updated every time a new file is created in the monitored directory, and also everytime a file is downloaded by the above libraries.

So how do I correctly Instantiate and communicate those 4 different features?
How do their Controllers commnuicate between them, since they are all related? How should I organize this project in terms of MVC?

PLEASE provide the basic MVC scaffold (skeleton) and show where I create the instances, where and how I call each others method. I'm new to Java, specially swing and mvc. I've read a lot, but I got stuck in this situation.

dcr
  • 191
  • 1
  • 2
  • 10
  • 2
    I wonder whether you will get a response on such high level. To some degree one can resolve such issues by an ApplicationModel with fields SettingsModel, MainModel. The same holds for the Controllers. Then the global MVC classes just delegate. And some methods have to become global. Another option is to use the Listener pattern, registering for some events. *With swing keep it simple.* Do not over-engineer. – Joop Eggen Jun 08 '12 at 22:21
  • 2
    Not sure about the controller part - you may be better off keeping it simple as Joop recommends, and just using a Model-view arrangement. The models can then be linked up as required , such as listening for events from each other, and these updates can use events / bound properties to keep the UI in step – davidfrancis Jun 08 '12 at 22:41
  • 3
    `"PLEASE provide the basic MVC scaffold (skeleton) and show where I create the instances, where and how I call each others method"` -- would you like some coffee and donuts to go with that? – Hovercraft Full Of Eels Jun 08 '12 at 22:47
  • But seriously -- you're asking for free advice from volunteers. Don't you think it a bit impertinent to ask for all these extras? Wouldn't we all be better off if you just asked for and received the advice, and then you yourself created the code and the MVC scaffolding, then if you run into further problems, show us *your* efforts and ask a specific question? – Hovercraft Full Of Eels Jun 09 '12 at 11:40
  • @HovercraftFullOfEels I see your point, but English is not my main language. I actually meant "outline" instead of "scaffold" but they are still the same for me. The reason I asked for the "scaffold" is because I'm getting stuck or having trouble understanding some explanation, altought I've read a lot about it. I'm also not Java developer and not so used to it, not used to swing, specially in terms of organizing the code. But I'm required to do this and I don't have enough time or experience to understand all the principles. – dcr Jun 09 '12 at 13:29
  • @HovercraftFullOfEels [...] I need to jump some steps, and I understand things better when I see an outline. I didn't mean to be arrogant or impertinent at all! And I'm sorry if I sounded like that. That's why I put "PLEASE" in uppercase, just because I'm in a hurry. Actually any help is much appreciated. I'm just tired of trying to understand and always getting stuck and now I'm running out of time, I must run, but you can't "hush" your brain to have an insight or epifany! I'm not java developer _per se_, so I have extra-difficulty on understanding peoples explanations... – dcr Jun 09 '12 at 13:29

1 Answers1

4

As suggested here, patterns such as model-view-controller and observer represent recurring design elements. A simple outline may illustrate the interaction of classes, but a general solution is beyond the scope of StackOverflow.

Still, it may be useful to look at how the principles might be applied. Suppose you are following the tutorial and have implemented a WatchService in a SwingWorker, as shown here. Now, suppose you want the main view, a JTable, to update itself in response to an arriving WatchEvent. You would arrange for the corresponding TableModel to register itself with the worker as a PropertyChangeListener, shown here and here. When the PropertyChangeEvent arrives, you update the TableModel and fire the appropriate event to update the JTable. The known TableModel implementations already include the required methods.

A similar approach would be used to update the table in response to data arriving from the web.

For settings, consider java.util.Preferences, mentioned here, or javax.jnlp.BasicService, discussed here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • There's a related table/worker example [here](http://stackoverflow.com/q/8169964/230513). – trashgod Jun 09 '12 at 01:08
  • sorry for the delay, I got really ill in this mean time. I didn't even know about SwingWorker existence, it's nice to know it. Actually I'm using this solution for file monitoring: http://whiteboardjunkie.wordpress.com/tag/watchservice/ so I can register a listener. I will check if I understand and try to implement your suggestion and will get back to you soon. Thank you again. – dcr Jun 12 '12 at 01:32
  • See also this [Q&A](*http://stackoverflow.com/q/7968488/230513). If your [find](http://whiteboardjunkie.wordpress.com/tag/watchservice/) works out, it might make a good answer. – trashgod Jun 12 '12 at 03:35