How to switch to a different view and controller in GWT? - gwt

I have a widget written in GWT. In the implementation of EntryPoint, I have the widget load a controller and a view that is tied to that controller. I add this view to the RootPanel and it works fine.
public class EntryImpl implements EntryPoint {
SimplePanel simplePanel;
View view;
Controller controller;
public final void onModuleLoad()
{
controller = createController();
view = createViewForController(controller);
simplePanel = new SimplePanel();
simplePanel.add(view);
RootPanel.get().add(simplePanel);
}
}
Something like above. Now I want to change the view and controller, say if the user clicks a button to load a different view. I tried something along the lines of adding an Anchor and handling the ClickEvent as follows:
public void onClick(Widget sender) {
simplePanel.remove(view);
controller = createController();
view = createViewForController(controller);
panel.add(view);
}
Is this the right way to go about doing this, or is there a better way? So far, this isn't working for me-- my new view is not showing up.
Thanks

You can use DeckPanel and switch the view using showWidget(index) method. Enabling deck panel's animations can improve usability.

what is panel in your OnClick event? if you just want to see the view you can get the root panel and see it RootPanel.get().add(view), Adding a div in the base HTML page may serve your purpose , if you have div you can add your view inside that div by RootPanel.get("div").add(view)...

You can use mvp pattern to control view transition (I have not used it yet)
http://code.google.com/intl/en/webtoolkit/articles/mvp-architecture.html

Related

Custom Perspective Switcher Toolbar: How can I dynamically update it?

I'm trying to implement a custom perspective switcher toolbar to replace eclipse's built-in one. I couldn't get the toolbar to display, and it was shown to me that due to a bug with the dynamic element in a menu contribution, I have to use a control element instead, as described in the workaround to the dynamic bug.
I have a toolbar displaying following that approach, but I cannot figure out how to update it dynamically. The workaround instruction is to call ContributionItem#fill(CoolBar, int) from my WorkbenchControlContributionItem's update method instead of doing the fill in the createControl method.
I don't know who is supposed to call update, but it never gets invoked no matter what I do. I have a perspective listener which knows when to update the toolbar, so from that listener's callback I call fill(CoolBar, int). But I wasn't sure how to get the CoolBar to pass to that method, so I created one on the current shell.
The end result of all this is that the toolbar displays the correct number of items initially, but when I need to add an item, it has no effect. I call fill(CoolBar, int) and it adds the new item to the toolbar, but everything I've tried to make the CoolBar and ToolBarupdate does not work. When I re-launch the app, the toolbar has the added item.
I'm sure I'm doing this wrong, but I can't figure out the right way. Here's an elided representation of my code (omitting methods, layout code, etc not related to the update problem).
public class PerspectiveSwitcherToolbar extends WorkbenchWindowControlContribution implements IPerspectiveListener {
...
#Override
protected Control createControl(Composite parent) {
this.parent = parent;
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.getWorkbenchWindow().addPerspectiveListener(this);
toolBarManager = (ToolBarManager)parent.getParent().getData();
fTopControl = new Composite(parent, SWT.BORDER);
fill(new CoolBar(page.getWorkbenchWindow().getShell(), SWT.HORIZONTAL), -1);
return fTopControl;
}
#Override
public void fill(CoolBar coolbar, int index) {
IPerspectiveDescriptor[] openPerspectives = page.getOpenPerspectives();
String activePerspective = getPerspectiveId();
ToolBar toolbar = new ToolBar(fTopControl, SWT.NONE);
for(IPerspectiveDescriptor descriptor : openPerspectives) {
ToolItem item = new ToolItem(toolbar, SWT.RADIO);
//overkill here, trying to find some way to upate the toolbar
toolbar.update();
parent.update();
parent.layout(true);
parent.getParent().update();
parent.getParent().layout(true);
coolbar.layout(true);
}
//PerspectiveListener callback
#Override
public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
fill(new CoolBar(page.getWorkbenchWindow().getShell(), SWT.HORIZONTAL), -1);
if (page.getWorkbenchWindow() instanceof WorkbenchWindow){
//this non-API call doesn't help either
((WorkbenchWindow) page.getWorkbenchWindow()).updateActionBars();
}
}
...
}

Add dropdown menu in a view's ToolBarManager

I want to add several dynamically created actions to a view. This works to add them to the View Menu in the top right corner:
private void fillActionBars() {
IActionBars bars = getViewSite().getActionBars();
IMenuManager manager = bars.getMenuManager();
IMenuManager myMenu = new MenuManager("Menu title", MY_MENU_ID);
// add actions to myMenu
manager.add(myMenu);
bars.updateActionBars();
}
This works fine. However, I want to add the actions to a dropdown menu in the toolbar instead (so the user can see them immediately). If I replace the third line with
IToolbarManager manager = bars.getToolBarManager();
the menu doesn't show up.
You're right, this doesn't work. A workaround that works fine, not using a MenuManager but a drop down action and a menu creator:
IActionBars bars = getViewSite().getActionBars();
IToolbarManager manager = bars.getToolBarManager();
Action act=new Action("Menu title",SWT.DROP_DOWN){};
act.setMenuCreator(new MyMenuCreator());
manager.add(act);
class MyMenuCreator implements IMenuCreator{
public Menu getMenu(Control ctrl){
...
}
}
You need to use IToolbarManager.add(IContributionItem) with a class that implements IContributionItem. See org.eclipse.ui.internal.FastViewBarContextMenuContribution as an example.

