JAL-3253 ApplicationSingletonProvider replaces Instance
[jalview.git] / src / jalview / gui / AssociatePdbFileWithSeq.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.datamodel.PDBEntry;
24 import jalview.datamodel.SequenceI;
25 import jalview.io.DataSourceType;
26 import jalview.io.StructureFile;
27 import jalview.util.MessageManager;
28
29 /**
30  * GUI related routines for associating PDB files with sequences. A single
31  * static method.
32  * 
33  * @author JimP
34  * 
35  */
36 public class AssociatePdbFileWithSeq
37 {
38
39   private AssociatePdbFileWithSeq()
40   {
41     // inaccessible
42   }
43
44   /**
45    * Associate the given PDB file name or URL with a sequence. Do not map
46    * mouse-over events.
47    * 
48    * @param fileName
49    *          or URL
50    * @param type
51    *          will be DataType.FILE or DataType.URL
52    * @param sequence
53    *          to associate
54    * @param prompt
55    *          true if the user should be asked what to do if the specified file
56    *          does not seem to contain PDB information (StructureChooser only)
57    * @return null if file is not found
58    */
59   public static PDBEntry associatePdbWithSeq(String fileName,
60           DataSourceType type, SequenceI sequence, boolean prompt)
61   {
62     PDBEntry entry = new PDBEntry();
63     StructureFile pdbfile = null;
64     pdbfile = Desktop.getInstance().getStructureSelectionManager()
65             .setMapping(false, new SequenceI[]
66             { sequence }, null, fileName, type);
67     if (pdbfile == null)
68     {
69       // stacktrace already thrown so just return
70       return null;
71     }
72     String id = pdbfile.getId();
73     if (id == null && (id = (prompt
74             ? JvOptionPane.showInternalInputDialog(Desktop.getDesktopPane(),
75                     MessageManager
76                             .getString("label.couldnt_find_pdb_id_in_file"),
77                     MessageManager.getString("label.no_pdb_id_in_file"),
78                     JvOptionPane.QUESTION_MESSAGE)
79             : null)) == null)
80     {
81       return null;
82     }
83     entry.setId(id);
84     entry.setType(PDBEntry.Type.FILE);
85     entry.setFile(fileName);
86     sequence.getDatasetSequence().addPDBId(entry);
87     Desktop.getInstance().getStructureSelectionManager()
88             .registerPDBEntry(entry);
89     return entry;
90   }
91 }