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

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

Introduction

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

Prototype

@Override
    public Object getFirstElement() 

Source Link

Usage

From source file:at.spardat.xma.gui.mapper.MapperDialog.java

License:Open Source License

/**
 * check if the selection stems from a widget selection and
 * assures that the right relationsship is retuned
 * @param attribute// w w  w  .  j av  a2s.c om
 * @return
 * @author s1462
 */
private Relationship getInvolvedRS(MdlAttribute attribute) {
    Iterator relIter = mdlRels_.find(attribute).iterator();
    Relationship nextRel = null;
    IBDAttachable actWidget = null;

    // fixe for mantis bug 18593
    TreeSelection actWidgetSel = (TreeSelection) widgetTreeViewer.getSelection();
    if (actWidgetSel.getFirstElement() instanceof IBDAttachable) {
        actWidget = (IBDAttachable) actWidgetSel.getFirstElement();
    }

    while (relIter.hasNext()) {
        nextRel = (Relationship) relIter.next();
        if (nextRel.bdAttachable_.equals(actWidget)) {
            break;
        }
    }
    return nextRel;
}

From source file:ch.hsr.ifs.cutelauncher.ui.CuteTestDClickListener.java

License:Open Source License

public void doubleClick(DoubleClickEvent event) {
    if (event.getSelection() instanceof TreeSelection) {
        TreeSelection treeSel = (TreeSelection) event.getSelection();
        if (treeSel.getFirstElement() instanceof TestCase) {
            TestCase tCase = (TestCase) treeSel.getFirstElement();
            if (tCase.getStatus() == TestStatus.failure) {
                openEditor(tCase.getFile(), tCase.getLineNumber(), false);
            } else {
                openEditorForNonFailingTestCase(tCase.getName());
            }/*from w  ww . j ava2  s  .c o m*/
        }
    }

}

From source file:ch.hsr.ifs.cutelauncher.ui.CuteTestSelecetionListener.java

License:Open Source License

public void selectionChanged(SelectionChangedEvent event) {
    if (event.getSelection() instanceof TreeSelection) {
        TreeSelection treeSel = (TreeSelection) event.getSelection();
        if (treeSel.getFirstElement() instanceof TestElement) {
            TestElement testElement = (TestElement) treeSel.getFirstElement();
            viewer.showTestDetails(testElement);
        }/*from  w  w w  .j  av  a2  s  . c  om*/
    }
}

From source file:ch.hsr.ifs.cutelauncher.ui.TestViewer.java

License:Open Source License

protected TestCase getTreeSelection() {
    ISelection sel = treeViewer.getSelection();
    if (sel instanceof TreeSelection) {
        TreeSelection treeSel = (TreeSelection) sel;
        if (treeSel.getFirstElement() instanceof TestCase) {
            TestCase testCase = (TestCase) treeSel.getFirstElement();
            return testCase;
        }//from   w  w w .j av a  2  s .  c  o m
    }
    return null;
}

From source file:com.abstratt.mdd.internal.ui.editors.source.SourceContentOutlinePage.java

License:Open Source License

@Override
public void createControl(Composite parent) {
    super.createControl(parent);
    viewer = getTreeViewer();/*  w  w  w.  j  a  v a  2 s  .c  om*/
    contentProvider = new TreeNodeContentProvider();
    viewer.setContentProvider(contentProvider);
    labelProvider = new TextUMLLabelProvider();
    viewer.setLabelProvider(labelProvider);
    //      disabled: used to make elements to show sorted by type        
    //      viewer.setComparator(new UIModelObjectViewerComparator());
    viewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);

    // tracks selections in the outline and reflects them in the editor
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            TreeSelection selection = (TreeSelection) event.getSelection();
            if (!selection.isEmpty()) {
                TreeNode treeNode = (TreeNode) selection.getFirstElement();
                UIModelObject model = (UIModelObject) treeNode.getValue();
                selectInEditor(model.getToken());
            }
        }
    });

    refresh();
}

From source file:com.aerospike.aql.plugin.actions.GenerateSource.java

License:Apache License

