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:com.clustercontrol.infra.view.action.ReviewCheckStatusAction.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    this.window = HandlerUtil.getActiveWorkbenchWindow(event);
    // In case this action has been disposed
    if (null == this.window || !isEnabled()) {
        return null;
    }//from w  w w. j ava2s.  c  om

    // ???
    this.viewPart = HandlerUtil.getActivePart(event);

    if (!(viewPart instanceof InfraModuleView)) {
        return null;
    }

    InfraModuleView infraModuleView = null;
    try {
        infraModuleView = (InfraModuleView) viewPart.getAdapter(InfraModuleView.class);
    } catch (Exception e) {
        m_log.info("execute " + e.getMessage());
        return null;
    }

    if (infraModuleView == null) {
        m_log.info("execute: view is null");
        return null;
    }

    StructuredSelection selection = null;
    if (infraModuleView.getComposite().getTableViewer().getSelection() instanceof StructuredSelection) {
        selection = (StructuredSelection) infraModuleView.getComposite().getTableViewer().getSelection();
    }

    String managerName = infraModuleView.getComposite().getManagerName();
    InfraManagementInfo management = null;

    try {
        InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(managerName);
        management = wrapper.getInfraManagement(infraModuleView.getComposite().getManagementId());
    } catch (InvalidRole_Exception e) {
        // ???
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.accesscontrol.16"));
        return null;
    } catch (HinemosUnknown_Exception | InvalidUserPass_Exception | NotifyNotFound_Exception
            | InfraManagementNotFound_Exception e) {
        m_log.error("execute() : " + e.getClass().getName() + ", " + e.getMessage());
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.infra.action.result",
                        new Object[] { Messages.getString("infra.module.id"),
                                Messages.getString("infra.module.check"), Messages.getString("failed"),
                                HinemosMessage.replace(e.getMessage()) }));
        return null;
    }

    if (null == selection || null == management) {
        return null;
    }

    String moduleId = (String) ((ArrayList<?>) selection.getFirstElement())
            .get(GetInfraModuleTableDefine.MODULE_ID);
    if (null == moduleId) {
        return null;
    }

    TextAreaDialog dialog = new TextAreaDialog(null, Messages.getString("infra.management.check.state"), false,
            false);
    dialog.setText(getStatusString(managerName, management.getManagementId(), moduleId));
    dialog.setCancelButtonText(Messages.getString("close"));
    dialog.open();

    return null;
}

From source file:com.clustercontrol.infra.view.action.ReviewCheckStatusAction.java

License:Open Source License

@Override
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
    boolean enable = false;
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    // page may not start at state restoring
    if (null != window) {
        IWorkbenchPage page = window.getActivePage();
        if (null != page) {
            IWorkbenchPart part = page.getActivePart();

            if (part instanceof InfraModuleView) {
                InfraModuleView view = (InfraModuleView) part.getAdapter(InfraModuleView.class);
                if (view == null) {
                    m_log.info("execute: view is null");
                    return;
                }//w  w w.ja  v a 2  s . c om
                // Enable button when 1 item is selected
                StructuredSelection selection = null;
                if (view.getComposite().getTableViewer().getSelection() instanceof StructuredSelection) {
                    selection = (StructuredSelection) view.getComposite().getTableViewer().getSelection();
                }
                if (selection != null && 1 == selection.size()) {
                    // Only enable when result exists (= String length > 0 )
                    String result = (String) ((ArrayList<?>) selection.getFirstElement())
                            .get(GetInfraModuleTableDefine.CHECK_CONDITION);
                    if (null != result) {
                        enable = true;
                    }
                }
            }
            this.setBaseEnabled(enable);
        } else {
            this.setBaseEnabled(false);
        }
    }
}

