View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.jsr168.pluto;
35  
36  import fr.paris.lutece.plugins.jsr168.pluto.core.PortletURLProviderImpl;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  
39  import org.apache.pluto.om.common.ObjectID;
40  import org.apache.pluto.om.portlet.ContentType;
41  import org.apache.pluto.om.portlet.PortletDefinition;
42  import org.apache.pluto.portalImpl.om.window.impl.PortletWindowImpl;
43  
44  import java.util.ArrayList;
45  import java.util.Iterator;
46  import java.util.List;
47  
48  import javax.portlet.PortletMode;
49  import javax.portlet.WindowState;
50  
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  
55  /**
56   * Buttons list to be displayed in portlet title
57   */
58  public class Buttons
59  {
60      private static final String PROPERTIES_MODE_PREFIX = "portlet.jsr168.mode.";
61      private static final String PROPERTIES_STATE_PREFIX = "portlet.jsr168.state.";
62      private static final String PROPERTIES_IMAGE_SUFFIX = ".image";
63      private final List _listModes;
64      private final List _listStates;
65  
66      /**
67       * Public default constructor
68       */
69      Buttons(  )
70      {
71          _listModes = new ArrayList(  );
72          _listStates = new ArrayList(  );
73      }
74  
75      /**
76       * Initialize list by inspecting current defined parameters
77       *
78       * @param request Current HTTP request
79       * @param response Current HTTP response (needed to encode URLs)
80       * @param portletDef Portlet definition (needed to access modes and states list)
81       * @param portletWindow Current portlet window
82       */
83      void init( HttpServletRequest request, HttpServletResponse response, PortletDefinition portletDef,
84          PortletWindowImpl portletWindow )
85      {
86          ObjectID portletID = portletWindow.getId(  );
87  
88          ArrayList listModes = new ArrayList(  );
89  
90          String strMimeType = "text/html";
91          ContentType contentTypeSet = portletDef.getContentTypeSet(  ).get( strMimeType );
92  
93          for ( Iterator itModes = contentTypeSet.getPortletModes(  ); itModes.hasNext(  ); )
94          {
95              listModes.add( itModes.next(  ) );
96          }
97  
98          for ( Iterator itModesStr = listModes.iterator(  ); itModesStr.hasNext(  ); )
99          {
100             PortletMode mode = (PortletMode) itModesStr.next(  );
101 
102             if ( !portletWindow.getPortletMode(  ).equals( mode ) )
103             {
104                 String strHelpRenderURL = PortletURLProviderImpl.getRenderURL( request, response, mode, null, portletID );
105                 String strHelpImagePath = AppPropertiesService.getProperty( PROPERTIES_MODE_PREFIX + mode +
106                         PROPERTIES_IMAGE_SUFFIX );
107 
108                 _listModes.add( new Button( strHelpRenderURL, strHelpImagePath ) );
109             }
110         }
111 
112         if ( !WindowState.MINIMIZED.equals( portletWindow.getWindowState(  ) ) )
113         {
114             String strHelpRenderURL = PortletURLProviderImpl.getRenderURL( request, response, null,
115                     WindowState.MINIMIZED, portletID );
116             String strHelpImagePath = AppPropertiesService.getProperty( PROPERTIES_STATE_PREFIX +
117                     WindowState.MINIMIZED + PROPERTIES_IMAGE_SUFFIX );
118 
119             _listStates.add( new Button( strHelpRenderURL, strHelpImagePath ) );
120         }
121 
122         // TODO il n'y a pas d'acces a la definition des etats (WindowState) en plus...
123         if ( !WindowState.NORMAL.equals( portletWindow.getWindowState(  ) ) )
124         {
125             String strHelpRenderURL = PortletURLProviderImpl.getRenderURL( request, response, null, WindowState.NORMAL,
126                     portletID );
127             String strHelpImagePath = AppPropertiesService.getProperty( PROPERTIES_STATE_PREFIX + WindowState.NORMAL +
128                     PROPERTIES_IMAGE_SUFFIX );
129 
130             _listStates.add( new Button( strHelpRenderURL, strHelpImagePath ) );
131         }
132 
133         if ( !WindowState.MAXIMIZED.equals( portletWindow.getWindowState(  ) ) )
134         {
135             String strHelpRenderURL = PortletURLProviderImpl.getRenderURL( request, response, null,
136                     WindowState.MAXIMIZED, portletID );
137             String strHelpImagePath = AppPropertiesService.getProperty( PROPERTIES_STATE_PREFIX +
138                     WindowState.MAXIMIZED + PROPERTIES_IMAGE_SUFFIX );
139 
140             _listStates.add( new Button( strHelpRenderURL, strHelpImagePath ) );
141         }
142     }
143 
144     /**
145      * Return an iterator for defined buttons for modes
146      *
147      * @return an iterator for defined buttons for modes
148      */
149     public Iterator modes(  )
150     {
151         return _listModes.iterator(  );
152     }
153 
154     /**
155      * Return an iterator for defined buttons for states
156      *
157      * @return an iterator for defined buttons for states
158      */
159     public Iterator states(  )
160     {
161         return _listStates.iterator(  );
162     }
163 }