com.clustercontrol.repository.view.ScopeListView.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.repository.view.ScopeListView.java

Source

/**********************************************************************
 * Copyright (C) 2014 NTT DATA Corporation
 * This program is free software; you can redistribute it and/or
 * Modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *********************************************************************/

package com.clustercontrol.repository.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;

import com.clustercontrol.util.Messages;
import com.clustercontrol.util.WidgetTestUtil;
import com.clustercontrol.ClusterControlPlugin;
import com.clustercontrol.accesscontrol.util.ClientSession;
import com.clustercontrol.accesscontrol.util.ObjectBean;
import com.clustercontrol.bean.HinemosModuleConstant;
import com.clustercontrol.composite.FacilityTreeComposite;
import com.clustercontrol.repository.FacilityPath;
import com.clustercontrol.repository.composite.ScopeListComposite;
import com.clustercontrol.repository.composite.action.FacilityTreeSelectionChangedListener;
import com.clustercontrol.repository.composite.action.ScopeListSelectionChangedListener;
import com.clustercontrol.repository.util.ScopePropertyUtil;
import com.clustercontrol.repository.view.action.NodeAssignAction;
import com.clustercontrol.repository.view.action.NodeReleaseAction;
import com.clustercontrol.repository.view.action.RefreshAction;
import com.clustercontrol.repository.view.action.ScopeAddAction;
import com.clustercontrol.repository.view.action.ScopeDeleteAction;
import com.clustercontrol.repository.view.action.ScopeModifyAction;
import com.clustercontrol.repository.view.action.ScopeObjectPrivilegeAction;
import com.clustercontrol.repository.view.action.ScopeShowAction;
import com.clustercontrol.view.ObjectPrivilegeTargetListView;
import com.clustercontrol.view.ScopeListBaseView;
import com.clustercontrol.ws.repository.FacilityTreeItem;

/**
 * <BR>
 *
 * @version 5.0.0
 * @since 1.0.0
 */
public class ScopeListView extends ScopeListBaseView implements ObjectPrivilegeTargetListView {
    public static final String ID = ScopeListView.class.getName();

    // ----- instance  ----- //

    /** ?[]? */
    private ScopeListComposite composite = null;

    /** Last focus composite (Tree/List) */
    private Composite lastFocusComposite = null;

    /** ??????? */
    private boolean builtin;

    /** ?? */
    private int type;

    /** ????? */
    private boolean notReferFlg;

    // -----  ----- //

    // ----- instance  ----- //
    protected String getViewName() {
        return this.getClass().getName();
    }

    /**
     * @see com.clustercontrol.view.ScopeListBaseView#createListContents(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Composite createListContents(Composite parent) {
        GridLayout layout = new GridLayout(1, true);
        parent.setLayout(layout);
        layout.marginHeight = 0;
        layout.marginWidth = 0;

        this.composite = new ScopeListComposite(parent, SWT.NONE);
        WidgetTestUtil.setTestId(this, null, composite);

        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        this.composite.setLayoutData(gridData);

        //??
        createContextMenu();

        //Listener?
        super.getScopeTreeComposite().getTreeViewer()
                .addSelectionChangedListener(new FacilityTreeSelectionChangedListener());

        this.composite.getTableViewer()
                .addSelectionChangedListener(new ScopeListSelectionChangedListener(composite));

        this.update();
        setLastFocusComposite(this.getScopeTreeComposite());

        return this.composite;
    }

    /**
     * ??
     *
     *
     */
    private void createContextMenu() {
        MenuManager menuManager = new MenuManager();
        menuManager.setRemoveAllWhenShown(true);

        Menu treeMenu = menuManager.createContextMenu(this.getScopeTreeComposite().getTree());
        this.getScopeTreeComposite().getTree().setMenu(treeMenu);

        Menu listMenu = menuManager.createContextMenu(this.composite.getTable());
        WidgetTestUtil.setTestId(this, null, listMenu);
        this.composite.getTable().setMenu(listMenu);
        getSite().registerContextMenu(menuManager, this.composite.getTableViewer());
    }

    /**
     * ???()????
     *
     * @param item
     *            
     */
    @Override
    protected void doSelectTreeItem(FacilityTreeItem selectItem) {
        FacilityPath path = new FacilityPath(ClusterControlPlugin.getDefault().getSeparator());
        setPathLabel(Messages.getString("scope") + " : " + path.getPath(selectItem));

        this.composite.update(selectItem);
    }

    /**
     * ???
     *
     * @see com.clustercontrol.view.AutoUpdateView#update()
     */
    @Override
    public void update() {
        ClientSession.doCheck();
    }

