001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.testapp.interactive.testscreen;
031:
032: import java.awt.Graphics2D;
033: import java.awt.image.BufferedImage;
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.OutputStream;
037: import java.net.URL;
038:
039: import javax.swing.ImageIcon;
040:
041: import nextapp.echo2.app.ApplicationInstance;
042: import nextapp.echo2.app.AwtImageReference;
043: import nextapp.echo2.app.HttpImageReference;
044: import nextapp.echo2.app.Label;
045: import nextapp.echo2.app.ResourceImageReference;
046: import nextapp.echo2.app.StreamImageReference;
047: import nextapp.echo2.testapp.interactive.Styles;
048: import nextapp.echo2.testapp.interactive.TestGrid;
049:
050: /**
051: * An interactive test for <code>ImageReference</code>s.
052: */
053: public class ImageReferenceTest extends TestGrid {
054:
055: private static final String RESOURCE_IMAGE_LOCATION = Styles.IMAGE_PATH
056: + "Two.jpg";
057:
058: private static final int BUFFER_SIZE = 4096;
059:
060: // Static for memory conservation w/ live demos.
061: private static final AwtImageReference AWT_IMAGE_REFERENCE;
062: static {
063: URL resourceUrl = ImageReferenceTest.class
064: .getResource(RESOURCE_IMAGE_LOCATION);
065: ImageIcon imageIcon = new ImageIcon(resourceUrl);
066: BufferedImage image = new BufferedImage(85, 100,
067: BufferedImage.TYPE_INT_RGB);
068: Graphics2D graphics = (Graphics2D) image.getGraphics();
069: graphics.drawImage(imageIcon.getImage(), 0, 0, null);
070: graphics.setColor(java.awt.Color.BLUE);
071: graphics.drawString("Java2D", 5, 40);
072: AWT_IMAGE_REFERENCE = new AwtImageReference(image);
073: }
074:
075: private StreamImageReference streamImageReference = new StreamImageReference() {
076:
077: private String id = ApplicationInstance.generateSystemId();
078:
079: /**
080: * @see nextapp.echo2.app.StreamImageReference#getContentType()
081: */
082: public String getContentType() {
083: return "image/jpeg";
084: }
085:
086: /**
087: * @see nextapp.echo2.app.RenderIdSupport#getRenderId()
088: */
089: public String getRenderId() {
090: return id;
091: }
092:
093: /**
094: * @see nextapp.echo2.app.StreamImageReference#render(java.io.OutputStream)
095: */
096: public void render(OutputStream out) throws IOException {
097: InputStream in = null;
098: byte[] buffer = new byte[BUFFER_SIZE];
099: int bytesRead = 0;
100:
101: try {
102: in = ImageReferenceTest.class
103: .getResourceAsStream(RESOURCE_IMAGE_LOCATION);
104: do {
105: bytesRead = in.read(buffer);
106: if (bytesRead > 0) {
107: out.write(buffer, 0, bytesRead);
108: }
109: } while (bytesRead > 0);
110: } finally {
111: if (in != null) {
112: try {
113: in.close();
114: } catch (IOException ex) {
115: }
116: }
117: }
118: }
119: };
120:
121: public ImageReferenceTest() {
122: addHeaderRow("ImageReference Types");
123: HttpImageReference httpImageReference = new HttpImageReference(
124: "images/two.jpg");
125: ResourceImageReference resourceImageReference = new ResourceImageReference(
126: RESOURCE_IMAGE_LOCATION);
127: addTestRow("AwtImageReference", new Label(AWT_IMAGE_REFERENCE));
128: addTestRow("HttpImageReference", new Label(httpImageReference));
129: addTestRow("ResourceImageReference", new Label(
130: resourceImageReference));
131: addTestRow("StreamImageReference", new Label(
132: streamImageReference));
133: }
134: }
|