JAL-1588 part work save/restore of Chimera session
[jalview.git] / src / jalview / gui / StructureViewerBase.java
1 package jalview.gui;
2
3 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
4 import jalview.jbgui.GStructureViewer;
5
6 import java.awt.Component;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Vector;
10
11 import javax.swing.JMenuItem;
12
13 /**
14  * Base class with common functionality for JMol, Chimera or other structure
15  * viewers.
16  * 
17  * @author gmcarstairs
18  *
19  */
20 public abstract class StructureViewerBase extends GStructureViewer
21         implements Runnable, ViewSetProvider
22 {
23
24   /**
25    * list of sequenceSet ids associated with the view
26    */
27   protected List<String> _aps = new ArrayList<String>();
28   /**
29    * list of alignment panels to use for superposition
30    */
31   protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
32   /**
33    * list of alignment panels that are used for colouring structures by aligned
34    * sequences
35    */
36   protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
37   private String viewId = null;
38   private AlignmentPanel ap;
39
40   /**
41    * 
42    * @param ap2
43    * @return true if this Jmol instance is linked with the given alignPanel
44    */
45   public boolean isLinkedWith(AlignmentPanel ap2)
46   {
47     return _aps.contains(ap2.av.getSequenceSetId());
48   }
49
50   public boolean isUsedforaligment(AlignmentPanel ap2)
51   {
52   
53     return (_alignwith != null) && _alignwith.contains(ap2);
54   }
55
56   public boolean isUsedforcolourby(AlignmentPanel ap2)
57   {
58     return (_colourwith != null) && _colourwith.contains(ap2);
59   }
60
61   /**
62    * 
63    * @return TRUE if the view is NOT being coloured by the alignment colours.
64    */
65   public boolean isColouredByViewer()
66   {
67     return !getBinding().isColourBySequence();
68   }
69
70   public String getViewId()
71   {
72     if (viewId == null)
73     {
74       viewId = System.currentTimeMillis() + "." + this.hashCode();
75     }
76     return viewId;
77   }
78
79   protected void setViewId(String viewId)
80   {
81     this.viewId = viewId;
82   }
83
84   public abstract String getStateInfo();
85
86   protected void buildActionMenu()
87   {
88     if (_alignwith == null)
89     {
90       _alignwith = new Vector<AlignmentPanel>();
91     }
92     if (_alignwith.size() == 0 && ap != null)
93     {
94       _alignwith.add(ap);
95     }
96     ;
97     for (Component c : viewerActionMenu.getMenuComponents())
98     {
99       if (c != alignStructs)
100       {
101         viewerActionMenu.remove((JMenuItem) c);
102       }
103     }
104   }
105
106   public AlignmentPanel getAlignmentPanel()
107   {
108     return ap;
109   }
110
111   protected void setAlignmentPanel(AlignmentPanel alp)
112   {
113     this.ap = alp;
114   }
115
116   public AlignmentPanel[] getAllAlignmentPanels()
117   {
118     AlignmentPanel[] t, list = new AlignmentPanel[0];
119     for (String setid : _aps)
120     {
121       AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
122       if (panels != null)
123       {
124         t = new AlignmentPanel[list.length + panels.length];
125         System.arraycopy(list, 0, t, 0, list.length);
126         System.arraycopy(panels, 0, t, list.length, panels.length);
127         list = t;
128       }
129     }
130   
131     return list;
132   }
133
134   /**
135    * set the primary alignmentPanel reference and add another alignPanel to the
136    * list of ones to use for colouring and aligning
137    * 
138    * @param nap
139    */
140   public void addAlignmentPanel(AlignmentPanel nap)
141   {
142     if (getAlignmentPanel() == null)
143     {
144       setAlignmentPanel(nap);
145     }
146     if (!_aps.contains(nap.av.getSequenceSetId()))
147     {
148       _aps.add(nap.av.getSequenceSetId());
149     }
150   }
151
152   /**
153    * remove any references held to the given alignment panel
154    * 
155    * @param nap
156    */
157   public void removeAlignmentPanel(AlignmentPanel nap)
158   {
159     try
160     {
161       _alignwith.remove(nap);
162       _colourwith.remove(nap);
163       if (getAlignmentPanel() == nap)
164       {
165         setAlignmentPanel(null);
166         for (AlignmentPanel aps : getAllAlignmentPanels())
167         {
168           if (aps != nap)
169           {
170             setAlignmentPanel(aps);
171             break;
172           }
173         }
174       }
175     } catch (Exception ex)
176     {
177     }
178     if (getAlignmentPanel() != null)
179     {
180       buildActionMenu();
181     }
182   }
183
184   public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
185   {
186     addAlignmentPanel(nap);
187     if (!_alignwith.contains(nap))
188     {
189       _alignwith.add(nap);
190     }
191   }
192
193   public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
194   {
195     if (_alignwith.contains(nap))
196     {
197       _alignwith.remove(nap);
198     }
199   }
200
201   public void useAlignmentPanelForColourbyseq(AlignmentPanel nap, boolean enableColourBySeq)
202   {
203     useAlignmentPanelForColourbyseq(nap);
204     getBinding().setColourBySequence(enableColourBySeq);
205     seqColour.setSelected(enableColourBySeq);
206     viewerColour.setSelected(!enableColourBySeq);
207   }
208
209   public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
210   {
211     addAlignmentPanel(nap);
212     if (!_colourwith.contains(nap))
213     {
214       _colourwith.add(nap);
215     }
216   }
217
218   public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
219   {
220     if (_colourwith.contains(nap))
221     {
222       _colourwith.remove(nap);
223     }
224   }
225 }