/*
* [QuiltPair.java]
*
* Summary: Generate HTML for grid of value-pairs like a quilt, with the entries sorted by columns.
*
* Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
*
* Licence: This software may be copied and used freely for any purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.8+
*
* Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
*
* Version History:
* 1.8 2009-02-06 include go package in ZIP bundle.
* Generates
... without
*/
package com.mindprod.htmlmacros.macro;
import com.mindprod.comparators.HTMLArrayComparator;
import com.mindprod.fastcat.FastCat;
import java.util.Arrays;
import static java.lang.System.*;
/**
* Generate HTML for grid of value-pairs like a quilt, with the entries sorted by columns.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.8 2009-02-06 include go package in ZIP bundle.
* Generates ... without
* @see Quilt
* @since 2009
*/
public final class QuiltPair extends Macro
{
/**
* how to use the macro
*/
private static final String USAGE = "\nQuiltPair macro needs size (sorted/asis) [value1a] [value1b] [value2a] " +
"[value2b]...";
/**
* guts to Generate the table for a quilt
*
* @param cols how many columns to generate. 3x2 = 6 would be coded as 3.
* @param cells cells to render, even,odd pairs
* @param grouping how cols are grouped, #cols per entry, 2=pairwise 3 in groups of 3. This is more general the the
* macro itself.
*
* @return expanded explanation Generates ... without . Allows user to supply table
* class.
*/
private static String expand( int cols, String[][] cells, final int grouping )
{
// calc covered quotient
int rows = ( cells.length + cols - 1 ) / cols;
// if there are not enough elements to fill the columns,
// reduce the number of cols.
cols = ( cells.length + rows - 1 ) / rows;
final FastCat sb = new FastCat( rows * 2 + rows * cols * grouping * 3 + 2 );
sb.append( "\n\n" );
for ( int i = 0; i < rows; i++ )
{
sb.append( "" );
for ( int j = 0; j < cols; j++ )
{
for ( int g = 0; g < grouping; g++ )
{
sb.append( "" );
int k = j * rows + i;
if ( k < cells.length )
{
sb.append( cells[ k ][ g ] );
}
else
{
// indexing out past the end of the cells
// sb.append( " " );
// leave empty to get a pale non-button square.
}
sb.append( " | \n" );
}
}
sb.append( "
\n" );
}
sb.append( "\n" );
return sb.toString();
}
/**
* sample macro invocation:
3 = number of columns. sort/asis. Generate the html for a grid
* of values, possibly containing HTML.
*
* @param parms from macro
* @param quiet true if want output suppressed.
* @param verbose @return expanded macro HTML
*/
public String expandMacro( String[] parms,
final boolean quiet, final boolean verbose )
{
if ( !quiet )
{
out.print( "Q" );
}
if ( parms.length < 4 || ( parms.length % 2 ) != 0 )
{
throw new IllegalArgumentException( USAGE );
}
final int grouping = 2;
int cols = Integer.parseInt( parms[ 0 ] );
String type = parms[ 1 ];
boolean sort;
if ( type.equalsIgnoreCase( "sort" ) )
{
sort = true;
}
else if ( type.equalsIgnoreCase( "asis" ) )
{
sort = false;
}
else
{
throw new IllegalArgumentException( USAGE );
}
String[][] cells = new String[ ( parms.length - 2 ) / 2 ][ 2 ];
for ( int k = 0; k < cells.length; k++ )
{
System.arraycopy( parms, 2 * k + 2, cells[ k ], 0, grouping );
}
if ( sort )
{
Arrays.sort( cells, new HTMLArrayComparator() );
}
return expand( cols, cells, grouping );
}
}