Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Page.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.Serializable;
  6. import java.lang.reflect.Method;
  7. import java.util.EventObject;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import com.vaadin.event.EventRouter;
  12. import com.vaadin.shared.ui.root.PageClientRpc;
  13. import com.vaadin.terminal.WrappedRequest.BrowserDetails;
  14. import com.vaadin.terminal.gwt.client.ui.notification.VNotification;
  15. import com.vaadin.terminal.gwt.client.ui.root.VRoot;
  16. import com.vaadin.terminal.gwt.server.WebApplicationContext;
  17. import com.vaadin.terminal.gwt.server.WebBrowser;
  18. import com.vaadin.tools.ReflectTools;
  19. import com.vaadin.ui.JavaScript;
  20. import com.vaadin.ui.Notification;
  21. import com.vaadin.ui.Root;
  22. public class Page implements Serializable {
  23. /**
  24. * Listener that gets notified when the size of the browser window
  25. * containing the root has changed.
  26. *
  27. * @see Root#addListener(BrowserWindowResizeListener)
  28. */
  29. public interface BrowserWindowResizeListener extends Serializable {
  30. /**
  31. * Invoked when the browser window containing a Root has been resized.
  32. *
  33. * @param event
  34. * a browser window resize event
  35. */
  36. public void browserWindowResized(BrowserWindowResizeEvent event);
  37. }
  38. /**
  39. * Event that is fired when a browser window containing a root is resized.
  40. */
  41. public class BrowserWindowResizeEvent extends EventObject {
  42. private final int width;
  43. private final int height;
  44. /**
  45. * Creates a new event
  46. *
  47. * @param source
  48. * the root for which the browser window has been resized
  49. * @param width
  50. * the new width of the browser window
  51. * @param height
  52. * the new height of the browser window
  53. */
  54. public BrowserWindowResizeEvent(Page source, int width, int height) {
  55. super(source);
  56. this.width = width;
  57. this.height = height;
  58. }
  59. @Override
  60. public Page getSource() {
  61. return (Page) super.getSource();
  62. }
  63. /**
  64. * Gets the new browser window height
  65. *
  66. * @return an integer with the new pixel height of the browser window
  67. */
  68. public int getHeight() {
  69. return height;
  70. }
  71. /**
  72. * Gets the new browser window width
  73. *
  74. * @return an integer with the new pixel width of the browser window
  75. */
  76. public int getWidth() {
  77. return width;
  78. }
  79. }
  80. /**
  81. * Private class for storing properties related to opening resources.
  82. */
  83. private class OpenResource implements Serializable {
  84. /**
  85. * The resource to open
  86. */
  87. private final Resource resource;
  88. /**
  89. * The name of the target window
  90. */
  91. private final String name;
  92. /**
  93. * The width of the target window
  94. */
  95. private final int width;
  96. /**
  97. * The height of the target window
  98. */
  99. private final int height;
  100. /**
  101. * The border style of the target window
  102. */
  103. private final int border;
  104. /**
  105. * Creates a new open resource.
  106. *
  107. * @param resource
  108. * The resource to open
  109. * @param name
  110. * The name of the target window
  111. * @param width
  112. * The width of the target window
  113. * @param height
  114. * The height of the target window
  115. * @param border
  116. * The border style of the target window
  117. */
  118. private OpenResource(Resource resource, String name, int width,
  119. int height, int border) {
  120. this.resource = resource;
  121. this.name = name;
  122. this.width = width;
  123. this.height = height;
  124. this.border = border;
  125. }
  126. /**
  127. * Paints the open request. Should be painted inside the window.
  128. *
  129. * @param target
  130. * the paint target
  131. * @throws PaintException
  132. * if the paint operation fails
  133. */
  134. private void paintContent(PaintTarget target) throws PaintException {
  135. target.startTag("open");
  136. target.addAttribute("src", resource);
  137. if (name != null && name.length() > 0) {
  138. target.addAttribute("name", name);
  139. }
  140. if (width >= 0) {
  141. target.addAttribute("width", width);
  142. }
  143. if (height >= 0) {
  144. target.addAttribute("height", height);
  145. }
  146. switch (border) {
  147. case BORDER_MINIMAL:
  148. target.addAttribute("border", "minimal");
  149. break;
  150. case BORDER_NONE:
  151. target.addAttribute("border", "none");
  152. break;
  153. }
  154. target.endTag("open");
  155. }
  156. }
  157. private static final Method BROWSWER_RESIZE_METHOD = ReflectTools
  158. .findMethod(BrowserWindowResizeListener.class,
  159. "browserWindowResized", BrowserWindowResizeEvent.class);
  160. /**
  161. * A border style used for opening resources in a window without a border.
  162. */
  163. public static final int BORDER_NONE = 0;
  164. /**
  165. * A border style used for opening resources in a window with a minimal
  166. * border.
  167. */
  168. public static final int BORDER_MINIMAL = 1;
  169. /**
  170. * A border style that indicates that the default border style should be
  171. * used when opening resources.
  172. */
  173. public static final int BORDER_DEFAULT = 2;
  174. /**
  175. * Listener that listens changes in URI fragment.
  176. */
  177. public interface FragmentChangedListener extends Serializable {
  178. public void fragmentChanged(FragmentChangedEvent event);
  179. }
  180. private static final Method FRAGMENT_CHANGED_METHOD = ReflectTools
  181. .findMethod(Page.FragmentChangedListener.class, "fragmentChanged",
  182. FragmentChangedEvent.class);
  183. /**
  184. * Resources to be opened automatically on next repaint. The list is
  185. * automatically cleared when it has been sent to the client.
  186. */
  187. private final LinkedList<OpenResource> openList = new LinkedList<OpenResource>();
  188. /**
  189. * A list of notifications that are waiting to be sent to the client.
  190. * Cleared (set to null) when the notifications have been sent.
  191. */
  192. private List<Notification> notifications;
  193. /**
  194. * Event fired when uri fragment changes.
  195. */
  196. public class FragmentChangedEvent extends EventObject {
  197. /**
  198. * The new uri fragment
  199. */
  200. private final String fragment;
  201. /**
  202. * Creates a new instance of UriFragmentReader change event.
  203. *
  204. * @param source
  205. * the Source of the event.
  206. */
  207. public FragmentChangedEvent(Page source, String fragment) {
  208. super(source);
  209. this.fragment = fragment;
  210. }
  211. /**
  212. * Gets the root in which the fragment has changed.
  213. *
  214. * @return the root in which the fragment has changed
  215. */
  216. public Page getPage() {
  217. return (Page) getSource();
  218. }
  219. /**
  220. * Get the new fragment
  221. *
  222. * @return the new fragment
  223. */
  224. public String getFragment() {
  225. return fragment;
  226. }
  227. }
  228. private EventRouter eventRouter;
  229. /**
  230. * The current URI fragment.
  231. */
  232. private String fragment;
  233. private final Root root;
  234. private int browserWindowWidth = -1;
  235. private int browserWindowHeight = -1;
  236. private JavaScript javaScript;
  237. public Page(Root root) {
  238. this.root = root;
  239. }
  240. private void addListener(Class<?> eventType, Object target, Method method) {
  241. if (eventRouter == null) {
  242. eventRouter = new EventRouter();
  243. }
  244. eventRouter.addListener(eventType, target, method);
  245. }
  246. private void removeListener(Class<?> eventType, Object target, Method method) {
  247. if (eventRouter != null) {
  248. eventRouter.removeListener(eventType, target, method);
  249. }
  250. }
  251. public void addListener(Page.FragmentChangedListener listener) {
  252. addListener(FragmentChangedEvent.class, listener,
  253. FRAGMENT_CHANGED_METHOD);
  254. }
  255. public void removeListener(Page.FragmentChangedListener listener) {
  256. removeListener(FragmentChangedEvent.class, listener,
  257. FRAGMENT_CHANGED_METHOD);
  258. }
  259. /**
  260. * Sets URI fragment. Optionally fires a {@link FragmentChangedEvent}
  261. *
  262. * @param newFragment
  263. * id of the new fragment
  264. * @param fireEvent
  265. * true to fire event
  266. * @see FragmentChangedEvent
  267. * @see Page.FragmentChangedListener
  268. */
  269. public void setFragment(String newFragment, boolean fireEvents) {
  270. if (newFragment == null) {
  271. throw new NullPointerException("The fragment may not be null");
  272. }
  273. if (!newFragment.equals(fragment)) {
  274. fragment = newFragment;
  275. if (fireEvents) {
  276. fireEvent(new FragmentChangedEvent(this, newFragment));
  277. }
  278. root.requestRepaint();
  279. }
  280. }
  281. private void fireEvent(EventObject event) {
  282. if (eventRouter != null) {
  283. eventRouter.fireEvent(event);
  284. }
  285. }
  286. /**
  287. * Sets URI fragment. This method fires a {@link FragmentChangedEvent}
  288. *
  289. * @param newFragment
  290. * id of the new fragment
  291. * @see FragmentChangedEvent
  292. * @see Page.FragmentChangedListener
  293. */
  294. public void setFragment(String newFragment) {
  295. setFragment(newFragment, true);
  296. }
  297. /**
  298. * Gets currently set URI fragment.
  299. * <p>
  300. * To listen changes in fragment, hook a
  301. * {@link Page.FragmentChangedListener}.
  302. *
  303. * @return the current fragment in browser uri or null if not known
  304. */
  305. public String getFragment() {
  306. return fragment;
  307. }
  308. public void init(WrappedRequest request) {
  309. BrowserDetails browserDetails = request.getBrowserDetails();
  310. if (browserDetails != null) {
  311. fragment = browserDetails.getUriFragment();
  312. }
  313. }
  314. public WebBrowser getWebBrowser() {
  315. return ((WebApplicationContext) root.getApplication().getContext())
  316. .getBrowser();
  317. }
  318. public void setBrowserWindowSize(Integer width, Integer height) {
  319. boolean fireEvent = false;
  320. if (width != null) {
  321. int newWidth = width.intValue();
  322. if (newWidth != browserWindowWidth) {
  323. browserWindowWidth = newWidth;
  324. fireEvent = true;
  325. }
  326. }
  327. if (height != null) {
  328. int newHeight = height.intValue();
  329. if (newHeight != browserWindowHeight) {
  330. browserWindowHeight = newHeight;
  331. fireEvent = true;
  332. }
  333. }
  334. if (fireEvent) {
  335. fireEvent(new BrowserWindowResizeEvent(this, browserWindowWidth,
  336. browserWindowHeight));
  337. }
  338. }
  339. /**
  340. * Adds a new {@link BrowserWindowResizeListener} to this root. The listener
  341. * will be notified whenever the browser window within which this root
  342. * resides is resized.
  343. *
  344. * @param resizeListener
  345. * the listener to add
  346. *
  347. * @see BrowserWindowResizeListener#browserWindowResized(BrowserWindowResizeEvent)
  348. * @see #setResizeLazy(boolean)
  349. */
  350. public void addListener(BrowserWindowResizeListener resizeListener) {
  351. addListener(BrowserWindowResizeEvent.class, resizeListener,
  352. BROWSWER_RESIZE_METHOD);
  353. }
  354. /**
  355. * Removes a {@link BrowserWindowResizeListener} from this root. The
  356. * listener will no longer be notified when the browser window is resized.
  357. *
  358. * @param resizeListener
  359. * the listener to remove
  360. */
  361. public void removeListener(BrowserWindowResizeListener resizeListener) {
  362. removeListener(BrowserWindowResizeEvent.class, resizeListener,
  363. BROWSWER_RESIZE_METHOD);
  364. }
  365. /**
  366. * Gets the last known height of the browser window in which this root
  367. * resides.
  368. *
  369. * @return the browser window height in pixels
  370. */
  371. public int getBrowserWindowHeight() {
  372. return browserWindowHeight;
  373. }
  374. /**
  375. * Gets the last known width of the browser window in which this root
  376. * resides.
  377. *
  378. * @return the browser window width in pixels
  379. */
  380. public int getBrowserWindowWidth() {
  381. return browserWindowWidth;
  382. }
  383. public JavaScript getJavaScript() {
  384. if (javaScript == null) {
  385. // Create and attach on first use
  386. javaScript = new JavaScript();
  387. javaScript.extend(root);
  388. }
  389. return javaScript;
  390. }
  391. public void paintContent(PaintTarget target) throws PaintException {
  392. if (!openList.isEmpty()) {
  393. for (final Iterator<OpenResource> i = openList.iterator(); i
  394. .hasNext();) {
  395. (i.next()).paintContent(target);
  396. }
  397. openList.clear();
  398. }
  399. // Paint notifications
  400. if (notifications != null) {
  401. target.startTag("notifications");
  402. for (final Iterator<Notification> it = notifications.iterator(); it
  403. .hasNext();) {
  404. final Notification n = it.next();
  405. target.startTag("notification");
  406. if (n.getCaption() != null) {
  407. target.addAttribute(
  408. VNotification.ATTRIBUTE_NOTIFICATION_CAPTION,
  409. n.getCaption());
  410. }
  411. if (n.getDescription() != null) {
  412. target.addAttribute(
  413. VNotification.ATTRIBUTE_NOTIFICATION_MESSAGE,
  414. n.getDescription());
  415. }
  416. if (n.getIcon() != null) {
  417. target.addAttribute(
  418. VNotification.ATTRIBUTE_NOTIFICATION_ICON,
  419. n.getIcon());
  420. }
  421. if (!n.isHtmlContentAllowed()) {
  422. target.addAttribute(
  423. VRoot.NOTIFICATION_HTML_CONTENT_NOT_ALLOWED, true);
  424. }
  425. target.addAttribute(
  426. VNotification.ATTRIBUTE_NOTIFICATION_POSITION,
  427. n.getPosition());
  428. target.addAttribute(VNotification.ATTRIBUTE_NOTIFICATION_DELAY,
  429. n.getDelayMsec());
  430. if (n.getStyleName() != null) {
  431. target.addAttribute(
  432. VNotification.ATTRIBUTE_NOTIFICATION_STYLE,
  433. n.getStyleName());
  434. }
  435. target.endTag("notification");
  436. }
  437. target.endTag("notifications");
  438. notifications = null;
  439. }
  440. if (fragment != null) {
  441. target.addAttribute(VRoot.FRAGMENT_VARIABLE, fragment);
  442. }
  443. }
  444. /**
  445. * Opens the given resource in this root. The contents of this Root is
  446. * replaced by the {@code Resource}.
  447. *
  448. * @param resource
  449. * the resource to show in this root
  450. */
  451. public void open(Resource resource) {
  452. openList.add(new OpenResource(resource, null, -1, -1, BORDER_DEFAULT));
  453. root.requestRepaint();
  454. }
  455. /**
  456. * Opens the given resource in a window with the given name.
  457. * <p>
  458. * The supplied {@code windowName} is used as the target name in a
  459. * window.open call in the client. This means that special values such as
  460. * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
  461. * <code>null</code> window name is also a special case.
  462. * </p>
  463. * <p>
  464. * "", null and "_self" as {@code windowName} all causes the resource to be
  465. * opened in the current window, replacing any old contents. For
  466. * downloadable content you should avoid "_self" as "_self" causes the
  467. * client to skip rendering of any other changes as it considers them
  468. * irrelevant (the page will be replaced by the resource). This can speed up
  469. * the opening of a resource, but it might also put the client side into an
  470. * inconsistent state if the window content is not completely replaced e.g.,
  471. * if the resource is downloaded instead of displayed in the browser.
  472. * </p>
  473. * <p>
  474. * "_blank" as {@code windowName} causes the resource to always be opened in
  475. * a new window or tab (depends on the browser and browser settings).
  476. * </p>
  477. * <p>
  478. * "_top" and "_parent" as {@code windowName} works as specified by the HTML
  479. * standard.
  480. * </p>
  481. * <p>
  482. * Any other {@code windowName} will open the resource in a window with that
  483. * name, either by opening a new window/tab in the browser or by replacing
  484. * the contents of an existing window with that name.
  485. * </p>
  486. *
  487. * @param resource
  488. * the resource.
  489. * @param windowName
  490. * the name of the window.
  491. */
  492. public void open(Resource resource, String windowName) {
  493. openList.add(new OpenResource(resource, windowName, -1, -1,
  494. BORDER_DEFAULT));
  495. root.requestRepaint();
  496. }
  497. /**
  498. * Opens the given resource in a window with the given size, border and
  499. * name. For more information on the meaning of {@code windowName}, see
  500. * {@link #open(Resource, String)}.
  501. *
  502. * @param resource
  503. * the resource.
  504. * @param windowName
  505. * the name of the window.
  506. * @param width
  507. * the width of the window in pixels
  508. * @param height
  509. * the height of the window in pixels
  510. * @param border
  511. * the border style of the window. See {@link #BORDER_NONE
  512. * Window.BORDER_* constants}
  513. */
  514. public void open(Resource resource, String windowName, int width,
  515. int height, int border) {
  516. openList.add(new OpenResource(resource, windowName, width, height,
  517. border));
  518. root.requestRepaint();
  519. }
  520. /**
  521. * Internal helper method to actually add a notification.
  522. *
  523. * @param notification
  524. * the notification to add
  525. */
  526. private void addNotification(Notification notification) {
  527. if (notifications == null) {
  528. notifications = new LinkedList<Notification>();
  529. }
  530. notifications.add(notification);
  531. root.requestRepaint();
  532. }
  533. /**
  534. * Shows a notification message.
  535. *
  536. * @see Notification
  537. *
  538. * @param notification
  539. * The notification message to show
  540. *
  541. * @deprecated Use Notification.show(Page) instead.
  542. */
  543. @Deprecated
  544. public void showNotification(Notification notification) {
  545. addNotification(notification);
  546. }
  547. /**
  548. * Gets the Page to which the current root belongs. This is automatically
  549. * defined when processing requests to the server. In other cases, (e.g.
  550. * from background threads), the current root is not automatically defined.
  551. *
  552. * @see Root#getCurrent()
  553. *
  554. * @return the current page instance if available, otherwise
  555. * <code>null</code>
  556. */
  557. public static Page getCurrent() {
  558. Root currentRoot = Root.getCurrent();
  559. if (currentRoot == null) {
  560. return null;
  561. }
  562. return currentRoot.getPage();
  563. }
  564. /**
  565. * Sets the page title. The page title is displayed by the browser e.g. as
  566. * the title of the browser window or as the title of the tab.
  567. *
  568. * @param title
  569. * the new page title to set
  570. */
  571. public void setTitle(String title) {
  572. root.getRpcProxy(PageClientRpc.class).setTitle(title);
  573. }
  574. }