Jalview 2.6 source licence
[jalview.git] / src / jalview / gui / AppJmolBinding.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.Container;
21 import java.awt.event.ComponentEvent;
22 import java.awt.event.ComponentListener;
23 import java.awt.event.ContainerEvent;
24 import java.awt.event.ContainerListener;
25 import java.util.BitSet;
26
27 import javax.swing.JPanel;
28
29 import jalview.bin.Cache;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.SequenceI;
32
33 import org.jmol.api.JmolAppConsoleInterface;
34 import org.jmol.api.JmolViewer;
35 import org.jmol.popup.JmolPopup;
36 import org.openscience.jmol.app.jmolpanel.AppConsole;
37
38 public class AppJmolBinding extends jalview.ext.jmol.JalviewJmolBinding 
39 {
40
41   /**
42    * 
43    */
44   private AppJmol appJmolWindow;
45
46   public AppJmolBinding(AppJmol appJmol, PDBEntry[] pdbentry,
47           SequenceI[][] sequenceIs, String[][] chains, String protocol)
48   {
49     super(pdbentry, sequenceIs, chains, protocol);
50     appJmolWindow = appJmol;
51   }
52
53   FeatureRenderer fr = null;
54
55   @Override
56   public jalview.api.FeatureRenderer getFeatureRenderer()
57   {
58     if (appJmolWindow.ap.av.showSequenceFeatures)
59     {
60       if (fr == null)
61       {
62         fr = new FeatureRenderer(appJmolWindow.ap);
63       }
64
65       fr.transferSettings(appJmolWindow.ap.seqPanel.seqCanvas
66               .getFeatureRenderer());
67     }
68
69     return fr;
70   }
71
72   @Override
73   public jalview.api.SequenceRenderer getSequenceRenderer()
74   {
75     return new SequenceRenderer(appJmolWindow.ap.av);
76   }
77
78   public void sendConsoleEcho(String strEcho)
79   {
80     if (console != null)
81     {
82       console.sendConsoleEcho(strEcho);
83     }
84   }
85
86   public void sendConsoleMessage(String strStatus)
87   {
88     if (console != null && strStatus != null)
89     // && !strStatus.equals("Script completed"))
90     // should we squash the script completed string ?
91     {
92       console.sendConsoleMessage(strStatus);
93     }
94   }
95
96   @Override
97   public void showUrl(String url, String target)
98   {
99     try
100     {
101       jalview.util.BrowserLauncher.openURL(url);
102     } catch (Exception e)
103     {
104       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
105       // TODO: 2.6 : warn user if browser was not configured.
106     }
107   }
108
109   @Override
110   public void refreshGUI()
111   {
112     // appJmolWindow.repaint();
113     javax.swing.SwingUtilities.invokeLater(new Runnable() {
114       public void run() {
115         appJmolWindow.updateTitleAndMenus();
116         appJmolWindow.revalidate();
117       }});
118   }
119
120   public void updateColours(Object source)
121   {
122     AlignmentPanel ap = (AlignmentPanel) source;
123     if (appJmolWindow.ap.alignFrame.getCurrentView() != ap.av)
124       return;
125
126     colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment);
127   }
128
129   public void notifyScriptTermination(String strStatus, int msWalltime)
130   {
131     // todo - script termination doesn't happen ?
132     //if (console != null)
133       //console.notifyScriptTermination(strStatus,
134       //        msWalltime);
135   }
136
137   public void showUrl(String url)
138   {
139     showUrl(url, "jmol");
140   }
141
142   public void newJmolPopup(boolean translateLocale, String menuName,
143           boolean asPopup)
144   {
145
146     jmolpopup = JmolPopup.newJmolPopup(viewer, translateLocale, menuName,
147             asPopup);
148   }
149
150   public void selectionChanged(BitSet arg0)
151   {
152     // TODO Auto-generated method stub
153
154   }
155
156   public void refreshPdbEntries()
157   {
158     // TODO Auto-generated method stub
159
160   }
161
162   public void showConsole(boolean b) {
163     appJmolWindow.showConsole(b);
164   }
165
166   /**
167    * add the given sequences to the mapping scope for the given pdb file handle
168    * 
169    * @param pdbFile
170    *          - pdbFile identifier
171    * @param seq
172    *          - set of sequences it can be mapped to
173    */
174   public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
175   {
176     for (int pe = 0; pe < pdbentry.length; pe++)
177     {
178       if (pdbentry[pe].getFile().equals(pdbFile))
179       {
180         addSequence(pe, seq);
181       }
182     }
183   }
184
185   @Override
186   protected JmolAppConsoleInterface createJmolConsole(JmolViewer viewer2,
187           Container consolePanel, String buttonsToShow)
188   {
189     return new AppConsole(viewer, consolePanel, buttonsToShow);
190   }
191
192   @Override
193   protected void releaseUIResources()
194   {
195     appJmolWindow = null;
196     if (console!=null) {
197       try {
198         console.setVisible(false);
199       } catch (Error e) {} catch (Exception x) {};
200       console=null;
201     }
202     
203   }
204 }