All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.magicwerk.brownies.html.content.HtmlFormatters Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 by Thomas Mauch
 *
 * 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.
 *
 * $Id$
 */
package org.magicwerk.brownies.html.content;

import java.util.function.Function;
import java.util.function.Predicate;

import org.jdom2.Attribute;
import org.magicwerk.brownies.collections.GapList;
import org.magicwerk.brownies.collections.IList;
import org.magicwerk.brownies.core.CheckTools;
import org.magicwerk.brownies.core.collections.GridSelection;
import org.magicwerk.brownies.core.conditional.ConditionalFunction;
import org.magicwerk.brownies.html.CssStyle;
import org.magicwerk.brownies.html.HtmlConst;
import org.magicwerk.brownies.html.HtmlFlow;
import org.magicwerk.brownies.html.content.HtmlTableFormatter.HtmlRegionFormatter;

/**
 * Class {@link HtmlFormatters} formats content of HTML table.
 */
public class HtmlFormatters {

	/**
	 * Context information which can be used for formatting.
	 */
	public static class Context {
		/** table or grid */
		Object source;
		int row;
		int col;
		Object val;
		String str;

		@SuppressWarnings("unchecked")
		public  T getValue() {
			return (T) val;
		}

		public String getText() {
			return str;
		}

		/** Getter for {@link #source} */
		public Object getSource() {
			return source;
		}

		/** Getter for {@link #row} */
		public int getRow() {
			return row;
		}

		/** Getter for {@link #col} */
		public int getCol() {
			return col;
		}

	}

	/**
	 * Interface for formatting HTML elements.
	 */
	public interface HtmlFormatter {
		/**
		 * Returns attribute which will be added to HTML element for formatting.
		 * Typically the name of the attribute will therefore be "class" or "style".
		 *
		 * @param ctx	context with information for formatting
		 * @return		attribute which will be added to HTML element, null for no attribute
		 */
		Attribute format(Context ctx);
	}

	/**
	 * AttrFormatter returns always the same attribute, irrespective of the context.
	 */
	public static class AttrFormatter implements HtmlFormatter {
		Attribute attr;

		/** Construct new {@link AttrFormatter} */
		public AttrFormatter(Attribute attr) {
			this.attr = attr;
		}

		@Override
		public Attribute format(Context ctx) {
			return attr;
		}
	}

	/**
	 * CssClassFormatter formats HTML by applying the CSS class attribute.
	 */
	public static class CssClassFormatter implements HtmlFormatter {
		Attribute classAttr;

		/** Construct new {@link CssClassFormatter} */
		public CssClassFormatter(String cssClass) {
			this.classAttr = new Attribute(HtmlConst.ATTR_CLASS, cssClass);
		}

		@Override
		public Attribute format(Context ctx) {
			return classAttr;
		}
	}

	/**
	 * CssStyleFormatter formats HTML by applying the CSS style attribute.
	 */
	public static class CssStyleFormatter implements HtmlFormatter {
		CssStyle cssStyle;
		Attribute styleAttr;

		/** Construct new {@link CssStyleFormatter} */
		public CssStyleFormatter(CssStyle cssStyle) {
			this.cssStyle = cssStyle;
		}

		public CssStyleFormatter(String style) {
			this.styleAttr = new Attribute(HtmlConst.ATTR_STYLE, style);
		}

		@Override
		public Attribute format(Context ctx) {
			return (styleAttr != null) ? styleAttr : cssStyle.getAttribute();
		}
	}

	/**
	 * ConditionalFormatter returns formatting attribute which is dependent on the value to format.
	 */
	public static class ConditionalFormatter implements HtmlFormatter {
		ConditionalFunction condFunc = new ConditionalFunction<>();

		public void add(Predicate pred, Function func) {
			condFunc.add(pred, func);
		}

		@Override
		public Attribute format(Context ctx) {
			return condFunc.apply(ctx);
		}
	}

	//

	/** True to check that formatters are applied to valid regions */
	boolean checkFormatters = true;
	/** Formatters to apply */
	IList formatters = GapList.create();

	public HtmlFormatters addFormatter(GridSelection region, HtmlFormatter formatter) {
		formatters.add(new HtmlRegionFormatter(region, formatter));
		return this;
	}

	// Method to override

	/**
	 * Add content to cell. The content is formatted by applying a formatter.
	 *
	 * @param cell	cell to format
	 * @param ctx	context information about cell
	 */
	public void formatElem(HtmlFlow cell, Context ctx) {
		HtmlFormatter formatter = getFormatter(ctx.row, ctx.col);
		if (formatter != null) {
			Attribute attr = formatter.format(ctx);
			if (attr != null) {
				attr = attr.clone();
				cell.getElement().setAttribute(attr);
			}
		}
		cell.addText(ctx.str);
	}

	/**
	 * Get formatter to use.
	 *
	 * @param row	row index
	 * @param col	column index
	 * @return		formatter to use, null for one
	 */
	HtmlFormatter getFormatter(int row, int col) {
		for (HtmlRegionFormatter formatter : formatters) {
			if (formatter.getRegion().contains(row, col)) {
				return formatter.getFormatter();
			}
		}
		return null;
	}

	/**
	 * Check whether formatters are within the allowed boundaries.
	 *
	 * @param numRows	number of rows
	 * @param numCols	number of columns
	 */
	void checkFormatters(int numRows, int numCols) {
		if (!checkFormatters) {
			return;
		}

		for (HtmlRegionFormatter formatter : formatters) {
			GridSelection region = formatter.getRegion();
			CheckTools.check(checkVal(region.getStartRow(), numRows), "Invalid row0 in region " + region);
			CheckTools.check(checkVal(region.getStartCol(), numCols), "Invalid col0 in region " + region);
			CheckTools.check(checkVal(region.getEndRow(), numRows), "Invalid row1 in region " + region);
			CheckTools.check(checkVal(region.getEndCol(), numCols), "Invalid col1 in region " + region);
		}
	}

	boolean checkVal(int val, int max) {
		if (val == Integer.MIN_VALUE || val == Integer.MAX_VALUE) {
			return true;
		}
		return val >= -1 && val < max;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy