Example usage for org.eclipse.jface.viewers Viewer refresh

List of usage examples for org.eclipse.jface.viewers Viewer refresh

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers Viewer refresh.

Prototype

public abstract void refresh();

Source Link

Document

Refreshes this viewer completely with information freshly obtained from this viewer's model.

Usage

From source file:alma.acs.eventbrowser.parts.EventListPart.java

License:Open Source License

/**
 * @see alma.acs.eventGui2.parts.IEventListPart#notifyEventTypeFilterChange(java.lang.String)
 *//*from   ww w . j a  va  2s  .  com*/
@Override
public void notifyEventTypeFilterChanged(final String filterText) {
    //      System.out.println("EventListPart#notifyEventTypeFilterChange : " + filterText);
    if (tableFilter != null) {
        viewer.removeFilter(tableFilter);
    }
    tableFilter = new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (filterText.equals("")) {
                return true;
            }
            EventData row = (EventData) element;
            String column = row.getEventTypeName();
            if (column.toUpperCase(Locale.ENGLISH).contains(filterText.toUpperCase(Locale.ENGLISH))) {
                return true;
            }
            return false;
        }
    };
    viewer.addFilter(tableFilter);
    viewer.refresh();
}

From source file:amltransformation.providers.transformable.TransformableContentProvider.java

License:Open Source License

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    viewer.refresh();
}

From source file:au.gov.ga.earthsci.discovery.ui.DiscoveryResultContentProvider.java

License:Apache License

private static void refreshViewer(final Viewer viewer) {
    if (viewer != null && !viewer.getControl().isDisposed()) {
        viewer.getControl().getDisplay().asyncExec(new Runnable() {
            @Override/*  w w  w . j  a  v  a2s  . co m*/
            public void run() {
                if (!viewer.getControl().isDisposed()) {
                    viewer.refresh();
                }
            }
        });
    }
}

From source file:ch.elexis.core.ui.views.SearchView.java

License:Open Source License

@Override
public void createPartControl(Composite parent) {
    parent.setLayout(new FillLayout());

    Composite main = new Composite(parent, SWT.NONE);
    main.setLayout(new GridLayout(1, false));

    tabFolder = new TabFolder(main, SWT.NONE);
    tabFolder.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainTabItem = new TabItem(tabFolder, SWT.NONE);
    mainTabItem.setText(Messages.SearchView_general); //$NON-NLS-1$
    Composite mainSearchArea = new Composite(tabFolder, SWT.NONE);
    mainTabItem.setControl(mainSearchArea);

    mainSearchArea.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainSearchArea.setLayout(new GridLayout(1, false));

    // text input box
    Composite mainInputArea = new Composite(mainSearchArea, SWT.NONE);
    mainInputArea.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainInputArea.setLayout(new GridLayout(2, false));

    Label searchTextLabel = new Label(mainInputArea, SWT.NONE);
    searchTextLabel.setLayoutData(SWTHelper.getFillGridData(2, true, 1, false));
    searchTextLabel.setText(Messages.SearchView_textToSearch); //$NON-NLS-1$

    mainSearchText = new Text(mainInputArea, SWT.BORDER);
    mainSearchText.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));

    mainCaseCheckbox = new Button(mainInputArea, SWT.CHECK);
    mainCaseCheckbox.setText(Messages.SearchView_honorCase); //$NON-NLS-1$

    // search options
    Composite mainOptionsArea = new Composite(mainSearchArea, SWT.NONE);
    mainOptionsArea.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainOptionsArea.setLayout(new GridLayout(2, true));

    Group typeGroup = new Group(mainOptionsArea, SWT.SHADOW_OUT);
    typeGroup.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    typeGroup.setLayout(new RowLayout());
    typeGroup.setText(Messages.SearchView_dosearch); //$NON-NLS-1$

    consultationRadio = new Button(typeGroup, SWT.RADIO);
    consultationRadio.setText(Messages.SearchView_consultations); //$NON-NLS-1$
    consultationRadio.setSelection(true);

    Group optionsGroup = new Group(mainOptionsArea, SWT.SHADOW_OUT);
    optionsGroup.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    optionsGroup.setLayout(new RowLayout());
    optionsGroup.setText(Messages.SearchView_limitTo); //$NON-NLS-1$

    consultationTextRadio = new Button(optionsGroup, SWT.RADIO);
    consultationTextRadio.setText(Messages.SearchView_entry); //$NON-NLS-1$
    consultationTextRadio.setSelection(true);

    searchButton = new Button(mainSearchArea, SWT.PUSH);
    searchButton.setText(Messages.SearchView_searchButtonCaption); //$NON-NLS-1$
    GridData gd = SWTHelper.getFillGridData(1, true, 1, false);
    gd.horizontalAlignment = GridData.END;
    searchButton.setLayoutData(gd);//from  www.ja  v a  2s.  c om

    searchButton.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            viewer.refresh();
        }

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

    viewer = new TableViewer(main);
    Table table = viewer.getTable();
    table.setLayoutData(SWTHelper.getFillGridData(1, true, 1, true));

    viewer.setContentProvider(new IStructuredContentProvider() {
        public void dispose() {
            // nothing to do
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // nothing to do
        }

        public Object[] getElements(Object inputElement) {
            return mainSearch();
        }
    });

    // simple default label provider
    viewer.setLabelProvider(new LabelProvider() {
        public String getText(Object element) {
            if (element instanceof PersistentObject) {
                PersistentObject po = (PersistentObject) element;
                String type = "?"; //$NON-NLS-1$
                String label = ""; //$NON-NLS-1$
                if (po instanceof Konsultation) {
                    type = Messages.SearchView_consultation; //$NON-NLS-1$

                    Konsultation konsultation = (Konsultation) po;
                    Fall fall = konsultation.getFall();
                    Patient pat = fall.getPatient();
                    label = pat.getLabel() + " - " + fall.getLabel() + " - " //$NON-NLS-1$ //$NON-NLS-2$
                            + konsultation.getLabel();
                } else {
                    label = po.getLabel();
                }

                return type + " - " + label; //$NON-NLS-1$
            } else {
                return super.getText(element);
            }
        }
    });

    viewer.setInput(this.getSite());
}

