com.htmlhifive.tools.wizard.log.ResultStatus.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.wizard.log.ResultStatus.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.htmlhifive.tools.wizard.log;

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

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;

import com.htmlhifive.tools.wizard.H5WizardPlugin;
import com.htmlhifive.tools.wizard.log.messages.Messages;
import com.htmlhifive.tools.wizard.log.messages.MessagesBase.Message;

/**
 * <H3>UI.</H3>
 * 
 * @author fkubo
 */
public class ResultStatus {

    /** . */
    private final StringBuilder allLog = new StringBuilder();

    /** Status. */
    private final List<IStatus> statusList = new ArrayList<IStatus>();

    /** ?. */
    private boolean success = true;

    /** ?. */
    private boolean interrupted = false;

    // e?null?

    /**
     * ?.
     * 
     * @param logLevel 
     * @param msg 
     * @param e 
     */
    private void put(LogLevel logLevel, String msg, Throwable e) {

        IStatus status = null;
        switch (logLevel) {
        case FATAL:
        case ERROR:
            status = new Status(IStatus.ERROR, H5WizardPlugin.getId(), msg, e);
            break;
        case WARN:
            status = new Status(IStatus.WARNING, H5WizardPlugin.getId(), msg, e);
            break;
        case INFO:
            status = new Status(IStatus.INFO, H5WizardPlugin.getId(), msg, e);
            break;
        default:
        }
        if (status != null) {
            statusList.add(status);
            if (status.getSeverity() != IStatus.INFO) { // Info
                // .metadata/.log?
                H5WizardPlugin.getInstance().getLog().log(status);
            }
        }
        allLog.append(msg);
    }

    /**
     * ?.
     * 
     * @param message 
     * @param params 
     */
    public void log(Message message, Object... params) {

        log(null, message, params);
    }

    /**
     * ?.
     * 
     * @param e 
     * @param message 
     * @param params 
     */
    public void logIgnoreSetSuccess(Throwable e, Message message, Object... params) {

        boolean oldSuccess = isSuccess();
        log(e, message, params);
        setSuccess(oldSuccess);
    }

    /**
     * ?.
     * 
     * @param e 
     * @param message 
     * @param params 
     */
    public void log(Throwable e, Message message, Object... params) {

        StringBuilder log = new StringBuilder();
        log.append("[");
        log.append(DateFormatUtils.format(System.currentTimeMillis(), "hh:mm:ss.SSS"));
        log.append("] ");
        log.append(message.getLevel().name());
        log.append(" ");
        log.append(message.getKey());
        log.append(" ");
        log.append(message.format(params));
        if (e != null) {
            setSuccess(false); // ??????????
            log.append("\n");
            log.append(ExceptionUtils.getStackTrace(e));
        }
        log.append("\n");
        put(message.getLevel(), log.toString(), e);
    }

    /**
     * ??.
     * 
     * @param method ???
     */
    public void showDialog(Message method) {

        if (interrupted) {
            ErrorDialog.openError(null, Messages.PI0131.format(), null,
                    getMultiStatus(IStatus.INFO, Messages.PI0134.format(method.format())));
        } else if (isSuccess()) {
            ErrorDialog.openError(null, Messages.PI0131.format(), null,
                    getMultiStatus(IStatus.INFO, Messages.PI0132.format(method.format())));
        } else {
            ErrorDialog.openError(null, Messages.PI0131.format(), null,
                    getMultiStatus(IStatus.WARNING, Messages.PI0133.format(method.format())));
        }

    }

    /**
     * ?.
     * 
     * @param method ???
     */
    public void falureDialog(Message title, Message message) {

        ErrorDialog.openError(null, title.format(), null, getMultiStatus(IStatus.WARNING, message.format()));
    }

    /**
     * ?.???
     * 
     * @return ?.
     */
    public boolean isSuccess() {

        return success;
    }

    /**
     * ?.???
     * 
     * @param success ?.
     */
    public void setSuccess(boolean success) {

        this.success = success;
    }

    /**
     * ?.
     * 
     * @return ?
     */
    public String getLog() {

        return allLog.toString();
    }

    /**
     * ?????
     * 
     * @return ?.
     */
    public List<IStatus> getStatusList() {

        return statusList;
    }

    /**
     * ?????
     * 
     * @return ?.
     */
    private MultiStatus getMultiStatus(final int level, String message) {

        return new MultiStatus(H5WizardPlugin.getId(), level, statusList.toArray(new IStatus[0]), message, null) {
            @Override
            public int getSeverity() {

                // ?Status??????.
                return level;
            }
        };
    }

    /**
     * ?.????.
     * 
     * @return ?.
     */
    public boolean isInterrupted() {

        return interrupted;
    }

    /**
     * ?.???.
     * 
     * @param interrupted ?.
     */
    public void setInterrupted(boolean interrupted) {

        this.interrupted = interrupted;
    }

}