com.clustercontrol.viewer.CommonTableLabelProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.viewer.CommonTableLabelProvider.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.viewer;

import java.sql.Time;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;

import com.clustercontrol.bean.CheckBoxImageConstant;
import com.clustercontrol.bean.DayOfWeekConstant;
import com.clustercontrol.bean.EndStatusImageConstant;
import com.clustercontrol.bean.EndStatusMessage;
import com.clustercontrol.bean.FacilityImageConstant;
import com.clustercontrol.bean.JobImageConstant;
import com.clustercontrol.bean.PerformanceStatusImageConstant;
import com.clustercontrol.bean.PriorityColorConstant;
import com.clustercontrol.bean.PriorityMessage;
import com.clustercontrol.bean.ProcessMessage;
import com.clustercontrol.bean.ScheduleConstant;
import com.clustercontrol.bean.StatusMessage;
import com.clustercontrol.bean.TableColumnInfo;
import com.clustercontrol.bean.ValidMessage;
import com.clustercontrol.bean.YesNoMessage;
import com.clustercontrol.jobmanagement.JobMessage;
import com.clustercontrol.jobmanagement.bean.DecisionObjectMessage;
import com.clustercontrol.jobmanagement.bean.JobApprovalResultImageConstant;
import com.clustercontrol.jobmanagement.bean.JobApprovalResultMessage;
import com.clustercontrol.jobmanagement.bean.JobApprovalStatusImageConstant;
import com.clustercontrol.jobmanagement.bean.JobApprovalStatusMessage;
import com.clustercontrol.jobmanagement.bean.JobParamTypeMessage;
import com.clustercontrol.jobmanagement.bean.JobRuntimeParamTypeMessage;
import com.clustercontrol.jobmanagement.bean.JudgmentObjectMessage;
import com.clustercontrol.jobmanagement.bean.ScheduleOnOffImageConstant;
import com.clustercontrol.jobmanagement.bean.StatusImageConstant;
import com.clustercontrol.jobmanagement.util.JobmapIconImageUtil;
import com.clustercontrol.monitor.bean.ConfirmMessage;
import com.clustercontrol.notify.util.NotifyTypeUtil;
import com.clustercontrol.performance.bean.PerformanceStatusConstant;
import com.clustercontrol.repository.bean.FacilityConstant;
import com.clustercontrol.util.Messages;
import com.clustercontrol.util.TimeStringConverter;
import com.clustercontrol.util.TimezoneUtil;
import com.clustercontrol.ws.common.Schedule;

/**
 * CommonTableViewer?LabelProvider<BR>
 *
 * @version 1.0.0
 * @since 1.0.0
 */
public class CommonTableLabelProvider extends LabelProvider implements ICommonTableLabelProvider {
    // 
    private static Log m_log = LogFactory.getLog(CommonTableLabelProvider.class);

    private CommonTableViewer m_viewer;

    /**
     * 
     *
     * ?
     *
     * @param viewer
     * @since 1.0.0
     */
    public CommonTableLabelProvider(CommonTableViewer viewer) {
        m_viewer = viewer;
    }

