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.

PortletDemo.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. *
  3. */
  4. package com.vaadin.demo;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import javax.portlet.ActionRequest;
  8. import javax.portlet.ActionResponse;
  9. import javax.portlet.PortletMode;
  10. import javax.portlet.PortletRequest;
  11. import javax.portlet.PortletURL;
  12. import javax.portlet.RenderRequest;
  13. import javax.portlet.RenderResponse;
  14. import javax.portlet.WindowState;
  15. import com.vaadin.Application;
  16. import com.vaadin.terminal.ExternalResource;
  17. import com.vaadin.terminal.gwt.server.PortletApplicationContext;
  18. import com.vaadin.terminal.gwt.server.PortletApplicationContext.PortletListener;
  19. import com.vaadin.ui.Label;
  20. import com.vaadin.ui.Link;
  21. import com.vaadin.ui.TextField;
  22. import com.vaadin.ui.Window;
  23. import com.vaadin.ui.Window.Notification;
  24. /**
  25. * @author marc
  26. *
  27. */
  28. public class PortletDemo extends Application {
  29. Window main = new Window();
  30. TextField tf = new TextField("Some value");
  31. Label userInfo = new Label();
  32. Link portletEdit = new Link();
  33. Link portletMax = new Link();
  34. Link someAction = null;
  35. public void init() {
  36. main = new Window();
  37. setMainWindow(main);
  38. userInfo.setCaption("User info");
  39. userInfo.setContentMode(Label.CONTENT_PREFORMATTED);
  40. main.addComponent(userInfo);
  41. tf.setEnabled(false);
  42. tf.setImmediate(true);
  43. main.addComponent(tf);
  44. portletEdit.setEnabled(false);
  45. main.addComponent(portletEdit);
  46. portletMax.setEnabled(false);
  47. main.addComponent(portletMax);
  48. if (getContext() instanceof PortletApplicationContext) {
  49. PortletApplicationContext ctx = (PortletApplicationContext) getContext();
  50. ctx.addPortletListener(this, new DemoPortletListener());
  51. } else {
  52. getMainWindow().showNotification("Not inited via Portal!",
  53. Notification.TYPE_ERROR_MESSAGE);
  54. }
  55. }
  56. private class DemoPortletListener implements PortletListener {
  57. public void handleActionRequest(ActionRequest request,
  58. ActionResponse response) {
  59. main.addComponent(new Label("Action received"));
  60. }
  61. public void handleRenderRequest(RenderRequest request,
  62. RenderResponse response) {
  63. // Portlet up-and-running, enable stuff
  64. portletEdit.setEnabled(true);
  65. portletMax.setEnabled(true);
  66. // Editable if we're in editmode
  67. tf.setEnabled((request.getPortletMode() == PortletMode.EDIT));
  68. // Show notification about current mode and state
  69. getMainWindow().showNotification(
  70. "Portlet status",
  71. "Mode: " + request.getPortletMode() + " State: "
  72. + request.getWindowState(),
  73. Notification.TYPE_WARNING_MESSAGE);
  74. // Display current user info
  75. Map uinfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
  76. if (uinfo != null) {
  77. String s = "";
  78. for (Iterator it = uinfo.keySet().iterator(); it.hasNext();) {
  79. Object key = it.next();
  80. Object val = uinfo.get(key);
  81. s += key + ": " + val + "\n";
  82. }
  83. if (request.isUserInRole("administrator")) {
  84. s += "(administrator)";
  85. }
  86. userInfo.setValue(s);
  87. } else {
  88. userInfo.setValue("-");
  89. }
  90. // Create Edit/Done link (actionUrl)
  91. PortletURL url = response.createActionURL();
  92. try {
  93. url
  94. .setPortletMode((request.getPortletMode() == PortletMode.VIEW ? PortletMode.EDIT
  95. : PortletMode.VIEW));
  96. portletEdit.setResource(new ExternalResource(url.toString()));
  97. portletEdit
  98. .setCaption((request.getPortletMode() == PortletMode.VIEW ? "Edit"
  99. : "Done"));
  100. } catch (Exception e) {
  101. portletEdit.setEnabled(false);
  102. }
  103. // Create Maximize/Normal link (actionUrl)
  104. url = response.createActionURL();
  105. try {
  106. url
  107. .setWindowState((request.getWindowState() == WindowState.NORMAL ? WindowState.MAXIMIZED
  108. : WindowState.NORMAL));
  109. portletMax.setResource(new ExternalResource(url.toString()));
  110. portletMax
  111. .setCaption((request.getWindowState() == WindowState.NORMAL ? "Maximize"
  112. : "Back to normal"));
  113. } catch (Exception e) {
  114. portletMax.setEnabled(false);
  115. }
  116. if (someAction == null) {
  117. url = response.createActionURL();
  118. try {
  119. someAction = new Link("An action", new ExternalResource(url
  120. .toString()));
  121. main.addComponent(someAction);
  122. } catch (Exception e) {
  123. // Oops
  124. System.err.println("Could not create someAction: " + e);
  125. }
  126. }
  127. }
  128. }
  129. }