2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
24 import java.awt.image.*;
\r
26 import javax.swing.*;
\r
33 * @version $Revision$
\r
35 public class SeqCanvas extends JComponent
\r
37 final FeatureRenderer fr;
\r
38 final SequenceRenderer sr;
\r
44 SearchResults searchResults = null;
\r
45 boolean fastPaint = false;
\r
55 * Creates a new SeqCanvas object.
\r
57 * @param av DOCUMENT ME!
\r
59 public SeqCanvas(AlignViewport av)
\r
62 fr = new FeatureRenderer(av);
\r
63 sr = new SequenceRenderer(av);
\r
64 setLayout(new BorderLayout());
\r
65 PaintRefresher.Register(this, av.getSequenceSetId());
\r
66 setBackground(Color.white);
\r
69 MCview.PDBCanvas pdbCanvas;
\r
70 public SequenceRenderer getSequenceRenderer()
\r
75 public FeatureRenderer getFeatureRenderer()
\r
80 public void setPDBCanvas(MCview.PDBCanvas pc)
\r
85 public AlignViewport getViewport()
\r
94 * @param g DOCUMENT ME!
\r
95 * @param startx DOCUMENT ME!
\r
96 * @param endx DOCUMENT ME!
\r
97 * @param ypos DOCUMENT ME!
\r
99 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
101 int scalestartx = startx - (startx % 10) + 10;
\r
103 g.setColor(Color.black);
\r
106 for (int i = scalestartx; i < endx; i += 10)
\r
109 if(av.hasHiddenColumns)
\r
110 value = av.getColumnSelection().adjustForHiddenColumns(value);
\r
112 g.drawString( String.valueOf(value), (i - startx - 1) * av.charWidth,
\r
113 ypos - (av.charHeight / 2));
\r
115 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
\r
116 (ypos + 2) - (av.charHeight / 2),
\r
117 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
\r
125 * @param g DOCUMENT ME!
\r
126 * @param startx DOCUMENT ME!
\r
127 * @param endx DOCUMENT ME!
\r
128 * @param ypos DOCUMENT ME!
\r
130 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
132 FontMetrics fm = getFontMetrics(av.getFont());
\r
133 ypos += av.charHeight;
\r
135 if(av.hasHiddenColumns)
\r
137 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
\r
138 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
\r
141 int maxwidth = av.alignment.getWidth();
\r
142 if (av.hasHiddenColumns)
\r
143 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
\r
146 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
148 SequenceI seq = av.alignment.getSequenceAt(i);
\r
149 int index = startx;
\r
152 while (index < endx)
\r
154 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
161 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
168 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))-av.charWidth/2;
\r
169 g.drawString(value + "", x,
\r
170 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
178 * @param g DOCUMENT ME!
\r
179 * @param startx DOCUMENT ME!
\r
180 * @param endx DOCUMENT ME!
\r
181 * @param ypos DOCUMENT ME!
\r
183 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
185 ypos += av.charHeight;
\r
187 if(av.hasHiddenColumns)
\r
188 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
\r
192 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
194 seq = av.alignment.getSequenceAt(i);
\r
198 while (index > startx)
\r
200 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
207 value = seq.findPosition(index);
\r
214 g.drawString(String.valueOf(value), 0,
\r
215 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
223 * @param horizontal DOCUMENT ME!
\r
224 * @param vertical DOCUMENT ME!
\r
226 public void fastPaint(int horizontal, int vertical)
\r
236 gg.copyArea(horizontal * av.charWidth,
\r
237 vertical * av.charHeight,
\r
240 -horizontal * av.charWidth,
\r
241 -vertical * av.charHeight);
\r
243 int sr = av.startRes;
\r
244 int er = av.endRes;
\r
245 int ss = av.startSeq;
\r
246 int es = av.endSeq;
\r
251 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
254 transX = (er - sr - horizontal) * av.charWidth;
\r
255 sr = er - horizontal;
\r
257 else if (horizontal < 0)
\r
259 er = sr - horizontal-1;
\r
261 else if (vertical > 0) // scroll down
\r
263 ss = es - vertical;
\r
265 if (ss < av.startSeq)
\r
266 { // ie scrolling too fast, more than a page at a time
\r
271 transY = imgHeight - (vertical * av.charHeight);
\r
274 else if (vertical < 0)
\r
276 es = ss - vertical;
\r
278 if (es > av.endSeq)
\r
284 gg.translate(transX, transY);
\r
285 drawPanel(gg, sr, er, ss, es, 0);
\r
286 gg.translate(-transX, -transY);
\r
292 * Definitions of startx and endx (hopefully):
\r
293 * SMJS This is what I'm working towards!
\r
294 * startx is the first residue (starting at 0) to display.
\r
295 * endx is the last residue to display (starting at 0).
\r
296 * starty is the first sequence to display (starting at 0).
\r
297 * endy is the last sequence to display (starting at 0).
\r
298 * NOTE 1: The av limits are set in setFont in this class and
\r
299 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
302 // Set this to false to force a full panel paint
\r
303 public void paintComponent(Graphics g)
\r
305 super.paintComponent(g);
\r
309 if ( img != null && (fastPaint
\r
310 || (getVisibleRect().width != g.getClipBounds().width)
\r
311 || (getVisibleRect().height != g.getClipBounds().height)))
\r
313 g.drawImage(img, 0, 0, this);
\r
319 // this draws the whole of the alignment
\r
320 imgWidth = getWidth();
\r
321 imgHeight = getHeight();
\r
323 imgWidth -= (imgWidth % av.charWidth);
\r
324 imgHeight -= (imgHeight % av.charHeight);
\r
326 if ((imgWidth < 1) || (imgHeight < 1))
\r
331 if (img == null || imgWidth != img.getWidth() || imgHeight != img.getHeight())
\r
334 img = new BufferedImage(imgWidth, imgHeight,
\r
335 BufferedImage.TYPE_INT_RGB);
\r
336 gg = (Graphics2D) img.getGraphics();
\r
337 gg.setFont(av.getFont());
\r
338 }catch(OutOfMemoryError er)
\r
341 System.out.println(er +" making image, SeqCanvas");
\r
342 javax.swing.SwingUtilities.invokeLater(new Runnable()
\r
346 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.
\r
348 "Out of memory creating alignment image!!"
\r
350 "\nSee help files for increasing Java Virtual Machine memory."
\r
352 javax.swing.JOptionPane.WARNING_MESSAGE);
\r
361 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
362 RenderingHints.VALUE_ANTIALIAS_ON);
\r
364 gg.setColor(Color.white);
\r
365 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
368 if (av.getWrapAlignment())
\r
370 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
374 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
\r
377 g.drawImage(img, 0, 0, this);
\r
379 if (pdbCanvas != null)
\r
381 pdbCanvas.updateSeqColours();
\r
389 * @param cwidth DOCUMENT ME!
\r
391 * @return DOCUMENT ME!
\r
393 public int getWrappedCanvasWidth(int cwidth)
\r
395 FontMetrics fm = getFontMetrics(av.getFont());
\r
400 if (av.scaleRightWrapped)
\r
402 LABEL_EAST = fm.stringWidth(getMask());
\r
405 if (av.scaleLeftWrapped)
\r
407 LABEL_WEST = fm.stringWidth(getMask());
\r
410 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
415 * Generates a string of zeroes.
\r
420 String mask = "00";
\r
423 for(int i=0; i<av.alignment.getHeight(); i++)
\r
425 tmp = av.alignment.getSequenceAt(i).getEnd();
\r
430 for (int i = maxWidth; i > 0; i /= 10)
\r
440 * @param g DOCUMENT ME!
\r
441 * @param canvasWidth DOCUMENT ME!
\r
442 * @param canvasHeight DOCUMENT ME!
\r
443 * @param startRes DOCUMENT ME!
\r
445 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
448 AlignmentI al = av.getAlignment();
\r
450 FontMetrics fm = getFontMetrics(av.getFont());
\r
453 if (av.scaleRightWrapped)
\r
455 LABEL_EAST = fm.stringWidth(getMask());
\r
459 if (av.scaleLeftWrapped)
\r
461 LABEL_WEST = fm.stringWidth(getMask());
\r
464 int hgap = av.charHeight;
\r
465 if(av.scaleAboveWrapped)
\r
466 hgap += av.charHeight;
\r
468 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
469 int cHeight = av.getAlignment().getHeight() * av.charHeight;
\r
471 av.setWrappedWidth(cWidth);
\r
473 av.endRes = av.startRes + cWidth;
\r
478 int maxwidth = av.alignment.getWidth();
\r
480 if(av.hasHiddenColumns)
\r
481 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth)-1;
\r
483 while ((ypos <= canvasHeight) && (startRes < maxwidth))
\r
485 endx = startRes + cWidth -1;
\r
487 if (endx > maxwidth)
\r
492 g.setFont(av.getFont());
\r
493 g.setColor(Color.black);
\r
495 if (av.scaleLeftWrapped)
\r
497 drawWestScale(g, startRes, endx, ypos);
\r
500 if (av.scaleRightWrapped)
\r
502 g.translate(canvasWidth - LABEL_EAST, 0);
\r
503 drawEastScale(g, startRes, endx, ypos);
\r
504 g.translate(-(canvasWidth - LABEL_EAST), 0);
\r
507 g.translate(LABEL_WEST, 0);
\r
509 if (av.scaleAboveWrapped)
\r
511 drawNorthScale(g, startRes, endx, ypos);
\r
514 if (av.hasHiddenColumns && av.showHiddenMarkers)
\r
516 g.setColor(Color.blue);
\r
518 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
\r
521 res = av.getColumnSelection().findHiddenRegionPosition(i) -
\r
524 if (res < 0 || res > endx - startRes)
\r
527 gg.fillPolygon(new int[]
\r
528 {res * av.charWidth - av.charHeight / 4,
\r
529 res * av.charWidth + av.charHeight / 4,
\r
530 res * av.charWidth},
\r
533 ypos - (av.charHeight / 2),
\r
534 ypos - (av.charHeight / 2),
\r
535 ypos - (av.charHeight / 2) + 8
\r
543 // When printing we have an extra clipped region,
\r
544 // the Printable page which we need to account for here
\r
545 Shape clip = g.getClip();
\r
549 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
553 g.setClip(0, (int) clip.getBounds().getY(),
\r
554 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
557 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
\r
559 if(av.showAnnotation)
\r
561 g.translate(0, cHeight + ypos + 3);
\r
562 if(annotations==null)
\r
563 annotations = new AnnotationPanel(av);
\r
565 annotations.drawComponent( (Graphics2D) g, startRes, endx+1);
\r
566 g.translate(0, -cHeight - ypos -3);
\r
569 g.translate(-LABEL_WEST, 0);
\r
571 ypos += cHeight+getAnnotationHeight()+hgap;
\r
573 startRes += cWidth;
\r
577 AnnotationPanel annotations;
\r
578 int getAnnotationHeight()
\r
580 if(!av.showAnnotation)
\r
583 if(annotations==null)
\r
584 annotations = new AnnotationPanel(av);
\r
586 return annotations.adjustPanelHeight();
\r
592 * @param g1 DOCUMENT ME!
\r
593 * @param startRes DOCUMENT ME!
\r
594 * @param endRes DOCUMENT ME!
\r
595 * @param startSeq DOCUMENT ME!
\r
596 * @param endSeq DOCUMENT ME!
\r
597 * @param offset DOCUMENT ME!
\r
599 void drawPanel(Graphics g1, int startRes, int endRes,
\r
600 int startSeq, int endSeq, int offset)
\r
602 if(!av.hasHiddenColumns)
\r
604 draw(g1, startRes, endRes, startSeq, endSeq, offset);
\r
608 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
\r
611 int blockStart = startRes;
\r
612 int blockEnd = endRes;
\r
614 for (int i = 0; i < regions.size(); i++)
\r
616 int[] region = (int[]) regions.elementAt(i);
\r
617 int hideStart = region[0];
\r
618 int hideEnd = region[1];
\r
620 if (hideStart <= blockStart)
\r
622 blockStart += (hideEnd - hideStart) + 1;
\r
626 blockEnd = hideStart - 1;
\r
628 g1.translate(screenY * av.charWidth, 0);
\r
630 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
\r
632 if(av.getShowHiddenMarkers())
\r
634 g1.setColor(Color.blue);
\r
636 g1.drawLine( (blockEnd - blockStart + 1) * av.charWidth - 1,
\r
638 (blockEnd - blockStart + 1) * av.charWidth - 1,
\r
639 (endSeq - startSeq) * av.charHeight + offset);
\r
642 g1.translate( -screenY * av.charWidth, 0);
\r
643 screenY += blockEnd - blockStart + 1;
\r
644 blockStart = hideEnd + 1;
\r
647 if (screenY <= (endRes - startRes))
\r
649 blockEnd = blockStart + (endRes - startRes) - screenY;
\r
650 g1.translate(screenY * av.charWidth, 0);
\r
651 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
\r
653 g1.translate( -screenY * av.charWidth, 0);
\r
662 //int startRes, int endRes, int startSeq, int endSeq, int x, int y,
\r
663 // int x1, int x2, int y1, int y2, int startx, int starty,
\r
664 void draw(Graphics g,
\r
665 int startRes, int endRes,
\r
666 int startSeq, int endSeq,
\r
669 g.setFont(av.getFont());
\r
670 sr.prepare(g, av.renderGaps);
\r
674 /// First draw the sequences
\r
675 /////////////////////////////
\r
676 for (int i = startSeq; i < endSeq; i++)
\r
678 nextSeq = av.alignment.getSequenceAt(i);
\r
680 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
\r
682 offset + ( (i - startSeq) * av.charHeight));
\r
684 if (av.showSequenceFeatures)
\r
686 fr.drawSequence(g, nextSeq, startRes, endRes,
\r
687 offset + ((i - startSeq) * av.charHeight));
\r
690 /// Highlight search Results once all sequences have been drawn
\r
691 //////////////////////////////////////////////////////////
\r
692 if (searchResults != null)
\r
694 int[] visibleResults = searchResults.getResults(nextSeq, startRes, endRes);
\r
695 if (visibleResults != null)
\r
696 for (int r = 0; r < visibleResults.length; r += 2)
\r
698 sr.drawHighlightedText(nextSeq, visibleResults[r],
\r
699 visibleResults[r + 1],
\r
700 (visibleResults[r] - startRes) * av.charWidth,
\r
701 offset + ( (i - startSeq) * av.charHeight));
\r
705 if(av.cursorMode && cursorY==i
\r
706 && cursorX>=startRes && cursorX<=endRes)
\r
708 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
\r
709 offset + ( (i - startSeq) * av.charHeight));
\r
713 if(av.getSelectionGroup()!=null || av.alignment.getGroups().size()>0)
\r
714 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
\r
718 void drawGroupsBoundaries(Graphics g1,
\r
719 int startRes, int endRes,
\r
720 int startSeq, int endSeq,
\r
723 Graphics2D g = (Graphics2D)g1;
\r
725 /////////////////////////////////////
\r
726 // Now outline any areas if necessary
\r
727 /////////////////////////////////////
\r
728 SequenceGroup group = av.getSelectionGroup();
\r
733 int groupIndex = -1;
\r
734 int visWidth = (endRes - startRes +1) * av.charWidth;
\r
736 if ((group == null) && (av.alignment.getGroups().size() > 0))
\r
738 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
\r
749 boolean inGroup = false;
\r
753 for (i = startSeq; i < endSeq; i++)
\r
755 sx = (group.getStartRes() - startRes) * av.charWidth;
\r
756 sy = offset + ((i - startSeq) * av.charHeight);
\r
757 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) -
\r
760 if(sx+ex<0 || sx>visWidth)
\r
765 if ( (sx <= (endRes-startRes)*av.charWidth) &&
\r
766 group.getSequences(false).
\r
767 contains(av.alignment.getSequenceAt(i)))
\r
769 if ((bottom == -1) &&
\r
770 !group.getSequences(false).contains(
\r
771 av.alignment.getSequenceAt(i + 1)))
\r
773 bottom = sy + av.charHeight;
\r
778 if (((top == -1) && (i == 0)) ||
\r
779 !group.getSequences(false).contains(
\r
780 av.alignment.getSequenceAt(i - 1)))
\r
788 if (group == av.getSelectionGroup())
\r
790 g.setStroke(new BasicStroke(1,
\r
791 BasicStroke.CAP_BUTT,
\r
792 BasicStroke.JOIN_ROUND, 3f,
\r
793 new float[] { 5f, 3f }, 0f));
\r
794 g.setColor(Color.RED);
\r
798 g.setStroke(new BasicStroke());
\r
799 g.setColor(group.getOutlineColour());
\r
807 if (sx >= 0 && sx < visWidth)
\r
808 g.drawLine(sx, oldY, sx, sy);
\r
810 if (sx + ex < visWidth)
\r
811 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
819 if (sx + ex > visWidth)
\r
822 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
\r
823 ex = (endRes - startRes + 1) * av.charWidth;
\r
827 g.drawLine(sx, top, sx + ex, top);
\r
833 g.drawLine(sx, bottom, sx + ex, bottom);
\r
844 sy = offset + ( (i - startSeq) * av.charHeight);
\r
845 if (sx >= 0 && sx < visWidth)
\r
846 g.drawLine(sx, oldY, sx, sy);
\r
848 if (sx + ex < visWidth)
\r
849 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
857 if (sx + ex > visWidth)
\r
859 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
\r
860 ex = (endRes - startRes + 1) * av.charWidth;
\r
864 g.drawLine(sx, top, sx + ex, top);
\r
870 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
879 g.setStroke(new BasicStroke());
\r
881 if (groupIndex >= av.alignment.getGroups().size())
\r
886 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
\r
890 while (groupIndex < av.alignment.getGroups().size());
\r
899 * @param results DOCUMENT ME!
\r
901 public void highlightSearchResults(SearchResults results)
\r
905 searchResults = results;
\r