lofwyr14 closed pull request #14: demo: enum for <tc:selectOneChoice>
URL: https://github.com/apache/myfaces-tobago/pull/14
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DemoController.java
 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DemoController.java
index ff45348da..2d2a90349 100644
--- 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DemoController.java
+++ 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/DemoController.java
@@ -61,10 +61,6 @@
   private String radioValue;
   private Currency[] currencyItems;
 
-  private Salutation singleValue;
-
-  private Salutation[] multiValue;
-
   /*
     @Required
   */
@@ -96,26 +92,11 @@ public DemoController() {
         Currency.getInstance("USD"),
         Currency.getInstance("EUR")
     };
-    singleValue = Salutation.UNKNOWN;
     treeSelectMode = TREE_SELECT_MODE_KEYS[3];
     treeListboxSelectMode = TREELISTBOX_SELECT_MODE_KEYS[0];
-    multiValue = new Salutation[0];
     sheetConfig = new SheetConfig();
   }
 
-  private static SelectItem[] getSalutationSelectItems() {
-    final Salutation[] salutations = Salutation.values();
-    final SelectItem[] items = new SelectItem[salutations.length];
-    for (int i = 0; i < items.length; i++) {
-      final String label = 
DemoBundle.getString(FacesContext.getCurrentInstance(), 
salutations[i].getKey());
-      if (LOG.isTraceEnabled()) {
-        LOG.trace("label = " + label + "");
-      }
-      items[i] = new SelectItem(salutations[i], label);
-    }
-    return items;
-  }
-
   private static SelectItem[] getSelectItems(final Selectable[] keys) {
     final SelectItem[] items = new SelectItem[keys.length];
     for (int i = 0; i < items.length; i++) {
@@ -166,10 +147,6 @@ public boolean getShowPopup() {
     return currencyItems;
   }
 
-  public SelectItem[] getItems() {
-    return getSalutationSelectItems();
-  }
-
   public SelectItem[] getItems2() {
     return getSelectItems(TREE_SELECT_MODE_KEYS);
 
@@ -194,22 +171,6 @@ public void setRadioValue(final String radioValue) {
     this.radioValue = radioValue;
   }
 
-  public Salutation getSingleValue() {
-    return singleValue;
-  }
-
-  public void setSingleValue(final Salutation singleValue) {
-    this.singleValue = singleValue;
-  }
-
-  public Salutation[] getMultiValue() {
-    return multiValue;
-  }
-
-  public void setMultiValue(final Salutation[] multiValue) {
-    this.multiValue = multiValue;
-  }
-
   public Date getBasicDate() {
     return basicDate;
   }
diff --git 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Salutation.java
 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Salutation.java
index acfb8aa46..e809047b4 100644
--- 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Salutation.java
+++ 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Salutation.java
@@ -19,30 +19,25 @@
 
 package org.apache.myfaces.tobago.example.demo;
 
-public enum Salutation {
-
-  UNKNOWN("basic_itemUnknown"),
-
-  MR("basic_itemMr"),
+import javax.faces.context.FacesContext;
 
-  MRS("basic_itemMrs");
-
-  private String key;
+public enum Salutation {
 
-  Salutation(final String key) {
-    this.key = key;
-  }
+  UNKNOWN, MR, MRS;
 
-  public String getKey() {
-    return key;
+  public String getLabel() {
+    // XXX This implementation is only for demo. This should be 
optimized/cached.
+    return DemoBundle.getString(FacesContext.getCurrentInstance(), 
getClass().getSimpleName() + "_" + name());
   }
 
-  public static Salutation getSalutation(final String key) {
-    for (final Salutation salutation : values()) {
-      if (salutation.getKey().equals(key)) {
-        return salutation;
-      }
+  public String getIcon() {
+    switch (this) {
+      case MR:
+        return "fa-male";
+      case MRS:
+        return "fa-female";
+      default:
+        return null;
     }
-    return null;
   }
 }
diff --git 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SalutationConverter.java
 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SalutationConverter.java
deleted file mode 100644
index a901a05fc..000000000
--- 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SalutationConverter.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.myfaces.tobago.example.demo;
-
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.convert.Converter;
-import javax.faces.convert.ConverterException;
-
-public class SalutationConverter implements Converter {
-
-  @Override
-  public Object getAsObject(final FacesContext context, final UIComponent 
component, final String value)
-      throws ConverterException {
-    return Salutation.getSalutation(value);
-  }
-
-  @Override
-  public String getAsString(final FacesContext context, final UIComponent 
component, final Object value)
-      throws ConverterException {
-    if (value instanceof Salutation) {
-      return ((Salutation) value).getKey();
-    }
-    return "";
-  }
-}
diff --git 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SelectOneChoiceController.java
 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SelectOneChoiceController.java
index 9980f51b9..1e8e238f8 100644
--- 
a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SelectOneChoiceController.java
+++ 
b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/SelectOneChoiceController.java
@@ -28,6 +28,11 @@
 public class SelectOneChoiceController extends PlanetExample implements 
Serializable {
 
   private String person;
+  private Salutation salutation;
+
+  public Salutation[] getSalutations() {
+    return Salutation.values();
+  }
 
   public String getPerson() {
     return person;
@@ -36,4 +41,12 @@ public String getPerson() {
   public void setPerson(final String person) {
     this.person = person;
   }
+
+  public Salutation getSalutation() {
+    return salutation;
+  }
+
+  public void setSalutation(Salutation salutation) {
+    this.salutation = salutation;
+  }
 }
diff --git 
a/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
 
b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
index d65cd7070..faede15a8 100644
--- 
a/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
+++ 
b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo.xml
@@ -217,9 +217,9 @@
   <entry key="basic_sampleInputTitle">Basic Input Examples</entry>
   <entry key="basic_sampleSelectTitle">Basic Select Examples</entry>
   <entry key="basic_sampleLinkTitle">Basic Controls Examples</entry>
-  <entry key="basic_itemUnknown">none</entry>
-  <entry key="basic_itemMr">Mr.</entry>
-  <entry key="basic_itemMrs">Mrs.</entry>
+  <entry key="Salutation_UNKNOWN">none</entry>
+  <entry key="Salutation_MR">Mr.</entry>
+  <entry key="Salutation_MRS">Mrs.</entry>
   <entry key="basic_textboxLabel">Inputfield</entry>
   <entry key="basic_textboxTip">A simple input field</entry>
   <entry key="basic_suggestLabel">Suggest (AJAX)</entry>
diff --git 
a/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo_de.xml
 
b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo_de.xml
index 57db4b537..03bb7cdf4 100644
--- 
a/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo_de.xml
+++ 
b/tobago-example/tobago-example-demo/src/main/resources/org/apache/myfaces/tobago/example/demo/Demo_de.xml
@@ -58,9 +58,9 @@
   <entry key="basic_sampleInputTitle">Eingabe Beispiele</entry>
   <entry key="basic_sampleSelectTitle">Auswahl Beispiele</entry>
   <entry key="basic_sampleLinkTitle">Steuerungs Beispiele</entry>
-  <entry key="basic_itemUnknown">keine</entry>
-  <entry key="basic_itemMr">Herr</entry>
-  <entry key="basic_itemMrs">Frau</entry>
+  <entry key="Salutation_UNKNOWN">keine</entry>
+  <entry key="Salutation_MR">Herr</entry>
+  <entry key="Salutation_MRS">Frau</entry>
   <entry key="basic_textboxLabel">Eingabefeld</entry>
   <entry key="basic_textboxTip">Ein einfaches Eingabefeld</entry>
   <entry key="basic_suggestLabel">Vorschlag (AJAX)</entry>
diff --git 
a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml 
b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
index 740c82201..fff55cb3d 100644
--- 
a/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
+++ 
b/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
@@ -51,11 +51,6 @@
     
<!--<phase-listener>org.apache.myfaces.tobago.util.DebugPhaseListener</phase-listener>-->
   </lifecycle>
 
-  <converter>
-    <converter-id>salutationId</converter-id>
-    
<converter-class>org.apache.myfaces.tobago.example.demo.SalutationConverter</converter-class>
-  </converter>
-
   <converter>
     
<converter-id>org.apache.myfaces.tobago.example.demo.OnOffConverter</converter-id>
     
<converter-class>org.apache.myfaces.tobago.example.demo.OnOffConverter</converter-class>
diff --git 
a/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/030-select/20-selectOneChoice/selectOneChoice.xhtml
 
b/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/030-select/20-selectOneChoice/selectOneChoice.xhtml
index f826d99ab..a651a9704 100644
--- 
a/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/030-select/20-selectOneChoice/selectOneChoice.xhtml
+++ 
b/tobago-example/tobago-example-demo/src/main/webapp/content/20-component/030-select/20-selectOneChoice/selectOneChoice.xhtml
@@ -69,6 +69,15 @@
     <tc:button id="submit" label="Submit"/>
   </tc:section>
 
+  <tc:section label="Java enum as value">
+    <tc:selectOneChoice id="selectSalutation" label="Salutation" 
value="#{selectOneChoiceController.salutation}">
+      <tc:selectItems value="#{selectOneChoiceController.salutations}" var="s"
+                      itemLabel="#{s.label}" itemValue="#{s}" 
itemImage="#{s.icon}"/>
+    </tc:selectOneChoice>
+    <tc:out id="outputSalutation" label="Selected Salutation" 
value="#{selectOneChoiceController.salutation.label}"/>
+    <tc:button id="submit2" label="Submit"/>
+  </tc:section>
+
   <tc:section label="Ajax">
     <p>Select a planet and a moon. The list of moons depends on the selected 
planet.
       The planet-dropdownbox contain a <code 
class="language-markup">&lt;f:ajax render="moonbox"/></code>.


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to