1 11 package org.eclipse.swt.graphics; 12 13 14 import java.io.*; 15 import java.util.Vector ; 16 import org.eclipse.swt.*; 17 import org.eclipse.swt.internal.Compatibility; 18 import org.eclipse.swt.internal.image.*; 19 20 42 43 public class ImageLoader { 44 45 50 public ImageData[] data; 51 52 57 public int logicalScreenWidth; 58 59 64 public int logicalScreenHeight; 65 66 72 public int backgroundPixel; 73 74 80 public int repeatCount; 81 82 85 Vector imageLoaderListeners; 86 87 90 public ImageLoader() { 91 reset(); 92 } 93 94 98 void reset() { 99 data = null; 100 logicalScreenWidth = 0; 101 logicalScreenHeight = 0; 102 backgroundPixel = -1; 103 repeatCount = 1; 104 } 105 106 124 public ImageData[] load(InputStream stream) { 125 if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 126 reset(); 127 data = FileFormat.load(stream, this); 128 return data; 129 } 130 131 149 public ImageData[] load(String filename) { 150 if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 151 InputStream stream = null; 152 try { 153 stream = Compatibility.newFileInputStream(filename); 154 return load(stream); 155 } catch (IOException e) { 156 SWT.error(SWT.ERROR_IO, e); 157 } finally { 158 try { 159 if (stream != null) stream.close(); 160 } catch (IOException e) { 161 } 163 } 164 return null; 165 } 166 167 197 public void save(OutputStream stream, int format) { 198 if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 199 FileFormat.save(stream, format, this); 200 } 201 202 232 public void save(String filename, int format) { 233 if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 234 OutputStream stream = null; 235 try { 236 stream = Compatibility.newFileOutputStream(filename); 237 } catch (IOException e) { 238 SWT.error(SWT.ERROR_IO, e); 239 } 240 save(stream, format); 241 try { 242 stream.close(); 243 } catch (IOException e) { 244 } 245 } 246 247 266 public void addImageLoaderListener(ImageLoaderListener listener) { 267 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 268 if (imageLoaderListeners == null) { 269 imageLoaderListeners = new Vector (); 270 } 271 imageLoaderListeners.addElement(listener); 272 } 273 274 286 public void removeImageLoaderListener(ImageLoaderListener listener) { 287 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 288 if (imageLoaderListeners == null) return; 289 imageLoaderListeners.removeElement(listener); 290 } 291 292 301 public boolean hasListeners() { 302 return imageLoaderListeners != null && imageLoaderListeners.size() > 0; 303 } 304 305 311 public void notifyListeners(ImageLoaderEvent event) { 312 if (!hasListeners()) return; 313 int size = imageLoaderListeners.size(); 314 for (int i = 0; i < size; i++) { 315 ImageLoaderListener listener = (ImageLoaderListener) imageLoaderListeners.elementAt(i); 316 listener.imageDataLoaded(event); 317 } 318 } 319 320 } 321 | Popular Tags |