123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.util;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.function.Function;
- import com.l2jserver.gameserver.model.PageResult;
- import com.l2jserver.util.StringUtil;
- /**
- * A class containing useful methods for constructing HTML
- * @author NosBit
- */
- public class HtmlUtil
- {
- /**
- * Gets the HTML representation of CP gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getCpGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_CP_bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_CP_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of HP gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getHpGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_HP_bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_HP_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of HP Warn gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getHpWarnGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_HPWarn_bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_HPWarn_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of HP Fill gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getHpFillGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_HPFill_bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_HPFill_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of MP Warn gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getMpGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_MP_bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_MP_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of EXP Warn gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getExpGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_EXP_bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_EXP_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of Food gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getFoodGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_Food_Bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_Food_Center", 17, -13);
- }
-
- /**
- * Gets the HTML representation of Weight gauge automatically changing level depending on current/max.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @return the HTML
- */
- public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage)
- {
- return getWeightGauge(width, current, max, displayAsPercentage, Util.map(current, 0, max, 1, 5));
- }
-
- /**
- * Gets the HTML representation of Weight gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @param level a number from 1 to 5 for the 5 different colors of weight gauge
- * @return the HTML
- */
- public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage, long level)
- {
- return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_bg_Center" + level, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_Center" + level, 17, -13);
- }
-
- /**
- * Gets the HTML representation of a gauge.
- * @param width the width
- * @param current the current value
- * @param max the max value
- * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
- * @param backgroundImage the background image
- * @param image the foreground image
- * @param imageHeight the image height
- * @param top the top adjustment
- * @return the HTML
- */
- private static String getGauge(int width, long current, long max, boolean displayAsPercentage, String backgroundImage, String image, long imageHeight, long top)
- {
- current = Math.min(current, max);
- final StringBuilder sb = new StringBuilder();
- StringUtil.append(sb, "<table width=", String.valueOf(width), " cellpadding=0 cellspacing=0><tr><td background=\"" + backgroundImage + "\">");
- StringUtil.append(sb, "<img src=\"" + image + "\" width=", String.valueOf((long) (((double) current / max) * width)), " height=", String.valueOf(imageHeight), ">");
- StringUtil.append(sb, "</td></tr><tr><td align=center><table cellpadding=0 cellspacing=", String.valueOf(top), "><tr><td>");
- if (displayAsPercentage)
- {
- StringUtil.append(sb, "<table cellpadding=0 cellspacing=2><tr><td>", String.format("%.2f%%", ((double) current / max) * 100), "</td></tr></table>");
- }
- else
- {
- final String tdWidth = String.valueOf((width - 10) / 2);
- StringUtil.append(sb, "<table cellpadding=0 cellspacing=0><tr><td width=" + tdWidth + " align=right>", String.valueOf(current), "</td>");
- StringUtil.append(sb, "<td width=10 align=center>/</td><td width=" + tdWidth + ">", String.valueOf(max), "</td></tr></table>");
- }
- StringUtil.append(sb, "</td></tr></table></td></tr></table>");
- return sb.toString();
- }
-
- public static <T> PageResult createPage(Collection<T> elements, int page, int elementsPerPage, Function<Integer, String> pagerFunction, Function<T, String> bodyFunction)
- {
- return createPage(elements, elements.size(), page, elementsPerPage, pagerFunction, bodyFunction);
- }
-
- public static <T> PageResult createPage(T[] elements, int page, int elementsPerPage, Function<Integer, String> pagerFunction, Function<T, String> bodyFunction)
- {
- return createPage(Arrays.asList(elements), elements.length, page, elementsPerPage, pagerFunction, bodyFunction);
- }
-
- public static <T> PageResult createPage(Iterable<T> elements, int size, int page, int elementsPerPage, Function<Integer, String> pagerFunction, Function<T, String> bodyFunction)
- {
- int pages = size / elementsPerPage;
- if ((elementsPerPage * pages) < size)
- {
- pages++;
- }
-
- final StringBuilder pagerTemplate = new StringBuilder();
- if (pages > 1)
- {
- int breakit = 0;
- for (int i = 0; i < pages; i++)
- {
- pagerTemplate.append(pagerFunction.apply(i));
- breakit++;
-
- if (breakit > 5)
- {
- pagerTemplate.append("</tr><tr>");
- breakit = 0;
- }
- }
- }
-
- if (page >= pages)
- {
- page = pages - 1;
- }
-
- int start = 0;
- if (page > 0)
- {
- start = elementsPerPage * page;
- }
-
- final StringBuilder sb = new StringBuilder();
- int i = 0;
- for (T element : elements)
- {
- if (i++ < start)
- {
- continue;
- }
-
- sb.append(bodyFunction.apply(element));
-
- if (i >= (elementsPerPage + start))
- {
- break;
- }
- }
- return new PageResult(pages, pagerTemplate, sb);
- }
- }
|