public void run(IAction action) {
    if (selection != null && selection instanceof TreeSelection) {
        TreeSelection ts = (TreeSelection) selection;
        Object element = ts.getFirstElement();
        if (element instanceof IFile && ((IFile) element).getFileExtension().equalsIgnoreCase("aql")) {
            final IFile sqlFile = (IFile) element;
            if (sqlFile == null)
                return;
            try {
                final List<String> errorList = new ArrayList<String>();
                final String actionID = action.getId();
                final AQLResult results = new AQLResult();
                // find the Aerospike console and display it
                IWorkbench wb = PlatformUI.getWorkbench();
                IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                IWorkbenchPage page = win.getActivePage();
                IConsoleView view = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
                view.display(results.getConsole());
                // set generation language
                String extension;
                final com.aerospike.aql.AQL.Language language;
                if (actionID.equals("com.aerospike.aql.plugin.actions.GenerateSource.java.popup")) {
                    language = com.aerospike.aql.AQL.Language.JAVA;
                    extension = ".java";
                } else if (actionID.equals("com.aerospike.aql.plugin.actions.GenerateSource.c.popup")) {
                    language = com.aerospike.aql.AQL.Language.C;
                    extension = ".c";
                } else if (actionID.equals("com.aerospike.aql.plugin.actions.GenerateSource.csharp.popup")) {
                    language = com.aerospike.aql.AQL.Language.CSHARP;
                    extension = ".csharp";
                } else {
                    return;
                }//from w ww.  j  av a 2  s .c om
                IProject project = sqlFile.getProject();
                IPath outputPath;
                String sqlFileName = sqlFile.getName();
                String outputFileName = sqlFileName.substring(0, sqlFileName.lastIndexOf('.')) + extension;

                final AsCluster cluster = (AsCluster) project
                        .getSessionProperty(CoreActivator.CLUSTER_PROPERTY);
                String outputDirectoryString = project
                        .getPersistentProperty(CoreActivator.AQL_GENERATION_DIRECTORY);
                if (outputDirectoryString == null || outputDirectoryString.isEmpty()) {
                    outputPath = project.getLocation().append(outputFileName);
                } else {
                    IPath dirPath = project.getLocation().append(outputDirectoryString);
                    if (!dirPath.toFile().exists())
                        dirPath.toFile().mkdirs();
                    outputPath = dirPath.append(outputFileName);
                }
                final File outputFile = outputPath.toFile();
                IPath location = sqlFile.getLocation();
                final File file = location.toFile();
                final IFile outputIFile = project.getWorkspace().getRoot().getFileForLocation(outputPath);

                Job job = new Job("Generate source code from AQL: " + sqlFile.getName()) {

                    @Override
                    protected IStatus run(IProgressMonitor monitor) {
                        AQL aql = new AQL();
                        try {
                            String seedNode = "";
                            int port = 3000;
                            if (cluster != null) {
                                seedNode = cluster.getSeedHost();
                                port = cluster.getPort();
                            } else {
                                IPreferenceStore store = CoreActivator.getDefault().getPreferenceStore();
                                seedNode = store.getString(PreferenceConstants.SEED_NODE);
                                port = store.getInt(PreferenceConstants.PORT);
                            }
                            aql.compileAndGenerate(file, outputFile, language, seedNode, port);
                            results.report("Completed generation for " + sqlFile.getName());
                            outputIFile.getParent().refreshLocal(IResource.DEPTH_ONE, null);
                            return Status.OK_STATUS;
                        } catch (Exception e) {
                            CoreActivator.showError(e,
                                    COULD_NOT_GENERATE_CODE_FROM_SQL_FILE + sqlFile.getName());
                            return Status.CANCEL_STATUS;
                        }
                    }
                };
                job.setUser(true);
                job.schedule();
            } catch (PartInitException e) {
                CoreActivator.showError(e, COULD_NOT_GENERATE_CODE_FROM_SQL_FILE + sqlFile.getName());
            } catch (CoreException e) {
                CoreActivator.showError(e, COULD_NOT_GENERATE_CODE_FROM_SQL_FILE + sqlFile.getName());
            }
        }
    }
}

From source file:com.aerospike.aql.plugin.actions.RunOnCluster.java

License:Apache License

