in progress
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / ImageLoader.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.awt.image.BufferedImage;
29 import java.io.IOException;
30 import java.net.MalformedURLException;
31 import java.util.ArrayList;
32 import java.util.Hashtable;
33 import java.util.List;
34
35 import javax.imageio.ImageIO;
36
37 import org.forester.archaeopteryx.AptxUtil;
38 import org.forester.archaeopteryx.Constants;
39 import org.forester.archaeopteryx.TreePanel;
40 import org.forester.phylogeny.PhylogenyNode;
41 import org.forester.phylogeny.data.Taxonomy;
42 import org.forester.phylogeny.data.Uri;
43 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
44
45 public class ImageLoader implements Runnable {
46
47     private final TreePanel            _tp;
48     private static final BufferedImage PLACEHOLDER = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB );
49
50     public ImageLoader( final TreePanel tp ) {
51         _tp = tp;
52     }
53
54     private void load() {
55         Hashtable<String, BufferedImage> image_map = null;
56         if ( _tp.getImageMap() != null ) {
57             image_map = _tp.getImageMap();
58         }
59         else {
60             image_map = new Hashtable<String, BufferedImage>();
61             _tp.setImageMap( image_map );
62         }
63         // ImageIO.setUseCache( false );
64         for( final PhylogenyNodeIterator it = _tp.getPhylogeny().iteratorPreorder(); it.hasNext(); ) {
65             final PhylogenyNode node = it.next();
66             if ( node.getNodeData().isHasTaxonomy() && ( node.getNodeData().getTaxonomy().getUris() != null )
67                     && !node.getNodeData().getTaxonomy().getUris().isEmpty() ) {
68                 final List<Uri> us = new ArrayList<Uri>();
69                 for( final Taxonomy t : node.getNodeData().getTaxonomies() ) {
70                     for( final Uri uri : t.getUris() ) {
71                         us.add( uri );
72                     }
73                 }
74                 for( final Uri uri : us ) {
75                     if ( uri != null ) {
76                         final String type = uri.getType().toLowerCase();
77                         final String uri_str = uri.getValue().toString().toLowerCase();
78                         if ( ( !image_map.containsKey( uri_str ) )
79                                 && ( type.equals( "image" ) || type.equals( "img" ) || type.equals( "photo" )
80                                         || type.equals( "picture" ) || uri_str.endsWith( ".jpg" )
81                                         || uri_str.endsWith( ".jpeg" ) || uri_str.endsWith( ".png" )
82                                         || uri_str.endsWith( ".gif" ) || uri_str.endsWith( ".bmp" ) ) ) {
83                             image_map.put( uri_str, PLACEHOLDER );
84                             BufferedImage bi = null;
85                             if ( !Constants.__RELEASE && !Constants.__SNAPSHOT_RELEASE ) {
86                                 System.out.println( "accessing:" + uri );
87                             }
88                             try {
89                                 bi = ImageIO.read( uri.getValue().toURL() );
90                             }
91                             catch ( final MalformedURLException e ) {
92                                 AptxUtil.printWarningMessage( Constants.PRG_NAME, "\"" + uri.getValue()
93                                         + "\": Malformed URL Exception: " + e.getLocalizedMessage() );
94                             }
95                             catch ( final IOException e ) {
96                                 AptxUtil.printWarningMessage( Constants.PRG_NAME, "\"" + uri.getValue()
97                                         + "\": IO Exception: " + e.getLocalizedMessage() );
98                             }
99                             if ( bi != null ) {
100                                 image_map.put( uri_str, bi );
101                                 _tp.repaint();
102                             }
103                             else {
104                                 image_map.remove( uri_str );
105                             }
106                         }
107                     }
108                 }
109             }
110         }
111     }
112
113     @Override
114     public void run() {
115         load();
116     }
117 }