001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.html;
021:
022: import java.util.Enumeration;
023:
024: /**
025: * HtmlPopup is a class which gives a developer a way to generate a div/layer component of a HTML page that can be used as a popup.
026: */
027: public class HtmlPopup extends HtmlContainer {
028: HtmlStyle _hsPopup;
029: HtmlScript _hscPopup;
030: HtmlLayer _hlPopup;
031: HtmlComponent _hcComp;
032: int _x = 0;
033: int _y = 0;
034: int _xvisibleoffset = 10;
035: int _yvisibleoffset = 10;
036: int _xanchoroffset = 0;
037: int _yanchoroffset = 0;
038: int _width = 0;
039: boolean _fixedLocation = false;
040: boolean _offsetByWidth = true;
041: boolean _offsetByHeight = true;
042: boolean _offsetByParent = false;
043: String _hideCondition = null;
044: String _visibleScript = null;
045: String _hideScript = null;
046: int _cellpadding = 0;
047:
048: /**
049: * HtmlPopup constructor comment.
050: * @param name java.lang.String Name of the div/layer tag generated.
051: * @param comp com.salmonllc.html.HtmlComponent The component which the div/layer is linked to.
052: * @param x int The x coordinate of the popup.
053: * @param y int The y coordinate of the popup.
054: * @param width int The width of the div/layer in pixels.
055: * @param xvisibleoffset int The number of horizontal pixels allowed to be away from the Popup in order for it to stay visible.
056: * @param yvisibleoffset int The number of vertical pixels allowed to be away from the Popup in order for it to stay visible.
057: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
058: * @param fixed boolean A boolean indicating whether the popup is to have a fixed location.
059: */
060: public HtmlPopup(String name, int x, int y, int width,
061: int xvisibleoffset, int yvisibleoffset, HtmlPage p,
062: boolean fixed) {
063: super (name, p);
064: _x = x;
065: _y = y;
066: _width = width;
067: _xvisibleoffset = xvisibleoffset;
068: _yvisibleoffset = yvisibleoffset;
069: _fixedLocation = fixed;
070: createPopup();
071: }
072:
073: /**
074: * HtmlPopup constructor comment.
075: * @param name java.lang.String Name of the div/layer tag generated.
076: * @param comp com.salmonllc.html.HtmlComponent The component which the div/layer is linked to.
077: * @param x int The x coordinate of the popup.
078: * @param y int The y coordinate of the popup.
079: * @param width int The width of the div/layer in pixels.
080: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
081: */
082: public HtmlPopup(String name, int x, int y, int width, HtmlPage p) {
083: super (name, p);
084: _x = x;
085: _y = y;
086: _width = width;
087: createPopup();
088: }
089:
090: /**
091: * HtmlPopup constructor comment.
092: * @param name java.lang.String Name of the div/layer tag generated.
093: * @param comp com.salmonllc.html.HtmlComponent The component which the div/layer is linked to.
094: * @param x int The x coordinate of the popup.
095: * @param y int The y coordinate of the popup.
096: * @param width int The width of the div/layer in pixels.
097: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
098: * @param fixed boolean A boolean indicating whether the popup is to have a fixed location.
099: */
100: public HtmlPopup(String name, int x, int y, int width, HtmlPage p,
101: boolean fixed) {
102: super (name, p);
103: _x = x;
104: _y = y;
105: _width = width;
106: _fixedLocation = fixed;
107: createPopup();
108: }
109:
110: /**
111: * HtmlPopup constructor comment.
112: * @param name java.lang.String Name of the div/layer tag generated.
113: * @param width int The width of the div/layer in pixels.
114: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
115: */
116: public HtmlPopup(String name, int width, HtmlPage p) {
117: super (name, p);
118: _width = width;
119: createPopup();
120: }
121:
122: /**
123: * HtmlPopup constructor comment.
124: * @param name java.lang.String Name of the div/layer tag generated.
125: * @param comp com.salmonllc.html.HtmlComponent The component which the div/layer is linked to.
126: * @param width int The width of the div/layer in pixels.
127: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
128: */
129: public HtmlPopup(String name, HtmlComponent comp, int width,
130: HtmlPage p) {
131: super (name, p);
132: _hcComp = comp;
133: _width = width;
134: createPopup();
135: }
136:
137: /**
138: * HtmlPopup constructor comment.
139: * @param name java.lang.String Name of the div/layer tag generated.
140: * @param comp com.salmonllc.html.HtmlComponent The component which the div/layer is linked to.
141: * @param width int The width of the div/layer in pixels.
142: * @param offsetByWidth boolean A flag to indicate to display the Popup to the right of comp.
143: * @param offsetByHeight boolean A flag to indicate to display the Popup below comp.
144: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
145: */
146: public HtmlPopup(String name, HtmlComponent comp, int width,
147: boolean offsetByWidth, boolean offsetByHeight, HtmlPage p) {
148: super (name, p);
149: _hcComp = comp;
150: _width = width;
151: _offsetByHeight = offsetByHeight;
152: _offsetByWidth = offsetByWidth;
153: createPopup();
154: }
155:
156: /**
157: * HtmlPopup constructor comment.
158: * @param name java.lang.String Name of the div/layer tag generated.
159: * @param comp com.salmonllc.html.HtmlComponent The component which the div/layer is linked to.
160: * @param width int The width of the div/layer in pixels.
161: * @param offsetByWidth boolean A flag to indicate to display the Popup to the right of comp.
162: * @param offsetByHeight boolean A flag to indicate to display the Popup below comp.
163: * @param offsetByParent boolean A flag to indicate to display the Popup relative to comp parent component.
164: * @param p com.salmonllc.html.HtmlPage The page that this popup is part of.
165: */
166: public HtmlPopup(String name, HtmlComponent comp, int width,
167: boolean offsetByWidth, boolean offsetByHeight,
168: boolean offsetByParent, HtmlPage p) {
169: super (name, p);
170: _hcComp = comp;
171: _width = width;
172: _offsetByHeight = offsetByHeight;
173: _offsetByWidth = offsetByWidth;
174: _offsetByParent = offsetByParent;
175: createPopup();
176: }
177:
178: /**
179: * Adds a new html component to the popup
180: * @param comp com.salmonllc.html.HtmlComponent
181: */
182: public void add(HtmlComponent comp) {
183:
184: _hlPopup.add(comp);
185: }
186:
187: /**
188: * A method to initialize the popup.
189: * Creation date: (3/27/01 3:58:12 PM)
190: */
191: private void createPopup() {
192: _hsPopup = new HtmlStyle(getName(), "#", "", getPage());
193: _hsPopup.setStyle("position:absolute;top:" + _y + "px;left:"
194: + _x + "px;width:" + _width
195: + "px;visibility:hidden;z-index:0;");
196: _hlPopup = new HtmlLayer(getName(), _hsPopup, getPage());
197: _hscPopup = new HtmlScript(getDisplayPopupScript(),
198: "JavaScript1.2", getPage());
199: }
200:
201: public void generateHTML(java.io.PrintWriter p, int rowNo)
202: throws Exception {
203: if (getPage().getBrowserType() != HtmlPageBase.BROWSER_MICROSOFT
204: || getPage().getBrowserVersion() < 4)
205: return;
206: if (!_visible)
207: return;
208:
209: _hsPopup.generateHTML(p, rowNo);
210: _hscPopup.generateHTML(p, rowNo);
211: _hlPopup.generateHTML(p, rowNo);
212:
213: }
214:
215: /**
216: * Returns the cell padding specified by the developer. This is usually the cell padding of a table the popup appears within
217: * Creation date: (3/27/01 1:56:08 PM)
218: * @return int The cellpadding specified with setCellPadding.
219: */
220: public int getCellPadding() {
221: return _cellpadding;
222: }
223:
224: /**
225: * This method will return a list of all components in the container.
226: * @return Enumeration An enumeration of the components within the popup.
227: */
228: public Enumeration getComponents() {
229: return _hlPopup.getComponents();
230: }
231:
232: private String getDisplayPopupScript() {
233: String sHideCondition = "";
234: if (_hideCondition != null && !_hideCondition.trim().equals(""))
235: sHideCondition = "if (" + _hideCondition + ") {\n";
236:
237: String sScript = "function " + _hlPopup.getName()
238: + "getPixelTop(htmlElement) {\n";
239: sScript += "if (htmlElement.offsetParent==null)\n";
240: sScript += "return htmlElement.offsetTop;\n";
241: sScript += "return htmlElement.offsetTop+" + _hlPopup.getName()
242: + "getPixelTop(htmlElement.offsetParent)\n";
243: sScript += "}\n";
244:
245: sScript += "function " + _hlPopup.getName()
246: + "getPixelLeft(htmlElement) {\n";
247: sScript += "if (htmlElement.offsetParent==null)\n";
248: sScript += "return htmlElement.offsetLeft;\n";
249: sScript += "return htmlElement.offsetLeft+"
250: + _hlPopup.getName()
251: + "getPixelLeft(htmlElement.offsetParent)\n";
252: sScript += "}\n";
253:
254: sScript += "function " + _hlPopup.getName() + "Popup() {\n";
255: sScript += "if (" + _hlPopup.getName()
256: + ".style.visibility=='hidden') {\n";
257: if (!_fixedLocation) {
258: if (_hcComp == null) {
259: sScript += _hlPopup.getName()
260: + ".style.top=getMouseY()+document.body.scrollTop;\n";
261: sScript += _hlPopup.getName()
262: + ".style.left=getMouseX()+document.body.scrollLeft;\n";
263: } else {
264: sScript += _hlPopup.getName()
265: + ".style.top="
266: + _hlPopup.getName()
267: + "getPixelTop("
268: + _hcComp.getFullName()
269: + (_offsetByParent ? ".offsetParent" : "")
270: + ")"
271: + (_offsetByHeight ? "+"
272: + _hcComp.getFullName()
273: + ".offsetHeight" : "") + ";\n";
274: sScript += _hlPopup.getName()
275: + ".style.left="
276: + _hlPopup.getName()
277: + "getPixelLeft("
278: + _hcComp.getFullName()
279: + (_offsetByParent ? ".offsetParent" : "")
280: + ")"
281: + (_offsetByWidth ? "+" + _hcComp.getFullName()
282: + ".offsetWidth" : "") + ";\n";
283: }
284: } else {
285: sScript += _hlPopup.getName() + ".style.top=" + _y + ";\n";
286: sScript += _hlPopup.getName() + ".style.left=" + _x + ";\n";
287: }
288: // sScript+=_hlPopup.getName()+".style.visibility='visible';\n"; //do not uncomment this line
289: sScript += "}\n";
290: sScript += "if (" + _hlPopup.getName()
291: + ".style.visibility=='visible') {\n";
292: if (_hcComp == null) {
293: sScript += "if ((getMouseX()+document.body.scrollLeft<"
294: + _hlPopup.getName() + ".offsetLeft-"
295: + _xvisibleoffset
296: + " || getMouseX()+document.body.scrollLeft>"
297: + _hlPopup.getName() + ".offsetLeft+"
298: + _hlPopup.getName() + ".offsetWidth+"
299: + _xvisibleoffset
300: + ") || (getMouseY()+document.body.scrollTop<"
301: + _hlPopup.getName() + ".offsetTop-"
302: + _yvisibleoffset
303: + " || getMouseY()+document.body.scrollTop>"
304: + _hlPopup.getName() + ".offsetTop+"
305: + _hlPopup.getName() + ".offsetHeight+"
306: + _yvisibleoffset + ")) {\n";
307: } else {
308: sScript += "if ((getMouseX()+document.body.scrollLeft<"
309: + _hlPopup.getName()
310: + ".offsetLeft-"
311: + _hcComp.getFullName()
312: + ".offsetWidth || getMouseX()+document.body.scrollLeft>"
313: + _hlPopup.getName()
314: + ".offsetLeft+"
315: + _hlPopup.getName()
316: + ".offsetWidth+"
317: + _hcComp.getFullName()
318: + ".offsetWidth) || (getMouseY()+document.body.scrollTop<"
319: + _hlPopup.getName()
320: + ".offsetTop-"
321: + _hcComp.getFullName()
322: + ".offsetHeight || getMouseY()+document.body.scrollTop>"
323: + _hlPopup.getName() + ".offsetTop+"
324: + _hlPopup.getName() + ".offsetHeight+"
325: + _hcComp.getFullName() + ".offsetHeight)) {\n";
326: sScript += sHideCondition + _hlPopup.getName()
327: + ".style.visibility='hidden';\n";
328: if (_hideScript != null)
329: sScript += _hideScript;
330: if (!sHideCondition.equals(""))
331: sScript += "}\n";
332: sScript += "}\n";
333: if (_offsetByWidth) {
334: sScript += "if (getMouseX()+document.body.scrollLeft<"
335: + _hlPopup.getName()
336: + ".offsetLeft && (getMouseY()+document.body.scrollTop<"
337: + _hlPopup.getName() + "getPixelTop("
338: + _hcComp.getFullName()
339: + (_offsetByParent ? ".offsetParent" : "")
340: + ") || getMouseY()+document.body.scrollTop>"
341: + _hlPopup.getName() + "getPixelTop("
342: + _hcComp.getFullName()
343: + (_offsetByParent ? ".offsetParent" : "")
344: + ")+" + _hcComp.getFullName()
345: + ".offsetHeight+" + (_cellpadding * 2)
346: + ")) {\n";
347: sScript += sHideCondition + _hlPopup.getName()
348: + ".style.visibility='hidden';\n";
349: if (_hideScript != null)
350: sScript += _hideScript;
351: if (!sHideCondition.equals(""))
352: sScript += "}\n";
353: sScript += "}\n";
354: }
355: if (_offsetByHeight) {
356: sScript += "if ((getMouseX()+document.body.scrollLeft<"
357: + _hlPopup.getName()
358: + "getPixelLeft("
359: + _hcComp.getFullName()
360: + (_offsetByParent ? ".offsetParent" : "")
361: + ") || getMouseX()+document.body.scrollLeft>"
362: + _hlPopup.getName()
363: + "getPixelLeft("
364: + _hcComp.getFullName()
365: + (_offsetByParent ? ".offsetParent" : "")
366: + ")+"
367: + _hcComp.getFullName()
368: + ".offsetWidth) && getMouseY()+document.body.scrollTop<"
369: + _hlPopup.getName() + ".offsetTop) {\n";
370: sScript += sHideCondition + _hlPopup.getName()
371: + ".style.visibility='hidden';\n";
372: if (_hideScript != null)
373: sScript += _hideScript;
374: if (!sHideCondition.equals(""))
375: sScript += "}\n";
376: sScript += "}\n";
377: }
378: }
379: sScript += "}\n";
380: sScript += "}\n";
381: sScript += "function " + _hlPopup.getName() + "ShowPopup() {\n";
382: if (!_fixedLocation) {
383: if (_hcComp == null) {
384: sScript += _hlPopup.getName()
385: + ".style.top=getMouseY()+document.body.scrollTop;";
386: sScript += _hlPopup.getName()
387: + ".style.left=getMouseX()+document.body.scrollLeft;";
388: } else {
389: sScript += _hlPopup.getName()
390: + ".style.top="
391: + _hlPopup.getName()
392: + "getPixelTop("
393: + _hcComp.getFullName()
394: + (_offsetByParent ? ".offsetParent" : "")
395: + ")"
396: + (_offsetByHeight ? "+"
397: + _hcComp.getFullName()
398: + ".offsetHeight" : "") + ";";
399: sScript += _hlPopup.getName()
400: + ".style.left="
401: + _hlPopup.getName()
402: + "getPixelLeft("
403: + _hcComp.getFullName()
404: + (_offsetByParent ? ".offsetParent" : "")
405: + ")"
406: + (_offsetByWidth ? "+" + _hcComp.getFullName()
407: + ".offsetWidth" : "") + ";";
408: }
409: } else {
410: sScript += _hlPopup.getName() + ".style.top=" + _y + ";";
411: sScript += _hlPopup.getName() + ".style.left=" + _x + ";";
412: }
413: sScript += _hlPopup.getName() + ".style.visibility='visible';";
414: if (_visibleScript != null)
415: sScript += _visibleScript;
416: sScript += "}\n";
417: return sScript;
418: }
419:
420: /**
421: * Returns whether the popup displays in a fixed location.
422: * Creation date: (3/27/01 1:56:08 PM)
423: * @return boolean Indicates whether the popup displays in a fixed location or not.
424: */
425: public boolean getFixedLocation() {
426: return _fixedLocation;
427: }
428:
429: /**
430: * Returns the Javascript condition under which the Popup hides.
431: * Creation date: (3/27/01 1:56:08 PM)
432: * @return String The Javascript representing the Condition under which the popup hides.
433: */
434: public String getHideCondition() {
435: return _hideCondition;
436: }
437:
438: /**
439: * Returns the Javascript used after hiding the Popup. Specified by user using setHideScript
440: * Creation date: (3/27/01 1:56:08 PM)
441: * @return String The Javascript used after hiding the popup.
442: */
443: public String getHideScript() {
444: return _hideScript;
445: }
446:
447: /**
448: * Returns the Javascript call to be made in javascript to decide to leaveup/hide popup.
449: * Creation date: (3/27/01 11:14:44 AM)
450: * @return java.lang.String The Javascript call used to leaveup/hide the popup.
451: */
452: public String getPopupScript() {
453: return _hlPopup.getName() + "Popup();";
454: }
455:
456: /**
457: * Returns the Javascript call to be made in javascript to show popup.
458: * Creation date: (3/27/01 11:14:44 AM)
459: * @return java.lang.String The Javascript call used to show the popup.
460: */
461: public String getPopupShowScript() {
462: return "setTimeout('" + _hlPopup.getName()
463: + "ShowPopup();',100);";
464: }
465:
466: /**
467: * Returns the Javascript used after showing the Popup. Specified by user with setVisibleScript.
468: * Creation date: (3/27/01 1:56:08 PM)
469: * @return String The Javascript used to show the popup.
470: */
471: public String getVisibleScript() {
472: return _visibleScript;
473: }
474:
475: /**
476: * Sets the cellpadding used in generating the scripts representing the cell padding of the table containing the popup.
477: * Creation date: (3/27/01 1:56:08 PM)
478: * @param iCellPadding The cellpadding of the table containing the popup.
479: */
480: public void setCellPadding(int iCellPadding) {
481: _cellpadding = iCellPadding;
482: }
483:
484: /**
485: * Sets whether the popup is in a fixed location or not.
486: * Creation date: (3/27/01 1:56:08 PM)
487: * @param bFixed boolean Indicates whether the popup is in a fixed location or not.
488: */
489: public void setFixedLocation(boolean bFixed) {
490: _fixedLocation = bFixed;
491: }
492:
493: /**
494: * Specifies the javascript condition to use to hide the popup.
495: * Creation date: (3/27/01 1:56:08 PM)
496: * @param sHideCondition string The condition used to hide the popup.
497: */
498: public void setHideCondition(String sHideConditon) {
499: _hideCondition = sHideConditon;
500: _hscPopup.setScript(getDisplayPopupScript());
501: }
502:
503: /**
504: * Specifies the javascript to be executed after hiding the popup.
505: * Creation date: (3/27/01 1:56:08 PM)
506: * @param sHideCondition string The javascipt to be executed after hiding popup.
507: */
508: public void setHideScript(String sHideScript) {
509: _hideScript = sHideScript;
510: _hscPopup.setScript(getDisplayPopupScript());
511: }
512:
513: /**
514: * Specifies the javascript to be executed after showing the popup.
515: * Creation date: (3/27/01 1:56:08 PM)
516: * @param sHideCondition string The javascipt to be executed after showing popup.
517: */
518: public void setVisibleScript(String sVisibleScript) {
519: _visibleScript = sVisibleScript;
520: _hscPopup.setScript(getDisplayPopupScript());
521: }
522: }
|