HtmlUtil.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.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.
  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. * @param type a number from 1 to 5 for the 5 different colors of weight gauge
  121. * @return the HTML
  122. */
  123. public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage, long type)
  124. {
  125. return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_bg_Center" + type, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_Center" + type, 17, -13);
  126. }
  127. /**
  128. * Gets the HTML representation of a 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 backgroundImage the background image
  134. * @param image the foreground image
  135. * @param imageHeight the image height
  136. * @param top the top adjustment
  137. * @return the HTML
  138. */
  139. private static String getGauge(int width, long current, long max, boolean displayAsPercentage, String backgroundImage, String image, long imageHeight, long top)
  140. {
  141. current = Math.min(current, max);
  142. final StringBuilder sb = new StringBuilder();
  143. sb.append("<table width=");
  144. sb.append(width);
  145. sb.append(" cellpadding=0 cellspacing=0>");
  146. sb.append("<tr>");
  147. sb.append("<td background=\"");
  148. sb.append(backgroundImage);
  149. sb.append("\">");
  150. sb.append("<img src=\"");
  151. sb.append(image);
  152. sb.append("\" width=");
  153. sb.append((long) (((double) current / max) * width));
  154. sb.append(" height=");
  155. sb.append(imageHeight);
  156. sb.append(">");
  157. sb.append("</td>");
  158. sb.append("</tr>");
  159. sb.append("<tr>");
  160. sb.append("<td align=center>");
  161. sb.append("<table cellpadding=0 cellspacing=");
  162. sb.append(top);
  163. sb.append(">");
  164. sb.append("<tr>");
  165. sb.append("<td>");
  166. if (displayAsPercentage)
  167. {
  168. sb.append("<table cellpadding=0 cellspacing=2>");
  169. sb.append("<tr><td>");
  170. sb.append(String.format("%.2f%%", ((double) current / max) * 100));
  171. sb.append("</td></tr>");
  172. sb.append("</table>");
  173. }
  174. else
  175. {
  176. int tdWidth = (width - 10) / 2;
  177. sb.append("<table cellpadding=0 cellspacing=0>");
  178. sb.append("<tr>");
  179. sb.append("<td width=");
  180. sb.append(tdWidth);
  181. sb.append(" align=right>");
  182. sb.append(current);
  183. sb.append("</td>");
  184. sb.append("<td width=10 align=center>/</td>");
  185. sb.append("<td width=");
  186. sb.append(tdWidth);
  187. sb.append(">");
  188. sb.append(max);
  189. sb.append("</td>");
  190. sb.append("</tr>");
  191. sb.append("</table>");
  192. }
  193. sb.append("</td>");
  194. sb.append("</tr>");
  195. sb.append("</table>");
  196. sb.append("</td>");
  197. sb.append("</tr>");
  198. sb.append("</table>");
  199. return sb.toString();
  200. }
  201. public static <T> PageResult createPage(Collection<T> elements, int page, int elementsPerPage, IProcedure<Integer, String> pagerProcedure, IProcedure<T, String> bodyProcedure)
  202. {
  203. return createPage(elements, elements.size(), page, elementsPerPage, pagerProcedure, bodyProcedure);
  204. }
  205. public static <T> PageResult createPage(T[] elements, int page, int elementsPerPage, IProcedure<Integer, String> pagerProcedure, IProcedure<T, String> bodyProcedure)
  206. {
  207. return createPage(Arrays.asList(elements), elements.length, page, elementsPerPage, pagerProcedure, bodyProcedure);
  208. }
  209. public static <T> PageResult createPage(Iterable<T> elements, int size, int page, int elementsPerPage, IProcedure<Integer, String> pagerProcedure, IProcedure<T, String> bodyProcedure)
  210. {
  211. int pages = size / elementsPerPage;
  212. if ((elementsPerPage * pages) < size)
  213. {
  214. pages++;
  215. }
  216. final StringBuilder pagerTemplate = new StringBuilder();
  217. if (pages > 1)
  218. {
  219. for (int i = 0; i < pages; i++)
  220. {
  221. pagerTemplate.append(pagerProcedure.execute(i));
  222. }
  223. }
  224. if (page >= pages)
  225. {
  226. page = pages - 1;
  227. }
  228. int start = 0;
  229. if (page > 0)
  230. {
  231. start = elementsPerPage * page;
  232. }
  233. final StringBuilder sb = new StringBuilder();
  234. int i = 0;
  235. for (T element : elements)
  236. {
  237. if (i++ < start)
  238. {
  239. continue;
  240. }
  241. sb.append(bodyProcedure.execute(element));
  242. if (i >= (elementsPerPage + start))
  243. {
  244. break;
  245. }
  246. }
  247. return new PageResult(pages, pagerTemplate, sb);
  248. }
  249. }