public class MyCustomPlugin extends PropertyFieldPlugin {
private static final String OBSERVER_ID = "observerId";
private static final String CATEGORIES_PROPERTY_NAME = "categories";
private static final String DEFAULT_CATEGORIES_PROPERTY_NAME = "mynamespace:categories";
private static final String DEFAULT_OBSERVER_ID = "testobservable";
private final JcrPropertyValueModel<String> propValueModel;
public MyCustomPlugin(final IPluginContext context, final IPluginConfig config) {
super(context, config);
final String nodePath = getCategoriesPropertyPath(); //get the path of the node that external document picker relates to
final JcrPropertyModel<String> propModel = new JcrPropertyModel<>(nodePath);
propValueModel = new JcrPropertyValueModel<>(propModel);
final String observableId = config.getString(OBSERVER_ID, DEFAULT_OBSERVER_ID); //I have a property called "observerId" on the plugin.
final ObservableValueModel<String> observableValueModel = new CategoriesPathModel(propValueModel, nodePath); //Observe the changes on the external dropdown
observableValueModel.setObject(nodePath);
//Start a service that sends the observable model, with observableId.
//Dynamic dropdown knows about this observableId (more on this further down)
final ModelReference<String> modelReference = new ModelReference<>(observableId, observableValueModel);
modelReference.init(context);
}
// The field that external document picker is linked to is called "mynamespace:categories"
private String getCategoriesPropertyPath() {
final String categories = getPluginConfig().getString(CATEGORIES_PROPERTY_NAME, DEFAULT_CATEGORIES_PROPERTY_NAME);
final JcrNodeModel nodeModel = (JcrNodeModel) getDefaultModel();
return nodeModel.getItemModel().getPath() + "/" + categories;
}
//Apache wicket stuff.
@Override
protected void onDetach() {
propValueModel.detach();
super.onDetach();
}
//More Apache wicket stuff. This is how you pass data around
private static class CategoriesPathModel extends ObservableValueModel<String> {
private final String path;
CategoriesPathModel(final JcrPropertyValueModel<String> valueModel, final String path) {
super(valueModel);
this.path = path;
}
@Override
protected String load() {
return path;
}
}
}