001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.ui.box;
021 
022 import js.Id;
023 import js.Initializer;
024 import js.Js;
025 import js.ObjectLike;
026 import jsx.dom.Styles;
027 import jsx.ui.Box;
028 import jsx.ui.Component;
029 import jsx.ui.Widget;
030 import jsx.ui.dd.Draggable;
031 import jsx.ui.dd.Mouse;
032 import jsx.ui.dd.Resizer;
033 import jsx.ui.event.OnPopup;
034 import jsx.ui.event.Popup;
035 import jsx.ui.event.Render;
036 
037 /**
038  * <p>A base class for dialog widgets.</p>
039  * <p>A dialog widget is a pop-up window which is movable and resizable.</p>
040  * <p>A {@link Dialog} is a {@link Frame} that has a title {@link Box} and a content {@link Box}.</p>
041  * 
042  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
043  */
044 public class Dialog extends Frame implements OnPopup
045 {
046     /**
047      * <p>Constructs an untitled dialog by default.</p>
048      * <p>This constructor invokes {@link #Dialog(Component)} with a new component created by calling
049      * {@link Component#untitled()} as argument.</p>
050      * @since 1.0
051      */
052     public Dialog() {
053         this(Component.untitled());
054     }
055 
056     /**
057      * <p>Typically constructs a dialog with the specified HTML text as title.</p>
058      * <p>This constructor invokes {@link Dialog#Dialog(Component)} with the component created by calling
059      * {@link Component#title(String)} as argument.</p>
060      * @param title The title text for the dialog.
061      * @since 1.0
062      */
063     public Dialog(String title) {
064         this(Component.title(title));
065     }
066 
067     /**
068      * <p>Typically constructs a dialog with a specified title component.</p>
069      * <p>This constructor invokes the default constructor of the superclass.</p>
070      * <p>It creates necessary {@link Mouse} and {@link Draggable} widgets with the underlying 
071      * component, adds {@link Resizer}s, and makes itself listen to event of {@link Popup} from itself.</p>
072      * @param title The title component for the dialog.
073      * @since 1.0
074      */
075     protected Dialog(Component title) {
076         Box<Widget> tb = getTitle();
077         tb.add(title);
078         Mouse m = Mouse.getMouse(tb.unwrap());
079         Draggable d = new Draggable(unwrap());
080         ini(d).var(Draggable.DELEGABLE, true);
081         d.attach(m);
082         Resizer.addResizers(unwrap(), Resizer.ALL);
083         unwrap().addListener(Popup.class, this);
084     }
085 
086     /**
087      * <p>Performs an action on the dispatched event.</p>
088      * <p>This method invokes the super handler for rendering, sets up mouse event handlers for the 
089      * underlying HTML element on the browser level and pops-up the newly rendered dialog window by
090      * executing a created {@link Popup} event from itself.</p>
091      * @param evt The event dispatched to this listener.
092      * @since 1.0
093      */
094     @Override
095     public void onEvent(Render evt) {
096         super.onEvent(evt);
097         attachEvent("mousedown", new Popup());
098         exec(new Popup());
099     }
100 
101     private final static Id<Integer> ZINDEX = new Id<Integer>();
102     private static int zIndexTopMost = 999;
103 
104     /**
105      * <p>Performs an action on the dispatched event.</p>
106      * <p>This method pops up the dialog window.</p>
107      * @param evt The event dispatched to this listener.
108      * @since 1.0
109      */
110     public void onEvent(Popup evt) {
111         Component e = unwrap();
112         if (Js.neq(ini(e).var(ZINDEX), zIndexTopMost)) {
113             int zIndex = ++zIndexTopMost;
114             ini(e).var(ZINDEX, zIndex);
115             ObjectLike p = new Initializer().var();
116             Styles.zIndex(p, Js.toString(zIndex));
117             Component.applyStyle(e, p);
118         }
119     }
120 }