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.wml;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/wml/WmlImage.java $
024: //$Author: Srufle $
025: //$Revision: 6 $
026: //$Modtime: 4/15/03 2:25p $
027: /////////////////////////
028:
029: import java.io.PrintWriter;
030:
031: import com.salmonllc.html.HtmlComponent;
032: import com.salmonllc.html.HtmlPage;
033: import com.salmonllc.jsp.JspController;
034: import com.salmonllc.sql.*;
035: import com.salmonllc.util.Util;
036:
037: /**
038: * This type can be used to add an image to your page.
039: */
040: public class WmlImage extends HtmlComponent {
041: public static final String ALIGN_BOTTOM = "BOTTOM";
042: public static final String ALIGN_MIDDLE = "MIDDLE";
043: public static final String ALIGN_TOP = "TOP";
044: public static final String ALIGN_NONE = "";
045: //
046: public static final int SIZE_PIXELS = 0;
047: public static final int SIZE_PERCENT = 1;
048: //
049: private String _source = "";
050: private String _localsrc = "";
051: private String _alt = "";
052: private String _align = ALIGN_NONE;
053: private String _class;
054: //
055: private int _width = -1;
056: private int _height = -1;
057: private int _hSpace = -1;
058: private int _vSpace = -1;
059: //
060: private int _widthSizeOption = SIZE_PIXELS;
061: private int _heightSizeOption = SIZE_PIXELS;
062: //
063:
064: private DataStoreEvaluator _dsEval = null;
065: private DataStoreEvaluator _altEval = null;
066:
067: /**
068: * Constructs an new Image
069: * @param source - image that is being used.
070: * @param p The page the image will be placed in
071: */
072: public WmlImage(String source, HtmlPage p) {
073: this ("", source, p);
074: }
075:
076: /**
077: *Does the binding for the component. This method is called by the framework and should not be called directly
078: */
079:
080: public void doBinding() throws Exception {
081: String dataSource = getDataSource();
082: String dsName = null;
083: String dsExp = null;
084:
085: if (dataSource == null)
086: return;
087:
088: int pos = dataSource.indexOf(":");
089: if (pos == -1)
090: dsName = dataSource;
091: else {
092: dsName = dataSource.substring(0, pos);
093: dsExp = dataSource.substring(pos + 1);
094: }
095:
096: DataStoreBuffer ds = ((JspController) getPage())
097: .getDataSource(dsName);
098: if (ds == null)
099: return;
100:
101: if (!ds.getAutoBind())
102: return;
103:
104: int index = -1;
105: if (dsExp != null)
106: index = ds.getColumnIndex(dsExp);
107: if (index != -1) {
108: setExpression(ds, ds.getColumnName(index));
109: index = ds.getColumnIndex(getName() + "ALT");
110: if (index > -1)
111: setAltExpression(ds, ds.getColumnName(index));
112: }
113: }
114:
115: /**
116: * Constructs an image object for the page
117: */
118: public WmlImage(String name, String source, HtmlPage p) {
119: super (name, p);
120: _source = source;
121: }
122:
123: /**
124: *Generates the Html for the component. This method is called by the framework and should not be called directly
125: */
126: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
127: if (!getVisible()) {
128: return;
129: }
130:
131: if (_dsEval != null) {
132: if (rowNo > -1)
133: _source = _dsEval.evaluateRowFormat(rowNo);
134: else
135: _source = _dsEval.evaluateRowFormat();
136:
137: }
138:
139: String row = "";
140: if (rowNo != -1)
141: row = "_" + rowNo;
142:
143: String source = _source;
144:
145: //
146: StringBuffer out = new StringBuffer("<img");
147:
148: if (!Util.isNull(source) && !Util.isEmpty(source))
149: out.append(" src=\"" + source + "\"");
150:
151: // NAME
152: if (!Util.isNull(getName()) && !Util.isEmpty(getName())) {
153: out.append(" id=\"" + getName() + row + "\"");
154: }
155: // ALIGN
156: if (!Util.isNull(_class) && !Util.isEmpty(_class)) {
157: out.append(" class=\"" + _class + "\"");
158: }
159:
160: // ALT
161: String alt = _alt;
162: if (_altEval != null) {
163: if (rowNo > -1)
164: alt = _altEval.evaluateRowFormat(rowNo);
165: else
166: alt = _altEval.evaluateRowFormat();
167: }
168: if (!Util.isNull(alt) && !Util.isEmpty(alt)) {
169: out.append(" alt=\"" + alt + "\"");
170: }
171:
172: // ALIGN
173: if (!Util.isNull(_align) && !Util.isEmpty(_align)) {
174: out.append(" align=\"" + _align + "\"");
175: }
176:
177: // WIDTH
178: if (_width > -1) {
179: out.append(" width=\"" + _width
180: + (_widthSizeOption == SIZE_PERCENT ? "%" : "")
181: + "\"");
182: }
183:
184: // HEIGHT
185: if (_height > -1) {
186: out.append(" height=\"" + _height
187: + (_heightSizeOption == SIZE_PERCENT ? "%" : "")
188: + "\"");
189: }
190:
191: // HSPACE
192: if (_hSpace > -1) {
193: out.append(" hspace=\"" + _hSpace + "\"");
194: }
195: // VSPACE
196: if (_vSpace > -1) {
197: out.append(" vspace=\"" + _vSpace + "\"");
198: }
199:
200: //
201: out.append("/>");
202: p.print(out);
203: }
204:
205: /**
206: * Returns the alignment property for the image.
207: */
208: public String getAlign() {
209: return _align;
210: }
211:
212: /**
213: * Returns the alt for the image
214: */
215: public String getAlt() {
216: return _alt;
217: }
218:
219: /**
220: * This method returns the localsrc of the image.
221: */
222: public String getLocalSrc() {
223: return _localsrc;
224: }
225:
226: /**
227: * This method returns the height of the image.
228: */
229: public int getHeight() {
230: return _height;
231: }
232:
233: /**
234: * This method returns SIZE_PIXELS or SIZE_PERCENT.
235: */
236: public int getHeightSizeOption() {
237: return _heightSizeOption;
238: }
239:
240: /**
241: * This method returns the horizontal space around the the image.
242: */
243: public int getHorizontalSpace() {
244: return _hSpace;
245: }
246:
247: /**
248: * This method gets the source url for the image.
249: */
250: public String getSource() {
251: return _source;
252: }
253:
254: /**
255: * This method returns the vertical space around the the image.
256: */
257: public int getVerticalSpace() {
258: return _vSpace;
259: }
260:
261: /**
262: * This method returns the width of the image.
263: */
264: public int getWidth() {
265: return _width;
266: }
267:
268: /**
269: * This method returns SIZE_PIXELS or SIZE_PERCENT.
270: */
271: public int getWidthSizeOption() {
272: return _widthSizeOption;
273: }
274:
275: /**
276: * Use this method to get the class for the image tag
277: */
278: public String getClassName() {
279: return _class;
280: }
281:
282: /**
283: * Sets the alignment property for the image.
284: */
285:
286: public void setAlign(String align) {
287: _align = align;
288: }
289:
290: /**
291: * This method sets the alt for the image
292: */
293: public void setAlt(String alt) {
294: _alt = alt;
295: }
296:
297: /**
298: * Use this method to bind the alt property to an expression in a DataStore
299: * @param ds The DataStore to bind to.
300: * @param expression The expression to bind to.
301: * @see DataStoreEvaluator
302: */
303: public void setAltExpression(DataStoreBuffer ds, String expression)
304: throws Exception {
305: _altEval = new DataStoreEvaluator(ds, expression);
306: }
307:
308: /**
309: * This method sets the localsrc of the image.
310: */
311: public void setLocalSrc(String localsrc) {
312: _localsrc = localsrc;
313: }
314:
315: /**
316: * Use this method to bind this component to an expression in a DataStore
317: * @param ds The DataStore to bind to.
318: * @param expression The expression to bind to.
319: * @see DataStoreEvaluator
320: */
321: public void setExpression(DataStoreBuffer ds,
322: DataStoreExpression expression) throws Exception {
323:
324: _dsEval = new DataStoreEvaluator(ds, expression);
325: }
326:
327: /**
328: * Use this method to bind this component to an expression in a DataStoreBuffer. The resulting expression wil be formatted according to the pattern specified.
329: * @param ds The DataStore to bind to.
330: * @param expression The expression to bind to.
331: * @param format The patter to use to format the result
332: * @see DataStore#setFormat
333: * @see DataStoreEvaluator
334: */
335: public void setExpression(DataStoreBuffer ds,
336: DataStoreExpression expression, String format)
337: throws Exception {
338: _dsEval = new DataStoreEvaluator(ds, expression, format);
339: }
340:
341: /**
342: * Use this method to bind this component to an expression in a DataStore
343: * @param ds The DataStore to bind to.
344: * @param expression The expression to bind to.
345: * @see DataStoreEvaluator
346: */
347: public void setExpression(DataStoreBuffer ds, String expression)
348: throws Exception {
349: _dsEval = new DataStoreEvaluator(ds, expression);
350: }
351:
352: /**
353: * Use this method to bind this component to an expression in a DataStoreBuffer. The resulting expression wil be formatted according to the pattern specified.
354: * @param ds The DataStore to bind to.
355: * @param expression The expression to bind to.
356: * @param format The patter to use to format the result
357: * @see DataStore#setFormat
358: * @see DataStoreEvaluator
359: */
360: public void setExpression(DataStoreBuffer ds, String expression,
361: String format) throws Exception {
362: _dsEval = new DataStoreEvaluator(ds, expression, format);
363: }
364:
365: /**
366: * This method sets the height of the image.
367: */
368: public void setHeight(int height) {
369: _height = height;
370: }
371:
372: /**
373: * Valid values for this method are SIZE_PIXELS or SIZE_PERCENT.
374: */
375: public void setHeightSizeOption(int option) {
376: _heightSizeOption = option;
377: }
378:
379: /**
380: * This method sets the Horizontal space around the image in pixels.
381: */
382: public void setHorizontalSpace(int width) {
383: _hSpace = width;
384: }
385:
386: /**
387: * This method sets the source url for the image.
388: */
389: public void setSource(String source) {
390: _source = source;
391: }
392:
393: /**
394: * This method sets the Vertical space around the image in pixels.
395: */
396: public void setVerticalSpace(int width) {
397: _vSpace = width;
398: }
399:
400: /**
401: * This method sets the width of the image.
402: */
403: public void setWidth(int width) {
404: _width = width;
405: }
406:
407: /**
408: * Valid values for this method are SIZE_PIXELS or SIZE_PERCENT.
409: */
410: public void setWidthSizeOption(int option) {
411: _widthSizeOption = option;
412: }
413:
414: /**
415: * This method gets the DataStoreEvaluator being used for expressions.
416: * @return DataStoreEvaluator
417: * @see DataStoreEvaluator
418: */
419: public DataStoreEvaluator getExpression() {
420: return _dsEval;
421: }
422:
423: /**
424: * This method gets the DataStoreEvaluator being used for alt expressions.
425: * @return DataStoreEvaluator
426: * @see DataStoreEvaluator
427: */
428: public DataStoreEvaluator getAltExpression() {
429: return _altEval;
430: }
431:
432: /**
433: * This method sets the class for the link.
434: */
435: public void setClassName(String className) {
436: _class = className;
437: }
438:
439: }
|