/* * [Quilt.java] * * Summary: Generate the for grid of values 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. */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.ST; import com.mindprod.comparators.HTMLComparator; import com.mindprod.fastcat.FastCat; import java.util.Arrays; import static java.lang.System.*; /** * Generate the for grid of values 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. * @see QuiltPair * @since 2009 */ public final class Quilt extends Macro { /** * how to use the macro */ private static final String USAGE = "\nQuilt macro needs {size} [-byrows][-bycols][-sorted][-asis] {tableClass} {value} {value} ..."; // methods /** * guts to Generate the table for a quilt * * @param tableClass CSS final class of the table we generate * @param cols how many columns to generate. * @param cells cells to render * @param byCols true quilt ordering in columns like a phone book * * @return expanded explanation */ private static String expand( String tableClass, int cols, String[] cells, boolean byCols ) { // calc covered quotient final 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; // out.println( "rows:" + rows + " cols:" + cols ); final FastCat sb = new FastCat( 16 + rows * 2 + rows * cols * 3 ); sb.append( "\n" ); sb.append( "" ); sb.append( "" ); sb.append( "\n" ); sb.append( "\n" ); for ( int i = 0; i < rows; i++ ) { sb.append( "" ); for ( int j = 0; j < cols; j++ ) { sb.append( "\n" ); } sb.append( "\n" ); } sb.append( "
menu
" ); final int k; if ( byCols ) { k = j * rows + i; } else { k = i * cols + j; } // out.println("i:" + i + " j:"+j + " k:" +k ); if ( k < cells.length ) { sb.append( cells[ k ] ); } else { // sb.append( " " ); // leave cell empty, renders as pale rectangle rather than // border button } sb.append( "
\n" ); return sb.toString(); }// /method /** * Sample macro invocation: <!-- macro Quilt 5 sort/asis gridmenu {bird} {cat} {rabbit} --> *

* 5 = number of columns. sort/asis. gridmenu is the tableClass. Generate the html for a grid of values, possibly * containing html tags and entities. * * @param parms variable number size, sort table class, items. * @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 < 3 ) { throw new IllegalArgumentException( USAGE ); } final int cols = Integer.parseInt( parms[ 0 ] ); // {size} [-byrows][-bycols][-sorted][-asis] {tableClass} {value} {value} ..."; boolean sort = false; boolean byCols = true; String tableClass = null; int firstCellAt = 0; parmLoop: for ( int i = 1; i < parms.length - 1; i++ ) { switch ( parms[ i ] ) { case "byrows": case "-byrows": byCols = false; break; case "bycols": case "-bycols": byCols = true; break; case "sorted": case "-sorted": case "sort": case "-sort": sort = true; break; case "asis": case "-asis": sort = false; break; default: // should be a class tableClass = parms[ i ]; if ( !ST.isLegal( tableClass, "abcdefghijklmnopqrstuvwxyz0123456789" ) ) { throw new IllegalArgumentException( "Quilt macro tableClass must be lower case alphabetic, probably missing tableClass." ); } firstCellAt = i + 1; break parmLoop; } } final String[] cells = Arrays.copyOfRange( parms, firstCellAt, parms.length /* end+1 not length */ ); // out.println( "first:" + firstCellAt + " parms:" + parms.length + " cells:" + cells.length) ; if ( sort ) { Arrays.sort( cells, new HTMLComparator() ); } return expand( tableClass, cols, cells, byCols ); }// /method // /methods }