Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

In this page you can find the example usage for org.dom4j Element addAttribute.

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.haulmont.cuba.gui.components.filter.condition.FtsCondition.java

License:Apache License

@Override
public void toXml(Element element, Param.ValueProperty valueProperty) {
    super.toXml(element, valueProperty);
    element.addAttribute("type", ConditionType.FTS.name());
    element.addAttribute("queryKeyParamName", queryKeyParamName);
    element.addAttribute("sessionIdParamName", sessionIdParamName);
}

From source file:com.haulmont.cuba.gui.components.filter.condition.PropertyCondition.java

License:Apache License

@Override
public void toXml(Element element, Param.ValueProperty valueProperty) {
    super.toXml(element, valueProperty);
    element.addAttribute("type", ConditionType.PROPERTY.name());
    element.addAttribute("operatorType", getOperatorType());
}

From source file:com.haulmont.cuba.gui.components.filter.FilterDelegateImpl.java

License:Apache License

@Override
public boolean saveSettings(Element element) {
    Boolean changed = false;//ww  w  .  j a va2  s . com
    Element e = element.element("defaultFilter");
    if (e == null)
        e = element.addElement("defaultFilter");

    UUID defaultId = null;
    Boolean applyDefault = false;

    for (FilterEntity filter : filterEntities) {
        if (BooleanUtils.isTrue(filter.getIsDefault())) {
            defaultId = filter.getId();
            applyDefault = filter.getApplyDefault();
            break;
        }
    }

    String newDef = defaultId != null ? defaultId.toString() : null;
    Attribute attr = e.attribute("id");
    String oldDef = attr != null ? attr.getValue() : null;
    if (!Objects.equals(oldDef, newDef)) {
        if (newDef == null && attr != null) {
            e.remove(attr);
        } else {
            if (attr == null)
                e.addAttribute("id", newDef);
            else
                attr.setValue(newDef);
        }
        changed = true;
    }
    Boolean newApplyDef = BooleanUtils.isTrue(applyDefault);
    Attribute applyDefaultAttr = e.attribute("applyDefault");
    Boolean oldApplyDef = applyDefaultAttr != null ? Boolean.valueOf(applyDefaultAttr.getValue()) : false;
    if (!Objects.equals(oldApplyDef, newApplyDef)) {
        if (applyDefaultAttr != null) {
            applyDefaultAttr.setValue(newApplyDef.toString());
        } else {
            e.addAttribute("applyDefault", newApplyDef.toString());
        }
        changed = true;
    }

    Element groupBoxExpandedEl = element.element("groupBoxExpanded");
    if (groupBoxExpandedEl == null)
        groupBoxExpandedEl = element.addElement("groupBoxExpanded");

    Boolean oldGroupBoxExpandedValue = Boolean.valueOf(groupBoxExpandedEl.getText());
    Boolean newGroupBoxExpandedValue = groupBoxLayout.isExpanded();
    if (!Objects.equals(oldGroupBoxExpandedValue, newGroupBoxExpandedValue)) {
        groupBoxExpandedEl.setText(newGroupBoxExpandedValue.toString());
        changed = true;
    }

    return changed;
}

From source file:com.haulmont.cuba.gui.components.filter.Param.java

License:Apache License

public void toXml(Element element, ValueProperty valueProperty) {
    Element paramElem = element.addElement("param");
    paramElem.addAttribute("name", getName());
    paramElem.addAttribute("javaClass", getJavaClass().getName());
    if (runtimeEnum != null) {
        paramElem.addAttribute("categoryAttrId", categoryAttrId.toString());
    }/*from w  w w .j  a v  a2  s. c  o  m*/
    if (isDateInterval) {
        paramElem.addAttribute("isDateInterval", "true");
    }

    paramElem.setText(formatValue(_getValue(valueProperty)));
}

From source file:com.haulmont.cuba.gui.components.filter.UserSetHelper.java

License:Apache License

public static String generateSetFilter(Set ids, String entityClass, String componentId, String entityAlias) {
    Document document = DocumentHelper.createDocument();
    Element root = DocumentHelper.createElement("filter");
    Element or = root.addElement("and");

    Element condition = or.addElement("c");
    condition.addAttribute("name", "set");
    condition.addAttribute("inExpr", "true");
    condition.addAttribute("hidden", "true");
    condition.addAttribute("locCaption", "Set filter");
    condition.addAttribute("entityAlias", entityAlias);
    condition.addAttribute("class", entityClass);
    condition.addAttribute("type", ConditionType.CUSTOM.name());

    String listOfId = createIdsString(ids);
    String randomName = RandomStringUtils.randomAlphabetic(10);
    condition.addText(entityAlias + ".id in :component$" + componentId + "." + randomName);

    Element param = condition.addElement("param");
    param.addAttribute("name", "component$" + componentId + "." + randomName);
    param.addText(listOfId);//  w  w w .ja v a  2  s.c  om

    document.add(root);
    return Dom4j.writeDocument(document, true);
}

From source file:com.haulmont.cuba.gui.components.WindowDelegate.java

