patched Jmol and code for embedding JmolAppConsole in a JPanel (JAL-638)
[jalview.git] / src / jalview / gui / AppJmolBinding.java
1 /**
2  * 
3  */
4 package jalview.gui;
5
6 import java.util.BitSet;
7
8 import javax.swing.JPanel;
9
10 import jalview.bin.Cache;
11 import jalview.datamodel.PDBEntry;
12 import jalview.datamodel.SequenceI;
13
14 import org.jmol.popup.JmolPopup;
15 import org.openscience.jmol.app.jmolpanel.AppConsole;
16
17 public class AppJmolBinding extends jalview.ext.jmol.JalviewJmolBinding
18 {
19
20   /**
21    * 
22    */
23   private AppJmol appJmolWindow;
24
25   public AppJmolBinding(AppJmol appJmol, PDBEntry[] pdbentry,
26           SequenceI[][] sequenceIs, String[][] chains, String protocol)
27   {
28     super(pdbentry, sequenceIs, chains, protocol);
29     appJmolWindow = appJmol;
30   }
31
32   FeatureRenderer fr = null;
33
34   @Override
35   public jalview.api.FeatureRenderer getFeatureRenderer()
36   {
37     if (appJmolWindow.ap.av.showSequenceFeatures)
38     {
39       if (fr == null)
40       {
41         fr = new FeatureRenderer(appJmolWindow.ap);
42       }
43
44       fr.transferSettings(appJmolWindow.ap.seqPanel.seqCanvas
45               .getFeatureRenderer());
46     }
47
48     return fr;
49   }
50
51   @Override
52   public jalview.api.SequenceRenderer getSequenceRenderer()
53   {
54     return new SequenceRenderer(appJmolWindow.ap.av);
55   }
56
57   public void sendConsoleEcho(String strEcho)
58   {
59     if (console != null)
60     {
61       console.sendConsoleEcho(strEcho);
62     }
63   }
64
65   public void sendConsoleMessage(String strStatus)
66   {
67     if (console != null && strStatus != null)
68     // && !strStatus.equals("Script completed"))
69     // should we squash the script completed string ?
70     {
71       console.sendConsoleMessage(strStatus);
72     }
73   }
74
75   @Override
76   public void showUrl(String url, String target)
77   {
78     try
79     {
80       jalview.util.BrowserLauncher.openURL(url);
81     } catch (Exception e)
82     {
83       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
84       // TODO: 2.6 : warn user if browser was not configured.
85     }
86   }
87
88   @Override
89   public void refreshGUI()
90   {
91     // appJmolWindow.repaint();
92     javax.swing.SwingUtilities.invokeLater(new Runnable() {
93       public void run() {
94         appJmolWindow.updateTitleAndMenus();
95         appJmolWindow.revalidate();
96       }});
97   }
98
99   public void updateColours(Object source)
100   {
101     AlignmentPanel ap = (AlignmentPanel) source;
102     if (appJmolWindow.ap.alignFrame.getCurrentView() != ap.av)
103       return;
104
105     colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment);
106   }
107
108   public void notifyScriptTermination(String strStatus, int msWalltime)
109   {
110     // todo - script termination doesn't happen ?
111     //if (console != null)
112       //console.notifyScriptTermination(strStatus,
113       //        msWalltime);
114   }
115
116   public void showUrl(String url)
117   {
118     showUrl(url, "jmol");
119   }
120
121   public void newJmolPopup(boolean translateLocale, String menuName,
122           boolean asPopup)
123   {
124
125     jmolpopup = JmolPopup.newJmolPopup(viewer, translateLocale, menuName,
126             asPopup);
127   }
128
129   public void selectionChanged(BitSet arg0)
130   {
131     // TODO Auto-generated method stub
132
133   }
134
135   public void refreshPdbEntries()
136   {
137     // TODO Auto-generated method stub
138
139   }
140
141   public void showConsole(boolean b) {
142     appJmolWindow.showConsole(b);
143   }
144
145   /**
146    * add the given sequences to the mapping scope for the given pdb file handle
147    * 
148    * @param pdbFile
149    *          - pdbFile identifier
150    * @param seq
151    *          - set of sequences it can be mapped to
152    */
153   public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
154   {
155     for (int pe = 0; pe < pdbentry.length; pe++)
156     {
157       if (pdbentry[pe].getFile().equals(pdbFile))
158       {
159         addSequence(pe, seq);
160       }
161     }
162   }
163 }