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

org.rapidoid.gui.KVGrid Maven / Gradle / Ivy

There is a newer version: 5.5.5
Show newest version
/*-
 * #%L
 * rapidoid-gui
 * %%
 * Copyright (C) 2014 - 2018 Nikolche Mihajlovski 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%
 */

package org.rapidoid.gui;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.gui.base.AbstractWidget;
import org.rapidoid.html.Tag;
import org.rapidoid.html.tag.TableTag;
import org.rapidoid.lambda.Lmbd;
import org.rapidoid.lambda.Mapper;

import java.util.Map;
import java.util.Map.Entry;


@Authors("Nikolche Mihajlovski")
@Since("2.4.0")
public class KVGrid extends AbstractWidget {

	private final String[] headers = {"Key", "Value"};

	private Mapper keyView = null;

	private Mapper valueView = null;

	private Map map;

	private boolean headless;

	@Override
	protected Object render() {
		if (map.isEmpty()) {
			return GUI.NOTHING;
		}

		TableTag tbl = headless ? GUI.table_() : GUI.table_(tr(th(headers[0]), th(headers[1])));

		for (Entry e : map.entrySet()) {
			Object key = e.getKey();
			if (keyView != null) {
				key = Lmbd.eval(keyView, key);
			} else {
				key = GUI.display(key);
			}

			Object val = e.getValue();
			if (valueView != null) {
				val = Lmbd.eval(valueView, val);
			} else {
				val = GUI.display(val);
			}

			Tag tr = val != null ? tr(td(key), td(val)) : tr(td(key).colspan("2"));
			tbl = tbl.append(tr);
		}

		return tbl;
	}

	public Map map() {
		return map;
	}

	public KVGrid map(Map map) {
		this.map = map;
		return this;
	}

	public String[] headers() {
		return headers;
	}

	public KVGrid headers(String keyHeader, String valueHeader) {
		this.headers[0] = keyHeader;
		this.headers[1] = valueHeader;
		return this;
	}

	public Mapper keyView() {
		return keyView;
	}

	public Mapper valueView() {
		return valueView;
	}

	@SuppressWarnings("unchecked")
	public  KVGrid keyView(Mapper keyView) {
		this.keyView = (Mapper) keyView;
		return this;
	}

	@SuppressWarnings("unchecked")
	public  KVGrid valueView(Mapper valueView) {
		this.valueView = (Mapper) valueView;
		return this;
	}

	public KVGrid headless(boolean headless) {
		this.headless = headless;
		return this;
	}

	public boolean headless() {
		return headless;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy