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 com.salmonllc.sql.*;
023: import com.salmonllc.html.events.*;
024: import com.salmonllc.jsp.JspController;
025:
026: import java.util.*;
027:
028: /**
029: * This component is used to transform an HtmlImage object into an Html Image Map. An image map is like an image except that clicking or a particular area in the map will cause a link to be followed or a submit to be executed.
030: * @see HtmlImage
031: */
032:
033: public class HtmlImageMapArea extends HtmlComponent {
034: public static final int AREA_CIRCLE = 0;
035: public static final int AREA_RECTANGLE = 1;
036:
037: private int _shape = 0;
038: private HtmlImage _img;
039: private int _x1, _y1, _x2, _y2, _radius;
040: private String _href, _target, _onMouseOver, _onMouseOut;
041:
042: private Vector _listeners;
043: private DataStoreEvaluator _dsEval;
044: private boolean _clicked = false;
045: private int _rowNo = 0;
046: private boolean _submitAlreadyGenerated = false;
047:
048: /**
049: * Use this constructor to create a circular shaped image map area.
050: * @param name The name of the component
051: * @param centerX The x position of the center of the area
052: * @param centerY The y position of the center of the area
053: * @param radius Radius of the circle
054: */
055: public HtmlImageMapArea(String name, int centerX, int centerY,
056: int radius) {
057: super (name, null);
058: _x1 = centerX;
059: _y1 = centerY;
060: _radius = radius;
061: _shape = AREA_RECTANGLE;
062: }
063:
064: /**
065: * Use this constructor to create a rectangular shaped image map area.
066: * @param name The name of the component
067: * @param x1 Top left corner x position of the area
068: * @param y1 Top left corner y position of the area
069: * @param x2 Bottom right corner x position of the area
070: * @param y2 Bottom right corner y position of the area
071: */
072: public HtmlImageMapArea(String name, int x1, int y1, int x2, int y2) {
073: super (name, null);
074: _x1 = x1;
075: _y1 = y1;
076: _y2 = y2;
077: _x2 = x2;
078: _shape = AREA_RECTANGLE;
079: }
080:
081: /**
082: * Use this constructor to create a rectangular shaped image map area.
083: * @param name The name of the component
084: * @param x1 Top left corner x position of the area
085: * @param y1 Top left corner y position of the area
086: * @param x2 Bottom right corner x position of the area
087: * @param y2 Bottom right corner y position of the area
088: * @param href The url of the page to open
089: */
090: public HtmlImageMapArea(String name, int x1, int y1, int x2,
091: int y2, String href) {
092: this (name, x1, y1, x2, y2);
093: _href = href;
094: }
095:
096: /**
097: * Use this constructor to create a circular shaped image map area.
098: * @param name The name of the component
099: * @param centerX The x position of the center of the area
100: * @param centerY The y position of the center of the area
101: * @param radius Radius of the circle
102: * @param href The url of the page to open
103: */
104: public HtmlImageMapArea(String name, int centerX, int centerY,
105: int radius, String href) {
106: this (name, centerX, centerY, radius);
107: _href = href;
108: }
109:
110: /**
111: * This method will clear all pending events from the event queue for this component and components it contains.
112: */
113: public void reset() {
114: super .reset();
115: _submitAlreadyGenerated = false;
116: }
117:
118: /**
119: * This method adds a listener the will be notified when this button causes the page to be submitted.
120: * @param l The listener to add.
121: */
122: public void addSubmitListener(SubmitListener l) {
123: if (_listeners == null)
124: _listeners = new Vector();
125:
126: for (int i = 0; i < _listeners.size(); i++) {
127: if (((SubmitListener) _listeners.elementAt(i)) == l)
128: return;
129: }
130:
131: _listeners.addElement(l);
132: }
133:
134: public boolean executeEvent(int eventType) throws Exception {
135: if (eventType == EVENT_SUBMIT && _clicked) {
136: if (_listeners != null && _listeners.size() > 0) {
137: SubmitEvent e = new SubmitEvent(getPage(), this ,
138: getName(), getFullName(), _rowNo);
139: for (int i = 0; i < _listeners.size(); i++) {
140: SubmitListener l = (SubmitListener) _listeners
141: .elementAt(i);
142: e
143: .setNextListener(_listeners.size() > (i + 1) ? _listeners
144: .elementAt(i + 1)
145: : null);
146: if (!l.submitPerformed(e))
147: return false;
148: }
149: }
150: }
151: return true;
152: }
153:
154: public void generateHTML(java.io.PrintWriter p, int rowNo)
155: throws Exception {
156: StringBuffer href = new StringBuffer("");
157: StringBuffer out = new StringBuffer();
158:
159: if (getPage() instanceof JspController
160: && !_submitAlreadyGenerated)
161: generateInitialHTML(p);
162: if (_listeners != null && _listeners.size() > 0) {
163: href.append("javascript:" + getFullName() + "_submit("
164: + rowNo + ");");
165: } else {
166: if (_dsEval != null)
167: if (rowNo > -1)
168: _href = _dsEval.evaluateRowFormat(rowNo);
169: else
170: _href = _dsEval.evaluateRowFormat();
171:
172: if (_href != null) {
173: href = new StringBuffer(_href);
174: for (int i = 0; i < href.length(); i++)
175: if (href.charAt(i) == ' ')
176: href.setCharAt(i, '+');
177: }
178: }
179:
180: out.append("<AREA ");
181: if (_shape == AREA_RECTANGLE) {
182: out.append("COORDS=\"" + _x1 + "," + _y1 + "," + _x2 + ","
183: + _y2 + "\" ");
184: out.append("SHAPE=\"RECT\" ");
185: } else {
186: out.append("COORDS=\"" + _x1 + "," + _y1 + "," + _radius
187: + "\" ");
188: out.append("SHAPE=\"CIRCLE\" ");
189: }
190:
191: if (href.length() == 0)
192: out.append("NOHREF ");
193: else
194: out.append("HREF=\"" + href + "\" ");
195:
196: if (_target != null)
197: out.append("TARGET=\"" + _target + "\" ");
198:
199: if (_onMouseOver != null)
200: out.append("ONMOUSEOVER=\"" + _onMouseOver + "\" ");
201:
202: if (_onMouseOut != null)
203: out.append("ONMOUSEOUT=\"" + _onMouseOut + "\" ");
204:
205: out.append("NAME=\"" + getFullName() + "\">");
206:
207: p.print(out.toString());
208: }
209:
210: public void generateInitialHTML(java.io.PrintWriter p)
211: throws Exception {
212: if (_listeners != null && _listeners.size() > 0) {
213: p.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + getFullName()
214: + "__HIDDEN_FIELD\" VALUE=\"\">");
215: getPage().writeScript(
216: getFormString() + getFullName()
217: + "__HIDDEN_FIELD.value = '';");
218:
219: p.println("<SCRIPT>");
220: p.println("function " + getFullName() + "_submit(row) {");
221: p.println(getFormString() + getFullName()
222: + "__HIDDEN_FIELD.value = row;");
223: p.println(getFormString() + "submit();");
224: p.println("}");
225: p.println("</SCRIPT>");
226: _submitAlreadyGenerated = true;
227: }
228:
229: }
230:
231: /**
232: * This gets the Href for the area. The link will be followed when the user clicks on the area.
233: */
234: public String getHref() {
235: return _href;
236: }
237:
238: /**
239: * This method gets the javascript that will be executed when the mouse leaves the area.
240: */
241: public String getOnMouseOut() {
242: return _onMouseOut;
243: }
244:
245: /**
246: * This method gets the javascript that will be executed when the mouse passes over the area.
247: */
248: public String getOnMouseOver() {
249: return _onMouseOver;
250: }
251:
252: /**
253: * This gets the target for the area. It specifies which frame will be used for the linked page.
254: */
255: public String getTarget() {
256: return _target;
257: }
258:
259: public boolean processParms(Hashtable parms, int rowNo)
260: throws Exception {
261: String name = getFullName() + "__HIDDEN_FIELD";
262: String[] p = (String[]) parms.get(name);
263:
264: if (p != null && p[0].trim().length() > 0) {
265: _rowNo = Integer.parseInt(p[0].trim());
266: _clicked = true;
267: return true;
268: }
269:
270: return false;
271:
272: }
273:
274: /**
275: * This method removes a listener from the list that will be notified if this button causes the page to be submitted.
276: * @param l The listener to remove.
277: */
278: public void removeSubmitListener(SubmitListener l) {
279: if (_listeners == null)
280: return;
281:
282: for (int i = 0; i < _listeners.size(); i++) {
283: if (((SubmitListener) _listeners.elementAt(i)) == l) {
284: _listeners.removeElementAt(i);
285: return;
286: }
287: }
288: }
289:
290: /**
291: * Sets the coordinates of a circular area of the image
292: * @param centerX The x position of the center of the area
293: * @param centerY The y position of the center of the area
294: * @param radius Radius of the circle
295: */
296: public void setCircularArea(int centerX, int centerY, int radius) {
297: _x1 = centerX;
298: _y1 = centerY;
299: _radius = radius;
300: _shape = AREA_CIRCLE;
301: }
302:
303: /**
304: * This sets the Href for the area. The link will be followed when the user clicks on the area.
305: */
306: public void setHref(String hRef) {
307: _href = hRef;
308: ;
309: }
310:
311: /**
312: * This method creates an Href on the fly from the expression.
313: * @param ds com.salmonllc.sql.DataStoreBuffer
314: * @param expression java.lang.String
315: * @exception java.lang.Exception The exception description.
316: */
317: public void setHrefExpression(DataStoreBuffer ds, String expression)
318: throws Exception {
319: _dsEval = new DataStoreEvaluator(ds, expression);
320: }
321:
322: /**
323: * Sets the image that this map will use
324: * @param img com.salmonllc.html.HtmlImage
325: */
326: public void setImage(HtmlImage img) {
327: _img = img;
328: setParent(_img);
329: setPage(_img.getPage());
330: }
331:
332: /**
333: * This method sets the javascript that will be executed when the mouse leaves the area.
334: */
335: public void setOnMouseOut(String javaScript) {
336: _onMouseOut = javaScript;
337: }
338:
339: /**
340: * This method sets the javascript that will be executed when the mouse passes over the area.
341: */
342: public void setOnMouseOver(String javaScript) {
343: _onMouseOver = javaScript;
344: }
345:
346: /**
347: * Sets the coordinates of a rectangular area of the image
348: * @param x1 Top left corner x position of the area
349: * @param y1 Top left corner y position of the area
350: * @param x2 Bottom right corner x position of the area
351: * @param y2 Bottom right corner y position of the area
352: */
353: public void setRectangularArea(int x1, int y1, int x2, int y2) {
354: _x1 = x1;
355: _y1 = y1;
356: _x2 = x2;
357: _y2 = y2;
358: _shape = AREA_RECTANGLE;
359: }
360:
361: /**
362: * This sets the target for the area. It specifies which frame will be used for the linked page.
363: */
364: public void setTarget(String target) {
365: _target = target;
366: }
367: }
|