// [gb:useInDocumentation]
// Use BoostedHashSets instead of normal HashSets, so that new elements
// will be created automatically by the editor
ApplicationDescription app = new ApplicationDescription(
new BoostedHashSet<ObjectDescription>(ObjectDescription.class),
new BoostedHashSet<ObjectDescription>(ObjectDescription.class),
new BoostedHashSet<ApplicationDescription>(
ApplicationDescription.class));
editor = SimpleBeanEditor.startEditor(app);
editor.getListeners().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
Object bean = event.getSource();
if (bean instanceof ApplicationDescription) {
// The BoostedHashSets should not be saved, so that the
// default classes are stored.
// So modify the default HashSet to BoostedHashSets after
// loading and back to HashSet before storing.
// modify sets to BoostedHashSets after loading
// modify sets to HashSet before saving
// modify sets to BoostedHashSets after saving
ApplicationDescription app = (ApplicationDescription) bean;
if (SimpleBeanEditor.ACTION_LOAD_BEAN.equals(event
.getActionCommand()))
changeToBoostedHashSet(app);
else if (SimpleBeanEditor.ACTION_SAVE_BEAN.equals(event
.getActionCommand())) {
if (event.getID() == GeneralConstants.START)
changeToHashSet(app);
else
changeToBoostedHashSet(app);
}
}
}
/**
* Changes the HashSets to BoostedHashSets
*/
private void changeToBoostedHashSet(ApplicationDescription app) {
BoostedHashSet<ObjectDescription> actions = new BoostedHashSet<ObjectDescription>(
ObjectDescription.class);
BoostedHashSet<ObjectDescription> values = new BoostedHashSet<ObjectDescription>(
ObjectDescription.class);
BoostedHashSet<ApplicationDescription> subApplications = new BoostedHashSet<ApplicationDescription>(
ApplicationDescription.class);
changeTo(app, actions, values, subApplications);
}
/**
* Changes the BoostedHashSets to HashSets.
*/
private void changeToHashSet(ApplicationDescription app) {
changeTo(app, new HashSet<ObjectDescription>(),
new HashSet<ObjectDescription>(),
new HashSet<ApplicationDescription>());
}
/**
* Exchanges the sets of actions, values and subapplications
*/
private void changeTo(ApplicationDescription app,
Set<ObjectDescription> actions,
Set<ObjectDescription> values,
Set<ApplicationDescription> subApplications) {
actions.addAll(app.getActions());
values.addAll(app.getValues());
subApplications.addAll(app.getSubApplications());
app.setActions(actions);
app.setValues(values);
app.setSubApplications(subApplications);
}
});