-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Firestarter builds on Java's bean customization API, and provides simple user interface components for viewing and editing properties of objects.
A property sheet is a table that can be used to edit the properties of a Java object. The most common use case for firestarter is creating and displaying basic property sheets for editing object properties. Here is the basic code for editing the properties of a JLabel:
// set up UI elements with a parent window "window"
// before showing the property sheet, you need to register the editors
EditorRegistration.registerEditors();
// to show a dialog for editing a component
JLabel label = new JLabel();
PropertySheetDialog.show(window, true, label);
The parameters of this static method include the parent window for the dialog, a modal property indicating whether the dialog should be modal, and the object to be edited. When executed, this shows a dialog as shown below:
If you want to create a component to add elsewhere in the UI, instead of showing a dialog, use PropertySheet sheet = PropertySheet.forBean(label);
If you want to limit what properties are being edited, you can provide a Predicate
as a fourth argument. (See the wiki page on Google guava's functional programming support for more on predicates.)
PropertySheetDialog.show(this, true, jLabel1, new Predicate<String>(){
@Override
public boolean apply(String input) {
return Arrays.asList("foreground", "background", "text").contains(input);
}
});
Sometimes the properties you want users to edit are stored in other ways. In this case, you can create a custom instance of com.googlecode.blaisemath.firestarter.PropertyModel
. Here is an example that can be used to edit properties within a map:
public class ExamplePropertyModel extends PropertyModelSupport {
/** Properties to edit */
public final List<String> props;
/** Types of the properties */
public final Map<String,Class> types;
/** Map being edited */
public final Map<String,Object> map;
public ExamplePropertyModel(Map<String, Class> types, Map<String, Object> map) {
this.props = new ArrayList<String>(types.keySet());
this.types = types;
this.map = map;
}
@Override
public int getSize() {
return props.size();
}
@Override
public String getElementAt(int index) {
return props.get(index);
}
@Override
public Class getPropertyType(int row) {
return types.get(props.get(row));
}
@Override
public boolean isWritable(int row) {
return true;
}
@Override
public Object getPropertyValue(int row) {
return map.get(props.get(row));
}
@Override
public void setPropertyValue(int row, Object value) {
map.put(props.get(row), value);
}
}
To use this model, construct a map encoding the expected types, and a second map with the values to be edited.
Map<String,Class> typ = ImmutableMap.<String,Class>of(
"prop 1", Integer.class, "prop 2", Color.class, "prop 3", Double.class);
Map<String,Object> map = ImmutableMap.<String,Object>of(
"prop 1", 10, "prop 2", Color.white, "prop 3", 15.5);
PropertySheetDialog.show(this, true, map, new TestPropertyModel(typ, map));
There are a few additional UI classes that can help you construct a sidebar displaying multiple property sheets.
RollupPanel rollupPanel = new RollupPanel();
rollupPanel.add("Label Properties", PropertySheet.forBean(label));
rollupPanel.add("Rectangle Properties", PropertySheet.forBean(new Rectangle()));
// now add the rollupPanel to another component
When you add a component to com.googlecode.blaisemath.util.RollupPanel
, it is displayed inside a collapsible panel. Here is the result: