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

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

Introduction

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

Prototype

@Override
    public Object getFirstElement() 

Source Link

Usage

From source file:org.apache.directory.studio.ldapbrowser.ui.actions.GotoDnNavigateMenuAction.java

License:Apache License

/**
 * Gets the currently selected connection.
 *
 * @return the currently selected connection
 *//*from w w  w . j a v a2 s .c o  m*/
private Connection getSelectedConnection() {
    // Getting the connections view
    ConnectionView connectionView = (ConnectionView) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
            .getActivePage().findView(ConnectionView.getId());

    if (connectionView != null) {
        // Getting the selection of the connections view
        StructuredSelection selection = (StructuredSelection) connectionView.getMainWidget().getViewer()
                .getSelection();

        // Checking if only one object is selected
        if (selection.size() == 1) {
            Object selectedObject = selection.getFirstElement();

            // Checking if the selected object is a connection
            if (selectedObject instanceof Connection) {
                return (Connection) selectedObject;
            }
        }
    }

    return null;
}

From source file:org.apache.directory.studio.ldapbrowser.ui.dialogs.preferences.EntryEditorsPreferencePage.java

License:Apache License

/**
 * Moves the currently selected entry editor.
 *
 * @param direction/* w ww.ja  va2s  .c om*/
 *      the direction (up or down)
 */
private void moveSelectedEntryEditor(MoveEntryEditorDirectionEnum direction) {
    StructuredSelection selection = (StructuredSelection) entryEditorsTableViewer.getSelection();
    if (selection.size() == 1) {
        EntryEditorExtension entryEditor = (EntryEditorExtension) selection.getFirstElement();
        if (sortedEntryEditorsList.contains(entryEditor)) {
            int oldIndex = sortedEntryEditorsList.indexOf(entryEditor);
            int newIndex = 0;

            // Determining the new index number
            switch (direction) {
            case UP:
                newIndex = oldIndex - 1;
                break;
            case DOWN:
                newIndex = oldIndex + 1;
                break;
            }

            // Checking bounds
            if ((newIndex >= 0) && (newIndex < sortedEntryEditorsList.size())) {
                // Switching the two entry editors
                EntryEditorExtension newIndexEntryEditorBackup = sortedEntryEditorsList.set(newIndex,
                        entryEditor);
                sortedEntryEditorsList.set(oldIndex, newIndexEntryEditorBackup);

                // Reloading the viewer
                entryEditorsTableViewer.refresh();

                // Updating the state of the buttons
                updateButtonsState(entryEditor);

                // Setting the "Use User Priority" to true
                useUserPriority = true;
            }
        }
    }
}

From source file:org.apache.directory.studio.ldapbrowser.ui.dialogs.preferences.EntryEditorsPreferencePage.java

License:Apache License

/**
 * Updates the state of the buttons.//  w  ww .  j a v a2s.c  om
 */
private void updateButtonsState() {
    StructuredSelection selection = (StructuredSelection) entryEditorsTableViewer.getSelection();
    if (selection.size() == 1) {
        EntryEditorExtension entryEditor = (EntryEditorExtension) selection.getFirstElement();

        // Updating the state of the buttons
        updateButtonsState(entryEditor);
    }
}

From source file:org.apache.directory.studio.ldapbrowser.ui.views.browser.BrowserView.java

License:Apache License

/**
 * {@inheritDoc}/*  w  w  w. j  a  v a2  s  . c om*/
 */
public Object getAdapter(Class required) {
    if (IShowInTarget.class.equals(required)) {
        return new IShowInTarget() {
            public boolean show(ShowInContext context) {
                StructuredSelection selection = (StructuredSelection) context.getSelection();
                Object obj = selection.getFirstElement();
                if (obj instanceof IValue) {
                    IValue value = (IValue) obj;
                    IEntry entry = value.getAttribute().getEntry();
                    // The entry may be a clone, lookup original entry from entry cache.
                    // The result may be null, in that case the selection won't change.
                    entry = entry.getBrowserConnection().getEntryFromCache(entry.getDn());
                    select(entry);
                } else if (obj instanceof IAttribute) {
                    IAttribute attribute = (IAttribute) obj;
                    IEntry entry = attribute.getEntry();
                    // The entry may be a clone, lookup original entry from entry cache.
                    // The result may be null, in that case the selection won't change.
                    entry = entry.getBrowserConnection().getEntryFromCache(entry.getDn());
                    select(entry);
                } else if (obj instanceof ISearchResult) {
                    ISearchResult sr = (ISearchResult) obj;
                    ISearch search = sr.getSearch();
                    // The search may be a clone, lookup original search from search manager.
                    // The result may be null, in that case the selection won't change.
                    search = search.getBrowserConnection().getSearchManager().getSearch(search.getName());
                    select(search);
                }
                return true;
            }
        };
    }

    return null;
}

