GWT 2.1 MVP (Activities/Places) and Tabbed Displays [duplicate] - gwt

This question already has an answer here:
GWT 2.1 Places example without Activities
(1 answer)
Closed 3 years ago.
On an existing project we’re using MVP (hand crafted) reasonably well. It’s understood and does mostly what we need. For a new project I'm looking at using the MVP framework built into GWT 2.1 (Activities and Places).
Our applications are mostly tabbed displays with each tab bound to a single view widget.
I’ve tried to use Activities and Places without success for this type of display. Part of the problem is that the example Hello World article ended up leaving me chasing my tail, too many new concepts for my brain to digest.
The Hello World sample IMO is not a sufficient introduction and doesn’t deal with many of the real world use cases. I was hoping someone could point me in the direction of any sample applications that use MVP for tabbed displays. Thomas Broyer has some excellent posts on his blog but these have still left me a little perplexed.
Previously I’ve used an AppController to handle tabs changes and single presenters for each tab. The new architecture in GWT 2.1 leaves me more confused that it should.

I'm using the gwt Activities/Places framework for a tabbed display, and it works great, BUT: I decided to abandon the TabLayoutPanel widget we had been using and create my own navbar (that looks like tabs) and a content pane. The effect is the same - it looks identical - but the implementation is much cleaner.
I think the problem is in trying to mix Activities/Places, which has its own idea of navigation, with a TabPanel, which has another idea of navigation. At first I tried to throw them together, overriding tab button behavior to trigger a PlaceController, which in turn switched the tabs around, but... it was messy. With the independent navbar / content pane, the PlaceController could do everything just like it wanted to. You just have to manually switch the views, instead of letting a TabPanel do it for you.

I also faced this problem but managed to make it work using one activity per Tab and each activity using a presenter (or more) to display the components of the tab.
Regarding the solution found by Riley Lark, I, instead, opted by using a Decorator pattern and, so, keep the original TabbedPanel. How ? Each activity gets injected (GIN) a presenter that contains a decorator for the TabbedPanel.
So, for example:
Tab1Activity gets injected with Tab1Presenter, which, in turn, gets injected with Tab1Decorator which decorates the TabbedPanel with a Tab1ContentPanel (this panel contains all the widgets to be displayed on the Tab1 tab)
Tab2Activity gets injected with Tab2Presenter, which, in turn, gets injected with Tab2Decorator which decorates the same TabbedPanel with a Tab2ContentPanel (this panel contains all the widgets to be displayed on the Tab2 tab)
Seems complex but, after creating the first decorator, it really paid off and I was able to keep the TabbedPanel and take advantage of the URL history management implicit in the framework.

Related

MVVM and modality in portable view models

My app (written using MvvmCross and with MVVM pattern in mind) needs to display popup windows where user can choose of confirm certain options. So basically it's a classic modal dialog, but since the app's view model is implemented in a portable class library, it needs to tackle modality in a generalized sense - some platforms simply don't have exact match for a modal dialog.
There are a few threads discussing dialogs in MVVM (Open dialog in WPF MVVM, WPF MVVM dialog example). Following their advices I could probably solve this by introducing DialogService and implementing it for each platform. However I will be treating then dialogs like other services - storage service, map service etc.. But a dialog is a part of the presentation concept, so I wonder if it can be treated more like a view, so instead of calling an instance of an obscure IDialogService I could navigate to it using an MVVM framework of my choice (MvvmCross in my case).
I checked MvvmCross implementation and samples but found almost no dialog-related stuff.
Within MvvmCross, the presenter is responsible for how Views/ViewModels are shown when using ShowViewModel.
This presenter is a view/UI level object - it's ultimately the UIs job to decide if a view should be shown as a page, as a control, in a tab, in a split-view, as a dialog, etc.
v3 does introduce a presentation hint that the ViewModel can help suggest how the View should be shown - but it's up to the presenter on each platform to determine how (if) to use this hint.
Alternatively, Dialogs/flyouts/etc can easily be shown using MvxMessenger messages from ViewModel to View with a little bit of code behind.
For 'modality', also consider Greg's post on 'returning results' - see http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/

GWT MVP Navigation Sidebar

