You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiffStatPanel.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.panels;
  17. import java.text.MessageFormat;
  18. import org.apache.wicket.markup.html.basic.Label;
  19. import org.apache.wicket.markup.html.panel.Panel;
  20. import com.gitblit.utils.DiffUtils;
  21. import com.gitblit.utils.DiffUtils.NormalizedDiffStat;
  22. import com.gitblit.wicket.WicketUtils;
  23. /**
  24. * Display a diffstat.
  25. *
  26. * @author James Moger
  27. *
  28. */
  29. public class DiffStatPanel extends Panel {
  30. private static final long serialVersionUID = 1L;
  31. final int total;
  32. final int insertions;
  33. final int deletions;
  34. final boolean inline;
  35. public DiffStatPanel(String wicketId, int insertions, int deletions) {
  36. this(wicketId, insertions, deletions, false);
  37. }
  38. public DiffStatPanel(String wicketId, int insertions, int deletions, boolean inline) {
  39. super(wicketId);
  40. this.insertions = insertions;
  41. this.deletions = deletions;
  42. this.total = insertions + deletions;
  43. this.inline = inline;
  44. }
  45. @Override
  46. protected void onInitialize() {
  47. super.onInitialize();
  48. final String diffStat = MessageFormat.format(getString("gb.diffStat"), "" + insertions, "" + deletions);
  49. WicketUtils.setHtmlTooltip(this, diffStat);
  50. final NormalizedDiffStat n = DiffUtils.normalizeDiffStat(5, insertions, deletions);
  51. final String square = "■";
  52. add(new Label("total", String.valueOf(total)));
  53. add(new Label("insertions", timesRepeat(n.insertions, square)).setEscapeModelStrings(false).setVisible(n.insertions > 0));
  54. add(new Label("deletions", timesRepeat(n.deletions, square)).setEscapeModelStrings(false).setVisible(n.deletions > 0));
  55. add(new Label("blank", timesRepeat(n.blanks, square)).setEscapeModelStrings(false).setVisible(n.blanks > 0));
  56. if (inline) {
  57. WicketUtils.setCssClass(this, "diffstat-inline");
  58. } else {
  59. WicketUtils.setCssClass(this, "diffstat");
  60. }
  61. setVisible(total > 0);
  62. }
  63. String timesRepeat(int cnt, String s) {
  64. StringBuilder sb = new StringBuilder();
  65. for (int i = 0; i < cnt; i++) {
  66. sb.append(s);
  67. }
  68. return sb.toString();
  69. }
  70. }