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.

FeatureView.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package com.vaadin.demo.sampler;
  2. import java.util.HashMap;
  3. import com.vaadin.demo.sampler.ActiveLink.LinkActivatedEvent;
  4. import com.vaadin.demo.sampler.ActiveLink.LinkActivatedListener;
  5. import com.vaadin.demo.sampler.SamplerApplication.SamplerWindow;
  6. import com.vaadin.terminal.ExternalResource;
  7. import com.vaadin.terminal.ThemeResource;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.Component;
  10. import com.vaadin.ui.HorizontalLayout;
  11. import com.vaadin.ui.Label;
  12. import com.vaadin.ui.Link;
  13. import com.vaadin.ui.Panel;
  14. import com.vaadin.ui.VerticalLayout;
  15. import com.vaadin.ui.Window;
  16. import com.vaadin.ui.Button.ClickEvent;
  17. public class FeatureView extends HorizontalLayout {
  18. private static final String MSG_SHOW_SRC = "Show Java™ source »";
  19. private Panel right;
  20. private VerticalLayout left;
  21. private VerticalLayout controls;
  22. private ActiveLink showSrc;
  23. private HashMap<Feature, Component> exampleCache = new HashMap<Feature, Component>();
  24. private Feature currentFeature;
  25. private Window srcWindow;
  26. public FeatureView() {
  27. setWidth("100%");
  28. setMargin(true);
  29. setSpacing(true);
  30. setStyleName("sample-view");
  31. left = new VerticalLayout();
  32. left.setWidth("100%");
  33. left.setSpacing(true);
  34. left.setMargin(false);
  35. addComponent(left);
  36. setExpandRatio(left, 1);
  37. right = new Panel();
  38. right.getLayout().setMargin(true, false, false, false);
  39. right.setStyleName(Panel.STYLE_LIGHT);
  40. right.addStyleName("feature-info");
  41. right.setWidth("369px");
  42. addComponent(right);
  43. controls = new VerticalLayout();
  44. controls.setWidth("100%");
  45. controls.setStyleName("feature-controls");
  46. HorizontalLayout controlButtons = new HorizontalLayout();
  47. controls.addComponent(controlButtons);
  48. Button resetExample = new Button("Reset example",
  49. new Button.ClickListener() {
  50. public void buttonClick(ClickEvent event) {
  51. resetExample();
  52. }
  53. });
  54. resetExample.setStyleName(Button.STYLE_LINK);
  55. resetExample.addStyleName("showcode");
  56. controlButtons.addComponent(resetExample);
  57. controlButtons.addComponent(new Label("|"));
  58. showSrc = new ActiveLink();
  59. showSrc
  60. .setDescription("Right / middle / ctrl / shift -click for browser window/tab");
  61. showSrc.addListener(new LinkActivatedListener() {
  62. public void linkActivated(LinkActivatedEvent event) {
  63. if (!event.isLinkOpened()) {
  64. showSource(currentFeature.getSource());
  65. }
  66. }
  67. });
  68. showSrc.setCaption(MSG_SHOW_SRC);
  69. showSrc.addStyleName("showcode");
  70. showSrc.setTargetBorder(Link.TARGET_BORDER_NONE);
  71. controlButtons.addComponent(showSrc);
  72. }
  73. public void showSource(String source) {
  74. if (srcWindow == null) {
  75. srcWindow = new Window("Java™ source");
  76. ((VerticalLayout) srcWindow.getLayout()).setSizeUndefined();
  77. srcWindow.setWidth("70%");
  78. srcWindow.setHeight("60%");
  79. srcWindow.setPositionX(100);
  80. srcWindow.setPositionY(100);
  81. }
  82. srcWindow.removeAllComponents();
  83. srcWindow.addComponent(new CodeLabel(source));
  84. if (srcWindow.getParent() == null) {
  85. getWindow().addWindow(srcWindow);
  86. }
  87. }
  88. private void resetExample() {
  89. if (currentFeature != null) {
  90. Feature f = currentFeature;
  91. currentFeature = null;
  92. exampleCache.remove(f);
  93. setFeature(f);
  94. }
  95. }
  96. public void setFeature(Feature feature) {
  97. if (feature != currentFeature) {
  98. currentFeature = feature;
  99. right.removeAllComponents();
  100. left.removeAllComponents();
  101. left.addComponent(controls);
  102. controls.setCaption(feature.getName());
  103. left.addComponent(getExampleFor(feature));
  104. right.setCaption("Description and Resources");
  105. final Feature parent = (Feature) SamplerApplication
  106. .getAllFeatures().getParent(feature);
  107. String desc = parent.getDescription();
  108. boolean hasParentDesc = false;
  109. final Label parentLabel = new Label(parent.getDescription());
  110. if (desc != null && desc != "") {
  111. parentLabel.setContentMode(Label.CONTENT_XHTML);
  112. right.addComponent(parentLabel);
  113. hasParentDesc = true;
  114. }
  115. desc = feature.getDescription();
  116. if (desc != null && desc != "") {
  117. // Sample description uses additional decorations if a parent
  118. // description is found
  119. final Label l = new Label(
  120. "<div class=\"outer-deco\"><div class=\"deco\"><span class=\"deco\"></span>"
  121. + desc + "</div></div>", Label.CONTENT_XHTML);
  122. right.addComponent(l);
  123. if (hasParentDesc) {
  124. l.setStyleName("sample-description");
  125. } else {
  126. l.setStyleName("description");
  127. }
  128. }
  129. { // open src in new window -link
  130. String path = SamplerApplication.getPathFor(currentFeature);
  131. showSrc.setTargetName(path);
  132. showSrc.setResource(new ExternalResource(getApplication()
  133. .getURL()
  134. + "src/" + path));
  135. }
  136. NamedExternalResource[] resources = feature.getRelatedResources();
  137. if (resources != null) {
  138. VerticalLayout res = new VerticalLayout();
  139. Label caption = new Label("<span>Additional Resources</span>",
  140. Label.CONTENT_XHTML);
  141. caption.setStyleName("section");
  142. caption.setWidth("100%");
  143. res.addComponent(caption);
  144. res.setMargin(false, false, true, false);
  145. for (NamedExternalResource r : resources) {
  146. final Link l = new Link(r.getName(), r);
  147. l
  148. .setIcon(new ThemeResource(
  149. "../default/icons/16/note.png"));
  150. res.addComponent(l);
  151. }
  152. right.addComponent(res);
  153. }
  154. APIResource[] apis = feature.getRelatedAPI();
  155. if (apis != null) {
  156. VerticalLayout api = new VerticalLayout();
  157. Label caption = new Label("<span>API Documentation</span>",
  158. Label.CONTENT_XHTML);
  159. caption.setStyleName("section");
  160. caption.setWidth("100%");
  161. api.addComponent(caption);
  162. api.setMargin(false, false, true, false);
  163. for (APIResource r : apis) {
  164. final Link l = new Link(r.getName(), r);
  165. l.setIcon(new ThemeResource(
  166. "../default/icons/16/document-txt.png"));
  167. api.addComponent(l);
  168. }
  169. right.addComponent(api);
  170. }
  171. Class[] features = feature.getRelatedFeatures();
  172. if (features != null) {
  173. VerticalLayout rel = new VerticalLayout();
  174. Label caption = new Label("<span>Related Samples</span>",
  175. Label.CONTENT_XHTML);
  176. caption.setStyleName("section");
  177. caption.setWidth("100%");
  178. rel.addComponent(caption);
  179. rel.setMargin(false, false, true, false);
  180. for (Class c : features) {
  181. final Feature f = SamplerApplication.getFeatureFor(c);
  182. if (f != null) {
  183. String path = SamplerApplication.getPathFor(f);
  184. ActiveLink al = new ActiveLink(f.getName(),
  185. new ExternalResource(getApplication().getURL()
  186. + "#" + path));
  187. al.setIcon(new ThemeResource(
  188. (f instanceof FeatureSet ? "icons/category.gif"
  189. : "icons/sample.png")));
  190. al.addListener(new LinkActivatedListener() {
  191. public void linkActivated(LinkActivatedEvent event) {
  192. if (event.isLinkOpened()) {
  193. getWindow()
  194. .showNotification(
  195. f.getName()
  196. + " opened if new window/tab");
  197. } else {
  198. SamplerWindow w = (SamplerWindow) getWindow();
  199. w.setFeature(f);
  200. }
  201. }
  202. });
  203. rel.addComponent(al);
  204. }
  205. }
  206. right.addComponent(rel);
  207. }
  208. }
  209. }
  210. private Component getExampleFor(Feature f) {
  211. Component ex = exampleCache.get(f);
  212. if (ex == null) {
  213. ex = f.getExample();
  214. exampleCache.put(f, ex);
  215. }
  216. return ex;
  217. }
  218. }