Example usage for org.eclipse.jface.viewers IStructuredSelection getFirstElement

List of usage examples for org.eclipse.jface.viewers IStructuredSelection getFirstElement

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers IStructuredSelection getFirstElement.

Prototype

public Object getFirstElement();

Source Link

Document

Returns the first element in this selection, or null if the selection is empty.

Usage

From source file:com.amalto.workbench.actions.XSDNewIdentityConstraintAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {/*from w w w. ja va  2s.com*/

        int index = -1;
        // EList list = schema.getIdentityConstraintDefinitions();

        // schema.getElement();
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        List<String> childNames = new ArrayList<String>();
        if (selection.getFirstElement() instanceof XSDElementDeclaration) {
            decl = (XSDElementDeclaration) selection.getFirstElement();
            // childNames = Util.getChildElementNames(decl.getElement());
        }

        else if (selection.getFirstElement() instanceof XSDIdentityConstraintDefinition) {
            XSDIdentityConstraintDefinition selIcd = (XSDIdentityConstraintDefinition) selection
                    .getFirstElement();
            decl = (XSDElementDeclaration) (selIcd.getContainer());

            // get position of the selected Identity Constraint in the container (Element Declaration)
            int i = 0;
            for (Iterator iter = decl.getIdentityConstraintDefinitions().iterator(); iter.hasNext();) {
                XSDIdentityConstraintDefinition ic = (XSDIdentityConstraintDefinition) iter.next();
                if (ic.equals(selIcd)) {
                    index = i;
                    break;
                }
                i++;
            }

        } else if (selection.getFirstElement() instanceof XSDParticle) {
            XSDParticle selParticle = (XSDParticle) selection.getFirstElement();
            if (!(selParticle.getTerm() instanceof XSDElementDeclaration))
                return Status.CANCEL_STATUS;
            decl = (XSDElementDeclaration) selParticle.getTerm();
            // childNames.add(decl.getName());

        } else {
            MessageDialog.openError(this.page.getSite().getShell(), Messages._Error,
                    Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg,
                            selection.getFirstElement().getClass().getName()));
            return Status.CANCEL_STATUS;
        }
        childNames = Util.getChildElementNames("", decl); //$NON-NLS-1$
        // filter the non top level fields
        List<String> topChilds = new ArrayList<String>();
        for (String child : childNames) {
            if (child.indexOf('/') == -1) {
                topChilds.add(child);
            }
        }
        dialog = new IdentityConstraintInputDialog(decl, page.getSite().getShell(),
                Messages.XSDNewIdentityConstraintAction_AddANewKey, topChilds, decl.getName());
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        keyName = dialog.getKeyName();
        fieldName = dialog.getFieldName();
        type = dialog.getType();

        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();

        XSDIdentityConstraintDefinition icd = factory.createXSDIdentityConstraintDefinition();
        icd.setName(keyName);
        icd.setIdentityConstraintCategory(type);
        XSDXPathDefinition selector = factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        selector.setValue("."); //$NON-NLS-1$
        icd.setSelector(selector);
        XSDXPathDefinition field = factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        field.setValue("."); //$NON-NLS-1$
        // if complex content set name of first field
        if (fieldName == null || fieldName.trim().length() == 0) {
            if (decl.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                XSDComplexTypeContent ctc = ((XSDComplexTypeDefinition) decl.getTypeDefinition()).getContent();
                if (ctc instanceof XSDParticle) {
                    if (((XSDParticle) ctc).getTerm() instanceof XSDModelGroup) {
                        XSDModelGroup mg = (XSDModelGroup) ((XSDParticle) ctc).getTerm();
                        if (mg.getContents().size() > 0)
                            if (mg.getContents().get(0).getTerm() instanceof XSDElementDeclaration)
                                field.setValue(((XSDElementDeclaration) (mg.getContents().get(0).getTerm()))
                                        .getName());
                    }
                }
            }
        } else {
            field.setValue(fieldName);
        }
        icd.getFields().add(field);

        decl.getIdentityConstraintDefinitions().add(index + 1, icd);
        decl.updateElement();

        updateElementForAddedfield(icd, fieldName);

        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(icd), true);
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDNewIdentityConstraintAction_ErrorMsg2, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewParticleFromParticleAction.java

