com.clustercontrol.repository.dialog.NodeAssignDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.repository.dialog.NodeAssignDialog.java

Source

/*
    
Copyright (C) 2006 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.dialog;

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

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.clustercontrol.accesscontrol.util.ClientSession;
import com.clustercontrol.dialog.CommonDialog;
import com.clustercontrol.dialog.ValidateResult;
import com.clustercontrol.repository.composite.NodeFilterComposite;
import com.clustercontrol.repository.util.RepositoryEndpointWrapper;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.ws.repository.InvalidRole_Exception;
import com.clustercontrol.util.WidgetTestUtil;

/**
 * ???<BR>
 *
 * @version 1.0.0
 * @since 1.0.0
 */
public class NodeAssignDialog extends CommonDialog {
    private static final int sizeX = 800;
    private static final int sizeY = 500;

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

    /** ??ID */
    private String facilityId = "";

    /**  */
    private NodeFilterComposite nodeList = null;

    /** ??? */
    private List<String> filterItems = null;

    /** ??? */
    private String managerName = null;
    // -----  ----- //

    /**
     * ????????
     *
     * @param parent
     *            ?
     * @param managerName
     *            ???
     * @param facilityId
     *            ???ID
     */
    public NodeAssignDialog(Shell parent, String managerName, String facilityId) {
        super(parent);

        this.managerName = managerName;
        this.facilityId = facilityId;
    }

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

    /**
     * ?????
     *
     * @return ?
     */
    @Override
    protected Point getInitialSize() {
        return new Point(sizeX, sizeY);
    }

    /**
     * ????
     *
     * @param parent
     *            ?
     */
    @Override
    protected void customizeDialog(Composite parent) {
        Shell shell = this.getShell();

        // 
        shell.setText(Messages.getString("dialog.repository.select.nodes"));

        // 
        GridLayout layout = new GridLayout(1, true);
        layout.marginWidth = 10;
        layout.marginHeight = 10;
        parent.setLayout(layout);

        /*
         * 
         */

        // 
        this.nodeList = new NodeFilterComposite(parent, SWT.NONE, this.managerName, facilityId, true);
        WidgetTestUtil.setTestId(this, null, nodeList);
        GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
        gridData.heightHint = SWT.MIN;
        this.nodeList.setLayoutData(gridData);

        this.nodeList.update();

        // ??
        Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
        WidgetTestUtil.setTestId(this, "line", line);
        gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalSpan = 1;
        line.setLayoutData(gridData);

        //?pack:resize to be its preferred size
        shell.pack();
        shell.setSize(new Point(sizeX, sizeY));

        // ??
        Display display = shell.getDisplay();
        shell.setLocation((display.getBounds().width - shell.getSize().x) / 2,
                (display.getBounds().height - shell.getSize().y) / 2);
    }

    /**
     * ???????
     *
     * @param parent
     *            ??
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        // 
        this.createButton(parent, IDialogConstants.OPEN_ID, Messages.getString("filter"), false);
        this.getButton(IDialogConstants.OPEN_ID).addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                // ?
                NodeFilterDialog dialog = new NodeFilterDialog(getShell());
                if (dialog.open() == IDialogConstants.OK_ID) {
                    nodeList.update(dialog.getInputData());
                }
            }
        });

        super.createButtonsForButtonBar(parent);
    }

    /**
     * ????
     *
     * @return ?
     */
    @Override
    protected ValidateResult validate() {
        return super.validate();
    }

    /**
     * ??ID???
     *
     * @return ??ID
     */
    public String getScopeId() {
        return this.facilityId;
    }

    /**
     * ??????
     *
     * @return ?ID
     */
    public List<String> getFilterItems() {
        return this.filterItems;
    }

    /**
     * ????
     *
     * @return ?
     */
    @Override
    protected String getOkButtonText() {
        return Messages.getString("assign");
    }

    /**
     * ????
     *
     * @return ?
     */
    @Override
    protected String getCancelButtonText() {
        return Messages.getString("cancel");
    }

    /**
     * OK?????
     */
    @Override
    protected void okPressed() {

        // ??
        StructuredSelection selection = (StructuredSelection) this.nodeList.getTableViewer().getSelection();

        Object[] items = selection.toArray();
        if (items != null) {
            int size = items.length;
            this.filterItems = new ArrayList<String>();
            for (int i = 0; i < size; i++) {
                this.filterItems.add((String) ((ArrayList<?>) items[i]).get(1));
            }
        }

        // 
        try {
            RepositoryEndpointWrapper wrapper = RepositoryEndpointWrapper.getWrapper(this.managerName);
            wrapper.assignNodeScope(facilityId, this.filterItems);

            // ??
            ClientSession.doCheck();

            // ??
            Object[] arg = { this.managerName };
            MessageDialog.openInformation(null, Messages.getString("successful"),
                    Messages.getString("message.repository.6", arg));

            super.okPressed();

        } catch (Exception e) {
            String errMessage = "";
            if (e instanceof InvalidRole_Exception) {
                // ??????
                MessageDialog.openInformation(null, Messages.getString("message"),
                        Messages.getString("message.accesscontrol.16"));
            } else {
                errMessage = ", " + HinemosMessage.replace(e.getMessage());
            }

            // ?
            MessageDialog.openError(null, Messages.getString("failed"),
                    Messages.getString("message.repository.7") + errMessage);
        }
    }
}