com.clustercontrol.hub.composite.LogFormatKeyListComposite.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.hub.composite.LogFormatKeyListComposite.java

Source

/*
    
 Copyright (C) 2016 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.hub.composite;

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

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
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.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.PlatformUI;

import com.clustercontrol.bean.PropertyDefineConstant;
import com.clustercontrol.bean.TableColumnInfo;
import com.clustercontrol.hub.dialog.LogKeyPatternDialog;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.WidgetTestUtil;
import com.clustercontrol.viewer.CommonTableViewer;
import com.clustercontrol.ws.hub.LogFormatKey;

public class LogFormatKeyListComposite extends Composite {

    /***/
    public static final int KEY = 0;

    /**  */
    public static final int DESCRIPTION = 1;

    /**  */
    public static final int PATTERN = 2;

    /** **/
    public static final int DUMMY = 3;

    /** ? */
    public static final int SORT_COLUMN_INDEX = KEY;

    /** ? */
    public static final int SORT_ORDER = 1;

    /**
     * []????<BR>
     *
     * @return
     */
    public static ArrayList<TableColumnInfo> getTableColumnInfoList() {
        //?
        ArrayList<TableColumnInfo> tableDefine = new ArrayList<TableColumnInfo>();
        Locale locale = Locale.getDefault();

        tableDefine.add(KEY, new TableColumnInfo(Messages.getString("hub.log.format.key", locale),
                TableColumnInfo.NONE, 100, SWT.LEFT));
        tableDefine.add(DESCRIPTION, new TableColumnInfo(Messages.getString("description", locale),
                TableColumnInfo.NONE, 120, SWT.LEFT));
        tableDefine.add(PATTERN, new TableColumnInfo(Messages.getString("hub.log.format.key.pattern.regex", locale),
                TableColumnInfo.NONE, 300, SWT.LEFT));

        return tableDefine;
    }

    /**  */
    private CommonTableViewer m_tableViewer = null;
    /** []? */
    private List<LogFormatKey> m_keyList = null;
    /** ID */
    private String m_ownerRoleId = null;
    /** ??? */
    private String m_managerName = null;

    /**
     * @return the m_managerName
     */
    public String getManagerName() {
        return m_managerName;
    }

    /**
     * ???LogFormatKey?????
     * ??LogFormatKey?
     * @param key
     * @return
     */
    public LogFormatKey getLogFormatKeyListByKey(String key) {
        for (LogFormatKey logFormatKey : getLogFormatKeyList()) {
            if (logFormatKey.getKey().equals(key)) {
                return logFormatKey;
            }
        }
        return null;
    }

    /**
     *
     * @return
     */
    public List<LogFormatKey> getLogFormatKeyList() {
        return this.m_keyList;
    }

    /**
     * ???[]??????
     * @param keyList
     */
    public void setLogFormatKeyList(List<LogFormatKey> keyList) {
        if (keyList != null) {
            this.m_keyList = keyList;
            this.update();
        }
    }

    public void addLogFormatKeyList(LogFormatKey key) {
        this.m_keyList.add(key);
    }

    /**
     *
     * @return
     */
    public String getOwnerRoleId() {
        return m_ownerRoleId;
    }

    /**
     *
     * @param ownerRoleId
     */
    public void setOwnerRoleId(String ownerRoleId) {
        this.m_ownerRoleId = ownerRoleId;
    }

    /**
     * ???
     * <p>
     * ??????????
     *
     * @param parent ??
     * @param style 
     * @param managerName ???
     *
     * @see org.eclipse.swt.SWT
     * @see org.eclipse.swt.widgets.Composite#Composite(Composite parent, int style)
     * @see #initialize()
     */
    public LogFormatKeyListComposite(Composite parent, int style, String managerName) {
        super(parent, style);
        this.m_managerName = managerName;
        this.m_keyList = new ArrayList<LogFormatKey>();
        this.initialize();
    }

    /**
     * ?????????
     *
     * @param identifier 
     */
    @SuppressWarnings("unused")
    private void selectItem(Integer order) {
        Table tblLogFormatKeyList = m_tableViewer.getTable();
        TableItem[] items = tblLogFormatKeyList.getItems();

        if (items == null || order == null) {
            return;
        }
        tblLogFormatKeyList.select(order);
        return;
    }

    /**
     * ?????
     */
    private void initialize() {

        /*
         * []?
         */
        GridLayout layout = new GridLayout(1, true);
        this.setLayout(layout);
        layout.marginHeight = 0;
        layout.marginWidth = 0;

        Table tblLogFormatKeyList = new Table(this, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
        WidgetTestUtil.setTestId(this, null, tblLogFormatKeyList);
        tblLogFormatKeyList.setHeaderVisible(true);
        tblLogFormatKeyList.setLinesVisible(true);

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

        // ??
        m_tableViewer = new CommonTableViewer(tblLogFormatKeyList);
        m_tableViewer.createTableColumn(getTableColumnInfoList(), SORT_COLUMN_INDEX, SORT_ORDER);
        m_tableViewer.addDoubleClickListener(new IDoubleClickListener() {
            @Override
            public void doubleClick(DoubleClickEvent event) {
                //?????
                String order = getSelectionLogFormatKey();

                // ?
                Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                //???LogFormatKey????
                LogFormatKey logFormatKey = getLogFormatKeyListByKey(order);

                if (logFormatKey != null) {
                    LogKeyPatternDialog dialog = new LogKeyPatternDialog(shell, PropertyDefineConstant.MODE_MODIFY,
                            m_keyList, logFormatKey);
                    if (dialog.open() == IDialogConstants.OK_ID) {
                        getLogFormatKeyList().remove(logFormatKey);
                        addLogFormatKeyList(dialog.getLogFormatKey());
                        update();
                    }
                } else {
                    MessageDialog.openWarning(null, Messages.getString("warning"),
                            Messages.getString("message.monitor.30"));
                }
            }
        });
    }

    /**
     * ????<BR>
     * []???????
     */
    @Override
    public void update() {
        // 
        ArrayList<Object> listAll = new ArrayList<Object>();
        for (LogFormatKey key : getLogFormatKeyList()) {
            ArrayList<Object> list = new ArrayList<Object>();
            //FIXME ??
            list.add(key.getKey());
            list.add(key.getDescription());
            list.add(key.getPattern());
            //
            list.add(null);
            listAll.add(list);
        }
        m_tableViewer.setInput(listAll);
    }

    /**
     * ?????
     *
     */
    public Integer getSelection() {
        StructuredSelection selection = (StructuredSelection) m_tableViewer.getSelection();
        if (selection.getFirstElement() instanceof List) {
            List<?> list = (List<?>) selection.getFirstElement();
            if (list.get(0) instanceof Integer) {
                return (Integer) list.get(0);
            }
        }
        return null;
    }

    /**
     * ????LogFormatKey??
     *
     */
    public String getSelectionLogFormatKey() {
        StructuredSelection selection = (StructuredSelection) m_tableViewer.getSelection();
        if (selection.getFirstElement() instanceof List) {
            List<?> list = (List<?>) selection.getFirstElement();
            if (list.get(0) instanceof String) {
                return (String) list.get(0);
            }
        }
        return null;
    }

    /**
     * 
     */
    public void setSelection() {
        Table setSelectionTable = m_tableViewer.getTable();
        WidgetTestUtil.setTestId(this, null, setSelectionTable);
        int selectIndex = setSelectionTable.getSelectionIndex();
        update();
        setSelectionTable.setSelection(selectIndex);
    }

    /* (? Javadoc)
     * @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
     */
    @Override
    public void setEnabled(boolean enabled) {
        this.m_tableViewer.getTable().setEnabled(enabled);
    }

}