License:Open Source License

public IStatus doAction() {
    try {/*from   w w w .  j  a  v  a  2  s.  c  o  m*/
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        selParticle = (XSDParticle) selection.getFirstElement();

        if (!(selParticle.getContainer() instanceof XSDModelGroup))
            return Status.CANCEL_STATUS;
        ;

        XSDModelGroup group = (XSDModelGroup) selParticle.getContainer();
        // get position of the selected particle in the container
        int index = 0;
        int i = 0;
        for (Iterator<XSDParticle> iter = group.getContents().iterator(); iter.hasNext();) {
            XSDParticle p = (XSDParticle) iter.next();
            if (p.equals(selParticle)) {
                index = i;
                break;
            }
            i++;
        }

        EList<XSDElementDeclaration> eDecls = schema.getElementDeclarations();
        List<String> elementDeclarations = new ArrayList<String>();
        for (Iterator<XSDElementDeclaration> iter = eDecls.iterator(); iter.hasNext();) {
            XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
            if (d.getTargetNamespace() != null && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE))
                continue;
            elementDeclarations
                    .add(d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));//$NON-NLS-1$//$NON-NLS-2$
        }
        elementDeclarations.add("");//$NON-NLS-1$

        dialog = new BusinessElementInputDialog(this, page.getSite().getShell(),
                Messages._AddANewBusinessElement, null, null, elementDeclarations, 0, 1, true, false);
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        XSDElementDeclaration elem = (XSDElementDeclaration) selParticle.getContent();
        if (Util.changeElementTypeToSequence(elem, maxOccurs) == Status.CANCEL_STATUS) {
            return Status.CANCEL_STATUS;
        }

        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();

        XSDElementDeclaration decl = factory.createXSDElementDeclaration();
        decl.setName(this.elementName);
        if (!refName.equals("")) {//$NON-NLS-1$
            XSDElementDeclaration ref = Util.findReference(refName, schema);
            if (ref != null) {
                decl.setResolvedElementDeclaration(ref);
            }
        } else {
            decl.setTypeDefinition(
                    schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), simpleTypeName));
        }

        XSDParticle particle = factory.createXSDParticle();
        particle.setContent(decl);
        particle.setMinOccurs(this.minOccurs);
        if (maxOccurs > -1) {
            particle.setMaxOccurs(this.maxOccurs);
        } else {
            particle.setMaxOccurs(this.maxOccurs);
            group.getContents().add(group.getContents().size(), particle);
            group.updateElement();
            if (particle.getElement().getAttributeNode("maxOccurs") != null)//$NON-NLS-1$
                particle.getElement().getAttributeNode("maxOccurs").setNodeValue("unbounded");//$NON-NLS-1$//$NON-NLS-2$
            else {
                particle.getElement().setAttribute("maxOccurs", "unbounded");//$NON-NLS-1$//$NON-NLS-2$
            }
        }
        if (maxOccurs > -1) {
            group.getContents().add(group.getContents().size(), particle);
            group.updateElement();
        }

        // fix 0010248. add annotion from parent

        if (dialog.isInherit()) {
            XSDTerm totm = particle.getTerm();
            XSDElementDeclaration concept = null;
            if (Util.getParent(selParticle) instanceof XSDElementDeclaration)
                concept = (XSDElementDeclaration) Util.getParent(selParticle);
            else if (Util.getParent(selParticle) instanceof XSDComplexTypeDefinition) {
                if (selParticle instanceof XSDParticle)
                    concept = (XSDElementDeclaration) ((XSDParticle) selParticle).getContent();
                else if (selParticle instanceof XSDElementDeclaration)
                    concept = (XSDElementDeclaration) selParticle;
            }
            XSDAnnotation fromannotation = null;
            if (concept != null)
                fromannotation = concept.getAnnotation();
            if (fromannotation != null) {
                XSDAnnotationsStructure struc = new XSDAnnotationsStructure(totm);
                if (((XSDElementDeclaration) totm).getType() != null)
                    addAnnotion(struc, fromannotation);
            }

        }

        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(particle), true);
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages._ErrorCreatBusinessElement, e.getLocalizedMessage()));

        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewParticleFromTypeAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {//from   w ww  .  ja  v  a 2s  . c  om
        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) {
            ctd = (XSDComplexTypeDefinition) selection.getFirstElement();
            if (!(ctd.getContent() instanceof XSDParticle)) {
                return Status.CANCEL_STATUS;
            }
            if (!(((XSDParticle) ctd.getContent()).getTerm() instanceof XSDModelGroup)) {
                return Status.CANCEL_STATUS;
            }
            ;
            group = (XSDModelGroup) ((XSDParticle) ctd.getContent()).getTerm();
        } else if (selection.getFirstElement() instanceof XSDParticle) {
            group = (XSDModelGroup) ((XSDParticle) selection.getFirstElement()).getTerm();
        } else if (selection.getFirstElement() instanceof XSDModelGroup) {
            group = (XSDModelGroup) selection.getFirstElement();
        } else {
            log.info(Messages.bind(Messages._UnkownSection, selection.getFirstElement().getClass().getName(),
                    selection.getFirstElement().toString()));
            return Status.CANCEL_STATUS;
        }

        EList<XSDElementDeclaration> eDecls = schema.getElementDeclarations();
        List<String> elementDeclarations = new LinkedList<String>();
        for (XSDElementDeclaration xsdElementDeclaration : eDecls) {
            XSDElementDeclaration d = xsdElementDeclaration;
            if (d.getTargetNamespace() != null
                    && d.getTargetNamespace().equals(IConstants.DEFAULT_NAME_SPACE)) {
                continue;
            }
            elementDeclarations
                    .add(d.getQName() + (d.getTargetNamespace() != null ? " : " + d.getTargetNamespace() : ""));//$NON-NLS-1$//$NON-NLS-2$
        }
        elementDeclarations.add("");//$NON-NLS-1$

        dialog = new BusinessElementInputDialog(this, page.getSite().getShell(),
                Messages._AddANewBusinessElement, "", "", elementDeclarations, 0, 1, true, false);//$NON-NLS-1$//$NON-NLS-2$;
        dialog.setBlockOnOpen(true);
        int ret = dialog.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();

        XSDElementDeclaration decl = factory.createXSDElementDeclaration();
        decl.setName(this.elementName);
        // decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),
        // simpleTypeName));
        if (!refName.equals("")) {//$NON-NLS-1$
            XSDElementDeclaration ref = Util.findReference(refName, schema);
            if (ref != null) {
                decl.setResolvedElementDeclaration(ref);
            }
        } else {
            decl.setTypeDefinition(
                    schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(), simpleTypeName));
        }

        XSDParticle particle = factory.createXSDParticle();
        particle.setContent(decl);
        particle.setMinOccurs(this.minOccurs);
        particle.setMaxOccurs(this.maxOccurs);

        group.getContents().add(group.getContents().size(), particle);
        group.updateElement();

        if (Util.changeElementTypeToSequence(decl, maxOccurs) == Status.CANCEL_STATUS) {
            return Status.CANCEL_STATUS;
        }
        // fix 0010248. add annotion from parent

        if (dialog.isInherit()) {
            XSDTerm totm = particle.getTerm();
            XSDElementDeclaration concept = null;
            Object obj = Util.getParent(particle);
            if (obj instanceof XSDElementDeclaration) {
                concept = (XSDElementDeclaration) obj;
            } else {
                concept = (XSDElementDeclaration) particle.getContent();
            }
            XSDAnnotation fromannotation = null;
            if (concept != null) {
                fromannotation = concept.getAnnotation();
            }
            if (fromannotation != null) {
                XSDAnnotationsStructure struc = new XSDAnnotationsStructure(totm);
                if (((XSDElementDeclaration) totm).getType() != null) {
                    addAnnotion(struc, fromannotation);
                }
            }

        }

        page.refresh();
        page.getTreeViewer().setSelection(new StructuredSelection(particle), true);
        page.markDirty();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages._ErrorCreatBusinessElement, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDNewXPathAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {/*ww w  . j  a v a 2  s  . c o m*/
        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;
        // };
        // }
        // );

        List<String> childNames = Util.getChildElementNames("", (XSDElementDeclaration) icd.getContainer()); //$NON-NLS-1$
        // 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;
}