From source file:com.clustercontrol.infra.view.action.RunInfraModuleAction.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    this.window = HandlerUtil.getActiveWorkbenchWindow(event);
    // In case this action has been disposed
    if (null == this.window || !isEnabled()) {
        return null;
    }//w  w w.  j av  a2  s .  c o  m

    // ???
    this.viewPart = HandlerUtil.getActivePart(event);
    if (!(viewPart instanceof InfraModuleView)) {
        return null;
    }

    InfraModuleView infraModuleView = null;
    try {
        infraModuleView = (InfraModuleView) viewPart.getAdapter(InfraModuleView.class);
    } catch (Exception e) {
        m_log.info("execute " + e.getMessage());
        return null;
    }
    if (infraModuleView == null) {
        m_log.info("execute: view is null");
        return null;
    }

    StructuredSelection selection = null;
    if (infraModuleView.getComposite().getTableViewer().getSelection() instanceof StructuredSelection) {
        selection = (StructuredSelection) infraModuleView.getComposite().getTableViewer().getSelection();
    }

    InfraManagementInfo management = null;
    String managerName = infraModuleView.getComposite().getManagerName();
    try {
        InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(managerName);
        management = wrapper.getInfraManagement(infraModuleView.getComposite().getManagementId());
    } catch (InvalidRole_Exception e) {
        // ???
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.accesscontrol.16"));
        return null;
    } catch (HinemosUnknown_Exception | InvalidUserPass_Exception | NotifyNotFound_Exception
            | InfraManagementNotFound_Exception e) {
        m_log.error("execute() : " + e.getClass().getName() + ", " + e.getMessage());
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.infra.action.result",
                        new Object[] { Messages.getString("infra.module.id"),
                                Messages.getString("infra.module.run"), Messages.getString("failed"),
                                HinemosMessage.replace(e.getMessage()) }));
        return null;
    }

    if (selection == null || management == null) {
        return null;
    }
    String moduleId = (String) ((ArrayList<?>) selection.getFirstElement())
            .get(GetInfraModuleTableDefine.MODULE_ID);

    boolean allRun = false;
    RunDialog dialog = new RunDialog(null, Messages.getString("message.infra.confirm.action", new Object[] {
            Messages.getString("infra.module.id"), Messages.getString("infra.module.run"), moduleId }));
    if (dialog.open() == IDialogConstants.CANCEL_ID) {
        return null;
    }
    allRun = dialog.isAllRun();

    List<AccessInfo> accessInfoList = AccessUtil.getAccessInfoList(viewPart.getSite().getShell(),
            management.getFacilityId(), management.getOwnerRoleId(), managerName,
            infraModuleView.isUseNodeProp());
    // ?????????null?????
    // ???????
    if (accessInfoList == null) {
        return null;
    }

    // module??
    InfraModuleInfo module = null;
    for (InfraModuleInfo m : management.getModuleList()) {
        if (moduleId.equals(m.getModuleId())) {
            module = m;
            break;
        }
    }
    if (module == null) {
        return null;
    }

    List<String> moduleIdList = new ArrayList<String>();
    moduleIdList.add(moduleId);
    try {
        InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(managerName);
        String sessionId = wrapper.createSession(management.getManagementId(), moduleIdList, accessInfoList);
        while (true) {
            ModuleResult moduleResult = wrapper.runInfraModule(sessionId);
            if (!allRun && !ModuleUtil.displayResult(moduleResult.getModuleId(), moduleResult)) {
                break;
            }
            if (!moduleResult.isHasNext()) {
                break;
            }
        }
        wrapper.deleteSession(sessionId);
        MessageDialog.openInformation(null, Messages.getString("message"),
                Messages.getString("message.infra.management.run.end"));
    } catch (InvalidRole_Exception e) {
        // ???
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.accesscontrol.16"));
        return null;
    } catch (HinemosUnknown_Exception | InvalidUserPass_Exception | InfraManagementNotFound_Exception
            | InfraModuleNotFound_Exception | SessionNotFound_Exception | FacilityNotFound_Exception
            | InvalidSetting_Exception e) {
        m_log.error("execute() :  " + e.getClass().getName() + ", " + e.getMessage());
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.infra.action.result",
                        new Object[] { Messages.getString("infra.module.id"),
                                Messages.getString("infra.module.run"), Messages.getString("failed"),
                                HinemosMessage.replace(e.getMessage()) }));
        return null;
    } catch (Exception e) {
        m_log.error("execute() :  " + e.getClass().getName() + ", " + e.getMessage());
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.infra.action.result",
                        new Object[] { Messages.getString("infra.module.id"),
                                Messages.getString("infra.module.run"), Messages.getString("failed"),
                                HinemosMessage.replace(e.getMessage()) }));
        return null;
    }

    infraModuleView.update(infraModuleView.getComposite().getManagerName(),
            infraModuleView.getComposite().getManagementId());
    return null;
}

From source file:com.clustercontrol.infra.view.action.UpOrderInfraModuleAction.java

License:Open Source License

