001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb;
022:
023: import java.awt.*;
024: import java.awt.image.*;
025: import java.io.*;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.Map;
029: import java.util.logging.Logger;
030: import javax.imageio.ImageIO;
031: import javax.swing.RepaintManager;
032: import org.onemind.awtbridge.BridgeContext;
033: import org.onemind.awtbridge.BridgeToolkit;
034: import org.onemind.commons.java.lang.ConfigurationException;
035: import org.onemind.swingweb.awt.SwingWebRepaintManager;
036: import org.onemind.swingweb.image.DummyImageProducer;
037: import org.onemind.swingweb.image.EmptyImage;
038: import org.onemind.swingweb.resource.*;
039: import org.onemind.swingweb.session.SwingWebSession;
040:
041: /**
042: * SwingWeb context
043: * @author TiongHiang Lee (thlee@onemindsoft.org)
044: *
045: */
046: public class SwingWebContext extends BridgeContext {
047:
048: /** the logger * */
049: private static final Logger _logger = Logger
050: .getLogger(SwingWebContext.class.getName());
051:
052: private static final SwingWebRepaintManager DEFAULT_REPAINT_MANGER = new SwingWebRepaintManager();
053:
054: /** the environments * */
055: private Map _environment;
056:
057: /** the url prefix **/
058: private String _urlPrefix = "";
059:
060: /** the resource cache **/
061: private ResourceCache _resCache = new ResourceCache();
062:
063: private boolean _useSessionCache = false;
064:
065: /**
066: * Constructor a thinkient tcontext with given environment
067: * @param tk the toolkit
068: * @throws ConfigurationException if there's render config problem
069: */
070: public SwingWebContext() {
071: super ((BridgeToolkit) Toolkit.getDefaultToolkit());
072: RepaintManager.setCurrentManager(DEFAULT_REPAINT_MANGER);
073: }
074:
075: /**
076: * Set the environment
077: * @param prop the environment
078: */
079: public final void setEnvironment(Map prop) {
080: _environment = prop;
081: }
082:
083: /**
084: * Get the environments
085: * @return the environments
086: */
087: public final Map getEnvironment() {
088: return _environment;
089: }
090:
091: /**
092: * {@inheritDoc}
093: */
094: public int checkImage(Image image, int width, int height,
095: ImageObserver observer) {
096: return ImageObserver.ALLBITS; //always done
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public Image createImage(byte[] imagedata, int imageoffset,
103: int imagelength) {
104: //TODO: improve this
105: try {
106: return ImageIO.read(new ByteArrayInputStream(imagedata,
107: imageoffset, imagelength));
108: } catch (Exception e) {
109: _logger.throwing(getClass().getName(), "createImage", e);
110: return new EmptyImage(getGraphics());
111: //return new BufferedByteImage(getGraphics(), imagedata, imageoffset, imagelength);//_logger.error(e.getMessage(), e);
112: }
113: }
114:
115: /**
116: * Register a resource
117: * @param image the image
118: * @return the URI for the resource
119: */
120: public String registerImageAsResource(Image image) {
121: String uri = "#";
122: /*
123: if (image instanceof URLImage)
124: {
125: URLImage img = (URLImage) image;
126: uri = registerResource(new URLResource("image", img.getURL()));
127: } else if (image instanceof BufferedByteImage)
128: {
129: BufferedByteImage img = (BufferedByteImage) image;
130: uri = registerResource(new BufferedByteResource("image", img.getBytes()));
131: } else
132: */
133: if (image instanceof BufferedImage) {
134: uri = registerResource(new BufferedImageResource(image
135: .toString(), "image", (BufferedImage) image));
136: }
137: return uri;
138: }
139:
140: private ResourceCache getResourceCache() {
141: if (_useSessionCache) {
142: return ((SwingWebSession) getSession()).getResourceCache();
143: } else {
144: return _resCache;
145: }
146: }
147:
148: /**
149: * Register the url
150: * @param image the image
151: * @return the uri
152: */
153: public String registerResource(Resource res) {
154: String id = _resCache.cacheResource(res);
155: return _urlPrefix + "?swresource=" + id;
156: }
157:
158: /**
159: * Get the URL object identified by uri
160: * @param uri the uri
161: * @return the URL object
162: */
163: public Resource getResource(String uri) {
164: return (Resource) _resCache.getResource(uri);
165: }
166:
167: /**
168: * {@inheritDoc}
169: */
170: public Image createImage(ImageProducer producer) {
171: if (producer instanceof DummyImageProducer) {
172: return ((DummyImageProducer) producer).getImage();
173: } else {
174: _logger.warning("Cannot create image with producer "
175: + producer + ". Use empty image.");
176: return new EmptyImage(getGraphics());
177: }
178: }
179:
180: /**
181: * {@inheritDoc}
182: */
183: public Image createImage(String filename) {
184: File f = new File(filename);
185: if (f.exists()) {
186: try {
187: return createImage(f.toURL());
188: } catch (MalformedURLException e) {
189: _logger
190: .throwing(getClass().getName(), "createImage",
191: e);
192: }
193: }
194: _logger.warning("Cannot create image with filename " + filename
195: + ". Use empty image.");
196: return new EmptyImage(getGraphics());
197: }
198:
199: /**
200: * {@inheritDoc}
201: */
202: public Image createImage(URL url) {
203: try {
204: String resId = "image:" + url.toString();
205: Resource res = getResourceCache().getResourceByKey(resId);
206: if (res != null) {
207: return (Image) res.getResourceObject();
208: } else {
209: BufferedImage buffImg = ImageIO.read(url.openStream());
210: if (buffImg != null) {
211: registerResource(new BufferedImageResource(resId,
212: "image", buffImg));
213: }
214: return buffImg;
215: }
216: } catch (IOException e) {
217: e.printStackTrace();
218: return new EmptyImage(getGraphics());
219: //return new URLImage(getGraphics(), url);
220: }
221:
222: }
223:
224: /**
225: * {@inheritDoc}
226: */
227: public Image getImage(String filename) {
228: return createImage(filename);
229: }
230:
231: /**
232: * {@inheritDoc}
233: */
234: public Image getImage(URL url) {
235: return createImage(url);
236: }
237:
238: /**
239: * {@inheritDoc}
240: */
241: public int getScreenResolution() throws HeadlessException {
242: // TODO: TBI
243: return 0;
244: }
245:
246: /**
247: * {@inheritDoc}
248: */
249: public Dimension getScreenSize() throws HeadlessException {
250: // TODO: TBI
251: return new Dimension(1024, 768);
252: }
253:
254: /**
255: * {@inheritDoc}
256: */
257: public boolean prepareImage(Image image, int width, int height,
258: ImageObserver observer) {
259: //TODO: implement scaling
260: //always true
261: return true;
262: }
263: }
|