License:Apache License

public void saveSettings() {
    if (settings != null) {
        ComponentsHelper.walkComponents(window, (component, name) -> {
            if (component.getId() != null && component instanceof Component.HasSettings) {
                log.trace("Saving settings for : {} : {}", name, component);

                Element e = WindowDelegate.this.settings.get(name);
                boolean modified = ((Component.HasSettings) component).saveSettings(e);

                if (component instanceof Component.HasPresentations
                        && ((Component.HasPresentations) component).isUsePresentations()) {
                    Object def = ((Component.HasPresentations) component).getDefaultPresentationId();
                    e.addAttribute("presentation", def != null ? def.toString() : "");
                    Presentations presentations = ((Component.HasPresentations) component).getPresentations();
                    if (presentations != null) {
                        presentations.commit();
                    }/*  ww w. j  a va2s.  c om*/
                }
                WindowDelegate.this.settings.setModified(modified);
            }
        });
        settings.commit();
    }
}

From source file:com.haulmont.cuba.gui.dynamicattributes.FilteringLookupAction.java

License:Apache License

protected Element createConditionXmlElement() {
    Element conditionElement = DocumentHelper.createDocument().addElement("c");
    conditionElement.addAttribute("name", RandomStringUtils.randomAlphabetic(10));
    conditionElement.addAttribute("width", "1");
    conditionElement.addAttribute("type", "CUSTOM");
    conditionElement.addAttribute("locCaption",
            messages.getMainMessage("dynamicAttributes.filter.conditionName"));
    return conditionElement;
}

From source file:com.haulmont.cuba.gui.relatedentities.RelatedEntitiesBean.java

License:Apache License

protected Element getConditionXmlElement(String conditionName, MetaClass metaClass) {
    Element conditionElement = DocumentHelper.createDocument().addElement("c");
    conditionElement.addAttribute("name", conditionName);
    conditionElement.addAttribute("width", "1");
    conditionElement.addAttribute("type", "CUSTOM");
    String conditionCaption = String.format("%s ids", metaClass.getName().split("\\$")[1]);
    // condition will be hidden so we don't have to load localized condition caption
    conditionElement.addAttribute("locCaption", conditionCaption);
    return conditionElement;
}

From source file:com.haulmont.cuba.gui.settings.SettingsImpl.java

License:Apache License

@Override
public Element get(final String componentId) {
    checkLoaded();/*from w  w w .  j a  v  a 2 s  . co  m*/
    Element componentsRoot = root.element("components");
    if (componentsRoot == null) {
        componentsRoot = root.addElement("components");
    }
    for (Element e : ((List<Element>) componentsRoot.elements())) {
        if (componentId.equals(e.attributeValue("name"))) {
            return e;
        }
    }
    Element e = componentsRoot.addElement("component");
    e.addAttribute("name", componentId);
    return e;
}

From source file:com.haulmont.cuba.gui.xml.XmlInheritanceProcessor.java

License:Apache License

private void process(Element resultElem, Element extElem) {
    // set text/*from w  ww .ja  v a 2  s.co  m*/
    if (!StringUtils.isBlank(extElem.getText()))
        resultElem.setText(extElem.getText());

    // add all attributes from extension
    for (Attribute attribute : Dom4j.attributes(extElem)) {
        if (resultElem == document.getRootElement() && attribute.getName().equals("extends")) {
            // ignore "extends" in root element
            continue;
        }
        resultElem.addAttribute(attribute.getName(), attribute.getValue());
    }

    String idx = extElem.attributeValue(new QName("index", extNs));
    if (resultElem != document.getRootElement() && StringUtils.isNotBlank(idx)) {
        int index = Integer.parseInt(idx);

        Element parent = resultElem.getParent();
        if (index < 0 || index > parent.elements().size()) {
            String message = String.format(
                    "Incorrect extension XML for screen. Could not move existing element %s to position %s",
                    resultElem.getName(), index);

            throw new DevelopmentException(message,
                    ParamsMap.of("element", resultElem.getName(), "index", index));
        }

        parent.remove(resultElem);
        //noinspection unchecked
        parent.elements().add(index, resultElem);
    }

    // add and process elements
    Set<Element> justAdded = new HashSet<>();
    for (Element element : Dom4j.elements(extElem)) {
        // look for suitable locator
        ElementTargetLocator locator = null;
        for (ElementTargetLocator l : targetLocators) {
            if (l.suitableFor(element)) {
                locator = l;
                break;
            }
        }
        if (locator != null) {
            Element target = locator.locate(resultElem, element);
            // process target or a new element if target not found
            if (target != null) {
                process(target, element);
            } else {
                addNewElement(resultElem, element, justAdded);
            }
        } else {
            // if no suitable locator found, look for a single element with the same name
            List<Element> list = Dom4j.elements(resultElem, element.getName());
            if (list.size() == 1 && !justAdded.contains(list.get(0))) {
                process(list.get(0), element);
            } else {
                addNewElement(resultElem, element, justAdded);
            }
        }
    }
}