@Override
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
    boolean enable = false;
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    // page may not start at state restoring
    if (null != window) {
        IWorkbenchPage page = window.getActivePage();
        if (null != page) {
            IWorkbenchPart part = page.getActivePart();

            if (part instanceof InfraModuleView) {
                InfraModuleView view = (InfraModuleView) part.getAdapter(InfraModuleView.class);
                if (view == null) {
                    m_log.info("execute: view is null");
                    return;
                }//ww  w  . java 2 s  .  c  o m
                // Enable button when 1 item is selected
                StructuredSelection selection = null;
                if (view.getComposite().getTableViewer().getSelection() instanceof StructuredSelection) {
                    selection = (StructuredSelection) view.getComposite().getTableViewer().getSelection();
                }
                if (selection != null && selection.size() == 1
                        && view.getComposite().getTable().getItemCount() > 1
                        && (int) ((ArrayList<?>) selection.getFirstElement())
                                .get(GetInfraModuleTableDefine.ORDER_NO) > 1) {
                    enable = true;
                }
            }
            this.setBaseEnabled(enable);
        } else {
            this.setBaseEnabled(false);
        }
    }
}

From source file:com.clustercontrol.infra.view.InfraModuleView.java

License:Open Source License

/**
 * Move an module order up/*  w  ww .  j av  a  2 s.c om*/
 */
public void moveUp() {
    TableViewer tableViewer = getComposite().getTableViewer();

    StructuredSelection selection = null;
    if (tableViewer.getSelection() instanceof StructuredSelection) {
        selection = (StructuredSelection) tableViewer.getSelection();
    }

    String moduleId = null;
    if (selection != null) {
        moduleId = (String) ((ArrayList<?>) selection.getFirstElement())
                .get(GetInfraModuleTableDefine.MODULE_ID);
    }

    int selectionIndex = getComposite().getTable().getSelectionIndex();
    String managerName = getComposite().getManagerName();
    String managementId = getComposite().getManagementId();

    InfraManagementInfo management = null;
    InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(managerName);
    try {
        management = wrapper.getInfraManagement(managementId);
    } catch (HinemosUnknown_Exception | InvalidRole_Exception | InvalidUserPass_Exception
            | NotifyNotFound_Exception | InfraManagementNotFound_Exception e) {
        m_log.debug("execute getInfraManagement, " + e.getMessage());
    }

    InfraModuleInfo module = null;
    if (management != null && management.getModuleList() != null) {
        for (InfraModuleInfo tmpModule : management.getModuleList()) {
            if (tmpModule.getModuleId().equals(moduleId)) {
                module = tmpModule;
                break;
            }
        }

        if (module != null) {
            int index = management.getModuleList().indexOf(module);
            if (index > 0) {
                InfraModuleInfo tmpModule = management.getModuleList().get(index - 1);
                management.getModuleList().set(index, tmpModule);
                management.getModuleList().set(index - 1, module);

                try {
                    wrapper.modifyInfraManagement(management);
                } catch (HinemosUnknown_Exception | InvalidRole_Exception | InvalidUserPass_Exception
                        | InvalidSetting_Exception | NotifyDuplicate_Exception | NotifyNotFound_Exception
                        | InfraManagementNotFound_Exception | InfraManagementDuplicate_Exception e) {
                    m_log.debug("execute modifyInfraManagement, " + e.getMessage());
                }
            }
        }

        update(managerName, managementId);

        // ??????
        selectItem(selectionIndex - 1);
    }
}

From source file:com.clustercontrol.infra.view.InfraModuleView.java

License:Open Source License

/**
 * Move an module order down//from   w ww  . ja  va 2 s  .c o  m
 */
public void moveDown() {
    TableViewer tableViewer = getComposite().getTableViewer();

    StructuredSelection selection = null;
    if (tableViewer.getSelection() instanceof StructuredSelection) {
        selection = (StructuredSelection) tableViewer.getSelection();
    }

    String moduleId = null;
    if (selection != null) {
        moduleId = (String) ((ArrayList<?>) selection.getFirstElement())
                .get(GetInfraModuleTableDefine.MODULE_ID);
    }

    int selectionIndex = getComposite().getTable().getSelectionIndex();
    String managerName = getComposite().getManagerName();
    String managementId = getComposite().getManagementId();

    InfraManagementInfo management = null;
    InfraEndpointWrapper wrapper = InfraEndpointWrapper.getWrapper(managerName);
    try {
        management = wrapper.getInfraManagement(managementId);
    } catch (HinemosUnknown_Exception | InvalidRole_Exception | InvalidUserPass_Exception
            | NotifyNotFound_Exception | InfraManagementNotFound_Exception e) {
        m_log.debug("execute getInfraManagement, " + e.getMessage());
    }

    InfraModuleInfo module = null;
    if (management != null && management.getModuleList() != null) {
        for (InfraModuleInfo tmpModule : management.getModuleList()) {
            if (tmpModule.getModuleId().equals(moduleId)) {
                module = tmpModule;
                break;
            }
        }

        if (module != null) {
            int index = management.getModuleList().indexOf(module);
            if (index < management.getModuleList().size() - 1) {
                InfraModuleInfo tmpModule = management.getModuleList().get(index + 1);
                management.getModuleList().set(index, tmpModule);
                management.getModuleList().set(index + 1, module);

                try {
                    wrapper.modifyInfraManagement(management);
                } catch (HinemosUnknown_Exception | InvalidRole_Exception | InvalidUserPass_Exception
                        | InvalidSetting_Exception | NotifyDuplicate_Exception | NotifyNotFound_Exception
                        | InfraManagementNotFound_Exception | InfraManagementDuplicate_Exception e) {
                    m_log.debug("execute modifyInfraManagement, " + e.getMessage());
                }
            }
        }

        update(managerName, managementId);

        // ??????
        selectItem(selectionIndex + 1);
    }
}

