View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UISelectManyShuttle;
23  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
24  import org.apache.myfaces.tobago.layout.Measure;
25  import org.apache.myfaces.tobago.renderkit.SelectManyRendererBase;
26  import org.apache.myfaces.tobago.renderkit.css.Classes;
27  import org.apache.myfaces.tobago.renderkit.css.Style;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlButtonTypes;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
32  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
33  import org.apache.myfaces.tobago.util.ComponentUtils;
34  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
35  
36  import javax.faces.component.UIComponent;
37  import javax.faces.context.FacesContext;
38  import javax.faces.model.SelectItem;
39  import java.io.IOException;
40  import java.util.List;
41  
42  public class SelectManyShuttleRenderer extends SelectManyRendererBase {
43  
44    @Override
45    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
46      UISelectManyShuttle select = (UISelectManyShuttle) component;
47      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
48      writer.startElement(HtmlElements.DIV, select);
49      Style style = new Style(facesContext, select);
50      writer.writeStyleAttribute(style);
51      writer.writeClassAttribute(Classes.create(select));
52      String clientId = select.getClientId(facesContext);
53      writer.writeIdAttribute(clientId);
54      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
55      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
56      if (title != null) {
57        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
58      }
59      boolean hasLabel = select.hasLabel();
60      // TODO get buttonWidth and label Height from theme
61      Measure buttonWidth = Measure.valueOf(50);
62  
63      Measure labelHeight = Measure.valueOf(18);
64      style.setTop(Measure.valueOf(0));
65      style.setLeft(Measure.valueOf(0));
66      Measure width = style.getWidth();
67      Measure selectWidth = width.subtract(buttonWidth).divide(2);
68      style.setWidth(selectWidth);
69      Style labelStyle = null;
70      if (hasLabel) {
71        labelStyle = new Style(style);
72        labelStyle.setHeight(labelHeight);
73        style.setHeight(style.getHeight().subtract(labelHeight));
74        style.setTop(style.getTop().add(labelHeight));
75      }
76      List<SelectItem> items = RenderUtils.getSelectItems(select);
77      boolean disabled = items.size() == 0 || select.isDisabled() || select.isReadonly();
78  
79      String unselectedLabel = select.getUnselectedLabel();
80      if (unselectedLabel != null) {
81        writer.startElement(HtmlElements.DIV, null);
82        writer.writeStyleAttribute(labelStyle);
83        writer.writeClassAttribute(Classes.create(select, "unselectedLabel"));
84        writer.flush(); // is needed in some cases, e. g. TOBAGO-1094
85        writer.write(unselectedLabel);
86        writer.endElement(HtmlElements.DIV);
87      }
88      writer.startElement(HtmlElements.SELECT, null);
89      String unselectedClientId = clientId + ComponentUtils.SUB_SEPARATOR + "unselected";
90      writer.writeIdAttribute(unselectedClientId);
91      writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
92  
93      // TODO tabIndex
94      Integer tabIndex = select.getTabIndex();
95      if (tabIndex != null) {
96        writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
97      }
98  
99      writer.writeStyleAttribute(style);
100     writer.writeClassAttribute(Classes.create(select, "unselected"));
101 
102     writer.writeAttribute(HtmlAttributes.MULTIPLE, HtmlAttributes.MULTIPLE, false);
103 
104     Object[] values = select.getSelectedValues();
105     HtmlRendererUtils.renderSelectItems(select, items, values, false, writer, facesContext);
106 
107     writer.endElement(HtmlElements.SELECT);
108     writer.startElement(HtmlElements.DIV, null);
109     style.setLeft(style.getLeft().add(selectWidth));
110     style.setWidth(buttonWidth);
111     writer.writeStyleAttribute(style);
112     writer.startElement(HtmlElements.DIV, null);
113     writer.writeClassAttribute(Classes.create(select, "toolBar"));
114     createButton(facesContext, component, writer, disabled, "image/selectManyShuttleAddAll.gif", "addAll");
115     createButton(facesContext, component, writer, disabled, "image/selectManyShuttleAdd.gif", "add");
116     createButton(facesContext, component, writer, disabled, "image/selectManyShuttleRemove.gif", "remove");
117     createButton(facesContext, component, writer, disabled, "image/selectManyShuttleRemoveAll.gif", "removeAll");
118     writer.endElement(HtmlElements.DIV);
119     writer.endElement(HtmlElements.DIV);
120     String selectedLabel = select.getSelectedLabel();
121     if (selectedLabel != null) {
122       writer.startElement(HtmlElements.DIV, null);
123       labelStyle.setLeft(labelStyle.getLeft().add(selectWidth).add(buttonWidth));
124       writer.writeStyleAttribute(labelStyle);
125       writer.writeClassAttribute(Classes.create(select, "selectedLabel"));
126       writer.flush(); // is needed in some cases, e. g. TOBAGO-1094
127       writer.write(selectedLabel);
128       writer.endElement(HtmlElements.DIV);
129     }
130 
131     writer.startElement(HtmlElements.SELECT, select);
132     String selectedClientId = clientId + ComponentUtils.SUB_SEPARATOR + "selected";
133     writer.writeIdAttribute(selectedClientId);
134 
135     writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
136     if (tabIndex != null) {
137       writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
138     }
139     style.setWidth(selectWidth);
140     style.setLeft(style.getLeft().add(buttonWidth));
141     writer.writeStyleAttribute(style);
142     writer.writeClassAttribute(Classes.create(select, "selected"));
143     writer.writeAttribute(HtmlAttributes.MULTIPLE, HtmlAttributes.MULTIPLE, false);
144     HtmlRendererUtils.renderSelectItems(select, items, values, true, writer, facesContext);
145 
146     writer.endElement(HtmlElements.SELECT);
147     writer.startElement(HtmlElements.SELECT, select);
148     writer.writeClassAttribute(Classes.create(component, "hidden"));
149     String hiddenClientId = clientId + ComponentUtils.SUB_SEPARATOR + "hidden";
150     writer.writeIdAttribute(hiddenClientId);
151     writer.writeNameAttribute(clientId);
152     writer.writeAttribute(HtmlAttributes.MULTIPLE, HtmlAttributes.MULTIPLE, false);
153     writer.writeAttribute(HtmlAttributes.REQUIRED, select.isRequired());
154     HtmlRendererUtils.renderCommandFacet(select, facesContext, writer);
155     HtmlRendererUtils.renderSelectItems(select, items, values, writer, facesContext);
156 
157     writer.endElement(HtmlElements.SELECT);
158 
159     writer.endElement(HtmlElements.DIV);
160   }
161 
162   private void createButton(FacesContext context, UIComponent component, TobagoResponseWriter writer,
163         boolean disabled, String image, String sub) throws IOException {
164     writer.startElement(HtmlElements.BUTTON, null);
165     writer.writeAttribute(HtmlAttributes.TYPE, HtmlButtonTypes.BUTTON, false);
166     writer.writeClassAttribute(Classes.create(component, sub));
167     writer.writeIdAttribute(component.getClientId(context) + ComponentUtils.SUB_SEPARATOR + sub);
168     writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
169     String imagePath = ResourceManagerUtils.getImageOrDisabledImageWithPath(context, image, disabled);
170     writer.startElement(HtmlElements.IMG, null);
171     writer.writeAttribute(HtmlAttributes.SRC, imagePath, true);
172     writer.endElement(HtmlElements.IMG);
173     writer.endElement(HtmlElements.BUTTON);
174   }
175 }