c18015ad9b1ae248a150b46d252c93dbf7e1291d
[jalview.git] / src / jalview / gui / AppJmol.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
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.util.regex.*;
21 import java.util.*;
22 import java.awt.*;
23 import javax.swing.*;
24 import javax.swing.event.*;
25 import java.awt.event.*;
26 import java.io.*;
27
28 import jalview.jbgui.GStructureViewer;
29 import jalview.datamodel.*;
30 import jalview.gui.*;
31 import jalview.structure.*;
32 import jalview.datamodel.PDBEntry;
33 import jalview.io.*;
34 import jalview.schemes.*;
35 import jalview.ws.ebi.EBIFetchClient;
36
37 import org.jmol.api.*;
38 import org.jmol.adapter.smarter.SmarterJmolAdapter;
39 import org.jmol.popup.*;
40
41 public class AppJmol extends GStructureViewer implements StructureListener,
42         JmolStatusListener, Runnable
43
44 {
45   JmolViewer viewer;
46
47   JmolPopup jmolpopup;
48
49   ScriptWindow scriptWindow;
50
51   PDBEntry pdbentry;
52
53   SequenceI[] sequence;
54
55   String[] chains;
56
57   StructureSelectionManager ssm;
58
59   JSplitPane splitPane;
60
61   RenderPanel renderPanel;
62
63   AlignmentPanel ap;
64
65   String fileLoadingError;
66
67   boolean colourBySequence = true;
68
69   boolean loadingFromArchive = false;
70
71   Vector atomsPicked = new Vector();
72
73   public AppJmol(String file, String id, SequenceI[] seq,
74           AlignmentPanel ap, String loadStatus, Rectangle bounds)
75   {
76     this(file, id, seq, ap, loadStatus, bounds, null);
77   }
78
79   public AppJmol(String file, String id, SequenceI[] seq,
80           AlignmentPanel ap, String loadStatus, Rectangle bounds,
81           String viewid)
82   {
83     loadingFromArchive = true;
84     pdbentry = new PDBEntry();
85     pdbentry.setFile(file);
86     pdbentry.setId(id);
87     this.chains = chains;
88     this.sequence = seq;
89     this.ap = ap;
90     this.setBounds(bounds);
91     colourBySequence = false;
92     seqColour.setSelected(false);
93     viewId = viewid;
94     // jalview.gui.Desktop.addInternalFrame(this, "Loading File",
95     // bounds.width,bounds.height);
96
97     initJmol(loadStatus);
98
99     this.addInternalFrameListener(new InternalFrameAdapter()
100     {
101       public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
102       {
103         closeViewer();
104       }
105     });
106   }
107
108   public synchronized void addSequence(SequenceI[] seq)
109   {
110     Vector v = new Vector();
111     for (int i = 0; i < sequence.length; i++)
112       v.addElement(sequence[i]);
113
114     for (int i = 0; i < seq.length; i++)
115       if (!v.contains(seq[i]))
116         v.addElement(seq[i]);
117
118     SequenceI[] tmp = new SequenceI[v.size()];
119     v.copyInto(tmp);
120     sequence = tmp;
121   }
122
123   public AppJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
124           AlignmentPanel ap)
125   {
126     // ////////////////////////////////
127     // Is the pdb file already loaded?
128     String alreadyMapped = StructureSelectionManager
129             .getStructureSelectionManager().alreadyMappedToFile(
130                     pdbentry.getId());
131
132     if (alreadyMapped != null)
133     {
134       int option = JOptionPane
135               .showInternalConfirmDialog(
136                       Desktop.desktop,
137                       pdbentry.getId()
138                               + " is already displayed."
139                               + "\nDo you want to map sequences to the visible structure?",
140                       "Map Sequences to Visible Window: "
141                               + pdbentry.getId(), JOptionPane.YES_NO_OPTION);
142
143       if (option == JOptionPane.YES_OPTION)
144       {
145         StructureSelectionManager.getStructureSelectionManager()
146                 .setMapping(seq, chains, alreadyMapped,
147                         AppletFormatAdapter.FILE);
148         if (ap.seqPanel.seqCanvas.fr != null)
149         {
150           ap.seqPanel.seqCanvas.fr.featuresAdded();
151           ap.paintAlignment(true);
152         }
153
154         // Now this AppJmol is mapped to new sequences. We must add them to
155         // the exisiting array
156         JInternalFrame[] frames = Desktop.instance.getAllFrames();
157
158         for (int i = 0; i < frames.length; i++)
159         {
160           if (frames[i] instanceof AppJmol)
161           {
162             AppJmol topJmol = ((AppJmol) frames[i]);
163             if (topJmol.pdbentry.getFile().equals(alreadyMapped))
164             {
165               topJmol.addSequence(seq);
166               break;
167             }
168           }
169         }
170
171         return;
172       }
173     }
174     // /////////////////////////////////
175
176     this.ap = ap;
177     this.pdbentry = pdbentry;
178     this.sequence = seq;
179     this.setSize(400, 400);
180     // jalview.gui.Desktop.addInternalFrame(this, "Jmol
181     // View"+(pdbentry.getId()!=null ? "for "+pdbentry.getId()
182     // : ""), 400, 400);
183
184     if (pdbentry.getFile() != null)
185     {
186       initJmol("load \"" + pdbentry.getFile() + "\"");
187     }
188     else
189     {
190       Thread worker = new Thread(this);
191       worker.start();
192     }
193
194     this.addInternalFrameListener(new InternalFrameAdapter()
195     {
196       public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
197       {
198         closeViewer();
199       }
200     });
201   }
202
203   void initJmol(String command)
204   {
205     renderPanel = new RenderPanel();
206
207     this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
208
209     StringBuffer title = new StringBuffer(sequence[0].getName() + ":"
210             + pdbentry.getId());
211
212     if (pdbentry.getProperty() != null)
213     {
214       if (pdbentry.getProperty().get("method") != null)
215       {
216         title.append(" Method: ");
217         title.append(pdbentry.getProperty().get("method"));
218       }
219       if (pdbentry.getProperty().get("chains") != null)
220       {
221         title.append(" Chain:");
222         title.append(pdbentry.getProperty().get("chains"));
223       }
224     }
225
226     this.setTitle(title.toString());
227     jalview.gui.Desktop.addInternalFrame(this, title.toString(),
228             getBounds().width, getBounds().height);
229
230     viewer = org.jmol.api.JmolViewer.allocateViewer(renderPanel,
231             new SmarterJmolAdapter());
232
233     viewer.setAppletContext("", null, null, "");
234
235     viewer.setJmolStatusListener(this);
236
237     jmolpopup = JmolPopup.newJmolPopup(viewer);
238
239     viewer.evalStringQuiet(command);
240   }
241
242   void setChainMenuItems(Vector chains)
243   {
244     chainMenu.removeAll();
245
246     JMenuItem menuItem = new JMenuItem("All");
247     menuItem.addActionListener(new ActionListener()
248     {
249       public void actionPerformed(ActionEvent evt)
250       {
251         allChainsSelected = true;
252         for (int i = 0; i < chainMenu.getItemCount(); i++)
253         {
254           if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
255             ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
256         }
257         centerViewer();
258         allChainsSelected = false;
259       }
260     });
261
262     chainMenu.add(menuItem);
263
264     for (int c = 0; c < chains.size(); c++)
265     {
266       menuItem = new JCheckBoxMenuItem(chains.elementAt(c).toString(), true);
267       menuItem.addItemListener(new ItemListener()
268       {
269         public void itemStateChanged(ItemEvent evt)
270         {
271           if (!allChainsSelected)
272             centerViewer();
273         }
274       });
275
276       chainMenu.add(menuItem);
277     }
278   }
279
280   boolean allChainsSelected = false;
281
282   void centerViewer()
283   {
284     StringBuffer cmd = new StringBuffer();
285     for (int i = 0; i < chainMenu.getItemCount(); i++)
286     {
287       if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
288       {
289         JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
290         if (item.isSelected())
291           cmd.append(":" + item.getText() + " or ");
292       }
293     }
294
295     if (cmd.length() > 0)
296       cmd.setLength(cmd.length() - 4);
297
298     viewer.evalStringQuiet("select *;restrict " + cmd + ";cartoon;center "
299             + cmd);
300   }
301
302   void closeViewer()
303   {
304     viewer.setModeMouse(org.jmol.viewer.JmolConstants.MOUSE_NONE);
305     viewer.evalStringQuiet("zap");
306     viewer.setJmolStatusListener(null);
307     viewer = null;
308
309     // We'll need to find out what other
310     // listeners need to be shut down in Jmol
311     StructureSelectionManager.getStructureSelectionManager()
312             .removeStructureViewerListener(this, pdbentry.getFile());
313   }
314
315   public void run()
316   {
317     try
318     {
319       // TODO: replace with reference fetching/transfer code (validate PDBentry
320       // as a DBRef?)
321       jalview.ws.dbsources.Pdb pdbclient = new jalview.ws.dbsources.Pdb();
322       AlignmentI pdbseq;
323       if ((pdbseq = pdbclient.getSequenceRecords(pdbentry.getId())) != null)
324       {
325         // just transfer the file name from the first seuqence's first PDBEntry
326         pdbentry.setFile(((PDBEntry) pdbseq.getSequenceAt(0).getPDBId()
327                 .elementAt(0)).getFile());
328         initJmol("load " + pdbentry.getFile());
329       }
330       else
331       {
332         JOptionPane
333                 .showInternalMessageDialog(
334                         Desktop.desktop,
335                         pdbentry.getId()
336                                 + " could not be retrieved. Please try downloading the file manually.",
337                         "Couldn't load file", JOptionPane.ERROR_MESSAGE);
338
339       }
340     } catch (OutOfMemoryError oomerror)
341     {
342       new OOMWarning("Retrieving PDB id " + pdbentry.getId() + " from MSD",
343               oomerror);
344     } catch (Exception ex)
345     {
346       ex.printStackTrace();
347     }
348   }
349
350   public void pdbFile_actionPerformed(ActionEvent actionEvent)
351   {
352     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
353             .getProperty("LAST_DIRECTORY"));
354
355     chooser.setFileView(new JalviewFileView());
356     chooser.setDialogTitle("Save PDB File");
357     chooser.setToolTipText("Save");
358
359     int value = chooser.showSaveDialog(this);
360
361     if (value == JalviewFileChooser.APPROVE_OPTION)
362     {
363       try
364       {
365         BufferedReader in = new BufferedReader(new FileReader(pdbentry
366                 .getFile()));
367         File outFile = chooser.getSelectedFile();
368
369         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
370         String data;
371         while ((data = in.readLine()) != null)
372         {
373           if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
374           {
375             out.println(data);
376           }
377         }
378         out.close();
379       } catch (Exception ex)
380       {
381         ex.printStackTrace();
382       }
383     }
384   }
385
386   public void viewMapping_actionPerformed(ActionEvent actionEvent)
387   {
388     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
389     jalview.gui.Desktop.addInternalFrame(cap, "PDB - Sequence Mapping",
390             550, 600);
391     cap.setText(StructureSelectionManager.getStructureSelectionManager()
392             .printMapping(pdbentry.getFile()));
393   }
394
395   /**
396    * DOCUMENT ME!
397    * 
398    * @param e
399    *          DOCUMENT ME!
400    */
401   public void eps_actionPerformed(ActionEvent e)
402   {
403     makePDBImage(jalview.util.ImageMaker.EPS);
404   }
405
406   /**
407    * DOCUMENT ME!
408    * 
409    * @param e
410    *          DOCUMENT ME!
411    */
412   public void png_actionPerformed(ActionEvent e)
413   {
414     makePDBImage(jalview.util.ImageMaker.PNG);
415   }
416
417   void makePDBImage(int type)
418   {
419     int width = getWidth();
420     int height = getHeight();
421
422     jalview.util.ImageMaker im;
423
424     if (type == jalview.util.ImageMaker.PNG)
425     {
426       im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
427               "Make PNG image from view", width, height, null, null);
428     }
429     else
430     {
431       im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
432               "Make EPS file from view", width, height, null, this
433                       .getTitle());
434     }
435
436     if (im.getGraphics() != null)
437     {
438       Rectangle rect = new Rectangle(width, height);
439       viewer.renderScreenImage(im.getGraphics(), rect.getSize(), rect);
440       im.writeImage();
441     }
442   }
443
444   public void seqColour_actionPerformed(ActionEvent actionEvent)
445   {
446     lastCommand = null;
447     colourBySequence = seqColour.isSelected();
448     colourBySequence(ap.alignFrame.alignPanel);
449   }
450
451   public void chainColour_actionPerformed(ActionEvent actionEvent)
452   {
453     colourBySequence = false;
454     seqColour.setSelected(false);
455     viewer.evalStringQuiet("select *;color chain");
456   }
457
458   public void chargeColour_actionPerformed(ActionEvent actionEvent)
459   {
460     colourBySequence = false;
461     seqColour.setSelected(false);
462     viewer.evalStringQuiet("select *;color white;select ASP,GLU;color red;"
463             + "select LYS,ARG;color blue;select CYS;color yellow");
464   }
465
466   public void zappoColour_actionPerformed(ActionEvent actionEvent)
467   {
468     setJalviewColourScheme(new ZappoColourScheme());
469   }
470
471   public void taylorColour_actionPerformed(ActionEvent actionEvent)
472   {
473     setJalviewColourScheme(new TaylorColourScheme());
474   }
475
476   public void hydroColour_actionPerformed(ActionEvent actionEvent)
477   {
478     setJalviewColourScheme(new HydrophobicColourScheme());
479   }
480
481   public void helixColour_actionPerformed(ActionEvent actionEvent)
482   {
483     setJalviewColourScheme(new HelixColourScheme());
484   }
485
486   public void strandColour_actionPerformed(ActionEvent actionEvent)
487   {
488     setJalviewColourScheme(new StrandColourScheme());
489   }
490
491   public void turnColour_actionPerformed(ActionEvent actionEvent)
492   {
493     setJalviewColourScheme(new TurnColourScheme());
494   }
495
496   public void buriedColour_actionPerformed(ActionEvent actionEvent)
497   {
498     setJalviewColourScheme(new BuriedColourScheme());
499   }
500
501   public void setJalviewColourScheme(ColourSchemeI cs)
502   {
503     colourBySequence = false;
504     seqColour.setSelected(false);
505
506     if (cs == null)
507       return;
508
509     String res;
510     int index;
511     Color col;
512
513     Enumeration en = ResidueProperties.aa3Hash.keys();
514     StringBuffer command = new StringBuffer("select *;color white;");
515     while (en.hasMoreElements())
516     {
517       res = en.nextElement().toString();
518       index = ((Integer) ResidueProperties.aa3Hash.get(res)).intValue();
519       if (index > 20)
520         continue;
521
522       col = cs.findColour(ResidueProperties.aa[index].charAt(0));
523
524       command.append("select " + res + ";color[" + col.getRed() + ","
525               + col.getGreen() + "," + col.getBlue() + "];");
526     }
527
528     viewer.evalStringQuiet(command.toString());
529   }
530
531   public void userColour_actionPerformed(ActionEvent actionEvent)
532   {
533     new UserDefinedColours(this, null);
534   }
535
536   public void backGround_actionPerformed(ActionEvent actionEvent)
537   {
538     java.awt.Color col = JColorChooser.showDialog(this,
539             "Select Background Colour", null);
540
541     if (col != null)
542     {
543       viewer.evalStringQuiet("background [" + col.getRed() + ","
544               + col.getGreen() + "," + col.getBlue() + "];");
545     }
546   }
547
548   public void jmolHelp_actionPerformed(ActionEvent actionEvent)
549   {
550     try
551     {
552       jalview.util.BrowserLauncher
553               .openURL("http://jmol.sourceforge.net/docs/JmolUserGuide/");
554     } catch (Exception ex)
555     {
556     }
557   }
558
559   // ////////////////////////////////
560   // /StructureListener
561   public String getPdbFile()
562   {
563     return pdbentry.getFile();
564   }
565
566   Pattern pattern = Pattern
567           .compile("\\[(.*)\\]([0-9]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?");
568
569   String lastMessage;
570
571   public void mouseOverStructure(int atomIndex, String strInfo)
572   {
573     Matcher matcher = pattern.matcher(strInfo);
574     matcher.find();
575     matcher.group(1);
576     int pdbResNum = Integer.parseInt(matcher.group(2));
577     String chainId = matcher.group(3);
578
579     if (chainId != null)
580       chainId = chainId.substring(1, chainId.length());
581     else
582     {
583       chainId = " ";
584     }
585
586     if (lastMessage == null || !lastMessage.equals(strInfo))
587     {
588       ssm.mouseOverStructure(pdbResNum, chainId, pdbentry.getFile());
589     }
590     lastMessage = strInfo;
591   }
592
593   StringBuffer resetLastRes = new StringBuffer();
594
595   StringBuffer eval = new StringBuffer();
596
597   public void highlightAtom(int atomIndex, int pdbResNum, String chain,
598           String pdbfile)
599   {
600     // TODO: rna: remove CA dependency in select string
601     if (!pdbfile.equals(pdbentry.getFile()))
602       return;
603
604     if (resetLastRes.length() > 0)
605     {
606       viewer.evalStringQuiet(resetLastRes.toString());
607     }
608
609     eval.setLength(0);
610     eval.append("select " + pdbResNum);
611
612     resetLastRes.setLength(0);
613     resetLastRes.append("select " + pdbResNum);
614
615     eval.append(":");
616     resetLastRes.append(":");
617     if (!chain.equals(" "))
618     {
619       eval.append(chain);
620       resetLastRes.append(chain);
621     }
622
623     eval.append(";wireframe 100;" + eval.toString() + " and not hetero;"); // ".*;");
624
625     resetLastRes.append(";wireframe 0;" + resetLastRes.toString()
626     // + ".*;spacefill 0;");
627             + " and not hetero;spacefill 0;");
628
629     eval.append("spacefill 200;select none");
630     // System.out.println("jmol:\n"+eval+"\n");
631     viewer.evalStringQuiet(eval.toString());
632   }
633
634   public Color getColour(int atomIndex, int pdbResNum, String chain,
635           String pdbfile)
636   {
637     if (!pdbfile.equals(pdbentry.getFile()))
638       return null;
639
640     return new Color(viewer.getAtomArgb(atomIndex));
641   }
642
643   public void updateColours(Object source)
644   {
645     colourBySequence((AlignmentPanel) source);
646   }
647
648   // End StructureListener
649   // //////////////////////////
650
651   String lastCommand;
652
653   FeatureRenderer fr = null;
654
655   public void colourBySequence(AlignmentPanel sourceap)
656   {
657     this.ap = sourceap;
658
659     if (!colourBySequence || ap.alignFrame.getCurrentView() != ap.av)
660       return;
661
662     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
663
664     if (mapping.length < 1)
665       return;
666
667     SequenceRenderer sr = new SequenceRenderer(ap.av);
668
669     boolean showFeatures = false;
670
671     if (ap.av.showSequenceFeatures)
672     {
673       showFeatures = true;
674       if (fr == null)
675       {
676         fr = new jalview.gui.FeatureRenderer(ap);
677       }
678
679       fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
680     }
681
682     StringBuffer command = new StringBuffer();
683
684     int lastPos = -1;
685     for (int sp, s = 0; s < sequence.length; s++)
686     {
687       for (int m = 0; m < mapping.length; m++)
688       {
689         if (mapping[m].getSequence() == sequence[s]
690                 && (sp = ap.av.alignment.findIndex(sequence[s])) > -1)
691         {
692           SequenceI asp = ap.av.alignment.getSequenceAt(sp);
693           for (int r = 0; r < asp.getLength(); r++)
694           {
695             // No mapping to gaps in sequence.
696             if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
697             {
698               continue;
699             }
700             int pos = mapping[m].getPDBResNum(asp.findPosition(r));
701
702             if (pos < 1 || pos == lastPos)
703               continue;
704
705             lastPos = pos;
706
707             Color col = sr.getResidueBoxColour(asp, r);
708
709             if (showFeatures)
710               col = fr.findFeatureColour(col, asp, r);
711
712             if (command.toString().endsWith(
713                     ":" + mapping[m].getChain() + ";color[" + col.getRed()
714                             + "," + col.getGreen() + "," + col.getBlue()
715                             + "]"))
716             {
717               command = condenseCommand(command, pos);
718               continue;
719             }
720
721             command.append(";select " + pos);
722
723             if (!mapping[m].getChain().equals(" "))
724             {
725               command.append(":" + mapping[m].getChain());
726             }
727
728             command.append(";color[" + col.getRed() + "," + col.getGreen()
729                     + "," + col.getBlue() + "]");
730
731           }
732           break;
733         }
734       }
735     }
736
737     if (lastCommand == null || !lastCommand.equals(command.toString()))
738     {
739       viewer.evalStringQuiet(command.toString());
740     }
741     lastCommand = command.toString();
742   }
743
744   StringBuffer condenseCommand(StringBuffer command, int pos)
745   {
746     StringBuffer sb = new StringBuffer(command.substring(0, command
747             .lastIndexOf("select") + 7));
748
749     command.delete(0, sb.length());
750
751     String start;
752
753     if (command.indexOf("-") > -1)
754     {
755       start = command.substring(0, command.indexOf("-"));
756     }
757     else
758     {
759       start = command.substring(0, command.indexOf(":"));
760     }
761
762     sb.append(start + "-" + pos + command.substring(command.indexOf(":")));
763
764     return sb;
765   }
766
767   // ///////////////////////////////
768   // JmolStatusListener
769
770   public String eval(String strEval)
771   {
772     // System.out.println(strEval);
773     // "# 'eval' is implemented only for the applet.";
774     return null;
775   }
776
777   public void createImage(String file, String type, int quality)
778   {
779     System.out.println("JMOL CREATE IMAGE");
780   }
781
782   public void setCallbackFunction(String callbackType,
783           String callbackFunction)
784   {
785   }
786
787   public void notifyFileLoaded(String fullPathName, String fileName,
788           String modelName, Object clientFile, String errorMsg)
789   {
790     if (errorMsg != null)
791     {
792       fileLoadingError = errorMsg;
793       repaint();
794       return;
795     }
796
797     fileLoadingError = null;
798
799     if (fileName != null)
800     {
801
802       // FILE LOADED OK
803       ssm = StructureSelectionManager.getStructureSelectionManager();
804       MCview.PDBfile pdbFile = ssm.setMapping(sequence, chains, pdbentry
805               .getFile(), AppletFormatAdapter.FILE);
806       ssm.addStructureViewerListener(this);
807       Vector chains = new Vector();
808       for (int i = 0; i < pdbFile.chains.size(); i++)
809       {
810         chains
811                 .addElement(((MCview.PDBChain) pdbFile.chains.elementAt(i)).id);
812       }
813       setChainMenuItems(chains);
814
815       jmolpopup.updateComputedMenus();
816
817       if (!loadingFromArchive)
818       {
819         viewer
820                 .evalStringQuiet("select backbone;restrict;cartoon;wireframe off;spacefill off");
821
822         colourBySequence(ap);
823       }
824       if (fr != null)
825         fr.featuresAdded();
826
827       loadingFromArchive = false;
828     }
829     else
830       return;
831   }
832
833   public void notifyFrameChanged(int frameNo)
834   {
835     boolean isAnimationRunning = (frameNo <= -2);
836   }
837
838   public void notifyScriptStart(String statusMessage, String additionalInfo)
839   {
840   }
841
842   public void sendConsoleEcho(String strEcho)
843   {
844     if (scriptWindow != null)
845       scriptWindow.sendConsoleEcho(strEcho);
846   }
847
848   public void sendConsoleMessage(String strStatus)
849   {
850     if (scriptWindow != null)
851       scriptWindow.sendConsoleMessage(strStatus);
852   }
853
854   public void notifyScriptTermination(String strStatus, int msWalltime)
855   {
856     if (scriptWindow != null)
857       scriptWindow.notifyScriptTermination(strStatus, msWalltime);
858   }
859
860   public void handlePopupMenu(int x, int y)
861   {
862     jmolpopup.show(x, y);
863   }
864
865   public void notifyNewPickingModeMeasurement(int iatom, String strMeasure)
866   {
867     notifyAtomPicked(iatom, strMeasure);
868   }
869
870   public void notifyNewDefaultModeMeasurement(int count, String strInfo)
871   {
872   }
873
874   public void notifyAtomPicked(int atomIndex, String strInfo)
875   {
876     Matcher matcher = pattern.matcher(strInfo);
877     matcher.find();
878
879     matcher.group(1);
880     String resnum = new String(matcher.group(2));
881     String chainId = matcher.group(3);
882
883     String picked = resnum;
884
885     if (chainId != null)
886       picked += (":" + chainId.substring(1, chainId.length()));
887
888     picked += ".CA";
889
890     if (!atomsPicked.contains(picked))
891     {
892       if (chainId != null)
893         viewer.evalString("select " + picked + ";label %n %r:%c");
894       else
895         viewer.evalString("select " + picked + ";label %n %r");
896       atomsPicked.addElement(picked);
897     }
898     else
899     {
900       viewer.evalString("select " + picked + ";label off");
901       atomsPicked.removeElement(picked);
902     }
903
904     if (scriptWindow != null)
905     {
906       scriptWindow.sendConsoleMessage(strInfo);
907       scriptWindow.sendConsoleMessage("\n");
908     }
909   }
910
911   public void notifyAtomHovered(int atomIndex, String strInfo)
912   {
913     mouseOverStructure(atomIndex, strInfo);
914   }
915
916   public void sendSyncScript(String script, String appletName)
917   {
918   }
919
920   public void showUrl(String url)
921   {
922   }
923
924   public void showConsole(boolean showConsole)
925   {
926     if (scriptWindow == null)
927       scriptWindow = new ScriptWindow(this);
928
929     if (showConsole)
930     {
931       if (splitPane == null)
932       {
933         splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
934         splitPane.setTopComponent(renderPanel);
935         splitPane.setBottomComponent(scriptWindow);
936         this.getContentPane().add(splitPane, BorderLayout.CENTER);
937       }
938
939       splitPane.setDividerLocation(getHeight() - 200);
940       splitPane.validate();
941     }
942     else
943     {
944       if (splitPane != null)
945         splitPane.setVisible(false);
946
947       splitPane = null;
948
949       this.getContentPane().add(renderPanel, BorderLayout.CENTER);
950     }
951
952     validate();
953   }
954
955   public float functionXY(String functionName, int x, int y)
956   {
957     return 0;
958   }
959
960   // /End JmolStatusListener
961   // /////////////////////////////
962
963   class RenderPanel extends JPanel
964   {
965     final Dimension currentSize = new Dimension();
966
967     final Rectangle rectClip = new Rectangle();
968
969     public void paintComponent(Graphics g)
970     {
971       getSize(currentSize);
972       g.getClipBounds(rectClip);
973
974       if (viewer == null)
975       {
976         g.setColor(Color.black);
977         g.fillRect(0, 0, currentSize.width, currentSize.height);
978         g.setColor(Color.white);
979         g.setFont(new Font("Verdana", Font.BOLD, 14));
980         g.drawString("Retrieving PDB data....", 20, currentSize.height / 2);
981       }
982       else if (fileLoadingError != null)
983       {
984         g.setColor(Color.black);
985         g.fillRect(0, 0, currentSize.width, currentSize.height);
986         g.setColor(Color.white);
987         g.setFont(new Font("Verdana", Font.BOLD, 14));
988         g.drawString("Error loading file..." + pdbentry.getId(), 20,
989                 currentSize.height / 2);
990       }
991       else
992       {
993         viewer.renderScreenImage(g, currentSize, rectClip);
994       }
995     }
996   }
997
998   String viewId = null;
999
1000   public String getViewId()
1001   {
1002     if (viewId == null)
1003     {
1004       viewId = System.currentTimeMillis() + "." + this.hashCode();
1005     }
1006     return viewId;
1007   }
1008
1009 }