org.shaf.shell.view.UnstructuredView.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.shell.view.UnstructuredView.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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 org.shaf.shell.view;

import org.shaf.core.content.UnstructuredContent;
import org.shaf.core.util.TimeUtils;
import org.shaf.shell.io.Terminal;

import com.google.common.base.Strings;

/**
 * The view for visualizing the {@link UnstructuredContent unstructured content}
 * .
 * 
 * @author Mykola Galushka
 */
public class UnstructuredView extends View<UnstructuredContent> {

    /**
     * Constructs a new view for visualizing unstructured content.
     * 
     * @param terminal
     *            the reference to the output terminal.
     */
    public UnstructuredView(Terminal terminal) {
        super(terminal);
    }

    /**
     * Shows the unstructured content on the text-based terminal.
     */
    @Override
    public void show(final UnstructuredContent content) {
        if (content == null) {
            return;
        }

        /*
         * The total length of the output content. If it length less than 30
         * characters the total value set to 30. If it length greater than 80
         * characters the total value set to 80. This trimming ensures the same
         * output style for structured and unstructured contends.
         */
        int total = content.getText().length();
        total = (total < 30) ? 30 : total;
        total = (total > 80) ? 80 : total;

        /*
         * Prints the top line. The top line of the unstructured content shows
         * the execution time spend by a process for generating this result.
         */
        String time = TimeUtils.formatTimeInterval(content.getTime());
        super.terminal.println(String.format(
                Strings.repeat("-", 11) + Strings.repeat("-", total - (time.length() + 16)) + "[%1$s]---", time));

        /*
         * Prints the content body.
         */
        this.terminal.println(content.getText());
    }
}