7fa300e523df1bf0c0329dd9d142960edefc381a
[jalview.git] / src / jalview / gui / AppJmolBinding.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.util.BitSet;
25
26 import jalview.api.AlignmentViewPanel;
27 import jalview.bin.Cache;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.structure.StructureSelectionManager;
31
32 import org.jmol.api.JmolAppConsoleInterface;
33 import org.jmol.api.JmolViewer;
34 import org.jmol.popup.JmolPopup;
35 import org.openscience.jmol.app.jmolpanel.AppConsole;
36
37 public class AppJmolBinding extends jalview.ext.jmol.JalviewJmolBinding
38 {
39
40   /**
41    * 
42    */
43   private AppJmol appJmolWindow;
44
45   public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm,
46           PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
47           String protocol)
48   {
49     super(sSm, pdbentry, sequenceIs, chains, protocol);
50     appJmolWindow = appJmol;
51   }
52
53   FeatureRenderer fr = null;
54
55   @Override
56   public jalview.api.FeatureRenderer getFeatureRenderer(
57           AlignmentViewPanel alignment)
58   {
59     AlignmentPanel ap = (alignment == null) ? appJmolWindow.ap
60             : (AlignmentPanel) alignment;
61     if (ap.av.showSequenceFeatures)
62     {
63       if (fr == null)
64       {
65         fr = ap.cloneFeatureRenderer();
66       }
67       else
68       {
69         ap.updateFeatureRenderer(fr);
70       }
71     }
72
73     return fr;
74   }
75
76   @Override
77   public jalview.api.SequenceRenderer getSequenceRenderer(
78           AlignmentViewPanel alignment)
79   {
80     return new SequenceRenderer(((AlignmentPanel) alignment).av);
81   }
82
83   public void sendConsoleEcho(String strEcho)
84   {
85     if (console != null)
86     {
87       console.sendConsoleEcho(strEcho);
88     }
89   }
90
91   public void sendConsoleMessage(String strStatus)
92   {
93     if (console != null && strStatus != null)
94     // && !strStatus.equals("Script completed"))
95     // should we squash the script completed string ?
96     {
97       console.sendConsoleMessage(strStatus);
98     }
99   }
100
101   @Override
102   public void showUrl(String url, String target)
103   {
104     try
105     {
106       jalview.util.BrowserLauncher.openURL(url);
107     } catch (Exception e)
108     {
109       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
110       // TODO: 2.6 : warn user if browser was not configured.
111     }
112   }
113
114   @Override
115   public void refreshGUI()
116   {
117     // appJmolWindow.repaint();
118     javax.swing.SwingUtilities.invokeLater(new Runnable()
119     {
120       public void run()
121       {
122         appJmolWindow.updateTitleAndMenus();
123         appJmolWindow.revalidate();
124       }
125     });
126   }
127
128   public void updateColours(Object source)
129   {
130     AlignmentPanel ap = (AlignmentPanel) source, topap;
131     // ignore events from panels not used to colour this view
132     if (!appJmolWindow.isUsedforcolourby(ap))
133       return;
134     if (!isLoadingFromArchive())
135     {
136       colourBySequence(ap.av.getShowSequenceFeatures(), ap);
137     }
138   }
139
140   public void notifyScriptTermination(String strStatus, int msWalltime)
141   {
142     // todo - script termination doesn't happen ?
143     // if (console != null)
144     // console.notifyScriptTermination(strStatus,
145     // msWalltime);
146   }
147
148   public void showUrl(String url)
149   {
150     showUrl(url, "jmol");
151   }
152
153   public void newJmolPopup(boolean translateLocale, String menuName,
154           boolean asPopup)
155   {
156
157     jmolpopup = new JmolPopup();
158     jmolpopup.initialize(viewer, translateLocale, menuName, asPopup);
159   }
160
161   public void selectionChanged(BitSet arg0)
162   {
163     // TODO Auto-generated method stub
164
165   }
166
167   public void refreshPdbEntries()
168   {
169     // TODO Auto-generated method stub
170
171   }
172
173   public void showConsole(boolean b)
174   {
175     appJmolWindow.showConsole(b);
176   }
177
178   /**
179    * add the given sequences to the mapping scope for the given pdb file handle
180    * 
181    * @param pdbFile
182    *          - pdbFile identifier
183    * @param seq
184    *          - set of sequences it can be mapped to
185    */
186   public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
187   {
188     for (int pe = 0; pe < pdbentry.length; pe++)
189     {
190       if (pdbentry[pe].getFile().equals(pdbFile))
191       {
192         addSequence(pe, seq);
193       }
194     }
195   }
196
197   @Override
198   protected JmolAppConsoleInterface createJmolConsole(JmolViewer viewer2,
199           Container consolePanel, String buttonsToShow)
200   {
201     return new AppConsole(viewer, consolePanel, buttonsToShow);
202   }
203
204   @Override
205   protected void releaseUIResources()
206   {
207     appJmolWindow = null;
208     if (console != null)
209     {
210       try
211       {
212         console.setVisible(false);
213       } catch (Error e)
214       {
215       } catch (Exception x)
216       {
217       }
218       ;
219       console = null;
220     }
221
222   }
223
224   @Override
225   public void releaseReferences(Object svl)
226   {
227     if (svl instanceof SeqPanel)
228     {
229       appJmolWindow.removeAlignmentPanel(((SeqPanel) svl).ap);
230
231     }
232     ;
233   }
234 }