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.

OptimizedWidgetsetPanel.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2000-2014 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.debug.internal;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import java.util.logging.Logger;
  20. import com.google.gwt.user.client.ui.FlowPanel;
  21. import com.google.gwt.user.client.ui.HTML;
  22. import com.vaadin.client.ApplicationConfiguration;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.ServerConnector;
  25. import com.vaadin.client.Util;
  26. import com.vaadin.client.ui.UnknownComponentConnector;
  27. /**
  28. * Optimized widgetset view panel of the debug window.
  29. *
  30. * @since 7.1.4
  31. */
  32. public class OptimizedWidgetsetPanel extends FlowPanel {
  33. /**
  34. * Update the panel contents based on the connectors that have been used so
  35. * far on this execution of the application.
  36. */
  37. public void update() {
  38. clear();
  39. HTML h = new HTML("Getting used connectors");
  40. add(h);
  41. String s = "";
  42. for (ApplicationConnection ac : ApplicationConfiguration
  43. .getRunningApplications()) {
  44. ApplicationConfiguration conf = ac.getConfiguration();
  45. s += "<h1>Used connectors for "
  46. + Util.escapeHTML(conf.getServiceUrl()) + "</h1>";
  47. for (String connectorName : getUsedConnectorNames(conf)) {
  48. s += Util.escapeHTML(connectorName) + "<br/>";
  49. }
  50. s += "<h2>To make an optimized widgetset based on these connectors, do:</h2>";
  51. s += "<h3>1. Add to your widgetset.gwt.xml file:</h2>";
  52. s += "<textarea rows=\"3\" style=\"width:90%\">";
  53. s += "<generate-with class=\"OptimizedConnectorBundleLoaderFactory\">\n";
  54. s += " <when-type-assignable class=\"com.vaadin.client.metadata.ConnectorBundleLoader\" />\n";
  55. s += "</generate-with>";
  56. s += "</textarea>";
  57. s += "<h3>2. Add the following java file to your project:</h2>";
  58. s += "<textarea rows=\"5\" style=\"width:90%\">";
  59. s += generateOptimizedWidgetSet(getUsedConnectorNames(conf));
  60. s += "</textarea>";
  61. s += "<h3>3. Recompile widgetset</h2>";
  62. }
  63. h.setHTML(s);
  64. }
  65. private Set<String> getUsedConnectorNames(
  66. ApplicationConfiguration configuration) {
  67. int tag = 0;
  68. Set<String> usedConnectors = new HashSet<String>();
  69. while (true) {
  70. String serverSideClass = configuration
  71. .getServerSideClassNameForTag(tag);
  72. if (serverSideClass == null) {
  73. break;
  74. }
  75. Class<? extends ServerConnector> connectorClass = configuration
  76. .getConnectorClassByEncodedTag(tag);
  77. if (connectorClass == null) {
  78. break;
  79. }
  80. if (connectorClass != UnknownComponentConnector.class) {
  81. usedConnectors.add(connectorClass.getName());
  82. }
  83. tag++;
  84. if (tag > 10000) {
  85. // Sanity check
  86. getLogger().severe(
  87. "Search for used connector classes was forcefully terminated");
  88. break;
  89. }
  90. }
  91. return usedConnectors;
  92. }
  93. public String generateOptimizedWidgetSet(Set<String> usedConnectors) {
  94. String s = "import java.util.HashSet;\n";
  95. s += "import java.util.Set;\n";
  96. s += "import com.google.gwt.core.ext.typeinfo.JClassType;\n";
  97. s += "import com.vaadin.client.ui.ui.UIConnector;\n";
  98. s += "import com.vaadin.server.widgetsetutils.ConnectorBundleLoaderFactory;\n";
  99. s += "import com.vaadin.shared.ui.Connect.LoadStyle;\n\n";
  100. s += "public class OptimizedConnectorBundleLoaderFactory extends\n";
  101. s += " ConnectorBundleLoaderFactory {\n";
  102. s += " private Set<String> eagerConnectors = new HashSet<String>();\n";
  103. s += " {\n";
  104. for (String c : usedConnectors) {
  105. s += " eagerConnectors.add(" + Util.escapeHTML(c)
  106. + ".class.getName());\n";
  107. }
  108. s += " }\n";
  109. s += "\n";
  110. s += " @Override\n";
  111. s += " protected LoadStyle getLoadStyle(JClassType connectorType) {\n";
  112. s += " if (eagerConnectors.contains(connectorType.getQualifiedBinaryName())) {\n";
  113. s += " return LoadStyle.EAGER;\n";
  114. s += " } else {\n";
  115. s += " // Loads all other connectors immediately after the initial view has\n";
  116. s += " // been rendered\n";
  117. s += " return LoadStyle.DEFERRED;\n";
  118. s += " }\n";
  119. s += " }\n";
  120. s += "}\n";
  121. return s;
  122. }
  123. private static Logger getLogger() {
  124. return Logger.getLogger(OptimizedWidgetsetPanel.class.getName());
  125. }
  126. }