From source file:org.apache.directory.studio.ldapservers.actions.DeleteAction.java

License:Apache License

/**
 * {@inheritDoc}/*from w  ww  . j  a  va 2  s . c om*/
 */
public void run() {
    if (view != null) {
        // What we get from the TableViewer is a StructuredSelection
        StructuredSelection selection = (StructuredSelection) view.getViewer().getSelection();

        // Here's the real object
        LdapServer server = (LdapServer) selection.getFirstElement();

        // Asking for confirmation
        DeleteServerDialog dsd = new DeleteServerDialog(view.getSite().getShell(), server);
        if (dsd.open() == DeleteServerDialog.OK) {
            // Creating and scheduling the job to delete the server
            StudioLdapServerJob job = new StudioLdapServerJob(new DeleteLdapServerRunnable(server));
            job.schedule();
        }
    }
}

From source file:org.apache.directory.studio.ldapservers.actions.OpenConfigurationAction.java

License:Apache License

/**
 * {@inheritDoc}/*from   w  w  w .  ja v  a  2 s .  c o m*/
 */
public void run() {
    if (view != null) {
        // Getting the selection
        StructuredSelection selection = (StructuredSelection) view.getViewer().getSelection();
        if ((!selection.isEmpty()) && (selection.size() == 1)) {
            // Getting the server
            LdapServer server = (LdapServer) selection.getFirstElement();

            // Creating and scheduling the job to start the server
            StudioLdapServerJob job = new StudioLdapServerJob(new OpenConfigurationLdapServerRunnable(server));
            job.schedule();
        }
    }
}

From source file:org.apache.directory.studio.ldapservers.actions.PropertiesAction.java

License:Apache License

/**
 * {@inheritDoc}/*w  ww.  j  a  va  2  s.  c o m*/
 */
public void run() {
    if (view != null) {
        StructuredSelection selection = (StructuredSelection) view.getViewer().getSelection();
        if (!selection.isEmpty()) {
            LdapServer server = (LdapServer) selection.getFirstElement();
            PreferenceDialog dialog = PreferencesUtil.createPropertyDialogOn(view.getViewSite().getShell(),
                    server, LdapServersPluginConstants.PROP_SERVER_PROPERTY_PAGE, null, null);
            dialog.getShell().setText(NLS.bind(Messages.getString("PropertiesAction.PropertiesFor"), //$NON-NLS-1$
                    shorten(server.getName(), 30)));
            dialog.open();
        }
    }
}

From source file:org.apache.directory.studio.ldapservers.actions.RenameAction.java

License:Apache License

/**
 * {@inheritDoc}/* w  ww .j  a  va 2  s  .  c  o  m*/
 */
public void run() {
    if (view != null) {
        // Getting the selected server
        StructuredSelection selection = (StructuredSelection) view.getViewer().getSelection();
        final LdapServer server = (LdapServer) selection.getFirstElement();

        if (server != null) {
            IInputValidator validator = new IInputValidator() {
                public String isValid(String newName) {
                    if (server.getName().equals(newName)) {
                        return null;
                    } else if (!LdapServersManager.getDefault().isNameAvailable(newName)) {
                        return Messages.getString("RenameAction.ErrorNameInUse"); //$NON-NLS-1$
                    } else {
                        return null;
                    }
                }
            };

            // Opening a dialog to ask the user a new name for the server
            InputDialog dialog = new InputDialog(view.getSite().getShell(),
                    Messages.getString("RenameAction.RenameServer"), //$NON-NLS-1$ 
                    Messages.getString("RenameAction.NewName"), //$NON-NLS-1$ 
                    server.getName(), validator);
            dialog.open();

            String newName = dialog.getValue();
            if (newName != null) {
                server.setName(newName);
            }
        }
    }
}

