[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Radiobuttons with Enum  XML
Forum Index -> Development
Author Message
cfuchs

Active

Joined: 21/01/2013 09:33:01
Messages: 16
Offline

Hallo together,

I am new to captaincasa and even after searching the board and reading the development guide, I still can't find a solution.

I have seen that the demoworkplace works with radiobuttons using String or boolean, however I want to use an enumeration (enum) for my radiobuttons, e.g. I have two radiobuttons and both link to a variable (enumWert) which has typ "Werte". If the first radiobutton is selected, the variable shall have the first enumvalue (WERTA) and if the second is selected, the enum value shall be WERTB.

Here is some example code:

JSP:
Code:
 <t:row id="g_4" >
 <t:radiobutton id="g_5" group="AUSWAHL" refvalue="A" text="Wert A" value="#{programm.enumWert}"  />
 
 <t:radiobutton id="g_6" group="AUSWAHL" refvalue="B" text="Wert B" value="#{programm.enumWert}"  />
 </t:row>
 


Java:
Code:
 	public enum Werte {
 		WERTA("A"), // 0
 		WERTB("B"); // 1
 
 		private String bezeichnung;
 		private Werte(String bezeichnung) {
 			this.bezeichnung = bezeichnung;
 		}
 
 		public String getBezeichnung() {
 			return this.bezeichnung;
 		}
 	}
 


In order to realize this, I have tried to modify the refvalue attribute to "A" or "B" or to "Werte.WERTA", however, I always get an IllegalArgumentException.

I know that it is possible to use a boolean if there are only two possible values, however I might have more, thus I really want to use enumerations.


Regards,

Christian


CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5534
Offline

Hi,

an example how to access enums is listed below...
Is this applicable for your scenario?

Regards, Björn

Code:
 public class DemoRadioButtonEnum
     extends WorkpageDispatchedBean 
     implements Serializable
 {
     public enum CarProducer
     {
         AUDI(1,"Audi AG"),
         MERCEDES(2,"Mercedes AG"),
         VOLKSWAGEN(3,"Volkswagen Deutschland AG");
         private final int id;
         private final String name;
         CarProducer(int id, String name)
         {
             this.id = id;
             this.name = name;
         }
     }
 
     public DemoRadioButtonEnum(IWorkpageDispatcher workpageDispatcher)
     {
         super(workpageDispatcher);        
         m_producer = CarProducer.AUDI;
     }
 
     CarProducer m_producer;
     public String getCarProducer() { return (m_producer != null) ? m_producer.toString() : null; }
     public void setCarProducer(String value) { m_producer = CarProducer.valueOf(value); }
 }
 
 
 <t:rowbodypane id="g_3" rowdistance="5">
 	<t:row id="g_4">
 		<t:radiobutton id="g_5" flush="true" group="CARPRODUCER"
 			refvalue="AUDI" text="Audi"
 			value="#{d.DemoRadioButtonEnum.carProducer}" />
 		<t:radiobutton id="g_6" flush="true" group="CARPRODUCER"
 			refvalue="MERCEDES" text="Mercedes"
 			value="#{d.DemoRadioButtonEnum.carProducer}" />
 		<t:radiobutton id="g_7" flush="true" group="CARPRODUCER"
 			refvalue="VOLKSWAGEN" text="Volkswagen"
 			value="#{d.DemoRadioButtonEnum.carProducer}" />
 	</t:row>
 	<t:row id="g_8">
 		<t:label id="g_9" text="Selected Value" width="100" />
 		<t:label id="g_10" text="#{d.DemoRadioButtonEnum.carProducer}" />
 	</t:row>
 </t:rowbodypane>
 
 

Björn Müller, CaptainCasa GmbH
cfuchs

Active

Joined: 21/01/2013 09:33:01
Messages: 16
Offline

Thanks for the reply!

My situation is a little more complex than this. I thought I could leave it simple but then a simple datatransformation would be a solution.

For my entities, I use hybernate, thus whenever I define a getter, this getter represents a field in the database. I know that it is possible to annotate in such a way that no field is generate, Nevertheless, as I am going to use quite a lot of enumerations, I was searching for an elegant solution.

Is there a way to define the getter / setter such that (refering to ur example)

Code:
 public CarProducer getCarProducer () {
   return m_producer;
 }
 
 public void setCarProducer(CarProducer producer) {
   m_producer = producer;
 }
 


If it is possible, how do I have to specify the refvalue in the JSP?


Thanks for ur help,

Christian
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5534
Offline

Hi,

no this is not possible.

...and I would not pass Hibernate objects directly into the user interface, because you always need some additional UI wrapping.

The most common way: wrap the object by some UIWrapper:

Code:
 public class UIBeanWrapper<CLASS extends Object>
 {
     CLASS m_bean;
     public UIBeanWrapper(CLASS bean)
     {
         m_bean = bean;
     }
     public CLASS getBean() { return m_bean; }
 }
 


THen you can refer to the original data by "#{d.xxxx.xxxx.bean.zzzz}" instead of "#{d.xxxx.yyyy.zzzz}".
And you can add own UI-properties into your extension of the wrapper.



Code:
 public class UICarWrapper extends UIBeanWarpper<Car>
 {
     ...
     ...
     public String getProducer() { ... }
     public void setProducer(String value) { ... }
 }
 


Regards, Björn

Björn Müller, CaptainCasa GmbH
bthalheim

Power User

Joined: 05/04/2012 11:45:24
Messages: 72
Offline

Hi,

I also have done something like this quite recently, my UI Bean Code looks like this:
Code:
 		private EintragsTyp eintragsTyp = EintragsTyp.KEINE_AKTION;
 
 		/**
 		 * @param eintragsTypAsString
 		 *            Die String-Repräsentation des Enum-Wertes, so dass er via {@link EintragsTyp#getEnum(String)} wiederhergestellt werden kann.
 		 */
 		public void setEintragsTyp(String eintragsTypAsString) {
 			this.eintragsTyp = EintragsTyp.getEnum(eintragsTypAsString);
 		}
 
 		/**
 		 * @return the selection
 		 */
 		public String getEintragsTyp() {
 			String eintragsTypAsString = this.eintragsTyp.getName();
 			return eintragsTypAsString;
 		}
 


Additionally to this, which is all that is necessary for the JSP Binding, I have an additional Getter for the use in the normal Java Code:
Code:
 		public EintragsTyp getEintragsTypEnum() {
 			return this.eintragsTyp;
 		}
 


I have even written a few lines of code which make sure that the mapping from the enum to the String and vice versa works well.

Cheers,

Björn
 
Forum Index -> Development
Go to:   
Powered by JForum 2.1.6 © JForum Team