a012092ef6aeac3da67b362c9caa00aa73dea3ec
[jalview.git] / src / jalview / appletgui / SplitFrame.java
1 package jalview.appletgui;
2
3 import jalview.analysis.AlignmentUtils;
4 import jalview.analysis.AlignmentUtils.MappingResult;
5 import jalview.api.ViewStyleI;
6 import jalview.bin.JalviewLite;
7 import jalview.datamodel.AlignmentI;
8 import jalview.structure.StructureSelectionManager;
9
10 import java.awt.BorderLayout;
11 import java.awt.GridLayout;
12 import java.awt.Panel;
13
14 public class SplitFrame extends EmbmenuFrame
15 {
16   private static final long serialVersionUID = 1L;
17
18   private AlignFrame topFrame;
19
20   private AlignFrame bottomFrame;
21
22   private Panel outermost;
23
24   /**
25    * Constructor
26    */
27   public SplitFrame(AlignFrame af1, AlignFrame af2)
28   {
29     topFrame = af1;
30     bottomFrame = af2;
31     init();
32   }
33
34   /**
35    * Creates a Panel containing two Panels, and adds the first and second
36    * AlignFrame's components to each. At this stage we have not yet committed to
37    * whether the enclosing panel will be added to this frame, for display as a
38    * separate frame, or added to the applet (embedded mode).
39    */
40   public void init()
41   {
42     setMenuBar(null);
43     outermost = new Panel();
44     outermost.setLayout(new GridLayout(2, 1));
45
46     Panel topPanel = new Panel();
47     Panel bottomPanel = new Panel();
48     outermost.add(topPanel);
49     outermost.add(bottomPanel);
50
51     addAlignFrameComponents(topFrame, topPanel);
52     addAlignFrameComponents(bottomFrame, bottomPanel);
53
54     /*
55      * Try to make and add dna/protein sequence mappings
56      */
57     final AlignViewport topViewport = topFrame.viewport;
58     final AlignViewport bottomViewport = bottomFrame.viewport;
59     final AlignmentI topAlignment = topViewport.getAlignment();
60     final AlignmentI bottomAlignment = bottomViewport.getAlignment();
61     // topAlignment.setDataset(null);
62     // bottomAlignment.setDataset(null);
63     AlignViewport cdna = topAlignment.isNucleotide() ? topViewport
64             : (bottomAlignment.isNucleotide() ? bottomViewport : null);
65     AlignViewport protein = !topAlignment.isNucleotide() ? topViewport
66             : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
67     MappingResult mapped = AlignmentUtils.mapProteinToCdna(
68             protein.getAlignment(), cdna.getAlignment());
69     if (mapped != MappingResult.NotMapped)
70     {
71       final StructureSelectionManager ssm = StructureSelectionManager
72               .getStructureSelectionManager(topViewport.applet);
73       ssm.addMappings(protein.getAlignment().getCodonFrames());
74       topViewport.setCodingComplement(bottomViewport);
75       ssm.addCommandListener(cdna);
76       ssm.addCommandListener(protein);
77     }
78
79     /*
80      * Expand protein to 3 times character width of dna
81      */
82     if (protein != null && cdna != null)
83     {
84       ViewStyleI vs = protein.getViewStyle();
85       vs.setCharWidth(3 * vs.getCharWidth());
86       protein.setViewStyle(vs);
87     }
88
89   }
90
91   /**
92    * Add the menu bar, alignment panel and status bar from the AlignFrame to the
93    * panel. The menu bar is a panel 'reconstructed' from the AlignFrame's frame
94    * menu bar. This allows each half of the SplitFrame to have its own menu bar.
95    * 
96    * @param af
97    * @param panel
98    */
99   private void addAlignFrameComponents(AlignFrame af, Panel panel)
100   {
101     panel.setLayout(new BorderLayout());
102     Panel menuPanel = af
103             .makeEmbeddedPopupMenu(af.getMenuBar(), true, false);
104     panel.add(menuPanel, BorderLayout.NORTH);
105     panel.add(af.statusBar, BorderLayout.SOUTH);
106     panel.add(af.alignPanel, BorderLayout.CENTER);
107   }
108
109   /**
110    * Display the content panel either as a new frame or embedded in the applet.
111    * 
112    * @param embedded
113    * @param applet
114    */
115   public void addToDisplay(boolean embedded, JalviewLite applet)
116   {
117     createAlignFrameWindow(embedded, applet);
118     validate();
119     topFrame.alignPanel.adjustAnnotationHeight();
120     topFrame.alignPanel.paintAlignment(true);
121     bottomFrame.alignPanel.adjustAnnotationHeight();
122     bottomFrame.alignPanel.paintAlignment(true);
123   }
124
125   /**
126    * Either show the content panel in this frame as a new frame, or (if
127    * embed=true) add it to the applet container instead.
128    * 
129    * @param embed
130    * @param applet
131    */
132   public void createAlignFrameWindow(boolean embed, JalviewLite applet)
133   {
134     if (embed)
135     {
136       applet.add(outermost);
137       applet.validate();
138     }
139     else
140     {
141       this.add(outermost);
142       int width = Math.max(topFrame.DEFAULT_WIDTH,
143               bottomFrame.DEFAULT_WIDTH);
144       int height = topFrame.DEFAULT_HEIGHT + bottomFrame.DEFAULT_HEIGHT;
145       jalview.bin.JalviewLite
146               .addFrame(this, this.getTitle(), width, height);
147     }
148   }
149 }