Search in sources :

Example 1 with SelectFieldDialog

use of com.amalto.workbench.dialogs.SelectFieldDialog in project tmdm-studio-se by Talend.

the class XSDEditXPathAction method doAction.

public IStatus doAction() {
    try {
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        XSDXPathDefinition xpath = (XSDXPathDefinition) selection.getFirstElement();
        icd = (XSDIdentityConstraintDefinition) xpath.getContainer();
        // InputDialog id = new InputDialog(
        // page.getSite().getShell(),
        // "Edit XPath",
        // "Enter a new XPath for the "+((xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))?"field":"selector"),
        // xpath.getValue(),
        // new IInputValidator() {
        // public String isValid(String newText) {
        // if ((newText==null) || "".equals(newText)) return "The XPath cannot be empty";
        // return null;
        // };
        // }
        // );
        // List<String> childNames = Util.getChildElementNames("", (XSDElementDeclaration) icd.getContainer());
        List<String> childNames = new ArrayList<String>();
        // $NON-NLS-1$
        childNames.add(".");
        SelectFieldDialog id = new SelectFieldDialog(page.getSite().getShell(), Messages.XSDEditXPathAction_DialogTitle, childNames, xpath.getValue());
        id.create();
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        String field = id.getField();
        if (field.length() == 0)
            return Status.CANCEL_STATUS;
        XSDXPathDefinition newXpath = XSDSchemaBuildingTools.getXSDFactory().createXSDXPathDefinition();
        newXpath.setValue(field);
        if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL)) {
            int index = icd.getFields().indexOf(xpath);
            newXpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
            icd.getFields().set(index, newXpath);
        } else {
            newXpath.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
            icd.setSelector(newXpath);
        }
        icd.updateElement();
        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(newXpath), true);
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditXPathAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : SelectFieldDialog(com.amalto.workbench.dialogs.SelectFieldDialog) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition)

Example 2 with SelectFieldDialog

use of com.amalto.workbench.dialogs.SelectFieldDialog in project tmdm-studio-se by Talend.

the class XSDNewXPathAction method doAction.

@Override
public IStatus doAction() {
    try {
        int index = 0;
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
            icd = (XSDIdentityConstraintDefinition) selection.getFirstElement();
        } else if (selection.getFirstElement() instanceof XSDXPathDefinition) {
            XSDXPathDefinition xpath = (XSDXPathDefinition) selection.getFirstElement();
            icd = (XSDIdentityConstraintDefinition) xpath.getContainer();
            if (xpath.getVariety().equals(XSDXPathVariety.FIELD_LITERAL))
                index = icd.getFields().indexOf(xpath) + 1;
            else
                index = 0;
        } else {
            MessageDialog.openError(this.page.getSite().getShell(), Messages._Error, Messages.XSDNewXPathAction_Huhhh + selection.getFirstElement().getClass().getName());
            return Status.CANCEL_STATUS;
        }
        // InputDialog id = new InputDialog(
        // page.getSite().getShell(),
        // "New XPath",
        // "Enter a new XPath to the field",
        // null,
        // new IInputValidator() {
        // public String isValid(String newText) {
        // if ((newText==null) || "".equals(newText)) return "The XPath cannot be empty";
        // return null;
        // };
        // }
        // );
        // $NON-NLS-1$
        List<String> childNames = Util.getChildElementNames("", (XSDElementDeclaration) icd.getContainer());
        // filter the non top level fields
        List<String> topChilds = new ArrayList<String>();
        for (String child : childNames) {
            if (child.indexOf('/') == -1) {
                topChilds.add(child);
            }
        }
        // forbid to add already exists field
        EList<XSDXPathDefinition> fields = icd.getFields();
        for (XSDXPathDefinition fd : fields) {
            if (topChilds.contains(fd.getValue()))
                topChilds.remove(fd.getValue());
        }
        SelectFieldDialog id = new SelectFieldDialog(page.getSite().getShell(), Messages.XSDNewXPathAction_SelectOnField, topChilds, null);
        id.create();
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        String field = id.getField();
        if (field.length() == 0)
            return Status.CANCEL_STATUS;
        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
        XSDXPathDefinition xpath = factory.createXSDXPathDefinition();
        xpath.setValue(field);
        xpath.setVariety(XSDXPathVariety.FIELD_LITERAL);
        icd.getFields().add(index, xpath);
        icd.updateElement();
        updateElementForAddedfield(icd, field);
        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(xpath), true);
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) SelectFieldDialog(com.amalto.workbench.dialogs.SelectFieldDialog) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition)

Aggregations

SelectFieldDialog (com.amalto.workbench.dialogs.SelectFieldDialog)2 ArrayList (java.util.ArrayList)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)2 XSDFactory (org.eclipse.xsd.XSDFactory)1 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)1