2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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 extends Panel implements AdjustmentListener
\r
31 OverviewPanel overviewPanel;
\r
34 IdwidthAdjuster idwidthAdjuster;
\r
35 public AlignFrame alignFrame;
\r
36 ScalePanel scalePanel;
\r
37 AnnotationPanel annotationPanel;
\r
38 AnnotationLabels alabels;
\r
40 // this value is set false when selection area being dragged
\r
41 boolean fastPaint = true;
\r
43 boolean MAC = false;
\r
45 public AlignmentPanel(AlignFrame af, final AlignViewport av)
\r
53 e.printStackTrace();
\r
56 if(System.getProperty("os.name").startsWith("Mac"))
\r
61 seqPanel = new SeqPanel(av, this);
\r
62 idPanel = new IdPanel(av, this);
\r
63 scalePanel = new ScalePanel(av, this);
\r
64 idwidthAdjuster = new IdwidthAdjuster(this);
\r
65 annotationPanel = new AnnotationPanel(this);
\r
67 sequenceHolderPanel.add(annotationPanel, BorderLayout.SOUTH);
\r
69 alabels = new AnnotationLabels(this);
\r
71 setAnnotationVisible(av.showAnnotation);
\r
73 idPanelHolder.add(idPanel, BorderLayout.CENTER);
\r
74 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
\r
75 annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER);
\r
76 scalePanelHolder.add(scalePanel, BorderLayout.CENTER);
\r
77 seqPanelHolder.add(seqPanel, BorderLayout.CENTER);
\r
80 setScrollValues(0, 0);
\r
82 hscroll.addAdjustmentListener(this);
\r
83 vscroll.addAdjustmentListener(this);
\r
85 addComponentListener(new ComponentAdapter()
\r
87 public void componentResized(ComponentEvent evt)
\r
89 setScrollValues(av.getStartRes(), av.getStartSeq());
\r
94 Dimension d = calculateIdWidth();
\r
95 idPanel.idCanvas.setSize(d);
\r
97 hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height);
\r
99 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
\r
100 annotationSpaceFillerHolder.setSize(d.width,
\r
101 annotationPanel.getSize().height);
\r
102 alabels.setSize(d.width, annotationPanel.getSize().height);
\r
107 public void fontChanged()
\r
109 // set idCanvas bufferedImage to null
\r
110 // to prevent drawing old image
\r
111 idPanel.idCanvas.image = null;
\r
112 FontMetrics fm = getFontMetrics(av.getFont());
\r
114 scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
\r
115 idwidthAdjuster.setSize(new Dimension(10, av.charHeight + fm.getDescent()));
\r
117 annotationPanel.image = null;
\r
118 int ap = annotationPanel.adjustPanelHeight();
\r
119 annotationPanel.repaint();
\r
120 Dimension d = calculateIdWidth();
\r
121 d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height);
\r
122 alabels.setSize(d.width+4, ap );
\r
123 idPanel.idCanvas.setSize(d);
\r
124 hscrollFillerPanel.setSize(d);
\r
129 if(overviewPanel!=null)
\r
130 overviewPanel.updateOverviewImage();
\r
133 public void setIdWidth(int w, int h)
\r
135 idPanel.idCanvas.setSize(w, h);
\r
136 idPanelHolder.setSize(w, idPanelHolder.getSize().height);
\r
137 alabels.setSize(w, alabels.getSize().height);
\r
141 Dimension calculateIdWidth()
\r
143 if (av.nullFrame == null)
\r
145 av.nullFrame = new Frame();
\r
146 av.nullFrame.addNotify();
\r
149 Graphics g = av.nullFrame.getGraphics();
\r
151 FontMetrics fm = g.getFontMetrics(av.font);
\r
152 AlignmentI al = av.getAlignment();
\r
157 while (i < al.getHeight() && al.getSequenceAt(i) != null)
\r
159 SequenceI s = al.getSequenceAt(i);
\r
160 id = s.getDisplayId(av.getShowJVSuffix());
\r
162 if (fm.stringWidth(id) > idWidth)
\r
164 idWidth = fm.stringWidth(id);
\r
169 // Also check annotation label widths
\r
171 if (al.getAlignmentAnnotation() != null)
\r
173 fm = g.getFontMetrics(av.nullFrame.getFont());
\r
174 while (i < al.getAlignmentAnnotation().length)
\r
176 String label = al.getAlignmentAnnotation()[i].label;
\r
177 if (fm.stringWidth(label) > idWidth)
\r
179 idWidth = fm.stringWidth(label);
\r
185 return new Dimension(idWidth, idPanel.idCanvas.getSize().height);
\r
188 public void highlightSearchResults(SearchResults results)
\r
190 seqPanel.seqCanvas.highlightSearchResults(results);
\r
192 // do we need to scroll the panel?
\r
193 if (results != null)
\r
195 SequenceI seq = results.getResultSequence(0);
\r
196 int seqIndex = av.alignment.findIndex(seq);
\r
197 int start = seq.findIndex(results.getResultStart(0)) - 1;
\r
198 int end = seq.findIndex(results.getResultEnd(0)) - 1;
\r
200 if(!av.wrapAlignment)
\r
202 if ( (av.getStartRes() > end) || (av.getEndRes() < start) ||
\r
203 ( (av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex)))
\r
205 if (start > av.alignment.getWidth() - hextent)
\r
207 start = av.alignment.getWidth() - hextent;
\r
211 if (seqIndex > av.alignment.getHeight() - vextent)
\r
213 seqIndex = av.alignment.getHeight() - vextent;
\r
217 setScrollValues(start, seqIndex);
\r
222 scrollToWrappedVisible(start);
\r
229 void scrollToWrappedVisible(int res)
\r
231 int cwidth = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width);
\r
232 if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth))
\r
234 vscroll.setValue(res / cwidth);
\r
235 av.startRes = vscroll.getValue() * cwidth;
\r
240 public OverviewPanel getOverviewPanel()
\r
242 return overviewPanel;
\r
245 public void setOverviewPanel(OverviewPanel op)
\r
247 overviewPanel = op;
\r
250 public void setAnnotationVisible(boolean b)
\r
252 if (!av.wrapAlignment)
\r
254 annotationSpaceFillerHolder.setVisible(b);
\r
255 annotationPanel.setVisible(b);
\r
261 public void setWrapAlignment(boolean wrap)
\r
264 scalePanelHolder.setVisible(!wrap);
\r
267 hscroll.setVisible(!wrap);
\r
268 idwidthAdjuster.setVisible(!wrap);
\r
272 annotationPanel.setVisible(false);
\r
273 annotationSpaceFillerHolder.setVisible(false);
\r
275 else if (av.showAnnotation)
\r
277 annotationPanel.setVisible(true);
\r
278 annotationSpaceFillerHolder.setVisible(true);
\r
282 idSpaceFillerPanel1.setVisible(!wrap);
\r
284 fontChanged();//This is so that the scalePanel is resized correctly
\r
295 // return value is true if the scroll is valid
\r
296 public boolean scrollUp(boolean up)
\r
300 if (vscroll.getValue() < 1)
\r
304 setScrollValues(hscroll.getValue(), vscroll.getValue()-1);
\r
308 if (vextent + vscroll.getValue() >= av.getAlignment().getHeight())
\r
312 setScrollValues(hscroll.getValue(), vscroll.getValue()+1);
\r
319 public boolean scrollRight(boolean right)
\r
323 if (hscroll.getValue() < 1)
\r
327 setScrollValues(hscroll.getValue()-1, vscroll.getValue());
\r
331 if (hextent + hscroll.getValue() >= av.getAlignment().getWidth())
\r
335 setScrollValues(hscroll.getValue()+1, vscroll.getValue());
\r
342 public void setScrollValues(int x, int y)
\r
346 av.setEndRes(x + seqPanel.seqCanvas.getSize().width / av.getCharWidth() - 1);
\r
348 hextent = seqPanel.seqCanvas.getSize().width / av.charWidth;
\r
349 vextent = seqPanel.seqCanvas.getSize().height / av.charHeight;
\r
351 if (hextent > av.alignment.getWidth())
\r
353 hextent = av.alignment.getWidth();
\r
355 if (vextent > av.alignment.getHeight())
\r
357 vextent = av.alignment.getHeight();
\r
360 if (hextent + x > av.getAlignment().getWidth())
\r
362 x = av.getAlignment().getWidth() - hextent;
\r
365 if (vextent + y > av.getAlignment().getHeight())
\r
367 y = av.getAlignment().getHeight() - vextent;
\r
380 int endSeq = y + vextent;
\r
381 if (endSeq > av.alignment.getHeight())
\r
383 endSeq = av.alignment.getHeight();
\r
386 av.setEndSeq(endSeq);
\r
387 hscroll.setValues(x, hextent, 0, av.getAlignment().getWidth());
\r
388 vscroll.setValues(y, vextent, 0, av.getAlignment().getHeight());
\r
390 if(overviewPanel!=null)
\r
391 overviewPanel.setBoxPosition();
\r
394 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
396 int oldX = av.getStartRes();
\r
397 int oldY = av.getStartSeq();
\r
400 if (evt==null || evt.getSource() == hscroll)
\r
402 int x = hscroll.getValue();
\r
404 av.setEndRes(x + seqPanel.seqCanvas.getSize().width / av.getCharWidth() -
\r
409 if (evt==null || evt.getSource() == vscroll)
\r
411 int offy = vscroll.getValue();
\r
412 if (av.getWrapAlignment())
\r
414 int rowSize = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.
\r
415 seqCanvas.getSize().width);
\r
416 av.setStartRes(vscroll.getValue() * rowSize);
\r
417 av.setEndRes( (vscroll.getValue() + 1) * rowSize);
\r
421 av.setStartSeq(offy);
\r
422 av.setEndSeq(offy +
\r
423 seqPanel.seqCanvas.getSize().height / av.getCharHeight());
\r
427 if (overviewPanel != null)
\r
429 overviewPanel.setBoxPosition();
\r
432 int scrollX = av.startRes - oldX;
\r
433 int scrollY = av.startSeq - oldY;
\r
435 if (av.getWrapAlignment() || !fastPaint || MAC)
\r
441 // Make sure we're not trying to draw a panel
\r
442 // larger than the visible window
\r
443 if(scrollX>av.endRes-av.startRes)
\r
445 scrollX = av.endRes - av.startRes;
\r
447 else if(scrollX<av.startRes-av.endRes)
\r
448 scrollX = av.startRes - av.endRes;
\r
450 idPanel.idCanvas.fastPaint(scrollY);
\r
451 seqPanel.seqCanvas.fastPaint(scrollX,
\r
454 scalePanel.repaint();
\r
455 if (av.getShowAnnotation())
\r
457 annotationPanel.fastPaint(av.getStartRes() - oldX);
\r
463 public void update(Graphics g)
\r
468 public void paint(Graphics g)
\r
471 Dimension d = idPanel.idCanvas.getSize();
\r
472 idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height);
\r
473 annotationSpaceFillerHolder.setSize(d.width,
\r
474 annotationPanel.getSize().height);
\r
476 alabels.setSize(d.width, annotationPanel.getSize().height);
\r
478 if (av.getWrapAlignment())
\r
480 int max = av.alignment.getWidth() /
\r
481 seqPanel.seqCanvas.
\r
482 getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width) +1;
\r
483 vscroll.setMaximum(max);
\r
484 vscroll.setUnitIncrement(1);
\r
485 vscroll.setVisibleAmount(1);
\r
489 setScrollValues(av.getStartRes(), av.getStartSeq());
\r
494 seqPanel.seqCanvas.repaint();
\r
495 scalePanel.repaint();
\r
496 annotationPanel.repaint();
\r
497 idPanel.idCanvas.repaint();
\r
499 if (getBounds() == g.getClipBounds())
\r
501 if (overviewPanel != null)
\r
502 overviewPanel.updateOverviewImage();
\r
507 protected Panel sequenceHolderPanel = new Panel();
\r
508 protected Scrollbar vscroll = new Scrollbar();
\r
509 protected Scrollbar hscroll = new Scrollbar();
\r
510 protected Panel seqPanelHolder = new Panel();
\r
511 BorderLayout borderLayout1 = new BorderLayout();
\r
512 BorderLayout borderLayout3 = new BorderLayout();
\r
513 protected Panel scalePanelHolder = new Panel();
\r
514 protected Panel idPanelHolder = new Panel();
\r
515 BorderLayout borderLayout5 = new BorderLayout();
\r
516 protected Panel idSpaceFillerPanel1 = new Panel();
\r
517 public Panel annotationSpaceFillerHolder = new Panel();
\r
518 BorderLayout borderLayout6 = new BorderLayout();
\r
519 BorderLayout borderLayout7 = new BorderLayout();
\r
520 Panel hscrollHolder = new Panel();
\r
521 BorderLayout borderLayout10 = new BorderLayout();
\r
522 protected Panel hscrollFillerPanel = new Panel();
\r
523 BorderLayout borderLayout11 = new BorderLayout();
\r
524 BorderLayout borderLayout4 = new BorderLayout();
\r
525 BorderLayout borderLayout2 = new BorderLayout();
\r
527 private void jbInit() throws Exception {
\r
528 // idPanelHolder.setPreferredSize(new Dimension(70, 10));
\r
529 this.setLayout(borderLayout7);
\r
531 // sequenceHolderPanel.setPreferredSize(new Dimension(150, 150));
\r
532 sequenceHolderPanel.setLayout(borderLayout3);
\r
533 seqPanelHolder.setLayout(borderLayout1);
\r
534 scalePanelHolder.setBackground(Color.white);
\r
536 // scalePanelHolder.setPreferredSize(new Dimension(10, 30));
\r
537 scalePanelHolder.setLayout(borderLayout6);
\r
538 idPanelHolder.setLayout(borderLayout5);
\r
539 idSpaceFillerPanel1.setBackground(Color.white);
\r
541 // idSpaceFillerPanel1.setPreferredSize(new Dimension(10, 30));
\r
542 idSpaceFillerPanel1.setLayout(borderLayout11);
\r
543 annotationSpaceFillerHolder.setBackground(Color.white);
\r
545 // annotationSpaceFillerHolder.setPreferredSize(new Dimension(10, 80));
\r
546 annotationSpaceFillerHolder.setLayout(borderLayout4);
\r
547 hscroll.setOrientation(Scrollbar.HORIZONTAL);
\r
548 hscrollHolder.setLayout(borderLayout10);
\r
549 hscrollFillerPanel.setBackground(Color.white);
\r
551 // hscrollFillerPanel.setPreferredSize(new Dimension(70, 10));
\r
552 hscrollHolder.setBackground(Color.white);
\r
554 // annotationScroller.setPreferredSize(new Dimension(10, 80));
\r
555 // this.setPreferredSize(new Dimension(220, 166));
\r
556 seqPanelHolder.setBackground(Color.white);
\r
557 idPanelHolder.setBackground(Color.white);
\r
558 sequenceHolderPanel.add(scalePanelHolder, BorderLayout.NORTH);
\r
559 sequenceHolderPanel.add(seqPanelHolder, BorderLayout.CENTER);
\r
560 seqPanelHolder.add(vscroll, BorderLayout.EAST);
\r
562 // Panel3.add(secondaryPanelHolder, BorderLayout.SOUTH);
\r
563 this.add(idPanelHolder, BorderLayout.WEST);
\r
564 idPanelHolder.add(idSpaceFillerPanel1, BorderLayout.NORTH);
\r
565 idPanelHolder.add(annotationSpaceFillerHolder, BorderLayout.SOUTH);
\r
566 this.add(hscrollHolder, BorderLayout.SOUTH);
\r
567 hscrollHolder.add(hscroll, BorderLayout.CENTER);
\r
568 hscrollHolder.add(hscrollFillerPanel, BorderLayout.WEST);
\r
569 this.add(sequenceHolderPanel, BorderLayout.CENTER);
\r