I am writing a web application using GWT, and following the MVP tutorial on the GWT website (i.e. using History for navigation).
I am a bit confused as to the best way of having a side-bar for navigation (i.e. clicking a navigation link changes content in main window. See below)
--------------------
| | |
| nav | main |
| | window |
| | |
| | |
--------------------
One potential way I see of doing this is to declare two <div> tags in the HTML for navigation and content. For example:
#Override
public void onValueChange(ValueChangeEvent<String> event) {
...
if (token.equals("navigation")) {
presenter = new NavigationPresenter(rpcService, eventBus, new NavigationView());
presenter.go(RootPanel.get("navigation"));
}
...
if (token.equals("content")) {
presenter = new ContentPresenter(rpcService, eventBus, new ContentView());
presenter.go(RootPanel.get("content"));
}
...
}
I am unsure if this is the best way of approaching this. (I guess bi-directional communication between the navigation panel and the content window could be done through an EventBus? Is this method flexible when it comes to changing the UI (e.g. for mobile sites))
I was wondering what people would suggest is the best way of going about this. I have read around and often land on discussions about Activities and Places, but as I understand it these are somewhat different to MVP architecture (activities and places is useful for browser history management, although I think my use of History covers that (?))
Any suggestions would be appreciated.
Yes, you are right: Activies and Places are about history and navigation management, a framework inside GWT. MVP is simply an architectural design pattern. You can achieve MVP using such framework as shown in the article you linked.
In your sample, you can do as you say, but I'd make each view a (lazy) singleton in order to avoid any potentially expensive re-creation. Personally I don't like this way, you end up defining your Displays in terms of (so many) HasXxx interfaces.
I think that if history management is important, choosing Activities and Places is almost a no-brainer (at least for me, and at least to define the skeleton of the app). Here you can find a really good article to get you started. And you will see how common your use case is: you have to define displayed regions (you navigation and main areas) that will react (thanks to the activity managers) on place change and create/restart the activities that will in turn update the UI. This way you achieve some sort of app-wide MVP in which the presenters are the activities.
As far as MVP is concerned, there is no best way of doing that: part 1, part 2 and MVP with A&P are simply different ways to achieve the same goal: separation between presentation and business logic, as well as pure junit testing. Simply choose what you like. See this post on groups for reference.
Hope that get you started.
For my apps, I use a ClientFactory just like in the GWT doc, with singleton for Views.
That way, the application structure (which includes the menu) is also a singleton referenced in the ClientFactory, so every other view can virtually access it directly through its interface.
For example some view might push some image in the header, hide the menu to display a video, or whatever you need. Most of the time, the app's structure does not need a separate activity/place to "persist" its state in the history stack...
I know most people don't like to have views calling each other, and so do I, the only exception is the app's structure view, which is kind of a specific one : it has a presenter which is not an activity, and the view itself is permanently displayed.
Communicating between your app structure and other views with notifications will make your app a spaghetti bowl IMHO. I have worked with frameworks like PureMVC which rely a lot on events/notifications to communicate between view, and the separation has a steep cost : maintainability and code readability.

Proper way to break apart a GWT widget into smaller chunks

I just want to know if this is the proper way to go about splitting up widgets in GWT that get too large, or if I am missing the concept of widgets/proper GWT usage all together.
I started out with a single class (widget), PCBuilder. As PCBuilder became too large, I decided to branch off and make two classes SuggestionPanel, and BuildControlPanel, both of which just split off PCBuilder's code into separate classes that still have access to the methods in PCBuilder:
This way, in my PCBuilder class, I can do something like this to add the SuggestionPanel and the BuildControlPanel to the tabs (TabLayoutPanel) that are specified in the UiBinder of PCBuilder while allowing for SuggestionPanel and BuildControlPanel to have their own separate UiBinder specifications:
My question is: Is this proper? Part of me thinks "no" just because it's not a nice way of doing it. On the other hand it works just fine, and my web application is somewhat broken up into manageable "sections" which is what I wanted.
Thanks for any insight.
It's fine apart from the fact that you have circular dependencies between classes.
Why do SuggestionPanel and BuildControlPanel need to call PCBuilder? Is there any business logic in it? RPC maybe? Separate that into another class.
First, you might want to take a look at GIN - this handles dependency injection. This is good for testability.
Second, if your app goes beyond one "page", then take a look at GWT MVP.
You should not consider your PCBuilder as a widget. Quoting gwt -
You construct user interfaces in GWT applications using widgets that are contained within panels. Widgets allow you to interact with the user. Panels control the placement of user interface elements on the page.
Coming back to your question, my take is to create widgets only if I can reuse the same element more than once. The rest of my layout logic goes into the view. Layout shouldn't be a part of the definition of the widget as much as possible.To conclude, push styling in css, push layout in the views; widgetize only if re-usable (and core) or if adding additional functionality to existing widgets.

Silverlight Navigation using Mvvm-light(oobe)+MEF?

