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.

TreeRendererConnector.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.connectors.grid;
  17. import com.google.gwt.safehtml.shared.SafeHtmlUtils;
  18. import com.google.gwt.user.client.Element;
  19. import com.vaadin.client.annotations.OnStateChange;
  20. import com.vaadin.client.renderers.HtmlRenderer;
  21. import com.vaadin.client.renderers.Renderer;
  22. import com.vaadin.client.widget.grid.RendererCellReference;
  23. import com.vaadin.shared.ui.Connect;
  24. import com.vaadin.shared.ui.tree.TreeRendererState;
  25. import com.vaadin.ui.Tree.TreeRenderer;
  26. import elemental.json.JsonObject;
  27. /**
  28. * Connector for TreeRenderer.
  29. *
  30. * @author Vaadin Ltd
  31. * @since 8.1
  32. */
  33. @Connect(TreeRenderer.class)
  34. public class TreeRendererConnector
  35. extends AbstractGridRendererConnector<String> {
  36. @Override
  37. public Renderer<String> createRenderer() {
  38. return new HtmlRenderer() {
  39. @Override
  40. public void render(RendererCellReference cell, String htmlString) {
  41. String content = "<span class=\"v-captiontext\">"
  42. + getContentString(htmlString) + "</span>";
  43. JsonObject row = getParent().getParent().getDataSource()
  44. .getRow(cell.getRowIndex());
  45. if (row != null && row.hasKey("itemIcon")) {
  46. String resourceId = row.getString("itemIcon");
  47. Element element = getConnection()
  48. .getIcon(getResourceUrl(resourceId)).getElement();
  49. content = element.getString() + content;
  50. }
  51. super.render(cell, content);
  52. }
  53. private String getContentString(String htmlString) {
  54. switch (getState().mode) {
  55. case HTML:
  56. return htmlString;
  57. case PREFORMATTED:
  58. return "<pre>" + SafeHtmlUtils.htmlEscape(htmlString)
  59. + "</pre>";
  60. default:
  61. return SafeHtmlUtils.htmlEscape(htmlString);
  62. }
  63. }
  64. };
  65. }
  66. @OnStateChange("mode")
  67. void updateContentMode() {
  68. // Redraw content
  69. getParent().getParent().getWidget().requestRefreshBody();
  70. // Some pre-formatted content might change size of content.
  71. getParent().getParent().getWidget().recalculateColumnWidths();
  72. }
  73. @Override
  74. public ColumnConnector getParent() {
  75. return (ColumnConnector) super.getParent();
  76. }
  77. @Override
  78. public TreeRendererState getState() {
  79. return (TreeRendererState) super.getState();
  80. }
  81. }