public void setPolicyBuilder(PolicyBuilder newBuilder) {
// Get the previously selected variant builder so that we can attempt
// to reselect it after the change.
PolicyEditorContext context = (PolicyEditorContext) getContext();
Proxy previousSelectedProxy = context.getSelectedVariant();
VariantBuilder previouslySelectedBuilder =
previousSelectedProxy == null ?
null : (VariantBuilder) previousSelectedProxy.getModelObject();
Proxy policyProxy = getContext().getInteractionModel();
policyProxy.setReadWriteState(ReadWriteState.READ_WRITE);
// Because the merge process may have modified the existing model
// directly before calling this method, we must force the proxy's
// structure to be updated to ensure new variants are added and
// appropriate change events are fired.
policyProxy.setModelObject(newBuilder, true);
try {
context.getPolicyFileAccessor().updatePolicyProxyState(
policyProxy, context.getProject());
} catch (PolicyFileAccessException pfae) {
EclipseCommonPlugin.logError(BuilderPlugin.getDefault(),
getClass(), pfae);
}
if (previouslySelectedBuilder == null) {
context.setSelectedVariant(null);
} else {
BeanProxy modelProxy = (BeanProxy) context.getInteractionModel();
ListProxy variants = (ListProxy)
modelProxy.getPropertyProxy(PolicyModel.VARIANTS);
boolean setSelection = false;
for (int i = 0; !setSelection && i < variants.size(); i++) {
VariantBuilder builder = (VariantBuilder)
variants.getItemProxy(i).getModelObject();
if (previouslySelectedBuilder.equals(builder)) {
context.setSelectedVariant((BeanProxy) variants.getItemProxy(i));
setSelection = true;
}