What is the best approach for navigating between UserControls/Pages(out of browser experience)? I'm fairly new to Silverlight and even newer to the mvvm pattern.
How well does the Navigation Framework Integrate with the MVVM Light Toolkit? A snippet for general application flow control with the two would be great.
The plan was to use the Navigation Framework for general flow or using Jeremy Likeness's approach to region management(http://csharperimage.jeremylikness.com/search/label/regions) and swapping out regions as needed. I've seen a few places mention replacing the Visual Root, but that sounded like a hack to me.
Any advice, snippets, or a nudge in the general direction would be greatly appreciated.
Thank you.
You may find the following post useful:
SL4 Navigation Template with MVVM Light
I had the same problem and found this link (applies to Silverlight too):
Windows Phone 7 — Navigation between pages using MVVM Light Messaging
I'm struggling with the same thing, However, I am kinda steered away from the "NavigationService" because allegedly the journal keeps the object reference in memory or something I read somewhere.
I've been trying to utilize a frame in the main window, and using mvvm light to send navigation messages to the window to change the frame.
Edit: I figured it out.
In the main window (code behind, not viewmodel) I added a Messenger.Default.Register(Of NotificationMessage(Of NavMsg))...
Then in ANY view model I do a Messenger.Default.Send(Of Notificationmessage(Of NavMsg))...
And then I created a NavMsg class which accepts a string uri or an object to navigate to.
Here's some T4 code templates to generate you the code for your view/viewmodel to work with mvvmlight and it demonstrated the closing window message above. http://dl.activeaspsoftware.net/dl.aspx?f=MvvmLightCodeTemplates.zip

Where to store "global" data in Eclipse RCP Application?

I'm a beginner with Eclipse RCP and I'm trying to build an application for myself to give it a go. I'm confused about how one actually goes about handling model objects. None of the examples I can find deal with the problem I'm having, so I suspect I'm going about it the wrong way.
Say I need to initialise the application with a class that holds authenticated user info. I used my WorkbenchWindowAdvisor (wrong place?) to perform some initialisation (e.g. authentication) to decide what view to show. Once that's done, a view is shown. Now, that view also needs access to the user info I had earlier retrieved/produced.
The question is, how is that view supposed to get that data? The view is wired up in the plugin.xml. I don't see any way I can give the data to the view. So I assume the view has to retrieve it somehow. But what's the proper place for it to retrieve it from? I thought of putting static variables in the IApplication implementation, but that felt wrong. Any advice or pointers much appreciated. Thanks.
The problem you are facing here is in my opinion not RCP related. Its more an architectural problem. Your view is wired with business logicand!
The solution can be done by two (common) design-patterns:
Model-View-Controler (MVC)
Model-View-Presenter (MVP)
You can find plenty information about this in the web. I am going to point a possible solution for your particular problem using MVP.
You will need to create several projects. One is of course an RCP plugin, lets call it rcp.view. Now you create another one, which doesnt make UI contributions (only org.eclipse.core.runtime to start with) and call it rcp.presenter. To simplify things, this plugin will also be the model for now.
Next steps:
Add the rcp.presenter to the
dependencies of rcp.view (its
important that the presenter has no
reference to the view)
Export all packages that you are
going to create in the rcp.presenter
so they are visible
In rcp.presenter create an interface
IPerspective that has some methods
like (showLogiDialog(), showAdministratorViews(User user), showStandardViews(User user))
Create a class PerspectivePresenter that takes IPerspective in the constructor and saves it in an attribute
In rcp.view go to your Perspective, implement your interface IPerspective, and in the constructor create a new reference presenter = new PerspectivePresenter(this)
call presenter.load() and implenent
this in the presenter maybe like this
code:
public void load()
{
User user = view.showLoginDialog(); // returns a user with the provided name/pw
user.login(); // login to system/database
if(user.isAdministrator())
view.showAdministratorViews(user);
else
view.showStandardViews(user);
}
As you can see, the view just creates a reference to the presenter, which is responsible for all the business logic, and the presenter tells the view what to display. So in your Perspective you implement those interface functions and in each one you can set up your Perspective in a different way.
For each View it goes in the same way, you will need a presenter for the view which performs operations and tells the view (using the interface) what to display and passing down the final data. The view doesnt care about the logic. This is also very usefull when using JFace-Databindings (then only bound data is passed to the view).
For example, the WorkbenchWindowAdisor will just create everything that is needed in the application. Other views, perspectives, then can enable/disable menus and so on depending on the data they got (like when isAdministrator you might want to enable an special adminMenu).
I know this is quite a heavy approach, but the Eclipse RCP is designed for big (as the name says rich) applications. So you should spend some time in the right architecture. My first RCP app was like you described...I never knew where to store things and how to handle all the references. At my work I learned about MVP (and I am still learning). It takes a while to understand the concept but its worth it.
You might want to look at my second post at this question to get another idea on how you could structure your plugins.