From source file:org.apache.directory.studio.ldapservers.actions.StartAction.java

License:Apache License

/**
 * {@inheritDoc}/*from  w  ww.  j a v  a2s .c om*/
 */
public void run() {
    if (view != null) {
        // Getting the selection
        StructuredSelection selection = (StructuredSelection) view.getViewer().getSelection();
        if ((!selection.isEmpty()) && (selection.size() == 1)) {
            // Getting the server
            LdapServer server = (LdapServer) selection.getFirstElement();

            LdapServerAdapterExtension ldapServerAdapterExtension = server.getLdapServerAdapterExtension();
            if ((ldapServerAdapterExtension != null) && (ldapServerAdapterExtension.getInstance() != null)) {
                LdapServerAdapter ldapServerAdapter = ldapServerAdapterExtension.getInstance();

                try {

                    // Getting the ports already in use
                    String[] portsAlreadyInUse = ldapServerAdapter.checkPortsBeforeServerStart(server);
                    if ((portsAlreadyInUse == null) || (portsAlreadyInUse.length > 0)) {
                        String title = null;
                        String message = null;

                        if (portsAlreadyInUse.length == 1) {
                            title = Messages.getString("StartAction.PortInUse"); //$NON-NLS-1$
                            message = NLS.bind(Messages.getString("StartAction.PortOfProtocolInUse"), //$NON-NLS-1$
                                    new String[] { portsAlreadyInUse[0] });
                        } else {
                            title = Messages.getString("StartAction.PortsInUse"); //$NON-NLS-1$
                            message = Messages.getString("StartAction.PortsOfProtocolsInUse"); //$NON-NLS-1$
                            for (String portAlreadyInUse : portsAlreadyInUse) {
                                message += "\n    - " + portAlreadyInUse; //$NON-NLS-1$
                            }
                        }

                        message += "\n\n" + Messages.getString("StartAction.Continue"); //$NON-NLS-1$ //$NON-NLS-2$

                        MessageDialog dialog = new MessageDialog(view.getSite().getShell(), title, null,
                                message, MessageDialog.WARNING,
                                new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL },
                                MessageDialog.OK);
                        if (dialog.open() == MessageDialog.CANCEL) {
                            return;
                        }
                    }

                    // Creating and scheduling the job to start the server
                    StudioLdapServerJob job = new StudioLdapServerJob(new StartLdapServerRunnable(server));
                    job.schedule();
                } catch (Exception e) {
                    // Showing an error in case no LDAP Server Adapter can be found
                    MessageDialog.openError(view.getSite().getShell(),
                            Messages.getString("StartAction.ErrorStartingServer"), //$NON-NLS-1$
                            NLS.bind(
                                    Messages.getString("StartAction.ServerCanNotBeStarted") + "\n" //$NON-NLS-1$//$NON-NLS-2$
                                            + Messages.getString("StartAction.Cause"), //$NON-NLS-1$
                                    server.getName(), e.getMessage()));
                }
            } else {
                // Showing an error in case no LDAP Server Adapter can be found
                MessageDialog.openError(view.getSite().getShell(),
                        Messages.getString("StartAction.NoLdapServerAdapter"), //$NON-NLS-1$
                        NLS.bind(Messages.getString("StartAction.ServerCanNotBeStarted") + "\n" //$NON-NLS-1$ //$NON-NLS-2$
                                + Messages.getString("StartAction.NoLdapServerAdapterCouldBeFound"), //$NON-NLS-1$
                                server.getName()));
            }
        }
    }
}

From source file:org.apache.directory.studio.ldapservers.actions.StopAction.java

License:Apache License

/**
 * {@inheritDoc}//from w  w  w . ja  v  a2s.  c  om
 */
public void run() {
    if (view != null) {
        // Getting the selection
        StructuredSelection selection = (StructuredSelection) view.getViewer().getSelection();
        if ((!selection.isEmpty()) && (selection.size() == 1)) {
            // Getting the server
            LdapServer server = (LdapServer) selection.getFirstElement();

            // Creating and scheduling the job to stop the server
            StudioLdapServerJob job = new StudioLdapServerJob(new StopLdapServerRunnable(server));
            job.schedule();
        }
    }
}