1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.ui;
-
- import java.lang.reflect.Method;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Map;
- import java.util.Set;
-
- import com.itmill.toolkit.Application;
- import com.itmill.toolkit.terminal.DownloadStream;
- import com.itmill.toolkit.terminal.PaintException;
- import com.itmill.toolkit.terminal.PaintTarget;
- import com.itmill.toolkit.terminal.ParameterHandler;
- import com.itmill.toolkit.terminal.Resource;
- import com.itmill.toolkit.terminal.Terminal;
- import com.itmill.toolkit.terminal.URIHandler;
-
- /**
- * Application window component.
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 3.0
- */
- public class Window extends Panel implements URIHandler, ParameterHandler {
-
- /**
- * Window with no border.
- */
- public static final int BORDER_NONE = 0;
-
- /**
- * Window with only minimal border.
- */
- public static final int BORDER_MINIMAL = 1;
-
- /**
- * Window with default borders.
- */
- public static final int BORDER_DEFAULT = 2;
-
- /**
- * The terminal this window is attached to.
- */
- private Terminal terminal = null;
-
- /**
- * The application this window is attached to.
- */
- private Application application = null;
-
- /**
- * List of URI handlers for this window.
- */
- private LinkedList uriHandlerList = null;
-
- /**
- * List of parameter handlers for this window.
- */
- private LinkedList parameterHandlerList = null;
-
- /** Set of subwindows */
- private final HashSet subwindows = new HashSet();
-
- /**
- * Explicitly specified theme of this window. If null, application theme is
- * used.
- */
- private String theme = null;
-
- /**
- * Resources to be opened automatically on next repaint.
- */
- private final LinkedList openList = new LinkedList();
-
- /**
- * The name of the window.
- */
- private String name = null;
-
- /**
- * Window border mode.
- */
- private int border = BORDER_DEFAULT;
-
- /**
- * Distance of Window top border in pixels from top border of the containing
- * (main window) or -1 if unspecified.
- */
- private int positionY = -1;
-
- /**
- * Distance of Window left border in pixels from left border of the
- * containing (main window) or -1 if unspecified .
- */
- private int positionX = -1;
-
- private LinkedList notifications;
-
- private boolean modal = false;
-
- private boolean resizable = true;
-
- private boolean centerRequested = false;
-
- /* ********************************************************************* */
-
- /**
- * Creates a new empty unnamed window with default layout.
- *
- * <p>
- * To show the window in application, it must be added to application with
- * <code>Application.addWindow</code> method.
- * </p>
- *
- * <p>
- * The windows are scrollable by default.
- * </p>
- *
- * @param caption
- * the Title of the window.
- */
- public Window() {
- this("", null);
- }
-
- /**
- * Creates a new empty window with default layout.
- *
- * <p>
- * To show the window in application, it must be added to application with
- * <code>Application.addWindow</code> method.
- * </p>
- *
- * <p>
- * The windows are scrollable by default.
- * </p>
- *
- * @param caption
- * the Title of the window.
- */
- public Window(String caption) {
- this(caption, null);
- }
-
- /**
- * Creates a new window.
- *
- * <p>
- * To show the window in application, it must be added to application with
- * <code>Application.addWindow</code> method.
- * </p>
- *
- * <p>
- * The windows are scrollable by default.
- * </p>
- *
- * @param caption
- * the Title of the window.
- * @param layout
- * the Layout of the window.
- */
- public Window(String caption, Layout layout) {
- super(caption, layout);
- setScrollable(true);
- }
-
- /**
- * Gets the terminal type.
- *
- * @return the Value of property terminal.
- */
- public Terminal getTerminal() {
- return terminal;
- }
-
- /* ********************************************************************* */
-
- /**
- * Gets the window of the component. Returns the window where this component
- * belongs to. If the component does not yet belong to a window the returns
- * null.
- *
- * @return the parent window of the component.
- */
- public final Window getWindow() {
- return this;
- }
-
- /**
- * Gets the application instance of the component. Returns the application
- * where this component belongs to. If the component does not yet belong to
- * a application the returns null.
- *
- * @return the parent application of the component.
- */
- public final Application getApplication() {
- if (getParent() == null) {
- return application;
- }
- return ((Window) getParent()).getApplication();
- }
-
- /**
- * Getter for property parent.
- *
- * <p>
- * Parent is the visual parent of a component. Each component can belong to
- * only one ComponentContainer at time.
- * </p>
- *
- * <p>
- * For windows attached directly to the application, parent is
- * <code>null</code>. For windows inside other windows, parent is the window
- * containing this window.
- * </p>
- *
- * @return the Value of property parent.
- */
- public final Component getParent() {
- return super.getParent();
- }
-
- /**
- * Setter for property parent.
- *
- * <p>
- * Parent is the visual parent of a component. This is mostly called by
- * containers add method and should not be called directly
- * </p>
- *
- * @param parent
- * the New value of property parent.
- */
- public void setParent(Component parent) {
- super.setParent(parent);
- }
-
- /**
- * Gets the component UIDL tag.
- *
- * @return the Component UIDL tag as string.
- */
- public String getTag() {
- return "window";
- }
-
- /* ********************************************************************* */
-
- /**
- * Adds the new URI handler to this window. For sub-windows, URI handlers
- * are attached to root level window.
- *
- * @param handler
- * the URI handler to add.
- */
- public void addURIHandler(URIHandler handler) {
- if (getParent() != null) {
- // this is subwindow, attach to main level instead
- // TODO hold internal list also and remove on detach
- Window mainWindow = (Window) getParent();
- mainWindow.addURIHandler(handler);
- } else {
- if (uriHandlerList == null) {
- uriHandlerList = new LinkedList();
- }
- synchronized (uriHandlerList) {
- if (!uriHandlerList.contains(handler)) {
- uriHandlerList.addLast(handler);
- }
- }
- }
- }
-
- /**
- * Removes the given URI handler from this window.
- *
- * @param handler
- * the URI handler to remove.
- */
- public void removeURIHandler(URIHandler handler) {
- if (getParent() != null) {
- // this is subwindow
- Window mainWindow = (Window) getParent();
- mainWindow.removeURIHandler(handler);
- } else {
- if (handler == null || uriHandlerList == null) {
- return;
- }
- synchronized (uriHandlerList) {
- uriHandlerList.remove(handler);
- if (uriHandlerList.isEmpty()) {
- uriHandlerList = null;
- }
- }
- }
- }
-
- /**
- * Handles uri recursively.
- *
- * @param context
- * @param relativeUri
- */
- public DownloadStream handleURI(URL context, String relativeUri) {
-
- DownloadStream result = null;
- if (uriHandlerList != null) {
- Object[] handlers;
- synchronized (uriHandlerList) {
- handlers = uriHandlerList.toArray();
- }
- for (int i = 0; i < handlers.length; i++) {
- final DownloadStream ds = ((URIHandler) handlers[i]).handleURI(
- context, relativeUri);
- if (ds != null) {
- if (result != null) {
- throw new RuntimeException("handleURI for " + context
- + " uri: '" + relativeUri
- + "' returns ambigious result.");
- }
- result = ds;
- }
- }
- }
- return result;
- }
-
- /* ********************************************************************* */
-
- /**
- * Adds the new parameter handler to this window. For sub windows, parameter
- * handlers are attached to parent windows.
- *
- * @param handler
- * the parameter handler to add.
- */
- public void addParameterHandler(ParameterHandler handler) {
- if (getParent() != null) {
- // this is subwindow
- // TODO hold internal list also and remove on detach
- Window mainWindow = (Window) getParent();
- mainWindow.addParameterHandler(handler);
- } else {
- if (parameterHandlerList == null) {
- parameterHandlerList = new LinkedList();
- }
- synchronized (parameterHandlerList) {
- if (!parameterHandlerList.contains(handler)) {
- parameterHandlerList.addLast(handler);
- }
- }
- }
-
- }
-
- /**
- * Removes the given URI handler from this window.
- *
- * @param handler
- * the parameter handler to remove.
- */
- public void removeParameterHandler(ParameterHandler handler) {
- if (getParent() != null) {
- // this is subwindow
- Window mainWindow = (Window) getParent();
- mainWindow.addParameterHandler(handler);
- } else {
- if (handler == null || parameterHandlerList == null) {
- return;
- }
- synchronized (parameterHandlerList) {
- parameterHandlerList.remove(handler);
- if (parameterHandlerList.isEmpty()) {
- parameterHandlerList = null;
- }
- }
- }
- }
-
- /* Documented by the interface */
- public void handleParameters(Map parameters) {
- if (parameterHandlerList != null) {
- Object[] handlers;
- synchronized (parameterHandlerList) {
- handlers = parameterHandlerList.toArray();
- }
- for (int i = 0; i < handlers.length; i++) {
- ((ParameterHandler) handlers[i]).handleParameters(parameters);
- }
- }
- }
-
- /* ********************************************************************* */
-
- /**
- * Gets the theme for this window.
- *
- * <p>
- * Subwindows do not support themes and thus return theme used by the parent
- * </p>
- *
- * @return the Name of the theme used in window. If the theme for this
- * individual window is not explicitly set, the application theme is
- * used instead. If application is not assigned the
- * terminal.getDefaultTheme is used. If terminal is not set, null is
- * returned
- */
- public String getTheme() {
- if (getParent() != null) {
- return ((Window) getParent()).getTheme();
- }
- if (theme != null) {
- return theme;
- }
- if ((application != null) && (application.getTheme() != null)) {
- return application.getTheme();
- }
- if (terminal != null) {
- return terminal.getDefaultTheme();
- }
- return null;
- }
-
- /**
- * Sets the theme for this window.
- *
- * Setting theme for subwindows is not supported.
- *
- * In Toolkit 5 terminal will reload its host page on theme changes.
- *
- * @param theme
- * the New theme for this window. Null implies the default theme.
- */
- public void setTheme(String theme) {
- if (getParent() != null) {
- throw new UnsupportedOperationException(
- "Setting theme for sub-windows is not supported.");
- }
- this.theme = theme;
- requestRepaint();
- }
-
- /**
- * Paints the content of this component.
- *
- * @param event
- * the Paint Event.
- * @throws PaintException
- * if the paint operation failed.
- */
- public synchronized void paintContent(PaintTarget target)
- throws PaintException {
-
- // Sets the window name
- final String name = getName();
- target.addAttribute("name", name == null ? "" : name);
-
- // Sets the window theme
- final String theme = getTheme();
- target.addAttribute("theme", theme == null ? "" : theme);
-
- if (modal) {
- target.addAttribute("modal", true);
- }
-
- if (resizable) {
- target.addAttribute("resizable", true);
- }
-
- if (centerRequested) {
- target.addAttribute("center", true);
- centerRequested = false;
- }
-
- // Marks the main window
- if (getApplication() != null
- && this == getApplication().getMainWindow()) {
- target.addAttribute("main", true);
- }
-
- // Open requested resource
- synchronized (openList) {
- if (!openList.isEmpty()) {
- for (final Iterator i = openList.iterator(); i.hasNext();) {
- ((OpenResource) i.next()).paintContent(target);
- }
- openList.clear();
- }
- }
-
- // Contents of the window panel is painted
- super.paintContent(target);
-
- // Window position
- target.addVariable(this, "positionx", getPositionX());
- target.addVariable(this, "positiony", getPositionY());
-
- // Window closing
- target.addVariable(this, "close", false);
-
- // Paint subwindows
- for (final Iterator i = subwindows.iterator(); i.hasNext();) {
- final Window w = (Window) i.next();
- w.paint(target);
- }
-
- // Paint notifications
- if (notifications != null) {
- target.startTag("notifications");
- for (final Iterator it = notifications.iterator(); it.hasNext();) {
- final Notification n = (Notification) it.next();
- target.startTag("notification");
- if (n.getCaption() != null) {
- target.addAttribute("caption", n.getCaption());
- }
- if (n.getMessage() != null) {
- target.addAttribute("message", n.getMessage());
- }
- if (n.getIcon() != null) {
- target.addAttribute("icon", n.getIcon());
- }
- target.addAttribute("position", n.getPosition());
- target.addAttribute("delay", n.getDelayMsec());
- if (n.getStyleName() != null) {
- target.addAttribute("style", n.getStyleName());
- }
- target.endTag("notification");
- }
- target.endTag("notifications");
- notifications = null;
- }
-
- }
-
- /* ********************************************************************* */
-
- /**
- * Opens the given resource in this window.
- *
- * @param resource
- */
- public void open(Resource resource) {
- synchronized (openList) {
- if (!openList.contains(resource)) {
- openList.add(new OpenResource(resource, null, -1, -1,
- BORDER_DEFAULT));
- }
- }
- requestRepaint();
- }
-
- /* ********************************************************************* */
-
- /**
- * Opens the given resource in named terminal window. Empty or
- * <code>null</code> window name results the resource to be opened in this
- * window.
- *
- * @param resource
- * the resource.
- * @param windowName
- * the name of the window.
- */
- public void open(Resource resource, String windowName) {
- synchronized (openList) {
- if (!openList.contains(resource)) {
- openList.add(new OpenResource(resource, windowName, -1, -1,
- BORDER_DEFAULT));
- }
- }
- requestRepaint();
- }
-
- /* ********************************************************************* */
-
- /**
- * Opens the given resource in named terminal window with given size and
- * border properties. Empty or <code>null</code> window name results the
- * resource to be opened in this window.
- *
- * @param resource
- * @param windowName
- * @param width
- * @param height
- * @param border
- */
- public void open(Resource resource, String windowName, int width,
- int height, int border) {
- synchronized (openList) {
- if (!openList.contains(resource)) {
- openList.add(new OpenResource(resource, windowName, width,
- height, border));
- }
- }
- requestRepaint();
- }
-
- /* ********************************************************************* */
-
- /**
- * Returns the full url of the window, this returns window specific url even
- * for the main window.
- *
- * @return the URL of the window.
- */
- public URL getURL() {
-
- if (application == null) {
- return null;
- }
-
- try {
- return new URL(application.getURL(), getName() + "/");
- } catch (final MalformedURLException e) {
- throw new RuntimeException(
- "Internal problem getting window URL, please report");
- }
- }
-
- /**
- * Gets the unique name of the window that indentifies it on the terminal.
- *
- * <p>
- * Name identifies the URL used to access application-level windows, but is
- * not used for windows inside other windows. all application-level windows
- * can be accessed by their names in url
- * <code>http://host:port/foo/bar/</code> where
- * <code>http://host:port/foo/</code> is the application url as returned by
- * getURL() and <code>bar</code> is the name of the window. Also note that
- * not all windows should be added to application - one can also add windows
- * inside other windows - these windows show as smaller windows inside those
- * windows.
- * </p>
- *
- * @return the Name of the Window.
- */
- public String getName() {
- return name;
- }
-
- /**
- * Returns the border.
- *
- * @return the border.
- */
- public int getBorder() {
- return border;
- }
-
- /**
- * Sets the border.
- *
- * @param border
- * the border to set.
- */
- public void setBorder(int border) {
- this.border = border;
- }
-
- /**
- * Sets the application this window is connected to.
- *
- * <p>
- * This method should not be invoked directly. Instead the
- * {@link com.itmill.toolkit.Application#addWindow(Window)} method should be
- * used to add the window to an application and
- * {@link com.itmill.toolkit.Application#removeWindow(Window)} method for
- * removing the window from the applicion. These methods call this method
- * implicitly.
- * </p>
- *
- * <p>
- * The method invokes {@link Component#attach()} and
- * {@link Component#detach()} methods when necessary.
- * <p>
- *
- * @param application
- * the application to set.
- */
- public void setApplication(Application application) {
-
- // If the application is not changed, dont do nothing
- if (application == this.application) {
- return;
- }
-
- // Sends detach event if the window is connected to application
- if (this.application != null) {
- detach();
- }
-
- // Connects to new parent
- this.application = application;
-
- // Sends the attach event if connected to a window
- if (application != null) {
- attach();
- }
- }
-
- /**
- * Sets the name.
- * <p>
- * The name of the window must be unique inside the application.
- * </p>
- *
- * <p>
- * If the name is null, the the window is given name automatically when it
- * is added to an application.
- * </p>
- *
- * @param name
- * the name to set.
- */
- public void setName(String name) {
-
- // The name can not be changed in application
- if (getApplication() != null) {
- throw new IllegalStateException(
- "Window name can not be changed while "
- + "the window is in application");
- }
-
- this.name = name;
- }
-
- /**
- * Sets the terminal type. The terminal type is set by the the terminal
- * adapter and may change from time to time.
- *
- * @param type
- * the terminal type to set.
- */
- public void setTerminal(Terminal type) {
- terminal = type;
- }
-
- /**
- * Private data structure for storing opening window properties.
- */
- private class OpenResource {
-
- private final Resource resource;
-
- private final String name;
-
- private final int width;
-
- private final int height;
-
- private final int border;
-
- /**
- * Creates a new open resource.
- *
- * @param resource
- * @param name
- * @param width
- * @param height
- * @param border
- */
- private OpenResource(Resource resource, String name, int width,
- int height, int border) {
- this.resource = resource;
- this.name = name;
- this.width = width;
- this.height = height;
- this.border = border;
- }
-
- /**
- * Paints the open-tag inside the window.
- *
- * @param target
- * the Paint Event.
- * @throws PaintException
- * if the Paint Operation fails.
- */
- private void paintContent(PaintTarget target) throws PaintException {
- target.startTag("open");
- target.addAttribute("src", resource);
- if (name != null && name.length() > 0) {
- target.addAttribute("name", name);
- }
- if (width >= 0) {
- target.addAttribute("width", width);
- }
- if (height >= 0) {
- target.addAttribute("height", height);
- }
- switch (border) {
- case Window.BORDER_MINIMAL:
- target.addAttribute("border", "minimal");
- break;
- case Window.BORDER_NONE:
- target.addAttribute("border", "none");
- break;
- }
-
- target.endTag("open");
- }
- }
-
- /**
- * Called when one or more variables handled by the implementing class are
- * changed.
- *
- * @see com.itmill.toolkit.terminal.VariableOwner#changeVariables(java.lang.Object,
- * java.util.Map)
- */
- public void changeVariables(Object source, Map variables) {
- super.changeVariables(source, variables);
-
- // Positioning
- final Integer positionx = (Integer) variables.get("positionx");
- if (positionx != null) {
- final int x = positionx.intValue();
- setPositionX(x < 0 ? -1 : x);
- }
- final Integer positiony = (Integer) variables.get("positiony");
- if (positiony != null) {
- final int y = positiony.intValue();
- setPositionY(y < 0 ? -1 : y);
- }
-
- // Closing
- final Boolean close = (Boolean) variables.get("close");
- if (close != null && close.booleanValue()) {
- close();
- }
- }
-
- /**
- * Method that handles window closing (from UI). If one wants to have window
- * that cannot be closed (with server side check), override this with and
- * empty method.
- */
- protected void close() {
- Window parent = (Window) getParent();
- if (parent == null) {
- setVisible(false);
- fireClose();
- } else {
- // subwindow is removed from parent
- parent.removeWindow(this);
- fireClose();
- }
- }
-
- /**
- * Gets the distance of Window left border in pixels from left border of the
- * containing (main window).
- *
- * @return the Distance of Window left border in pixels from left border of
- * the containing (main window). or -1 if unspecified.
- * @since 4.0.0
- */
- public int getPositionX() {
- return positionX;
- }
-
- /**
- * Sets the distance of Window left border in pixels from left border of the
- * containing (main window).
- *
- * @param positionX
- * the Distance of Window left border in pixels from left border
- * of the containing (main window). or -1 if unspecified.
- * @since 4.0.0
- */
- public void setPositionX(int positionX) {
- this.positionX = positionX;
- }
-
- /**
- * Gets the distance of Window top border in pixels from top border of the
- * containing (main window).
- *
- * @return Distance of Window top border in pixels from top border of the
- * containing (main window). or -1 if unspecified .
- *
- * @since 4.0.0
- */
- public int getPositionY() {
- return positionY;
- }
-
- /**
- * Sets the distance of Window top border in pixels from top border of the
- * containing (main window).
- *
- * @param positionY
- * the Distance of Window top border in pixels from top border of
- * the containing (main window). or -1 if unspecified
- *
- * @since 4.0.0
- */
- public void setPositionY(int positionY) {
- this.positionY = positionY;
- }
-
- private static final Method WINDOW_CLOSE_METHOD;
- static {
- try {
- WINDOW_CLOSE_METHOD = CloseListener.class.getDeclaredMethod(
- "windowClose", new Class[] { CloseEvent.class });
- } catch (final java.lang.NoSuchMethodException e) {
- // This should never happen
- throw new java.lang.RuntimeException(
- "Internal error, window close method not found");
- }
- }
-
- public class CloseEvent extends Component.Event {
-
- /**
- * Serial generated by eclipse.
- */
- private static final long serialVersionUID = -7235770057344367327L;
-
- /**
- *
- * @param source
- */
- public CloseEvent(Component source) {
- super(source);
- }
-
- /**
- * Gets the Window.
- *
- * @return the window.
- */
- public Window getWindow() {
- return (Window) getSource();
- }
- }
-
- public interface CloseListener {
- public void windowClose(CloseEvent e);
- }
-
- /**
- * Adds the listener.
- *
- * @param listener
- * the listener to add.
- */
- public void addListener(CloseListener listener) {
- addListener(CloseEvent.class, listener, WINDOW_CLOSE_METHOD);
- }
-
- /**
- * Removes the listener.
- *
- * @param listener
- * the listener to remove.
- */
- public void removeListener(CloseListener listener) {
- addListener(CloseEvent.class, listener, WINDOW_CLOSE_METHOD);
- }
-
- protected void fireClose() {
- fireEvent(new Window.CloseEvent(this));
- }
-
- /**
- * Adds a new window inside another window.
- *
- * <p>
- * Adding windows inside another window creates "subwindows". These windows
- * should not be added to application directly and are not accessible
- * directly with any url. Addding windows implicitly sets their parents.
- * </p>
- *
- * <p>
- * Only one level of subwindows are supported. Thus you can add windows
- * inside such windows whose parent is <code>null</code>.
- * </p>
- *
- * @param window
- * @throws IllegalArgumentException
- * if a window is added inside non-application level window.
- * @throws NullPointerException
- * if the given <code>Window</code> is <code>null</code>.
- */
- public void addWindow(Window window) throws IllegalArgumentException,
- NullPointerException {
-
- if (getParent() != null) {
- throw new IllegalArgumentException(
- "You can only add windows inside application-level windows");
- }
-
- if (window == null) {
- throw new NullPointerException("Argument must not be null");
- }
-
- subwindows.add(window);
- window.setParent(this);
- requestRepaint();
- }
-
- /**
- * Remove the given subwindow from this window.
- *
- * @param window
- * Window to be removed.
- */
- public void removeWindow(Window window) {
- subwindows.remove(window);
- window.setParent(null);
- requestRepaint();
-
- }
-
- /**
- * Get the set of all child windows.
- *
- * @return Set of child windows.
- */
- public Set getChildWindows() {
- return Collections.unmodifiableSet(subwindows);
- }
-
- /**
- * Sets sub-window modal, so that widgets behind it cannot be accessed.
- * <b>Note:</b> affects sub-windows only.
- *
- * @param modality
- * true if modality is to be turned on
- */
- public void setModal(boolean modality) {
- modal = modality;
- center();
- requestRepaint();
- }
-
- /**
- * @return true if this window is modal.
- */
- public boolean isModal() {
- return modal;
- }
-
- /**
- * Sets sub-window resizable. <b>Note:</b> affects sub-windows only.
- *
- * @param resizable
- * true if resizability is to be turned on
- */
- public void setResizable(boolean resizeability) {
- resizable = resizeability;
- requestRepaint();
- }
-
- /**
- *
- * @return true if window is resizable by the end-user, otherwise false.
- */
- public boolean isResizable() {
- return resizable;
- }
-
- /**
- * Request to center this window on the screen. <b>Note:</b> affects
- * sub-windows only.
- */
- public void center() {
- centerRequested = true;
- }
-
- /**
- * Shows a notification message on the middle of the window. The message
- * automatically disappears ("humanized message").
- *
- * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
- * @see Notification
- *
- * @param caption
- * The message
- */
- public void showNotification(String caption) {
- addNotification(new Notification(caption));
- }
-
- /**
- * Shows a notification message the window. The position and behavior of the
- * message depends on the type, which is one of the basic types defined in
- * {@link Notification}, for instance Notification.TYPE_WARNING_MESSAGE.
- *
- * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
- * @see Notification
- *
- * @param caption
- * The message
- * @param type
- * The message type
- */
- public void showNotification(String caption, int type) {
- addNotification(new Notification(caption, type));
- }
-
- /**
- * Shows a notification consisting of a bigger caption and a smaller
- * description on the middle of the window. The message automatically
- * disappears ("humanized message").
- *
- * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
- * @see Notification
- *
- * @param caption
- * The caption of the message
- * @param description
- * The message description
- *
- */
- public void showNotification(String caption, String description) {
- addNotification(new Notification(caption, description));
- }
-
- /**
- * Shows a notification consisting of a bigger caption and a smaller
- * description. The position and behavior of the message depends on the
- * type, which is one of the basic types defined in {@link Notification},
- * for instance Notification.TYPE_WARNING_MESSAGE.
- *
- * @see #showNotification(com.itmill.toolkit.ui.Window.Notification)
- * @see Notification
- *
- * @param caption
- * The caption of the message
- * @param description
- * The message description
- * @param type
- * The message type
- */
- public void showNotification(String caption, String description, int type) {
- addNotification(new Notification(caption, description, type));
- }
-
- /**
- * Shows a notification message.
- *
- * @see Notification
- * @see #showNotification(String)
- * @see #showNotification(String, int)
- * @see #showNotification(String, String)
- * @see #showNotification(String, String, int)
- *
- * @param notification
- * The notification message to show
- */
- public void showNotification(Notification notification) {
- addNotification(notification);
- }
-
- private void addNotification(Notification notification) {
- if (notifications == null) {
- notifications = new LinkedList();
- }
- notifications.add(notification);
- requestRepaint();
- }
-
- /**
- * A notification message, used to display temporary messages to the user -
- * for example "Document saved", or "Save failed".
- * <p>
- * The notification message can consist of several parts: caption,
- * description and icon. It is usually used with only caption - one should
- * be wary of filling the notification with too much information.
- * </p>
- * <p>
- * The notification message tries to be as unobtrusive as possible, while
- * still drawing needed attention. There are several basic types of messages
- * that can be used in different situations:
- * <ul>
- * <li>TYPE_HUMANIZED_MESSAGE fades away quickly as soon as the user uses
- * the mouse or types something. It can be used to show fairly unimportant
- * messages, such as feedback that an operation succeeded ("Document Saved")
- * - the kind of messages the user ignores once the application is familiar.
- * </li>
- * <li>TYPE_WARNING_MESSAGE is shown for a short while after the user uses
- * the mouse or types something. It's default style is also more noticeable
- * than the humanized message. It can be used for messages that do not
- * contain a lot of important information, but should be noticed by the
- * user. Despite the name, it does not have to be a warning, but can be used
- * instead of the humanized message whenever you want to make the message a
- * little more noticeable.</li>
- * <li>TYPE_ERROR_MESSAGE requires to user to click it before disappearing,
- * and can be used for critical messages.</li>
- * <li>TYPE_TRAY_NOTIFICATION is shown for a while in the lower left corner
- * of the window, and can be used for "convenience notifications" that do
- * not have to be noticed immediately, and should not interfere with the
- * current task - for instance to show "You have a new message in your
- * inbox" while the user is working in some other area of the application.</li>
- * </ul>
- * </p>
- * <p>
- * In addition to the basic pre-configured types, a Notification can also be
- * configured to show up in a custom position, for a specified time (or
- * until clicked), and with a custom stylename. An icon can also be added.
- * </p>
- *
- */
- public static class Notification {
- public static final int TYPE_HUMANIZED_MESSAGE = 1;
- public static final int TYPE_WARNING_MESSAGE = 2;
- public static final int TYPE_ERROR_MESSAGE = 3;
- public static final int TYPE_TRAY_NOTIFICATION = 4;
-
- public static final int POSITION_CENTERED = 1;
- public static final int POSITION_CENTERED_TOP = 2;
- public static final int POSITION_CENTERED_BOTTOM = 3;
- public static final int POSITION_TOP_LEFT = 4;
- public static final int POSITION_TOP_RIGHT = 5;
- public static final int POSITION_BOTTOM_LEFT = 6;
- public static final int POSITION_BOTTOM_RIGHT = 7;
-
- public static final int DELAY_FOREVER = -1;
- public static final int DELAY_NONE = 0;
-
- private String caption;
- private String description;
- private Resource icon;
- private int position = POSITION_CENTERED;
- private int delayMsec = 0;
- private String styleName;
-
- /**
- * Creates a "humanized" notification message.
- *
- * @param caption
- * The message to show
- */
- public Notification(String caption) {
- this(caption, null, TYPE_HUMANIZED_MESSAGE);
- }
-
- /**
- * Creates a notification message of the specified type.
- *
- * @param caption
- * The message to show
- * @param type
- * The type of message
- */
- public Notification(String caption, int type) {
- this(caption, null, type);
- }
-
- /**
- * Creates a "humanized" notification message with a bigger caption and
- * smaller description.
- *
- * @param caption
- * The message caption
- * @param description
- * The message description
- */
- public Notification(String caption, String description) {
- this(caption, description, TYPE_HUMANIZED_MESSAGE);
- }
-
- /**
- * Creates a notification message of the specified type, with a bigger
- * caption and smaller description.
- *
- * @param caption
- * The message caption
- * @param description
- * The message description
- * @param type
- * The type of message
- */
- public Notification(String caption, String description, int type) {
- this.caption = caption;
- this.description = description;
- setType(type);
- }
-
- private void setType(int type) {
- switch (type) {
- case TYPE_WARNING_MESSAGE:
- delayMsec = 1500;
- styleName = "warning";
- break;
- case TYPE_ERROR_MESSAGE:
- delayMsec = -1;
- styleName = "error";
- break;
- case TYPE_TRAY_NOTIFICATION:
- delayMsec = 3000;
- position = POSITION_BOTTOM_RIGHT;
- styleName = "tray";
-
- case TYPE_HUMANIZED_MESSAGE:
- default:
- break;
- }
-
- }
-
- /**
- * Gets the caption part of the notification message.
- *
- * @return The message caption
- */
- public String getCaption() {
- return caption;
- }
-
- /**
- * Sets the caption part of the notification message
- *
- * @param caption
- * The message caption
- */
- public void setCaption(String caption) {
- this.caption = caption;
- }
-
- /**
- * @deprecated Use {@link #getDescription()} instead.
- * @return
- */
- public String getMessage() {
- return description;
- }
-
- /**
- * @deprecated Use {@link #setDescription(String)} instead.
- * @param description
- */
- public void setMessage(String description) {
- this.description = description;
- }
-
- /**
- * Gets the description part of the notification message.
- *
- * @return The message description.
- */
- public String getDescription() {
- return description;
- }
-
- /**
- * Sets the description part of the notification message.
- *
- * @param description
- */
- public void setDescription(String description) {
- this.description = description;
- }
-
- /**
- * Gets the position of the notification message.
- *
- * @return The position
- */
- public int getPosition() {
- return position;
- }
-
- /**
- * Sets the position of the notification message.
- *
- * @param position
- * The desired notification position
- */
- public void setPosition(int position) {
- this.position = position;
- }
-
- /**
- * Gets the icon part of the notification message.
- *
- * @return The message icon
- */
- public Resource getIcon() {
- return icon;
- }
-
- /**
- * Sets the icon part of the notification message.
- *
- * @param icon
- * The desired message icon
- */
- public void setIcon(Resource icon) {
- this.icon = icon;
- }
-
- /**
- * Gets the delay before the notification disappears.
- *
- * @return the delay in msec, -1 indicates the message has to be
- * clicked.
- */
- public int getDelayMsec() {
- return delayMsec;
- }
-
- /**
- * Sets the delay before the notification disappears.
- *
- * @param delayMsec
- * the desired delay in msec, -1 to require the user to click
- * the message
- */
- public void setDelayMsec(int delayMsec) {
- this.delayMsec = delayMsec;
- }
-
- /**
- * Sets the style name for the notification message.
- *
- * @param styleName
- * The desired style name.
- */
- public void setStyleName(String styleName) {
- this.styleName = styleName;
- }
-
- /**
- * Gets the style name for the notification message.
- *
- * @return
- */
- public String getStyleName() {
- return styleName;
- }
- }
-
- }
|