    /**
     * ????????????
     *
     * @param cls
     *            ?
     * @return ?
     */
    @Override
    public Object getAdapter(@SuppressWarnings("rawtypes") Class cls) {
        if (cls.isInstance(this)) {
            return this;
        } else {
            return super.getAdapter(cls);
        }
    }

    public ScopeListComposite getComposite() {
        return this.composite;
    }

    public boolean getBuiltin() {
        return this.builtin;
    }

    public int getType() {
        return this.type;
    }

    public boolean getNotReferFlg() {
        return this.notReferFlg;
    }

    /**
     * ??/???
     *
     * @param notReferFlg ???
     *
     * @see com.clustercontrol.bean.FacilityConstant
     */
    public void setEnabledAction(boolean builtin, int type, ISelection selection, boolean notReferFlg) {
        this.builtin = builtin;
        this.type = type;
        this.notReferFlg = notReferFlg;

        //??/??
        ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
        if (null != service) {
            service.refreshElements(ScopeAddAction.ID, null);
            service.refreshElements(ScopeModifyAction.ID, null);
            service.refreshElements(ScopeDeleteAction.ID, null);
            service.refreshElements(NodeAssignAction.ID, null);
            service.refreshElements(NodeReleaseAction.ID, null);
            service.refreshElements(ScopeObjectPrivilegeAction.ID, null);
            service.refreshElements(RefreshAction.ID, null);
            service.refreshElements(ScopeShowAction.ID, null);

            // Update ToolBar after elements refreshed
            // WARN : Both ToolBarManager must be updated after updateActionBars(), otherwise icon won't change.
            getViewSite().getActionBars().updateActionBars();
            getViewSite().getActionBars().getToolBarManager().update(false);
        }
    }

    @Override
    public List<ObjectBean> getSelectedObjectBeans() {

        // ??????
        FacilityTreeItem item = getSelectedScopeItem();

        // ?????????
        List<ObjectBean> objectBeans = new ArrayList<ObjectBean>();
        if (item != null) {
            FacilityTreeItem manager = ScopePropertyUtil.getManager(item);
            String managerName = manager == null ? item.getData().getFacilityId()
                    : manager.getData().getFacilityId();
            String objectId = item.getData().getFacilityId();
            String objectType = HinemosModuleConstant.PLATFORM_REPOSITORY;
            ObjectBean objectBean = new ObjectBean(managerName, objectType, objectId);
            objectBeans.add(objectBean);
        }
        return objectBeans;
    }

    @Override
    public String getSelectedOwnerRoleId() {

        // ??????
        FacilityTreeItem item = getSelectedScopeItem();

        // ?????????
        String ownerRoleId = null;
        if (item != null) {
            ownerRoleId = item.getData().getOwnerRoleId();
        }
        return ownerRoleId;
    }

    public FacilityTreeItem getSelectedScopeItem() {
        FacilityTreeComposite tree = this.getScopeTreeComposite();
        ScopeListComposite list = (ScopeListComposite) this.getListComposite();

        // tree.getTree().isFocusControl() is not working under RAP because of Toolbar focus
        FacilityTreeItem item = null;
        if (this.lastFocusComposite instanceof FacilityTreeComposite) {
            item = tree.getSelectItem();
        } else if (this.lastFocusComposite instanceof ScopeListComposite) {
            item = list.getSelectItem();
        }

        // ?????????
        if (null != item) {
            return item;
        } else {
            return null;
        }
    }

    public List<?> getSelectedScopeItems() {
        FacilityTreeComposite tree = this.getScopeTreeComposite();
        ScopeListComposite list = (ScopeListComposite) this.getListComposite();

        // tree.getTree().isFocusControl() is not working under RAP because of Toolbar focus
        List<?> items = null;
        if (this.lastFocusComposite instanceof FacilityTreeComposite) {
            items = tree.getSelectionList();
        } else if (this.lastFocusComposite instanceof ScopeListComposite) {
            StructuredSelection selection = (StructuredSelection) list.getTableViewer().getSelection();
            items = selection.toList();
        }

        // ?????????
        if (!(null == items || items.isEmpty())) {
            return items;
        } else {
            return null;
        }
    }

    /**
     * Set last focus composite(Tree/List)
     * @param composite
     */
    public void setLastFocusComposite(Composite composite) {
        if (!composite.equals(this.lastFocusComposite)) {
            this.lastFocusComposite = composite;
        }
    }

    @Override
    public void update(boolean refreshFlag) {
        // ??

    }
}