2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
23 import java.awt.event.*;
\r
25 import jalview.datamodel.*;
\r
27 public class AlignmentPanel
\r
28 extends Panel implements AdjustmentListener
\r
31 public AlignViewport av;
\r
32 OverviewPanel overviewPanel;
\r
35 IdwidthAdjuster idwidthAdjuster;
\r
36 public AlignFrame alignFrame;
\r
37 ScalePanel scalePanel;
\r
38 AnnotationPanel annotationPanel;
\r
39 AnnotationLabels alabels;
\r
41 // this value is set false when selection area being dragged
\r
42 boolean fastPaint = true;
\r
44 public AlignmentPanel(AlignFrame af, final AlignViewport av)
\r
52 e.printStackTrace();
\r
57 seqPanel = new SeqPanel(av, this);
\r
58 idPanel = new IdPanel(av, this);
\r
59 scalePanel = new ScalePanel(av, this);
\r
60 idwidthAdjuster = new IdwidthAdjuster(this);
\r
61 annotationPanel = new AnnotationPanel(this);
\r
63 sequenceHolderPanel.add(annotationPanel, BorderLayout.SOUTH);
\r
65 alabels = new AnnotationLabels(this);
\r
67 setAnnotationVisible(av.showAnnotation);
\r
69 idPanelHolder.add(idPanel, BorderLayout.CENTER);
\r
70 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
\r
71 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
\r
72 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
\r
73 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
\r
76 setScrollValues(0, 0);
\r
78 hscroll.addAdjustmentListener(this);
\r
79 vscroll.addAdjustmentListener(this);
\r
81 addComponentListener(new ComponentAdapter()
\r
83 public void componentResized(ComponentEvent evt)
\r
85 setScrollValues(av.getStartRes(), av.getStartSeq());
\r
90 Dimension d = calculateIdWidth();
\r
91 idPanel.idCanvas.setSize(d);
\r
93 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
\r
95 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
\r
96 annotationSpaceFillerHolder.setSize(d.width,
\r
97 annotationPanel.getSize().height);
\r
98 alabels.setSize(d.width, annotationPanel.getSize().height);
\r
100 final AlignmentPanel ap = this;
\r
101 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
\r
103 public void propertyChange(java.beans.PropertyChangeEvent evt)
\r
105 if (evt.getPropertyName().equals("alignment"))
\r
107 PaintRefresher.Refresh(ap,
\r
108 av.getSequenceSetId(),
\r
111 alignmentChanged();
\r
118 public SequenceRenderer getSequenceRenderer()
\r
120 return seqPanel.seqCanvas.sr;
\r
123 public FeatureRenderer getFeatureRenderer()
\r
125 return seqPanel.seqCanvas.fr;
\r
128 public void alignmentChanged()
\r
130 av.alignmentChanged(this);
\r
132 if (overviewPanel != null)
\r
134 overviewPanel.updateOverviewImage();
\r
137 alignFrame.updateEditMenuBar();
\r
142 public void fontChanged()
\r
144 // set idCanvas bufferedImage to null
\r
145 // to prevent drawing old image
\r
146 idPanel.idCanvas.image = null;
\r
147 FontMetrics fm = getFontMetrics(av.getFont());
\r
149 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
\r
150 idwidthAdjuster.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
\r
152 annotationPanel.image = null;
\r
153 int ap = annotationPanel.adjustPanelHeight();
\r
154 annotationPanel.repaint();
\r
155 Dimension d = calculateIdWidth();
\r
156 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
\r
157 alabels.setSize(d.width + 4, ap);
\r
158 idPanel.idCanvas.setSize(d);
\r
159 hscrollFillerPanel.setSize(d);
\r
164 if (overviewPanel != null)
\r
166 overviewPanel.updateOverviewImage();
\r
170 public void setIdWidth(int w, int h)
\r
172 idPanel.idCanvas.setSize(w, h);
\r
173 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
\r
174 alabels.setSize(w, alabels.getSize().height);
\r
178 Dimension calculateIdWidth()
\r
180 if (av.nullFrame == null)
\r
182 av.nullFrame = new Frame();
\r
183 av.nullFrame.addNotify();
\r
186 Graphics g = av.nullFrame.getGraphics();
\r
188 FontMetrics fm = g.getFontMetrics(av.font);
\r
189 AlignmentI al = av.getAlignment();
\r
194 while (i < al.getHeight() && al.getSequenceAt(i) != null)
\r
196 SequenceI s = al.getSequenceAt(i);
\r
197 id = s.getDisplayId(av.getShowJVSuffix());
\r
199 if (fm.stringWidth(id) > idWidth)
\r
201 idWidth = fm.stringWidth(id);
\r
206 // Also check annotation label widths
\r
208 if (al.getAlignmentAnnotation() != null)
\r
210 fm = g.getFontMetrics(av.nullFrame.getFont());
\r
211 while (i < al.getAlignmentAnnotation().length)
\r
213 String label = al.getAlignmentAnnotation()[i].label;
\r
214 if (fm.stringWidth(label) > idWidth)
\r
216 idWidth = fm.stringWidth(label);
\r
222 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
\r
225 public void highlightSearchResults(SearchResults results)
\r
227 seqPanel.seqCanvas.highlightSearchResults(results);
\r
229 // do we need to scroll the panel?
\r
230 if (results != null)
\r
232 SequenceI seq = results.getResultSequence(0);
\r
233 int seqIndex = av.alignment.findIndex(seq);
\r
234 int start = seq.findIndex(results.getResultStart(0)) - 1;
\r
235 int end = seq.findIndex(results.getResultEnd(0)) - 1;
\r
237 if (!av.wrapAlignment)
\r
239 if ( (av.getStartRes() > end) || (av.getEndRes() < start) ||
\r
240 ( (av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
\r
242 if (start > av.alignment.getWidth() - hextent)
\r
244 start = av.alignment.getWidth() - hextent;
\r
250 if (seqIndex > av.alignment.getHeight() - vextent)
\r
252 seqIndex = av.alignment.getHeight() - vextent;
\r
258 setScrollValues(start, seqIndex);
\r
263 scrollToWrappedVisible(start);
\r
270 void scrollToWrappedVisible(int res)
\r
272 int cwidth = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.
\r
274 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
\r
276 vscroll.setValue(res / cwidth);
\r
277 av.startRes = vscroll.getValue() * cwidth;
\r
281 public OverviewPanel getOverviewPanel()
\r
283 return overviewPanel;
\r
286 public void setOverviewPanel(OverviewPanel op)
\r
288 overviewPanel = op;
\r
291 public void setAnnotationVisible(boolean b)
\r
293 if (!av.wrapAlignment)
\r
295 annotationSpaceFillerHolder.setVisible(b);
\r
296 annotationPanel.setVisible(b);
\r
302 public void setWrapAlignment(boolean wrap)
\r
306 scalePanelHolder.setVisible(!wrap);
\r
308 hscroll.setVisible(!wrap);
\r
309 idwidthAdjuster.setVisible(!wrap);
\r
313 annotationPanel.setVisible(false);
\r
314 annotationSpaceFillerHolder.setVisible(false);
\r
316 else if (av.showAnnotation)
\r
318 annotationPanel.setVisible(true);
\r
319 annotationSpaceFillerHolder.setVisible(true);
\r
322 idSpaceFillerPanel1.setVisible(!wrap);
\r
324 fontChanged(); //This is so that the scalePanel is resized correctly
\r
334 // return value is true if the scroll is valid
\r
335 public boolean scrollUp(boolean up)
\r
339 if (vscroll.getValue() < 1)
\r
343 setScrollValues(hscroll.getValue(), vscroll.getValue() - 1);
\r
347 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
\r
351 setScrollValues(hscroll.getValue(), vscroll.getValue() + 1);
\r
358 public boolean scrollRight(boolean right)
\r
362 if (hscroll.getValue() < 1)
\r
366 setScrollValues(hscroll.getValue() - 1, vscroll.getValue());
\r
370 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
\r
374 setScrollValues(hscroll.getValue() + 1, vscroll.getValue());
\r
381 public void setScrollValues(int x, int y)
\r
383 int width = av.alignment.getWidth();
\r
384 int height = av.alignment.getHeight();
\r
386 if (av.hasHiddenColumns)
\r
388 width = av.getColumnSelection().findColumnPosition(width);
\r
392 av.setEndRes( (x + (seqPanel.seqCanvas.getSize().width / av.charWidth)) - 1);
\r
394 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
\r
395 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
\r
397 if (hextent > width)
\r
402 if (vextent > height)
\r
407 if ( (hextent + x) > width)
\r
409 x = width - hextent;
\r
412 if ( (vextent + y) > height)
\r
414 y = height - vextent;
\r
429 int endSeq = y + vextent;
\r
430 if (endSeq > av.alignment.getHeight())
\r
432 endSeq = av.alignment.getHeight();
\r
435 av.setEndSeq(endSeq);
\r
436 hscroll.setValues(x, hextent, 0, width);
\r
437 vscroll.setValues(y, vextent, 0, height);
\r
439 if (overviewPanel != null)
\r
441 overviewPanel.setBoxPosition();
\r
446 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
448 int oldX = av.getStartRes();
\r
449 int oldY = av.getStartSeq();
\r
451 if (evt == null || evt.getSource() == hscroll)
\r
453 int x = hscroll.getValue();
\r
455 av.setEndRes(x + seqPanel.seqCanvas.getSize().width / av.getCharWidth() -
\r
459 if (evt == null || evt.getSource() == vscroll)
\r
461 int offy = vscroll.getValue();
\r
462 if (av.getWrapAlignment())
\r
464 int rowSize = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.
\r
465 seqCanvas.getSize().width);
\r
466 av.setStartRes(vscroll.getValue() * rowSize);
\r
467 av.setEndRes( (vscroll.getValue() + 1) * rowSize);
\r
471 av.setStartSeq(offy);
\r
472 av.setEndSeq(offy +
\r
473 seqPanel.seqCanvas.getSize().height / av.getCharHeight());
\r
477 if (overviewPanel != null)
\r
479 overviewPanel.setBoxPosition();
\r
482 int scrollX = av.startRes - oldX;
\r
483 int scrollY = av.startSeq - oldY;
\r
485 if (av.getWrapAlignment() || !fastPaint || av.MAC)
\r
491 // Make sure we're not trying to draw a panel
\r
492 // larger than the visible window
\r
493 if (scrollX > av.endRes - av.startRes)
\r
495 scrollX = av.endRes - av.startRes;
\r
497 else if (scrollX < av.startRes - av.endRes)
\r
499 scrollX = av.startRes - av.endRes;
\r
502 idPanel.idCanvas.fastPaint(scrollY);
\r
503 seqPanel.seqCanvas.fastPaint(scrollX,
\r
506 scalePanel.repaint();
\r
507 if (av.getShowAnnotation())
\r
509 annotationPanel.fastPaint(av.getStartRes() - oldX);
\r
515 public void paintAlignment(boolean updateOverview)
\r
521 jalview.structure.StructureSelectionManager.getStructureSelectionManager()
\r
522 .sequenceColoursChanged(this);
\r
524 if (overviewPanel != null)
\r
526 overviewPanel.updateOverviewImage();
\r
531 public void update(Graphics g)
\r
536 public void paint(Graphics g)
\r
539 Dimension d = idPanel.idCanvas.getSize();
\r
540 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
\r
541 annotationSpaceFillerHolder.setSize(d.width,
\r
542 annotationPanel.getSize().height);
\r
544 alabels.setSize(d.width, annotationPanel.getSize().height);
\r
546 if (av.getWrapAlignment())
\r
548 int maxwidth = av.alignment.getWidth();
\r
550 if (av.hasHiddenColumns)
\r
552 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
\r
555 int canvasWidth = seqPanel.seqCanvas.getWrappedCanvasWidth
\r
556 (seqPanel.seqCanvas.getSize().width);
\r
558 if (canvasWidth > 0)
\r
560 int max = maxwidth / canvasWidth;
\r
561 vscroll.setMaximum(max);
\r
562 vscroll.setUnitIncrement(1);
\r
563 vscroll.setVisibleAmount(1);
\r
568 setScrollValues(av.getStartRes(), av.getStartSeq());
\r
573 seqPanel.seqCanvas.repaint();
\r
574 scalePanel.repaint();
\r
575 annotationPanel.repaint();
\r
576 idPanel.idCanvas.repaint();
\r
579 protected Panel sequenceHolderPanel = new Panel();
\r
580 protected Scrollbar vscroll = new Scrollbar();
\r
581 protected Scrollbar hscroll = new Scrollbar();
\r
582 protected Panel seqPanelHolder = new Panel();
\r
583 BorderLayout borderLayout1 = new BorderLayout();
\r
584 BorderLayout borderLayout3 = new BorderLayout();
\r
585 protected Panel scalePanelHolder = new Panel();
\r
586 protected Panel idPanelHolder = new Panel();
\r
587 BorderLayout borderLayout5 = new BorderLayout();
\r
588 protected Panel idSpaceFillerPanel1 = new Panel();
\r
589 public Panel annotationSpaceFillerHolder = new Panel();
\r
590 BorderLayout borderLayout6 = new BorderLayout();
\r
591 BorderLayout borderLayout7 = new BorderLayout();
\r
592 Panel hscrollHolder = new Panel();
\r
593 BorderLayout borderLayout10 = new BorderLayout();
\r
594 protected Panel hscrollFillerPanel = new Panel();
\r
595 BorderLayout borderLayout11 = new BorderLayout();
\r
596 BorderLayout borderLayout4 = new BorderLayout();
\r
597 BorderLayout borderLayout2 = new BorderLayout();
\r
599 private void jbInit()
\r
602 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
\r
603 this.setLayout(borderLayout7);
\r
605 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
\r
606 sequenceHolderPanel.setLayout(borderLayout3);
\r
607 seqPanelHolder.setLayout(borderLayout1);
\r
608 scalePanelHolder.setBackground(Color.white);
\r
610 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
\r
611 scalePanelHolder.setLayout(borderLayout6);
\r
612 idPanelHolder.setLayout(borderLayout5);
\r
613 idSpaceFillerPanel1.setBackground(Color.white);
\r
615 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
\r
616 idSpaceFillerPanel1.setLayout(borderLayout11);
\r
617 annotationSpaceFillerHolder.setBackground(Color.white);
\r
619 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
\r
620 annotationSpaceFillerHolder.setLayout(borderLayout4);
\r
621 hscroll.setOrientation(Scrollbar.HORIZONTAL);
\r
622 hscrollHolder.setLayout(borderLayout10);
\r
623 hscrollFillerPanel.setBackground(Color.white);
\r
625 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
\r
626 hscrollHolder.setBackground(Color.white);
\r
628 // annotationScroller.setPreferredSize(new Dimension(10, 80));
\r
629 // this.setPreferredSize(new Dimension(220, 166));
\r
630 seqPanelHolder.setBackground(Color.white);
\r
631 idPanelHolder.setBackground(Color.white);
\r
632 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
\r
633 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
\r
634 seqPanelHolder.add(vscroll, BorderLayout.EAST);
\r
636 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
\r
637 this.add(idPanelHolder, BorderLayout.WEST);
\r
638 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
\r
639 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
\r
640 this.add(hscrollHolder, BorderLayout.SOUTH);
\r
641 hscrollHolder.add(hscroll, BorderLayout.CENTER);
\r
642 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
\r
643 this.add(sequenceHolderPanel, BorderLayout.CENTER);
\r