HtmlUtil.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.util;
  20. import java.util.Arrays;
  21. import java.util.Collection;
  22. import java.util.function.Function;
  23. import com.l2jserver.gameserver.model.PageResult;
  24. import com.l2jserver.util.StringUtil;
  25. /**
  26. * A class containing useful methods for constructing HTML
  27. * @author NosBit
  28. */
  29. public class HtmlUtil
  30. {
  31. /**
  32. * Gets the HTML representation of CP gauge.
  33. * @param width the width
  34. * @param current the current value
  35. * @param max the max value
  36. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  37. * @return the HTML
  38. */
  39. public static String getCpGauge(int width, long current, long max, boolean displayAsPercentage)
  40. {
  41. 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);
  42. }
  43. /**
  44. * Gets the HTML representation of HP gauge.
  45. * @param width the width
  46. * @param current the current value
  47. * @param max the max value
  48. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  49. * @return the HTML
  50. */
  51. public static String getHpGauge(int width, long current, long max, boolean displayAsPercentage)
  52. {
  53. 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);
  54. }
  55. /**
  56. * Gets the HTML representation of HP Warn gauge.
  57. * @param width the width
  58. * @param current the current value
  59. * @param max the max value
  60. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  61. * @return the HTML
  62. */
  63. public static String getHpWarnGauge(int width, long current, long max, boolean displayAsPercentage)
  64. {
  65. 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);
  66. }
  67. /**
  68. * Gets the HTML representation of HP Fill gauge.
  69. * @param width the width
  70. * @param current the current value
  71. * @param max the max value
  72. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  73. * @return the HTML
  74. */
  75. public static String getHpFillGauge(int width, long current, long max, boolean displayAsPercentage)
  76. {
  77. 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);
  78. }
  79. /**
  80. * Gets the HTML representation of MP Warn gauge.
  81. * @param width the width
  82. * @param current the current value
  83. * @param max the max value
  84. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  85. * @return the HTML
  86. */
  87. public static String getMpGauge(int width, long current, long max, boolean displayAsPercentage)
  88. {
  89. 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);
  90. }
  91. /**
  92. * Gets the HTML representation of EXP Warn gauge.
  93. * @param width the width
  94. * @param current the current value
  95. * @param max the max value
  96. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  97. * @return the HTML
  98. */
  99. public static String getExpGauge(int width, long current, long max, boolean displayAsPercentage)
  100. {
  101. 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);
  102. }
  103. /**
  104. * Gets the HTML representation of Food gauge.
  105. * @param width the width
  106. * @param current the current value
  107. * @param max the max value
  108. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  109. * @return the HTML
  110. */
  111. public static String getFoodGauge(int width, long current, long max, boolean displayAsPercentage)
  112. {
  113. 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);
  114. }
  115. /**
  116. * Gets the HTML representation of Weight gauge automatically changing level depending on current/max.
  117. * @param width the width
  118. * @param current the current value
  119. * @param max the max value
  120. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  121. * @return the HTML
  122. */
  123. public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage)
  124. {
  125. return getWeightGauge(width, current, max, displayAsPercentage, Util.map(current, 0, max, 1, 5));
  126. }
  127. /**
  128. * Gets the HTML representation of Weight gauge.
  129. * @param width the width
  130. * @param current the current value
  131. * @param max the max value
  132. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  133. * @param level a number from 1 to 5 for the 5 different colors of weight gauge
  134. * @return the HTML
  135. */
  136. public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage, long level)
  137. {
  138. 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);
  139. }
  140. /**
  141. * Gets the HTML representation of a gauge.
  142. * @param width the width
  143. * @param current the current value
  144. * @param max the max value
  145. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  146. * @param backgroundImage the background image
  147. * @param image the foreground image
  148. * @param imageHeight the image height
  149. * @param top the top adjustment
  150. * @return the HTML
  151. */
  152. private static String getGauge(int width, long current, long max, boolean displayAsPercentage, String backgroundImage, String image, long imageHeight, long top)
  153. {
  154. current = Math.min(current, max);
  155. final StringBuilder sb = new StringBuilder();
  156. StringUtil.append(sb, "<table width=", String.valueOf(width), " cellpadding=0 cellspacing=0><tr><td background=\"" + backgroundImage + "\">");
  157. StringUtil.append(sb, "<img src=\"" + image + "\" width=", String.valueOf((long) (((double) current / max) * width)), " height=", String.valueOf(imageHeight), ">");
  158. StringUtil.append(sb, "</td></tr><tr><td align=center><table cellpadding=0 cellspacing=", String.valueOf(top), "><tr><td>");
  159. if (displayAsPercentage)
  160. {
  161. StringUtil.append(sb, "<table cellpadding=0 cellspacing=2><tr><td>", String.format("%.2f%%", ((double) current / max) * 100), "</td></tr></table>");
  162. }
  163. else
  164. {
  165. final String tdWidth = String.valueOf((width - 10) / 2);
  166. StringUtil.append(sb, "<table cellpadding=0 cellspacing=0><tr><td width=" + tdWidth + " align=right>", String.valueOf(current), "</td>");
  167. StringUtil.append(sb, "<td width=10 align=center>/</td><td width=" + tdWidth + ">", String.valueOf(max), "</td></tr></table>");
  168. }
  169. StringUtil.append(sb, "</td></tr></table></td></tr></table>");
  170. return sb.toString();
  171. }
  172. public static <T> PageResult createPage(Collection<T> elements, int page, int elementsPerPage, Function<Integer, String> pagerFunction, Function<T, String> bodyFunction)
  173. {
  174. return createPage(elements, elements.size(), page, elementsPerPage, pagerFunction, bodyFunction);
  175. }
  176. public static <T> PageResult createPage(T[] elements, int page, int elementsPerPage, Function<Integer, String> pagerFunction, Function<T, String> bodyFunction)
  177. {
  178. return createPage(Arrays.asList(elements), elements.length, page, elementsPerPage, pagerFunction, bodyFunction);
  179. }
  180. public static <T> PageResult createPage(Iterable<T> elements, int size, int page, int elementsPerPage, Function<Integer, String> pagerFunction, Function<T, String> bodyFunction)
  181. {
  182. int pages = size / elementsPerPage;
  183. if ((elementsPerPage * pages) < size)
  184. {
  185. pages++;
  186. }
  187. final StringBuilder pagerTemplate = new StringBuilder();
  188. if (pages > 1)
  189. {
  190. int breakit = 0;
  191. for (int i = 0; i < pages; i++)
  192. {
  193. pagerTemplate.append(pagerFunction.apply(i));
  194. breakit++;
  195. if (breakit > 5)
  196. {
  197. pagerTemplate.append("</tr><tr>");
  198. breakit = 0;
  199. }
  200. }
  201. }
  202. if (page >= pages)
  203. {
  204. page = pages - 1;
  205. }
  206. int start = 0;
  207. if (page > 0)
  208. {
  209. start = elementsPerPage * page;
  210. }
  211. final StringBuilder sb = new StringBuilder();
  212. int i = 0;
  213. for (T element : elements)
  214. {
  215. if (i++ < start)
  216. {
  217. continue;
  218. }
  219. sb.append(bodyFunction.apply(element));
  220. if (i >= (elementsPerPage + start))
  221. {
  222. break;
  223. }
  224. }
  225. return new PageResult(pages, pagerTemplate, sb);
  226. }
  227. }