Dynamically adding a custom view to RemoteViews

Could any help me to do this? My code is like:
public CustomClass extends View {
//uses ondraw() to do something
}
For displaying my custom view on the home screen I created a class to extend Broadcast Receiver:
public class customAppWidgetProvider extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.main);
//Here I want to create my custom view class object and I want to add this view to linear layout in main.xml
CustomClass object = new CustomClass(context) ;
LinearLayout layout = new LinearLayout(context) ;
layout.setLayoutParameters(new LayoutParameters(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(object);
views.addview(R.id.linearlayout, (ViewGroup) layout) ;
views.setOnClickPendingIntent(R.id.analog_appwidget,
PendingIntent.getActivity(context, 0,
new Intent(context, AlarmClock.class),
PendingIntent.FLAG_CANCEL_CURRENT));
int[] appWidgetIds = intent.getIntArrayExtra(
AppWidgetManager.EXTRA_APPWIDGET_IDS);
AppWidgetManager gm = AppWidgetManager.getInstance(context);
gm.updateAppWidget(appWidgetIds, views);
}
}
}
But adding ViewGroup to RemoteView reference is not working... The main.xml layout above contains only LinearLayout. I want to add a custom view object to it. But after running this nothing shows on screen...
Please help me to do this. Thanks in Advance.
It's not possible to add a custom View to an app widget. See the "Creating the App Widget Layout" section of the App Widgets Dev Guide for what View types are allowed.
Android 3.0 adds support for some views to display collections. See the "Using App Widgets with Collections" section for details.
Otherwise, to dynamically add an allowed View to an App Widget, after inflating the RemoteViews and getting a reference to it, you can use its addView(View) method, or the addView(View) method on any of the View objects already in the RemoteViews.

Identify which instance of the view is currently active in RCP?

I am creating an RCP application. I need to open multiple instances of the same view but with different data. I did it by setting secondary id for different instances of the same view. Specifically, my problem is as follows: Please take a look
I have a graph view called Views.GraphView. I opened different instances of it from a command called openGraphView to show different graphs. The command is as follows:
page.showView("Views.GraphView", Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);
Now, I have a command called TreeLayout on this Views.GraphView toolbar, which suppose to change the layout of the graph and it will operate on each instance of the view. But for this, I think, I need to identify which instance of the view is active. The TreeLayout command looks something like this:
IViewPart findView = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage(). findView( "Views.GraphView"); //I think in the findView I need to give the id of the view [but how can I put the secondary id?]
GraphView view = (GraphView) findView;
view.changeLayout(); //I wrote this method in the graph view to change the layout
//I just tried to print the secondary id, but it did not print anyting
System.out.println("From treelayout command:- " + view.getViewSite().getSecondaryId());
So how can I identify which instance of the view is currently active and to operate on it?
You can use IWorkBenchPage.findViewReference(String viewId, String viewId) , if it returns null, the view with viewId and viewId is not present in the current perspective.
If you have a ViewReference you can use ViewReference.getView(boolean restore) to get the view
so in your handler you get something like:
final IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(
event).getActivePage();
final int instanceNum = 8;//the number of instances that are created
for (int index = 0; index < instanceNum; index++) {
final IViewReference viewReference = page.findViewReference(
"Views.GraphView", Integer.toString(index));
if (viewReference != null) {
final IViewPart view = viewReference.getView(true);
if (view instanceof GraphView) {
final GraphView graphView = (GraphView) view;
graphView.changeLayout();
}
}
}
The view.getViewSite().getSecondaryId() method is the good one to identify a secondary view. This method only returns the Null string for the "primary" view: the one opened when user click Window -> Show View - Your View.
I don't understand why your view toolbar button has to operate on all the view instances. TO my eyes, you should have one button in each view instance toolbar operating only in its own view. If you really need to operate from one button to ALL the views I think you will have to keep the open views references yourself, because I think the workbench doesn't provide a findViews method returning a view array.
using the below code you should be able to get the current active view.
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart()
Active view and Editor name display on MessageDialogbox
public class Active_Workbench extends AbstractHandler{
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// A part service tracks the creation and activation of parts within a workbench page.
IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
event).getShell(), "Current Workbench Window", service.getActivePart().getTitle().toString());
return null;
}
}
I hope this answer is useful.

gwt anchor tag not anchoring

Why does the browser not scroll to the anchor?
url:http://localhost:8080/index.html#myAnchor3
this.anchor1.setName("myAnchor1");
this.add(this.anchor1);
this.anchor2.setName("myAnchor2");
this.add(this.anchor2);
this.anchor3.setName("myAnchor3");
this.add(this.anchor3);
Is it because the anchor is created after the page has finished loading, so the browser doesn't see the anchor when it tries to scroll to it?
Try this:
this.anchor.setName("myAnchor");
this.add(this.anchor);
location.hash = '#myAnchor';
And yes, you are right, your anchor was created/inserted after the page load, so well.....
You could try using Element.scrollIntoView(), which not only will scroll the window, but any scrollable container in the DOM hierarchy that holds the element.
Had to override the onLoad method, and call scrollIntoView there, otherwise it was trying to scroll to an object that wasn't added to the DOM yet.
public class Foo extends Widget
{
Foo(){
}
#Override
protected void onLoad(){
super.onLoad();
getElement().scrollIntoView();
}
}