new UI types?
[jalview.git] / forester / java / src / org / forester / archaeopteryx / TaxonomyDataObtainer.java
1 // $Id:
2 //
3 // forester -- software libraries and applications
4 // for genomics and evolutionary biology research.
5 //
6 // Copyright (C) 2010 Christian M Zmasek
7 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
8 // All rights reserved
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/forester
26
27 package org.forester.archaeopteryx;
28
29 import java.io.IOException;
30 import java.net.UnknownHostException;
31 import java.util.SortedSet;
32
33 import javax.swing.JOptionPane;
34
35 import org.forester.analysis.AncestralTaxonomyInference;
36 import org.forester.phylogeny.Phylogeny;
37 import org.forester.ws.uniprot.UniProtWsTools;
38
39 public class TaxonomyDataObtainer implements Runnable {
40
41     private final Phylogeny            _phy;
42     private final MainFrameApplication _mf;
43     private final TreePanel            _treepanel;
44
45     TaxonomyDataObtainer( final MainFrameApplication mf, final TreePanel treepanel, final Phylogeny phy ) {
46         _phy = phy;
47         _mf = mf;
48         _treepanel = treepanel;
49     }
50
51     private String getBaseUrl() {
52         return UniProtWsTools.BASE_URL;
53     }
54
55     private void execute() {
56         _mf.getMainPanel().getCurrentTreePanel().setWaitCursor();
57         SortedSet<String> not_found = null;
58         try {
59             not_found = AncestralTaxonomyInference.obtainDetailedTaxonomicInformation( _phy );
60         }
61         catch ( final UnknownHostException e ) {
62             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
63             JOptionPane.showMessageDialog( _mf,
64                                            "Could not connect to \"" + getBaseUrl() + "\"",
65                                            "Network error during taxonomic information gathering",
66                                            JOptionPane.ERROR_MESSAGE );
67             return;
68         }
69         catch ( final IOException e ) {
70             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
71             e.printStackTrace();
72             JOptionPane.showMessageDialog( _mf,
73                                            e.toString(),
74                                            "Failed to obtain taxonomic information",
75                                            JOptionPane.ERROR_MESSAGE );
76             return;
77         }
78         finally {
79             _mf.getMainPanel().getCurrentTreePanel().setArrowCursor();
80         }
81         _treepanel.setTree( _phy );
82         _mf.showWhole();
83         _treepanel.setEdited( true );
84         if ( ( not_found != null ) && ( not_found.size() > 0 ) ) {
85             int max = not_found.size();
86             boolean more = false;
87             if ( max > 20 ) {
88                 more = true;
89                 max = 20;
90             }
91             final StringBuffer sb = new StringBuffer();
92             sb.append( "Not all taxonomies could be resolved.\n" );
93             if ( not_found.size() == 1 ) {
94                 sb.append( "The following taxonomy was not found:\n" );
95             }
96             else {
97                 sb.append( "The following taxonomies were not found (total: " + not_found.size() + "):\n" );
98             }
99             int i = 0;
100             for( final String string : not_found ) {
101                 if ( i > 19 ) {
102                     break;
103                 }
104                 sb.append( string );
105                 sb.append( "\n" );
106                 ++i;
107             }
108             if ( more ) {
109                 sb.append( "..." );
110             }
111             try {
112                 JOptionPane.showMessageDialog( _mf,
113                                                sb.toString(),
114                                                "Taxonomy Tool Completed",
115                                                JOptionPane.WARNING_MESSAGE );
116             }
117             catch ( final Exception e ) {
118                 // Not important if this fails, do nothing. 
119             }
120         }
121         else {
122             try {
123                 JOptionPane.showMessageDialog( _mf,
124                                                "Taxonomy tool successfully completed",
125                                                "Taxonomy Tool Completed",
126                                                JOptionPane.INFORMATION_MESSAGE );
127             }
128             catch ( final Exception e ) {
129                 // Not important if this fails, do nothing.
130             }
131         }
132     }
133
134     @Override
135     public void run() {
136         execute();
137     }
138 }