From source file:ch.elexis.views.SearchView.java

License:Open Source License

@Override
public void createPartControl(Composite parent) {
    parent.setLayout(new FillLayout());

    Composite main = new Composite(parent, SWT.NONE);
    main.setLayout(new GridLayout(1, false));

    tabFolder = new TabFolder(main, SWT.NONE);
    tabFolder.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainTabItem = new TabItem(tabFolder, SWT.NONE);
    mainTabItem.setText(Messages.getString("SearchView.general")); //$NON-NLS-1$
    Composite mainSearchArea = new Composite(tabFolder, SWT.NONE);
    mainTabItem.setControl(mainSearchArea);

    mainSearchArea.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainSearchArea.setLayout(new GridLayout(1, false));

    // text input box
    Composite mainInputArea = new Composite(mainSearchArea, SWT.NONE);
    mainInputArea.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainInputArea.setLayout(new GridLayout(2, false));

    Label searchTextLabel = new Label(mainInputArea, SWT.NONE);
    searchTextLabel.setLayoutData(SWTHelper.getFillGridData(2, true, 1, false));
    searchTextLabel.setText(Messages.getString("SearchView.textToSearch")); //$NON-NLS-1$

    mainSearchText = new Text(mainInputArea, SWT.BORDER);
    mainSearchText.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));

    mainCaseCheckbox = new Button(mainInputArea, SWT.CHECK);
    mainCaseCheckbox.setText(Messages.getString("SearchView.honorCase")); //$NON-NLS-1$

    // search options
    Composite mainOptionsArea = new Composite(mainSearchArea, SWT.NONE);
    mainOptionsArea.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    mainOptionsArea.setLayout(new GridLayout(2, true));

    Group typeGroup = new Group(mainOptionsArea, SWT.SHADOW_OUT);
    typeGroup.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    typeGroup.setLayout(new RowLayout());
    typeGroup.setText(Messages.getString("SearchView.dosearch")); //$NON-NLS-1$

    consultationRadio = new Button(typeGroup, SWT.RADIO);
    consultationRadio.setText(Messages.getString("SearchView.consultations")); //$NON-NLS-1$
    consultationRadio.setSelection(true);

    Group optionsGroup = new Group(mainOptionsArea, SWT.SHADOW_OUT);
    optionsGroup.setLayoutData(SWTHelper.getFillGridData(1, true, 1, false));
    optionsGroup.setLayout(new RowLayout());
    optionsGroup.setText(Messages.getString("SearchView.limitTo")); //$NON-NLS-1$

    consultationTextRadio = new Button(optionsGroup, SWT.RADIO);
    consultationTextRadio.setText(Messages.getString("SearchView.entry")); //$NON-NLS-1$
    consultationTextRadio.setSelection(true);

    Button searchButton = new Button(mainSearchArea, SWT.PUSH);
    searchButton.setText(Messages.getString("SearchView.searchButtonCaption")); //$NON-NLS-1$
    GridData gd = SWTHelper.getFillGridData(1, true, 1, false);
    gd.horizontalAlignment = GridData.END;
    searchButton.setLayoutData(gd);//from   w  ww .j  a v  a2s.c  o  m

    searchButton.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            viewer.refresh();
        }

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

    viewer = new TableViewer(main);
    Table table = viewer.getTable();
    table.setLayoutData(SWTHelper.getFillGridData(1, true, 1, true));

    viewer.setContentProvider(new IStructuredContentProvider() {
        public void dispose() {
            // nothing to do
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // nothing to do
        }

        public Object[] getElements(Object inputElement) {
            return mainSearch();
        }
    });

    // simple default label provider
    viewer.setLabelProvider(new LabelProvider() {
        public String getText(Object element) {
            if (element instanceof PersistentObject) {
                PersistentObject po = (PersistentObject) element;
                String type = "?"; //$NON-NLS-1$
                String label = ""; //$NON-NLS-1$
                if (po instanceof Konsultation) {
                    type = Messages.getString("SearchView.consultation"); //$NON-NLS-1$

                    Konsultation konsultation = (Konsultation) po;
                    Fall fall = konsultation.getFall();
                    Patient pat = fall.getPatient();
                    label = pat.getLabel() + " - " + fall.getLabel() + " - " //$NON-NLS-1$ //$NON-NLS-2$
                            + konsultation.getLabel();
                } else {
                    label = po.getLabel();
                }

                return type + " - " + label; //$NON-NLS-1$
            } else {
                return super.getText(element);
            }
        }
    });

    viewer.setInput(this.getSite());
}

