2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
22 import java.util.Hashtable;
23 import java.util.Vector;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.*;
27 import jalview.structure.StructureSelectionManager;
29 public class AlignmentPanel extends Panel implements AdjustmentListener, AlignmentViewPanel
32 public AlignViewport av;
34 OverviewPanel overviewPanel;
40 IdwidthAdjuster idwidthAdjuster;
42 public AlignFrame alignFrame;
44 ScalePanel scalePanel;
46 AnnotationPanel annotationPanel;
48 AnnotationLabels alabels;
50 // this value is set false when selection area being dragged
51 boolean fastPaint = true;
53 public void finalize() {
58 sequenceHolderPanel=null;
60 scalePanelHolder=null;
62 annotationPanelHolder=null;
63 annotationSpaceFillerHolder=null;
65 public AlignmentPanel(AlignFrame af, final AlignViewport av)
77 seqPanel = new SeqPanel(av, this);
78 idPanel = new IdPanel(av, this);
79 scalePanel = new ScalePanel(av, this);
80 idwidthAdjuster = new IdwidthAdjuster(this);
81 annotationPanel = new AnnotationPanel(this);
82 annotationPanelHolder.add(annotationPanel, BorderLayout.CENTER);
84 sequenceHolderPanel.add(annotationPanelHolder, BorderLayout.SOUTH);
86 alabels = new AnnotationLabels(this);
88 setAnnotationVisible(av.showAnnotation);
90 idPanelHolder.add(idPanel, BorderLayout.CENTER);
91 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
92 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
93 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
94 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
97 setScrollValues(0, 0);
99 apvscroll.addAdjustmentListener(this);
100 hscroll.addAdjustmentListener(this);
101 vscroll.addAdjustmentListener(this);
103 addComponentListener(new ComponentAdapter()
105 public void componentResized(ComponentEvent evt)
107 setScrollValues(av.getStartRes(), av.getStartSeq());
113 Dimension d = calculateIdWidth();
114 idPanel.idCanvas.setSize(d);
116 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
118 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
119 annotationSpaceFillerHolder.setSize(d.width,
120 annotationPanel.getSize().height);
121 alabels.setSize(d.width, annotationPanel.getSize().height);
122 final AlignmentPanel ap = this;
123 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
125 public void propertyChange(java.beans.PropertyChangeEvent evt)
127 if (evt.getPropertyName().equals("alignment"))
129 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
137 public SequenceRenderer getSequenceRenderer()
139 return seqPanel.seqCanvas.sr;
142 public FeatureRenderer getFeatureRenderer()
144 return seqPanel.seqCanvas.fr;
147 public void alignmentChanged()
149 av.alignmentChanged(this);
151 if (overviewPanel != null)
153 overviewPanel.updateOverviewImage();
156 alignFrame.updateEditMenuBar();
161 public void fontChanged()
163 // set idCanvas bufferedImage to null
164 // to prevent drawing old image
165 idPanel.idCanvas.image = null;
166 FontMetrics fm = getFontMetrics(av.getFont());
168 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
169 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
171 av.updateSequenceIdColours();
172 annotationPanel.image = null;
173 int ap = annotationPanel.adjustPanelHeight(false);
174 Dimension d = calculateIdWidth();
175 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
176 alabels.setSize(d.width + 4, ap);
177 idPanel.idCanvas.setSize(d);
178 hscrollFillerPanel.setSize(d);
180 validateAnnotationDimensions(false);
181 annotationPanel.repaint();
185 if (overviewPanel != null)
187 overviewPanel.updateOverviewImage();
191 public void setIdWidth(int w, int h)
193 idPanel.idCanvas.setSize(w, h);
194 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
195 annotationSpaceFillerHolder.setSize(w,annotationSpaceFillerHolder.getSize().height);
196 alabels.setSize(w, alabels.getSize().height);
200 Dimension calculateIdWidth()
202 if (av.nullFrame == null)
204 av.nullFrame = new Frame();
205 av.nullFrame.addNotify();
208 Graphics g = av.nullFrame.getGraphics();
210 FontMetrics fm = g.getFontMetrics(av.font);
211 AlignmentI al = av.getAlignment();
216 while (i < al.getHeight() && al.getSequenceAt(i) != null)
218 SequenceI s = al.getSequenceAt(i);
219 id = s.getDisplayId(av.getShowJVSuffix());
221 if (fm.stringWidth(id) > idWidth)
223 idWidth = fm.stringWidth(id);
228 // Also check annotation label widths
230 if (al.getAlignmentAnnotation() != null)
232 fm = g.getFontMetrics(av.nullFrame.getFont());
233 while (i < al.getAlignmentAnnotation().length)
235 String label = al.getAlignmentAnnotation()[i].label;
236 if (fm.stringWidth(label) > idWidth)
238 idWidth = fm.stringWidth(label);
244 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
248 * Highlight the given results on the alignment.
251 public void highlightSearchResults(SearchResults results)
253 scrollToPosition(results);
254 seqPanel.seqCanvas.highlightSearchResults(results);
258 * scroll the view to show the position of the highlighted region in results
259 * (if any) and redraw the overview
262 * @return false if results were not found
264 public boolean scrollToPosition(SearchResults results)
266 return scrollToPosition(results, true);
270 * scroll the view to show the position of the highlighted region in results
274 * @param redrawOverview
275 * - when set, the overview will be recalculated (takes longer)
276 * @return false if results were not found
278 public boolean scrollToPosition(SearchResults results,
279 boolean redrawOverview)
281 // do we need to scroll the panel?
282 if (results != null && results.getSize() > 0)
284 int seqIndex = av.alignment.findIndex(results);
289 SequenceI seq = av.alignment.getSequenceAt(seqIndex);
290 int[] r = results.getResults(seq, 0,av.alignment.getWidth());
293 if (av.applet.debug) {// DEBUG
294 System.out.println("DEBUG: scroll didn't happen - results not within alignment : "+seq.getStart()+","+seq.getEnd());
298 if (av.applet.debug) {
300 System.out.println("DEBUG: scroll: start=" + r[0]
301 + " av.getStartRes()=" + av.getStartRes() + " end=" + r[1]
302 + " seq.end=" + seq.getEnd() + " av.getEndRes()="
303 + av.getEndRes() + " hextent=" + hextent);
311 if (end == seq.getEnd())
315 return scrollTo(start, end, seqIndex, false, redrawOverview);
319 public boolean scrollTo(int ostart, int end, int seqIndex, boolean scrollToNearest, boolean redrawOverview)
322 if (av.hasHiddenColumns)
324 start = av.getColumnSelection().findColumnPosition(ostart);
325 end = av.getColumnSelection().findColumnPosition(end);
328 if (!scrollToNearest && !av.colSel.isVisible(ostart))
330 // don't scroll - position isn't visible
335 if (!av.wrapAlignment)
337 if ((av.getStartRes() > end)
338 || (av.getEndRes() < start)
339 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
341 if (start > av.alignment.getWidth() - hextent)
343 start = av.alignment.getWidth() - hextent;
350 if (seqIndex > av.alignment.getHeight() - vextent)
352 seqIndex = av.alignment.getHeight() - vextent;
358 setScrollValues(start, seqIndex);
363 scrollToWrappedVisible(start);
365 if (redrawOverview && overviewPanel != null)
367 overviewPanel.setBoxPosition();
369 paintAlignment(redrawOverview);
373 void scrollToWrappedVisible(int res)
375 int cwidth = seqPanel.seqCanvas
376 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
377 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
379 vscroll.setValue(res / cwidth);
380 av.startRes = vscroll.getValue() * cwidth;
384 public OverviewPanel getOverviewPanel()
386 return overviewPanel;
389 public void setOverviewPanel(OverviewPanel op)
394 public void setAnnotationVisible(boolean b)
396 if (!av.wrapAlignment)
398 annotationSpaceFillerHolder.setVisible(b);
399 annotationPanelHolder.setVisible(b);
406 * automatically adjust annotation panel height for new annotation whilst
407 * ensuring the alignment is still visible.
409 public void adjustAnnotationHeight()
411 // TODO: display vertical annotation scrollbar if necessary
412 // this is called after loading new annotation onto alignment
413 if (alignFrame.getSize().height == 0)
415 System.out.println("NEEDS FIXING");
418 validateAnnotationDimensions(true);
419 apvscroll.addNotify();
426 * calculate the annotation dimensions and refresh slider values accordingly.
427 * need to do repaints/notifys afterwards.
429 protected void validateAnnotationDimensions(boolean adjustPanelHeight) {
430 int height = annotationPanel.calcPanelHeight();
431 if (hscroll.isVisible())
433 height += hscroll.getPreferredSize().height;
435 int mheight = height;
436 // sets initial preferred height
437 if ((height+40) > getSize().height / 2)
439 height = getSize().height / 2;
441 if (!adjustPanelHeight)
443 // maintain same window layout whilst updating sliders
444 height=seqPanelHolder.getSize().height;
446 Dimension d=seqPanelHolder.getSize(),e=idPanel.getSize();
447 annotationPanel.setSize(new Dimension(d.width,height));
448 alabels.setSize(e.width,height);
449 annotationSpaceFillerHolder.setSize(new Dimension(e.width, height));
450 annotationPanelHolder.setSize(new Dimension(d.width, height));
451 seqPanelHolder.setSize(d.width,d.height-height);
452 int s=apvscroll.getValue();
453 if (s>mheight-height)
457 apvscroll.setValues(apvscroll.getValue(), height, 0, mheight);
458 annotationPanel.setScrollOffset(apvscroll.getValue());
459 alabels.setScrollOffset(apvscroll.getValue());
462 public void setWrapAlignment(boolean wrap)
466 scalePanelHolder.setVisible(!wrap);
468 hscroll.setVisible(!wrap);
469 idwidthAdjuster.setVisible(!wrap);
473 annotationPanelHolder.setVisible(false);
474 annotationSpaceFillerHolder.setVisible(false);
476 else if (av.showAnnotation)
478 annotationPanelHolder.setVisible(true);
479 annotationSpaceFillerHolder.setVisible(true);
482 idSpaceFillerPanel1.setVisible(!wrap);
484 fontChanged(); // This is so that the scalePanel is resized correctly
495 // return value is true if the scroll is valid
496 public boolean scrollUp(boolean up)
500 if (vscroll.getValue() < 1)
504 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
508 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
512 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
519 public boolean scrollRight(boolean right)
523 if (hscroll.getValue() < 1)
527 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
531 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
535 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
542 public void setScrollValues(int x, int y)
544 int width = av.alignment.getWidth();
545 int height = av.alignment.getHeight();
547 if (av.hasHiddenColumns)
549 width = av.getColumnSelection().findColumnPosition(width);
553 av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
555 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
556 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
563 if (vextent > height)
568 if ((hextent + x) > width)
573 if ((vextent + y) > height)
575 y = height - vextent;
590 int endSeq = y + vextent;
591 if (endSeq > av.alignment.getHeight())
593 endSeq = av.alignment.getHeight();
596 av.setEndSeq(endSeq);
597 hscroll.setValues(x, hextent, 0, width);
598 vscroll.setValues(y, vextent, 0, height);
600 if (overviewPanel != null)
602 overviewPanel.setBoxPosition();
609 public void adjustmentValueChanged(AdjustmentEvent evt)
611 int oldX = av.getStartRes();
612 int oldY = av.getStartSeq();
614 if (evt == null || evt.getSource() == apvscroll)
616 annotationPanel.setScrollOffset(apvscroll.getValue());
617 alabels.setScrollOffset(apvscroll.getValue());
618 // annotationPanel.image=null;
619 // alabels.image=null;
620 // alabels.repaint();
621 // annotationPanel.repaint();
623 if (evt == null || evt.getSource() == hscroll)
625 int x = hscroll.getValue();
627 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
628 / av.getCharWidth() - 1);
631 if (evt == null || evt.getSource() == vscroll)
633 int offy = vscroll.getValue();
634 if (av.getWrapAlignment())
636 int rowSize = seqPanel.seqCanvas
637 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
638 av.setStartRes(vscroll.getValue() * rowSize);
639 av.setEndRes((vscroll.getValue() + 1) * rowSize);
643 av.setStartSeq(offy);
644 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
645 / av.getCharHeight());
649 if (overviewPanel != null)
651 overviewPanel.setBoxPosition();
654 int scrollX = av.startRes - oldX;
655 int scrollY = av.startSeq - oldY;
657 if (av.getWrapAlignment() || !fastPaint || av.MAC)
663 // Make sure we're not trying to draw a panel
664 // larger than the visible window
665 if (scrollX > av.endRes - av.startRes)
667 scrollX = av.endRes - av.startRes;
669 else if (scrollX < av.startRes - av.endRes)
671 scrollX = av.startRes - av.endRes;
674 idPanel.idCanvas.fastPaint(scrollY);
675 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
677 scalePanel.repaint();
678 if (av.getShowAnnotation())
680 annotationPanel.fastPaint(av.getStartRes() - oldX);
686 private void sendViewPosition()
688 StructureSelectionManager.getStructureSelectionManager(av.applet).sendViewPosition(this, av.startRes, av.endRes, av.startSeq, av.endSeq);
691 public void paintAlignment(boolean updateOverview)
697 jalview.structure.StructureSelectionManager
698 .getStructureSelectionManager(av.applet).sequenceColoursChanged(this);
700 if (overviewPanel != null)
702 overviewPanel.updateOverviewImage();
707 public void update(Graphics g)
712 public void paint(Graphics g)
715 Dimension d = idPanel.idCanvas.getSize();
716 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
718 if (av.getWrapAlignment())
720 int maxwidth = av.alignment.getWidth();
722 if (av.hasHiddenColumns)
724 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
727 int canvasWidth = seqPanel.seqCanvas
728 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
732 int max = maxwidth / canvasWidth;
733 vscroll.setMaximum(1 + max);
734 vscroll.setUnitIncrement(1);
735 vscroll.setVisibleAmount(1);
740 setScrollValues(av.getStartRes(), av.getStartSeq());
745 seqPanel.seqCanvas.repaint();
746 scalePanel.repaint();
747 annotationPanel.repaint();
748 idPanel.idCanvas.repaint();
751 protected Panel sequenceHolderPanel = new Panel();
753 protected Scrollbar vscroll = new Scrollbar();
755 protected Scrollbar hscroll = new Scrollbar();
757 protected Panel seqPanelHolder = new Panel();
759 BorderLayout borderLayout1 = new BorderLayout();
761 BorderLayout borderLayout3 = new BorderLayout();
763 protected Panel scalePanelHolder = new Panel();
765 protected Panel idPanelHolder = new Panel();
767 BorderLayout borderLayout5 = new BorderLayout();
769 protected Panel idSpaceFillerPanel1 = new Panel();
771 public Panel annotationSpaceFillerHolder = new Panel();
773 BorderLayout borderLayout6 = new BorderLayout();
775 BorderLayout borderLayout7 = new BorderLayout();
777 Panel hscrollHolder = new Panel();
779 BorderLayout borderLayout10 = new BorderLayout();
781 protected Panel hscrollFillerPanel = new Panel();
783 BorderLayout borderLayout11 = new BorderLayout();
785 BorderLayout borderLayout4 = new BorderLayout();
787 BorderLayout borderLayout2 = new BorderLayout();
789 Panel annotationPanelHolder = new Panel();
791 protected Scrollbar apvscroll = new Scrollbar();
793 BorderLayout borderLayout12 = new BorderLayout();
795 private void jbInit() throws Exception
797 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
798 this.setLayout(borderLayout7);
800 //sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
801 sequenceHolderPanel.setLayout(borderLayout3);
802 seqPanelHolder.setLayout(borderLayout1);
803 scalePanelHolder.setBackground(Color.white);
805 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
806 scalePanelHolder.setLayout(borderLayout6);
807 idPanelHolder.setLayout(borderLayout5);
808 idSpaceFillerPanel1.setBackground(Color.white);
810 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
811 idSpaceFillerPanel1.setLayout(borderLayout11);
812 annotationSpaceFillerHolder.setBackground(Color.white);
814 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
815 annotationSpaceFillerHolder.setLayout(borderLayout4);
816 hscroll.setOrientation(Scrollbar.HORIZONTAL);
817 hscrollHolder.setLayout(borderLayout10);
818 hscrollFillerPanel.setBackground(Color.white);
819 apvscroll.setOrientation(Scrollbar.VERTICAL);
820 apvscroll.setVisible(true);
821 apvscroll.addAdjustmentListener(this);
823 annotationPanelHolder.setBackground(Color.white);
824 annotationPanelHolder.setLayout(borderLayout12);
825 annotationPanelHolder.add(apvscroll, BorderLayout.EAST);
826 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
827 hscrollHolder.setBackground(Color.white);
829 // annotationScroller.setPreferredSize(new Dimension(10, 80));
830 // this.setPreferredSize(new Dimension(220, 166));
831 seqPanelHolder.setBackground(Color.white);
832 idPanelHolder.setBackground(Color.white);
833 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
834 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
835 seqPanelHolder.add(vscroll, BorderLayout.EAST);
837 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
838 this.add(idPanelHolder, BorderLayout.WEST);
839 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
840 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
841 this.add(hscrollHolder, BorderLayout.SOUTH);
842 hscrollHolder.add(hscroll, BorderLayout.CENTER);
843 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
844 this.add(sequenceHolderPanel, BorderLayout.CENTER);
848 * hides or shows dynamic annotation rows based on groups and av state flags
850 public void updateAnnotation()
852 updateAnnotation(false);
855 public void updateAnnotation(boolean applyGlobalSettings)
857 // TODO: this should be merged with other annotation update stuff - that
858 // sits on AlignViewport
859 boolean updateCalcs = false;
860 boolean conv = av.isShowGroupConservation();
861 boolean cons = av.isShowGroupConsensus();
862 boolean showprf = av.isShowSequenceLogo();
863 boolean showConsHist = av.isShowConsensusHistogram();
865 boolean sortg = true;
867 // remove old automatic annotation
868 // add any new annotation
870 Vector gr = av.alignment.getGroups(); // OrderedBy(av.alignment.getSequencesArray());
871 // intersect alignment annotation with alignment groups
873 AlignmentAnnotation[] aan = av.alignment.getAlignmentAnnotation();
874 Hashtable oldrfs = new Hashtable();
877 for (int an = 0; an < aan.length; an++)
879 if (aan[an].autoCalculated && aan[an].groupRef != null)
881 oldrfs.put(aan[an].groupRef, aan[an].groupRef);
882 av.alignment.deleteAnnotation(aan[an]);
890 for (int g = 0; g < gr.size(); g++)
893 sg = (SequenceGroup) gr.elementAt(g);
894 if (applyGlobalSettings || !oldrfs.containsKey(sg))
896 // set defaults for this group's conservation/consensus
897 sg.setshowSequenceLogo(showprf);
898 sg.setShowConsensusHistogram(showConsHist);
903 av.alignment.addAnnotation(sg.getConservationRow(), 0);
908 av.alignment.addAnnotation(sg.getConsensus(), 0);
910 // refresh the annotation rows
913 sg.recalcConservation();
918 adjustAnnotationHeight();
922 public AlignmentI getAlignment()
927 public StructureSelectionManager getStructureSelectionManager()
929 return StructureSelectionManager.getStructureSelectionManager(av.applet);