<commit_before>package org.rapidoid.gui;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll;
import org.rapidoid.gui.base.AbstractWidget;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.TdTag;
import org.rapidoid.lambda.Lmbd;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.model.Item;
import org.rapidoid.model.Items;
import org.rapidoid.model.Models;
import org.rapidoid.model.Property;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import org.rapidoid.var.Var;
import org.rapidoid.wrap.BoolWrap;

import java.util.Iterator;
import java.util.List;

/*
 * #%L
 * rapidoid-gui
 * %%
 * Copyright (C) 2014 - 2016 <NAME> and contributors
 * %%
 * 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.
 * #L%
 */

@Authors("<NAME>")
@Since("2.0.0")
public class Grid extends AbstractWidget<Grid> {

	private volatile Iterable<?> items;

	private volatile String orderBy;
	private volatile int pageSize = 10;
	private volatile String[] columns = {};
	private volatile Object[] headers = {};
	private volatile String rowCmd;

	private volatile Mapper<Object, String> toUri;

	@Override
	protected Object render() {

		Pager pager = noPager();
		boolean paging = pageSize > 0;
		Iterable<?> rows;

		BoolWrap isLastPage = new BoolWrap();

		if (paging) {
			String pageParam = "_p" + seq("pager");
			pager = GUI.pager(pageParam).min(1);

			Integer size = Coll.getSizeOrNull(items);

			if (size != null) {
				int pages = (int) Math.ceil(size / (double) pageSize);
				pager.max(pages);
			}

			rows = Msc.getPage(items, pager.pageNumber(), pageSize, size, isLastPage);

		} else {
			rows = items;
		}

		return renderGridPage(pager, rows, isLastPage.value);
	}

	private Object renderGridPage(Pager pager, Iterable<?> rows, boolean isLastPage) {
		Iterator<?> it = rows.iterator();
		boolean hasData = it.hasNext();

		if (pager != null && (isLastPage || !hasData)) {
			pager.max(pager.pageNumber());
		}

		if (!hasData) {
			return U.list(noDataAvailable(), pager); // no data
		}

		Class<?> type = it.next().getClass();

		Items itemsModel;
		if (rows instanceof Items) {
			itemsModel = (Items) rows;
		} else {
			itemsModel = Models.beanItems(type, U.array(rows));
		}

		final List<Property> props = itemsModel.properties(columns);
		boolean ordered = !U.isEmpty(orderBy);
		Var<String> order = null;
		String currentOrder = orderBy;

		if (ordered) {
			order = GUI.var("_o" + seq("order"), orderBy);
			currentOrder = order.get();
			itemsModel = itemsModel.orderedBy(currentOrder);
		}

		Tag header = tableHeader(props, order);
		Tag body = tableBody(props, itemsModel);

		return fullTable(header, body, pager);
	}

	protected Tag noDataAvailable() {
		return GUI.h4("No data available!");
	}
<commit_msg>Use "N/A" icon when the grid data is empty.<commit_after>package org.rapidoid.gui;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.collection.Coll;
import org.rapidoid.gui.base.AbstractWidget;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.TdTag;
import org.rapidoid.lambda.Lmbd;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.model.Item;
import org.rapidoid.model.Items;
import org.rapidoid.model.Models;
import org.rapidoid.model.Property;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import org.rapidoid.var.Var;
import org.rapidoid.wrap.BoolWrap;

import java.util.Iterator;
import java.util.List;

/*
 * #%L
 * rapidoid-gui
 * %%
 * Copyright (C) 2014 - 2016 <NAME> and contributors
 * %%
 * 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.
 * #L%
 */

@Authors("<NAME>")
@Since("2.0.0")
public class Grid extends AbstractWidget<Grid> {

	private volatile Iterable<?> items;

	private volatile String orderBy;
	private volatile int pageSize = 10;
	private volatile String[] columns = {};
	private volatile Object[] headers = {};
	private volatile String rowCmd;

	private volatile Mapper<Object, String> toUri;

	@Override
	protected Object render() {

		Pager pager = noPager();
		boolean paging = pageSize > 0;
		Iterable<?> rows;

		BoolWrap isLastPage = new BoolWrap();

		if (paging) {
			String pageParam = "_p" + seq("pager");
			pager = GUI.pager(pageParam).min(1);

			Integer size = Coll.getSizeOrNull(items);

			if (size != null) {
				int pages = (int) Math.ceil(size / (double) pageSize);
				pager.max(pages);
			}

			rows = Msc.getPage(items, pager.pageNumber(), pageSize, size, isLastPage);

		} else {
			rows = items;
		}

		return renderGridPage(pager, rows, isLastPage.value);
	}

	private Object renderGridPage(Pager pager, Iterable<?> rows, boolean isLastPage) {
		Iterator<?> it = rows.iterator();
		boolean hasData = it.hasNext();

		if (pager != null && (isLastPage || !hasData)) {
			pager.max(pager.pageNumber());
		}

		if (!hasData) {
			return U.list(noDataAvailable(), pager); // no data
		}

		Class<?> type = it.next().getClass();

		Items itemsModel;
		if (rows instanceof Items) {
			itemsModel = (Items) rows;
		} else {
			itemsModel = Models.beanItems(type, U.array(rows));
		}

		final List<Property> props = itemsModel.properties(columns);
		boolean ordered = !U.isEmpty(orderBy);
		Var<String> order = null;
		String currentOrder = orderBy;

		if (ordered) {
			order = GUI.var("_o" + seq("order"), orderBy);
			currentOrder = order.get();
			itemsModel = itemsModel.orderedBy(currentOrder);
		}

		Tag header = tableHeader(props, order);
		Tag body = tableBody(props, itemsModel);

		return fullTable(header, body, pager);
	}

	protected Tag noDataAvailable() {
		return GUI.NOTHING;
	}

{'fraction_non_alphanumeric': 0.127996381727725, 'fraction_numerical': 0.005879692446856626, 'mean_word_length': 4.0372057706909645, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4935253', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2121, 'n_words': 604}