From source file:com.amalto.workbench.actions.XSDPasteConceptAction.java

License:Open Source License

public void copyElements() {
    ArrayList<XSDParticle> particles = WorkbenchClipboard.getWorkbenchClipboard().getParticles();
    IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
    XSDComplexTypeContent content = null;
    XSDElementDeclaration element = null;
    if (selection.getFirstElement() instanceof XSDElementDeclaration) {
        element = (XSDElementDeclaration) selection.getFirstElement();
        content = ((XSDComplexTypeDefinition) element.getTypeDefinition()).getContent();
    } else if (selection.getFirstElement() instanceof XSDComplexTypeDefinition) {
        content = ((XSDComplexTypeDefinition) selection.getFirstElement()).getContent();
    } else if (selection.getFirstElement() instanceof XSDModelGroup) {
        XSDComplexTypeDefinition complexType = (XSDComplexTypeDefinition) ((XSDModelGroup) selection
                .getFirstElement()).getContainer().getContainer();
        content = complexType.getContent();
    } else {/*from  w  w w  .j  a v a 2 s . c o  m*/
        if (selection.getFirstElement() instanceof XSDParticle) {
            XSDParticle particle = (XSDParticle) selection.getFirstElement();
            XSDElementDeclaration declar = (XSDElementDeclaration) particle.getTerm();
            if (declar.getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                XSDComplexTypeDefinition typeDefinition = (XSDComplexTypeDefinition) declar.getTypeDefinition();
                content = typeDefinition.getContent();
            }
        }
    }

    if (content instanceof XSDParticle) {
        XSDParticle partile = (XSDParticle) content;
        if (partile.getTerm() instanceof XSDModelGroup) {
            XSDModelGroup toGroup = ((XSDModelGroup) partile.getTerm());
            for (XSDParticle particle : particles) {
                // if the is particle with the same name, donot copy it.
                if (isExist(toGroup, particle)) {
                    boolean ifOverwrite = MessageDialog.openConfirm(this.page.getSite().getShell(),
                            Messages.XSDPasteConceptAction_Confirm,
                            Messages.bind(Messages.XSDPasteConceptAction_ErrorMsg3,
                                    ((XSDElementDeclaration) particle.getTerm()).getName()));
                    if (ifOverwrite) {
                        reomveElement(toGroup, particle);
                    } else {
                        continue;
                    }
                }

                XSDParticle newParticle = (XSDParticle) particle.cloneConcreteComponent(true, false);
                if (newParticle.getContent() instanceof XSDElementDeclaration
                        && Util.changeElementTypeToSequence(element,
                                newParticle.getMaxOccurs()) == Status.CANCEL_STATUS) {
                    break;
                }
                toGroup.getContents().add(newParticle);
                toGroup.updateElement();

                if (newParticle.getContent() instanceof XSDElementDeclaration) {
                    if (((XSDElementDeclaration) newParticle.getContent())
                            .getTypeDefinition() instanceof XSDComplexTypeDefinition) {
                        addAnnotationForComplexType(
                                (XSDComplexTypeDefinition) ((XSDElementDeclaration) particle.getContent())
                                        .getTypeDefinition(),
                                (XSDComplexTypeDefinition) ((XSDElementDeclaration) newParticle.getContent())
                                        .getTypeDefinition());
                    }

                    XSDAnnotationsStructure struc1 = new XSDAnnotationsStructure(newParticle.getTerm());
                    addAnnotion(struc1, ((XSDElementDeclaration) particle.getTerm()).getAnnotation());

                    Util.changeElementTypeToSequence((XSDElementDeclaration) newParticle.getContent(),
                            newParticle.getMaxOccurs());
                }

            }
        }
    }
}

