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.

Page.java 20KB

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