]> source.dussan.org Git - vaadin-framework.git/commitdiff
#3918 Portlet 2.0 modes (edit, view, custom modes) and a sample portlet using modes
authorHenri Sara <henri.sara@itmill.com>
Mon, 4 Jan 2010 16:03:30 +0000 (16:03 +0000)
committerHenri Sara <henri.sara@itmill.com>
Mon, 4 Jan 2010 16:03:30 +0000 (16:03 +0000)
svn changeset:10611/svn branch:6.2

WebContent/WEB-INF/liferay-display.xml
WebContent/WEB-INF/liferay-portlet.xml
WebContent/WEB-INF/portlet.xml
src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java
src/com/vaadin/terminal/gwt/server/PortletApplicationContext2.java
src/com/vaadin/terminal/gwt/server/PortletCommunicationManager.java

index 302f9961dd18ff28db2bbd86605e08ebb5531fa6..d83b47d8b0b0323684344c86080ad18100f7a05c 100644 (file)
@@ -22,5 +22,6 @@
                                <portlet id="InterPortletEventPortlet" />
                                <portlet id="VaadinInterPortletEventPortlet" />
                                -->
+                <portlet id="Portlet Mode Example"/>
         </category>
 </display>
\ No newline at end of file
index 696afaaee835c6270c19cc6121845cdc94aeb04f..a3c61dc439f9fe97e09aaba93de4087bdb70e6f7 100644 (file)
                 <instanceable>true</instanceable>       
                 <ajaxable>false</ajaxable>
         </portlet>
+        <portlet>
+            <portlet-name>Portlet Mode Example</portlet-name>
+            <instanceable>true</instanceable>
+            <ajaxable>false</ajaxable>
+        </portlet>
         
         <role-mapper>
                 <role-name>administrator</role-name>
index 2d58a61ae4d64813ca757b956640140b2ca64a28..b1a6d4722d119dab2df20b98b4755e102f73b675 100644 (file)
                </supported-publishing-event>
                <supported-public-render-parameter>HelloState</supported-public-render-parameter>               
        </portlet>
-       -->
+    -->
+    <portlet>
+        <portlet-name>Portlet Mode Example</portlet-name>
+        <display-name>Portlet Mode Example</display-name>
+        <portlet-class>com.vaadin.demo.portlet.PortletModePortlet</portlet-class>
+        <init-param>
+            <name>widgetset</name>
+            <value>com.vaadin.portal.gwt.PortalDefaultWidgetSet</value>
+        </init-param>
+        <supports>
+            <mime-type>text/html</mime-type>
+            <portlet-mode>view</portlet-mode>
+            <portlet-mode>edit</portlet-mode>
+            <portlet-mode>help</portlet-mode>
+            <portlet-mode>config</portlet-mode>
+        </supports>
+        <portlet-info>
+            <title>PortletModeExample</title>
+            <short-title>PortletModeExample</short-title>
+        </portlet-info>
+    </portlet>
 
        <portlet>
                <portlet-name>AddressBookPortlet</portlet-name>
                </security-role-ref>
        </portlet>
        
+       <!-- Used by the Portlet mode demo -->
+    <custom-portlet-mode>
+        <description>Custom mode</description>
+        <portlet-mode>config</portlet-mode>
+        <portal-managed>false</portal-managed>
+    </custom-portlet-mode>
+
        <!--
                These can be used to customize the event object types.
                The types must be serializable and have JAXB binding.
index fd3133393083d1535a424f658c640f744e327d4a..2dfb22c19755658c69dd5fe5b2f2c8d12e920087 100644 (file)
@@ -28,10 +28,12 @@ import javax.portlet.PortalContext;
 import javax.portlet.PortletConfig;
 import javax.portlet.PortletContext;
 import javax.portlet.PortletException;
+import javax.portlet.PortletMode;
 import javax.portlet.PortletRequest;
 import javax.portlet.PortletResponse;
 import javax.portlet.PortletSession;
 import javax.portlet.PortletURL;
+import javax.portlet.RenderMode;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 import javax.portlet.ResourceRequest;
@@ -424,7 +426,7 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
                     /*
                      * Always use the main window when running inside a portlet.
                      */
-                    Window window = application.getMainWindow();
+                    Window window = getPortletWindow(request, application);
                     if (window == null) {
                         throw new PortletException(ERROR_NO_WINDOW_FOUND);
                     }
@@ -492,6 +494,34 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
         }
     }
 
