Java tutorial
/** * 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.StructuredContent; import org.shaf.core.util.TextMatrix; import org.shaf.core.util.TimeUtils; import org.shaf.shell.io.Terminal; import com.google.common.base.Strings; /** * The view for visualizing the {@link StructuredContent structured content}. * * @author Mykola Galushka */ public class StructuredView extends View<StructuredContent> { /** * Constructs a new view for visualizing structured content. * * @param terminal * the reference to the output terminal. */ public StructuredView(Terminal terminal) { super(terminal); } /** * Shows the structured content on the text-based terminal. */ @Override public void show(final StructuredContent content) { /* * Defines the maximum widths for every table's column. */ TextMatrix table = content.getTable(); /* * Defines the total table width. The table width computes as a sum of * all column's width plus (2 * number of columns + 1 ). The twice * number of colum plus one provide an extra space for inserting columns * separators '|' with one blank character. This blank character intends * to provide a padding from the left for every cell value. */ int total = (2 * table.getNumColumns()) + 1; int[] widths = table.getColumnWidths(); for (int width : widths) { total += width; } /* * Expands table's columns if it total table width less them 30 * characters. The next loop expands each column's width to make their * overall equals 30 characters. */ if (table.getNumColumns() > 0) { int index = -1; while (total < 30) { ++widths[index = (index + 1 < widths.length) ? ++index : 0]; ++total; } } else { total = 30; } /* * Prints the top line. The top line of the structured 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("-", total - (time.length() + 5)) + "[%1$s]" + Strings.repeat("-", 3), time)); /* * Constructs the formating string. */ String fmt = ""; for (int width : widths) { fmt += " %-" + (width + 1) + "s"; } fmt += " "; /* * Prints the table to the terminal. */ for (int row = 0; row < table.getNumRows(); row++) { super.terminal.println(String.format(fmt, (Object[]) table.getRow(row))); } /* * Prints the top bottom. The bottom line of the structured content * shows the total number of rows included into the result. */ String count = String.valueOf(table.getNumRows()); super.terminal.println(String.format( Strings.repeat("-", total - (count.length() + 12)) + "[Total: %1$s]" + Strings.repeat("-", 3), count)); } }