JAL-1619 split frame now offered in applet 'input from textbox'
[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(new GridLayout(2, 1));
44
45     Panel topPanel = new Panel();
46     Panel bottomPanel = new Panel();
47     outermost.add(topPanel);
48     outermost.add(bottomPanel);
49
50     addAlignFrameComponents(topFrame, topPanel);
51     addAlignFrameComponents(bottomFrame, bottomPanel);
52
53     /*
54      * Try to make and add dna/protein sequence mappings
55      */
56     final AlignViewport topViewport = topFrame.viewport;
57     final AlignViewport bottomViewport = bottomFrame.viewport;
58     final AlignmentI topAlignment = topViewport.getAlignment();
59     final AlignmentI bottomAlignment = bottomViewport.getAlignment();
60     AlignViewport cdna = topAlignment.isNucleotide() ? topViewport
61             : (bottomAlignment.isNucleotide() ? bottomViewport : null);
62     AlignViewport protein = !topAlignment.isNucleotide() ? topViewport
63             : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
64
65     MappingResult mapped = AlignmentUtils.mapProteinToCdna(
66             protein.getAlignment(), cdna.getAlignment());
67     if (mapped != MappingResult.NotMapped)
68     {
69       final StructureSelectionManager ssm = StructureSelectionManager
70               .getStructureSelectionManager(topViewport.applet);
71       ssm.addMappings(protein.getAlignment().getCodonFrames());
72       topViewport.setCodingComplement(bottomViewport);
73       ssm.addCommandListener(cdna);
74       ssm.addCommandListener(protein);
75     }
76
77     /*
78      * Expand protein to 3 times character width of dna
79      */
80     if (protein != null && cdna != null)
81     {
82       ViewStyleI vs = protein.getViewStyle();
83       vs.setCharWidth(3 * vs.getCharWidth());
84       protein.setViewStyle(vs);
85     }
86
87   }
88
89   /**
90    * Add the menu bar, alignment panel and status bar from the AlignFrame to the
91    * panel. The menu bar is a panel 'reconstructed' from the AlignFrame's frame
92    * menu bar. This allows each half of the SplitFrame to have its own menu bar.
93    * 
94    * @param af
95    * @param panel
96    */
97   private void addAlignFrameComponents(AlignFrame af, Panel panel)
98   {
99     panel.setLayout(new BorderLayout());
100     Panel menuPanel = af
101             .makeEmbeddedPopupMenu(af.getMenuBar(), true, false);
102     panel.add(menuPanel, BorderLayout.NORTH);
103     panel.add(af.statusBar, BorderLayout.SOUTH);
104     panel.add(af.alignPanel, BorderLayout.CENTER);
105   }
106
107   /**
108    * Display the content panel either as a new frame or embedded in the applet.
109    * 
110    * @param embedded
111    * @param applet
112    */
113   public void addToDisplay(boolean embedded, JalviewLite applet)
114   {
115     createAlignFrameWindow(embedded, applet);
116     validate();
117     topFrame.alignPanel.adjustAnnotationHeight();
118     topFrame.alignPanel.paintAlignment(true);
119     bottomFrame.alignPanel.adjustAnnotationHeight();
120     bottomFrame.alignPanel.paintAlignment(true);
121   }
122
123   /**
124    * Either show the content panel in this frame as a new frame, or (if
125    * embed=true) add it to the applet container instead.
126    * 
127    * @param embed
128    * @param applet
129    */
130   public void createAlignFrameWindow(boolean embed, JalviewLite applet)
131   {
132     if (embed)
133     {
134       applet.add(outermost);
135       applet.validate();
136     }
137     else
138     {
139       this.add(outermost);
140       int width = Math.max(topFrame.DEFAULT_WIDTH,
141               bottomFrame.DEFAULT_WIDTH);
142       int height = topFrame.DEFAULT_HEIGHT + bottomFrame.DEFAULT_HEIGHT;
143       jalview.bin.JalviewLite
144               .addFrame(this, this.getTitle(), width, height);
145     }
146   }
147 }