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