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

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