Search in sources :

Example 26 with IStructuredContentProvider

use of org.eclipse.jface.viewers.IStructuredContentProvider in project tmdm-studio-se by Talend.

the class XSDEditComplexTypeAction method doAction.

@Override
public IStatus doAction() {
    try {
        ISelection selection = page.getTreeViewer().getSelection();
        IStructuredContentProvider provider = (IStructuredContentProvider) page.getSchemaContentProvider();
        XSDComplexTypeDefinition decl = (XSDComplexTypeDefinition) ((IStructuredSelection) selection).getFirstElement();
        String oldName = decl.getName();
        InputDialog id = new InputDialog(page.getSite().getShell(), Messages.XSDEditComplexTypeAction_EditComplexType, Messages.XSDEditComplexTypeAction_EnterNameForEntity, oldName, new IInputValidator() {

            public String isValid(String newText) {
                if (// $NON-NLS-1$
                (newText == null) || "".equals(newText))
                    return Messages.XSDEditComplexTypeAction_ComplexTypeCannotBeEmpty;
                if (// $NON-NLS-1$
                Pattern.compile("^\\s+\\w+\\s*").matcher(newText).matches() || // $NON-NLS-1$//$NON-NLS-2$
                newText.trim().replaceAll("\\s", "").length() != newText.trim().length())
                    return Messages.XSDEditComplexTypeAction_NameCannotContainEmpty;
                if (!XSDUtil.isValidatedXSDName(newText)) {
                    return Messages.InvalidName_Message;
                }
                EList list = schema.getTypeDefinitions();
                for (Iterator iter = list.iterator(); iter.hasNext(); ) {
                    Object d = iter.next();
                    if (d instanceof XSDComplexTypeDefinition) {
                        XSDComplexTypeDefinition type = (XSDComplexTypeDefinition) d;
                        if (type.getName().equals(newText.trim()))
                            return Messages.XSDEditComplexTypeAction_ComplexAlreadyExists;
                    }
                }
                return null;
            }
        });
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        decl.setName(id.getValue().trim());
        Util.updateReferenceToXSDTypeDefinition(page.getSite(), decl, provider);
        page.refresh();
        page.markDirty();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.XSDEditComplexTypeAction_ErrorEditEntity + e.getLocalizedMessage());
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : InputDialog(org.eclipse.jface.dialogs.InputDialog) EList(org.eclipse.emf.common.util.EList) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) Iterator(java.util.Iterator) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) IInputValidator(org.eclipse.jface.dialogs.IInputValidator)

Example 27 with IStructuredContentProvider

use of org.eclipse.jface.viewers.IStructuredContentProvider in project tmdm-studio-se by Talend.

the class XSDEditConceptAction method doAction.

@Override
public IStatus doAction() {
    try {
        ISelection selection = page.getTreeViewer().getSelection();
        XSDElementDeclaration decl = (XSDElementDeclaration) ((IStructuredSelection) selection).getFirstElement();
        ArrayList<Object> objList = new ArrayList<Object>();
        IStructuredContentProvider provider = (IStructuredContentProvider) page.getTreeViewer().getContentProvider();
        String oldName = decl.getName();
        InputDialog id = new InputDialog(page.getSite().getShell(), Messages.XSDEditConceptAction_Text, Messages.XSDEditConceptAction_DialogTip, oldName, new IInputValidator() {

            public String isValid(String newText) {
                if ((newText == null) || "".equals(newText)) {
                    return Messages.XSDEditConceptAction_NameCannotBeEmpty;
                }
                if (// $NON-NLS-1$
                Pattern.compile("^\\s+\\w+\\s*").matcher(newText).matches() || newText.trim().replaceAll("\\s", "").length() != newText.trim().length()) {
                    return Messages.XSDEditConceptAction_NameCannotContainEmpty;
                }
                if (!XSDUtil.isValidatedXSDName(newText)) {
                    return Messages.InvalidName_Message;
                }
                EList list = schema.getElementDeclarations();
                for (Iterator iter = list.iterator(); iter.hasNext(); ) {
                    XSDElementDeclaration d = (XSDElementDeclaration) iter.next();
                    if (d.getName().equalsIgnoreCase(newText.trim())) {
                        return Messages.XSDEditConceptAction_EntityAlreadyExist;
                    }
                }
                return null;
            }
        });
        id.setBlockOnOpen(true);
        int ret = id.open();
        if (ret == Dialog.CANCEL) {
            return Status.CANCEL_STATUS;
        }
        Object[] objs = Util.getAllObject(page.getSite(), objList, provider);
        Object[] allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(page.getSite(), new ArrayList<Object>(), provider, new HashSet<Object>());
        String newName = id.getValue().trim();
        decl.setName(newName);
        decl.updateElement();
        Util.updateReference(decl, objs, allForeignKeyRelatedInfos, oldName, newName);
        EntitySyncProcessor.syncNameForAnnotation(decl, oldName, newName);
        if (mapinfoExAdapter != null) {
            mapinfoExAdapter.renameEntityMapinfo(oldName, newName);
        }
        if (elementExAdapter != null) {
            elementExAdapter.renameEntityName(decl.getSchema(), oldName, newName);
        }
        // change unique key with new name of concept
        EList list = decl.getIdentityConstraintDefinitions();
        XSDIdentityConstraintDefinition toUpdate = null;
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            XSDIdentityConstraintDefinition icd = (XSDIdentityConstraintDefinition) iter.next();
            if (icd.getName().equals(oldName)) {
                toUpdate = icd;
                break;
            }
        }
        if (toUpdate != null) {
            toUpdate.setName(newName);
            toUpdate.updateElement();
        }
        page.refresh();
        page.markDirty();
    // page.refreshPage();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        MessageDialog.openError(page.getSite().getShell(), Messages._Error, Messages.bind(Messages.XSDEditConceptAction_ErrorMsg, e.getLocalizedMessage()));
        return Status.CANCEL_STATUS;
    }
    return Status.OK_STATUS;
}
Also used : InputDialog(org.eclipse.jface.dialogs.InputDialog) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) Iterator(java.util.Iterator) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) IInputValidator(org.eclipse.jface.dialogs.IInputValidator)

