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 41KB

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