From source file:com.amalto.workbench.dialogs.AnnotationOrderedListsDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite parent) {

    // Should not really be here but well,....

    parent.getShell().setText(this.title);

    Composite composite = (Composite) super.createDialogArea(parent);

    GridLayout layout = (GridLayout) composite.getLayout();
    layout.numColumns = 3;//from   ww  w . j  a va2  s  . c  om
    layout.makeColumnsEqualWidth = false;
    // layout.verticalSpacing = 10;

    if (actionType == AnnotationWrite_ActionType || actionType == AnnotationHidden_ActionType) {
        textControl = new CCombo(composite, SWT.BORDER | SWT.READ_ONLY);

        // roles=Util.getCachedXObjectsNameSet(this.xObject, TreeObject.ROLE);
        roles = getAllRolesStr();
        ((CCombo) textControl).setItems(roles.toArray(new String[roles.size()]));

    } else if (actionType == AnnotationLookupField_ActionType
            || actionType == AnnotationPrimaKeyInfo_ActionType) {
        textControl = new CCombo(composite, SWT.BORDER | SWT.READ_ONLY);

        // roles=Util.getCachedXObjectsNameSet(this.xObject, TreeObject.ROLE);
        roles = getConceptElements();
        ((CCombo) textControl).setItems(roles.toArray(new String[roles.size()]));

    } else {
        if (actionType == AnnotationOrderedListsDialog.AnnotationSchematron_ActionType) {
            textControl = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        } else {
            textControl = new Text(composite, SWT.BORDER | SWT.SINGLE);
        }
    }

    if (actionType == AnnotationOrderedListsDialog.AnnotationForeignKeyInfo_ActionType) {
        textControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    } else {
        if (actionType == AnnotationOrderedListsDialog.AnnotationSchematron_ActionType) {
            textControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 7));
        } else {
            textControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
        }
    }

    ((GridData) textControl.getLayoutData()).minimumWidth = 400;

    textControl.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            if ((e.stateMask == 0) && (e.character == SWT.CR)) {
                xPaths.add(AnnotationOrderedListsDialog.getControlText(textControl));
                viewer.refresh();
                fireXPathsChanges();
            }
        }
    });

    if (actionType == AnnotationOrderedListsDialog.AnnotationForeignKeyInfo_ActionType) {
        Button xpathButton = new Button(composite, SWT.PUSH | SWT.CENTER);
        xpathButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
        xpathButton.setText("...");//$NON-NLS-1$
        xpathButton.setToolTipText(Messages.AnnotationOrderedListsDialog_SelectXpath);
        xpathButton.addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(SelectionEvent e) {

            }

            public void widgetSelected(SelectionEvent e) {
                XpathSelectDialog dlg = getNewXpathSelectDialog(parentPage, dataModelName);

                dlg.setLock(lock);

                dlg.setBlockOnOpen(true);

                dlg.open();

                if (dlg.getReturnCode() == Window.OK) {
                    ((Text) textControl).setText(dlg.getXpath());
                    dlg.close();
                }

            }

        });

    }

    Button addLabelButton = new Button(composite, SWT.PUSH);
    addLabelButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    // addLabelButton.setText("Set");
    addLabelButton.setImage(ImageCache.getCreatedImage(EImage.ADD_OBJ.getPath()));
    addLabelButton.setToolTipText(Messages.AnnotationOrderedListsDialog_Add);
    addLabelButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            boolean exist = false;
            for (String string : xPaths) {
                if (string.equals(getControlText(textControl))) {
                    exist = true;
                }
            }
            if (!exist && getControlText(textControl) != null && getControlText(textControl) != "") {
                xPaths.add(getControlText(textControl));
            }
            viewer.refresh();
            fireXPathsChanges();
        };
    });

    final String COLUMN = columnName;

    viewer = new TableViewer(composite,
            SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
    viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    ((GridData) viewer.getControl().getLayoutData()).heightHint = 100;
    // Set up the underlying table
    Table table = viewer.getTable();
    // table.setLayoutData(new GridData(GridData.FILL_BOTH));

    new TableColumn(table, SWT.CENTER).setText(COLUMN);
    table.getColumn(0).setWidth(500);
    for (int i = 1, n = table.getColumnCount(); i < n; i++) {
        table.getColumn(i).pack();
    }

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Create the cell editors --> We actually discard those later: not natural for an user
    CellEditor[] editors = new CellEditor[1];
    if (actionType == AnnotationOrderedListsDialog.AnnotationWrite_ActionType
            || actionType == AnnotationOrderedListsDialog.AnnotationHidden_ActionType
            || actionType == AnnotationLookupField_ActionType
            || actionType == AnnotationPrimaKeyInfo_ActionType) {
        editors[0] = new ComboBoxCellEditor(table, roles.toArray(new String[] {}), SWT.READ_ONLY);
    } else {
        editors[0] = new TextCellEditor(table);
    }

    viewer.setCellEditors(editors);

    // set the content provider
    viewer.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

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

        public Object[] getElements(Object inputElement) {
            @SuppressWarnings("unchecked")
            ArrayList<String> xPaths = (ArrayList<String>) inputElement;
            ArrayList<DescriptionLine> lines = new ArrayList<DescriptionLine>();
            for (String xPath : xPaths) {
                DescriptionLine line = new DescriptionLine(xPath);
                lines.add(line);
            }
            // we return an instance line made of a Sring and a boolean
            return lines.toArray(new DescriptionLine[lines.size()]);
        }
    });

    // set the label provider
    viewer.setLabelProvider(new ITableLabelProvider() {

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

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void removeListener(ILabelProviderListener listener) {
        }

        public String getColumnText(Object element, int columnIndex) {
            // System.out.println("getColumnText() "+columnIndex);
            DescriptionLine line = (DescriptionLine) element;
            switch (columnIndex) {
            case 0:
                return line.getLabel();
            }
            return "";//$NON-NLS-1$
        }

        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    });

    // Set the column properties
    viewer.setColumnProperties(new String[] { COLUMN });

    // set the Cell Modifier
    viewer.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            // if (INSTANCE_ACCESS.equals(property)) return true; Deactivated
            return true;
            // return false;
        }

        public void modify(Object element, String property, Object value) {

            TableItem item = (TableItem) element;
            DescriptionLine line = (DescriptionLine) item.getData();
            String orgValue = line.getLabel();
            if (actionType != AnnotationWrite_ActionType && actionType != AnnotationHidden_ActionType
                    && actionType != AnnotationLookupField_ActionType
                    && actionType != AnnotationPrimaKeyInfo_ActionType) {
                int targetPos = xPaths.indexOf(value.toString());
                if (targetPos < 0) {
                    line.setLabel(value.toString());
                    int index = xPaths.indexOf(orgValue);
                    xPaths.remove(index);
                    xPaths.add(index, value.toString());
                    viewer.update(line, null);
                } else if (targetPos >= 0 && !value.toString().equals(orgValue)) {
                    MessageDialog.openInformation(null, Messages.Warning,
                            Messages.AnnotationOrderedListsDialog_ValueAlreadyExists);
                }
            } else {

                String[] attrs = roles.toArray(new String[] {});
                int index = Integer.parseInt(value.toString());
                if (index == -1) {
                    return;
                }
                value = attrs[index];
                int pos = xPaths.indexOf(value.toString());
                if (pos >= 0 && !(orgValue.equals(value))) {
                    MessageDialog.openInformation(null, Messages.Warning,
                            Messages.AnnotationOrderedListsDialog_);
                    return;
                } else if (pos < 0) {
                    line.setLabel(value.toString());
                    xPaths.set(index, value.toString());
                    viewer.update(line, null);
                }
            }

            fireXPathsChanges();
        }

        public Object getValue(Object element, String property) {
            DescriptionLine line = (DescriptionLine) element;
            String value = line.getLabel();

            if (actionType == AnnotationWrite_ActionType || actionType == AnnotationHidden_ActionType
                    || actionType == AnnotationLookupField_ActionType
                    || actionType == AnnotationPrimaKeyInfo_ActionType) {
                String[] attrs = roles.toArray(new String[] {});
                return Arrays.asList(attrs).indexOf(value);
            } else {
                return value;
            }

        }
    });

    // Listen for changes in the selection of the viewer to display additional parameters
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            DescriptionLine line = (DescriptionLine) ((IStructuredSelection) viewer.getSelection())
                    .getFirstElement();
            if (line != null) {
                if (textControl instanceof CCombo) {
                    ((CCombo) textControl).setText(line.getLabel());
                }
                if (textControl instanceof Text) {
                    ((Text) textControl).setText(line.getLabel());
                }
            }
        }
    });

    // display for Delete Key events to delete an instance
    viewer.getTable().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            // System.out.println("Table keyReleased() ");
            if ((e.stateMask == 0) && (e.character == SWT.DEL) && (viewer.getSelection() != null)) {
                DescriptionLine line = (DescriptionLine) ((IStructuredSelection) viewer.getSelection())
                        .getFirstElement();
                @SuppressWarnings("unchecked")
                ArrayList<String> xPaths = (ArrayList<String>) viewer.getInput();
                xPaths.remove(line.getLabel());
                viewer.refresh();
            }
        }
    });

    viewer.setInput(xPaths);
    viewer.refresh();

    Composite rightButtonsComposite = new Composite(composite, SWT.NULL);
    rightButtonsComposite.setLayout(new GridLayout(1, true));
    rightButtonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));

    Button upButton = new Button(rightButtonsComposite, SWT.PUSH | SWT.CENTER);
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
    upButton.setImage(ImageCache.getCreatedImage(EImage.PREV_NAV.getPath()));
    upButton.setToolTipText(Messages.AnnotationOrderedListsDialog_MoveUpTheItem);
    upButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            DescriptionLine line = (DescriptionLine) ((IStructuredSelection) viewer.getSelection())
                    .getFirstElement();
            if (line == null) {
                return;
            }
            int i = 0;
            for (String xPath : xPaths) {
                if (xPath.equals(line.getLabel())) {
                    if (i > 0) {
                        xPaths.remove(i);
                        xPaths.add(i - 1, xPath);
                        viewer.refresh();
                        viewer.getTable().setSelection(i - 1);
                        viewer.getTable().showSelection();
                        fireXPathsChanges();
                    }
                    return;
                }
                i++;
            }
        };
    });
    Button downButton = new Button(rightButtonsComposite, SWT.PUSH | SWT.CENTER);
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
    downButton.setImage(ImageCache.getCreatedImage(EImage.NEXT_NAV.getPath()));
    downButton.setToolTipText(Messages.AnnotationOrderedListsDialog_MoveDownTheItem);
    downButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            DescriptionLine line = (DescriptionLine) ((IStructuredSelection) viewer.getSelection())
                    .getFirstElement();
            if (line == null) {
                return;
            }
            int i = 0;
            for (String xPath : xPaths) {
                if (xPath.equals(line.getLabel())) {
                    if (i < xPaths.size() - 1) {
                        xPaths.remove(i);
                        xPaths.add(i + 1, xPath);
                        viewer.refresh();
                        viewer.getTable().setSelection(i + 1);
                        viewer.getTable().showSelection();
                        fireXPathsChanges();
                    }
                    return;
                }
                i++;
            }
        };
    });
    Button delButton = new Button(rightButtonsComposite, SWT.PUSH | SWT.CENTER);
    delButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
    delButton.setImage(ImageCache.getCreatedImage(EImage.DELETE_OBJ.getPath()));
    delButton.setToolTipText(Messages.AnnotationOrderedListsDialog_DelTheItem);

    delButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            DescriptionLine line = (DescriptionLine) ((IStructuredSelection) viewer.getSelection())
                    .getFirstElement();
            if (line != null) {
                @SuppressWarnings("unchecked")
                ArrayList<String> xPaths = (ArrayList<String>) viewer.getInput();
                xPaths.remove(line.getLabel());
                viewer.refresh();
                fireXPathsChanges();
            }
        };
    });

    textControl.setFocus();
    if (actionType != AnnotationOrderedListsDialog.AnnotationForeignKeyInfo_ActionType
            && actionType != AnnotationOrderedListsDialog.AnnotationTargetSystems_ActionType
            && actionType != AnnotationOrderedListsDialog.AnnotationSchematron_ActionType
            && actionType != AnnotationOrderedListsDialog.AnnotationLookupField_ActionType
            && actionType != AnnotationOrderedListsDialog.AnnotationPrimaKeyInfo_ActionType) {
        checkBox = new Button(composite, SWT.CHECK);
        checkBox.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 2, 1));
        checkBox.addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
            };

            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                recursive = checkBox.getSelection();
            };
        });
        checkBox.setSelection(recursive);
        checkBox.setText(Messages.AnnotationOrderedListsDialog_SetRoleRecursively);
        // Label label = new Label(composite, SWT.LEFT);
        // label.setText("set role recursively");
        // label.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true,
        // 1, 1));
    }

    if (actionType == AnnotationForeignKeyInfo_ActionType) {
        createFKInfoFormatComp(composite);
        addDoubleClickListener();
    }

    return composite;
}