Example 28 with IStructuredContentProvider

use of org.eclipse.jface.viewers.IStructuredContentProvider in project tmdm-studio-se by Talend.

the class UtilMockTest method testGetAllForeignKeyRelatedInfos.

@Test
public void testGetAllForeignKeyRelatedInfos() {
    // $NON-NLS-1$
    String localName = "appinfo";
    // $NON-NLS-1$
    String attr_key = "source";
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String[] fkRelatedInfo = { "X_ForeignKey", "X_ForeignKeyInfo", "X_ForeignKey_Filter" };
    // $NON-NLS-1$
    String namespaceURI = "http://www.w3.org/XML/1998/namespace";
    Object elem1 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
    List<Object> objList = new ArrayList<Object>();
    Set<Object> visited = new HashSet<Object>();
    IStructuredContentProvider provider = mock(IStructuredContentProvider.class);
    Object[] allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(null, new ArrayList<Object>(), provider, visited);
    assertNull(allForeignKeyRelatedInfos);
    allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(new Object(), null, provider, visited);
    assertNull(allForeignKeyRelatedInfos);
    allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(new Object(), new ArrayList<Object>(), null, visited);
    assertNull(allForeignKeyRelatedInfos);
    allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(new Object(), new ArrayList<Object>(), provider, null);
    assertNull(allForeignKeyRelatedInfos);
    allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(new Object(), new ArrayList<Object>(), provider, visited);
    assertNotNull(allForeignKeyRelatedInfos);
    assertTrue(allForeignKeyRelatedInfos.length == 0);
    try {
        Document doc = getEmptyDocument();
        Object[] elems1 = new Object[4];
        for (int i = 0; i < elems1.length - 1; i++) {
            Element fkRelatedElement = doc.createElementNS(namespaceURI, localName);
            fkRelatedElement.setAttribute(attr_key, fkRelatedInfo[i]);
            elems1[i] = fkRelatedElement;
        }
        Object elem2 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        elems1[3] = elem2;
        Object[] elems2 = new Object[3];
        for (int i = 0; i < elems2.length; i++) {
            Element fkRelatedElement = doc.createElementNS(namespaceURI, localName);
            fkRelatedElement.setAttribute(attr_key, fkRelatedInfo[i]);
            elems2[i] = fkRelatedElement;
        }
        when(provider.getElements(eq(elem1))).thenReturn(elems1);
        when(provider.getElements(eq(elem2))).thenReturn(elems2);
        allForeignKeyRelatedInfos = Util.getAllForeignKeyRelatedInfos(elem1, objList, provider, visited);
        assertNotNull(allForeignKeyRelatedInfos);
        assertEquals(6, allForeignKeyRelatedInfos.length);
        assertArrayEquals(objList.toArray(), allForeignKeyRelatedInfos);
        for (int j = 0; j < elems1.length - 1; j++) {
            assertTrue(objList.contains(elems1[j]));
        }
        for (int j = 0; j < elems2.length; j++) {
            assertTrue(objList.contains(elems1[j]));
        }
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 29 with IStructuredContentProvider

use of org.eclipse.jface.viewers.IStructuredContentProvider in project jbosstools-hibernate by jbosstools.

the class AddPropertyDialog method initDefaultNames.

private void initDefaultNames(ExporterFactory ef2, ComboViewer viewer) {
    viewer.setContentProvider(new IStructuredContentProvider() {

        ExporterFactory localEf;

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            localEf = (ExporterFactory) newInput;
        }

        public void dispose() {
            localEf = null;
        }

        public Object[] getElements(Object inputElement) {
            Iterator<Map.Entry<String, ExporterProperty>> set = localEf.getDefaultExporterProperties().entrySet().iterator();
            List<ExporterProperty> values = new ArrayList<ExporterProperty>(4);
            while (set.hasNext()) {
                Map.Entry<String, ExporterProperty> element = set.next();
                // if(!localEf.hasLocalValueFor((String) element.getKey())) {
                ExporterProperty exporterProperty = localEf.getExporterProperty(element.getKey());
                if (exporterProperty != null) {
                    values.add(exporterProperty);
                }
            // }
            }
            return values.toArray(new ExporterProperty[values.size()]);
        }
    });
    viewer.setLabelProvider(new ILabelProvider() {

        public void removeListener(ILabelProviderListener listener) {
        }

        public boolean isLabelProperty(Object element, String property) {
            return false;
        }

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public String getText(Object element) {
            ExporterProperty exporterProperty = ((ExporterProperty) element);
            return exporterProperty.getDescriptionForLabel();
        }

        public Image getImage(Object element) {
            return null;
        }
    });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        private SelectionListener getSelectionListener(ExporterProperty prop) {
            // $NON-NLS-1$//$NON-NLS-2$
            if (!("path".equals(prop.getType()) || "directory".equals(prop.getType())))
                return null;
            // $NON-NLS-1$
            final boolean isPath = "path".equals(prop.getType());
            return new SelectionListener() {

                public void widgetDefaultSelected(SelectionEvent e) {
                    widgetSelected(e);
                }

                public void widgetSelected(SelectionEvent e) {
                    String title = isPath ? HibernateConsoleMessages.ExporterSettingsTab_select_path : HibernateConsoleMessages.ExporterSettingsTab_select_dir;
                    String description = isPath ? HibernateConsoleMessages.ExporterSettingsTab_select_path2 : HibernateConsoleMessages.ExporterSettingsTab_select_dir2;
                    MessageDialog dialog = new MessageDialog(getShell(), title, null, description, MessageDialog.QUESTION, new String[] { HibernateConsoleMessages.CodeGenerationSettingsTab_filesystem, HibernateConsoleMessages.CodeGenerationSettingsTab_workspace, IDialogConstants.CANCEL_LABEL }, 1);
                    int answer = dialog.open();
                    String strPath = null;
                    if (answer == 0) {
                        // filesystem
                        DirectoryDialog dialog2 = new DirectoryDialog(getShell());
                        dialog2.setText(title);
                        dialog2.setMessage(description);
                        String dir = dialog2.open();
                        if (dir != null) {
                            strPath = dir;
                        }
                    } else if (answer == 1) {
                        // workspace
                        IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(), (IPath) null, new Path[0], title, description, new String[0], isPath, true, false);
                        if (paths != null && paths.length > 0) {
                            strPath = paths[0].toOSString();
                            if (isPath) {
                                for (int i = 1; i < paths.length; i++) {
                                    strPath += ';' + paths[i].toOSString();
                                }
                            }
                        }
                    } else
                        return;
                    String oldPath = ((Text) value).getText();
                    if (isPath && oldPath.trim().length() > 0 && strPath != null)
                        ((Text) value).setText(oldPath + ';' + strPath);
                    else {
                        if (strPath != null)
                            ((Text) value).setText(strPath);
                    }
                }
            };
        }

        public void selectionChanged(SelectionChangedEvent event) {
            if (value == null)
                return;
            IStructuredSelection iss = (IStructuredSelection) event.getSelection();
            if (!iss.isEmpty()) {
                ExporterProperty prop = (ExporterProperty) iss.getFirstElement();
                if ("boolean".equalsIgnoreCase(prop.getType())) {
                    // $NON-NLS-1$
                    disposeBrowseButton();
                    createComboValueComposite(new String[] { String.valueOf(true), String.valueOf(false) });
                    ((Combo) value).select(Boolean.valueOf(ef.getPropertyValue(prop.getName())).booleanValue() ? 0 : 1);
                } else if (// $NON-NLS-1$
                "directory".equalsIgnoreCase(prop.getType()) || "path".equalsIgnoreCase(prop.getType())) {
                    // $NON-NLS-1$
                    disposeBrowseButton();
                    createTextValueComposite(1);
                    ((Text) value).setText(ef.getPropertyValue(prop.getName()));
                    createBrowseButton(getSelectionListener(prop), prop);
                } else {
                    disposeBrowseButton();
                    createTextValueComposite(2);
                    ((Text) value).setText(ef.getPropertyValue(prop.getName()));
                }
            } else {
                createTextValueComposite(2);
            }
        }
    });
    viewer.setInput(ef);
    if (viewer.getCombo().getItemCount() > 0) {
        Object selected = null;
        if (selectedPropertyId != null) {
            selected = ef.getExporterProperty(selectedPropertyId);
        } else {
            selected = viewer.getElementAt(0);
        }
        viewer.setSelection(new StructuredSelection(selected));
        viewer.getCombo().select(viewer.getCombo().getSelectionIndex());
    }
}
Also used : StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Image(org.eclipse.swt.graphics.Image) Iterator(java.util.Iterator) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog) IPath(org.eclipse.core.runtime.IPath) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Text(org.eclipse.swt.widgets.Text) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) ExporterProperty(org.hibernate.eclipse.console.model.impl.ExporterProperty) ExporterFactory(org.hibernate.eclipse.console.model.impl.ExporterFactory) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) Map(java.util.Map) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 30 with IStructuredContentProvider

