001: /*
002: * Copyright 2000,2006 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.plaf.css.PopupCG;
016: import org.wings.session.SessionManager;
017:
018: /**
019: * Popups are used to display a <code>SComponent</code> to the user, typically
020: * on top of all the other <code>SComponent</code>s in a particular containment
021: * hierarchy.
022: * <p>
023: * The general contract is that if you need to change the size of the
024: * <code>SComponent</code>, or location of the <code>SPopup</code>, you should
025: * obtain a new <code>Popup</code>.
026: * <p>
027: * <code>SPopup</code> does not descend from <code>SComponent</code>, rather
028: * implementations of <code>SPopup</code> are responsible for creating
029: * and maintaining their own <code>SComponent</code>s to render the
030: * requested <code>SComponent</code> to the user.
031: *
032: * @author Christian Schyma
033: */
034: public class SPopup extends SComponent {
035:
036: /**
037: * The SComponent representing the Popup.
038: */
039: private SComponent contents;
040:
041: /**
042: * Owner Frame.
043: */
044: private SComponent owner;
045:
046: /**
047: * popup x position, origin is the top left of the browser viewport
048: */
049: private int x;
050:
051: /**
052: * popup y position, origin is the top left of the browser viewport
053: */
054: private int y;
055:
056: /**
057: * width of the popup which will be retrieved by using
058: * contents.getPreferredSize()
059: */
060: private int width;
061:
062: /**
063: * height of the popup which will be retrieved by using
064: * contents.getPreferredSize()
065: */
066: private int height;
067:
068: // TODO: use constants from Swing
069: public static final String TOP_RIGHT = "tr";
070: public static final String TOP_LEFT = "tl";
071: public static final String BOTTOM_RIGHT = "br";
072: public static final String BOTTOM_LEFT = "bl";
073:
074: private boolean anchored = false;
075: private SComponent context;
076: private String contentsCorner;
077: private String contextCorner;
078:
079: /**
080: * Creates a <code>SPopup</code>.
081: * <code>x</code> and <code>y</code> specify the preferred initial location
082: * to place the <code>SPopup</code> at. Based on screen size, or other
083: * paramaters, the <code>SPopup</code> may not display at <code>x</code> and
084: * <code>y</code>.
085: * <p>
086: * N.B.: check that the contents SComponent has a valid preferred size!
087: * (<code>getPreferredSize()</code>)
088: * <p>
089: * @param owner owner of component SPopup, if null the root frame will
090: * be used
091: * @param contents Contents of the Popup
092: * @param x Initial x screen coordinate
093: * @param y Initial y screen coordinate
094: * @exception IllegalArgumentException if contents is null
095: */
096: public SPopup(SComponent owner, SComponent contents, int x, int y) {
097: if (contents == null) {
098: throw new IllegalArgumentException(
099: "Contents must be non-null");
100: }
101:
102: if (owner == null) {
103: this .owner = SessionManager.getSession().getRootFrame();
104: ;
105: } else {
106: this .owner = owner;
107: }
108:
109: this .contents = contents;
110: this .x = x;
111: this .y = y;
112: this .width = contents.getPreferredSize().getWidthInt();
113: this .height = contents.getPreferredSize().getHeightInt();
114:
115: setCG(new PopupCG(this ));
116: }
117:
118: /**
119: * Anchors the popup to the the given component <code>context</code>.
120: * @param context component to be anchored to
121: * @param contentsCorner anchor point: e.g. SAnchoredPopup.TOP_LEFT
122: * @param contextCorner anchor point: e.g. SAnchoredPopup.BOTTOM_LEFT
123: */
124: public void setContext(SComponent context, String contentsCorner,
125: String contextCorner) {
126: this .anchored = true;
127: this .context = context;
128: this .contentsCorner = contentsCorner;
129: this .contextCorner = contextCorner;
130: ((PopupCG) getCG()).attachJavaScript();
131: }
132:
133: protected void finalize() throws Throwable {
134: ((PopupCG) getCG()).tidyUp();
135: }
136:
137: /**
138: * Returns the <code>Component</code> returned from
139: * <code>createComponent</code> that will hold the <code>Popup</code>.
140: */
141: public SComponent getComponent() {
142: return this .contents;
143: }
144:
145: public String getContentsCorner() {
146: return contentsCorner;
147: }
148:
149: public String getContextCorner() {
150: return contextCorner;
151: }
152:
153: public int getHeight() {
154: return height;
155: }
156:
157: public int getWidth() {
158: return width;
159: }
160:
161: public int getX() {
162: return x;
163: }
164:
165: public int getY() {
166: return y;
167: }
168:
169: public SComponent getOwner() {
170: return owner;
171: }
172:
173: public boolean isAnchored() {
174: return anchored;
175: }
176:
177: public SComponent getContext() {
178: return context;
179: }
180:
181: }
|