HtmlUtil.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2004-2014 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 com.l2jserver.gameserver.model.PageResult;
  23. import com.l2jserver.gameserver.model.interfaces.IProcedure;
  24. /**
  25. * A class containing useful methods for constructing HTML
  26. * @author Nos
  27. */
  28. public class HtmlUtil
  29. {
  30. /**
  31. * Gets the HTML representation of CP gauge.
  32. * @param width the width
  33. * @param current the current value
  34. * @param max the max value
  35. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  36. * @return the HTML
  37. */
  38. public static String getCpGauge(int width, long current, long max, boolean displayAsPercentage)
  39. {
  40. 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);
  41. }
  42. /**
  43. * Gets the HTML representation of HP gauge.
  44. * @param width the width
  45. * @param current the current value
  46. * @param max the max value
  47. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  48. * @return the HTML
  49. */
  50. public static String getHpGauge(int width, long current, long max, boolean displayAsPercentage)
  51. {
  52. 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);
  53. }
  54. /**
  55. * Gets the HTML representation of HP Warn gauge.
  56. * @param width the width
  57. * @param current the current value
  58. * @param max the max value
  59. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  60. * @return the HTML
  61. */
  62. public static String getHpWarnGauge(int width, long current, long max, boolean displayAsPercentage)
  63. {
  64. 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);
  65. }
  66. /**
  67. * Gets the HTML representation of HP Fill gauge.
  68. * @param width the width
  69. * @param current the current value
  70. * @param max the max value
  71. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  72. * @return the HTML
  73. */
  74. public static String getHpFillGauge(int width, long current, long max, boolean displayAsPercentage)
  75. {
  76. 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);
  77. }
  78. /**
  79. * Gets the HTML representation of MP Warn gauge.
  80. * @param width the width
  81. * @param current the current value
  82. * @param max the max value
  83. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  84. * @return the HTML
  85. */
  86. public static String getMpGauge(int width, long current, long max, boolean displayAsPercentage)
  87. {
  88. 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);
  89. }
  90. /**
  91. * Gets the HTML representation of EXP Warn gauge.
  92. * @param width the width
  93. * @param current the current value
  94. * @param max the max value
  95. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  96. * @return the HTML
  97. */
  98. public static String getExpGauge(int width, long current, long max, boolean displayAsPercentage)
  99. {
  100. 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);
  101. }
  102. /**
  103. * Gets the HTML representation of Food gauge.
  104. * @param width the width
  105. * @param current the current value
  106. * @param max the max value
  107. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  108. * @return the HTML
  109. */
  110. public static String getFoodGauge(int width, long current, long max, boolean displayAsPercentage)
  111. {
  112. 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);
  113. }
  114. /**
  115. * Gets the HTML representation of Weight gauge automatically changing level depending on current/max.
  116. * @param width the width
  117. * @param current the current value
  118. * @param max the max value
  119. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  120. * @return the HTML
  121. */
  122. public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage)
  123. {
  124. return getWeightGauge(width, current, max, displayAsPercentage, Util.map(current, 0, max, 1, 5));
  125. }
  126. /**
  127. * Gets the HTML representation of Weight gauge.
  128. * @param width the width
  129. * @param current the current value
  130. * @param max the max value
  131. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  132. * @param level a number from 1 to 5 for the 5 different colors of weight gauge
  133. * @return the HTML
  134. */
  135. public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage, long level)
  136. {
  137. 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);
  138. }
  139. /**
  140. * Gets the HTML representation of a gauge.
  141. * @param width the width
  142. * @param current the current value
  143. * @param max the max value
  144. * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
  145. * @param backgroundImage the background image
  146. * @param image the foreground image
  147. * @param imageHeight the image height
  148. * @param top the top adjustment
  149. * @return the HTML
  150. */
  151. private static String getGauge(int width, long current, long max, boolean displayAsPercentage, String backgroundImage, String image, long imageHeight, long top)
  152. {
  153. current = Math.min(current, max);
  154. final StringBuilder sb = new StringBuilder();
  155. sb.append("<table width=");
  156. sb.append(width);
  157. sb.append(" cellpadding=0 cellspacing=0>");
  158. sb.append("<tr>");
  159. sb.append("<td background=\"");
  160. sb.append(backgroundImage);
  161. sb.append("\">");
  162. sb.append("<img src=\"");
  163. sb.append(image);
  164. sb.append("\" width=");
  165. sb.append((long) (((double) current / max) * width));
  166. sb.append(" height=");
  167. sb.append(imageHeight);
  168. sb.append(">");
  169. sb.append("</td>");
  170. sb.append("</tr>");
  171. sb.append("<tr>");
  172. sb.append("<td align=center>");
  173. sb.append("<table cellpadding=0 cellspacing=");
  174. sb.append(top);
  175. sb.append(">");
  176. sb.append("<tr>");
  177. sb.append("<td>");
  178. if (displayAsPercentage)
  179. {
  180. sb.append("<table cellpadding=0 cellspacing=2>");
  181. sb.append("<tr><td>");
  182. sb.append(String.format("%.2f%%", ((double) current / max) * 100));
  183. sb.append("</td></tr>");
  184. sb.append("</table>");
  185. }
  186. else
  187. {
  188. int tdWidth = (width - 10) / 2;
  189. sb.append("<table cellpadding=0 cellspacing=0>");
  190. sb.append("<tr>");
  191. sb.append("<td width=");
  192. sb.append(tdWidth);
  193. sb.append(" align=right>");
  194. sb.append(current);
  195. sb.append("</td>");
  196. sb.append("<td width=10 align=center>/</td>");
  197. sb.append("<td width=");
  198. sb.append(tdWidth);
  199. sb.append(">");
  200. sb.append(max);
  201. sb.append("</td>");
  202. sb.append("</tr>");
  203. sb.append("</table>");
  204. }
  205. sb.append("</td>");
  206. sb.append("</tr>");
  207. sb.append("</table>");
  208. sb.append("</td>");
  209. sb.append("</tr>");
  210. sb.append("</table>");
  211. return sb.toString();
  212. }
  213. public static <T> PageResult createPage(Collection<T> elements, int page, int elementsPerPage, IProcedure<Integer, String> pagerProcedure, IProcedure<T, String> bodyProcedure)
  214. {
  215. return createPage(elements, elements.size(), page, elementsPerPage, pagerProcedure, bodyProcedure);
  216. }
  217. public static <T> PageResult createPage(T[] elements, int page, int elementsPerPage, IProcedure<Integer, String> pagerProcedure, IProcedure<T, String> bodyProcedure)
  218. {
  219. return createPage(Arrays.asList(elements), elements.length, page, elementsPerPage, pagerProcedure, bodyProcedure);
  220. }
  221. public static <T> PageResult createPage(Iterable<T> elements, int size, int page, int elementsPerPage, IProcedure<Integer, String> pagerProcedure, IProcedure<T, String> bodyProcedure)
  222. {
  223. int pages = size / elementsPerPage;
  224. if ((elementsPerPage * pages) < size)
  225. {
  226. pages++;
  227. }
  228. final StringBuilder pagerTemplate = new StringBuilder();
  229. if (pages > 1)
  230. {
  231. for (int i = 0; i < pages; i++)
  232. {
  233. pagerTemplate.append(pagerProcedure.execute(i));
  234. }
  235. }
  236. if (page >= pages)
  237. {
  238. page = pages - 1;
  239. }
  240. int start = 0;
  241. if (page > 0)
  242. {
  243. start = elementsPerPage * page;
  244. }
  245. final StringBuilder sb = new StringBuilder();
  246. int i = 0;
  247. for (T element : elements)
  248. {
  249. if (i++ < start)
  250. {
  251. continue;
  252. }
  253. sb.append(bodyProcedure.execute(element));
  254. if (i >= (elementsPerPage + start))
  255. {
  256. break;
  257. }
  258. }
  259. return new PageResult(pages, pagerTemplate, sb);
  260. }
  261. }