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.

UI.java 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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.ui;
  17. import java.lang.reflect.Method;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.EventListener;
  24. import java.util.Iterator;
  25. import java.util.LinkedHashSet;
  26. import java.util.Map;
  27. import com.vaadin.Application;
  28. import com.vaadin.annotations.EagerInit;
  29. import com.vaadin.event.Action;
  30. import com.vaadin.event.Action.Handler;
  31. import com.vaadin.event.ActionManager;
  32. import com.vaadin.event.MouseEvents.ClickEvent;
  33. import com.vaadin.event.MouseEvents.ClickListener;
  34. import com.vaadin.server.AbstractApplicationServlet;
  35. import com.vaadin.server.Page;
  36. import com.vaadin.server.PaintException;
  37. import com.vaadin.server.PaintTarget;
  38. import com.vaadin.server.Resource;
  39. import com.vaadin.server.Vaadin6Component;
  40. import com.vaadin.server.WrappedRequest;
  41. import com.vaadin.server.Page.BrowserWindowResizeEvent;
  42. import com.vaadin.server.Page.BrowserWindowResizeListener;
  43. import com.vaadin.server.WrappedRequest.BrowserDetails;
  44. import com.vaadin.shared.EventId;
  45. import com.vaadin.shared.MouseEventDetails;
  46. import com.vaadin.shared.ui.BorderStyle;
  47. import com.vaadin.shared.ui.ui.UIConstants;
  48. import com.vaadin.shared.ui.ui.UIServerRpc;
  49. import com.vaadin.shared.ui.ui.UIState;
  50. import com.vaadin.tools.ReflectTools;
  51. /**
  52. * The topmost component in any component hierarchy. There is one UI for every
  53. * Vaadin instance in a browser window. A UI may either represent an entire
  54. * browser window (or tab) or some part of a html page where a Vaadin
  55. * application is embedded.
  56. * <p>
  57. * The UI is the server side entry point for various client side features that
  58. * are not represented as components added to a layout, e.g notifications, sub
  59. * windows, and executing javascript in the browser.
  60. * </p>
  61. * <p>
  62. * When a new UI instance is needed, typically because the user opens a URL in a
  63. * browser window which points to {@link AbstractApplicationServlet},
  64. * {@link Application#getUIForRequest(WrappedRequest)} is invoked to get a UI.
  65. * That method does by default create a UI according to the
  66. * {@value Application#UI_PARAMETER} parameter from web.xml.
  67. * </p>
  68. * <p>
  69. * After a UI has been created by the application, it is initialized using
  70. * {@link #init(WrappedRequest)}. This method is intended to be overridden by
  71. * the developer to add components to the user interface and initialize
  72. * non-component functionality. The component hierarchy is initialized by
  73. * passing a {@link ComponentContainer} with the main layout of the view to
  74. * {@link #setContent(ComponentContainer)}.
  75. * </p>
  76. * <p>
  77. * If a {@link EagerInit} annotation is present on a class extending
  78. * <code>UI</code>, the framework will use a faster initialization method which
  79. * will not ensure that {@link BrowserDetails} are present in the
  80. * {@link WrappedRequest} passed to the init method.
  81. * </p>
  82. *
  83. * @see #init(WrappedRequest)
  84. * @see Application#getUI(WrappedRequest)
  85. *
  86. * @since 7.0
  87. */
  88. public abstract class UI extends AbstractComponentContainer implements
  89. Action.Container, Action.Notifier, Vaadin6Component {
  90. /**
  91. * Helper class to emulate the main window from Vaadin 6 using UIs. This
  92. * class should be used in the same way as Window used as a browser level
  93. * window in Vaadin 6 with {@link com.vaadin.Application.LegacyApplication}
  94. */
  95. @Deprecated
  96. @EagerInit
  97. public static class LegacyWindow extends UI {
  98. private String name;
  99. /**
  100. * Create a new legacy window
  101. */
  102. public LegacyWindow() {
  103. super();
  104. }
  105. /**
  106. * Creates a new legacy window with the given caption
  107. *
  108. * @param caption
  109. * the caption of the window
  110. */
  111. public LegacyWindow(String caption) {
  112. super(caption);
  113. }
  114. /**
  115. * Creates a legacy window with the given caption and content layout
  116. *
  117. * @param caption
  118. * @param content
  119. */
  120. public LegacyWindow(String caption, ComponentContainer content) {
  121. super(caption, content);
  122. }
  123. @Override
  124. protected void init(WrappedRequest request) {
  125. // Just empty
  126. }
  127. /**
  128. * Gets the unique name of the window. The name of the window is used to
  129. * uniquely identify it.
  130. * <p>
  131. * The name also determines the URL that can be used for direct access
  132. * to a window. All windows can be accessed through
  133. * {@code http://host:port/app/win} where {@code http://host:port/app}
  134. * is the application URL (as returned by {@link Application#getURL()}
  135. * and {@code win} is the window name.
  136. * </p>
  137. * <p>
  138. * Note! Portlets do not support direct window access through URLs.
  139. * </p>
  140. *
  141. * @return the Name of the Window.
  142. */
  143. public String getName() {
  144. return name;
  145. }
  146. /**
  147. * Sets the unique name of the window. The name of the window is used to
  148. * uniquely identify it inside the application.
  149. * <p>
  150. * The name also determines the URL that can be used for direct access
  151. * to a window. All windows can be accessed through
  152. * {@code http://host:port/app/win} where {@code http://host:port/app}
  153. * is the application URL (as returned by {@link Application#getURL()}
  154. * and {@code win} is the window name.
  155. * </p>
  156. * <p>
  157. * This method can only be called before the window is added to an
  158. * application.
  159. * <p>
  160. * Note! Portlets do not support direct window access through URLs.
  161. * </p>
  162. *
  163. * @param name
  164. * the new name for the window or null if the application
  165. * should automatically assign a name to it
  166. * @throws IllegalStateException
  167. * if the window is attached to an application
  168. */
  169. public void setName(String name) {
  170. this.name = name;
  171. // The name can not be changed in application
  172. if (getApplication() != null) {
  173. throw new IllegalStateException(
  174. "Window name can not be changed while "
  175. + "the window is in application");
  176. }
  177. }
  178. /**
  179. * Gets the full URL of the window. The returned URL is window specific
  180. * and can be used to directly refer to the window.
  181. * <p>
  182. * Note! This method can not be used for portlets.
  183. * </p>
  184. *
  185. * @return the URL of the window or null if the window is not attached
  186. * to an application
  187. */
  188. public URL getURL() {
  189. Application application = getApplication();
  190. if (application == null) {
  191. return null;
  192. }
  193. try {
  194. return new URL(application.getURL(), getName() + "/");
  195. } catch (MalformedURLException e) {
  196. throw new RuntimeException(
  197. "Internal problem getting window URL, please report");
  198. }
  199. }
  200. /**
  201. * Opens the given resource in this UI. The contents of this UI is
  202. * replaced by the {@code Resource}.
  203. *
  204. * @param resource
  205. * the resource to show in this UI
  206. *
  207. * @deprecated As of 7.0, use getPage().open instead
  208. */
  209. @Deprecated
  210. public void open(Resource resource) {
  211. getPage().open(resource);
  212. }
  213. /* ********************************************************************* */
  214. /**
  215. * Opens the given resource in a window with the given name.
  216. * <p>
  217. * The supplied {@code windowName} is used as the target name in a
  218. * window.open call in the client. This means that special values such
  219. * as "_blank", "_self", "_top", "_parent" have special meaning. An
  220. * empty or <code>null</code> window name is also a special case.
  221. * </p>
  222. * <p>
  223. * "", null and "_self" as {@code windowName} all causes the resource to
  224. * be opened in the current window, replacing any old contents. For
  225. * downloadable content you should avoid "_self" as "_self" causes the
  226. * client to skip rendering of any other changes as it considers them
  227. * irrelevant (the page will be replaced by the resource). This can
  228. * speed up the opening of a resource, but it might also put the client
  229. * side into an inconsistent state if the window content is not
  230. * completely replaced e.g., if the resource is downloaded instead of
  231. * displayed in the browser.
  232. * </p>
  233. * <p>
  234. * "_blank" as {@code windowName} causes the resource to always be
  235. * opened in a new window or tab (depends on the browser and browser
  236. * settings).
  237. * </p>
  238. * <p>
  239. * "_top" and "_parent" as {@code windowName} works as specified by the
  240. * HTML standard.
  241. * </p>
  242. * <p>
  243. * Any other {@code windowName} will open the resource in a window with
  244. * that name, either by opening a new window/tab in the browser or by
  245. * replacing the contents of an existing window with that name.
  246. * </p>
  247. *
  248. * @param resource
  249. * the resource.
  250. * @param windowName
  251. * the name of the window.
  252. * @deprecated As of 7.0, use getPage().open instead
  253. */
  254. @Deprecated
  255. public void open(Resource resource, String windowName) {
  256. getPage().open(resource, windowName);
  257. }
  258. /**
  259. * Opens the given resource in a window with the given size, border and
  260. * name. For more information on the meaning of {@code windowName}, see
  261. * {@link #open(Resource, String)}.
  262. *
  263. * @param resource
  264. * the resource.
  265. * @param windowName
  266. * the name of the window.
  267. * @param width
  268. * the width of the window in pixels
  269. * @param height
  270. * the height of the window in pixels
  271. * @param border
  272. * the border style of the window.
  273. * @deprecated As of 7.0, use getPage().open instead
  274. */
  275. @Deprecated
  276. public void open(Resource resource, String windowName, int width,
  277. int height, BorderStyle border) {
  278. getPage().open(resource, windowName, width, height, border);
  279. }
  280. /**
  281. * Adds a new {@link BrowserWindowResizeListener} to this UI. The
  282. * listener will be notified whenever the browser window within which
  283. * this UI resides is resized.
  284. *
  285. * @param resizeListener
  286. * the listener to add
  287. *
  288. * @see BrowserWindowResizeListener#browserWindowResized(BrowserWindowResizeEvent)
  289. * @see #setResizeLazy(boolean)
  290. *
  291. * @deprecated As of 7.0, use the similarly named api in Page instead
  292. */
  293. @Deprecated
  294. public void addListener(BrowserWindowResizeListener resizeListener) {
  295. getPage().addListener(resizeListener);
  296. }
  297. /**
  298. * Removes a {@link BrowserWindowResizeListener} from this UI. The
  299. * listener will no longer be notified when the browser window is
  300. * resized.
  301. *
  302. * @param resizeListener
  303. * the listener to remove
  304. * @deprecated As of 7.0, use the similarly named api in Page instead
  305. */
  306. @Deprecated
  307. public void removeListener(BrowserWindowResizeListener resizeListener) {
  308. getPage().removeListener(resizeListener);
  309. }
  310. /**
  311. * Gets the last known height of the browser window in which this UI
  312. * resides.
  313. *
  314. * @return the browser window height in pixels
  315. * @deprecated As of 7.0, use the similarly named api in Page instead
  316. */
  317. @Deprecated
  318. public int getBrowserWindowHeight() {
  319. return getPage().getBrowserWindowHeight();
  320. }
  321. /**
  322. * Gets the last known width of the browser window in which this UI
  323. * resides.
  324. *
  325. * @return the browser window width in pixels
  326. *
  327. * @deprecated As of 7.0, use the similarly named api in Page instead
  328. */
  329. @Deprecated
  330. public int getBrowserWindowWidth() {
  331. return getPage().getBrowserWindowWidth();
  332. }
  333. /**
  334. * Executes JavaScript in this window.
  335. *
  336. * <p>
  337. * This method allows one to inject javascript from the server to
  338. * client. A client implementation is not required to implement this
  339. * functionality, but currently all web-based clients do implement this.
  340. * </p>
  341. *
  342. * <p>
  343. * Executing javascript this way often leads to cross-browser
  344. * compatibility issues and regressions that are hard to resolve. Use of
  345. * this method should be avoided and instead it is recommended to create
  346. * new widgets with GWT. For more info on creating own, reusable
  347. * client-side widgets in Java, read the corresponding chapter in Book
  348. * of Vaadin.
  349. * </p>
  350. *
  351. * @param script
  352. * JavaScript snippet that will be executed.
  353. *
  354. * @deprecated as of 7.0, use JavaScript.getCurrent().execute(String)
  355. * instead
  356. */
  357. @Deprecated
  358. public void executeJavaScript(String script) {
  359. getPage().getJavaScript().execute(script);
  360. }
  361. @Override
  362. public void setCaption(String caption) {
  363. // Override to provide backwards compatibility
  364. getState().setCaption(caption);
  365. getPage().setTitle(caption);
  366. }
  367. }
  368. /**
  369. * Event fired when a UI is removed from the application.
  370. */
  371. public static class CloseEvent extends Event {
  372. private static final String CLOSE_EVENT_IDENTIFIER = "uiClose";
  373. public CloseEvent(UI source) {
  374. super(source);
  375. }
  376. public UI getUI() {
  377. return (UI) getSource();
  378. }
  379. }
  380. /**
  381. * Interface for listening {@link UI.CloseEvent UI close events}.
  382. *
  383. */
  384. public interface CloseListener extends EventListener {
  385. public static final Method closeMethod = ReflectTools.findMethod(
  386. CloseListener.class, "click", CloseEvent.class);
  387. /**
  388. * Called when a CloseListener is notified of a CloseEvent.
  389. * {@link UI#getCurrent()} returns <code>event.getUI()</code> within
  390. * this method.
  391. *
  392. * @param event
  393. * The close event that was fired.
  394. */
  395. public void close(CloseEvent event);
  396. }
  397. /**
  398. * The application to which this UI belongs
  399. */
  400. private Application application;
  401. /**
  402. * List of windows in this UI.
  403. */
  404. private final LinkedHashSet<Window> windows = new LinkedHashSet<Window>();
  405. /**
  406. * The component that should be scrolled into view after the next repaint.
  407. * Null if nothing should be scrolled into view.
  408. */
  409. private Component scrollIntoView;
  410. /**
  411. * The id of this UI, used to find the server side instance of the UI form
  412. * which a request originates. A negative value indicates that the UI id has
  413. * not yet been assigned by the Application.
  414. *
  415. * @see Application#nextUIId
  416. */
  417. private int uiId = -1;
  418. /**
  419. * Keeps track of the Actions added to this component, and manages the
  420. * painting and handling as well.
  421. */
  422. protected ActionManager actionManager;
  423. /**
  424. * Thread local for keeping track of the current UI.
  425. */
  426. private static final ThreadLocal<UI> currentUI = new ThreadLocal<UI>();
  427. /** Identifies the click event */
  428. private ConnectorTracker connectorTracker = new ConnectorTracker(this);
  429. private Page page = new Page(this);
  430. private UIServerRpc rpc = new UIServerRpc() {
  431. @Override
  432. public void click(MouseEventDetails mouseDetails) {
  433. fireEvent(new ClickEvent(UI.this, mouseDetails));
  434. }
  435. @Override
  436. public void resize(int viewWidth, int viewHeight, int windowWidth,
  437. int windowHeight) {
  438. // TODO We're not doing anything with the view dimensions
  439. getPage().setBrowserWindowSize(windowWidth, windowHeight);
  440. }
  441. };
  442. /**
  443. * Timestamp keeping track of the last heartbeat of this UI. Updated to the
  444. * current time whenever the application receives a heartbeat or UIDL
  445. * request from the client for this UI.
  446. */
  447. private long lastHeartbeat = System.currentTimeMillis();
  448. private long lastUidlRequest = System.currentTimeMillis();
  449. /**
  450. * Creates a new empty UI without a caption. This UI will have a
  451. * {@link VerticalLayout} with margins enabled as its content.
  452. */
  453. public UI() {
  454. this((ComponentContainer) null);
  455. }
  456. /**
  457. * Creates a new UI with the given component container as its content.
  458. *
  459. * @param content
  460. * the content container to use as this UIs content.
  461. *
  462. * @see #setContent(ComponentContainer)
  463. */
  464. public UI(ComponentContainer content) {
  465. registerRpc(rpc);
  466. setSizeFull();
  467. setContent(content);
  468. }
  469. /**
  470. * Creates a new empty UI with the given caption. This UI will have a
  471. * {@link VerticalLayout} with margins enabled as its content.
  472. *
  473. * @param caption
  474. * the caption of the UI, used as the page title if there's
  475. * nothing but the application on the web page
  476. *
  477. * @see #setCaption(String)
  478. */
  479. public UI(String caption) {
  480. this((ComponentContainer) null);
  481. setCaption(caption);
  482. }
  483. /**
  484. * Creates a new UI with the given caption and content.
  485. *
  486. * @param caption
  487. * the caption of the UI, used as the page title if there's
  488. * nothing but the application on the web page
  489. * @param content
  490. * the content container to use as this UIs content.
  491. *
  492. * @see #setContent(ComponentContainer)
  493. * @see #setCaption(String)
  494. */
  495. public UI(String caption, ComponentContainer content) {
  496. this(content);
  497. setCaption(caption);
  498. }
  499. @Override
  500. protected UIState getState() {
  501. return (UIState) super.getState();
  502. }
  503. @Override
  504. public Class<? extends UIState> getStateType() {
  505. // This is a workaround for a problem with creating the correct state
  506. // object during build
  507. return UIState.class;
  508. }
  509. /**
  510. * Overridden to return a value instead of referring to the parent.
  511. *
  512. * @return this UI
  513. *
  514. * @see com.vaadin.ui.AbstractComponent#getUI()
  515. */
  516. @Override
  517. public UI getUI() {
  518. return this;
  519. }
  520. @Override
  521. public void replaceComponent(Component oldComponent, Component newComponent) {
  522. throw new UnsupportedOperationException();
  523. }
  524. @Override
  525. public Application getApplication() {
  526. return application;
  527. }
  528. @Override
  529. public void paintContent(PaintTarget target) throws PaintException {
  530. page.paintContent(target);
  531. if (scrollIntoView != null) {
  532. target.addAttribute("scrollTo", scrollIntoView);
  533. scrollIntoView = null;
  534. }
  535. if (pendingFocus != null) {
  536. // ensure focused component is still attached to this main window
  537. if (pendingFocus.getUI() == this
  538. || (pendingFocus.getUI() != null && pendingFocus.getUI()
  539. .getParent() == this)) {
  540. target.addAttribute("focused", pendingFocus);
  541. }
  542. pendingFocus = null;
  543. }
  544. if (actionManager != null) {
  545. actionManager.paintActions(null, target);
  546. }
  547. if (isResizeLazy()) {
  548. target.addAttribute(UIConstants.RESIZE_LAZY, true);
  549. }
  550. }
  551. /**
  552. * Fire a click event to all click listeners.
  553. *
  554. * @param object
  555. * The raw "value" of the variable change from the client side.
  556. */
  557. private void fireClick(Map<String, Object> parameters) {
  558. MouseEventDetails mouseDetails = MouseEventDetails
  559. .deSerialize((String) parameters.get("mouseDetails"));
  560. fireEvent(new ClickEvent(this, mouseDetails));
  561. }
  562. /**
  563. * For internal use only.
  564. */
  565. public void fireCloseEvent() {
  566. UI current = UI.getCurrent();
  567. UI.setCurrent(this);
  568. fireEvent(new CloseEvent(this));
  569. UI.setCurrent(current);
  570. }
  571. @Override
  572. @SuppressWarnings("unchecked")
  573. public void changeVariables(Object source, Map<String, Object> variables) {
  574. if (variables.containsKey(EventId.CLICK_EVENT_IDENTIFIER)) {
  575. fireClick((Map<String, Object>) variables
  576. .get(EventId.CLICK_EVENT_IDENTIFIER));
  577. }
  578. // Actions
  579. if (actionManager != null) {
  580. actionManager.handleActions(variables, this);
  581. }
  582. if (variables.containsKey(UIConstants.FRAGMENT_VARIABLE)) {
  583. String fragment = (String) variables
  584. .get(UIConstants.FRAGMENT_VARIABLE);
  585. getPage().setFragment(fragment, true);
  586. }
  587. }
  588. /*
  589. * (non-Javadoc)
  590. *
  591. * @see com.vaadin.ui.ComponentContainer#getComponentIterator()
  592. */
  593. @Override
  594. public Iterator<Component> getComponentIterator() {
  595. // TODO could directly create some kind of combined iterator instead of
  596. // creating a new ArrayList
  597. ArrayList<Component> components = new ArrayList<Component>();
  598. if (getContent() != null) {
  599. components.add(getContent());
  600. }
  601. components.addAll(windows);
  602. return components.iterator();
  603. }
  604. /*
  605. * (non-Javadoc)
  606. *
  607. * @see com.vaadin.ui.ComponentContainer#getComponentCount()
  608. */
  609. @Override
  610. public int getComponentCount() {
  611. return windows.size() + (getContent() == null ? 0 : 1);
  612. }
  613. /**
  614. * Sets the application to which this UI is assigned. It is not legal to
  615. * change the application once it has been set nor to set a
  616. * <code>null</code> application.
  617. * <p>
  618. * This method is mainly intended for internal use by the framework.
  619. * </p>
  620. *
  621. * @param application
  622. * the application to set
  623. *
  624. * @throws IllegalStateException
  625. * if the application has already been set
  626. *
  627. * @see #getApplication()
  628. */
  629. public void setApplication(Application application) {
  630. if ((application == null) == (this.application == null)) {
  631. throw new IllegalStateException("Application has already been set");
  632. } else {
  633. this.application = application;
  634. }
  635. if (application != null) {
  636. attach();
  637. } else {
  638. detach();
  639. }
  640. }
  641. /**
  642. * Sets the id of this UI within its application. The UI id is used to route
  643. * requests to the right UI.
  644. * <p>
  645. * This method is mainly intended for internal use by the framework.
  646. * </p>
  647. *
  648. * @param uiId
  649. * the id of this UI
  650. *
  651. * @throws IllegalStateException
  652. * if the UI id has already been set
  653. *
  654. * @see #getUIId()
  655. */
  656. public void setUIId(int uiId) {
  657. if (this.uiId != -1) {
  658. throw new IllegalStateException("UI id has already been defined");
  659. }
  660. this.uiId = uiId;
  661. }
  662. /**
  663. * Gets the id of the UI, used to identify this UI within its application
  664. * when processing requests. The UI id should be present in every request to
  665. * the server that originates from this UI.
  666. * {@link Application#getUIForRequest(WrappedRequest)} uses this id to find
  667. * the route to which the request belongs.
  668. *
  669. * @return
  670. */
  671. public int getUIId() {
  672. return uiId;
  673. }
  674. /**
  675. * Adds a window as a subwindow inside this UI. To open a new browser window
  676. * or tab, you should instead use {@link open(Resource)} with an url
  677. * pointing to this application and ensure
  678. * {@link Application#getUI(WrappedRequest)} returns an appropriate UI for
  679. * the request.
  680. *
  681. * @param window
  682. * @throws IllegalArgumentException
  683. * if the window is already added to an application
  684. * @throws NullPointerException
  685. * if the given <code>Window</code> is <code>null</code>.
  686. */
  687. public void addWindow(Window window) throws IllegalArgumentException,
  688. NullPointerException {
  689. if (window == null) {
  690. throw new NullPointerException("Argument must not be null");
  691. }
  692. if (window.getApplication() != null) {
  693. throw new IllegalArgumentException(
  694. "Window is already attached to an application.");
  695. }
  696. attachWindow(window);
  697. }
  698. /**
  699. * Helper method to attach a window.
  700. *
  701. * @param w
  702. * the window to add
  703. */
  704. private void attachWindow(Window w) {
  705. windows.add(w);
  706. w.setParent(this);
  707. markAsDirty();
  708. }
  709. /**
  710. * Remove the given subwindow from this UI.
  711. *
  712. * Since Vaadin 6.5, {@link CloseListener}s are called also when explicitly
  713. * removing a window by calling this method.
  714. *
  715. * Since Vaadin 6.5, returns a boolean indicating if the window was removed
  716. * or not.
  717. *
  718. * @param window
  719. * Window to be removed.
  720. * @return true if the subwindow was removed, false otherwise
  721. */
  722. public boolean removeWindow(Window window) {
  723. if (!windows.remove(window)) {
  724. // Window window is not a subwindow of this UI.
  725. return false;
  726. }
  727. window.setParent(null);
  728. window.fireClose();
  729. markAsDirty();
  730. return true;
  731. }
  732. /**
  733. * Gets all the windows added to this UI.
  734. *
  735. * @return an unmodifiable collection of windows
  736. */
  737. public Collection<Window> getWindows() {
  738. return Collections.unmodifiableCollection(windows);
  739. }
  740. @Override
  741. public void focus() {
  742. super.focus();
  743. }
  744. /**
  745. * Component that should be focused after the next repaint. Null if no focus
  746. * change should take place.
  747. */
  748. private Focusable pendingFocus;
  749. private boolean resizeLazy = false;
  750. /**
  751. * This method is used by Component.Focusable objects to request focus to
  752. * themselves. Focus renders must be handled at window level (instead of
  753. * Component.Focusable) due we want the last focused component to be focused
  754. * in client too. Not the one that is rendered last (the case we'd get if
  755. * implemented in Focusable only).
  756. *
  757. * To focus component from Vaadin application, use Focusable.focus(). See
  758. * {@link Focusable}.
  759. *
  760. * @param focusable
  761. * to be focused on next paint
  762. */
  763. public void setFocusedComponent(Focusable focusable) {
  764. pendingFocus = focusable;
  765. markAsDirty();
  766. }
  767. /**
  768. * Scrolls any component between the component and UI to a suitable position
  769. * so the component is visible to the user. The given component must belong
  770. * to this UI.
  771. *
  772. * @param component
  773. * the component to be scrolled into view
  774. * @throws IllegalArgumentException
  775. * if {@code component} does not belong to this UI
  776. */
  777. public void scrollIntoView(Component component)
  778. throws IllegalArgumentException {
  779. if (component.getUI() != this) {
  780. throw new IllegalArgumentException(
  781. "The component where to scroll must belong to this UI.");
  782. }
  783. scrollIntoView = component;
  784. markAsDirty();
  785. }
  786. /**
  787. * Gets the content of this UI. The content is a component container that
  788. * serves as the outermost item of the visual contents of this UI.
  789. *
  790. * @return a component container to use as content
  791. *
  792. * @see #setContent(ComponentContainer)
  793. * @see #createDefaultLayout()
  794. */
  795. public ComponentContainer getContent() {
  796. return (ComponentContainer) getState().getContent();
  797. }
  798. /**
  799. * Helper method to create the default content layout that is used if no
  800. * content has not been explicitly defined.
  801. *
  802. * @return a newly created layout
  803. */
  804. private static VerticalLayout createDefaultLayout() {
  805. VerticalLayout layout = new VerticalLayout();
  806. layout.setMargin(true);
  807. return layout;
  808. }
  809. /**
  810. * Sets the content of this UI. The content is a component container that
  811. * serves as the outermost item of the visual contents of this UI. If no
  812. * content has been set, a {@link VerticalLayout} with margins enabled will
  813. * be used by default - see {@link #createDefaultLayout()}. The content can
  814. * also be set in a constructor.
  815. *
  816. * @return a component container to use as content
  817. *
  818. * @see #UI(ComponentContainer)
  819. * @see #createDefaultLayout()
  820. */
  821. public void setContent(ComponentContainer content) {
  822. if (content == null) {
  823. content = createDefaultLayout();
  824. }
  825. if (getState().getContent() != null) {
  826. super.removeComponent((Component) getState().getContent());
  827. }
  828. getState().setContent(content);
  829. if (content != null) {
  830. super.addComponent(content);
  831. }
  832. }
  833. /**
  834. * Adds a component to this UI. The component is not added directly to the
  835. * UI, but instead to the content container ({@link #getContent()}).
  836. *
  837. * @param component
  838. * the component to add to this UI
  839. *
  840. * @see #getContent()
  841. */
  842. @Override
  843. public void addComponent(Component component) {
  844. getContent().addComponent(component);
  845. }
  846. /**
  847. * This implementation removes the component from the content container (
  848. * {@link #getContent()}) instead of from the actual UI.
  849. */
  850. @Override
  851. public void removeComponent(Component component) {
  852. getContent().removeComponent(component);
  853. }
  854. /**
  855. * This implementation removes the components from the content container (
  856. * {@link #getContent()}) instead of from the actual UI.
  857. */
  858. @Override
  859. public void removeAllComponents() {
  860. getContent().removeAllComponents();
  861. }
  862. /**
  863. * Internal initialization method, should not be overridden. This method is
  864. * not declared as final because that would break compatibility with e.g.
  865. * CDI.
  866. *
  867. * @param request
  868. * the initialization request
  869. */
  870. public void doInit(WrappedRequest request) {
  871. getPage().init(request);
  872. // Call the init overridden by the application developer
  873. init(request);
  874. }
  875. /**
  876. * Initializes this UI. This method is intended to be overridden by
  877. * subclasses to build the view and configure non-component functionality.
  878. * Performing the initialization in a constructor is not suggested as the
  879. * state of the UI is not properly set up when the constructor is invoked.
  880. * <p>
  881. * The {@link WrappedRequest} can be used to get information about the
  882. * request that caused this UI to be created. By default, the
  883. * {@link BrowserDetails} will be available in the request. If the browser
  884. * details are not required, loading the application in the browser can take
  885. * some shortcuts giving a faster initial rendering. This can be indicated
  886. * by adding the {@link EagerInit} annotation to the UI class.
  887. * </p>
  888. *
  889. * @param request
  890. * the wrapped request that caused this UI to be created
  891. */
  892. protected abstract void init(WrappedRequest request);
  893. /**
  894. * Sets the thread local for the current UI. This method is used by the
  895. * framework to set the current application whenever a new request is
  896. * processed and it is cleared when the request has been processed.
  897. * <p>
  898. * The application developer can also use this method to define the current
  899. * UI outside the normal request handling, e.g. when initiating custom
  900. * background threads.
  901. * </p>
  902. *
  903. * @param uI
  904. * the UI to register as the current UI
  905. *
  906. * @see #getCurrent()
  907. * @see ThreadLocal
  908. */
  909. public static void setCurrent(UI ui) {
  910. currentUI.set(ui);
  911. }
  912. /**
  913. * Gets the currently used UI. The current UI is automatically defined when
  914. * processing requests to the server. In other cases, (e.g. from background
  915. * threads), the current UI is not automatically defined.
  916. *
  917. * @return the current UI instance if available, otherwise <code>null</code>
  918. *
  919. * @see #setCurrent(UI)
  920. */
  921. public static UI getCurrent() {
  922. return currentUI.get();
  923. }
  924. public void setScrollTop(int scrollTop) {
  925. throw new RuntimeException("Not yet implemented");
  926. }
  927. @Override
  928. protected ActionManager getActionManager() {
  929. if (actionManager == null) {
  930. actionManager = new ActionManager(this);
  931. }
  932. return actionManager;
  933. }
  934. @Override
  935. public <T extends Action & com.vaadin.event.Action.Listener> void addAction(
  936. T action) {
  937. getActionManager().addAction(action);
  938. }
  939. @Override
  940. public <T extends Action & com.vaadin.event.Action.Listener> void removeAction(
  941. T action) {
  942. if (actionManager != null) {
  943. actionManager.removeAction(action);
  944. }
  945. }
  946. @Override
  947. public void addActionHandler(Handler actionHandler) {
  948. getActionManager().addActionHandler(actionHandler);
  949. }
  950. @Override
  951. public void removeActionHandler(Handler actionHandler) {
  952. if (actionManager != null) {
  953. actionManager.removeActionHandler(actionHandler);
  954. }
  955. }
  956. /**
  957. * Should resize operations be lazy, i.e. should there be a delay before
  958. * layout sizes are recalculated. Speeds up resize operations in slow UIs
  959. * with the penalty of slightly decreased usability.
  960. * <p>
  961. * Default value: <code>false</code>
  962. *
  963. * @param resizeLazy
  964. * true to use a delay before recalculating sizes, false to
  965. * calculate immediately.
  966. */
  967. public void setResizeLazy(boolean resizeLazy) {
  968. this.resizeLazy = resizeLazy;
  969. markAsDirty();
  970. }
  971. /**
  972. * Checks whether lazy resize is enabled.
  973. *
  974. * @return <code>true</code> if lazy resize is enabled, <code>false</code>
  975. * if lazy resize is not enabled
  976. */
  977. public boolean isResizeLazy() {
  978. return resizeLazy;
  979. }
  980. /**
  981. * Add a click listener to the UI. The listener is called whenever the user
  982. * clicks inside the UI. Also when the click targets a component inside the
  983. * UI, provided the targeted component does not prevent the click event from
  984. * propagating.
  985. *
  986. * Use {@link #removeListener(ClickListener)} to remove the listener.
  987. *
  988. * @param listener
  989. * The listener to add
  990. */
  991. public void addListener(ClickListener listener) {
  992. addListener(EventId.CLICK_EVENT_IDENTIFIER, ClickEvent.class, listener,
  993. ClickListener.clickMethod);
  994. }
  995. /**
  996. * Remove a click listener from the UI. The listener should earlier have
  997. * been added using {@link #addListener(ClickListener)}.
  998. *
  999. * @param listener
  1000. * The listener to remove
  1001. */
  1002. public void removeListener(ClickListener listener) {
  1003. removeListener(EventId.CLICK_EVENT_IDENTIFIER, ClickEvent.class,
  1004. listener);
  1005. }
  1006. /**
  1007. * Adds a close listener to the UI. The listener is called when the UI is
  1008. * removed from the application.
  1009. *
  1010. * @param listener
  1011. * The listener to add.
  1012. */
  1013. public void addListener(CloseListener listener) {
  1014. addListener(CloseEvent.CLOSE_EVENT_IDENTIFIER, CloseEvent.class,
  1015. listener, CloseListener.closeMethod);
  1016. }
  1017. /**
  1018. * Removes a close listener from the UI if it has previously been added with
  1019. * {@link #addListener(ClickListener)}. Otherwise, has no effect.
  1020. *
  1021. * @param listener
  1022. * The listener to remove.
  1023. */
  1024. public void removeListener(CloseListener listener) {
  1025. removeListener(CloseEvent.CLOSE_EVENT_IDENTIFIER, CloseEvent.class,
  1026. listener);
  1027. }
  1028. @Override
  1029. public boolean isConnectorEnabled() {
  1030. // TODO How can a UI be invisible? What does it mean?
  1031. return isVisible() && isEnabled();
  1032. }
  1033. public ConnectorTracker getConnectorTracker() {
  1034. return connectorTracker;
  1035. }
  1036. public Page getPage() {
  1037. return page;
  1038. }
  1039. /**
  1040. * Setting the caption of a UI is not supported. To set the title of the
  1041. * HTML page, use Page.setTitle
  1042. *
  1043. * @deprecated as of 7.0.0, use {@link Page#setTitle(String)}
  1044. */
  1045. @Override
  1046. @Deprecated
  1047. public void setCaption(String caption) {
  1048. throw new IllegalStateException(
  1049. "You can not set the title of a UI. To set the title of the HTML page, use Page.setTitle");
  1050. }
  1051. /**
  1052. * Shows a notification message on the middle of the UI. The message
  1053. * automatically disappears ("humanized message").
  1054. *
  1055. * Care should be taken to to avoid XSS vulnerabilities as the caption is
  1056. * rendered as html.
  1057. *
  1058. * @see #showNotification(Notification)
  1059. * @see Notification
  1060. *
  1061. * @param caption
  1062. * The message
  1063. *
  1064. * @deprecated As of 7.0, use Notification.show instead but be aware that
  1065. * Notification.show does not allow HTML.
  1066. */
  1067. @Deprecated
  1068. public void showNotification(String caption) {
  1069. Notification notification = new Notification(caption);
  1070. notification.setHtmlContentAllowed(true);// Backwards compatibility
  1071. getPage().showNotification(notification);
  1072. }
  1073. /**
  1074. * Shows a notification message the UI. The position and behavior of the
  1075. * message depends on the type, which is one of the basic types defined in
  1076. * {@link Notification}, for instance Notification.TYPE_WARNING_MESSAGE.
  1077. *
  1078. * Care should be taken to to avoid XSS vulnerabilities as the caption is
  1079. * rendered as html.
  1080. *
  1081. * @see #showNotification(Notification)
  1082. * @see Notification
  1083. *
  1084. * @param caption
  1085. * The message
  1086. * @param type
  1087. * The message type
  1088. *
  1089. * @deprecated As of 7.0, use Notification.show instead but be aware that
  1090. * Notification.show does not allow HTML.
  1091. */
  1092. @Deprecated
  1093. public void showNotification(String caption, Notification.Type type) {
  1094. Notification notification = new Notification(caption, type);
  1095. notification.setHtmlContentAllowed(true);// Backwards compatibility
  1096. getPage().showNotification(notification);
  1097. }
  1098. /**
  1099. * Shows a notification consisting of a bigger caption and a smaller
  1100. * description on the middle of the UI. The message automatically disappears
  1101. * ("humanized message").
  1102. *
  1103. * Care should be taken to to avoid XSS vulnerabilities as the caption and
  1104. * description are rendered as html.
  1105. *
  1106. * @see #showNotification(Notification)
  1107. * @see Notification
  1108. *
  1109. * @param caption
  1110. * The caption of the message
  1111. * @param description
  1112. * The message description
  1113. *
  1114. * @deprecated As of 7.0, use new Notification(...).show(Page) instead but
  1115. * be aware that HTML by default not allowed.
  1116. */
  1117. @Deprecated
  1118. public void showNotification(String caption, String description) {
  1119. Notification notification = new Notification(caption, description);
  1120. notification.setHtmlContentAllowed(true);// Backwards compatibility
  1121. getPage().showNotification(notification);
  1122. }
  1123. /**
  1124. * Shows a notification consisting of a bigger caption and a smaller
  1125. * description. The position and behavior of the message depends on the
  1126. * type, which is one of the basic types defined in {@link Notification} ,
  1127. * for instance Notification.TYPE_WARNING_MESSAGE.
  1128. *
  1129. * Care should be taken to to avoid XSS vulnerabilities as the caption and
  1130. * description are rendered as html.
  1131. *
  1132. * @see #showNotification(Notification)
  1133. * @see Notification
  1134. *
  1135. * @param caption
  1136. * The caption of the message
  1137. * @param description
  1138. * The message description
  1139. * @param type
  1140. * The message type
  1141. *
  1142. * @deprecated As of 7.0, use new Notification(...).show(Page) instead but
  1143. * be aware that HTML by default not allowed.
  1144. */
  1145. @Deprecated
  1146. public void showNotification(String caption, String description,
  1147. Notification.Type type) {
  1148. Notification notification = new Notification(caption, description, type);
  1149. notification.setHtmlContentAllowed(true);// Backwards compatibility
  1150. getPage().showNotification(notification);
  1151. }
  1152. /**
  1153. * Shows a notification consisting of a bigger caption and a smaller
  1154. * description. The position and behavior of the message depends on the
  1155. * type, which is one of the basic types defined in {@link Notification} ,
  1156. * for instance Notification.TYPE_WARNING_MESSAGE.
  1157. *
  1158. * Care should be taken to avoid XSS vulnerabilities if html content is
  1159. * allowed.
  1160. *
  1161. * @see #showNotification(Notification)
  1162. * @see Notification
  1163. *
  1164. * @param caption
  1165. * The message caption
  1166. * @param description
  1167. * The message description
  1168. * @param type
  1169. * The type of message
  1170. * @param htmlContentAllowed
  1171. * Whether html in the caption and description should be
  1172. * displayed as html or as plain text
  1173. *
  1174. * @deprecated As of 7.0, use new Notification(...).show(Page).
  1175. */
  1176. @Deprecated
  1177. public void showNotification(String caption, String description,
  1178. Notification.Type type, boolean htmlContentAllowed) {
  1179. getPage()
  1180. .showNotification(
  1181. new Notification(caption, description, type,
  1182. htmlContentAllowed));
  1183. }
  1184. /**
  1185. * Shows a notification message.
  1186. *
  1187. * @see Notification
  1188. * @see #showNotification(String)
  1189. * @see #showNotification(String, int)
  1190. * @see #showNotification(String, String)
  1191. * @see #showNotification(String, String, int)
  1192. *
  1193. * @param notification
  1194. * The notification message to show
  1195. *
  1196. * @deprecated As of 7.0, use Notification.show instead
  1197. */
  1198. @Deprecated
  1199. public void showNotification(Notification notification) {
  1200. getPage().showNotification(notification);
  1201. }
  1202. /**
  1203. * Returns the timestamp (milliseconds since the epoch) of the last received
  1204. * heartbeat for this UI.
  1205. *
  1206. * @see #heartbeat()
  1207. * @see Application#closeInactiveUIs()
  1208. *
  1209. * @return The time the last heartbeat request occurred.
  1210. */
  1211. public long getLastHeartbeatTime() {
  1212. return lastHeartbeat;
  1213. }
  1214. /**
  1215. * Returns the timestamp (milliseconds since the epoch) of the last received
  1216. * UIDL request for this UI.
  1217. *
  1218. * @return
  1219. */
  1220. public long getLastUidlRequestTime() {
  1221. return lastUidlRequest;
  1222. }
  1223. /**
  1224. * Sets the last heartbeat request timestamp for this UI. Called by the
  1225. * framework whenever the application receives a valid heartbeat request for
  1226. * this UI.
  1227. */
  1228. public void setLastHeartbeatTime(long lastHeartbeat) {
  1229. this.lastHeartbeat = lastHeartbeat;
  1230. }
  1231. /**
  1232. * Sets the last UIDL request timestamp for this UI. Called by the framework
  1233. * whenever the application receives a valid UIDL request for this UI.
  1234. */
  1235. public void setLastUidlRequestTime(long lastUidlRequest) {
  1236. this.lastUidlRequest = lastUidlRequest;
  1237. }
  1238. }