Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LegacyWindow.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright 2000-2018 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 com.vaadin.server.LegacyApplication;
  20. import com.vaadin.server.Page.BrowserWindowResizeListener;
  21. import com.vaadin.server.Resource;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.shared.ui.BorderStyle;
  24. /**
  25. * Helper class to emulate the main window from Vaadin 6 using UIs. This class
  26. * should be used in the same way as Window used as a browser level window in
  27. * Vaadin 6 with {@link com.vaadin.server.LegacyApplication}
  28. */
  29. @Deprecated
  30. public class LegacyWindow extends UI {
  31. private String name;
  32. private LegacyApplication application;
  33. /**
  34. * Create a new legacy window.
  35. */
  36. public LegacyWindow() {
  37. super(new VerticalLayout());
  38. ((VerticalLayout) getContent()).setSpacing(false);
  39. }
  40. /**
  41. * Creates a new legacy window with the given caption.
  42. *
  43. * @param caption
  44. * the caption of the window
  45. */
  46. public LegacyWindow(String caption) {
  47. this();
  48. setCaption(caption);
  49. }
  50. /**
  51. * Creates a legacy window with the given caption and content layout.
  52. *
  53. * @param caption
  54. * @param content
  55. */
  56. public LegacyWindow(String caption, ComponentContainer content) {
  57. super(content);
  58. setCaption(caption);
  59. }
  60. @Override
  61. protected void init(VaadinRequest request) {
  62. // Just empty
  63. }
  64. public void setApplication(LegacyApplication application) {
  65. this.application = application;
  66. }
  67. public LegacyApplication getApplication() {
  68. return application;
  69. }
  70. /**
  71. * Gets the unique name of the window. The name of the window is used to
  72. * uniquely identify it.
  73. * <p>
  74. * The name also determines the URL that can be used for direct access to a
  75. * window. All windows can be accessed through
  76. * {@code http://host:port/app/win} where {@code http://host:port/app} is
  77. * the application URL (as returned by {@link LegacyApplication#getURL()}
  78. * and {@code win} is the window name.
  79. * </p>
  80. * <p>
  81. * Note! Portlets do not support direct window access through URLs.
  82. * </p>
  83. *
  84. * @return the Name of the Window.
  85. */
  86. public String getName() {
  87. return name;
  88. }
  89. /**
  90. * Sets the unique name of the window. The name of the window is used to
  91. * uniquely identify it inside the application.
  92. * <p>
  93. * The name also determines the URL that can be used for direct access to a
  94. * window. All windows can be accessed through
  95. * {@code http://host:port/app/win} where {@code http://host:port/app} is
  96. * the application URL (as returned by {@link LegacyApplication#getURL()}
  97. * and {@code win} is the window name.
  98. * </p>
  99. * <p>
  100. * This method can only be called before the window is added to an
  101. * application.
  102. * <p>
  103. * Note! Portlets do not support direct window access through URLs.
  104. * </p>
  105. *
  106. * @param name
  107. * the new name for the window or null if the application should
  108. * automatically assign a name to it
  109. * @throws IllegalStateException
  110. * if the window is attached to an application
  111. */
  112. public void setName(String name) {
  113. this.name = name;
  114. // The name can not be changed in application
  115. if (isAttached()) {
  116. throw new IllegalStateException(
  117. "Window name can not be changed while "
  118. + "the window is in application");
  119. }
  120. }
  121. /**
  122. * Gets the full URL of the window. The returned URL is window specific and
  123. * can be used to directly refer to the window.
  124. * <p>
  125. * Note! This method can not be used for portlets.
  126. * </p>
  127. *
  128. * @return the URL of the window or null if the window is not attached to an
  129. * application
  130. */
  131. public URL getURL() {
  132. LegacyApplication application = getApplication();
  133. if (application == null) {
  134. return null;
  135. }
  136. try {
  137. return new URL(application.getURL(), getName() + "/");
  138. } catch (MalformedURLException e) {
  139. throw new RuntimeException(
  140. "Internal problem getting window URL, please report");
  141. }
  142. }
  143. /**
  144. * Opens the given resource in this UI. The contents of this UI is replaced
  145. * by the {@code Resource}.
  146. *
  147. * @param resource
  148. * the resource to show in this UI
  149. *
  150. * @deprecated As of 7.0, use getPage().setLocation instead
  151. */
  152. @Deprecated
  153. public void open(Resource resource) {
  154. open(resource, null, false);
  155. }
  156. /* ********************************************************************* */
  157. /**
  158. * Opens the given resource in a window with the given name.
  159. * <p>
  160. * The supplied {@code windowName} is used as the target name in a
  161. * window.open call in the client. This means that special values such as
  162. * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
  163. * <code>null</code> window name is also a special case.
  164. * </p>
  165. * <p>
  166. * "", null and "_self" as {@code windowName} all causes the resource to be
  167. * opened in the current window, replacing any old contents. For
  168. * downloadable content you should avoid "_self" as "_self" causes the
  169. * client to skip rendering of any other changes as it considers them
  170. * irrelevant (the page will be replaced by the resource). This can speed up
  171. * the opening of a resource, but it might also put the client side into an
  172. * inconsistent state if the window content is not completely replaced e.g.,
  173. * if the resource is downloaded instead of displayed in the browser.
  174. * </p>
  175. * <p>
  176. * "_blank" as {@code windowName} causes the resource to always be opened in
  177. * a new window or tab (depends on the browser and browser settings).
  178. * </p>
  179. * <p>
  180. * "_top" and "_parent" as {@code windowName} works as specified by the HTML
  181. * standard.
  182. * </p>
  183. * <p>
  184. * Any other {@code windowName} will open the resource in a window with that
  185. * name, either by opening a new window/tab in the browser or by replacing
  186. * the contents of an existing window with that name.
  187. * </p>
  188. * <p>
  189. * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page
  190. * has been replaced with similar methods based on a String URL. This is
  191. * because the usage of Resource is problematic with memory management and
  192. * with security features in some browsers. Is is recommended to instead use
  193. * {@link Link} for starting downloads.
  194. * </p>
  195. *
  196. * @param resource
  197. * the resource.
  198. * @param windowName
  199. * the name of the window.
  200. * @deprecated As of 7.0, use getPage().open instead
  201. */
  202. @Deprecated
  203. public void open(Resource resource, String windowName) {
  204. open(resource, windowName, true);
  205. }
  206. /**
  207. * Opens the given resource in a window with the given name and optionally
  208. * tries to force the resource to open in a new window instead of a new tab.
  209. * <p>
  210. * The supplied {@code windowName} is used as the target name in a
  211. * window.open call in the client. This means that special values such as
  212. * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
  213. * <code>null</code> window name is also a special case.
  214. * </p>
  215. * <p>
  216. * "", null and "_self" as {@code windowName} all causes the resource to be
  217. * opened in the current window, replacing any old contents. For
  218. * downloadable content you should avoid "_self" as "_self" causes the
  219. * client to skip rendering of any other changes as it considers them
  220. * irrelevant (the page will be replaced by the resource). This can speed up
  221. * the opening of a resource, but it might also put the client side into an
  222. * inconsistent state if the window content is not completely replaced e.g.,
  223. * if the resource is downloaded instead of displayed in the browser.
  224. * </p>
  225. * <p>
  226. * "_blank" as {@code windowName} causes the resource to always be opened in
  227. * a new window or tab (depends on the browser and browser settings).
  228. * </p>
  229. * <p>
  230. * "_top" and "_parent" as {@code windowName} works as specified by the HTML
  231. * standard.
  232. * </p>
  233. * <p>
  234. * Any other {@code windowName} will open the resource in a window with that
  235. * name, either by opening a new window/tab in the browser or by replacing
  236. * the contents of an existing window with that name.
  237. * </p>
  238. * <p>
  239. * If {@code windowName} is set to open the resource in a new window or tab
  240. * and {@code tryToOpenAsPopup} is true, this method attempts to force the
  241. * browser to open a new window instead of a tab. NOTE: This is a
  242. * best-effort attempt and may not work reliably with all browsers and
  243. * different pop-up preferences. With most browsers using default settings,
  244. * {@code tryToOpenAsPopup} works properly.
  245. * </p>
  246. * <p>
  247. * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page
  248. * has been replaced with similar methods based on a String URL. This is
  249. * because the usage of Resource is problematic with memory management and
  250. * with security features in some browsers. Is is recommended to instead use
  251. * {@link Link} for starting downloads.
  252. * </p>
  253. *
  254. * @param resource
  255. * the resource.
  256. * @param windowName
  257. * the name of the window.
  258. * @param tryToOpenAsPopup
  259. * Whether to try to force the resource to be opened in a new
  260. * window
  261. */
  262. public void open(Resource resource, String windowName,
  263. boolean tryToOpenAsPopup) {
  264. getPage().open(resource, windowName, tryToOpenAsPopup);
  265. }
  266. /**
  267. * Opens the given resource in a window with the given size, border and
  268. * name. For more information on the meaning of {@code windowName}, see
  269. * {@link #open(Resource, String)}.
  270. * <p>
  271. * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page
  272. * has been replaced with similar methods based on a String URL. This is
  273. * because the usage of Resource is problematic with memory management and
  274. * with security features in some browsers. Is is recommended to instead use
  275. * {@link Link} for starting downloads.
  276. * </p>
  277. *
  278. * @param resource
  279. * the resource.
  280. * @param windowName
  281. * the name of the window.
  282. * @param width
  283. * the width of the window in pixels
  284. * @param height
  285. * the height of the window in pixels
  286. * @param border
  287. * the border style of the window.
  288. * @deprecated As of 7.0, use getPage().open instead
  289. */
  290. @Deprecated
  291. public void open(Resource resource, String windowName, int width,
  292. int height, BorderStyle border) {
  293. getPage().open(resource, windowName, width, height, border);
  294. }
  295. /**
  296. * Adds a new {@link BrowserWindowResizeListener} to this UI. The listener
  297. * will be notified whenever the browser window within which this UI resides
  298. * is resized.
  299. *
  300. * @param resizeListener
  301. * the listener to add
  302. *
  303. * @see BrowserWindowResizeListener#browserWindowResized(com.vaadin.server.Page.BrowserWindowResizeEvent)
  304. * BrowserWindowResizeListener#browserWindowResized(BrowserWindowResizeEvent)
  305. * @see #setResizeLazy(boolean)
  306. *
  307. * @deprecated As of 7.0, use the similarly named api in Page instead
  308. */
  309. @Deprecated
  310. public void addListener(BrowserWindowResizeListener resizeListener) {
  311. getPage().addBrowserWindowResizeListener(resizeListener);
  312. }
  313. /**
  314. * Removes a {@link BrowserWindowResizeListener} from this UI. The listener
  315. * will no longer be notified when the browser window is resized.
  316. *
  317. * @param resizeListener
  318. * the listener to remove
  319. * @deprecated As of 7.0, use the similarly named api in Page instead
  320. */
  321. @Deprecated
  322. public void removeListener(BrowserWindowResizeListener resizeListener) {
  323. getPage().removeBrowserWindowResizeListener(resizeListener);
  324. }
  325. /**
  326. * Gets the last known height of the browser window in which this UI
  327. * resides.
  328. *
  329. * @return the browser window height in pixels
  330. * @deprecated As of 7.0, use the similarly named api in Page instead
  331. */
  332. @Deprecated
  333. public int getBrowserWindowHeight() {
  334. return getPage().getBrowserWindowHeight();
  335. }
  336. /**
  337. * Gets the last known width of the browser window in which this UI resides.
  338. *
  339. * @return the browser window width in pixels
  340. *
  341. * @deprecated As of 7.0, use the similarly named api in Page instead
  342. */
  343. @Deprecated
  344. public int getBrowserWindowWidth() {
  345. return getPage().getBrowserWindowWidth();
  346. }
  347. /**
  348. * Executes JavaScript in this window.
  349. *
  350. * <p>
  351. * This method allows one to inject javascript from the server to client. A
  352. * client implementation is not required to implement this functionality,
  353. * but currently all web-based clients do implement this.
  354. * </p>
  355. *
  356. * <p>
  357. * Executing javascript this way often leads to cross-browser compatibility
  358. * issues and regressions that are hard to resolve. Use of this method
  359. * should be avoided and instead it is recommended to create new widgets
  360. * with GWT. For more info on creating own, reusable client-side widgets in
  361. * Java, read the corresponding chapter in Book of Vaadin.
  362. * </p>
  363. *
  364. * @param script
  365. * JavaScript snippet that will be executed.
  366. *
  367. * @deprecated As of 7.0, use JavaScript.getCurrent().execute(String)
  368. * instead
  369. */
  370. @Deprecated
  371. public void executeJavaScript(String script) {
  372. getPage().getJavaScript().execute(script);
  373. }
  374. @Override
  375. public void setCaption(String caption) {
  376. // Override to provide backwards compatibility
  377. getState().caption = caption;
  378. getPage().setTitle(caption);
  379. }
  380. @Override
  381. public ComponentContainer getContent() {
  382. return (ComponentContainer) super.getContent();
  383. }
  384. /**
  385. * Set the content of the window. For a {@link LegacyWindow}, the content
  386. * must be a {@link ComponentContainer}.
  387. *
  388. * @param content
  389. */
  390. @Override
  391. public void setContent(Component content) {
  392. if (!(content instanceof ComponentContainer)) {
  393. throw new IllegalArgumentException(
  394. "The content of a LegacyWindow must be a ComponentContainer");
  395. }
  396. super.setContent(content);
  397. }
  398. /**
  399. * This implementation replaces a component in the content container (
  400. * {@link #getContent()}) instead of in the actual UI.
  401. *
  402. * This method should only be called when the content is a
  403. * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
  404. * set).
  405. */
  406. public void replaceComponent(Component oldComponent,
  407. Component newComponent) {
  408. getContent().replaceComponent(oldComponent, newComponent);
  409. }
  410. /**
  411. * Adds a component to this UI. The component is not added directly to the
  412. * UI, but instead to the content container ({@link #getContent()}).
  413. *
  414. * This method should only be called when the content is a
  415. * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
  416. * set).
  417. *
  418. * @param component
  419. * the component to add to this UI
  420. *
  421. * @see #getContent()
  422. */
  423. public void addComponent(Component component) {
  424. getContent().addComponent(component);
  425. }
  426. /**
  427. * This implementation removes the component from the content container (
  428. * {@link #getContent()}) instead of from the actual UI.
  429. *
  430. * This method should only be called when the content is a
  431. * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
  432. * set).
  433. */
  434. public void removeComponent(Component component) {
  435. getContent().removeComponent(component);
  436. }
  437. /**
  438. * This implementation removes the components from the content container (
  439. * {@link #getContent()}) instead of from the actual UI.
  440. *
  441. * This method should only be called when the content is a
  442. * {@link ComponentContainer} (default {@link VerticalLayout} or explicitly
  443. * set).
  444. */
  445. public void removeAllComponents() {
  446. getContent().removeAllComponents();
  447. }
  448. }