Fixed issue with reading from TreeBase
[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: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.archaeopteryx.tools;
27
28 import java.awt.color.CMMException;
29 import java.awt.image.BufferedImage;
30 import java.io.IOException;
31 import java.net.MalformedURLException;
32 import java.util.ArrayList;
33 import java.util.Hashtable;
34 import java.util.List;
35
36 import javax.imageio.ImageIO;
37
38 import org.forester.archaeopteryx.AptxUtil;
39 import org.forester.archaeopteryx.AptxConstants;
40 import org.forester.archaeopteryx.TreePanel;
41 import org.forester.phylogeny.PhylogenyNode;
42 import org.forester.phylogeny.data.Taxonomy;
43 import org.forester.phylogeny.data.Uri;
44 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
45
46 public class ImageLoader implements Runnable {
47
48     private final TreePanel            _tp;
49     private static final BufferedImage PLACEHOLDER = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB );
50     private final static boolean       DEBUG       = false;
51
52     public ImageLoader( final TreePanel tp ) {
53         _tp = tp;
54     }
55
56     @Override
57     public void run() {
58         load();
59     }
60
61     private void load() {
62         Hashtable<String, BufferedImage> image_map = null;
63         if ( _tp.getImageMap() != null ) {
64             image_map = _tp.getImageMap();
65         }
66         else {
67             image_map = new Hashtable<String, BufferedImage>();
68             _tp.setImageMap( image_map );
69         }
70         // ImageIO.setUseCache( false );
71         for( final PhylogenyNodeIterator it = _tp.getPhylogeny().iteratorPreorder(); it.hasNext(); ) {
72             final PhylogenyNode node = it.next();
73             if ( node.getNodeData().isHasTaxonomy() && ( node.getNodeData().getTaxonomy().getUris() != null )
74                     && !node.getNodeData().getTaxonomy().getUris().isEmpty() ) {
75                 final List<Uri> us = new ArrayList<Uri>();
76                 for( final Taxonomy t : node.getNodeData().getTaxonomies() ) {
77                     for( final Uri uri : t.getUris() ) {
78                         us.add( uri );
79                     }
80                 }
81                 for( final Uri uri : us ) {
82                     if ( uri != null ) {
83                         final String type = uri.getType().toLowerCase();
84                         final String uri_str = uri.getValue().toString().toLowerCase();
85                         if ( ( !image_map.containsKey( uri_str ) )
86                                 && ( type.equals( "image" ) || type.equals( "img" ) || type.equals( "photo" )
87                                         || type.equals( "picture" ) || uri_str.endsWith( ".jpg" )
88                                         || uri_str.endsWith( ".jpeg" ) || uri_str.endsWith( ".png" )
89                                         || uri_str.endsWith( ".gif" ) || uri_str.endsWith( ".bmp" ) ) ) {
90                             image_map.put( uri_str, PLACEHOLDER );
91                             BufferedImage bi = null;
92                             if ( DEBUG ) {
93                                 System.out.println( "accessing: " + uri );
94                             }
95                             try {
96                                 bi = ImageIO.read( uri.getValue().toURL() );
97                             }
98                             catch ( final MalformedURLException e ) {
99                                 AptxUtil.printWarningMessage( AptxConstants.PRG_NAME,
100                                                               "could not load image from \"" + uri.getValue()
101                                                               + "\": Malformed URL Exception: "
102                                                               + e.getLocalizedMessage() );
103                             }
104                             catch ( final IOException e ) {
105                                 AptxUtil.printWarningMessage( AptxConstants.PRG_NAME,
106                                                               "could not load image from \"" + uri.getValue()
107                                                               + "\": IO Exception: " + e.getLocalizedMessage() );
108                             }
109                             catch ( final CMMException e ) {
110                                 AptxUtil.printWarningMessage( AptxConstants.PRG_NAME,
111                                                               "could not load image from \"" + uri.getValue()
112                                                               + "\": CMMException: " + e.getLocalizedMessage() );
113                             }
114                             if ( bi != null ) {
115                                 image_map.put( uri_str, bi );
116                                 _tp.repaint();
117                             }
118                             else {
119                                 image_map.remove( uri_str );
120                             }
121                         }
122                     }
123                 }
124             }
125         }
126     }
127 }