Welcome!

Java IoT Authors: Pat Romanski, Elizabeth White, Liz McMillan, Yeshim Deniz, Mehdi Daoudi

Related Topics: Java IoT

Java IoT: 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)

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.


IoT & Smart Cities Stories
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DSR is a supplier of project management, consultancy services and IT solutions that increase effectiveness of a company's operations in the production sector. The company combines in-depth knowledge of international companies with expert knowledge utilising IT tools that support manufacturing and distribution processes. DSR ensures optimization and integration of internal processes which is necessary for companies to grow rapidly. The rapid growth is possible thanks, to specialized services an...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Scala Hosting is trusted by 50 000 customers from 120 countries and hosting 700 000+ websites. The company has local presence in the United States and Europe and runs an internal R&D department which focuses on changing the status quo in the web hosting industry. Imagine every website owner running their online business on a fully managed cloud VPS platform at an affordable price that's very close to the price of shared hosting. The efforts of the R&D department in the last 3 years made that pos...