From source file:com.amalto.workbench.actions.XSDSetAnnotaionDisplayFormatAction.java

License:Open Source License

public IStatus doAction() {
    try {/*from  www.  j  a v  a 2  s.c  o m*/
        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation)
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
            }
        } else
            xSDCom = (XSDComponent) selection.getFirstElement();
        XSDAnnotationsStructure struc = new XSDAnnotationsStructure(xSDCom);
        // IStructuredSelection selection = (IStructuredSelection)page.getTreeViewer().getSelection();
        // XSDAnnotationsStructure struc = new XSDAnnotationsStructure((XSDComponent)selection.getFirstElement());
        if (struc.getAnnotation() == null) {
            throw new RuntimeException(
                    Messages.XSDSetAnnoXX_ExceptionInfo + selection.getFirstElement().getClass().getName());
        }

        dlg = new AnnotationLanguageLabelsDialog(struc.getDisplayFormat(), new SelectionListener() {

            public void widgetDefaultSelected(SelectionEvent e) {
            }

            public void widgetSelected(SelectionEvent e) {
                dlg.close();
            }
        }, page.getSite().getShell(), Messages.XSDSetAnnoXX_DialogTitle2);

        dlg.setBlockOnOpen(true);
        int ret = dlg.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        LinkedHashMap<String, String> fomats = dlg.getDescriptionsMap();
        struc.setDisplayFormat(fomats);

        if (struc.hasChanged()) {
            page.refresh();
            page.getTreeViewer().expandToLevel(selection.getFirstElement(), 2);
            page.markDirty();
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDSetAnnoXX_ErrorMsg2, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDSetAnnotationDescriptionsAction.java

License:Open Source License

public IStatus doAction() {
    try {/*from  www.j  a v  a2  s. c  o  m*/

        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation)
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
            }
        } else
            xSDCom = (XSDComponent) selection.getFirstElement();
        XSDAnnotationsStructure struc = new XSDAnnotationsStructure(xSDCom);
        if (struc.getAnnotation() == null) {
            throw new RuntimeException(
                    Messages.bind(Messages.XSDSetAnnotationXX_ExceptionInfo2, xSDCom.getClass().getName()));
        }

        AnnotationLanguageLabelsDialog dlg = new AnnotationLanguageLabelsDialog(struc.getDescriptions(),
                new AnnotationLabelDialogSelectionListener(page), page.getEditorSite().getShell(),
                Messages.XSDSetAnnotationXX_SetDescOfThisItem);
        dlg.setBlockOnOpen(true);
        dlg.open();

        if (dlg.getReturnCode() == Window.OK) {
            // remove existing annotations with labels
            struc.removeAllDescriptions();
            // add the new ones
            LinkedHashMap<String, String> descriptions = dlg.getDescriptionsMap();
            Set<String> isoCodes = descriptions.keySet();
            for (Iterator iter = isoCodes.iterator(); iter.hasNext();) {
                String isoCode = (String) iter.next();
                struc.setDescription(isoCode, descriptions.get(isoCode));
            }
        } else {
            return Status.CANCEL_STATUS;
        }

        if (struc.hasChanged()) {
            page.markDirty();
            page.refresh();
            page.getTreeViewer().expandToLevel(xSDCom, 2);
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDSetAnnotationXX_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDSetAnnotationDocumentationAction.java

License:Open Source License

public IStatus doAction() {
    try {/*from  w w  w  . j a v a2 s . c o  m*/

        IStructuredSelection selection = (IStructuredSelection) page.getTreeViewer().getSelection();
        XSDAnnotationsStructure struc = new XSDAnnotationsStructure((XSDComponent) selection.getFirstElement());
        if (struc.getAnnotation() == null) {
            throw new RuntimeException(Messages.bind(Messages.XSDSetXX_ExceptionInfo,
                    selection.getFirstElement().getClass().getName()));
        }

        InputDialog id = new InputDialog(page.getSite().getShell(), Messages.XSDSetXX_DialogTitle,
                Messages.XSDSetXX_DialogTip, struc.getDocumentation(), new IInputValidator() {

                    public String isValid(String newText) {
                        return null;
                    };
                });

        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        struc.setDocumentation("".equals(id.getValue()) ? null : id.getValue());//$NON-NLS-1$

        if (struc.hasChanged()) {
            page.refresh();
            page.getTreeViewer().expandToLevel(selection.getFirstElement(), 2);
            page.markDirty();
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.XSDSetXX_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDSetAnnotationFKFilterAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {//from w  w w  . j av  a2  s . c o m
        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        String conceptName = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation) {
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
                }
            }
        } else {
            xSDCom = (XSDComponent) selection.getFirstElement();
        }
        if (xSDCom instanceof XSDElementDeclaration) {
            conceptName = xSDCom.getElement().getAttributes().getNamedItem("name").getNodeValue();//$NON-NLS-1$
        }
        if (xSDCom instanceof XSDParticle) {
        }
        XSDAnnotationsStructure struc = null;
        if (xSDCom != null) {
            struc = new XSDAnnotationsStructure(xSDCom);
        }
        if (struc == null || struc.getAnnotation() == null) {
            throw new RuntimeException(
                    Messages.bind(Messages.UnableEditAnnotationType, xSDCom.getClass().getName()));
        }

        fkd = getNewFKFilterDialog(page.getSite().getShell(), struc.getFKFilter(), page, conceptName);
        fkd.setDataModel(dataModelName);
        fkd.setLock(true);
        fkd.setBlockOnOpen(true);
        int ret = fkd.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        String fkfilter = fkd.getFilter();
        struc.setFKFilter(fkfilter);

        if (struc.hasChanged()) {
            page.refresh();
            page.getTreeViewer().expandToLevel(xSDCom, 2);
            page.markDirty();
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.ErrorFKFilter, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}

From source file:com.amalto.workbench.actions.XSDSetAnnotationForeignKeyAction.java

License:Open Source License

@Override
public IStatus doAction() {
    try {// w  w  w .j a v a2s.  co m
        IStructuredSelection selection = (TreeSelection) page.getTreeViewer().getSelection();
        XSDComponent xSDCom = null;
        if (selection.getFirstElement() instanceof Element) {
            TreePath tPath = ((TreeSelection) selection).getPaths()[0];
            for (int i = 0; i < tPath.getSegmentCount(); i++) {
                if (tPath.getSegment(i) instanceof XSDAnnotation) {
                    xSDCom = (XSDAnnotation) (tPath.getSegment(i));
                }
            }
        } else {
            xSDCom = (XSDComponent) selection.getFirstElement();
        }
        XSDAnnotationsStructure struc = null;
        if (xSDCom != null) {
            struc = new XSDAnnotationsStructure(xSDCom);
        }
        if (struc == null || struc.getAnnotation() == null) {
            throw new RuntimeException(Messages.bind(Messages.UnableEditType, xSDCom.getClass().getName()));
        }

        sxid = getNewSimpleXpathInputDlg(struc.getForeignKey());
        sxid.setLock(true);
        sxid.setPKXpaths(XSDUtil.getAllPKXpaths(schema));
        String fksep = struc.getForeignKeyNotSep();
        if (fksep != null) {
            sxid.setFkSep(Boolean.valueOf(fksep));
        }
        sxid.setBlockOnOpen(true);
        int ret = sxid.open();
        if (ret == Window.CANCEL) {
            return Status.CANCEL_STATUS;
        }

        String fk = "".equals(sxid.getXpath()) ? null : sxid.getXpath().replaceAll("'|\"", "");//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        // keep the foreignkey in memory to improve performance
        if (Util.getForeignKeys() != null && fk != null) {
            if (struc.getForeignKey() != null) {
                Util.getForeignKeys().remove(Util.getConceptFromPath(struc.getForeignKey()));
            }
            Util.getForeignKeys().add(Util.getConceptFromPath(fk));
        }
        struc.setForeignKey(fk);
        Boolean sep = sxid.getSepFk();
        struc.setForeignKeyNotSep(sep);
        updateAnnotationStructure(struc);
        if (struc.hasChanged()) {
            page.refresh();
            page.getTreeViewer().expandToLevel(xSDCom, 2);
            page.markDirty();
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error,
                Messages.bind(Messages.ErrorForeignKey, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}