JAL-1807 still testing
[jalviewjs.git] / unused / appletgui / SplitFrame.java
1 package jalview.appletgui;\r
2 \r
3 import jalview.analysis.AlignmentUtils;\r
4 import jalview.api.AlignmentViewPanel;\r
5 import jalview.api.ViewStyleI;\r
6 import jalview.bin.JalviewLite;\r
7 import jalview.datamodel.AlignmentI;\r
8 import jalview.structure.StructureSelectionManager;\r
9 import jalview.viewmodel.AlignmentViewport;\r
10 \r
11 import java.awt.BorderLayout;\r
12 import java.awt.Dimension;\r
13 import java.awt.GridLayout;\r
14 \r
15 import javax.swing.JPanel;\r
16 \r
17 public class SplitFrame extends EmbmenuFrame\r
18 {\r
19   private static final long serialVersionUID = 1L;\r
20 \r
21   private AlignFrame topFrame;\r
22 \r
23   private AlignFrame bottomFrame;\r
24 \r
25   private JPanel outermost;\r
26   \r
27 \r
28   /**\r
29    * Constructor\r
30    */\r
31   public SplitFrame(AlignFrame af1, AlignFrame af2)\r
32   {\r
33     topFrame = af1;\r
34     bottomFrame = af2;\r
35     init();\r
36   }\r
37 \r
38   /**\r
39    * Creates a Panel containing two Panels, and adds the first and second\r
40    * AlignFrame's components to each. At this stage we have not yet committed to\r
41    * whether the enclosing panel will be added to this frame, for display as a\r
42    * separate frame, or added to the applet (embedded mode).\r
43    */\r
44   public void init()\r
45   {\r
46     constructSplit();\r
47 \r
48     /*\r
49      * Try to make and add dna/protein sequence mappings\r
50      */\r
51     final AlignViewport topViewport = topFrame.viewport;\r
52     final AlignViewport bottomViewport = bottomFrame.viewport;\r
53     final AlignmentI topAlignment = topViewport.getAlignment();\r
54     final AlignmentI bottomAlignment = bottomViewport.getAlignment();\r
55     AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport\r
56             : (bottomAlignment.isNucleotide() ? bottomViewport : null);\r
57     AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport\r
58             : (!bottomAlignment.isNucleotide() ? bottomViewport : null);\r
59 \r
60     boolean mapped = AlignmentUtils.mapProteinToCdna(\r
61             protein.getAlignment(), cdna.getAlignment());\r
62     if (mapped)\r
63     {\r
64       final StructureSelectionManager ssm = StructureSelectionManager\r
65               .getStructureSelectionManager(topViewport.applet);\r
66       ssm.addMappings(protein.getAlignment().getCodonFrames());\r
67       topViewport.setCodingComplement(bottomViewport);\r
68       ssm.addCommandListener(cdna);\r
69       ssm.addCommandListener(protein);\r
70     }\r
71 \r
72     /*\r
73      * Now mappings exist, can compute cDNA consensus on protein alignment\r
74      */\r
75     protein.initComplementConsensus();\r
76     AlignmentViewPanel ap = topAlignment.isNucleotide() ? bottomFrame.alignPanel\r
77             : topFrame.alignPanel;\r
78     protein.updateConsensus(ap);\r
79 \r
80     adjustLayout();\r
81   }\r
82 \r
83   /**\r
84    * \r
85    */\r
86   protected void constructSplit()\r
87   {\r
88     setMenuBar(null);\r
89     outermost = new JPanel(new GridLayout(2, 1));\r
90 \r
91     JPanel topPanel = new JPanel();\r
92     JPanel bottomPanel = new JPanel();\r
93     outermost.add(topPanel);\r
94     outermost.add(bottomPanel);\r
95 \r
96     addAlignFrameComponents(topFrame, topPanel);\r
97     addAlignFrameComponents(bottomFrame, bottomPanel);\r
98   }\r
99 \r
100   /**\r
101    * Make any adjustments to the layout\r
102    */\r
103   protected void adjustLayout()\r
104   {\r
105     AlignmentViewport cdna = topFrame.getAlignViewport().getAlignment()\r
106             .isNucleotide() ? topFrame.viewport : bottomFrame.viewport;\r
107     AlignmentViewport protein = cdna == topFrame.viewport ? bottomFrame.viewport\r
108             : topFrame.viewport;\r
109 \r
110     /*\r
111      * Ensure sequence ids are the same width for good alignment.\r
112      */\r
113     // TODO should do this via av.getViewStyle/setViewStyle\r
114     // however at present av.viewStyle is not set in IdPanel.fontChanged\r
115     int w1 = topFrame.alignPanel.idPanel.idCanvas.getWidth();\r
116     int w2 = bottomFrame.alignPanel.idPanel.idCanvas.getWidth();\r
117     int w3 = Math.max(w1, w2);\r
118     if (w1 != w3)\r
119     {\r
120       Dimension d = topFrame.alignPanel.idPanel.idCanvas.getSize();\r
121       topFrame.alignPanel.idPanel.idCanvas.setSize(new Dimension(w3,\r
122               d.height));\r
123     }\r
124     if (w2 != w3)\r
125     {\r
126       Dimension d = bottomFrame.alignPanel.idPanel.idCanvas.getSize();\r
127       bottomFrame.alignPanel.idPanel.idCanvas.setSize(new Dimension(w3,\r
128               d.height));\r
129     }\r
130 \r
131     /*\r
132      * Scale protein to either 1 or 3 times character width of dna\r
133      */\r
134     if (protein != null && cdna != null)\r
135     {\r
136       ViewStyleI vs = protein.getViewStyle();\r
137       int scale = vs.isScaleProteinAsCdna() ? 3 : 1;\r
138       vs.setCharWidth(scale * cdna.getViewStyle().getCharWidth());\r
139       protein.setViewStyle(vs);\r
140     }\r
141   }\r
142 \r
143         /**\r
144          * Add the menu bar, alignment panel and status bar from the AlignFrame to the\r
145          * panel. The menu bar is a panel 'reconstructed' from the AlignFrame's frame\r
146          * menu bar. This allows each half of the SplitFrame to have its own menu bar.\r
147          * \r
148          * @param af\r
149          * @param panel\r
150          */\r
151         private void addAlignFrameComponents(AlignFrame af, JPanel panel) {\r
152                 panel.setLayout(new BorderLayout());\r
153                 JPanel menuPanel = af.makeEmbeddedPopupMenu(af.getJMenuBar(), true, false);\r
154                 panel.add(menuPanel, BorderLayout.NORTH);\r
155                 panel.add(af.statusBar, BorderLayout.SOUTH);\r
156                 panel.add(af.alignPanel, BorderLayout.CENTER);\r
157 \r
158                 af.setSplitFrame(this);\r
159         }\r
160 \r
161   /**\r
162    * Display the content panel either as a new frame or embedded in the applet.\r
163    * \r
164    * @param embedded\r
165    * @param applet\r
166    */\r
167   public void addToDisplay(boolean embedded, JalviewLite applet)\r
168   {\r
169     createSplitFrameWindow(embedded, applet);\r
170     validate();\r
171     topFrame.alignPanel.adjustAnnotationHeight();\r
172     topFrame.alignPanel.paintAlignment(true);\r
173     bottomFrame.alignPanel.adjustAnnotationHeight();\r
174     bottomFrame.alignPanel.paintAlignment(true);\r
175   }\r
176 \r
177   /**\r
178    * Either show the content panel in this frame as a new frame, or (if\r
179    * embed=true) add it to the applet container instead.\r
180    * \r
181    * @param embed\r
182    * @param applet\r
183    */\r
184   protected void createSplitFrameWindow(boolean embed, JalviewLite applet)\r
185   {\r
186     if (embed)\r
187     {\r
188       applet.add(outermost);\r
189       applet.validate();\r
190     }\r
191     else\r
192     {\r
193       this.add(outermost);\r
194       int width = Math.max(topFrame.frameWidth, bottomFrame.frameWidth);\r
195       int height = topFrame.frameHeight + bottomFrame.frameHeight;\r
196       JalviewLite.addFrame(this, this.getTitle(), width, height);\r
197     }\r
198   }\r
199 \r
200   /**\r
201    * Returns the contained AlignFrame complementary to the one given (or null if\r
202    * no match to top or bottom component).\r
203    * \r
204    * @param af\r
205    * @return\r
206    */\r
207   public AlignFrame getComplement(AlignFrame af)\r
208   {\r
209     if (topFrame == af)\r
210     {\r
211       return bottomFrame;\r
212     }\r
213     else if (bottomFrame == af)\r
214     {\r
215       return topFrame;\r
216     }\r
217     return null;\r
218   }\r
219 }\r