View Javadoc

1   /*
2    * Copyright 2003,2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portlet.admin.taglib;
17  
18  import java.io.IOException;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import javax.portlet.PortletRequest;
25  import javax.portlet.PortletSession;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.JspTagException;
28  import javax.servlet.jsp.JspWriter;
29  import javax.servlet.jsp.tagext.TagSupport;
30  
31  import org.apache.pluto.Constants;
32  import org.apache.pluto.portlet.admin.PlutoAdminConstants;
33  import org.apache.pluto.portlet.admin.bean.PortletTO;
34  
35  /***
36   * Taglib that creates an HTML select control containing portlet
37   * names and values as defined in the <code>PortletTO</code>
38   * class.
39   *
40   * @author Craig Doremus
41   * @see org.apache.pluto.portlet.admin.bean.PortletTO
42   *
43   */
44  public class PortletSelectTag extends TagSupport {
45  
46  	private Map portletMap = null;
47  	private int row = 1;
48  	private int column = 1;
49  
50  	public int doStartTag()	throws JspException {
51      PortletRequest request = (PortletRequest)pageContext.getRequest().getAttribute(Constants.PORTLET_REQUEST);
52      PortletSession session = request.getPortletSession();
53      List portlets = (List)session.getAttribute(PlutoAdminConstants.PORTLET_APP_LIST_ATTR);
54  
55      if (portletMap != null ) {
56  	      try {
57  	        JspWriter out = pageContext.getOut();
58  	        out.println("<select name=\"portlet" + row + "." + column + "\">");
59  	        Set vals = portletMap.entrySet();
60  	        Iterator iter = vals.iterator();
61  	        while (iter.hasNext()) {
62  	        	Map.Entry item = (Map.Entry) iter.next();
63  	        	String name = (String)item.getKey();
64  	        	String val = (String)item.getValue();
65  		        out.print("<option value=\"" + name + "_" + val + "\"");
66  		        //find out what portlet should be the 'checked' one
67  		        if (portlets != null) {
68  		        	Iterator iter2 = portlets.iterator();
69  		        	while(iter.hasNext()) {
70  		        		PortletTO plet = (PortletTO)iter2.next();
71  		        		int currrow = plet.getRow();
72  		        		int currcol = plet.getCol();
73  		        		if (row == currrow && column == currcol) {
74  		  		        out.print(" checked ");
75  		  		        break;
76  		        		}
77  		        	}
78  		        }
79  		        out.print(">");
80  		        out.print(name);
81  		        out.print("</option>");
82  	        }
83  	        out.println("</select>");
84  	      } catch (IOException e) {
85  	        throw new JspTagException("Error: " + e.toString());
86  	      }
87  	    }
88  			return SKIP_BODY;
89  	  }
90  
91  	/***
92  	 * @param portletMap The portletMap to set.
93  	 */
94  	public void setPortletMap(Map portletMap) {
95  		this.portletMap = portletMap;
96  	}
97  	/***
98  	 * @param column The column to set.
99  	 */
100 	public void setColumn(int column) {
101 		this.column = column;
102 	}
103 	/***
104 	 * @param row The row to set.
105 	 */
106 	public void setRow(int row) {
107 		this.row = row;
108 	}
109 }