2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
22 import java.awt.event.*;
24 import jalview.datamodel.*;
26 public class AlignmentPanel extends Panel implements AdjustmentListener
29 public AlignViewport av;
31 OverviewPanel overviewPanel;
37 IdwidthAdjuster idwidthAdjuster;
39 public AlignFrame alignFrame;
41 ScalePanel scalePanel;
43 AnnotationPanel annotationPanel;
45 AnnotationLabels alabels;
47 // this value is set false when selection area being dragged
48 boolean fastPaint = true;
50 public AlignmentPanel(AlignFrame af, final AlignViewport av)
62 seqPanel = new SeqPanel(av, this);
63 idPanel = new IdPanel(av, this);
64 scalePanel = new ScalePanel(av, this);
65 idwidthAdjuster = new IdwidthAdjuster(this);
66 annotationPanel = new AnnotationPanel(this);
68 sequenceHolderPanel.add(annotationPanel, BorderLayout.SOUTH);
70 alabels = new AnnotationLabels(this);
72 setAnnotationVisible(av.showAnnotation);
74 idPanelHolder.add(idPanel, BorderLayout.CENTER);
75 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
76 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
77 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
78 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
81 setScrollValues(0, 0);
83 hscroll.addAdjustmentListener(this);
84 vscroll.addAdjustmentListener(this);
86 addComponentListener(new ComponentAdapter()
88 public void componentResized(ComponentEvent evt)
90 setScrollValues(av.getStartRes(), av.getStartSeq());
95 Dimension d = calculateIdWidth();
96 idPanel.idCanvas.setSize(d);
98 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
100 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
101 annotationSpaceFillerHolder.setSize(d.width,
102 annotationPanel.getSize().height);
103 alabels.setSize(d.width, annotationPanel.getSize().height);
105 final AlignmentPanel ap = this;
106 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
108 public void propertyChange(java.beans.PropertyChangeEvent evt)
110 if (evt.getPropertyName().equals("alignment"))
112 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
120 public SequenceRenderer getSequenceRenderer()
122 return seqPanel.seqCanvas.sr;
125 public FeatureRenderer getFeatureRenderer()
127 return seqPanel.seqCanvas.fr;
130 public void alignmentChanged()
132 av.alignmentChanged(this);
134 if (overviewPanel != null)
136 overviewPanel.updateOverviewImage();
139 alignFrame.updateEditMenuBar();
144 public void fontChanged()
146 // set idCanvas bufferedImage to null
147 // to prevent drawing old image
148 idPanel.idCanvas.image = null;
149 FontMetrics fm = getFontMetrics(av.getFont());
151 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
152 idwidthAdjuster.setSize(new Dimension(10, av.charHeight
154 av.updateSequenceIdColours();
155 annotationPanel.image = null;
156 int ap = annotationPanel.adjustPanelHeight();
157 annotationPanel.repaint();
158 Dimension d = calculateIdWidth();
159 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
160 alabels.setSize(d.width + 4, ap);
161 idPanel.idCanvas.setSize(d);
162 hscrollFillerPanel.setSize(d);
167 if (overviewPanel != null)
169 overviewPanel.updateOverviewImage();
173 public void setIdWidth(int w, int h)
175 idPanel.idCanvas.setSize(w, h);
176 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
177 alabels.setSize(w, alabels.getSize().height);
181 Dimension calculateIdWidth()
183 if (av.nullFrame == null)
185 av.nullFrame = new Frame();
186 av.nullFrame.addNotify();
189 Graphics g = av.nullFrame.getGraphics();
191 FontMetrics fm = g.getFontMetrics(av.font);
192 AlignmentI al = av.getAlignment();
197 while (i < al.getHeight() && al.getSequenceAt(i) != null)
199 SequenceI s = al.getSequenceAt(i);
200 id = s.getDisplayId(av.getShowJVSuffix());
202 if (fm.stringWidth(id) > idWidth)
204 idWidth = fm.stringWidth(id);
209 // Also check annotation label widths
211 if (al.getAlignmentAnnotation() != null)
213 fm = g.getFontMetrics(av.nullFrame.getFont());
214 while (i < al.getAlignmentAnnotation().length)
216 String label = al.getAlignmentAnnotation()[i].label;
217 if (fm.stringWidth(label) > idWidth)
219 idWidth = fm.stringWidth(label);
225 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
228 public void highlightSearchResults(SearchResults results)
230 seqPanel.seqCanvas.highlightSearchResults(results);
233 * scroll the view to show the position of the highlighted
234 * region in results (if any)
237 public void scrollToPosition(SearchResults results)
239 // do we need to scroll the panel?
240 if (results != null && results.getSize()>0)
242 int seqIndex = av.alignment.findIndex(results);
247 SequenceI seq = av.alignment.getSequenceAt(seqIndex);
248 int [] r = results.getResults(seq, seq.getStart(), seq.getEnd());
259 if (end==seq.getEnd())
263 if (!av.wrapAlignment)
265 if ((av.getStartRes() > end)
266 || (av.getEndRes() < start)
267 || ((av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
269 if (start > av.alignment.getWidth() - hextent)
271 start = av.alignment.getWidth() - hextent;
277 if (seqIndex > av.alignment.getHeight() - vextent)
279 seqIndex = av.alignment.getHeight() - vextent;
285 setScrollValues(start, seqIndex);
290 scrollToWrappedVisible(start);
297 void scrollToWrappedVisible(int res)
299 int cwidth = seqPanel.seqCanvas
300 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
301 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
303 vscroll.setValue(res / cwidth);
304 av.startRes = vscroll.getValue() * cwidth;
308 public OverviewPanel getOverviewPanel()
310 return overviewPanel;
313 public void setOverviewPanel(OverviewPanel op)
318 public void setAnnotationVisible(boolean b)
320 if (!av.wrapAlignment)
322 annotationSpaceFillerHolder.setVisible(b);
323 annotationPanel.setVisible(b);
329 public void setWrapAlignment(boolean wrap)
333 scalePanelHolder.setVisible(!wrap);
335 hscroll.setVisible(!wrap);
336 idwidthAdjuster.setVisible(!wrap);
340 annotationPanel.setVisible(false);
341 annotationSpaceFillerHolder.setVisible(false);
343 else if (av.showAnnotation)
345 annotationPanel.setVisible(true);
346 annotationSpaceFillerHolder.setVisible(true);
349 idSpaceFillerPanel1.setVisible(!wrap);
351 fontChanged(); // This is so that the scalePanel is resized correctly
362 // return value is true if the scroll is valid
363 public boolean scrollUp(boolean up)
367 if (vscroll.getValue() < 1)
371 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
375 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
379 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
386 public boolean scrollRight(boolean right)
390 if (hscroll.getValue() < 1)
394 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
398 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
402 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
409 public void setScrollValues(int x, int y)
411 int width = av.alignment.getWidth();
412 int height = av.alignment.getHeight();
414 if (av.hasHiddenColumns)
416 width = av.getColumnSelection().findColumnPosition(width);
421 .setEndRes((x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
423 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
424 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
431 if (vextent > height)
436 if ((hextent + x) > width)
441 if ((vextent + y) > height)
443 y = height - vextent;
458 int endSeq = y + vextent;
459 if (endSeq > av.alignment.getHeight())
461 endSeq = av.alignment.getHeight();
464 av.setEndSeq(endSeq);
465 hscroll.setValues(x, hextent, 0, width);
466 vscroll.setValues(y, vextent, 0, height);
468 if (overviewPanel != null)
470 overviewPanel.setBoxPosition();
475 public void adjustmentValueChanged(AdjustmentEvent evt)
477 int oldX = av.getStartRes();
478 int oldY = av.getStartSeq();
480 if (evt == null || evt.getSource() == hscroll)
482 int x = hscroll.getValue();
484 av.setEndRes(x + seqPanel.seqCanvas.getSize().width
485 / av.getCharWidth() - 1);
488 if (evt == null || evt.getSource() == vscroll)
490 int offy = vscroll.getValue();
491 if (av.getWrapAlignment())
493 int rowSize = seqPanel.seqCanvas
494 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
495 av.setStartRes(vscroll.getValue() * rowSize);
496 av.setEndRes((vscroll.getValue() + 1) * rowSize);
500 av.setStartSeq(offy);
501 av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height
502 / av.getCharHeight());
506 if (overviewPanel != null)
508 overviewPanel.setBoxPosition();
511 int scrollX = av.startRes - oldX;
512 int scrollY = av.startSeq - oldY;
514 if (av.getWrapAlignment() || !fastPaint || av.MAC)
520 // Make sure we're not trying to draw a panel
521 // larger than the visible window
522 if (scrollX > av.endRes - av.startRes)
524 scrollX = av.endRes - av.startRes;
526 else if (scrollX < av.startRes - av.endRes)
528 scrollX = av.startRes - av.endRes;
531 idPanel.idCanvas.fastPaint(scrollY);
532 seqPanel.seqCanvas.fastPaint(scrollX, scrollY);
534 scalePanel.repaint();
535 if (av.getShowAnnotation())
537 annotationPanel.fastPaint(av.getStartRes() - oldX);
543 public void paintAlignment(boolean updateOverview)
549 jalview.structure.StructureSelectionManager
550 .getStructureSelectionManager().sequenceColoursChanged(this);
552 if (overviewPanel != null)
554 overviewPanel.updateOverviewImage();
559 public void update(Graphics g)
564 public void paint(Graphics g)
567 Dimension d = idPanel.idCanvas.getSize();
568 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
569 annotationSpaceFillerHolder.setSize(d.width,
570 annotationPanel.getSize().height);
572 alabels.setSize(d.width, annotationPanel.getSize().height);
574 if (av.getWrapAlignment())
576 int maxwidth = av.alignment.getWidth();
578 if (av.hasHiddenColumns)
580 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
583 int canvasWidth = seqPanel.seqCanvas
584 .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
588 int max = maxwidth / canvasWidth;
589 vscroll.setMaximum(max);
590 vscroll.setUnitIncrement(1);
591 vscroll.setVisibleAmount(1);
596 setScrollValues(av.getStartRes(), av.getStartSeq());
601 seqPanel.seqCanvas.repaint();
602 scalePanel.repaint();
603 annotationPanel.repaint();
604 idPanel.idCanvas.repaint();
607 protected Panel sequenceHolderPanel = new Panel();
609 protected Scrollbar vscroll = new Scrollbar();
611 protected Scrollbar hscroll = new Scrollbar();
613 protected Panel seqPanelHolder = new Panel();
615 BorderLayout borderLayout1 = new BorderLayout();
617 BorderLayout borderLayout3 = new BorderLayout();
619 protected Panel scalePanelHolder = new Panel();
621 protected Panel idPanelHolder = new Panel();
623 BorderLayout borderLayout5 = new BorderLayout();
625 protected Panel idSpaceFillerPanel1 = new Panel();
627 public Panel annotationSpaceFillerHolder = new Panel();
629 BorderLayout borderLayout6 = new BorderLayout();
631 BorderLayout borderLayout7 = new BorderLayout();
633 Panel hscrollHolder = new Panel();
635 BorderLayout borderLayout10 = new BorderLayout();
637 protected Panel hscrollFillerPanel = new Panel();
639 BorderLayout borderLayout11 = new BorderLayout();
641 BorderLayout borderLayout4 = new BorderLayout();
643 BorderLayout borderLayout2 = new BorderLayout();
645 private void jbInit() throws Exception
647 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
648 this.setLayout(borderLayout7);
650 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
651 sequenceHolderPanel.setLayout(borderLayout3);
652 seqPanelHolder.setLayout(borderLayout1);
653 scalePanelHolder.setBackground(Color.white);
655 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
656 scalePanelHolder.setLayout(borderLayout6);
657 idPanelHolder.setLayout(borderLayout5);
658 idSpaceFillerPanel1.setBackground(Color.white);
660 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
661 idSpaceFillerPanel1.setLayout(borderLayout11);
662 annotationSpaceFillerHolder.setBackground(Color.white);
664 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
665 annotationSpaceFillerHolder.setLayout(borderLayout4);
666 hscroll.setOrientation(Scrollbar.HORIZONTAL);
667 hscrollHolder.setLayout(borderLayout10);
668 hscrollFillerPanel.setBackground(Color.white);
670 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
671 hscrollHolder.setBackground(Color.white);
673 // annotationScroller.setPreferredSize(new Dimension(10, 80));
674 // this.setPreferredSize(new Dimension(220, 166));
675 seqPanelHolder.setBackground(Color.white);
676 idPanelHolder.setBackground(Color.white);
677 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
678 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
679 seqPanelHolder.add(vscroll, BorderLayout.EAST);
681 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
682 this.add(idPanelHolder, BorderLayout.WEST);
683 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
684 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
685 this.add(hscrollHolder, BorderLayout.SOUTH);
686 hscrollHolder.add(hscroll, BorderLayout.CENTER);
687 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
688 this.add(sequenceHolderPanel, BorderLayout.CENTER);