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:ca.mcgill.cs.swevo.qualyzer.editors.pages.CodeEditorPage.java

License:Open Source License

/**
 * Updates Description boxes as the selected item in the table changes. Also updates the local
 * information so that changes are not lost.
 * @return/*from w  w  w  .j  a v  a 2  s . c o  m*/
 */
private ISelectionChangedListener createTableSelectionListener() {
    return new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
            CodeTableRow row = (CodeTableRow) selection.getFirstElement();

            if (row == null) {
                fDescription.setText(EMPTY);
                return;
            }

            if (fForm.getMessageType() == IMessageProvider.ERROR) {
                fTableViewer.setSelection(new StructuredSelection(fCurrentRow));
            }

            if (row != fCurrentRow) {
                if (fCurrentRow != null) {
                    fCurrentRow.setDescription(fDescription.getText().trim());
                    fTableViewer.refresh(fCurrentRow);
                }

                fCurrentRow = row;
                fDescription.setText(fCurrentRow.getDescription());
                fCodeName.setText(Messages.getString("editors.pages.CodeEditorPage.selectedCode") //$NON-NLS-1$
                        + fCurrentRow.getName());
            }
        }

    };
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.AddParticipantHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    CommonNavigator view = (CommonNavigator) page.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);
    ISelection selection = view.getCommonViewer().getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection strucSelection = (IStructuredSelection) selection;
        Object element = strucSelection.getFirstElement();

        Project project = ResourcesUtil.getProject(element);

        AddParticipantWizard wizard = new AddParticipantWizard(project);

        QualyzerWizardDialog dialog = new QualyzerWizardDialog(wizard);
        dialog.setBlockOnOpen(!fTesting);
        dialog.open();/*ww  w  . j  av  a 2s.co m*/
        fTester.execute(dialog);

        if (dialog.getReturnCode() == Window.OK) {
            view.getCommonViewer().refresh();
            WrapperParticipant wrapper = new WrapperParticipant(project);
            IProject wProject = ResourcesPlugin.getWorkspace().getRoot().getProject(project.getFolderName());
            view.getCommonViewer().expandToLevel(wProject, IResource.DEPTH_ONE);
            view.getCommonViewer().expandToLevel(wrapper, IResource.DEPTH_INFINITE);

            ResourcesUtil.openEditor(page, wizard.getParticipant());
        }
    }

    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.ExportCodesFragmentsHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    ISelection selection = page.getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        Object element = structured.getFirstElement();

        if (element instanceof WrapperCode) {
            Project project = ((WrapperCode) element).getProject();
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

            FileDialog dialog = new FileDialog(shell, SWT.SAVE);
            dialog.setText(Messages.getString("handlers.ExportCodesHandler.exportDestination")); //$NON-NLS-1$
            dialog.setFilterExtensions(new String[] { "*.csv" }); //$NON-NLS-1$
            String fileName = dialog.open();

            if (fileName != null) {
                int index = fileName.indexOf('.');
                if (index == -1 || !fileName.substring(index).equals(CSV)) {
                    fileName += CSV;/*from  www  .  ja v  a2s . c  om*/
                }

                exportCodes(fileName, project, shell);
            }
        }
    }
    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.ExportCodesHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    ISelection selection = page.getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        Object element = structured.getFirstElement();

        if (element instanceof WrapperCode) {
            Project project = ((WrapperCode) element).getProject();
            Facade facade = Facade.getInstance();

            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

            FileDialog dialog = new FileDialog(shell, SWT.SAVE);
            dialog.setText(Messages.getString("handlers.ExportCodesHandler.exportDestination")); //$NON-NLS-1$
            dialog.setFilterExtensions(new String[] { "*.csv" }); //$NON-NLS-1$
            String fileName = dialog.open();

            if (fileName != null) {
                int index = fileName.indexOf('.');
                if (index == -1 || !fileName.substring(index).equals(CSV)) {
                    fileName += CSV;//from  w  w w.  jav a  2  s . c  o m
                }

                Map<String, List<String>> documentMap = new HashMap<String, List<String>>();
                Map<String, List<Integer>> freqMap = new HashMap<String, List<Integer>>();
                Map<String, String> codeMap = buildCodeMap(project);

                detectDocuments(project, facade, documentMap, freqMap);
                StringBuilder buffer = buildStringBuilder(documentMap, freqMap, codeMap);

                writeFile(shell, fileName, buffer);
            }
        }
    }
    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.MemoPropertiesHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    CommonNavigator view = (CommonNavigator) page.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);
    ISelection selection = view.getCommonViewer().getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection struct = (IStructuredSelection) selection;
        Object element = struct.getFirstElement();

        if (element instanceof Memo) {
            Memo memo = (Memo) element;//  w w  w.  ja  v a 2 s . c om
            MemoPropertiesDialog dialog = new MemoPropertiesDialog(shell, memo);
            dialog.create();
            dialog.setBlockOnOpen(!fTesting);
            dialog.open();
            fTester.execute(dialog);

            if (dialog.getReturnCode() == Window.OK) {
                memo.setDate(dialog.getDate());
                memo.setAuthor(dialog.getAuthor());
                memo.setParticipants(dialog.getParticipants());
                memo.setCode(dialog.getCode());
                memo.setTranscript(dialog.getTranscript());

                Facade.getInstance().saveMemo(memo);

                view.getCommonViewer().refresh();
            }
        }
    }
    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.NewCodeHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    CommonNavigator viewer = (CommonNavigator) activePage.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);
    ISelection selection = viewer.getCommonViewer().getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection strucSelection = (IStructuredSelection) selection;
        Object element = strucSelection.getFirstElement();

        Project project = ResourcesUtil.getProject(element);

        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

        NewCodeDialog dialog = new NewCodeDialog(shell, project);
        dialog.create();/*from www  .  j av  a2 s  .  c om*/
        dialog.setBlockOnOpen(!fTesting);
        dialog.open();
        fTester.execute(dialog);

        if (dialog.getReturnCode() == Window.OK) {
            Facade.getInstance().createCode(dialog.getName(), dialog.getDescription(), project);
            viewer.getCommonViewer().refresh();
            IProject wProject = ResourcesPlugin.getWorkspace().getRoot().getProject(project.getFolderName());
            viewer.getCommonViewer().expandToLevel(wProject, IResource.DEPTH_ONE);

            ResourcesUtil.openEditor(activePage, new WrapperCode(project));
        }
    }

    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.ProjectPropertiesHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    CommonNavigator view = (CommonNavigator) page.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);
    ISelection selection = view.getCommonViewer().getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection struct = (IStructuredSelection) selection;
        Object element = struct.getFirstElement();
        if (element instanceof IProject) {
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            Project project = ResourcesUtil.getProject(element);
            ProjectPropertiesDialog dialog = new ProjectPropertiesDialog(shell, project);
            dialog.setBlockOnOpen(!fTesting);
            dialog.open();//  w w  w  .ja va  2  s  .  c o  m
            fTester.execute(dialog);
            try {
                if (dialog.getReturnCode() == Window.OK) {
                    IProject wProject = (IProject) element;
                    try {
                        FileUtil.setProjectProperty(wProject, FileUtil.ACTIVE_INV, dialog.getInvestigator());

                        Facade facade = Facade.getInstance();
                        facade.getListenerManager().notifyProjectListeners(ChangeType.MODIFY, project, facade);
                    } catch (CoreException e) {
                        gLogger.error("Unable to set Active Investigator", e); //$NON-NLS-1$
                        MessageDialog.openError(shell,
                                Messages.getString("handlers.ProjectPropertiesHandler.fileAccessError"), //$NON-NLS-1$
                                Messages.getString("handlers.ProjectPropertiesHandler.errorMessage")); //$NON-NLS-1$
                    }
                }
            } catch (QualyzerException e) {
                MessageDialog.openError(shell,
                        Messages.getString("handlers.ProjectPropertiesHandler.fileAccessError"), //$NON-NLS-1$
                        e.getMessage());
            }
        }
    }

    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.handlers.RenameProjectHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    CommonNavigator view = (CommonNavigator) page.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);

    ISelection selection = view.getCommonViewer().getSelection();

    if (selection != null && selection instanceof IStructuredSelection) {
        IStructuredSelection struct = (IStructuredSelection) selection;
        Object element = struct.getFirstElement();
        if (element instanceof IProject) {
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            Project project = ResourcesUtil.getProject(element);
            RenameProjectDialog dialog = new RenameProjectDialog(shell, project);
            dialog.setBlockOnOpen(!fTesting);
            dialog.create();//  www. j  av a  2s . co  m
            dialog.open();
            fTester.execute(dialog);
            if (dialog.getReturnCode() == Window.OK) {
                try {
                    String oldFolderName = project.getFolderName();
                    Facade.getInstance().renameProject(project, dialog.getNewName());

                    FileUtil.renameProject(oldFolderName, dialog.getNewName());

                    IProject wProject = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(dialog.getNewName().replace(' ', '_'));
                    PersistenceManager.getInstance().refreshManager(wProject);

                    view.getCommonViewer().refresh();
                } catch (QualyzerException e) {
                    MessageDialog.openError(shell,
                            Messages.getString("handlers.RenameProjectHandler.renameError"), e.getMessage()); //$NON-NLS-1$
                }
            }
        }
    }
    return null;
}

From source file:ca.mcgill.cs.swevo.qualyzer.providers.TableDragListener.java

License:Open Source License

/**
 * Build the drag data.//from  w w w . j  ava2 s .  c  o  m
 */
@Override
public void dragSetData(DragSourceEvent event) {
    IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
    CodeTableRow row = (CodeTableRow) selection.getFirstElement();

    if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
        event.data = row.getName() + SPLIT + row.getPersistenceId() + SPLIT + row.getFrequency();
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.providers.TableDragListener.java

License:Open Source License

/**
 * Defines the SelectionTransfer so that ValidateDrop can work.
 *//*from  w  w w .j  a  v  a 2s.c o  m*/
@Override
public void dragStart(DragSourceEvent event) {
    IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
    CodeTableRow row = (CodeTableRow) selection.getFirstElement();
    String data = row.getName() + SPLIT + row.getPersistenceId() + SPLIT + row.getFrequency();

    LocalSelectionTransfer tran = LocalSelectionTransfer.getTransfer();
    tran.setSelection(new StructuredSelection(data));
}