X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAlignmentPanel.java;h=a8b80d2077d84b5db70343d3a508c02c746674af;hb=ebb52a433edbbdbd31ab82cbb1c59fc116a72b1d;hp=d4788f2d4f8829936b7c3ec642bc84c626c669e2;hpb=2f75e4ae854e11477d7dac4591d7104de4dec7d9;p=jalview.git diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index d4788f2..a8b80d2 100755 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -7,10 +7,12 @@ import jalview.datamodel.*; import java.awt.*; import java.awt.event.*; import java.awt.print.*; - -import com.sun.image.codec.jpeg.*; import java.io.*; import java.awt.image.*; +import org.jibble.epsgraphics.*; +import javax.imageio.*; +import javax.swing.*; + public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListener, Printable @@ -20,10 +22,14 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene OverviewPanel overviewPanel; SeqPanel seqPanel; IdPanel idPanel; - SecondaryStructurePanel ssPanel; + IdwidthAdjuster idwidthAdjuster; public AlignFrame alignFrame; ScalePanel scalePanel; - ScorePanel scorePanel; + AnnotationPanel annotationPanel; + AnnotationLabels alabels; + + // this value is set false when selection area being dragged + boolean fastPaint = true; public AlignmentPanel(AlignFrame af, final AlignViewport av) { @@ -31,34 +37,32 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene this.av = av; seqPanel = new SeqPanel (av, this); idPanel = new IdPanel (av, this); + scalePanel = new ScalePanel(av, this); - scorePanel = new ScorePanel(av); - ssPanel = new SecondaryStructurePanel(av); - secondaryPanelHolder.add(ssPanel, BorderLayout.CENTER); idPanelHolder.add(idPanel, BorderLayout.CENTER); - idPanel.addNotify(); + idwidthAdjuster = new IdwidthAdjuster(this); + idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER); + + annotationPanel = new AnnotationPanel(this); + alabels = new AnnotationLabels(this); + + annotationSpaceFillerHolder.setPreferredSize(annotationPanel.getPreferredSize()); + annotationScroller.setPreferredSize(annotationPanel.getPreferredSize()); + annotationScroller.setViewportView(annotationPanel); + annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER); + + fontChanged(); + scalePanelHolder.add(scalePanel, BorderLayout.CENTER); - scorePanelHolder.add(scorePanel, BorderLayout.CENTER); seqPanelHolder.add(seqPanel, BorderLayout.CENTER); - setScrollValues(0,0); + + setScrollValues(0, 0); hscroll.addAdjustmentListener(this); vscroll.addAdjustmentListener(this); - addComponentListener(new ComponentAdapter() - { - public void componentResized(ComponentEvent evt) - { - RefreshPanels(); - } - }); - - - hscroll.setFocusable(false); - vscroll.setFocusable(false); setFocusable(true); - addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) @@ -66,19 +70,23 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene switch(evt.getKeyCode()) { case 27: // escape key - av.setRubberbandGroup(null); - RefreshPanels(); + av.setSelectionGroup(null); + repaint(); break; case KeyEvent.VK_X: + if(evt.isControlDown()) alignFrame.cut_actionPerformed(null); break; case KeyEvent.VK_C: + if(evt.isControlDown()) alignFrame.copy_actionPerformed(null); break; case KeyEvent.VK_V: + if(evt.isControlDown()) alignFrame.paste(true); break; case KeyEvent.VK_A: + if(evt.isControlDown()) alignFrame.selectAllSequenceMenuItem_actionPerformed(null); break; case KeyEvent.VK_DOWN: @@ -88,6 +96,7 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene alignFrame.moveSelectedSequences(true); break; case KeyEvent.VK_F: + if(evt.isControlDown()) alignFrame.findMenuItem_actionPerformed(null); break; } @@ -95,6 +104,71 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene }); } + public void fontChanged() + { + // set idCanvas bufferedImage to null + // to prevent drawing old image + FontMetrics fm = getFontMetrics(av.getFont()); + + scalePanelHolder.setPreferredSize(new Dimension(10, av.charHeight+fm.getDescent())); + idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av.charHeight+fm.getDescent())); + + idPanel.idCanvas.gg = null; + annotationPanel.adjustPanelHeight(); + Dimension d = calculateIdWidth(); + d.setSize(d.width + 4, d.height); + idPanel.idCanvas.setPreferredSize(d); + hscrollFillerPanel.setPreferredSize(d); + repaint(); + } + + + Dimension calculateIdWidth() + { + Graphics g = this.getGraphics(); + if(g==null) + { + javax.swing.JFrame f = new javax.swing.JFrame(); + f.addNotify(); + g = f.getGraphics(); + } + + FontMetrics fm = g.getFontMetrics(av.font); + AlignmentI al = av.getAlignment(); + + int i = 0; + int idWidth = 0; + String id; + while (i < al.getHeight() && al.getSequenceAt(i) != null) + { + SequenceI s = al.getSequenceAt(i); + if(av.getShowFullId()) + id = s.getDisplayId(); + else + id = s.getName(); + + if (fm.stringWidth(id) > idWidth) + idWidth = fm.stringWidth(id); + i++; + } + + // Also check annotation label widths + i=0; + if(al.getAlignmentAnnotation()!=null) + { + fm = g.getFontMetrics(alabels.getFont()); + while (i < al.getAlignmentAnnotation().length) + { + String label = al.getAlignmentAnnotation()[i].label; + if (fm.stringWidth(label) > idWidth) + idWidth = fm.stringWidth(label); + i++; + } + } + + return new Dimension(idWidth, 12); + } + public void highlightSearchResults(int [] results) { @@ -122,34 +196,25 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene } - public void setGraphPanelVisible(boolean b) + public void setAnnotationVisible(boolean b) { - idSpaceFillerPanel.setVisible(b); - scorePanelHolder.setVisible(b); - - RefreshPanels(); - // bit annoying to call this twice, can you do better? - RefreshPanels(); + annotationSpaceFillerHolder.setVisible(b); + annotationScroller.setVisible(b); } - public void setSecondaryStructureVisible(boolean b) - { - secondaryPanelHolder.setVisible(b); - RefreshPanels(); - } public void setWrapAlignment(boolean wrap) { - scorePanelHolder.setVisible(!wrap); scalePanelHolder.setVisible(!wrap); - secondaryPanelHolder.setVisible(!wrap); - hscroll.setVisible(!wrap); + idwidthAdjuster.setVisible(!wrap); - idSpaceFillerPanel.setVisible(!wrap); + av.setShowAnnotation(!wrap); + annotationScroller.setVisible(!wrap); + annotationSpaceFillerHolder.setVisible(!wrap); idSpaceFillerPanel1.setVisible(!wrap); - RefreshPanels(); + repaint(); } @@ -167,49 +232,74 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene al.getWidth() ); c.calculate(); - c.verdict(false, 100); + c.verdict(false, av.ConsPercGaps); ConservationColourScheme ccs = new ConservationColourScheme(c, cs); av.setGlobalColourScheme( ccs ); } - RefreshPanels(); + repaint(); } - public void RefreshPanels() - { - requestFocus(); - invalidate(); - idPanelHolder.setPreferredSize( idPanel.idCanvas.getPreferredSize() ); - if(av.getWrapAlignment()) - { - int max = av.alignment.getWidth()/ (seqPanel.seqCanvas.getWidth()/av.charWidth)+1; + int hextent = 0; + int vextent = 0; - int h = (av.alignment.getHeight() + 2)*av.charHeight; - vextent = seqPanel.seqCanvas.getHeight()/h; - vscroll.setValues(0,vextent,0,max); + // return value is true if the scroll is valid + public boolean scrollUp(boolean up) + { + if(up) + { + if(vscroll.getValue()<1) + return false; + fastPaint = false; + vscroll.setValue(vscroll.getValue() - 1); } else + { + if(vextent+vscroll.getValue() >= av.getAlignment().getHeight()) + return false; + fastPaint = false; + vscroll.setValue(vscroll.getValue() + 1); + } + fastPaint = true; + return true; + } - setScrollValues(av.getStartRes(), av.getStartSeq()); - av.getConsensus(true); - if(overviewPanel!=null) - overviewPanel.updateOverviewImage(); - - validate(); - repaint(); + public boolean scrollRight(boolean right) + { + if (right) + { + if (hscroll.getValue() < 1) + return false; + fastPaint = false; + hscroll.setValue(hscroll.getValue() - 1); + } + else + { + if (hextent + hscroll.getValue() >= av.getAlignment().getWidth()) + return false; + fastPaint = false; + hscroll.setValue(hscroll.getValue() + 1); + } + fastPaint = true; + return true; } - int hextent = 0; - int vextent = 0; public void setScrollValues(int x, int y) { - hextent = seqPanel.seqCanvas.getWidth()/av.getCharWidth(); - vextent = seqPanel.seqCanvas.getHeight()/av.getCharHeight(); + av.setEndRes(x + seqPanel.seqCanvas.getWidth()/av.getCharWidth()-1); + + hextent = seqPanel.seqCanvas.getWidth()/av.charWidth; + vextent = seqPanel.seqCanvas.getHeight()/av.charHeight; + + if(hextent > av.alignment.getWidth()) + hextent = av.alignment.getWidth(); + if(vextent > av.alignment.getHeight()) + vextent = av.alignment.getHeight(); if(hextent+x > av.getAlignment().getWidth()) x = av.getAlignment().getWidth()- hextent; @@ -227,13 +317,14 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene vscroll.setValues(y,vextent,0,av.getAlignment().getHeight() ); - repaint(); } public void adjustmentValueChanged(AdjustmentEvent evt) { + int oldX = av.getStartRes(); + int oldY = av.getStartSeq(); if (evt.getSource() == hscroll) { @@ -247,7 +338,9 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene int offy = vscroll.getValue(); if (av.getWrapAlignment()) { - av.setStartRes( vscroll.getValue() * av.getChunkWidth()); + int rowSize = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + av.setStartRes( vscroll.getValue() * rowSize ); + av.setEndRes( (vscroll.getValue()+1) * rowSize ); } else { @@ -256,11 +349,47 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene } } - if(overviewPanel!=null) overviewPanel.setBoxPosition(); - repaint(); + if(av.getWrapAlignment() || !fastPaint) + repaint(); + else + { + idPanel.idCanvas.fastPaint(av.getStartSeq() - oldY); + seqPanel.seqCanvas.fastPaint(av.getStartRes() - oldX, + av.getStartSeq() - oldY); + + scalePanel.repaint(); + if (av.getShowAnnotation()) + annotationPanel.fastPaint(av.getStartRes() - oldX); + } + + } + + + public void paintComponent(Graphics g) + { + invalidate(); + Dimension d = idPanel.idCanvas.getPreferredSize(); + idPanelHolder.setPreferredSize(d); + hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12)); + validate(); + + if (av.getWrapAlignment()) + { + int max = av.alignment.getWidth() / seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + vscroll.setMaximum(max); + vscroll.setUnitIncrement(1); + vscroll.setVisibleAmount(1); + } + else + { + setScrollValues(av.getStartRes(), av.getStartSeq()); + } + + + } public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException @@ -279,180 +408,294 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi) throws PrinterException { - int idWidth = (int)idPanel.idCanvas.getLabelWidth().getWidth(); + int idWidth = calculateIdWidth().width + 4; + FontMetrics fm = getFontMetrics(av.getFont()); + int scaleHeight = av.charHeight + fm.getDescent(); + pg.setColor(Color.white); + pg.fillRect(0, 0, pwidth, pheight); + pg.setFont(av.getFont()); - pg.setColor(Color.white); - pg.fillRect(0,0,pwidth, pheight); - pg.setFont( av.getFont() ); + //////////////////////////////////// + /// How many sequences and residues can we fit on a printable page? + int totalRes = (pwidth - idWidth) / av.getCharWidth(); - //////////////////////////////////// - /// How many sequences and residues can we fit on a printable page? - int totalRes = (pwidth - idWidth)/av.getCharWidth(); - int totalSeq = (int)((pheight - 30)/av.getCharHeight())-1; - int pagesWide = av.getAlignment().getWidth() / totalRes +1; - int pagesHigh = av.getAlignment().getHeight() / totalSeq +1; + int totalSeq = (int) ( (pheight - scaleHeight) / av.getCharHeight()) - 1; - if (pi >= pagesWide*pagesHigh) - return Printable.NO_SUCH_PAGE; + int pagesWide = av.getAlignment().getWidth() / totalRes + 1; - ///////////////////////////// - /// Only print these sequences and residues on this page - int startRes, endRes, startSeq, endSeq; - startRes = (pi % pagesWide) * totalRes; - endRes = startRes + totalRes-1; - if(endRes>av.getAlignment().getWidth()) - endRes = av.getAlignment().getWidth(); + ///////////////////////////// + /// Only print these sequences and residues on this page + int startRes, endRes, startSeq, endSeq; + startRes = (pi % pagesWide) * totalRes; + endRes = startRes + totalRes - 1; + if (endRes > av.getAlignment().getWidth()-1) + endRes = av.getAlignment().getWidth()-1; - startSeq = (pi / pagesWide) * totalSeq; - endSeq = startSeq + totalSeq; - if(endSeq > av.getAlignment().getHeight()) - endSeq = av.getAlignment().getHeight(); + startSeq = (pi / pagesWide) * totalSeq; + endSeq = startSeq + totalSeq; + if (endSeq > av.getAlignment().getHeight()) + endSeq = av.getAlignment().getHeight(); + int pagesHigh = ( (av.alignment.getHeight() / totalSeq) + 1) * pheight; - //////////////// - //draw Scale - pg.translate(idWidth,0); - scalePanel.scaleCanvas.drawScale(pg, startRes, endRes, pwidth-idWidth); + if (av.showAnnotation) + pagesHigh += annotationPanel.getHeight(); - pg.translate(-idWidth, 30); - //////////////// - // Draw the ids - Color currentColor=null; - Color currentTextColor=null; - for(int i=startSeq; i= pagesWide * pagesHigh) + return Printable.NO_SUCH_PAGE; + + //draw Scale + pg.translate(idWidth, 0); + scalePanel.drawScale(pg, startRes, endRes, pwidth - idWidth, scaleHeight); + pg.translate( -idWidth, scaleHeight); + //////////////// + // Draw the ids + Color currentColor = null; + Color currentTextColor = null; + for (int i = startSeq; i < endSeq; i++) { - currentColor = av.getAlignment().getSequenceAt(i).getColor(); - currentTextColor = Color.black; + if (av.getSelectionGroup() != null && + av.getSelectionGroup().sequences. + contains(av.getAlignment().getSequenceAt(i))) + { + currentColor = Color.gray; + currentTextColor = Color.black; + } + else + { + currentColor = av.getAlignment().getSequenceAt(i).getColor(); + currentTextColor = Color.black; + } + + pg.setColor(currentColor); + pg.fillRect(0, + jalview.analysis.AlignmentUtil.getPixelHeight(startSeq, i, + av.getCharHeight()), + idWidth, + av.getCharHeight()); + + pg.setColor(currentTextColor); + + String string = av.getAlignment().getSequenceAt(i).getName(); + if (av.getShowFullId()) + string = av.getAlignment().getSequenceAt(i).getDisplayId(); + + pg.drawString(string, 0, jalview.analysis.AlignmentUtil.getPixelHeight + (startSeq, i, av.getCharHeight()) + av.getCharHeight() - + (av.getCharHeight() / 5)); } - pg.setColor(currentColor); - pg.fillRect(0, jalview.analysis.AlignmentUtil.getPixelHeight(startSeq, i, av.getCharHeight()), - idWidth, - av.getCharHeight()); + // draw main sequence panel + pg.translate(idWidth, 0); + seqPanel.seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, + startRes, startSeq, 0); - pg.setColor(currentTextColor); - String string = av.getAlignment().getSequenceAt(i).getDisplayId(); - pg.drawString(string, 0, jalview.analysis.AlignmentUtil.getPixelHeight - (startSeq, i, av.getCharHeight()) + av.getCharHeight() - (av.getCharHeight() / 5)); - } + if (av.showAnnotation && endSeq == av.alignment.getHeight()) + { + pg.translate( -idWidth, (endSeq - startSeq) * av.charHeight); + alabels.drawComponent( (Graphics2D) pg); + pg.translate(idWidth, 0); + annotationPanel.drawComponent( (Graphics2D) pg, startRes, endRes+1); + } - // draw main sequence panel - pg.translate(idWidth,0); - pg.setClip(0,0,pwidth-idWidth, pheight); - seqPanel.seqCanvas.drawPanel(pg,startRes,endRes,startSeq,endSeq,startRes,startSeq,0); + return Printable.PAGE_EXISTS; - return Printable.PAGE_EXISTS; } public int printWrappedAlignment(Graphics pg, int pwidth, int pheight, int pi) throws PrinterException { + int idWidth = calculateIdWidth().width + 4; - int idWidth = (int)idPanel.idCanvas.getLabelWidth().getWidth(); + int resWidth = seqPanel.seqCanvas.getWrappedCanvasWidth(pwidth - idWidth); + int totalHeight = totalHeight = (av.alignment.getHeight() + 2) * + ( (av.alignment.getWidth() + / resWidth) + 1) * av.charHeight; - if( seqPanel.seqCanvas.getWidth() < pwidth-idWidth) - pwidth = seqPanel.seqCanvas.getWidth() + idWidth; + pg.setColor(Color.white); + pg.fillRect(0, 0, pwidth, pheight); + pg.setFont(av.getFont()); - pg.setColor(Color.white); - pg.fillRect(0,0,pwidth, pheight); - pg.setFont( av.getFont() ); + //////////////// + // Draw the ids + pg.setColor(Color.black); - //////////////////////////////////// - /// How many sequences and residues can we fit on a printable page? - AlignmentI da = av.alignment; - int endy = da.getHeight(); - int chunkHeight = (da.getHeight() + 2)*av.charHeight; - int chunkWidth = (pwidth-idWidth)/av.charWidth; + pg.translate(0, -pi * pheight); - int noChunksOnPage = pheight / chunkHeight; - int totalChunks = da.getWidth() / chunkWidth; + pg.setClip(0,pi*pheight,pwidth, pheight); - if ( pi*noChunksOnPage > totalChunks ) - return Printable.NO_SUCH_PAGE; + int ypos = 2 * av.charHeight; + do + { + for (int i = 0; i < av.alignment.getHeight(); i++) + { + SequenceI s = av.alignment.getSequenceAt(i); + String string = s.getName(); + if (av.getShowFullId()) + string = s.getDisplayId(); - //////////////// - // Draw the ids - pg.setClip(0,0,pwidth, noChunksOnPage*chunkHeight); + pg.drawString(string, 0, + AlignmentUtil.getPixelHeight(0, i, av.charHeight) + + ypos + + av.charHeight - (av.charHeight / 5)); + + } + + ypos += (av.alignment.getHeight() + 2) * av.charHeight; - int row = pi*noChunksOnPage; - pg.setColor(Color.black); - for(int ypos=2*av.charHeight; - ypos <= pheight && row*chunkWidth