use of org.eclipse.jface.viewers.IStructuredContentProvider in project jbosstools-hibernate by jbosstools.

the class ConnectionProfileCtrl method createComboWithTwoButtons.

public Composite createComboWithTwoButtons(Composite container, int hspan, String defaultValue, ButtonPressedAction action1, ButtonPressedAction action2) {
    Composite comp = SWTFactory.createComposite(container, container.getFont(), 3, 1, GridData.FILL_HORIZONTAL, 0, 0);
    Combo combo;
    combo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
    combo.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
    comboControl = new ComboViewer(combo);
    comboControl.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        public Object[] getElements(Object inputElement) {
            return getProfileNameList().toArray();
        }
    });
    comboControl.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            ConnectionWrapper cw = (ConnectionWrapper) element;
            return cw.getId();
        }
    });
    comboControl.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            buttonEdit.setEnabled(getSelectedConnection().getProfile() != null);
            notifyModifyListeners();
        }
    });
    combo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
    buttonNew = new Button(comp, SWT.PUSH);
    buttonNew.setText(HibernateConsoleMessages.ConnectionProfileCtrl_New);
    buttonNew.addSelectionListener(action1);
    buttonEdit = new Button(comp, SWT.PUSH);
    buttonEdit.setText(HibernateConsoleMessages.ConnectionProfileCtrl_Edit);
    buttonEdit.addSelectionListener(action2);
    updateInput();
    return comp;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Combo(org.eclipse.swt.widgets.Combo) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) ComboViewer(org.eclipse.jface.viewers.ComboViewer) Button(org.eclipse.swt.widgets.Button) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) GridData(org.eclipse.swt.layout.GridData) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Aggregations

IStructuredContentProvider (org.eclipse.jface.viewers.IStructuredContentProvider)89 Viewer (org.eclipse.jface.viewers.Viewer)69 Composite (org.eclipse.swt.widgets.Composite)52 GridData (org.eclipse.swt.layout.GridData)49 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)40 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)39 TableViewer (org.eclipse.jface.viewers.TableViewer)37 GridLayout (org.eclipse.swt.layout.GridLayout)37 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)36 Label (org.eclipse.swt.widgets.Label)29 LabelProvider (org.eclipse.jface.viewers.LabelProvider)26 Button (org.eclipse.swt.widgets.Button)26 SelectionEvent (org.eclipse.swt.events.SelectionEvent)25 Image (org.eclipse.swt.graphics.Image)25 ArrayList (java.util.ArrayList)23 Table (org.eclipse.swt.widgets.Table)23 ComboViewer (org.eclipse.jface.viewers.ComboViewer)19 ILabelProviderListener (org.eclipse.jface.viewers.ILabelProviderListener)18 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)17 Text (org.eclipse.swt.widgets.Text)17