@SuppressWarnings("unused")
public void run(IAction action) {
    if (selection != null && selection instanceof TreeSelection) {
        TreeSelection ts = (TreeSelection) selection;
        Object element = ts.getFirstElement();
        if (element instanceof IFile && ((IFile) element).getFileExtension().equalsIgnoreCase("aql")) {
            final IFile sqlFile = (IFile) element;
            if (sqlFile == null)
                return;
            final List<String> errorList = new ArrayList<String>();
            try {
                final AerospikeClient client = CoreActivator.getClient(sqlFile.getProject());
                if (client == null) {
                    CoreActivator.showError("Aerospike client is null");
                    return;
                }//from ww w  .jav  a  2  s.  c o m
                if (!client.isConnected()) {
                    CoreActivator.showError("Aerospike client is not connected");
                    return;
                }
                final ResultsConsoleView results = new ResultsConsoleView();
                // find the Aerospike console and display it
                IWorkbench wb = PlatformUI.getWorkbench();
                IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                IWorkbenchPage page = win.getActivePage();
                IConsoleView view = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
                view.display(results.getConsole());

                final File aqlFile = sqlFile.getRawLocation().makeAbsolute().toFile();

                // create and run a Job to execute the AQL
                Job job = new Job("Run AQL on cluster") {

                    @Override
                    protected IStatus run(IProgressMonitor monitor) {
                        results.report("Ecexuting AQL file: " + sqlFile.getName());
                        AQL aql = new AQL(client);
                        aql.execute(aqlFile, results, results);
                        results.report(sqlFile.getName() + " completed");
                        return Status.OK_STATUS;
                    }
                };
                job.schedule();
            } catch (CoreException e) {
                CoreActivator.showError(e, COULD_NOT_EXECUTE_SQL_FILE + sqlFile.getName());
            }
        }
    }
}

From source file:com.aerospike.udf.actions.DeregisterUDF.java

License:Apache License

/**
 * The action has been activated. The argument of the
 * method represents the 'real' action sitting
 * in the workbench UI.//from   w ww.  j  a  v a 2 s . c  o m
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
    if (selection != null && selection instanceof TreeSelection) {
        TreeSelection ts = (TreeSelection) selection;
        Object element = ts.getFirstElement();
        if (element instanceof Module) {
            Module udfModule = (Module) element;
            try {
                AerospikeClient client = CoreActivator.getClient(udfModule.getPackage());
                if (client == null) {
                    CoreActivator.log(IStatus.WARNING, "Aerospike client is null");
                    return;
                }
                final ResultsConsoleView results = new ResultsConsoleView();
                // find the Aerospike console and display it
                IWorkbench wb = PlatformUI.getWorkbench();
                IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                IWorkbenchPage page = win.getActivePage();
                IConsoleView view = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
                view.display(results.getConsole());
                Node[] nodes = client.getNodes();
                StringBuilder message = new StringBuilder();
                for (Node node : nodes) {
                    String msg = Info.request(node, "udf-remove:filename=" + udfModule.getName());
                    if (msg.contains("error")) {
                        message.append("Could not delete module: " + udfModule.getName());
                        CoreActivator.showError("Could remove UDF module: " + udfModule.getName());
                        return;
                    } else {

                        message.append("UDF ");
                        message.append(udfModule.getName());
                        message.append(" removed from:\n ");
                        message.append("\t");
                        message.append(node.getHost());
                        message.append(" ");
                        message.append(node.getName());
                        message.append("\n");
                    }
                }

                results.report(message.toString());
                MessageDialog.openInformation(shell, "Aerospike UDF", message.toString());
                ClusterRefreshJob job = new ClusterRefreshJob(udfModule.getCluster());
                job.schedule();
            } catch (CoreException e) {
                CoreActivator.showError(e, "Could not deregister UDF in file: " + udfModule.getName());
            } catch (NumberFormatException e) {
                CoreActivator.showError(e, "Could not deregister UDF in file: " + udfModule.getName());
            } catch (AerospikeException e) {
                CoreActivator.showError(e, "Could not deregister UDF in file: " + udfModule.getName());
            }

        }
    }
}

From source file:com.aerospike.udf.actions.EditRegisteredUDF.java

License:Apache License

/**
 * The action has been activated. The argument of the
 * method represents the 'real' action sitting
 * in the workbench UI./*from   w ww.ja  v a 2s.co  m*/
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
    if (selection != null && selection instanceof TreeSelection) {
        TreeSelection ts = (TreeSelection) selection;
        Object element = ts.getFirstElement();
        if (element instanceof Module) {
            Module udfModule = (Module) element;
            IProject project = udfModule.getPackage();

            /*
             * export it to the local project
             */
            IFolder udfFolder = CoreActivator.getUdfFolder(project);
            IFile udfFile = udfFolder.getFile(udfModule.getName());
            try {
                String contense = udfModule.getSource();
                InputStream stream = new ByteArrayInputStream(contense.getBytes());
                if (udfFile.exists()) {
                    udfFile.setContents(stream, true, true, null);
                } else {
                    udfFile.create(stream, true, null);
                }
                stream.close();
                udfFile.setPersistentProperty(CoreActivator.EDITED_FROM_NODE,
                        udfModule.getCluster().getRandomNodeID());
                /*
                 * Edit it
                 */
                IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                IDE.openEditor(page, udfFile);
            } catch (IOException e) {
            } catch (PartInitException e) {
                CoreActivator.showError("Could not open UDF module: " + udfModule.getName());
            } catch (CoreException e) {
                CoreActivator.showError("Could not download UDF module: " + udfModule.getName());
            }

        }
    }
}