From source file:com.amalto.workbench.widgets.ComplexTableViewer.java

License:Open Source License

protected void createViewer() {
    createTable();//from  w  w  w.  j  a  v  a 2  s  .  c om
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, columns.size(), 1);
    table.setLayoutData(gd);
    gd.heightHint = 80;
    // Up Down Delete button group
    Composite stepUpDownComposite = toolkit.createComposite(mainComposite, SWT.NONE);
    stepUpDownComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
    stepUpDownComposite.setLayout(new GridLayout(1, false));

    upButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    upButton.setImage(ImageCache.getCreatedImage(EImage.PREV_NAV.getPath()));
    upButton.setToolTipText(Messages.ComplexTableViewer_UpBtnTip);
    upButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index > 0 && index < viewer.getTable().getItemCount()) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index - 1, line);

                viewer.refresh();

                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        };
    });
    downButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    downButton.setImage(ImageCache.getCreatedImage(EImage.NEXT_NAV.getPath()));
    downButton.setToolTipText(Messages.ComplexTableViewer_DownBtnTip);
    downButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index >= 0 && index < viewer.getTable().getItemCount() - 1) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index + 1, line);
                // viewer.setInput(items);
                viewer.refresh();
                // TODO
                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        };
    });
    deleteButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    deleteButton.setToolTipText(Messages.ComplexTableViewer_DelBtnLabel);
    deleteButton.setImage(ImageCache.getCreatedImage(EImage.DELETE_OBJ.getPath()));
    deleteButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index >= 0 && index < viewer.getTable().getItemCount()) {
                List<Line> items = (List<Line>) viewer.getInput();
                items.remove(index);
                viewer.refresh();
                int pos = index - 1;
                if (pos >= 0) {
                    viewer.getTable().select(pos);
                }
                markDirty();
            }
        };
    });

    // Create the cell editors --> We actually discard those later: not natural for an user
    CellEditor[] editors = new CellEditor[columns.size()];
    for (int i = 0; i < columns.size(); i++) {
        if (columns.get(i).isText()) {
            editors[i] = new TextCellEditor(table);
        } else if (columns.get(i).isCombo()) {
            editors[i] = new ComboBoxCellEditor(table, columns.get(i).getComboValues(), SWT.READ_ONLY);
        } else if (columns.get(i).isXPATH()) {
            editors[i] = new XpathCellEditor(table, validators.get(columns.get(i)));
        } else if (columns.get(i).isMultiMessage()) {
            editors[i] = new MultiMessageEditor(table);
            multiMsg.setColumn(table.getColumn(i));
        } else if (columns.get(i).isValidationRule()) {
            editors[i] = new ValidationRuleEditor(table);
            validationRule.setColumn(table.getColumn(i));
        }
    }
    viewer.setCellEditors(editors);

    // set the content provider
    viewer.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

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

        @SuppressWarnings("unchecked")
        public Object[] getElements(Object inputElement) {
            List<Line> lines = (List<Line>) inputElement;
            return lines.toArray(new Line[lines.size()]);
        }
    });

    // set the label provider
    viewer.setLabelProvider(new ITableLabelProvider() {

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

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void removeListener(ILabelProviderListener listener) {
        }

        public String getColumnText(Object element, int columnIndex) {
            Line line = (Line) element;
            if (columnIndex >= 0 && columnIndex < columns.size()) {
                for (KeyValue keyvalue : line.keyValues) {
                    if (keyvalue.key.equals(columns.get(columnIndex).getName())) {
                        String val = keyvalue.value;
                        if (columns.get(columnIndex).isNillable()) {
                            if (columns.get(columnIndex).getNillValue().equals(val)) {
                                val = columns.get(columnIndex).getNillDisplay();
                            }
                        }
                        return val;
                    }
                }
            }
            return "";//$NON-NLS-1$
        }

        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    });

    // Set the column properties
    ArrayList<String> columnNames = new ArrayList<String>();
    for (ComplexTableViewerColumn column : columns) {
        columnNames.add(column.getName());
    }
    viewer.setColumnProperties(columnNames.toArray(new String[columnNames.size()]));

    // set the Cell Modifier
    viewer.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            // if (INSTANCE_ACCESS.equals(property)) return true; Deactivated
            return editable;
        }

        @SuppressWarnings("unchecked")
        public void modify(Object element, String property, Object value) {
            if (value instanceof Integer) {
                if (Integer.valueOf(value.toString()) == -1) {
                    return;
                }
            }
            // modify the text and combo cell value
            TableItem item = (TableItem) element;
            Line line = (Line) item.getData();
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            if (!isForceTextCellEditor(columnIndex) && isAColumnWithCombo(columnIndex)) {
                String[] attrs = columns.get(columnIndex).getComboValues();
                value = attrs[Integer.parseInt(value.toString())];
            }
            KeyValue kv = line.keyValues.get(columnIndex);
            boolean noChange = kv.value.equals(value.toString());
            kv.value = value.toString();

            viewer.refresh();
            if (!noChange) {
                markDirty();
            }
        }

        public Object getValue(Object element, String property) {
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            Line line = (Line) element;
            // add getting value from combo
            if (isAColumnWithCombo(columnIndex)) {
                String value = line.keyValues.get(columnIndex).value;
                String[] attrs = columns.get(columnIndex).getComboValues();
                return Arrays.asList(attrs).indexOf(value);
            }
            for (KeyValue keyvalue : line.keyValues) {
                if (property.equals(keyvalue.key)) {
                    if (keyvalue.value.equals("")) {//$NON-NLS-1$
                        return columns.get(columnIndex).getNillDisplay();
                    }
                    return keyvalue.value;
                }
            }
            return null;
        }

        private boolean isAColumnWithCombo(int columnIdx) {
            return columns.get(columnIdx).isCombo();
        }

        private boolean isForceTextCellEditor(int columnIdx) {
            return columns.get(columnIdx).isText();
        }
    });

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {

            Line line = (Line) ((IStructuredSelection) event.getSelection()).getFirstElement();

            for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
                if (line == null) {
                    Control control = columns.get(columnIndex).getControl();
                    if (control instanceof Text) {
                        ((Text) control).setText("");//$NON-NLS-1$
                    }
                    if (control instanceof CCombo) {
                        ((CCombo) control).select(0);
                    }
                    if (control instanceof Combo) {
                        ((CCombo) control).select(0);
                    }
                } else {
                    for (KeyValue keyvalue : line.keyValues) {
                        if (keyvalue.key.equals(columns.get(columnIndex).getName())) {
                            String val = keyvalue.value;
                            Control control = columns.get(columnIndex).getControl();
                            if (control instanceof Text) {
                                ((Text) control).setText(val);
                            }
                            if (control instanceof CCombo) {
                                ((CCombo) control).setText(val);
                            }
                            if (control instanceof Combo) {
                                ((CCombo) control).setText(val);
                            }
                        }
                    }
                }
            }
        }

    });

    // display for Delete Key events to delete an instance
    viewer.getTable().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void keyReleased(KeyEvent e) {
            if ((e.stateMask == 0) && (e.character == SWT.DEL) && (viewer.getSelection() != null)) {
                Line line = (Line) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
                // update the underlying role and refresh the table
                // update the underlying model
                List<Line> items = (List<Line>) viewer.getInput();
                items.remove(line);
                // refresh
                viewer.refresh();
                // mark for update
                markDirty();
            }
        }
    });
}

