001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.base.ui;
039:
040: import java.util.Hashtable;
041: import java.util.Iterator;
042:
043: import org.millstone.base.terminal.PaintTarget;
044: import org.millstone.base.terminal.PaintException;
045: import org.millstone.base.terminal.Resource;
046: import org.millstone.base.terminal.Sizeable;
047:
048: /** Component for embedding external objects.
049: *
050: * @author IT Mill Ltd.
051: * @version 3.1.1
052: * @since 3.0
053: */
054: public class Embedded extends AbstractComponent implements Sizeable {
055:
056: /** General object type */
057: public static final int TYPE_OBJECT = 0;
058:
059: /** Image types */
060: public static final int TYPE_IMAGE = 1;
061:
062: /** Type of the object */
063: private int type = TYPE_OBJECT;
064:
065: /** Source of the embedded object */
066: private Resource source = null;
067:
068: /** Dimensions of the object. */
069: private int width = -1;
070: private int height = -1;
071: private int widthUnits = Sizeable.UNITS_PIXELS;
072: private int heightUnits = Sizeable.UNITS_PIXELS;
073:
074: /** Generic object attributes */
075: private String mimeType = null;
076: private String standby = null;
077:
078: /** Hash of object parameteres. */
079: private Hashtable parameters = new Hashtable();
080:
081: /** Applet or other client side runnable properties. */
082: private String codebase = null;
083: private String codetype = null;
084: private String classId = null;
085: private String archive = null;
086:
087: /** Creates a new empty Embedded object.
088: */
089: public Embedded() {
090: }
091:
092: /** Creates a new empty Embedded object with caption.
093: */
094: public Embedded(String caption) {
095: setCaption(caption);
096: }
097:
098: /** Creates a new Embedded object whose contents is loaded from given resource.
099: * The dimensions are assumed if possible. The type is guessed from resource.
100: */
101: public Embedded(String caption, Resource source) {
102: setCaption(caption);
103: setSource(source);
104: }
105:
106: /** Get component UIDL tag.
107: * @return Component UIDL tag as string.
108: */
109: public String getTag() {
110: return "embedded";
111: }
112:
113: /** Invoked when the component state should be painted */
114: public void paintContent(PaintTarget target) throws PaintException {
115:
116: if (type == TYPE_IMAGE) {
117: target.addAttribute("type", "image");
118: }
119:
120: if (source != null)
121: target.addAttribute("src", source);
122:
123: // Dimensions
124: if (width > 0)
125: target.addAttribute("width", "" + width
126: + Sizeable.UNIT_SYMBOLS[this .widthUnits]);
127: if (height > 0)
128: target.addAttribute("height", "" + height
129: + Sizeable.UNIT_SYMBOLS[this .heightUnits]);
130: if (mimeType != null && !"".equals(mimeType))
131: target.addAttribute("mimetype", mimeType);
132: if (classId != null && !"".equals(classId))
133: target.addAttribute("classid", classId);
134: if (codebase != null && !"".equals(codebase))
135: target.addAttribute("codebase", codebase);
136: if (codetype != null && !"".equals(codetype))
137: target.addAttribute("codetype", codetype);
138: if (standby != null && !"".equals(standby))
139: target.addAttribute("standby", standby);
140: if (archive != null && !"".equals(archive))
141: target.addAttribute("archive", archive);
142:
143: // Params
144: for (Iterator i = this .getParameterNames(); i.hasNext();) {
145: target.startTag("embeddedparam");
146: String key = (String) i.next();
147: target.addAttribute("name", key);
148: target.addAttribute("value", (String) getParameter(key));
149: target.endTag("embeddedparam");
150: }
151: }
152:
153: /** Set an object parameter.
154: * Parameters are optional information, and they are passed to the
155: * instantiated object. Parameters are are stored as name value pairs.
156: * This overrides the previous value assigned to this parameter.
157: * @param name - The name of the parameter.
158: * @param value - The value of the parameter.
159: */
160: public void setParameter(String name, String value) {
161: parameters.put(name, value);
162: requestRepaint();
163: }
164:
165: /** Get the value of an object parameter.
166: * Parameters are optional information, and they are passed to the
167: * instantiated object. Parameters are are stored as name value pairs.
168: * @return Value of parameter or null if not found.
169: */
170: public String getParameter(String name) {
171: return (String) parameters.get(name);
172: }
173:
174: /** Remove an object parameter from the list.
175: * @param name - The name of the parameter to remove.
176: */
177: public void removeParameter(String name) {
178: parameters.remove(name);
179: requestRepaint();
180: }
181:
182: /** Get embedded object parameter names.
183: * @return Iterator of parameters names.
184: */
185: public Iterator getParameterNames() {
186: return parameters.keySet().iterator();
187: }
188:
189: /**
190: * Returns the codebase, the root-path used to access resources with relative paths.
191: * @return String
192: */
193: public String getCodebase() {
194: return codebase;
195: }
196:
197: /**
198: * Returns the MIME-Type of the code.
199: * @return String
200: */
201: public String getCodetype() {
202: return codetype;
203: }
204:
205: /**
206: * Returns the MIME-Type of the object
207: * @return String
208: */
209: public String getMimeType() {
210: return mimeType;
211: }
212:
213: /**
214: * Returns the standby text displayed when
215: * the object is loading.
216: * @return String
217: */
218: public String getStandby() {
219: return standby;
220: }
221:
222: /**
223: * Sets the codebase, the root-path used to access resources with relative paths.
224: * @param codebase The codebase to set
225: */
226: public void setCodebase(String codebase) {
227: if (codebase != this .codebase
228: || (codebase != null && !codebase.equals(this .codebase))) {
229: this .codebase = codebase;
230: requestRepaint();
231: }
232: }
233:
234: /**
235: * Sets the codetype, the MIME-Type of the code.
236: * @param codetype The codetype to set
237: */
238: public void setCodetype(String codetype) {
239: if (codetype != this .codetype
240: || (codetype != null && !codetype.equals(this .codetype))) {
241: this .codetype = codetype;
242: requestRepaint();
243: }
244: }
245:
246: /**
247: * Sets the mimeType, the MIME-Type of the object.
248: * @param mimeType The mimeType to set
249: */
250: public void setMimeType(String mimeType) {
251: if (mimeType != this .mimeType
252: || (mimeType != null && !mimeType.equals(this .mimeType))) {
253: this .mimeType = mimeType;
254: requestRepaint();
255: }
256: }
257:
258: /**
259: * Sets the standby, the text to display while loading the object.
260: * @param standby The standby to set
261: */
262: public void setStandby(String standby) {
263: if (standby != this .standby
264: || (standby != null && !standby.equals(this .standby))) {
265: this .standby = standby;
266: requestRepaint();
267: }
268: }
269:
270: /**
271: * Returns the visual height of the object.
272: * Default height is -1, which is interpreted as "unspecified".
273: * @return The height in units specified by heightUnits property.
274: */
275: public int getHeight() {
276: return height;
277: }
278:
279: /**
280: * Returns the visual width of the object.
281: * Default width is -1, which is interpreted as "unspecified".
282: * @return The width in units specified by widthUnits property.
283: */
284: public int getWidth() {
285: return width;
286: }
287:
288: /** Sets the visual height of the object.
289: * Default height is -1, which is interpreted as "unspecified".
290: * @param height The height in units specified by heightUnits property.
291: */
292: public void setHeight(int height) {
293: if (this .height != height) {
294: this .height = height;
295: requestRepaint();
296: }
297: }
298:
299: /** Sets the visual width of the object.
300: * Default width is -1, which is interpreted as "unspecified".
301: * @param width The width in units specified by widthUnits property.
302: */
303: public void setWidth(int width) {
304: if (this .width != width) {
305: this .width = width;
306: requestRepaint();
307: }
308: }
309:
310: /**
311: * Returns the classId attribute.
312: * @return String
313: */
314: public String getClassId() {
315: return classId;
316: }
317:
318: /**
319: * Sets the classId attribute.
320: * @param classId The classId to set
321: */
322: public void setClassId(String classId) {
323: if (classId != this .classId
324: || (classId != null && !classId.equals(classId))) {
325: this .classId = classId;
326: requestRepaint();
327: }
328: }
329:
330: /** Get the resource contained in the embedded object.
331: * @return Resource
332: */
333: public Resource getSource() {
334: return source;
335: }
336:
337: /** Get the type of the embedded object.
338: * <p>This can be one of the following:<ul>
339: * <li>TYPE_OBJECT <i>(This is the default)</i>
340: * <li>TYPE_IMAGE
341: * </ul>
342: * </p>
343: * @return int
344: */
345: public int getType() {
346: return type;
347: }
348:
349: /** Set the object source resource.
350: * The dimensions are assumed if possible.
351: * The type is guessed from resource.
352: * @param source The source to set
353: */
354: public void setSource(Resource source) {
355: if (source != null && !source.equals(this .source)) {
356: this .source = source;
357: String mt = source.getMIMEType();
358: if ((mt.substring(0, mt.indexOf("/"))
359: .equalsIgnoreCase("image"))) {
360: type = TYPE_IMAGE;
361: } else {
362: type = TYPE_OBJECT;
363: }
364: }
365: }
366:
367: /** Sets the object type.
368: * <p>This can be one of the following:<ul>
369: * <li>TYPE_OBJECT <i>(This is the default)</i>
370: * <li>TYPE_IMAGE
371: * </ul>
372: * </p>
373: * @param type The type to set
374: */
375: public void setType(int type) {
376: if (type != TYPE_OBJECT && type != TYPE_IMAGE)
377: throw new IllegalArgumentException("Unsupported type");
378: if (type != this .type) {
379: this .type = type;
380: requestRepaint();
381: }
382: }
383:
384: /**
385: * Returns the archive attribute.
386: * @return String
387: */
388: public String getArchive() {
389: return archive;
390: }
391:
392: /**
393: * Sets the archive attribute.
394: * @param archive The archive string to set
395: */
396: public void setArchive(String archive) {
397: if (archive != this .archive
398: || (archive != null && !archive.equals(this .archive))) {
399: this .archive = archive;
400: requestRepaint();
401: }
402: }
403:
404: /**Get height property units.
405: * Default units are <code>Sizeable.UNITS_PIXELS</code>.
406: * @see org.millstone.base.terminal.Sizeable#getHeightUnits()
407: */
408: public int getHeightUnits() {
409: return this .heightUnits;
410: }
411:
412: /**Get width property units.
413: * Default units are <code>Sizeable.UNITS_PIXELS</code>.
414: * @see org.millstone.base.terminal.Sizeable#getWidthUnits()
415: */
416: public int getWidthUnits() {
417: return this .widthUnits;
418: }
419:
420: /**Set height property units.
421: * @see org.millstone.base.terminal.Sizeable#setHeightUnits(int)
422: */
423: public void setHeightUnits(int units) {
424: if (units >= 0 && units <= Sizeable.UNITS_PERCENTAGE
425: && this .heightUnits != units) {
426: this .heightUnits = units;
427: requestRepaint();
428: }
429: }
430:
431: /**Set width property units.
432: * @see org.millstone.base.terminal.Sizeable#setWidthUnits(int)
433: */
434: public void setWidthUnits(int units) {
435: if (units >= 0 && units <= Sizeable.UNITS_PERCENTAGE
436: && this.widthUnits != units) {
437: this.widthUnits = units;
438: requestRepaint();
439: }
440: }
441:
442: }
|