Welcome!

Java Authors: Maureen O'Gara, Liz McMillan, Walter H. Pinson, III, Yakov Werde, Tony Bishop

Related Topics: Java

Java: Article

Enterprise Java - Properties Editor Framework

Solving the problem of managing application properties

The PropertyEditorDialog class is responsible for a big part of the work involved in creating the user interface for the component. As the name suggests, it extends JDialog; it also implements the Observer and ListSelectionListener interfaces. The Observer interface is implemented to change the title of the dialog when any of the property value is changed.

The PropertyEditorDialog obtains the number of groups of property key-values in the EditorFile and creates an equal number of JTables to render them. These JTables are, in turn, put in a JTabbedPane, one tab for each group, the tab's title corresponding to the group name. The PropertyEditorDialog attaches itself as the ListSelectionListeners for all the Jtables. The PropertyEditorDialog gets the description text for the property on the valueChanged event and displays it on the description JEdtiorPane.

Finally, let's look at the rendering and editing mechanisms for the Property Key-Value pairs.

The toVisual method of the Group creates the EditorTable and the TableModel for the EditorTable. The EditorTable, in turn, creates and sets an instance of the EditorTableCellRenderer as the cell renderer and an instance of the EditorTableCellEditor as the cell editor for the columns of the Editor-Table.

The EditorTableCellRenderer and EditorTableCellEditor create the controls necessary to render and edit the property values. The different controls for each Property type are listed in Table 4.

EditorTableCellEditor adds an anonymous inner class to each control overriding the functionality for updating the controls to include actions to set the new value back to the model and invalidate the model.

Persisting the Properties
Writing the Property key-values back to the Properties File is pretty straightforward. A BufferedWriter is created with the instance of EditorFile as the FileWriter. The Parameters are written group-by-group into this file. Concatenating the text form of each instance of the Parameter hierarchy creates each group.

Extending the Framework To Add New Type and Controls
Now we'll discuss adding a new type for a property and the corresponding control to edit the property's value. Say your application uses JDBC to connect to a database and you want to give the user the ability to specify the JDBC driver to use via a property. The framework, by default, doesn't support the required type, which lets you choose a valid driver from a list. We'll see how to extend the framework to add this new type.

To display all the JDBC drivers we could use a dropdown list with all the JDBC drivers in the classpath. Let's name the new parameter type JDBCDRIVER.

For the new type, we'd need to define a new class on the model side, say, ParameterJDBCDriver, which is a sub-class of the ParameterSymbol class. The new class needs to implement the method "toText" that should return the driver's fully qualified class name. The following code snippet does just that:


public class ParameterJDBCDriver extends ParameterSymbol {
String strBaseClassName;
...
public ParameterJDBCDriver(String name, String value, String baseClassName,
boolean editable, String doc) {
Driver[] driversInClasspath = ClassUtils.findAllDriversInClasspath(baseClassName);
...
setAllowedValues(driverNames);
type = "JDBCDRIVER";
}
public String toText(){
String newLine = System.getProperty("line.separator");
return toTextType() + newLine + toTextValue();
}
}

Now we need to add the following code to the createParameter method of the Parser class. It creates an instance of ParameterJDBCDriver when the Parameter type "JDBC-DRIVER" is encountered in the properties file.


if (pType.compareToIgnoreCase("JDBCDRIVER") == 0) {
p = new ParameterJDBCDriver(pName, pValue, pBaseClass,pEdit, pDoc);
}

This completes the changes needed on the model. On the view front, since we have decided to show the values in a drop down list, we can reuse the combo box control used to display values of type Symbol/Boolean. But we have to make changes in a couple of classes to ensure that the dropdown list is correctly rendered for the new Property type.

The following code needs to be added to the createComponent method of the EditorTableCellRenderer class. It will render the FormattedComboBox control for the new Property type.


else if (cellValue instanceof ParameterJDBCDriver) {
o = new FormattedComboBox(cellValue,
((ParameterJDBCDriver) cellValue).getAllowedValues(),
value.toString()) {
public void itemStateChanged(ItemEvent e) {
}
};

}

Now we need to add the following code to the createComponent method of EditorTableCellEditor class to ensure that editing changes are reflected correctly.


else if (cellValue instanceof ParameterJDBCDriver) {
o = new FormattedComboBox(cellValue, ((ParameterJDBCDriver)
cellValue).getAllowedValues(), value.toString()) {
public void itemStateChanged(ItemEvent e) {
((ParameterJDBCDriver) cellValue).setValue(getSelectedItem().toString());
PropEditor.eFile.setModified(true);
}
};
}

The last step is to compile all the changes and we're good to go! You can fire up the PropertyEditor Dialog and verify that it works.

More Stories By David Bismut

David Bismut is a third-year student at the Ecole des Mines in Nantes (EMN), a French engineering school. He specializes in information management technologies. He was part of InStep, Infosys's global internship program in 2004.

More Stories By Krishnakumar Pooloth

Krishnakumar Pooloth is a senior technical architect with Infosys Technologies. His areas of expertise include object design, component technology, Java, and expert systems. He holds a Bachelor's degree in electronics and communication from Calicut University, India.

More Stories By Swaminathan Natarajan

Swaminathan Natarajan is a technical architect with Infosys Technologies. His area of expertise is Java, repository technologies, and metadata management.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Java Developer's Journal 07/31/05 06:42:09 PM EDT

Enterprise Java - Properties Editor Framework
Property files are frequently used in systems built using Java whether it's a thick Java client, a servlet, or a business component. Java specifies the format for a property file and provides the Properties class to read from and write to these files. However, Java is silent on the aspects related to validations of a value entered in a property file, providing room for errors to creep into an application system. How many times have you started to debug a failure in an application only to realize that it's because of an incorrect value in a property file?