From source file:com.amalto.workbench.widgets.TisTableViewer.java

License:Open Source License

@Override
protected void createViewer() {
    super.createTable();
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    table.setLayoutData(gd);/*ww w.  j a v  a2  s . c  o  m*/
    gd.heightHint = 80;
    // Up Down Delete button group
    Composite stepUpDownComposite = toolkit.createComposite(mainComposite, SWT.NONE);
    stepUpDownComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    GridLayout layout = new GridLayout(8, false);
    layout.marginTop = 0;
    layout.marginBottom = 0;
    layout.marginHeight = 0;
    stepUpDownComposite.setLayout(layout);
    //
    addButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    addButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    addButton.setToolTipText(Messages.Add);
    if (isXpath()) {
        addButton.setImage(ImageCache.getCreatedImage(EImage.ADD_NEWXPATH.getPath()));
    } else {
        addButton.setImage(ImageCache.getCreatedImage(EImage.ADD_OBJ.getPath()));
    }
    addButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

            // check uniqueness by concatenating all the values
            List<Line> list = (List<Line>) getViewer().getInput();
            // Update the model
            Line line = new Line(columns.toArray(new ComplexTableViewerColumn[columns.size()]),
                    getInitValues());
            list.add(line);
            viewer.setInput(list);
            // update the instances viewer
            viewer.setSelection(null);
            viewer.refresh();
            viewer.getTable().select(viewer.getTable().getItemCount() - 1);
            markDirty();
        };
    });
    // Add Multi
    if (isAddMulti()) {
        Button selNewPathButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
        selNewPathButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
        selNewPathButton.setToolTipText(Messages.AddMultiple);
        selNewPathButton.setImage(ImageCache.getCreatedImage(EImage.SELECT_NEWXPATH.getPath()));
        selNewPathButton.addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
            };

            @SuppressWarnings("unchecked")
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {

                xpathDialog = getNewXpathDlgInstance();
                xpathDialog.setConceptName(conceptName);
                xpathDialog.setBlockOnOpen(true);
                xpathDialog.open();

                if (xpathDialog.getReturnCode() == Window.OK) {
                    datamodelName = xpathDialog.getDataModelName();
                    String[] xpaths = xpathDialog.getXpath().split("&");//$NON-NLS-1$
                    List<Line> list = new LinkedList<Line>();
                    for (String xpath : xpaths) {
                        // check uniqueness by concatenating all the values
                        list = (List<Line>) getViewer().getInput();
                        // Update the model
                        Line line = new Line(columns.toArray(new ComplexTableViewerColumn[columns.size()]),
                                getLineValues(xpath, 0));
                        list.add(line);
                    }
                    viewer.setInput(list);
                    // update the instances viewer
                    viewer.setSelection(null);
                    viewer.refresh();
                    viewer.getTable().select(viewer.getTable().getItemCount() - 1);

                    markDirty();
                }
            };
        });
    }
    deleteButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    deleteButton.setToolTipText(Messages.DeleteSelectedItem);
    deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    deleteButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            List<Line> input = (List<Line>) viewer.getInput();
            TableItem[] items = viewer.getTable().getSelection();
            for (int i = 0; i < items.length; i++) {
                input.remove(items[i].getData());
            }
            viewer.setInput(input);
            markDirty();
        };
    });
    deleteButton.setImage(ImageCache.getCreatedImage(EImage.DELETE_OBJ.getPath()));
    // delete all
    Button deleteAllButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    deleteAllButton.setToolTipText(Messages.DeleteAllItems);
    deleteAllButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    deleteAllButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            List<Line> items = (List<Line>) viewer.getInput();
            items.clear();
            viewer.setInput(items);
            viewer.refresh();
            markDirty();
        };
    });
    deleteAllButton.setImage(ImageCache.getCreatedImage(EImage.PROGRESS_REMALL.getPath()));

    upButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    upButton.setToolTipText(Messages.MoveUpSelectedItem);
    upButton.setImage(ImageCache.getCreatedImage(EImage.PREV_NAV.getPath()));
    upButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    upButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index > 0 && index < viewer.getTable().getItemCount()) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index - 1, line);

                viewer.refresh();

                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        };
    });
    downButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    downButton.setToolTipText(Messages.MovedownSelectedItem);
    downButton.setImage(ImageCache.getCreatedImage(EImage.NEXT_NAV.getPath()));
    downButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    downButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            int index = viewer.getTable().getSelectionIndex();
            if (index >= 0 && index < viewer.getTable().getItemCount() - 1) {
                // commit as we go
                if (mainPage != null) {
                    mainPage.setComitting(true);
                }
                List<Line> items = (List<Line>) viewer.getInput();
                Line line = items.get(index);
                items.remove(index);
                items.add(index + 1, line);
                // viewer.setInput(items);
                viewer.refresh();
                // TODO
                if (mainPage != null) {
                    mainPage.setComitting(false);
                }
                markDirty();
            }
        };
    });

    copyButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    copyButton.setToolTipText(Messages.CopySelectedItems);
    copyButton.setImage(ImageCache.getCreatedImage(EImage.COPY.getPath()));
    copyButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    copyButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {

        }

        public void widgetSelected(SelectionEvent e) {
            int[] indexs = viewer.getTable().getSelectionIndices();
            List<Line> copyLines = new ArrayList<Line>();
            for (int index : indexs) {
                if (index >= 0 && index <= viewer.getTable().getItemCount() - 1) {
                    List<Line> items = (List<Line>) viewer.getInput();
                    Line line = items.get(index);
                    Line copyLine = line.clone();
                    copyLines.add(copyLine);
                }
            }
            if (indexs.length > 0) {
                // enable all paste buttons
                HashSet<Button> btns = pastBtns.get(String.valueOf(columns.size()));
                if (btns != null) {
                    for (Button btn : btns) {
                        if (btn != null) {
                            btn.setEnabled(true);
                        }
                    }
                }
                // add to workbenchclipboard
                WorkbenchClipboard.getWorkbenchClipboard().setLines(String.valueOf(columns.size()), copyLines);
            }
        }
    });
    pastButton = toolkit.createButton(stepUpDownComposite, "", SWT.PUSH | SWT.CENTER);//$NON-NLS-1$
    pastButton.setToolTipText(Messages.PasteSelectedItem);
    pastButton.setImage(ImageCache.getCreatedImage(EImage.PASTE.getPath()));
    pastButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
    pastButton.setEnabled(false);
    pastButton.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
        };

        @SuppressWarnings("unchecked")
        public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            if (mainPage != null) {
                mainPage.setComitting(true);
            }
            boolean dirty = false;
            List<Line> items = (List<Line>) viewer.getInput();
            List<Line> cacheLine = WorkbenchClipboard.getWorkbenchClipboard()
                    .getLines(String.valueOf(columns.size()));
            List<Line> coloneLine = new ArrayList<Line>();
            for (Line l : cacheLine) {
                coloneLine.add(l.clone());
            }
            if (cacheLine.size() > 0) {
                items.addAll(coloneLine);
                dirty = true;
            }
            viewer.refresh();
            // TODO
            if (mainPage != null) {
                mainPage.setComitting(false);
            }
            if (dirty) {
                markDirty();
            }
        };
    });
    HashSet<Button> btns = pastBtns.get(String.valueOf(columns.size()));
    if (btns == null) {
        btns = new HashSet<Button>();
        pastBtns.put(String.valueOf(columns.size()), btns);
    }
    btns.add(pastButton);
    // Create the cell editors --> We actually discard those later: not natural for an user
    CellEditor[] editors = new CellEditor[columns.size()];
    for (int i = 0; i < columns.size(); i++) {
        if (columns.get(i).isText()) {
            editors[i] = new TextCellEditor(table);
        } else if (columns.get(i).isCombo()) {
            editors[i] = new ComboBoxCellEditor(table, columns.get(i).getComboValues(), SWT.READ_ONLY);
        } else if (columns.get(i).isXPATH()) {
            editors[i] = new XpathCellEditor(table, validators.get(columns.get(i)));
        } else if (columns.get(i).isMultiMessage()) {
            editors[i] = new MultiMessageEditor(table);
            multiMsg.setColumn(table.getColumn(i));
        } else if (columns.get(i).isValidationRule()) {
            editors[i] = createValidationRuleEditor();
            validationRule.setColumn(table.getColumn(i));
        }
    }
    viewer.setCellEditors(editors);

    // set the content provider
    viewer.setContentProvider(new IStructuredContentProvider() {

        public void dispose() {
        }

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

        @SuppressWarnings("unchecked")
        public Object[] getElements(Object inputElement) {
            ArrayList<Line> lines = (ArrayList<Line>) inputElement;
            return lines.toArray(new Line[lines.size()]);
        }
    });

    // set the label provider
    viewer.setLabelProvider(new ITableLabelProvider() {

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

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public void removeListener(ILabelProviderListener listener) {
        }

        public String getColumnText(Object element, int columnIndex) {
            Line line = (Line) element;
            if (columnIndex >= 0 && columnIndex < columns.size()) {
                for (KeyValue keyvalue : line.keyValues) {
                    if (keyvalue.key.equals(columns.get(columnIndex).getName())) {
                        String val = keyvalue.value;
                        if (columns.get(columnIndex).isNillable()) {
                            if (columns.get(columnIndex).getNillValue().equals(val)) {
                                val = columns.get(columnIndex).getNillDisplay();
                            }
                        }
                        return val;
                    }
                }
            }
            return "";//$NON-NLS-1$
        }

        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    });

    // Set the column properties
    ArrayList<String> columnNames = new ArrayList<String>();
    for (ComplexTableViewerColumn column : columns) {
        columnNames.add(column.getName());
    }
    viewer.setColumnProperties(columnNames.toArray(new String[columnNames.size()]));

    // set the Cell Modifier
    viewer.setCellModifier(new ICellModifier() {

        public boolean canModify(Object element, String property) {
            return editable;
        }

        @SuppressWarnings("unchecked")
        public void modify(Object element, String property, Object value) {
            if (value instanceof Integer) {
                if (Integer.valueOf(value.toString()) == -1) {
                    return;
                }
            }
            // modify the text and combo cell value
            TableItem item = (TableItem) element;
            Line line = (Line) item.getData();
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            if (columnIndex >= 0 && columnIndex < viewer.getColumnProperties().length) {
                if (isAColumnWithCombo(columnIndex)) {
                    String[] attrs = columns.get(columnIndex).getComboValues();
                    value = attrs[Integer.parseInt(value.toString())];
                }
                KeyValue kv = line.keyValues.get(columnIndex);
                boolean noChange = kv.value.equals(value.toString());
                kv.value = value.toString();
                viewer.refresh();
                if (!noChange) {
                    markDirty();
                }
            }
        }

        public Object getValue(Object element, String property) {
            int columnIndex = Arrays.asList(viewer.getColumnProperties()).indexOf(property);
            Line line = (Line) element;
            // add getting value from combo
            if (isAColumnWithCombo(columnIndex)) {
                String value = line.keyValues.get(columnIndex).value;
                String[] attrs = columns.get(columnIndex).getComboValues();
                return Arrays.asList(attrs).indexOf(value);
            }
            for (KeyValue keyvalue : line.keyValues) {
                if (property.equals(keyvalue.key)) {
                    if (keyvalue.value.equals("")) {//$NON-NLS-1$
                        return columns.get(columnIndex).getNillDisplay();
                    }
                    return keyvalue.value;
                }
            }
            return null;
        }

        private boolean isAColumnWithCombo(int columnIdx) {
            return columns.get(columnIdx).isCombo();
        }

    });

    // display for Delete Key events to delete an instance
    viewer.getTable().addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
        }

        @SuppressWarnings("unchecked")
        public void keyReleased(KeyEvent e) {
            if ((e.stateMask == 0) && (e.character == SWT.DEL) && (viewer.getSelection() != null)) {
                Line line = (Line) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
                // update the underlying role and refresh the table
                // update the underlying model
                List<Line> items = (List<Line>) viewer.getInput();
                items.remove(line);
                // refresh
                viewer.refresh();
                // mark for update
                markDirty();
            }
        }
    });
    // add dispose listener
    viewer.getTable().addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            HashSet<Button> btns = pastBtns.get(String.valueOf(columns.size()));
            btns.remove(pastButton);
        }
    });
}

