2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.ItemEvent;
28 import java.awt.event.ItemListener;
29 import java.io.BufferedReader;
31 import java.io.FileOutputStream;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.PrintWriter;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Random;
39 import java.util.Vector;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JCheckBoxMenuItem;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.JRadioButtonMenuItem;
46 import javax.swing.event.MenuEvent;
47 import javax.swing.event.MenuListener;
49 import jalview.api.AlignmentViewPanel;
50 import jalview.bin.Cache;
51 import jalview.datamodel.AlignmentI;
52 import jalview.datamodel.PDBEntry;
53 import jalview.datamodel.SequenceI;
54 import jalview.gui.JalviewColourChooser.ColourChooserListener;
55 import jalview.gui.StructureViewer.ViewerType;
56 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
57 import jalview.io.DataSourceType;
58 import jalview.io.JalviewFileChooser;
59 import jalview.io.JalviewFileView;
60 import jalview.jbgui.GStructureViewer;
61 import jalview.schemes.ColourSchemeI;
62 import jalview.schemes.ColourSchemes;
63 import jalview.structure.StructureMapping;
64 import jalview.structures.models.AAStructureBindingModel;
65 import jalview.util.BrowserLauncher;
66 import jalview.util.MessageManager;
67 import jalview.ws.dbsources.Pdb;
70 * Base class with common functionality for JMol, Chimera or other structure
76 public abstract class StructureViewerBase extends GStructureViewer
77 implements Runnable, ViewSetProvider
79 private static final String UNMAPPED = "(unmapped)";
82 * names for colour options (additional to Jalview colour schemes)
86 BySequence, ByChain, ChargeCysteine, ByViewer
90 * list of sequenceSet ids associated with the view
92 protected List<String> _aps = new ArrayList<>();
95 * list of alignment panels to use for superposition
97 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
100 * list of alignment panels that are used for colouring structures by aligned
103 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
105 private String viewId = null;
107 private AlignmentPanel ap;
109 protected boolean alignAddedStructures = false;
111 protected volatile boolean _started = false;
113 protected volatile boolean addingStructures = false;
115 protected Thread worker = null;
117 protected boolean allChainsSelected = false;
119 protected JMenu viewSelectionMenu;
122 * set after sequence colouring has been applied for this structure viewer.
123 * used to determine if the final sequence/structure mapping has been
126 protected volatile boolean seqColoursApplied = false;
128 private IProgressIndicator progressBar = null;
130 private Random random = new Random();
133 * Default constructor
135 public StructureViewerBase()
141 * @return true if added structures should be aligned to existing one(s)
144 public boolean isAlignAddedStructures()
146 return alignAddedStructures;
152 * if added structures should be aligned to existing one(s)
155 public void setAlignAddedStructures(boolean alignAdded)
157 alignAddedStructures = alignAdded;
163 * @return true if this Jmol instance is linked with the given alignPanel
165 public boolean isLinkedWith(AlignmentPanel ap2)
167 return _aps.contains(ap2.av.getSequenceSetId());
170 public boolean isUsedforaligment(AlignmentViewPanel ap2)
173 return (_alignwith != null) && _alignwith.contains(ap2);
177 public boolean isUsedForColourBy(AlignmentViewPanel avp)
179 return (_colourwith != null) && _colourwith.contains(avp);
184 * @return TRUE if the view is NOT being coloured by the alignment colours.
186 public boolean isColouredByViewer()
188 return !getBinding().isColourBySequence();
191 public String getViewId()
195 viewId = System.currentTimeMillis() + "." + this.hashCode();
200 protected void setViewId(String viewId)
202 this.viewId = viewId;
205 protected void buildActionMenu()
207 if (_alignwith == null)
209 _alignwith = new Vector<>();
211 if (_alignwith.size() == 0 && ap != null)
216 for (Component c : viewerActionMenu.getMenuComponents())
218 if (c != alignStructs)
220 viewerActionMenu.remove((JMenuItem) c);
226 public AlignmentPanel getAlignmentPanel()
231 protected void setAlignmentPanel(AlignmentPanel alp)
237 public AlignmentPanel[] getAllAlignmentPanels()
239 AlignmentPanel[] t, list = new AlignmentPanel[0];
240 for (String setid : _aps)
242 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
245 t = new AlignmentPanel[list.length + panels.length];
246 System.arraycopy(list, 0, t, 0, list.length);
247 System.arraycopy(panels, 0, t, list.length, panels.length);
256 * set the primary alignmentPanel reference and add another alignPanel to the
257 * list of ones to use for colouring and aligning
261 public void addAlignmentPanel(AlignmentPanel nap)
263 if (getAlignmentPanel() == null)
265 setAlignmentPanel(nap);
267 if (!_aps.contains(nap.av.getSequenceSetId()))
269 _aps.add(nap.av.getSequenceSetId());
274 * remove any references held to the given alignment panel
279 public void removeAlignmentPanel(AlignmentViewPanel nap)
283 _alignwith.remove(nap);
284 _colourwith.remove(nap);
285 if (getAlignmentPanel() == nap)
287 setAlignmentPanel(null);
288 for (AlignmentPanel aps : getAllAlignmentPanels())
292 setAlignmentPanel(aps);
297 } catch (Exception ex)
300 if (getAlignmentPanel() != null)
306 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
308 addAlignmentPanel(nap);
309 if (!_alignwith.contains(nap))
315 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
317 if (_alignwith.contains(nap))
319 _alignwith.remove(nap);
323 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
324 boolean enableColourBySeq)
326 useAlignmentPanelForColourbyseq(nap);
327 getBinding().setColourBySequence(enableColourBySeq);
328 seqColour.setSelected(enableColourBySeq);
329 viewerColour.setSelected(!enableColourBySeq);
332 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
334 addAlignmentPanel(nap);
335 if (!_colourwith.contains(nap))
337 _colourwith.add(nap);
341 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
343 if (_colourwith.contains(nap))
345 _colourwith.remove(nap);
349 public abstract ViewerType getViewerType();
352 * add a new structure (with associated sequences and chains) to this viewer,
353 * retrieving it if necessary first.
359 * if true, new structure(s) will be aligned using associated
363 protected void addStructure(final PDBEntry pdbentry,
364 final SequenceI[] seqs, final String[] chains,
365 final IProgressIndicator alignFrame)
367 if (pdbentry.getFile() == null)
369 if (worker != null && worker.isAlive())
371 // a retrieval is in progress, wait around and add ourselves to the
373 new Thread(new Runnable()
378 while (worker != null && worker.isAlive() && _started)
382 Thread.sleep(100 + ((int) Math.random() * 100));
384 } catch (Exception e)
388 // and call ourselves again.
389 addStructure(pdbentry, seqs, chains, alignFrame);
395 // otherwise, start adding the structure.
396 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
398 { seqs }, new String[][] { chains });
399 addingStructures = true;
401 worker = new Thread(this);
406 protected boolean hasPdbId(String pdbId)
408 return getBinding().hasPdbId(pdbId);
412 * Returns a list of any viewer of the instantiated type. The list is
413 * restricted to those linked to the given alignment panel if it is not null.
415 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
417 return Desktop.instance.getStructureViewers(alp, this.getClass());
421 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
422 String[] chains, final AlignmentViewPanel apanel, String pdbId)
425 * JAL-1742 exclude view with this structure already mapped (don't offer
426 * to align chain B to chain A of the same structure); code may defend
427 * against this possibility before we reach here
433 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error if this
435 useAlignmentPanelForSuperposition(alignPanel);
436 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
440 * Adds mappings for the given sequences to an already opened PDB structure,
441 * and updates any viewers that have the PDB file
448 public void addSequenceMappingsToStructure(SequenceI[] seq,
449 String[] chains, final AlignmentViewPanel alpanel,
452 AlignmentPanel apanel = (AlignmentPanel) alpanel;
454 // TODO : Fix multiple seq to one chain issue here.
456 * create the mappings
458 apanel.getStructureSelectionManager().setMapping(seq, chains,
459 pdbFilename, DataSourceType.FILE, getProgressIndicator());
462 * alert the FeatureRenderer to show new (PDB RESNUM) features
464 if (apanel.getSeqPanel().seqCanvas.fr != null)
466 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
467 // note - we don't do a refresh for structure here because we do it
468 // explicitly for all panels later on
469 apanel.paintAlignment(true, false);
473 * add the sequences to any other viewers (of the same type) for this pdb
476 // JBPNOTE: this looks like a binding routine, rather than a gui routine
477 for (StructureViewerBase viewer : getViewersFor(null))
479 AAStructureBindingModel bindingModel = viewer.getBinding();
480 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
482 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
484 bindingModel.addSequence(pe, seq);
485 viewer.addAlignmentPanel(apanel);
487 * add it to the set of alignments used for colouring structure by
490 viewer.useAlignmentPanelForColourbyseq(apanel);
491 viewer.buildActionMenu();
492 apanel.getStructureSelectionManager()
493 .sequenceColoursChanged(apanel);
501 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
502 final AlignmentViewPanel apanel, String pdbId)
504 String alreadyMapped = apanel.getStructureSelectionManager()
505 .alreadyMappedToFile(pdbId);
507 if (alreadyMapped == null)
512 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
517 * Add menu items to the 'Show Chains' sub-menu, given a list of values formatted as "pdbId:chainCode"
520 void setChainMenuItems(List<String> chainNames)
522 chainMenu.removeAll();
523 if (chainNames == null || chainNames.isEmpty())
529 * add the 'All' menu item
531 JMenuItem menuItem = new JMenuItem(
532 MessageManager.getString("label.all"));
533 menuItem.addActionListener(new ActionListener()
536 public void actionPerformed(ActionEvent evt)
538 allChainsSelected = true;
539 for (int i = 0; i < chainMenu.getItemCount(); i++)
541 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
543 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
546 showSelectedChains();
547 allChainsSelected = false;
550 chainMenu.add(menuItem);
553 * add a menu item for each structure and chain
555 Collections.sort(chainNames);
556 boolean hideUnmapped = getBinding().isShowAlignmentOnly();
557 for (String chain : chainNames)
559 String[] tokens = chain.split(":");
560 String pdbId = tokens[0];
561 String chainCode = tokens[1];
562 String seqName = getSequenceNameForChain(chain);
563 boolean isUnmapped = seqName == null;
568 int nameLength = seqName.length();
571 seqName = seqName.substring(0, 8) + "..."
572 + seqName.substring(nameLength - 8, nameLength);
574 String text = chain + " " + seqName;
575 menuItem = new JCheckBoxMenuItem(text, true);
578 * set the item's state _before_ adding its listener!
580 menuItem.setSelected(getBinding().isShowChain(pdbId, chainCode));
581 if (hideUnmapped && isUnmapped)
583 menuItem.setEnabled(false);
585 menuItem.addItemListener(new ItemListener()
588 public void itemStateChanged(ItemEvent evt)
590 if (!allChainsSelected)
592 showSelectedChains();
596 chainMenu.add(menuItem);
601 * Answers the name of the sequence mapped to the given chain (formatted as
602 * pdbId:chainId, e.g. 1A70:A). Answers null if no mapped sequence is found. If
603 * more than one sequence is matched, just answers the name of the first one
609 private String getSequenceNameForChain(String chain)
611 String[] tokens = chain.split(":");
612 String pdbId = tokens[0];
613 String chainId = tokens[1];
614 List<StructureMapping> mappings = getBinding().getSsm()
615 .getMappingForChain(pdbId, chainId);
616 return mappings.isEmpty() ? null
617 : mappings.get(0).getSequence().getName();
621 * Action on selecting one of Jalview's registered colour schemes
624 public void changeColour_actionPerformed(String colourSchemeName)
626 AlignmentI al = getAlignmentPanel().av.getAlignment();
627 ColourSchemeI cs = ColourSchemes.getInstance()
628 .getColourScheme(colourSchemeName, getAlignmentPanel().av, al,
630 getBinding().colourByJalviewColourScheme(cs);
634 * Builds the colour menu
636 protected void buildColourMenu()
638 colourMenu.removeAll();
639 AlignmentI al = getAlignmentPanel().av.getAlignment();
642 * add colour by sequence, by chain, by charge and cysteine
644 colourMenu.add(seqColour);
645 colourMenu.add(chainColour);
646 colourMenu.add(chargeColour);
647 chargeColour.setEnabled(!al.isNucleotide());
650 * add all 'simple' (per-residue) colour schemes registered to Jalview
652 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
656 * add 'colour by viewer' (menu item text is set in subclasses)
658 viewerColour.setSelected(false);
659 viewerColour.addActionListener(new ActionListener()
662 public void actionPerformed(ActionEvent actionEvent)
664 viewerColour_actionPerformed();
667 colourMenu.add(viewerColour);
670 * add 'set background colour'
672 JMenuItem backGround = new JMenuItem();
674 .setText(MessageManager.getString("action.background_colour"));
675 backGround.addActionListener(new ActionListener()
678 public void actionPerformed(ActionEvent actionEvent)
680 background_actionPerformed();
683 colourMenu.add(backGround);
686 * add colour buttons to a group so their selection is
687 * mutually exclusive (background colour is a separate option)
689 itemGroup.add(seqColour);
690 itemGroup.add(chainColour);
691 itemGroup.add(chargeColour);
692 itemGroup.add(viewerColour);
696 * Construct menu items
698 protected void initMenus()
700 AAStructureBindingModel binding = getBinding();
702 seqColour = new JRadioButtonMenuItem();
703 seqColour.setText(MessageManager.getString("action.by_sequence"));
704 seqColour.setName(ViewerColour.BySequence.name());
705 seqColour.setSelected(binding.isColourBySequence());
706 seqColour.addActionListener(new ActionListener()
709 public void actionPerformed(ActionEvent actionEvent)
711 seqColour_actionPerformed();
715 chainColour = new JRadioButtonMenuItem();
716 chainColour.setText(MessageManager.getString("action.by_chain"));
717 chainColour.setName(ViewerColour.ByChain.name());
718 chainColour.addActionListener(new ActionListener()
721 public void actionPerformed(ActionEvent actionEvent)
723 chainColour_actionPerformed();
727 chargeColour = new JRadioButtonMenuItem();
728 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
729 chargeColour.setName(ViewerColour.ChargeCysteine.name());
730 chargeColour.addActionListener(new ActionListener()
733 public void actionPerformed(ActionEvent actionEvent)
735 chargeColour_actionPerformed();
739 viewerColour = new JRadioButtonMenuItem();
741 .setText(MessageManager.getString("label.colour_with_viewer"));
742 viewerColour.setToolTipText(MessageManager
743 .getString("label.let_viewer_manage_structure_colours"));
744 viewerColour.setName(ViewerColour.ByViewer.name());
745 viewerColour.setSelected(!binding.isColourBySequence());
747 if (_colourwith == null)
749 _colourwith = new Vector<>();
751 if (_alignwith == null)
753 _alignwith = new Vector<>();
756 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
757 MessageManager.getString("label.colour_by"), this, _colourwith,
761 public void itemStateChanged(ItemEvent e)
763 if (!seqColour.isSelected())
769 // update the viewer display now.
770 seqColour_actionPerformed();
774 viewMenu.add(seqColourBy);
776 showAlignmentOnly = new JCheckBoxMenuItem(
777 MessageManager.getString("label.show_alignment_only"));
778 showAlignmentOnly.addActionListener(new ActionListener()
781 public void actionPerformed(ActionEvent e)
783 hideHiddenRegions.setEnabled(showAlignmentOnly.isSelected());
784 getBinding().setShowAlignmentOnly(showAlignmentOnly.isSelected());
785 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
787 updateTitleAndMenus(); // to add or remove unmapped chain menu items
790 viewMenu.add(showAlignmentOnly);
792 hideHiddenRegions = new JCheckBoxMenuItem(
793 MessageManager.getString("label.hide_hidden_regions"));
794 hideHiddenRegions.setEnabled(false);
795 hideHiddenRegions.addActionListener(new ActionListener()
798 public void actionPerformed(ActionEvent e)
800 getBinding().setHideHiddenRegions(hideHiddenRegions.isSelected());
801 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
805 viewMenu.add(hideHiddenRegions);
806 viewMenu.add(fitToWindow);
808 final ItemListener handler = new ItemListener()
811 public void itemStateChanged(ItemEvent e)
813 if (_alignwith.isEmpty())
815 alignStructs.setEnabled(false);
816 alignStructs.setToolTipText(null);
820 alignStructs.setEnabled(true);
821 alignStructs.setToolTipText(MessageManager.formatMessage(
822 "label.align_structures_using_linked_alignment_views",
827 viewSelectionMenu = new ViewSelectionMenu(
828 MessageManager.getString("label.superpose_with"), this,
829 _alignwith, handler);
830 handler.itemStateChanged(null);
831 viewerActionMenu.add(viewSelectionMenu, 0);
832 viewerActionMenu.addMenuListener(new MenuListener()
835 public void menuSelected(MenuEvent e)
837 handler.itemStateChanged(null);
841 public void menuDeselected(MenuEvent e)
846 public void menuCanceled(MenuEvent e)
851 viewerActionMenu.setText(getViewerName());
852 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
859 * Sends commands to the structure viewer to superimpose structures based on
860 * currently associated alignments. May optionally return an error message for
864 protected String alignStructsWithAllAlignPanels()
866 if (getAlignmentPanel() == null)
871 if (_alignwith.size() == 0)
873 _alignwith.add(getAlignmentPanel());
879 reply = getBinding().superposeStructures(_alignwith);
880 if (reply != null && !reply.isEmpty())
882 String text = MessageManager
883 .formatMessage("error.superposition_failed", reply);
884 statusBar.setText(text);
886 } catch (Exception e)
888 StringBuilder sb = new StringBuilder();
889 for (AlignmentViewPanel alignPanel : _alignwith)
891 sb.append("'").append(alignPanel.getViewName()).append("' ");
893 Cache.log.info("Couldn't align structures with the " + sb.toString()
894 + "associated alignment panels.", e);
900 * Opens a colour chooser dialog, and applies the chosen colour to the
901 * background of the structure viewer
904 public void background_actionPerformed()
906 String ttl = MessageManager.getString("label.select_background_colour");
907 ColourChooserListener listener = new ColourChooserListener()
910 public void colourSelected(Color c)
912 getBinding().setBackgroundColour(c);
915 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
919 public void viewerColour_actionPerformed()
921 if (viewerColour.isSelected())
923 // disable automatic sequence colouring.
924 getBinding().setColourBySequence(false);
929 public void chainColour_actionPerformed()
931 chainColour.setSelected(true);
932 getBinding().colourByChain();
936 public void chargeColour_actionPerformed()
938 chargeColour.setSelected(true);
939 getBinding().colourByCharge();
943 public void seqColour_actionPerformed()
945 AAStructureBindingModel binding = getBinding();
946 binding.setColourBySequence(seqColour.isSelected());
947 if (_colourwith == null)
949 _colourwith = new Vector<>();
951 if (binding.isColourBySequence())
953 if (!binding.isLoadingFromArchive())
955 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
957 // Make the currently displayed alignment panel the associated view
958 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
961 // Set the colour using the current view for the associated alignframe
962 for (AlignmentViewPanel avp : _colourwith)
964 binding.updateStructureColours(avp);
966 seqColoursApplied = true;
971 public void pdbFile_actionPerformed()
973 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
974 JalviewFileChooser chooser = new JalviewFileChooser(
975 Cache.getProperty("LAST_DIRECTORY"));
977 chooser.setFileView(new JalviewFileView());
978 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
979 chooser.setToolTipText(MessageManager.getString("action.save"));
981 int value = chooser.showSaveDialog(this);
983 if (value == JalviewFileChooser.APPROVE_OPTION)
985 BufferedReader in = null;
988 // TODO: cope with multiple PDB files in view
989 in = new BufferedReader(
990 new FileReader(getBinding().getStructureFiles()[0]));
991 File outFile = chooser.getSelectedFile();
993 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
995 while ((data = in.readLine()) != null)
997 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
1003 } catch (Exception ex)
1005 ex.printStackTrace();
1013 } catch (IOException e)
1023 public void viewMapping_actionPerformed()
1025 CutAndPasteTransfer cap = new CutAndPasteTransfer();
1028 cap.appendText(getBinding().printMappings());
1029 } catch (OutOfMemoryError e)
1032 "composing sequence-structure alignments for display in text box.",
1037 Desktop.addInternalFrame(cap,
1038 MessageManager.getString("label.pdb_sequence_mapping"), 550,
1042 protected abstract String getViewerName();
1045 * Configures the title and menu items of the viewer panel.
1048 public void updateTitleAndMenus()
1050 AAStructureBindingModel binding = getBinding();
1051 if (binding.hasFileLoadingError())
1056 synchronized (chainMenu)
1058 setChainMenuItems(binding.getChainNames());
1061 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1064 * enable 'Superpose with' if more than one mapped structure
1066 viewSelectionMenu.setEnabled(false);
1067 if (getBinding().getMappedStructureCount() > 1
1068 && getBinding().getSequence().length > 1)
1070 viewSelectionMenu.setEnabled(true);
1074 * Show action menu if it has any enabled items
1076 viewerActionMenu.setVisible(false);
1077 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1079 if (viewerActionMenu.getItem(i).isEnabled())
1081 viewerActionMenu.setVisible(true);
1086 if (!binding.isLoadingFromArchive())
1088 seqColour_actionPerformed();
1093 public String toString()
1099 public boolean hasMapping()
1101 if (worker != null && (addingStructures || _started))
1105 if (getBinding() == null)
1107 if (_aps == null || _aps.size() == 0)
1109 // viewer has been closed, but we did at some point run.
1114 String[] pdbids = getBinding().getStructureFiles();
1120 for (String pdbid:pdbids) {
1121 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1122 if (sm!=null && sm.length>0 && sm[0]!=null) {
1126 // only return true if there is a mapping for every structure file we have loaded
1127 if (p == 0 || p != pdbids.length)
1131 // and that coloring has been applied
1132 return seqColoursApplied;
1136 public void raiseViewer()
1142 public abstract AAStructureBindingModel getBinding();
1145 * Show only the selected chain(s) in the viewer
1147 protected void showSelectedChains()
1152 * refresh display without resizing - easier to see what changed
1154 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
1159 public long startProgressBar(String msg)
1161 // TODO would rather have startProgress/stopProgress as the
1162 // IProgressIndicator interface
1163 long tm = random.nextLong();
1164 if (progressBar != null)
1166 progressBar.setProgressBar(msg, tm);
1172 public void stopProgressBar(String msg, long handle)
1174 if (progressBar != null)
1176 progressBar.setProgressBar(msg, handle);
1180 protected IProgressIndicator getProgressIndicator()
1185 protected void setProgressIndicator(IProgressIndicator pi)
1190 protected void setProgressMessage(String message, long id)
1192 if (progressBar != null)
1194 progressBar.setProgressBar(message, id);
1199 public void showConsole(boolean show)
1201 // default does nothing
1205 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1206 * saved file path if successful, or null if not.
1208 * @param processingEntry
1211 protected String fetchPdbFile(PDBEntry processingEntry)
1213 String filePath = null;
1214 Pdb pdbclient = new Pdb();
1215 AlignmentI pdbseq = null;
1216 String pdbid = processingEntry.getId();
1217 long handle = System.currentTimeMillis()
1218 + Thread.currentThread().hashCode();
1221 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1223 String msg = MessageManager.formatMessage("status.fetching_pdb",
1226 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1227 // long hdl = startProgressBar(MessageManager.formatMessage(
1228 // "status.fetching_pdb", new Object[]
1232 pdbseq = pdbclient.getSequenceRecords(pdbid);
1233 } catch (Exception e)
1236 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1239 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1240 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1241 // stopProgressBar(msg, hdl);
1244 * If PDB data were saved and are not invalid (empty alignment), return the
1247 if (pdbseq != null && pdbseq.getHeight() > 0)
1249 // just use the file name from the first sequence's first PDBEntry
1250 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1251 .elementAt(0).getFile()).getAbsolutePath();
1252 processingEntry.setFile(filePath);
1258 * If supported, saves the state of the structure viewer to a temporary file
1259 * and returns the file, else returns null
1263 public File saveSession()
1265 // TODO: a wait loop to ensure the file is written fully before returning?
1266 return getBinding() == null ? null : getBinding().saveSession();
1270 * Close down this instance of Jalview's Chimera viewer, giving the user the
1271 * option to close the associated Chimera window (process). They may wish to
1272 * keep it open until they have had an opportunity to save any work.
1275 * if true, close any linked Chimera process; if false, prompt first
1278 public void closeViewer(boolean forceClose)
1280 AAStructureBindingModel binding = getBinding();
1281 if (binding != null && binding.isViewerRunning())
1285 String viewerName = getViewerName();
1286 String prompt = MessageManager
1287 .formatMessage("label.confirm_close_viewer", new Object[]
1288 { binding.getViewerTitle(viewerName, false), viewerName });
1289 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1290 int confirm = JvOptionPane.showConfirmDialog(this, prompt,
1291 MessageManager.getString("label.close_viewer"),
1292 JvOptionPane.YES_NO_CANCEL_OPTION);
1294 * abort closure if user hits escape or Cancel
1296 if (confirm == JvOptionPane.CANCEL_OPTION
1297 || confirm == JvOptionPane.CLOSED_OPTION)
1301 forceClose = confirm == JvOptionPane.YES_OPTION;
1304 if (binding != null)
1306 binding.closeViewer(forceClose);
1308 setAlignmentPanel(null);
1311 _colourwith.clear();
1312 // TODO: check for memory leaks where instance isn't finalised because jmb
1313 // holds a reference to the window
1319 public void showHelp_actionPerformed()
1323 String url = getBinding().getHelpURL();
1326 BrowserLauncher.openURL(url);
1328 } catch (IOException ex)
1331 .println("Show " + getViewerName() + " failed with: "