JAL-3253-applet headless branch - just experimenting.
[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.structure.StructureSelectionManager;
28 import jalview.util.MessageManager;
29
30 /**
31  * GUI related routines for associating PDB files with sequences. A single
32  * static method.
33  * 
34  * @author JimP
35  * 
36  */
37 public class AssociatePdbFileWithSeq
38 {
39
40   private AssociatePdbFileWithSeq()
41   {
42     // inaccessible
43   }
44
45   /**
46    * Associate the given PDB file name or URL with a sequence. Do not map
47    * mouse-over events.
48    * 
49    * @param fileName
50    *          or URL
51    * @param type
52    *          will be DataType.FILE or DataType.URL
53    * @param sequence
54    *          to associate
55    * @param prompt
56    *          true if the user should be asked what to do if the specified file
57    *          does not seem to contain PDB information (StructureChooser only)
58    * @return null if file is not found
59    */
60   public static PDBEntry associatePdbWithSeq(String fileName,
61           DataSourceType type, SequenceI sequence, boolean prompt)
62   {
63     PDBEntry entry = new PDBEntry();
64     StructureFile pdbfile = null;
65     pdbfile = StructureSelectionManager
66             .getStructureSelectionManager(Desktop.getInstance())
67             .setMapping(false, new SequenceI[]
68             { sequence }, null, fileName, type);
69     if (pdbfile == null)
70     {
71       // stacktrace already thrown so just return
72       return null;
73     }
74     String id = pdbfile.getId();
75     if (id == null && (id = (prompt
76             ? JvOptionPane.showInternalInputDialog(Desktop.getDesktopPane(),
77                     MessageManager
78                             .getString("label.couldnt_find_pdb_id_in_file"),
79                     MessageManager.getString("label.no_pdb_id_in_file"),
80                     JvOptionPane.QUESTION_MESSAGE)
81             : null)) == null)
82     {
83       return null;
84     }
85     entry.setId(id);
86     entry.setType(PDBEntry.Type.FILE);
87     entry.setFile(fileName);
88     sequence.getDatasetSequence().addPDBId(entry);
89     Desktop.getInstance().getStructureSelectionManager()
90             .registerPDBEntry(entry);
91     return entry;
92   }
93 }