Portlet modes

How to implement different portlet modes in Vaadin application?

I would like user to be able to set preferences for my Vaadin application in portlet edit mode. I couldn’t find any information about it.

I tried it with PortletApplicationContext2.PortletListener but it throws PortletException doEdit method not implemented.


public class TestVaadinPortletApplication extends Application {

	@Override
	public void init() {
		setMainWindow(new Window("Main window"));		

		if (getContext() instanceof PortletApplicationContext2) {
			PortletApplicationContext2 portletContext = (PortletApplicationContext2) getContext();

			portletContext.addPortletListener(this, new TestPortletListener());
		}
	}

	private class TestPortletListener implements PortletListener {

		@Override
		public void handleActionRequest(ActionRequest request,
				ActionResponse response) {
						
		}

		@Override
		public void handleEventRequest(EventRequest request,
				EventResponse response) {
						
		}

		@Override
		public void handleRenderRequest(RenderRequest request,
				RenderResponse response) {
			if (request.getPortletMode() == PortletMode.VIEW)
				getMainWindow().addComponent(new Label("view mode"));
			else if (request.getPortletMode() == PortletMode.EDIT)
				getMainWindow().addComponent(new Label("edit mode"));
		}

		@Override
		public void handleResourceRequest(ResourceRequest request,
				ResourceResponse response) {
						
		}
		
	}
	
}

doEdit is a method in GenericPortlet which ApplicationPortlet2 extends so if you extend ApplicationPortlet2 and override the default doEdit() method (throws the exception) your doEdit() method should be called automatically for all render requests when in edit mode. This is a bit tricky at the moment and I hope it will be improved in the future.

Yes, I figured that I could do that but I was looking for more elegant solution.

I created
#3918
for this issue.

Same applies for portlet help mode.

Hi

There is another problem. I am testing portlets 2.0 by extending Application class. If I extend ApplicationPortlet2 there is neither main window, nor application listeners to add.

The ApplicationPortlet2 seems to be a very raw class.

When extending Application, I’ve added @RenderMode(name=“edit”) before methods like the renderRequest, also adding the PortletException as throwable, but it doesn’t work.

Any ideas to handle easily the mode change and not to rewrite the whole application? Is there any cheat sheet or complete sample portlet?

Thanks
aniceto

This is how I resolved it. And changed portlet-class and added supported portlet-mode edit to portlet.xml. Works for me. Hope it helps.


public class MyApplication extends Application implements PortletListener {

	private Window viewWindow;
	private Window editWindow;

	@Override
	public void init() {
		viewWindow = new Window("My View Window");
		editWindow = new Window("My Edit Window");

		if (getContext() instanceof PortletApplicationContext2) {
			PortletApplicationContext2 portletContext = (PortletApplicationContext2) getContext();

			portletContext.addPortletListener(this, this);
		}
	}

	@Override
	public void handleActionRequest(ActionRequest request,
			ActionResponse response) {
	}

	@Override
	public void handleEventRequest(EventRequest request,
			EventResponse response) {
	}

	@Override
	public void handleRenderRequest(RenderRequest request,
			RenderResponse response) {
		setLocale(request.getLocale());

		if (request.getPortletMode() == PortletMode.VIEW)
			setMainWindow(viewWindow);
		else if (request.getPortletMode() == PortletMode.EDIT)
			setMainWindow(editWindow);
	}

	@Override
	public void handleResourceRequest(ResourceRequest request,
			ResourceResponse response) {
	}

}

public class MyVaadinPortlet extends ApplicationPortlet2 {

	@Override
	protected void doEdit(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		handleRequest(request, response);
	}

}

In 6.2.1, there will be an easier way to handle the simple case of implementing the edit or help portlet modes in JSR-286 (Portlet 2.0) portlets.

It will suffice to register a window with the name “edit” in the application and it will automatically be used by the portlet if the edit mode is allowed in the portlet configuration.

See
http://vaadin.com/forum/-/message_boards/message/99240
for some more information and links to an example.

OK, Thanks. That’s good to know.