| By David Bismut, Krishnakumar Pooloth, Swaminathan Natarajan | Article Rating: |
|
| July 31, 2005 07:00 PM EDT | Reads: |
44,419 |
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.
Published July 31, 2005 Reads 44,419
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
Java Developer's Journal 07/31/05 06:42:09 PM EDT | |||
Enterprise Java - Properties Editor Framework |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?





































