consolidation of Platform isJS calls
[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 jalview.api.AlignmentViewPanel;
24 import jalview.api.structures.JalviewStructureDisplayI;
25 import jalview.bin.Cache;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.SequenceI;
28 import jalview.ext.jmol.JalviewJmolBinding;
29 import jalview.io.DataSourceType;
30 import jalview.structure.StructureSelectionManager;
31 import jalview.util.Platform;
32
33 import java.awt.Container;
34 import java.io.File;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.jmol.api.JmolAppConsoleInterface;
39
40 import javajs.util.BS;
41
42 public class AppJmolBinding extends JalviewJmolBinding
43 {
44   protected AppJmol appJmolWindow;
45
46   public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm,
47           PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
48           DataSourceType protocol)
49   {
50     super(sSm, pdbentry, sequenceIs, protocol);
51     appJmolWindow = appJmol;
52   }
53
54   @Override
55   protected IProgressIndicator getIProgressIndicator()
56   {
57     return appJmolWindow.progressBar;
58   }
59
60   @Override
61   public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
62   {
63     return new SequenceRenderer(((AlignmentPanel) alignment).av);
64   }
65
66   @Override
67   public void sendConsoleEcho(String strEcho)
68   {
69     if (console != null)
70     {
71       console.sendConsoleEcho(strEcho);
72     }
73   }
74
75   @Override
76   public void sendConsoleMessage(String strStatus)
77   {
78     if (console != null && strStatus != null)
79     // && !strStatus.equals("Script completed"))
80     // should we squash the script completed string ?
81     {
82       console.sendConsoleMessage(strStatus);
83     }
84   }
85
86   @Override
87   public void showUrl(String url, String target)
88   {
89     try
90     {
91       jalview.util.BrowserLauncher.openURL(url);
92     } catch (Exception e)
93     {
94       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
95       // TODO: 2.6 : warn user if browser was not configured.
96     }
97   }
98
99   @Override
100   public void refreshGUI()
101   {
102     // appJmolWindow.repaint();
103     javax.swing.SwingUtilities.invokeLater(new Runnable()
104     {
105       @Override
106       public void run()
107       {
108         appJmolWindow.updateTitleAndMenus();
109         appJmolWindow.revalidate();
110       }
111     });
112   }
113
114   @Override
115   public void updateColours(Object source)
116   {
117     AlignmentPanel ap = (AlignmentPanel) source;
118     // ignore events from panels not used to colour this view
119     if (!appJmolWindow.isUsedforcolourby(ap))
120     {
121       return;
122     }
123     if (!isLoadingFromArchive())
124     {
125       colourBySequence(ap);
126     }
127   }
128
129   @Override
130   public void notifyScriptTermination(String strStatus, int msWalltime)
131   {
132     // todo - script termination doesn't happen ?
133     // if (console != null)
134     // console.notifyScriptTermination(strStatus,
135     // msWalltime);
136   }
137
138   @Override
139   public void showUrl(String url)
140   {
141     showUrl(url, "jmol");
142   }
143
144   public void newJmolPopup(String menuName)
145   {
146     // jmolpopup = new JmolAwtPopup();
147     // jmolpopup.jpiInitialize((viewer), menuName);
148   }
149
150   @Override
151   public void selectionChanged(BS arg0)
152   {
153     // TODO Auto-generated method stub
154
155   }
156
157   @Override
158   public void refreshPdbEntries()
159   {
160     // TODO Auto-generated method stub
161
162   }
163
164   @Override
165   public void showConsole(boolean b)
166   {
167     appJmolWindow.showConsole(b);
168   }
169
170   @Override
171   protected JmolAppConsoleInterface createJmolConsole(
172           Container consolePanel, String buttonsToShow)
173   {
174     viewer.setJmolCallbackListener(this);
175     return null;//BH can't do this yet. new AppConsole(viewer, consolePanel, buttonsToShow);
176   }
177
178   @Override
179   protected void releaseUIResources()
180   {
181     appJmolWindow = null;
182     closeConsole();
183   }
184
185   @Override
186   public void releaseReferences(Object svl)
187   {
188     if (svl instanceof SeqPanel)
189     {
190       appJmolWindow.removeAlignmentPanel(((SeqPanel) svl).ap);
191     }
192   }
193
194   @Override
195   public Map<String, Object> getJSpecViewProperty(String arg0)
196   {
197     // TODO Auto-generated method stub
198     return null;
199   }
200
201   @Override
202   public JalviewStructureDisplayI getViewer()
203   {
204     return appJmolWindow;
205   }
206
207   @Override
208   public jalview.api.FeatureRenderer getFeatureRenderer(
209           AlignmentViewPanel alignment)
210   {
211     AlignmentPanel ap = (alignment == null)
212             ? appJmolWindow.getAlignmentPanel()
213             : (AlignmentPanel) alignment;
214     if (ap.av.isShowSequenceFeatures())
215     {
216       return ap.av.getAlignPanel().getSeqPanel().seqCanvas.fr;
217     }
218
219     return null;
220   }
221
222   @SuppressWarnings("unused")
223   public void cacheFiles(List<File> files)
224   {
225     if (files == null)
226     {
227       return;
228     }
229     for (File f : files)
230     {
231       String path = f.getAbsolutePath();
232       byte[] data = Platform.getFileBytes(f);
233       if (data != null)
234       {
235         Platform.cacheFileData(path, data);
236       }
237     }
238   }
239 }