JAL-629 Fix --structureimage and options
[jalview.git] / src / jalview / gui / AppJmolBinding.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 java.awt.Container;
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.swing.JComponent;
30
31 import org.jmol.api.JmolAppConsoleInterface;
32 import org.openscience.jmol.app.jmolpanel.console.AppConsole;
33
34 import jalview.api.AlignmentViewPanel;
35 import jalview.api.structures.JalviewStructureDisplayI;
36 import jalview.bin.Console;
37 import jalview.bin.Jalview;
38 import jalview.datamodel.PDBEntry;
39 import jalview.datamodel.SequenceI;
40 import jalview.ext.jmol.JalviewJmolBinding;
41 import jalview.io.DataSourceType;
42 import jalview.structure.StructureSelectionManager;
43 import jalview.util.MessageManager;
44 import jalview.util.Platform;
45 import javajs.util.BS;
46
47 public class AppJmolBinding extends JalviewJmolBinding
48 {
49   public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm,
50           PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
51           DataSourceType protocol)
52   {
53     super(sSm, pdbentry, sequenceIs, protocol);
54     setViewer(appJmol);
55   }
56
57   @Override
58   public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
59   {
60     return new SequenceRenderer(((AlignmentPanel) alignment).av);
61   }
62
63   @Override
64   public void sendConsoleEcho(String strEcho)
65   {
66     if (console != null)
67     {
68       console.sendConsoleEcho(strEcho);
69     }
70   }
71
72   @Override
73   public void sendConsoleMessage(String strStatus)
74   {
75     if (console != null && strStatus != null)
76     // && !strStatus.equals("Script completed"))
77     // should we squash the script completed string ?
78     {
79       console.sendConsoleMessage(strStatus);
80     }
81   }
82
83   @Override
84   public void showUrl(String url, String target)
85   {
86     try
87     {
88       jalview.util.BrowserLauncher.openURL(url);
89     } catch (Exception e)
90     {
91       Console.error("Failed to launch Jmol-associated url " + url, e);
92       // TODO: 2.6 : warn user if browser was not configured.
93     }
94   }
95
96   @Override
97   public void refreshGUI()
98   {
99     if (getMappedStructureCount() == 0)
100     {
101       // too soon!
102       return;
103     }
104     // appJmolWindow.repaint();
105     javax.swing.SwingUtilities.invokeLater(new Runnable()
106     {
107       @Override
108       public void run()
109       {
110         JalviewStructureDisplayI theViewer = getViewer();
111         // invokes colourbySequence() via seqColour_ActionPerformed()
112         theViewer.updateTitleAndMenus();
113         ((JComponent) theViewer).revalidate();
114       }
115     });
116   }
117
118   @Override
119   public void notifyScriptTermination(String strStatus, int msWalltime)
120   {
121     // todo - script termination doesn't happen ?
122     // if (console != null)
123     // console.notifyScriptTermination(strStatus,
124     // msWalltime);
125   }
126
127   @Override
128   public void showUrl(String url)
129   {
130     showUrl(url, "jmol");
131   }
132
133   public void newJmolPopup(String menuName)
134   {
135     // jmolpopup = new JmolAwtPopup();
136     // jmolpopup.jpiInitialize((viewer), menuName);
137   }
138
139   @Override
140   public void selectionChanged(BS arg0)
141   {
142   }
143
144   @Override
145   public void showConsole(boolean b)
146   {
147     getViewer().showConsole(b);
148   }
149
150   @Override
151   protected JmolAppConsoleInterface createJmolConsole(
152           Container consolePanel, String buttonsToShow)
153   {
154     jmolViewer.setJmolCallbackListener(this);
155     // BH comment: can't do this yet [for JS only, or generally?]
156     return Platform.isJS() || Jalview.isHeadlessMode() ? null
157             : new AppConsole(jmolViewer, consolePanel, buttonsToShow);
158   }
159
160   @Override
161   protected void releaseUIResources()
162   {
163     setViewer(null);
164     closeConsole();
165   }
166
167   @Override
168   public void releaseReferences(Object svl)
169   {
170     if (svl instanceof SeqPanel)
171     {
172       getViewer().removeAlignmentPanel(((SeqPanel) svl).ap);
173     }
174   }
175
176   @Override
177   public Map<String, Object> getJSpecViewProperty(String arg0)
178   {
179     // TODO Auto-generated method stub
180     return null;
181   }
182
183   @SuppressWarnings("unused")
184   public void cacheFiles(List<File> files)
185   {
186     if (files == null)
187     {
188       return;
189     }
190     for (File f : files)
191     {
192       Platform.cacheFileData(f);
193     }
194   }
195
196   /**
197    * Retrieves and saves as file any modelled PDB entries for which we do not
198    * already have a file saved. Returns a list of absolute paths to structure
199    * files which were either retrieved, or already stored but not modelled in
200    * the structure viewer (i.e. files to add to the viewer display).
201    * 
202    * Currently only used by Jmol - similar but different code used for Chimera/X
203    * and Pymol so still need to refactor
204    * 
205    * @param structureViewer
206    *          UI proxy for the structure viewer
207    * @return list of absolute paths to structures retrieved that need to be
208    *         added to the display
209    */
210   public List<String> fetchPdbFiles(StructureViewerBase structureViewer)
211   {
212     // todo - record which pdbids were successfully imported.
213     StringBuilder errormsgs = new StringBuilder();
214
215     List<String> files = new ArrayList<>();
216     String pdbid = "";
217     try
218     {
219       String[] filesInViewer = getStructureFiles();
220       // TODO: replace with reference fetching/transfer code (validate PDBentry
221       // as a DBRef?)
222
223       for (int pi = 0; pi < getPdbCount(); pi++)
224       {
225         PDBEntry strucEntry = getPdbEntry(pi);
226
227         String file = strucEntry.getFile();
228         if (file == null)
229         {
230           pdbid = strucEntry.getId();
231           try
232           {
233             file = structureViewer.fetchPdbFile(strucEntry);
234           } catch (OutOfMemoryError oomerror)
235           {
236             new OOMWarning("Retrieving PDB id " + pdbid, oomerror);
237           } catch (Exception ex)
238           {
239             ex.printStackTrace();
240             errormsgs.append("'").append(pdbid).append("'");
241           }
242           if (file != null)
243           {
244             // success
245             files.add(file);
246           }
247           else
248           {
249             errormsgs.append("'").append(pdbid).append("' ");
250           }
251         }
252         else
253         {
254           if (filesInViewer != null && filesInViewer.length > 0)
255           {
256             structureViewer.setAddingStructures(true); // already files loaded.
257             for (int c = 0; c < filesInViewer.length; c++)
258             {
259               if (Platform.pathEquals(filesInViewer[c], file))
260               {
261                 file = null;
262                 break;
263               }
264             }
265           }
266           if (file != null)
267           {
268             files.add(file);
269           }
270         }
271       }
272     } catch (OutOfMemoryError oomerror)
273     {
274       new OOMWarning("Retrieving PDB files: " + pdbid, oomerror);
275     } catch (Exception ex)
276     {
277       ex.printStackTrace();
278       errormsgs.append("When retrieving pdbfiles : current was: '")
279               .append(pdbid).append("'");
280     }
281     if (errormsgs.length() > 0)
282     {
283       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
284               MessageManager.formatMessage(
285                       "label.pdb_entries_couldnt_be_retrieved", new String[]
286                       { errormsgs.toString() }),
287               MessageManager.getString("label.couldnt_load_file"),
288               JvOptionPane.ERROR_MESSAGE);
289     }
290     return files;
291   }
292
293 }