From source file:com.clustercontrol.jobmanagement.composite.action.JobDoubleClickListener.java

License:Open Source License

/**
 * ?????<BR>/*from   ww w  .  j  a  va  2s  .  c o m*/
 * []?????????????
 * <P>
 * <ol>
 * <li>???????ID????</li>
 * <li>[]???????</li>
 * <li>?????ID??????</li>
 * <li>[]????ID?????</li>
 * <li>ID????????</li>
 * </ol>
 *
 * @param event 
 *
 * @see com.clustercontrol.jobmanagement.dialog.JobDialog
 * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
 */
@Override
public void doubleClick(DoubleClickEvent event) {
    JobTreeItem selectJobTreeItem = null;

    //[]??
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IViewPart viewPart = page.findView(JobListView.ID);

    //??
    StructuredSelection selection = (StructuredSelection) event.getSelection();

    if (viewPart != null && selection != null) {
        ArrayList<?> item = (ArrayList<?>) selection.getFirstElement();
        JobListView view = (JobListView) viewPart.getAdapter(JobListView.class);
        if (view == null) {
            m_log.info("selection changed: job list view is null");
            return;
        }

        if (item != null) {
            String jobId = (String) item.get(GetJobTableDefine.JOB_ID);

            if (m_composite.getJobTreeItem() != null) {
                List<JobTreeItem> items = m_composite.getJobTreeItem().getChildren();

                for (int i = 0; i < items.size(); i++) {
                    if (jobId.equals(items.get(i).getData().getId())) {
                        selectJobTreeItem = items.get(i);
                        break;
                    }
                }
            }
        }

        if (selectJobTreeItem != null) {
            //?
            List<JobTreeItem> list = new ArrayList<JobTreeItem>();
            list.add(selectJobTreeItem);
            m_composite.setSelectJobTreeItemList(list);

            String managerName = null;
            JobTreeItem mgrTree = JobTreeItemUtil.getManager(selectJobTreeItem);
            if (mgrTree == null) {
                managerName = selectJobTreeItem.getChildren().get(0).getData().getId();
            } else {
                managerName = mgrTree.getData().getId();
            }
            JobEditState jobEditState = JobEditStateUtil.getJobEditState(managerName);
            boolean readOnly = !jobEditState.isLockedJobunitId(selectJobTreeItem.getData().getJobunitId());

            // ?
            JobDialog dialog = new JobDialog(view.getJobTreeComposite(), m_composite.getShell(), managerName,
                    readOnly);

            // ???????????
            dialog.setJobTreeItem(selectJobTreeItem);
            m_log.info("jobDialog " + selectJobTreeItem.getData().getId());
            //
            if (dialog.open() == IDialogConstants.OK_ID) {
                m_composite.update(selectJobTreeItem.getParent());
                if (jobEditState.isLockedJobunitId(selectJobTreeItem.getData().getJobunitId())) {
                    // ?????(???????
                    jobEditState.addEditedJobunit(selectJobTreeItem);
                    if (selectJobTreeItem.getData().getType() == JobConstant.TYPE_JOBUNIT) {
                        JobUtil.setJobunitIdAll(selectJobTreeItem, selectJobTreeItem.getData().getJobunitId());
                    }
                }

                // Refresh after modified
                // @see ModifyJobAction#run()
                JobTreeItem parent = selectJobTreeItem.getParent();
                JobTreeComposite tree = view.getJobTreeComposite();
                tree.getTreeViewer().sort(parent);
                tree.refresh(item);
                // Also fresh parent in case of selecting from list
                tree.refresh(parent);
            }
        }
    }
}

