You want to use a context attribute that can only have a few predefined values and you want to assign values to this context attribute using self explaining JAVA constants rather than integers.
An example for this is the WDVisibility type of Web Dynpro framework. If you assign a value to a context attribute of this type you use e.g.
wdContext.currentContextElement().setMyAttribute( WDVisibility.VISIBLE );
To achieve this for your own simple types you first have to create a simple type in your local dictionary and define the allowed values at the enumeration tab.
If you then create a context attribute of this type and bind it to a UI element the Web Dynpro framework does the validation against the enumeration for you (Users can only enter one of the predefined values in the UI element).
But the question is how to assign a value to the context attribute in your code without using the keys of the enumeration directly but, more JAVA like, using constants.
The solution is quite simple. In the same package that your simple type belongs to (may also be another one) you create a JAVA class with the same name as your simple type.
In this class you declare constants for all enumeration values. You can now assign a constant to the context attribute in spite the enumeration key.
The best of it is that if you add your new type to a public part of a DC other developers can also use the type AND the constants in DCs that are using your DC.
Lets say we want to use the context attribute “gender” that can only have the values “male” and “female”.
1. In your DC or Web Dynpro project go to “Dictionaries / Local Dictionary / Data Types / Simple Types”.
2. Create the simple type “gender” in package com.test.simpletypes (or any package you like) by invoking the “create simple type” item of the context menu.
3. Change the Build-In Type to “short”.
4. Move to tab “enumeration” and add two new key-value pairs (0 = male, 1 = female).
5. Open the Navigator view and move to the folder “packages/com/test/simpletypes” of your DC or Web Dynpro project.
6. Create a new JAVA class in this package. Give it the name “Gender”.
7. Add the following lines to the class
public static final short MALE = 0;
public static final short FEMALE = 1;
8. Save your JAVA file.
You can now create a context attribute, lets say “attribGender” of type “com.test.simpletypes.gender” and assign a value to it in your code with the following line of code
wdContext.currentContextElement().setAttribGender( Gender.MALE );