    /**
     * ??
     *
     * @since 1.0.0
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object,
     *      int)
     */
    @Override
    public String getColumnText(Object element, int columnIndex) {
        ArrayList<TableColumnInfo> tableColumnList = m_viewer.getTableColumnList();

        ArrayList<?> list = (ArrayList<?>) element;
        if (list.size() <= columnIndex) {
            m_log.debug("Bad implements. IndexOutOfBoundsException." + " list.size=" + list.size()
                    + ", columnIndex=" + columnIndex);
            return "";
        }
        Object item = list.get(columnIndex);

        TableColumnInfo tableColumn = tableColumnList.get(columnIndex);

        if (item == null || item.equals("")) {
            return "";
        }

        if (tableColumn.getType() == TableColumnInfo.JOB) {
            //????
            return JobMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.STATE) {
            //????
            return StatusMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.PRIORITY) {
            //??????
            return PriorityMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.VALID) {
            //?/???
            return ValidMessage.typeToString(((Boolean) item).booleanValue());
        } else if (tableColumn.getType() == TableColumnInfo.JUDGMENT_OBJECT) {
            //????
            return JudgmentObjectMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.NOTIFY_TYPE) {
            //????
            return NotifyTypeUtil.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.WAIT_RULE_VALUE) {
            //?????
            Class<?> itemClass = item.getClass();
            if (itemClass == Date.class) {
                //?0??24(??48)??
                return TimeStringConverter.formatTime((Date) item);
            } else if (itemClass == String.class) {
                return String.valueOf(item);
            } else if (itemClass.getSuperclass() == Number.class) {
                return ((Number) item).toString();
            }
        } else if (tableColumn.getType() == TableColumnInfo.SCHEDULE) {
            //????
            Schedule schedule = (Schedule) item;
            String scheduleString = null;
            DecimalFormat format = new DecimalFormat("00");
            if (schedule.getType() == ScheduleConstant.TYPE_DAY) {
                if (schedule.getMonth() != null) {
                    scheduleString = format.format(schedule.getMonth()) + "/" + format.format(schedule.getDay())
                            + " " + format.format(schedule.getHour()) + ":" + format.format(schedule.getMinute());
                } else if (schedule.getDay() != null) {
                    scheduleString = format.format(schedule.getDay()) + Messages.getString("monthday") + " "
                            + format.format(schedule.getHour()) + ":" + format.format(schedule.getMinute());
                } else if (schedule.getHour() != null) {
                    scheduleString = format.format(schedule.getHour()) + ":" + format.format(schedule.getMinute());
                } else if (schedule.getMinute() != null) {
                    scheduleString = format.format(schedule.getMinute()) + Messages.getString("minute");
                }
            } else if (schedule.getType() == ScheduleConstant.TYPE_WEEK) {
                if (schedule.getHour() != null) {
                    scheduleString = DayOfWeekConstant.typeToString(schedule.getWeek()) + " "
                            + format.format(schedule.getHour()) + ":" + format.format(schedule.getMinute());
                } else {
                    scheduleString = DayOfWeekConstant.typeToString(schedule.getWeek()) + " "
                            + format.format(schedule.getMinute()) + Messages.getString("minute");
                }
            } else {
                // ???????
                m_log.warn("CommonTableLabelProvider 165");
            }
            return scheduleString;
        } else if (tableColumn.getType() == TableColumnInfo.CONFIRM) {
            //??/????
            return ConfirmMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.WAIT_RULE) {
            //??????
            return YesNoMessage.typeToString(((Boolean) item).booleanValue());
        } else if (tableColumn.getType() == TableColumnInfo.PROCESS) {
            //?????
            return ProcessMessage.typeToString(((Boolean) item).booleanValue());
        } else if (tableColumn.getType() == TableColumnInfo.END_STATUS) {
            //????
            return EndStatusMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.CHECKBOX) {
            //?????
            return "";
        } else if (tableColumn.getType() == TableColumnInfo.DAY_OF_WEEK) {
            //????
            return DayOfWeekConstant.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.SCHEDULE_ON_OFF) {
            //????
            return "";
        } else if (tableColumn.getType() == TableColumnInfo.JOB_PARAM_TYPE) {
            //????
            return JobParamTypeMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.COLLECT_STATUS) {
            //?????
            return PerformanceStatusConstant.typeToString(((Boolean) item).booleanValue());
        } else if (tableColumn.getType() == TableColumnInfo.JOB_RUNTIME_PARAM_TYPE) {
            //????
            return String.format("%s(%s)", JobParamTypeMessage.STRING_RUNTIME,
                    JobRuntimeParamTypeMessage.typeToString(((Number) item).intValue()));
        } else if (tableColumn.getType() == TableColumnInfo.JOB_RUNTIME_PARAM_TYPE) {
            //????
            return String.format("%s(%s)", JobParamTypeMessage.STRING_RUNTIME,
                    JobRuntimeParamTypeMessage.typeToString(((Number) item).intValue()));
        } else if (tableColumn.getType() == TableColumnInfo.JOBMAP_ICON_IMAGE) {
            //????
            return "";
        } else if (tableColumn.getType() == TableColumnInfo.APPROVAL_STATUS) {
            //?????
            return JobApprovalStatusMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.APPROVAL_RESULT) {
            //??????
            return JobApprovalResultMessage.typeToString(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.DECISION_CONDITION) {
            //?????
            return DecisionObjectMessage.typeToString(((Number) item).intValue());
        } else {
            //???
            Class<?> itemClass = item.getClass();

            if (itemClass == String.class) {
                return String.valueOf(item);
            } else if (itemClass == Date.class) {
                return TimezoneUtil.getSimpleDateFormat().format((Date) item);
            } else if (itemClass == Time.class) {
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                formatter.setTimeZone(TimezoneUtil.getTimeZone());
                return formatter.format((Time) item);
            } else if (itemClass.getSuperclass() == Number.class) {
                return ((Number) item).toString();
            } else if (itemClass.isEnum()) {
                return ((Enum<?>) item).toString();
            } else {
                return item.toString();
            }
        }
        return "";
    }

    /**
     * ()??
     *
     * @since 1.0.0
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
     *      int)
     */
    @Override
    public Image getColumnImage(Object element, int columnIndex) {
        ArrayList<TableColumnInfo> tableColumnList = m_viewer.getTableColumnList();

        ArrayList<?> list = (ArrayList<?>) element;
        if (list.size() <= columnIndex) {
            m_log.debug("Bad implements. IndexOutOfBoundsException");
            return null;
        }
        Object item = list.get(columnIndex);

        TableColumnInfo tableColumn = tableColumnList.get(columnIndex);

        if (item == null || item.equals("")) {
            return null;
        }

        if (tableColumn.getType() == TableColumnInfo.JOB) {
            //????
            return JobImageConstant.typeToImage(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.FACILITY) {
            //????
            // TODO ?????
            Pattern p = Pattern.compile(".*>");
            Matcher m = p.matcher((String) item);
            if (m.matches()) {
                return FacilityImageConstant.typeToImage(FacilityConstant.TYPE_SCOPE, true);
            } else {
                return FacilityImageConstant.typeToImage(FacilityConstant.TYPE_NODE, true);
            }

        } else if (tableColumn.getType() == TableColumnInfo.STATE) {
            //????
            return StatusImageConstant.typeToImage(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.END_STATUS) {
            //????
            return EndStatusImageConstant.typeToImage(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.CHECKBOX) {
            //?????
            return CheckBoxImageConstant.typeToImage(((Boolean) item).booleanValue());
        } else if (tableColumn.getType() == TableColumnInfo.SCHEDULE_ON_OFF) {
            //????
            return ScheduleOnOffImageConstant.dateToImage(new Date((Long) item));
        } else if (tableColumn.getType() == TableColumnInfo.COLLECT_STATUS) {
            //?????
            return PerformanceStatusImageConstant.typeToImage(((Boolean) item).booleanValue());
        } else if (tableColumn.getType() == TableColumnInfo.APPROVAL_STATUS) {
            //?????
            return JobApprovalStatusImageConstant.typeToImage(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.APPROVAL_RESULT) {
            //??????
            return JobApprovalResultImageConstant.typeToImage(((Number) item).intValue());
        } else if (tableColumn.getType() == TableColumnInfo.JOBMAP_ICON_IMAGE) {
            //????
            return JobmapIconImageUtil.getIconImage(((byte[]) item));
        }

        return null;
    }

    /**
     * ??
     *
     * @since 1.0.0
     * @see com.clustercontrol.viewer.ICommonTableLabelProvider#getColumnColor(java.lang.Object,
     *      int)
     */
    @Override
    public Color getColumnColor(Object element, int columnIndex) {
        ArrayList<TableColumnInfo> tableColumnList = m_viewer.getTableColumnList();

        ArrayList<?> list = (ArrayList<?>) element;
        if (list.size() <= columnIndex) {
            m_log.debug("Bad implements. IndexOutOfBoundsException");
            return null;
        }
        Object item = list.get(columnIndex);

        TableColumnInfo tableColumn = tableColumnList.get(columnIndex);

        if (item == null) {
            return null;
        }

        if (tableColumn.getType() == TableColumnInfo.PRIORITY) {
            //??????
            return PriorityColorConstant.typeToColor(((Number) item).intValue());
        }

        return null;
    }
}