/[alph]/alph/org/nongnu/alph/impl/SimpleImageScroll.java
ViewVC logotype

Diff of /alph/org/nongnu/alph/impl/SimpleImageScroll.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by tjl, Sat Apr 12 06:29:53 2003 UTC revision 1.3 by tjl, Sun Apr 20 08:50:02 2003 UTC
# Line 38  import java.awt.image.*; Line 38  import java.awt.image.*;
38  /** An image scrollblock  /** An image scrollblock
39   */   */
40    
41  public class SimpleImageScroll  public class SimpleImageScroll extends AbstractScrollBlock {
        implements ScrollBlockManager.MediaserverScrollBlock {  
42  String rcsid = "$Id$";  String rcsid = "$Id$";
43    
44      final static String DIR="../mstmpimg/"; // XXX Platform dependent!      int width=-1, height=-1;
   
     Image im;  
     int width, height;  
   
     StormPool ms;  
     BlockId id;  
   
     public SimpleImageScroll(BlockId id, Image im, int width, int height) {  
         this.id = id;  
         this.im = im;  
         this.width = width;  
         this.height = height;  
     }  
   
     public SimpleImageScroll(Image im, int width, int height) {  
         this( null, im, width, height );  
     }  
45    
46      public SimpleImageScroll(StormPool ms,  BlockId id) {      public SimpleImageScroll(StormPool ms,  BlockId id) {
47          this.ms = ms;          super(ms, id);
48          this.id = id;          try {
49                ms.request(id, null);
50            } catch(IOException e) {
51                throw new Error("Couldn't load image block");
52            }
53      }      }
54    
     public String getID() { return id.toString(); }  
     public BlockId saveOrGetId(StormPool ms) { return id; }  
   
55      public boolean equals(Object o) {      public boolean equals(Object o) {
56          if(!(o instanceof ScrollBlock)) return false;          if(!(o instanceof ScrollBlock)) return false;
57          ScrollBlock sb = (ScrollBlock)o;          ScrollBlock sb = (ScrollBlock)o;
# Line 78  String rcsid = "$Id$"; Line 61  String rcsid = "$Id$";
61          return getID().hashCode();          return getID().hashCode();
62      }      }
63    
64      public String imageFilename() {      private void loadImageSize() {
         return new String(DIR+getID());  
     }  
   
     private void loadImage() {  
         // Background.getDefaultInstance().addTask(  
65                    
66          Runnable r = (  new Runnable() {          String ct = msid.getContentType();
67              public void run() {  
68                  Block block ;          if(!ct.substring(0,ct.indexOf('/')).equals("image"))
69                  String ct;              throw new Error("Block isn't an image");
70                  try {  
71                      block = ms.request(id, null);          Block block ;
72                      ct = id.getContentType();          Dimension d;
73                  } catch(IOException e) {          try {
74                      throw new Error("Couldn't load image block");              block = ms.get(msid);
75                  }              java.io.InputStream is = block.getInputStream();
76                d = org.nongnu.navidoc.util.ImageSize.readSize(is);
77                  if(!ct.substring(0,ct.indexOf('/')).equals("image"))              if(d == null)
78                      throw new Error("Block isn't an image");                  throw new Error("Couldn't get size of image block");
79                is.close();
80                  Image img = null;  
81                  try {          } catch(Exception e) {
82                      img = java.awt.Toolkit.getDefaultToolkit().createImage(              throw new Error("Couldn't load image block");
83                          SlurpStream.slurp(block.getInputStream()));          }
84                  } catch (IOException _) {}  
85                  if(img == null)          this.width = d.width;
86                      throw new Error("Unknown image type");          this.height = d.height;
                 int count = 0;  
                 while(img.getWidth(null) < 0 || img.getHeight(null) < 0) {  
                     try {  
                         count++;  
                         if(count > 100)  
                             throw new Error("Timeout");  
                         Thread.sleep(200);  
                     } catch(InterruptedException e) {  
                         throw new Error("Interrupted");  
                     }  
                 }  
                 SimpleImageScroll.this.im = img;  
                 SimpleImageScroll.this.width = img.getWidth(null);  
                 SimpleImageScroll.this.height = img.getHeight(null);  
             }  
         });  
87      }      }
88    
89      class SimpleImageSpan extends ScrollBlockManager.ImageSpanBase implements Runnable{      class SimpleImageSpan extends ScrollBlockManager.ImageSpanBase {
90          SimpleImageSpan(int x, int y, int w, int h) {          SimpleImageSpan(int x, int y, int w, int h) {
91              super(SimpleImageScroll.this, x, y, w, h);              super(SimpleImageScroll.this, x, y, w, h);
92          }          }
# Line 134  String rcsid = "$Id$"; Line 96  String rcsid = "$Id$";
96              return new SimpleImageSpan(x, y, w, h);              return new SimpleImageSpan(x, y, w, h);
97          }          }
98    
         Image cached;  
   
         public Image getImage() {  
             if(cached != null) return cached;  
             if(im == null) {  
                 loadImage();  
                 return null;  
             }  
             if(x==0 && y==0 && w==width && h==height)  
                 return im;  
   
             // Background.getDefaultInstance().addTask(this);  
             return null;  
         }  
   
         public void run() {  
             if(cached != null) return;  
   
             ImageFilter filter = new CropImageFilter(x,y,w,h);  
             cached = java.awt.Toolkit.getDefaultToolkit().createImage  
                     (new FilteredImageSource(im.getSource(), filter));  
         }  
99      }      }
100    
101      public Span getCurrent() {      public Span getCurrent() {
102          if(im == null) loadImage();          if(width < 0) loadImageSize();
103          return new SimpleImageSpan(0, 0, width, height);          return new SimpleImageSpan(0, 0, width, height);
104      }      }
105    
# Line 168  String rcsid = "$Id$"; Line 108  String rcsid = "$Id$";
108      }      }
109    
110    
     public boolean isFinalized() { return true; }  
111  }  }
112    
113    

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26