+    /**
+     * Returns a window for a portlet mode. By default, the main window is
+     * returned. To use different portlet modes in a portlet, register windows
+     * named after the portlet modes in the application.
+     *
+     * Alternatively, a PortletListener can change the main window content.
+     *
+     * The window name requested from the application
+     *
+     * @param request
+     * @param application
+     * @return Window to show in the portlet
+     */
+    protected Window getPortletWindow(PortletRequest request,
+            Application application) {
+        PortletMode mode = request.getPortletMode();
+        if (PortletMode.VIEW.equals(mode)) {
+            return application.getMainWindow();
+        } else {
+            Window window = application.getWindow(mode.toString());
+            if (window != null) {
+                return window;
+            }
+        }
+        // no specific window found
+        return application.getMainWindow();
+    }
+
     private void updateBrowserProperties(WebBrowser browser,
             PortletRequest request) {
         browser.updateBrowserProperties(request.getLocale(), null, request
@@ -624,12 +654,55 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
         handleRequest(request, response);
     }
 
+    /**
+     * Handles a request for the "view" (default) portlet mode. In Vaadin, the
+     * basic portlet modes ("view", "edit" and "help") are handled identically,
+     * and their behavior can be changed by registering windows in the
+     * application with window names identical to the portlet mode names.
+     * Alternatively, a PortletListener can change the application main window
+     * contents.
+     *
+     * To implement custom portlet modes, subclass the portlet class and
+     * implement a method annotated with {@link RenderMode} for the custom mode,
+     * calling {@link #handleRequest(PortletRequest, PortletResponse)} directly
+     * from it.
+     *
+     * Note that the portlet class in the portlet configuration needs to be
+     * changed when overriding methods of this class.
+     *
+     * @param request
+     * @param response
+     * @throws PortletException
+     * @throws IOException
+     */
     @Override
     protected void doView(RenderRequest request, RenderResponse response)
             throws PortletException, IOException {
         handleRequest(request, response);
     }
 
+    /**
+     * Handle a request for the "edit" portlet mode.
+     *
+     * @see #doView(RenderRequest, RenderResponse)
+     */
+    @Override
+    protected void doEdit(RenderRequest request, RenderResponse response)
+            throws PortletException, IOException {
+        handleRequest(request, response);
+    }
+
+    /**
+     * Handle a request for the "help" portlet mode.
+     *
+     * @see #doView(RenderRequest, RenderResponse)
+     */
+    @Override
+    protected void doHelp(RenderRequest request, RenderResponse response)
+            throws PortletException, IOException {
+        handleRequest(request, response);
+    }
+
     @Override
     public void serveResource(ResourceRequest request, ResourceResponse response)
             throws PortletException, IOException {
index 2471680d29caa90b74b8c3e228086c09d28ea551..8effcb6c65491afc079523730c2961dabcea659c 100644 (file)
@@ -13,6 +13,8 @@ import javax.portlet.ActionResponse;
 import javax.portlet.EventRequest;
 import javax.portlet.EventResponse;
 import javax.portlet.MimeResponse;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletModeException;
 import javax.portlet.PortletResponse;
 import javax.portlet.PortletSession;
 import javax.portlet.PortletURL;
@@ -351,4 +353,31 @@ public class PortletApplicationContext2 extends AbstractWebApplicationContext {
                     "Shared parameters can only be set from a portlet request");
         }
     }
+
+    /**
+     * Sets the portlet mode. This may trigger a new render request.
+     *
+     * Portlet modes used by a portlet need to be declared in portlet.xml .
+     *
+     * @param window
+     *            a window in which the render URL can be opened if necessary
+     * @param portletMode
+     *            the portlet mode to switch to
+     * @throws PortletModeException
+     *             if the portlet mode is not allowed for some reason
+     *             (configuration, permissions etc.)
+     */
+    public void setPortletMode(Window window, PortletMode portletMode)
+            throws IllegalStateException, PortletModeException {
+        if (response instanceof MimeResponse) {
+            PortletURL url = ((MimeResponse) response).createRenderURL();
+            url.setPortletMode(portletMode);
+            window.open(new ExternalResource(url.toString()));
+        } else if (response instanceof StateAwareResponse) {
+            ((StateAwareResponse) response).setPortletMode(portletMode);
+        } else {
+            throw new IllegalStateException(
+                    "Portlet mode can only be changed from a portlet request");
+        }
+    }
 }
index 679468dd0b57be4d780df541af7fd670e67ef1a7..092bc4c55ec6150adec8e5719875abca0d81d485 100644 (file)
@@ -24,9 +24,9 @@ import com.vaadin.ui.Window;
 
 /**
  * TODO document me!
- * 
+ *
  * @author peholmst
- * 
+ *
  */
 @SuppressWarnings("serial")
 public class PortletCommunicationManager extends AbstractCommunicationManager {
@@ -165,6 +165,19 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
                             + themeName + "/" + resource);
         }
 
+        /**
+         * Find the application window to use based on the portlet mode. For
+         * internal use only, not in the {@link Callback} interface.
+         *
+         * @param request
+         * @param application
+         * @return
+         */
+        public Window getPortletWindow(PortletRequest request,
+                Application application) {
+            return portlet.getPortletWindow(request, application);
+        }
+
     }
 
     public PortletCommunicationManager(Application application) {
@@ -202,8 +215,9 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
             ((ActionResponse) response.getWrappedResponse())
                     .sendRedirect(dummyURL == null ? "http://www.google.com"
                             : dummyURL);
-        } else
+        } else {
             super.sendUploadResponse(request, response);
+        }
     }
 
     public void handleUidlRequest(ResourceRequest request,
@@ -223,4 +237,18 @@ public class PortletCommunicationManager extends AbstractCommunicationManager {
                 new AbstractApplicationPortletWrapper(applicationPortlet));
     }
 
+    @Override
+    protected Window doGetApplicationWindow(Request request, Callback callback,
+            Application application, Window assumedWindow) {
+        // find window based on portlet mode
+        if (assumedWindow == null
+                && callback instanceof AbstractApplicationPortletWrapper
+                && request.getWrappedRequest() instanceof PortletRequest) {
+            assumedWindow = ((AbstractApplicationPortletWrapper) callback)
+                    .getPortletWindow((PortletRequest) request
+                            .getWrappedRequest(), application);
+        }
+        return super.doGetApplicationWindow(request, callback, application,
+                assumedWindow);
+    }
 }