From source file:com.clustercontrol.jobmanagement.composite.action.JobKickSelectionChangedListener.java

License:Open Source License

/**
 * ??????/*ww  w  . j a  va 2  s .co m*/
 * <P>
 * <ol>
 * <li>???????</li>
 * <li>?[]??????</li>
 * </ol>
 *
 * @param event ?
 *
 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
 */
@Override
public void selectionChanged(SelectionChangedEvent event) {
    ArrayList<ArrayList<?>> info = new ArrayList<ArrayList<?>>();
    List<?> list;

    //[]??
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IViewPart viewPart = page.findView(JobKickListView.ID);

    //??
    StructuredSelection selection = (StructuredSelection) event.getSelection();

    if (selection.getFirstElement() != null) {
        list = ((StructuredSelection) event.getSelection()).toList();
        for (Object obj : list) {
            if (obj instanceof ArrayList) {
                info.add((ArrayList<?>) obj);
            }
        }

        if (viewPart != null) {
            JobKickListView view = (JobKickListView) viewPart.getAdapter(JobKickListView.class);
            if (view == null) {
                m_log.info("selection changed: view is null");
                return;
            }
            //???/?????/???
            view.setEnabledAction(selection.size(), selection);
        }
    } else {
        if (viewPart != null) {
            JobKickListView view = (JobKickListView) viewPart.getAdapter(JobKickListView.class);
            if (view == null) {
                m_log.info("selection changed: view is null");
                return;
            }
            //???/?????/???
            view.setEnabledAction(0, selection);
        }
    }

    m_composite.setSelectItem(info);
}

From source file:com.clustercontrol.jobmanagement.composite.action.JobTreeSelectionChangedListener.java

License:Open Source License

/**
 * ??????<BR>/*from  ww w  .  j  av a  2  s .  c om*/
 * []??????<BR>
 * ?????[]???<BR>
 * ?????[]?????
 * <P>
 * <ol>
 * <li>??????</li>
 * <li>[]????????</li>
 * <li>[]?????</li>
 * <li>[]??????</li>
 * </ol>
 *
 * @param event ?
 *
 * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
 */
@Override
public void selectionChanged(SelectionChangedEvent event) {
    JobTreeItem selectItem = null;

    //[]??
    IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IViewPart viewPart = page.findView(JobListView.ID);

    //??
    StructuredSelection selection = (StructuredSelection) event.getSelection();

    if (viewPart != null && selection != null) {
        selectItem = (JobTreeItem) selection.getFirstElement();
        List<?> list = selection.toList();
        List<JobTreeItem> itemList = new ArrayList<JobTreeItem>();
        for (Object obj : list) {
            if (obj instanceof JobTreeItem) {
                itemList.add((JobTreeItem) obj);
            }
        }

        JobListView view = (JobListView) viewPart.getAdapter(JobListView.class);

        if (view == null) {
            m_log.info("selection changed: view is null");
            return;
        }

        // Set last focus
        JobTreeComposite composite = view.getJobTreeComposite();
        if (composite != null && composite.getTree().isFocusControl()) {
            view.setLastFocusComposite(composite);
        }

        view.setEnabledAction(selectItem, itemList, true);
    }

}

From source file:com.clustercontrol.jobmanagement.composite.JobTreeComposite.java

License:Open Source License

public void doSearch(String keyword) {
    // Check and format keyword
    if (null == keyword) {
        return;/*www.  j  av a 2 s  .co m*/
    }
    keyword = keyword.trim();
    if (keyword.isEmpty()) {
        return;
    }

    StructuredSelection selection = (StructuredSelection) m_viewer.getSelection();
    Object targetItem = selection.getFirstElement();
    JobTreeItem result = searchItem((JobTreeItem) (null != targetItem ? targetItem : m_viewer.getInput()),
            keyword);
    if (null != result) {
        JobTreeItem trace = result;
        LinkedList<JobTreeItem> pathList = new LinkedList<>();
        do {
            pathList.addFirst(trace);
            trace = trace.getParent();
        } while (null != trace);
        TreePath path = new TreePath(pathList.toArray(new JobTreeItem[] {}));
        m_viewer.setSelection(new TreeSelection(path), true);
    } else {
        MessageDialog.openInformation(this.getShell(), Messages.getString("message"),
                Messages.getString("search.not.found"));
        m_viewer.setSelection(new StructuredSelection(((JobTreeItem) m_viewer.getInput()).getChildren().get(0)),
                true);
    }
}