@Override public Object caseEAttribute(EAttribute eAttribute) { try { eAttribute.getDefaultValue(); } catch (Exception e) { eAttribute.setDefaultValueLiteral(null); } return eAttribute; }
@SuppressWarnings("unchecked") private static void handleDefaults(EObject root) { Queue<EObject> queue = new LinkedList<EObject>(); queue.add(root); // root while (!queue.isEmpty()) { EObject eObject = queue.remove(); EClass eClass = eObject.eClass(); for (EAttribute attribute : eClass.getEAllAttributes()) { // visit if (!eObject.eIsSet(attribute) && attribute.getLowerBound() > 0 && attribute.getDefaultValueLiteral() != null) { if (attribute.isMany()) { List<Object> list = (List<Object>) eObject.eGet(attribute); list.add(attribute.getDefaultValue()); } else { eObject.eSet(attribute, attribute.getDefaultValue()); } } } for (EReference reference : eClass.getEAllReferences()) { // process successors Object value = eObject.eGet(reference); if (value != null) { if (reference.isMany()) { queue.addAll((List<EObject>) value); } else { queue.add((EObject) value); } } } } }
@Override @SuppressWarnings("unchecked") public EObject createPObjectInstance(Object object, PObject po) throws CreateObjectException { EObject poInstance = super.createPObjectInstance(object, po); Map<String, Object> map = (Map<String, Object>) object; List<EAttribute> attributes = po.getEAttributes(); for (EAttribute attribute : attributes) { String attrName = attribute.getName(); Object value = null; if (map.containsKey(attrName)) { value = map.get(attrName); } else { if (attribute.getDefaultValueLiteral() != null) { value = attribute.getDefaultValue(); } else { throw new CreateObjectException(MessageFormat.format("Can''t create instance of {0} protected object: the value of attribute {1} isn''t defined", po.getName(), attrName)); } } try { poInstance.eSet(attribute, value); } catch (ClassCastException e) { throw new CreateObjectException(MessageFormat.format("Can''t create instance of {0} protected object: the value of attribute {1} has incorrect type: {2}", po.getName(), attrName, e.getLocalizedMessage())); } } return poInstance; }
public Object getPropertyDefaultValue(Object o) { if (feature instanceof EAttribute) { EAttribute attribute = (EAttribute)feature; if (!attribute.isMany()) { Object result = attribute.getDefaultValue(); if (result == null) { result = getDefaultValue(attribute.getEType()); } if (result == null) { return XSDEditPlugin.INSTANCE.getString("_UI_DefaultValue_label", new Object [] { "" }); } else { return XSDEditPlugin.INSTANCE.getString("_UI_DefaultValue_label", new Object [] { this.itemDelegator.getText(result) }); } } } return null; } }