public Command deleteWithCommand() { if (canDelete()) { XamlAttribute bindAttr = (XamlAttribute) bindingNode.eContainer(); return new DeleteCommand(bindAttr); } return null; }
private void computeAttatchCommand(XamlAttribute parent, FormAttachment attachment) { XamlElement attachmentChild = parent.getChild("FormAttachment", IConstants.XWT_NAMESPACE); if (attachmentChild == null) { attachmentChild = XamlFactory.eINSTANCE.createElement("FormAttachment", IConstants.XWT_NAMESPACE); } Control control = attachment.control; if (control != null) { String controlName = getName(control); if (controlName == null) { controlName = NamedCommand.generateName(control); addCommand(new NamedCommand(control, controlName)); } XamlAttribute controlAttr = attachmentChild.getAttribute("control", IConstants.XWT_NAMESPACE); if (controlAttr == null) { controlAttr = XamlFactory.eINSTANCE.createAttribute("control", IConstants.XWT_NAMESPACE); } XamlElement bindChild = controlAttr.getChild("Binding", IConstants.XWT_NAMESPACE); if (bindChild == null) { bindChild = XamlFactory.eINSTANCE.createElement("Binding", IConstants.XWT_NAMESPACE); } ApplyAttributeSettingCommand command = new ApplyAttributeSettingCommand( bindChild, "ElementName", IConstants.XWT_NAMESPACE, controlName); addCommand(command); addNewChild(controlAttr, bindChild); addNewChild(attachmentChild, controlAttr); /* if (attachment.alignment != SWT.DEFAULT) */ { String alignment = "SWT.DEFAULT"; switch (attachment.alignment) { case SWT.TOP: alignment = "SWT.TOP"; break; case SWT.BOTTOM: alignment = "SWT.BOTTOM"; break; case SWT.RIGHT: alignment = "SWT.RIGHT"; break; case SWT.CENTER: alignment = "SWT.CENTER"; break; case SWT.LEFT: alignment = "SWT.LEFT"; break; } command = new ApplyAttributeSettingCommand( attachmentChild, "alignment", IConstants.XWT_NAMESPACE, alignment); addCommand(command); } } else { addCommand(new DeleteCommand(attachmentChild.getAttribute("control"))); } /* if (attachment.denominator != 100) */ { ApplyAttributeSettingCommand command = new ApplyAttributeSettingCommand( attachmentChild, "denominator", IConstants.XWT_NAMESPACE, Integer.toString(attachment.denominator)); addCommand(command); } /* if (attachment.numerator != 0) */ { ApplyAttributeSettingCommand command = new ApplyAttributeSettingCommand( attachmentChild, "numerator", IConstants.XWT_NAMESPACE, Integer.toString(attachment.numerator)); addCommand(command); } /* if (attachment.offset != 0) */ { ApplyAttributeSettingCommand command = new ApplyAttributeSettingCommand( attachmentChild, "offset", IConstants.XWT_NAMESPACE, Integer.toString(attachment.offset)); addCommand(command); } addNewChild(parent, attachmentChild); }
public Command bindWithCommand() { if (!canBinding()) { return null; } CompoundCommand commandList = new CompoundCommand(); Property targetProperty = context.getTargetProperty(); Property modelProperty = context.getModelProperty(); IObservable target = context.getTarget(); IObservable model = context.getModel(); Object targetSource = target.getSource(); Object modelSource = model.getSource(); XamlNode node = null; if (targetSource instanceof Widget) { node = XWTProxy.getModel((Widget) targetSource); } else if (targetSource instanceof XamlElement) { node = (XamlElement) targetSource; } if (node == null) { return UnexecutableCommand.INSTANCE; } // 1. Get attr of targetProperty, such as < text="{}"/> XamlAttribute attribute = node.getAttribute(targetProperty.getName(), IConstants.XWT_NAMESPACE); if (attribute != null) { commandList.add(new DeleteCommand(attribute)); attribute = null; } if (attribute == null) { attribute = XamlFactory.eINSTANCE.createAttribute(targetProperty.getName(), IConstants.XWT_NAMESPACE); } // 2. Binding Node. if (bindingNode == null) { bindingNode = attribute.getChild(BINDING, IConstants.XWT_NAMESPACE); } if (bindingNode == null) { bindingNode = XamlFactory.eINSTANCE.createElement(BINDING, IConstants.XWT_NAMESPACE); } // 3. ElementName int type = model.getType(); if (type == IObservable.OBSERVE_SWT_JFACE) { addAttr( bindingNode, ELEMENT_NAME, IConstants.XWT_NAMESPACE, getCreateName(modelSource, commandList)); } // 4. DataContext else if (IObservable.OBSERVE_JAVA_BAEN == type) { XamlDocument ownerDocument = node.getOwnerDocument(); XamlElement rootElement = ownerDocument.getRootElement(); DataContext dataContext = (DataContext) modelSource; String key = dataContext.getKey(); if (key != null) { boolean canUseDC = getCodeStyles().useDataContext; if (canUseDC) { XamlAttribute contextNode = getAttribute(rootElement, DATA_CONTEXT, IConstants.XWT_NAMESPACE); contextNode.setUseFlatValue(true); XamlElement resourceNode = getElement(contextNode, STATIC_RESOURCE, IConstants.XWT_NAMESPACE); if (!key.equals(resourceNode.getValue())) { if (resourceNode.getValue() != null) { canUseDC = false; } else { resourceNode.setValue(key); commandList.add(new AddNewChildCommand(contextNode, resourceNode)); commandList.add(new AddNewChildCommand(rootElement, contextNode)); } } } if (!canUseDC || !getCodeStyles().useDataContext) { XamlAttribute sourceAttr = getAttribute(bindingNode, SOURCE, IConstants.XWT_NAMESPACE); sourceAttr.setUseFlatValue(true); XamlElement resourceNode = getElement(sourceAttr, STATIC_RESOURCE, IConstants.XWT_NAMESPACE); resourceNode.setValue(key); commandList.add(new AddNewChildCommand(sourceAttr, resourceNode)); commandList.add(new AddNewChildCommand(bindingNode, sourceAttr)); } } } // 5. Path addAttr(bindingNode, PATH, IConstants.XWT_NAMESPACE, modelProperty.toString()); // 6. BindingMode if (bindingMode != null && BindingMode.TwoWay.equals(bindingMode)) { addAttr(bindingNode, MODE, IConstants.XWT_NAMESPACE, bindingMode.name()); } // 7. Converter. if (converter != null) { addAttr(bindingNode, CONVERTER, IConstants.XWT_NAMESPACE, converter); } // 8. UpdateSourceTrigger if (triggerMode != null) { addAttr(bindingNode, UPDATE_SOURCE_TRIGGER, IConstants.XWT_NAMESPACE, triggerMode.name()); } // 9. Code Style. attribute.setUseFlatValue(getCodeStyles().useFlatVlaue); if (!attribute.getChildNodes().contains(bindingNode)) { attribute.getChildNodes().add(bindingNode); } if (!node.getAttributes().contains(attribute)) { commandList.add(new AddNewChildCommand(node, attribute)); } return commandList.unwrap(); }
public boolean canDelete() { return bindingNode != null && bindingNode.eContainer() != null; }
private static BindingInfo createBindingInfo( Object targetSource, String propertyName, XamlElement bindingNode) { BindingInfo bindingInfo = bindingCache.get(bindingNode); if (bindingInfo != null) { // TODO: To check if binding changed. return bindingInfo; } Observable target = ObservableUtil.getObservable(targetSource); Property targetProperty = target.getProperty(propertyName); String elementName = null; String modelPropertyName = null; String bindingMode = null; String updateTtrigger = null; String converter = null; XamlAttribute attribute = bindingNode.getAttribute(ELEMENT_NAME); if (attribute != null) { elementName = attribute.getValue(); } attribute = bindingNode.getAttribute(PATH); if (attribute != null) { modelPropertyName = attribute.getValue(); } attribute = bindingNode.getAttribute(MODE); if (attribute != null) { bindingMode = attribute.getValue(); } attribute = bindingNode.getAttribute(CONVERTER); if (attribute != null) { converter = attribute.getValue(); } attribute = bindingNode.getAttribute(UPDATE_SOURCE_TRIGGER); if (attribute != null) { updateTtrigger = attribute.getValue(); } Object modelSource; if (elementName != null) { modelSource = XWT.findElementByName(targetSource, elementName); } else { modelSource = getDataContext(targetSource); } IObservable model = ObservableUtil.getObservable(modelSource); if (model == null) { return null; } Property modelProperty = model.getProperty(modelPropertyName); bindingInfo = new BindingInfo(new BindingContext(target, targetProperty, model, modelProperty)); bindingInfo.setElementName(elementName); if (bindingMode != null) { bindingInfo.setBindingMode(BindingMode.valueOf(bindingMode)); } if (updateTtrigger != null) { bindingInfo.setTriggerMode(UpdateSourceTrigger.valueOf(updateTtrigger)); } bindingInfo.setConverter(converter); bindingInfo.setBindingNode(bindingNode); bindingCache.put(bindingNode, bindingInfo); return bindingInfo; }