From source file:com.amitinside.e4.rcp.todo.parts.TodoOverviewPart.java

License:Apache License

@PostConstruct
public void createControls(Composite parent, final MWindow window, EMenuService menuService) {
    applyGridLayout(parent).numColumns(1);

    btnNewButton = new Button(parent, SWT.PUSH);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override/* w w w.j a va 2 s.c om*/
        public void widgetSelected(SelectionEvent e) {
            final Job job = new Job("loading") {
                @Override
                protected IStatus run(IProgressMonitor monitor) {
                    final List<Todo> list = model.getTodos();
                    broker.post(LOCAL_EVENT_FINISH, list);
                    return Status.OK_STATUS;
                }
            };
            job.schedule();
        }
    });
    btnNewButton.setText("Load Data");

    final Text search = new Text(parent, SWT.SEARCH | SWT.CANCEL | SWT.ICON_SEARCH);

    // Assuming that GridLayout is used
    search.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
    search.setMessage("Filter");

    // Filter at every keystroke
    search.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            final Text source = (Text) e.getSource();
            searchString = source.getText();
            // Trigger update in the viewer
            viewer.refresh();
        }
    });

    // SWT.SEARCH | SWT.CANCEL not supported under Windows7
    // This does not work under Windows7
    search.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            if (e.detail == SWT.CANCEL) {
                final Text text = (Text) e.getSource();
                text.setText("");
                //
            }
        }
    });

    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
    final Table table = viewer.getTable();
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    final TableViewerColumn colSummary = new TableViewerColumn(viewer, SWT.NONE);

    colSummary.getColumn().setWidth(100);
    colSummary.getColumn().setText("Summary");

    colSummary.setEditingSupport(new EditingSupport(viewer) {

        @Override
        protected void setValue(Object element, Object value) {
            final Todo todo = (Todo) element;
            todo.setSummary(String.valueOf(value));
            viewer.refresh();
        }

        @Override
        protected Object getValue(Object element) {
            final Todo todo = (Todo) element;
            return todo.getSummary();
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new TextCellEditor(viewer.getTable(), SWT.NONE);
        }

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }
    });
    final TableViewerColumn colDescription = new TableViewerColumn(viewer, SWT.NONE);

    colDescription.getColumn().setWidth(200);
    colDescription.getColumn().setText("Description");

    // We search in the summary and description field
    viewer.addFilter(new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            final Todo todo = (Todo) element;
            return todo.getSummary().contains(searchString) || todo.getDescription().contains(searchString)
                    || todo.getNote().contains(searchString);
        }
    });

    final TableViewerColumn colNotes = new TableViewerColumn(viewer, SWT.NONE);
    colNotes.getColumn().setWidth(50);
    colNotes.getColumn().setText("Notes");

    colNotes.setEditingSupport(new EditingSupport(viewer) {

        @Override
        protected void setValue(Object element, Object value) {
            final Todo todo = (Todo) element;
            todo.setNote(String.valueOf(value));
            viewer.refresh();
        }

        @Override
        protected Object getValue(Object element) {
            final Todo todo = (Todo) element;
            return todo.getNote();
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new TextCellEditor(viewer.getTable(), SWT.NONE);
        }

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }
    });

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            service.setSelection(selection.getFirstElement());
        }
    });
    menuService.registerContextMenu(viewer.getControl(), "com.amitinside.e4.rcp.todo.popupmenu.table");
    writableList = new WritableList(model.getTodos(), Todo.class);
    ViewerSupport.bind(viewer, writableList, BeanProperties
            .values(new String[] { Todo.FIELD_SUMMARY, Todo.FIELD_DESCRIPTION, Todo.FIELD_NOTE }));

}

From source file:com.appnativa.studio.editors.OutlineContentProvider.java

License:Open Source License

public void editorPageChanged(Viewer viewer) {
    if (editorInput != null) {
        IDocument document = documentProvider.getDocument(editorInput);

        if (document != null) {
            RMLDocument d = Studio.getSelectedDocument();
            DesignPane pane = d == null ? null : d.getDesignPane();

            if (pane == null || pane.getRootWidget() == null) {
                rootElement = parseNodes(document);
            } else {
                rootElement = new Node(pane.getRootWidget());
            }/*from   w w  w  .  j  av a2 s  .  com*/
            viewer.refresh();
        }
    }
}