This documents describes how to create Custom Property Editors for Visual Designer v2. See general information about Property Editors here: MobilityDesigner2Properties.
Used shortcuts:
Simple PE is a PE what doesn't support user code input instead of particular type of value. For example for String value user can input only String but not an expression in java (or in other language) which will generate a String. To create simple PE developer should extend org.netbeans.modules.vmd.api.properties.DesignPropertyEditor class. Good example of simple PE is a org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorInstanceName class. This PE allows user input name of instance of some particular entity in the Visual Designer, for example "backCommand1". Obviously there is no need for user code support in this PE. Let's in brief see what each method does.
User code support allows user to input or select not particular value but also an java expression which will generate this value. To create PE with user code support developer should extend org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorUserCode class and provide at least one implementation of org.netbeans.modules.vmd.midp.propertyeditors.api.usercode.PropertyEditorElement class. Let's see as an example org.netbeans.modules.vmd.midp.propertyeditors.PropertyEditorInputMode class. This PE is intended for input mode specification of TextBox and TextField lcdui Form elements.
In constructor developer must provide label for user code element and invoke method initElements() with given elements:
private PropertyEditorInputMode() {
super(NbBundle.getMessage(PropertyEditorInputMode.class, "LBL_INPUT_MODE_UCLABEL")); // NOI18N
elements = new ArrayList<PropertyEditorElement>(2);
elements.add(new PredefinedEditor());
elements.add(new CustomEditor());
initElements(elements);
}
On the following picture each PropertyEditorElement bordered by red rectangle. Last one (for user code itseld) provided and handled by PropertyEditorUserCode infrastructure, first provided by PE developer.
Developer provides an implementation of following methods of PropertyEditorElement:
1st element implementation (PredefinedEditor) :
public void updateState(PropertyValue value) {
if (!isCurrentValueANull() && value != null) {
String inputMode;
for (int i = 0; i < model.getSize(); i++) {
inputMode = (String) model.getElementAt(i);
if (inputMode.equals((String) value.getPrimitiveValue())) {
model.setSelectedItem(inputMode);
break;
}
}
}
}
2nd element implementation (CustomEditor) :
public void updateState(PropertyValue value) {
if (!isCurrentValueANull() && value != null) {
String str = (String) value.getPrimitiveValue();
if (!isPredefined(str)) {
// if that value is not predefined
textField.setText(str);
}
} else {
textField.setText(null);
}
}
@Override
public void customEditorOKButtonPressed() {
super.customEditorOKButtonPressed();
for (PropertyEditorElement element : elements) {
if (element.getRadioButton().isSelected()) {
saveValue(element.getTextForPropertyValue());
break;
}
}
}
In other aspects PE with user code support works in the same manner as simple PE. Another good example of PE with user code using if PropertyEditorString. It provides only one PropertyEditorElement though.
This kind of PE extends functionality of PropertyEditorUserCode and allows user to manage so-called resources in the Visual Editor. Resources are such entities as fonts, images, etc. They are independent from other components and can be reused by several of them. For example one font object can be reused by different form or canvas elements in the design time (and in MIDP runtime correspondingly). The main problem what has been solved in PropertyEditorResource is that all resource changes should be committed to the model only after user pressed OK button. I.e. if user has changed one font's size or created another font and pressed "Cancel" then no changes should be done in the model. So changes are cached in memory by PropertyEditorResource. This PE except changing allows to create new or delete existing resource or resources. Let's see how to use PropertyEditorResource API by org.netbeans.modules.vmd.midp.propertyeditors.resource.elementsFontEditorElement class example. This PE is managing Font resources.
In the picture above PropertyEditorResourceElement is bordered by green rectangle, PropertyEditorElement is bordered by red one. So we can see so hierarchically out PEs and their elements are integrated into each other. Developer provides implementation of PropertyEditorResourceElement (org.netbeans.modules.vmd.midp.propertyeditors.resource.elements.FontEditorElement in our case) and PropertyEditorResource does all managing resources job for him. Let's see detailed methods of PropertyEditorResourceElement:
public TypeID getTypeID() {
return FontCD.TYPEID;
}
public List<String> getPropertyValueNames() {
return Arrays.asList(FontCD.PROP_FONT_KIND, FontCD.PROP_FACE, FontCD.PROP_STYLE, FontCD.PROP_SIZE);
}
private void checkNumberStatus() {
if (!radioButton.isSelected()) {
clearErrorStatus();
return;
}
if (!Pattern.matches("[\\d\\-]+", textField.getText())) { //NOI18N
displayWarning(PropertyEditorNumber.NON_DIGITS_TEXT);
} else {
clearErrorStatus();
}
try {
int number = Integer.valueOf(textField.getText());
if (number < 0 && !unlockedCheckBox.isSelected()) {
displayWarning(NbBundle.getMessage(PropertyEditorPreferredSize.class, "MSG_POSITIVE_CHARS")); //NOI18N
} else {
clearErrorStatus();
}
} catch (NumberFormatException ex) {
displayWarning(PropertyEditorNumber.NON_DIGITS_TEXT);
}
}
Contact: Anton Chechel
| font_property_editor.png | ![]() |
18747 bytes |
| input_mode.PNG | ![]() |
12900 bytes |
| input_mode.png | ![]() |
12900 bytes |
| instanceName.png | ![]() |
12817 bytes |