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.

Window.java 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.ui;
  5. import java.lang.reflect.Method;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.Collections;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.LinkedList;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import com.itmill.toolkit.Application;
  15. import com.itmill.toolkit.terminal.DownloadStream;
  16. import com.itmill.toolkit.terminal.PaintException;
  17. import com.itmill.toolkit.terminal.PaintTarget;
  18. import com.itmill.toolkit.terminal.ParameterHandler;
  19. import com.itmill.toolkit.terminal.Resource;
  20. import com.itmill.toolkit.terminal.Terminal;
  21. import com.itmill.toolkit.terminal.URIHandler;
  22. /**
  23. * Application window component.
  24. *
  25. * @author IT Mill Ltd.
  26. * @version
  27. * @VERSION@
  28. * @since 3.0
  29. */
  30. public class Window extends Panel implements URIHandler, ParameterHandler {
  31. /**
  32. * Window with no border.
  33. */
  34. public static final int BORDER_NONE = 0;
  35. /**
  36. * Window with only minimal border.
  37. */
  38. public static final int BORDER_MINIMAL = 1;
  39. /**
  40. * Window with default borders.
  41. */
  42. public static final int BORDER_DEFAULT = 2;
  43. /**
  44. * The terminal this window is attached to.
  45. */
  46. private Terminal terminal = null;
  47. /**
  48. * The application this window is attached to.
  49. */
  50. private Application application = null;
  51. /**
  52. * List of URI handlers for this window.
  53. */
  54. private LinkedList uriHandlerList = null;
  55. /**
  56. * List of parameter handlers for this window.
  57. */
  58. private LinkedList parameterHandlerList = null;
  59. /** Set of subwindows */
  60. private final HashSet subwindows = new HashSet();
  61. /**
  62. * Explicitly specified theme of this window. If null, application theme is
  63. * used.
  64. */
  65. private String theme = null;
  66. /**
  67. * Resources to be opened automatically on next repaint.
  68. */
  69. private final LinkedList openList = new LinkedList();
  70. /**
  71. * The name of the window.
  72. */
  73. private String name = null;
  74. /**
  75. * Window border mode.
  76. */
  77. private int border = BORDER_DEFAULT;
  78. /**
  79. * Distance of Window top border in pixels from top border of the containing
  80. * (main window) or -1 if unspecified.
  81. */
  82. private int positionY = -1;
  83. /**
  84. * Distance of Window left border in pixels from left border of the
  85. * containing (main window) or -1 if unspecified .
  86. */
  87. private int positionX = -1;
  88. private LinkedList notifications;
  89. private boolean modal = false;
  90. private boolean resizable = true;
  91. private boolean centerRequested = false;
  92. /* ********************************************************************* */
  93. /**
  94. * Creates a new empty unnamed window with default layout.
  95. *
  96. * <p>
  97. * To show the window in application, it must be added to application with
  98. * <code>Application.addWindow</code> method.
  99. * </p>
  100. *
  101. * <p>
  102. * The windows are scrollable by default.
  103. * </p>
  104. *
  105. * @param caption
  106. * the Title of the window.
  107. */
  108. public Window() {
  109. this("", null);
  110. }
  111. /**
  112. * Creates a new empty window with default layout.
  113. *
  114. * <p>
  115. * To show the window in application, it must be added to application with
  116. * <code>Application.addWindow</code> method.
  117. * </p>
  118. *
  119. * <p>
  120. * The windows are scrollable by default.
  121. * </p>
  122. *
  123. * @param caption
  124. * the Title of the window.
  125. */
  126. public Window(String caption) {
  127. this(caption, null);
  128. }
  129. /**
  130. * Creates a new window.
  131. *
  132. * <p>
  133. * To show the window in application, it must be added to application with
  134. * <code>Application.addWindow</code> method.
  135. * </p>
  136. *
  137. * <p>
  138. * The windows are scrollable by default.
  139. * </p>
  140. *
  141. * @param caption
  142. * the Title of the window.
  143. * @param layout
  144. * the Layout of the window.
  145. */
  146. public Window(String caption, Layout layout) {
  147. super(caption, layout);
  148. setScrollable(true);
  149. }
  150. /**
  151. * Gets the terminal type.
  152. *
  153. * @return the Value of property terminal.
  154. */
  155. public Terminal getTerminal() {
  156. return terminal;
  157. }
  158. /* ********************************************************************* */
  159. /**
  160. * Gets the window of the component. Returns the window where this component
  161. * belongs to. If the component does not yet belong to a window the returns
  162. * null.
  163. *
  164. * @return the parent window of the component.
  165. */
  166. public final Window getWindow() {
  167. return this;
  168. }
  169. /**
  170. * Gets the application instance of the component. Returns the application
  171. * where this component belongs to. If the component does not yet belong to
  172. * a application the returns null.
  173. *
  174. * @return the parent application of the component.
  175. */
  176. public final Application getApplication() {
  177. if (getParent() == null) {
  178. return application;
  179. }
  180. return ((Window) getParent()).getApplication();
  181. }
  182. /**
  183. * Getter for property parent.
  184. *
  185. * <p>
  186. * Parent is the visual parent of a component. Each component can belong to
  187. * only one ComponentContainer at time.
  188. * </p>
  189. *
  190. * <p>
  191. * For windows attached directly to the application, parent is
  192. * <code>null</code>. For windows inside other windows, parent is the window
  193. * containing this window.
  194. * </p>
  195. *
  196. * @return the Value of property parent.
  197. */
  198. public final Component getParent() {
  199. return super.getParent();
  200. }
  201. /**
  202. * Setter for property parent.
  203. *
  204. * <p>
  205. * Parent is the visual parent of a component. This is mostly called by
  206. * containers add method and should not be called directly
  207. * </p>
  208. *
  209. * @param parent
  210. * the New value of property parent.
  211. */
  212. public void setParent(Component parent) {
  213. super.setParent(parent);
  214. }
  215. /**
  216. * Gets the component UIDL tag.
  217. *
  218. * @return the Component UIDL tag as string.
  219. */
  220. public String getTag() {
  221. return "window";
  222. }
  223. /* ********************************************************************* */
  224. /**
  225. * Adds the new URI handler to this window. For sub-windows, URI handlers
  226. * are attached to root level window.
  227. *
  228. * @param handler
  229. * the URI handler to add.
  230. */
  231. public void addURIHandler(URIHandler handler) {
  232. if (getParent() != null) {
  233. // this is subwindow, attach to main level instead
  234. // TODO hold internal list also and remove on detach
  235. Window mainWindow = (Window) getParent();
  236. mainWindow.addURIHandler(handler);
  237. } else {
  238. if (uriHandlerList == null) {
  239. uriHandlerList = new LinkedList();
  240. }
  241. synchronized (uriHandlerList) {
  242. if (!uriHandlerList.contains(handler)) {
  243. uriHandlerList.addLast(handler);
  244. }
  245. }
  246. }
  247. }
  248. /**
  249. * Removes the given URI handler from this window.
  250. *
  251. * @param handler
  252. * the URI handler to remove.
  253. */
  254. public void removeURIHandler(URIHandler handler) {
  255. if (getParent() != null) {
  256. // this is subwindow
  257. Window mainWindow = (Window) getParent();
  258. mainWindow.removeURIHandler(handler);
  259. } else {
  260. if (handler == null || uriHandlerList == null) {
  261. return;
  262. }
  263. synchronized (uriHandlerList) {
  264. uriHandlerList.remove(handler);
  265. if (uriHandlerList.isEmpty()) {
  266. uriHandlerList = null;
  267. }
  268. }
  269. }
  270. }
  271. /**
  272. * Handles uri recursively.
  273. *
  274. * @param context
  275. * @param relativeUri
  276. */
  277. public DownloadStream handleURI(URL context, String relativeUri) {
  278. DownloadStream result = null;
  279. if (uriHandlerList != null) {
  280. Object[] handlers;
  281. synchronized (uriHandlerList) {
  282. handlers = uriHandlerList.toArray();
  283. }
  284. for (int i = 0; i < handlers.length; i++) {
  285. final DownloadStream ds = ((URIHandler) handlers[i]).handleURI(
  286. context, relativeUri);
  287. if (ds != null) {
  288. if (result != null) {
  289. throw new RuntimeException("handleURI for " + context
  290. + " uri: '" + relativeUri
  291. + "' returns ambigious result.");
  292. }
  293. result = ds;
  294. }
  295. }
  296. }
  297. return result;
  298. }
  299. /* ********************************************************************* */
  300. /**
  301. * Adds the new parameter handler to this window. For sub windows, parameter
  302. * handlers are attached to parent windows.
  303. *
  304. * @param handler
  305. * the parameter handler to add.
  306. */
  307. public void addParameterHandler(ParameterHandler handler) {
  308. if (getParent() != null) {
  309. // this is subwindow
  310. // TODO hold internal list also and remove on detach
  311. Window mainWindow = (Window) getParent();
  312. mainWindow.addParameterHandler(handler);
  313. } else {
  314. if (parameterHandlerList == null) {
  315. parameterHandlerList = new LinkedList();
  316. }
  317. synchronized (parameterHandlerList) {
  318. if (!parameterHandlerList.contains(handler)) {
  319. parameterHandlerList.addLast(handler);
  320. }
  321. }
  322. }
  323. }
  324. /**
  325. * Removes the given URI handler from this window.
  326. *
  327. * @param handler
  328. * the parameter handler to remove.
  329. */
  330. public void removeParameterHandler(ParameterHandler handler) {
  331. if (getParent() != null) {
  332. // this is subwindow
  333. Window mainWindow = (Window) getParent();
  334. mainWindow.addParameterHandler(handler);
  335. } else {
  336. if (handler == null || parameterHandlerList == null) {
  337. return;
  338. }
  339. synchronized (parameterHandlerList) {
  340. parameterHandlerList.remove(handler);
  341. if (parameterHandlerList.isEmpty()) {
  342. parameterHandlerList = null;
  343. }
  344. }
  345. }
  346. }
  347. /* Documented by the interface */
  348. public void handleParameters(Map parameters) {
  349. if (parameterHandlerList != null) {
  350. Object[] handlers;
  351. synchronized (parameterHandlerList) {
  352. handlers = parameterHandlerList.toArray();
  353. }
  354. for (int i = 0; i < handlers.length; i++) {
  355. ((ParameterHandler) handlers[i]).handleParameters(parameters);
  356. }
  357. }
  358. }
  359. /* ********************************************************************* */
  360. /**
  361. * Gets the theme for this window.
  362. *
  363. * <p>
  364. * Subwindows do not support themes and thus return theme used by the parent
  365. * </p>
  366. *
  367. * @return the Name of the theme used in window. If the theme for this
  368. * individual window is not explicitly set, the application theme is
  369. * used instead. If application is not assigned the
  370. * terminal.getDefaultTheme is used. If terminal is not set, null is
  371. * returned
  372. */
  373. public String getTheme() {
  374. if (getParent() != null) {
  375. return ((Window) getParent()).getTheme();
  376. }
  377. if (theme != null) {
  378. return theme;
  379. }
  380. if ((application != null) && (application.getTheme() != null)) {
  381. return application.getTheme();
  382. }
  383. if (terminal != null) {
  384. return terminal.getDefaultTheme();
  385. }
  386. return null;
  387. }
  388. /**
  389. * Sets the theme for this window.
  390. *
  391. * Setting theme for subwindows is not supported.
  392. *
  393. * In Toolkit 5 terminal will reload its host page on theme changes.
  394. *
  395. * @param theme
  396. * the New theme for this window. Null implies the default theme.
  397. */
  398. public void setTheme(String theme) {
  399. if (getParent() != null) {
  400. throw new UnsupportedOperationException(
  401. "Setting theme for sub-windows is not supported.");
  402. }
  403. this.theme = theme;
  404. requestRepaint();
  405. }
  406. /**
  407. * Paints the content of this component.
  408. *
  409. * @param event
  410. * the Paint Event.
  411. * @throws PaintException
  412. * if the paint operation failed.
  413. */
  414. public synchronized void paintContent(PaintTarget target)
  415. throws PaintException {
  416. // Sets the window name
  417. final String name = getName();
  418. target.addAttribute("name", name == null ? "" : name);
  419. // Sets the window theme
  420. final String theme = getTheme();
  421. target.addAttribute("theme", theme == null ? "" : theme);
  422. if (modal) {
  423. target.addAttribute("modal", true);
  424. }
  425. if (resizable) {
  426. target.addAttribute("resizable", true);
  427. }
  428. if (centerRequested) {
  429. target.addAttribute("center", true);
  430. centerRequested = false;
  431. }
  432. // Marks the main window
  433. if (getApplication() != null
  434. && this == getApplication().getMainWindow()) {
  435. target.addAttribute("main", true);
  436. }
  437. // Open requested resource
  438. synchronized (openList) {
  439. if (!openList.isEmpty()) {
  440. for (final Iterator i = openList.iterator(); i.hasNext();) {
  441. ((OpenResource) i.next()).paintContent(target);
  442. }
  443. openList.clear();
  444. }
  445. }
  446. // Contents of the window panel is painted
  447. super.paintContent(target);
  448. // Window position
  449. target.addVariable(this, "positionx", getPositionX());
  450. target.addVariable(this, "positiony", getPositionY());
  451. // Window closing
  452. target.addVariable(this, "close", false);
  453. // Paint subwindows
  454. for (final Iterator i = subwindows.iterator(); i.hasNext();) {
  455. final Window w = (Window) i.next();
  456. w.paint(target);
  457. }
  458. // Paint notifications
  459. if (notifications != null) {
  460. target.startTag("notifications");
  461. for (final Iterator it = notifications.iterator(); it.hasNext();) {
  462. final Notification n = (Notification) it.next();
  463. target.startTag("notification");
  464. if (n.getCaption() != null) {
  465. target.addAttribute("caption", n.getCaption());
  466. }
  467. if (n.getMessage() != null) {
  468. target.addAttribute("message", n.getMessage());
  469. }
  470. if (n.getIcon() != null) {
  471. target.addAttribute("icon", n.getIcon());
  472. }
  473. target.addAttribute("position", n.getPosition());
  474. target.addAttribute("delay", n.getDelayMsec());
  475. if (n.getStyleName() != null) {
  476. target.addAttribute("style", n.getStyleName());
  477. }
  478. target.endTag("notification");
  479. }
  480. target.endTag("notifications");
  481. notifications = null;
  482. }
  483. }
  484. /* ********************************************************************* */
  485. /**
  486. * Opens the given resource in this window.
  487. *
  488. * @param resource
  489. */
  490. public void open(Resource resource) {
  491. synchronized (openList) {
  492. if (!openList.contains(resource)) {
  493. openList.add(new OpenResource(resource, null, -1, -1,
  494. BORDER_DEFAULT));
  495. }
  496. }
  497. requestRepaint();
  498. }
  499. /* ********************************************************************* */
  500. /**
  501. * Opens the given resource in named terminal window. Empty or
  502. * <code>null</code> window name results the resource to be opened in this
  503. * window.
  504. *
  505. * @param resource
  506. * the resource.
  507. * @param windowName
  508. * the name of the window.
  509. */
  510. public void open(Resource resource, String windowName) {
  511. synchronized (openList) {
  512. if (!openList.contains(resource)) {
  513. openList.add(new OpenResource(resource, windowName, -1, -1,
  514. BORDER_DEFAULT));
  515. }
  516. }
  517. requestRepaint();
  518. }
  519. /* ********************************************************************* */
  520. /**
  521. * Opens the given resource in named terminal window with given size and
  522. * border properties. Empty or <code>null</code> window name results the
  523. * resource to be opened in this window.
  524. *
  525. * @param resource
  526. * @param windowName
  527. * @param width
  528. * @param height
  529. * @param border
  530. */
  531. public void open(Resource resource, String windowName, int width,
  532. int height, int border) {
  533. synchronized (openList) {
  534. if (!openList.contains(resource)) {
  535. openList.add(new OpenResource(resource, windowName, width,
  536. height, border));
  537. }
  538. }
  539. requestRepaint();
  540. }
  541. /* ********************************************************************* */
  542. /**
  543. * Returns the full url of the window, this returns window specific url even
  544. * for the main window.
  545. *
  546. * @return the URL of the window.
  547. */
  548. public URL getURL() {
  549. if (application == null) {
  550. return null;
  551. }
  552. try {
  553. return new URL(application.getURL(), getName() + "/");
  554. } catch (final MalformedURLException e) {
  555. throw new RuntimeException(
  556. "Internal problem getting window URL, please report");
  557. }
  558. }
  559. /**
  560. * Gets the unique name of the window that indentifies it on the terminal.
  561. *
  562. * <p>
  563. * Name identifies the URL used to access application-level windows, but is
  564. * not used for windows inside other windows. all application-level windows
  565. * can be accessed by their names in url
  566. * <code>http://host:port/foo/bar/</code> where
  567. * <code>http://host:port/foo/</code> is the application url as returned by
  568. * getURL() and <code>bar</code> is the name of the window. Also note that
  569. * not all windows should be added to application - one can also add windows
  570. * inside other windows - these windows show as smaller windows inside those
  571. * windows.
  572. * </p>
  573. *
  574. * @return the Name of the Window.
  575. */
  576. public String getName() {
  577. return name;
  578. }
  579. /**
  580. * Returns the border.
  581. *
  582. * @return the border.
  583. */
  584. public int getBorder() {
  585. return border;
  586. }
  587. /**
  588. * Sets the border.
  589. *
  590. * @param border
  591. * the border to set.
  592. */
  593. public void setBorder(int border) {
  594. this.border = border;
  595. }
  596. /**
  597. * Sets the application this window is connected to.
  598. *
  599. * <p>
  600. * This method should not be invoked directly. Instead the
  601. * {@link com.itmill.toolkit.Application#addWindow(Window)} method should be
  602. * used to add the window to an application and
  603. * {@link com.itmill.toolkit.Application#removeWindow(Window)} method for
  604. * removing the window from the applicion. These methods call this method
  605. * implicitly.
  606. * </p>
  607. *
  608. * <p>
  609. * The method invokes {@link Component#attach()} and
  610. * {@link Component#detach()} methods when necessary.
  611. * <p>
  612. *
  613. * @param application
  614. * the application to set.
  615. */
  616. public void setApplication(Application application) {
  617. // If the application is not changed, dont do nothing
  618. if (application == this.application) {
  619. return;
  620. }
  621. // Sends detach event if the window is connected to application
  622. if (this.application != null) {
  623. detach();
  624. }
  625. // Connects to new parent
  626. this.application = application;
  627. // Sends the attach event if connected to a window
  628. if (application != null) {
  629. attach();
  630. }
  631. }
  632. /**
  633. * Sets the name.
  634. * <p>
  635. * The name of the window must be unique inside the application.
  636. * </p>
  637. *
  638. * <p>
  639. * If the name is null, the the window is given name automatically when it
  640. * is added to an application.
  641. * </p>
  642. *
  643. * @param name
  644. * the name to set.
  645. */
  646. public void setName(String name) {
  647. // The name can not be changed in application
  648. if (getApplication() != null) {
  649. throw new IllegalStateException(
  650. "Window name can not be changed while "
  651. + "the window is in application");
  652. }
  653. this.name = name;
  654. }
  655. /**
  656. * Sets the terminal type. The terminal type is set by the the terminal
  657. * adapter and may change from time to time.
  658. *
  659. * @param type
  660. * the terminal type to set.
  661. */
  662. public void setTerminal(Terminal type) {
  663. terminal = type;
  664. }
  665. /**
  666. * Private data structure for storing opening window properties.
  667. */
  668. private class OpenResource {
  669. private final Resource resource;
  670. private final String name;
  671. private final int width;
  672. private final int height;
  673. private final int border;
  674. /**
  675. * Creates a new open resource.
  676. *
  677. * @param resource
  678. * @param name
  679. * @param width
  680. * @param height
  681. * @param border
  682. */
  683. private OpenResource(Resource resource, String name, int width,
  684. int height, int border) {
  685. this.resource = resource;
  686. this.name = name;
  687. this.width = width;
  688. this.height = height;
  689. this.border = border;
  690. }
  691. /**
  692. * Paints the open-tag inside the window.
  693. *
  694. * @param target
  695. * the Paint Event.
  696. * @throws PaintException
  697. * if the Paint Operation fails.
  698. */
  699. private void paintContent(PaintTarget target) throws PaintException {
  700. target.startTag("open");
  701. target.addAttribute("src", resource);
  702. if (name != null && name.length() > 0) {
  703. target.addAttribute("name", name);
  704. }
  705. if (width >= 0) {
  706. target.addAttribute("width", width);
  707. }
  708. if (height >= 0) {
  709. target.addAttribute("height", height);
  710. }
  711. switch (border) {
  712. case Window.BORDER_MINIMAL:
  713. target.addAttribute("border", "minimal");
  714. break;
  715. case Window.BORDER_NONE:
  716. target.addAttribute("border", "none");
  717. break;
  718. }
  719. target.endTag("open");
  720. }
  721. }
  722. /**
  723. * Called when one or more variables handled by the implementing class are
  724. * changed.
  725. *
  726. * @see com.itmill.toolkit.terminal.VariableOwner#changeVariables(java.lang.Object,
  727. * java.util.Map)
  728. */
  729. public void changeVariables(Object source, Map variables) {
  730. super.changeVariables(source, variables);
  731. // Positioning
  732. final Integer positionx = (Integer) variables.get("positionx");
  733. if (positionx != null) {
  734. final int x = positionx.intValue();
  735. setPositionX(x < 0 ? -1 : x);
  736. }
  737. final Integer positiony = (Integer) variables.get("positiony");
  738. if (positiony != null) {
  739. final int y = positiony.intValue();
  740. setPositionY(y < 0 ? -1 : y);
  741. }
  742. // Closing
  743. final Boolean close = (Boolean) variables.get("close");
  744. if (close != null && close.booleanValue()) {
  745. close();
  746. }
  747. }
  748. /**
  749. * Method that handles window closing (from UI). If one wants to have window
  750. * that cannot be closed (with server side check), override this with and
  751. * empty method.
  752. */
  753. protected void close() {
  754. Window parent = (Window) getParent();
  755. if (parent == null) {
  756. setVisible(false);
  757. fireClose();
  758. } else {
  759. // subwindow is removed from parent
  760. parent.removeWindow(this);
  761. fireClose();
  762. }
  763. }
  764. /**
  765. * Gets the distance of Window left border in pixels from left border of the
  766. * containing (main window).
  767. *
  768. * @return the Distance of Window left border in pixels from left border of
  769. * the containing (main window). or -1 if unspecified.
  770. * @since 4.0.0
  771. */
  772. public int getPositionX() {
  773. return positionX;
  774. }
  775. /**
  776. * Sets the distance of Window left border in pixels from left border of the
  777. * containing (main window).
  778. *
  779. * @param positionX
  780. * the Distance of Window left border in pixels from left border
  781. * of the containing (main window). or -1 if unspecified.
  782. * @since 4.0.0
  783. */
  784. public void setPositionX(int positionX) {
  785. this.positionX = positionX;
  786. }
  787. /**
  788. * Gets the distance of Window top border in pixels from top border of the
  789. * containing (main window).
  790. *
  791. * @return Distance of Window top border in pixels from top border of the
  792. * containing (main window). or -1 if unspecified .
  793. *
  794. * @since 4.0.0
  795. */
  796. public int getPositionY() {
  797. return positionY;
  798. }
  799. /**
  800. * Sets the distance of Window top border in pixels from top border of the
  801. * containing (main window).
  802. *
  803. * @param positionY
  804. * the Distance of Window top border in pixels from top border of
  805. * the containing (main window). or -1 if unspecified
  806. *
  807. * @since 4.0.0
  808. */
  809. public void setPositionY(int positionY) {
  810. this.positionY = positionY;
  811. }
  812. private static final Method WINDOW_CLOSE_METHOD;
  813. static {
  814. try {
  815. WINDOW_CLOSE_METHOD = CloseListener.class.getDeclaredMethod(
  816. "windowClose", new Class[] { CloseEvent.class });
  817. } catch (final java.lang.NoSuchMethodException e) {
  818. // This should never happen
  819. throw new java.lang.RuntimeException(
  820. "Internal error, window close method not found");
  821. }
  822. }
  823. public class CloseEvent extends Component.Event {
  824. /**
  825. * Serial generated by eclipse.
  826. */
  827. private static final long serialVersionUID = -7235770057344367327L;
  828. /**
  829. *
  830. * @param source
  831. */
  832. public CloseEvent(Component source) {
  833. super(source);
  834. }
  835. /**
  836. * Gets the Window.
  837. *
  838. * @return the window.
  839. */
  840. public Window getWindow() {
  841. return (Window) getSource();
  842. }
  843. }
  844. public interface CloseListener {
  845. public void windowClose(CloseEvent e);
  846. }
  847. /**
  848. * Adds the listener.
  849. *
  850. * @param listener
  851. * the listener to add.
  852. */
  853. public void addListener(CloseListener listener) {
  854. addListener(CloseEvent.class, listener, WINDOW_CLOSE_METHOD);
  855. }
  856. /**
  857. * Removes the listener.
  858. *
  859. * @param listener
  860. * the listener to remove.
  861. */
  862. public void removeListener(CloseListener listener) {
  863. addListener(CloseEvent.class, listener, WINDOW_CLOSE_METHOD);
  864. }
  865. protected void fireClose() {
  866. fireEvent(new Window.CloseEvent(this));
  867. }
  868. /**
  869. * Adds a new window inside another window.
  870. *
  871. * <p>
  872. * Adding windows inside another window creates "subwindows". These windows
  873. * should not be added to application directly and are not accessible
  874. * directly with any url. Addding windows implicitly sets their parents.
  875. * </p>
  876. *
  877. * <p>
  878. * Only one level of subwindows are supported. Thus you can add windows
  879. * inside such windows whose parent is <code>null</code>.
  880. * </p>
  881. *
  882. * @param window
  883. * @throws IllegalArgumentException
  884. * if a window is added inside non-application level window.
  885. * @throws NullPointerException
  886. * if the given <code>Window</code> is <code>null</code>.
  887. */
  888. public void addWindow(Window window) throws IllegalArgumentException,
  889. NullPointerException {
  890. if (getParent() != null) {
  891. throw new IllegalArgumentException(
  892. "You can only add windows inside application-level windows");
  893. }
  894. if (window == null) {
  895. throw new NullPointerException("Argument must not be null");
  896. }
  897. subwindows.add(window);
  898. window.setParent(this);
  899. requestRepaint();
  900. }
  901. /**
  902. * Remove the given subwindow from this window.
  903. *
  904. * @param window
  905. * Window to be removed.
  906. */
  907. public void removeWindow(Window window) {
  908. subwindows.remove(window);
  909. window.setParent(null);
  910. requestRepaint();
  911. }
  912. /**
  913. * Get the set of all child windows.
  914. *
  915. * @return Set of child windows.
  916. */
  917. public Set getChildWindows() {
  918. return Collections.unmodifiableSet(subwindows);
  919. }
  920. /**
  921. * Sets sub-window modal, so that widgets behind it cannot be accessed.
  922. * <b>Note:</b> affects sub-windows only.
  923. *
  924. * @param modality
  925. * true if modality is to be turned on
  926. */
  927. public void setModal(boolean modality) {
  928. modal = modality;
  929. center();
  930. requestRepaint();
  931. }
  932. /**
  933. * @return true if this window is modal.
  934. */
  935. public boolean isModal() {
  936. return modal;
  937. }
  938. /**
  939. * Sets sub-window resizable. <b>Note:</b> affects sub-windows only.
  940. *
  941. * @param resizable
  942. * true if resizability is to be turned on
  943. */
  944. public void setResizable(boolean resizeability) {
  945. resizable = resizeability;
  946. requestRepaint();
  947. }
  948. /**
  949. *
  950. * @return true if window is resizable by the end-user, otherwise false.
  951. */
  952. public boolean isResizable() {
  953. return resizable;
  954. }
  955. /**
  956. * Request to center this window on the screen. <b>Note:</b> affects
  957. * sub-windows only.
  958. */
  959. public void center() {
  960. centerRequested = true;
  961. }
  962. /**
  963. * Shows a notification message on the middle of the window. The message
  964. * automatically disappears ("humanized message").
  965. *
  966. * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
  967. * @see Notification
  968. *
  969. * @param caption
  970. * The message
  971. */
  972. public void showNotification(String caption) {
  973. addNotification(new Notification(caption));
  974. }
  975. /**
  976. * Shows a notification message the window. The position and behavior of the
  977. * message depends on the type, which is one of the basic types defined in
  978. * {@link Notification}, for instance Notification.TYPE_WARNING_MESSAGE.
  979. *
  980. * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
  981. * @see Notification
  982. *
  983. * @param caption
  984. * The message
  985. * @param type
  986. * The message type
  987. */
  988. public void showNotification(String caption, int type) {
  989. addNotification(new Notification(caption, type));
  990. }
  991. /**
  992. * Shows a notification consisting of a bigger caption and a smaller
  993. * description on the middle of the window. The message automatically
  994. * disappears ("humanized message").
  995. *
  996. * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
  997. * @see Notification
  998. *
  999. * @param caption
  1000. * The caption of the message
  1001. * @param description
  1002. * The message description
  1003. *
  1004. */
  1005. public void showNotification(String caption, String description) {
  1006. addNotification(new Notification(caption, description));
  1007. }
  1008. /**
  1009. * Shows a notification consisting of a bigger caption and a smaller
  1010. * description. The position and behavior of the message depends on the
  1011. * type, which is one of the basic types defined in {@link Notification},
  1012. * for instance Notification.TYPE_WARNING_MESSAGE.
  1013. *
  1014. * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
  1015. * @see Notification
  1016. *
  1017. * @param caption
  1018. * The caption of the message
  1019. * @param description
  1020. * The message description
  1021. * @param type
  1022. * The message type
  1023. */
  1024. public void showNotification(String caption, String description, int type) {
  1025. addNotification(new Notification(caption, description, type));
  1026. }
  1027. /**
  1028. * Shows a notification message.
  1029. *
  1030. * @see Notification
  1031. * @see #showNotification(String)
  1032. * @see #showNotification(String, int)
  1033. * @see #showNotification(String, String)
  1034. * @see #showNotification(String, String, int)
  1035. *
  1036. * @param notification
  1037. * The notification message to show
  1038. */
  1039. public void showNotification(Notification notification) {
  1040. addNotification(notification);
  1041. }
  1042. private void addNotification(Notification notification) {
  1043. if (notifications == null) {
  1044. notifications = new LinkedList();
  1045. }
  1046. notifications.add(notification);
  1047. requestRepaint();
  1048. }
  1049. /**
  1050. * A notification message, used to display temporary messages to the user -
  1051. * for example "Document saved", or "Save failed".
  1052. * <p>
  1053. * The notification message can consist of several parts: caption,
  1054. * description and icon. It is usually used with only caption - one should
  1055. * be wary of filling the notification with too much information.
  1056. * </p>
  1057. * <p>
  1058. * The notification message tries to be as unobtrusive as possible, while
  1059. * still drawing needed attention. There are several basic types of messages
  1060. * that can be used in different situations:
  1061. * <ul>
  1062. * <li>TYPE_HUMANIZED_MESSAGE fades away quickly as soon as the user uses
  1063. * the mouse or types something. It can be used to show fairly unimportant
  1064. * messages, such as feedback that an operation succeeded ("Document Saved")
  1065. * - the kind of messages the user ignores once the application is familiar.
  1066. * </li>
  1067. * <li>TYPE_WARNING_MESSAGE is shown for a short while after the user uses
  1068. * the mouse or types something. It's default style is also more noticeable
  1069. * than the humanized message. It can be used for messages that do not
  1070. * contain a lot of important information, but should be noticed by the
  1071. * user. Despite the name, it does not have to be a warning, but can be used
  1072. * instead of the humanized message whenever you want to make the message a
  1073. * little more noticeable.</li>
  1074. * <li>TYPE_ERROR_MESSAGE requires to user to click it before disappearing,
  1075. * and can be used for critical messages.</li>
  1076. * <li>TYPE_TRAY_NOTIFICATION is shown for a while in the lower left corner
  1077. * of the window, and can be used for "convenience notifications" that do
  1078. * not have to be noticed immediately, and should not interfere with the
  1079. * current task - for instance to show "You have a new message in your
  1080. * inbox" while the user is working in some other area of the application.</li>
  1081. * </ul>
  1082. * </p>
  1083. * <p>
  1084. * In addition to the basic pre-configured types, a Notification can also be
  1085. * configured to show up in a custom position, for a specified time (or
  1086. * until clicked), and with a custom stylename. An icon can also be added.
  1087. * </p>
  1088. *
  1089. */
  1090. public static class Notification {
  1091. public static final int TYPE_HUMANIZED_MESSAGE = 1;
  1092. public static final int TYPE_WARNING_MESSAGE = 2;
  1093. public static final int TYPE_ERROR_MESSAGE = 3;
  1094. public static final int TYPE_TRAY_NOTIFICATION = 4;
  1095. public static final int POSITION_CENTERED = 1;
  1096. public static final int POSITION_CENTERED_TOP = 2;
  1097. public static final int POSITION_CENTERED_BOTTOM = 3;
  1098. public static final int POSITION_TOP_LEFT = 4;
  1099. public static final int POSITION_TOP_RIGHT = 5;
  1100. public static final int POSITION_BOTTOM_LEFT = 6;
  1101. public static final int POSITION_BOTTOM_RIGHT = 7;
  1102. public static final int DELAY_FOREVER = -1;
  1103. public static final int DELAY_NONE = 0;
  1104. private String caption;
  1105. private String description;
  1106. private Resource icon;
  1107. private int position = POSITION_CENTERED;
  1108. private int delayMsec = 0;
  1109. private String styleName;
  1110. /**
  1111. * Creates a "humanized" notification message.
  1112. *
  1113. * @param caption
  1114. * The message to show
  1115. */
  1116. public Notification(String caption) {
  1117. this(caption, null, TYPE_HUMANIZED_MESSAGE);
  1118. }
  1119. /**
  1120. * Creates a notification message of the specified type.
  1121. *
  1122. * @param caption
  1123. * The message to show
  1124. * @param type
  1125. * The type of message
  1126. */
  1127. public Notification(String caption, int type) {
  1128. this(caption, null, type);
  1129. }
  1130. /**
  1131. * Creates a "humanized" notification message with a bigger caption and
  1132. * smaller description.
  1133. *
  1134. * @param caption
  1135. * The message caption
  1136. * @param description
  1137. * The message description
  1138. */
  1139. public Notification(String caption, String description) {
  1140. this(caption, description, TYPE_HUMANIZED_MESSAGE);
  1141. }
  1142. /**
  1143. * Creates a notification message of the specified type, with a bigger
  1144. * caption and smaller description.
  1145. *
  1146. * @param caption
  1147. * The message caption
  1148. * @param description
  1149. * The message description
  1150. * @param type
  1151. * The type of message
  1152. */
  1153. public Notification(String caption, String description, int type) {
  1154. this.caption = caption;
  1155. this.description = description;
  1156. setType(type);
  1157. }
  1158. private void setType(int type) {
  1159. switch (type) {
  1160. case TYPE_WARNING_MESSAGE:
  1161. delayMsec = 1500;
  1162. styleName = "warning";
  1163. break;
  1164. case TYPE_ERROR_MESSAGE:
  1165. delayMsec = -1;
  1166. styleName = "error";
  1167. break;
  1168. case TYPE_TRAY_NOTIFICATION:
  1169. delayMsec = 3000;
  1170. position = POSITION_BOTTOM_RIGHT;
  1171. styleName = "tray";
  1172. case TYPE_HUMANIZED_MESSAGE:
  1173. default:
  1174. break;
  1175. }
  1176. }
  1177. /**
  1178. * Gets the caption part of the notification message.
  1179. *
  1180. * @return The message caption
  1181. */
  1182. public String getCaption() {
  1183. return caption;
  1184. }
  1185. /**
  1186. * Sets the caption part of the notification message
  1187. *
  1188. * @param caption
  1189. * The message caption
  1190. */
  1191. public void setCaption(String caption) {
  1192. this.caption = caption;
  1193. }
  1194. /**
  1195. * @deprecated Use {@link #getDescription()} instead.
  1196. * @return
  1197. */
  1198. public String getMessage() {
  1199. return description;
  1200. }
  1201. /**
  1202. * @deprecated Use {@link #setDescription(String)} instead.
  1203. * @param description
  1204. */
  1205. public void setMessage(String description) {
  1206. this.description = description;
  1207. }
  1208. /**
  1209. * Gets the description part of the notification message.
  1210. *
  1211. * @return The message description.
  1212. */
  1213. public String getDescription() {
  1214. return description;
  1215. }
  1216. /**
  1217. * Sets the description part of the notification message.
  1218. *
  1219. * @param description
  1220. */
  1221. public void setDescription(String description) {
  1222. this.description = description;
  1223. }
  1224. /**
  1225. * Gets the position of the notification message.
  1226. *
  1227. * @return The position
  1228. */
  1229. public int getPosition() {
  1230. return position;
  1231. }
  1232. /**
  1233. * Sets the position of the notification message.
  1234. *
  1235. * @param position
  1236. * The desired notification position
  1237. */
  1238. public void setPosition(int position) {
  1239. this.position = position;
  1240. }
  1241. /**
  1242. * Gets the icon part of the notification message.
  1243. *
  1244. * @return The message icon
  1245. */
  1246. public Resource getIcon() {
  1247. return icon;
  1248. }
  1249. /**
  1250. * Sets the icon part of the notification message.
  1251. *
  1252. * @param icon
  1253. * The desired message icon
  1254. */
  1255. public void setIcon(Resource icon) {
  1256. this.icon = icon;
  1257. }
  1258. /**
  1259. * Gets the delay before the notification disappears.
  1260. *
  1261. * @return the delay in msec, -1 indicates the message has to be
  1262. * clicked.
  1263. */
  1264. public int getDelayMsec() {
  1265. return delayMsec;
  1266. }
  1267. /**
  1268. * Sets the delay before the notification disappears.
  1269. *
  1270. * @param delayMsec
  1271. * the desired delay in msec, -1 to require the user to click
  1272. * the message
  1273. */
  1274. public void setDelayMsec(int delayMsec) {
  1275. this.delayMsec = delayMsec;
  1276. }
  1277. /**
  1278. * Sets the style name for the notification message.
  1279. *
  1280. * @param styleName
  1281. * The desired style name.
  1282. */
  1283. public void setStyleName(String styleName) {
  1284. this.styleName = styleName;
  1285. }
  1286. /**
  1287. * Gets the style name for the notification message.
  1288. *
  1289. * @return
  1290. */
  1291. public String getStyleName() {
  1292. return styleName;
  1293. }
  1294. }
  1295. }