From source file:com.aerospike.udf.actions.RegisterUDF.java

License:Apache License

/**
 * The action has been activated. The argument of the
 * method represents the 'real' action sitting
 * in the workbench UI./*  w ww . j  a va 2 s  .  c o m*/
 * @see IWorkbenchWindowActionDelegate#run
 */
public void run(IAction action) {
    if (selection != null && selection instanceof TreeSelection) {
        TreeSelection ts = (TreeSelection) selection;
        Object element = ts.getFirstElement();
        if (element instanceof IFile && ((IFile) element).getFileExtension().equalsIgnoreCase("lua")) {
            IFile udfFile = (IFile) element;
            try {
                udfFile.deleteMarkers(AEROSPIKE_UDF_PROBLEM, false, IResource.DEPTH_ONE);
                AerospikeClient client = CoreActivator.getClient(udfFile.getProject());
                if (client == null) {
                    CoreActivator.log(IStatus.WARNING, "Aerospike client is null");
                    return;
                }
                ClientPolicy policy = new ClientPolicy();
                policy.failIfNotConnected = true;
                final ResultsConsoleView results = new ResultsConsoleView();
                // find the Aerospike console and display it
                IWorkbench wb = PlatformUI.getWorkbench();
                IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
                IWorkbenchPage page = win.getActivePage();
                IConsoleView view = (IConsoleView) page.showView(IConsoleConstants.ID_CONSOLE_VIEW);
                view.display(results.getConsole());

                RegisterTask task = client.register(new Policy(), udfFile.getRawLocation().toOSString(),
                        udfFile.getName(), Language.LUA); //UDF language
                task.waitTillComplete();
                Node[] nodes = client.getNodes();
                StringBuilder message = new StringBuilder();
                message.append("UDF ");
                message.append(udfFile.getName());
                message.append("  registered successfully on nodes:\n ");

                for (Node node : nodes) {
                    message.append("\t");
                    message.append(node.getHost());
                    message.append(" ");
                    message.append(node.getName());
                    message.append("\n");
                }
                results.report(message.toString());
                MessageDialog.openInformation(shell, "Aerospike UDF", message.toString());
                udfFile.setPersistentProperty(CoreActivator.UDF_REGISTERED, "true");
                ClusterRefreshJob job = new ClusterRefreshJob(udfFile.getProject());
                job.schedule();
            } catch (CoreException e) {
                CoreActivator.showError(e, "Could not register UDF in file: " + udfFile.getName());
            } catch (NumberFormatException e) {
                CoreActivator.showError(e, "Could not register UDF in file: " + udfFile.getName());
            } catch (AerospikeException e) {
                String message = e.getMessage();
                if (message.contains("compile_error")) {
                    /*
                     * Registration failed: compile_error
                     * File: bad.lua
                     * Line: 3
                     * Message: null
                     */
                    Matcher matcher = udfErrorPattern.matcher(message);
                    if (matcher.matches()) {
                        String error = matcher.group(2);
                        int line = Integer.parseInt(matcher.group(1));
                        addMarker(udfFile, error, line);
                    }
                    MessageDialog.openInformation(shell, "Aerospike UDF", message);
                } else {
                    CoreActivator.showError(e, "Could not register UDF in file: " + udfFile.getName());
                }
            }

        }
    }
}