package jalview.gui; import java.awt.*; import java.awt.Graphics2D.*; import javax.swing.*; import jalview.datamodel.*; import jalview.analysis.*; public class IdCanvas extends JPanel { protected AlignViewport av; public boolean paintFlag = false; protected boolean showScores = true; protected int maxIdLength = -1; protected String maxIdStr = null; public IdCanvas(AlignViewport av) { setLayout(new BorderLayout()); this.av = av; PaintRefresher.Register(this); } public void drawIdString(Graphics gg,SequenceI ds,int i, int starty, int ypos) { int charHeight = av.getCharHeight(); if (av.getSelection().contains(ds)) { gg.setColor(Color.lightGray); gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight); gg.setColor(Color.white); } else { gg.setColor(ds.getColor()); gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight); gg.setColor(Color.black); } String string = ds.getName() + "/" + ds.getStart() + "-" + ds.getEnd(); gg.drawString(string,0,AlignmentUtil.getPixelHeight(starty,i,charHeight) + ypos + charHeight- (charHeight/5)); } public void paintComponent(Graphics gg) { AlignmentI da = av.getAlignment(); int charHeight = av.getCharHeight(); gg.setFont(av.getFont()); //Fill in the background gg.setColor(Color.white); gg.fillRect(0,0,getWidth(),getHeight()); Color currentColor = Color.white; Color currentTextColor = Color.black; //Which ids are we printing int starty = av.getStartSeq(); int endy = av.getEndSeq(); if (av.getWrapAlignment()) { starty = starty%av.getChunkHeight(); int ypos = 0; int rowstart = starty; if (starty == 0) ypos = 2*charHeight; else if (starty == 1) { starty = 0; ypos = charHeight; } endy = starty + da.getHeight(); if (endy > da.getHeight()) { endy = da.getHeight(); } for (int i = starty; i < endy; i++) { SequenceI s = da.getSequenceAt(i); drawIdString(gg,s,i,starty,ypos); } if (rowstart == 0) { ypos = ypos + av.getChunkHeight(); } else if (rowstart == 1) { ypos = ypos + av.getChunkHeight(); } else { ypos = ypos + av.getChunkHeight() - rowstart*charHeight; } starty = 0; int chunkwidth = av.getChunkWidth(); int startx = (int)(av.getEndSeq()/chunkwidth)*chunkwidth; int endx = startx + chunkwidth; while (ypos <= getHeight() && endx < da.getWidth()) { for (int i = starty; i < endy; i++) { SequenceI s = da.getSequenceAt(i); drawIdString(gg,s,i,starty,ypos); } ypos += av.getChunkHeight(); startx += chunkwidth; endx = startx + chunkwidth; if (endx > da.getWidth()) { endx = da.getWidth(); } starty = 0; if (endy > da.getHeight()) { endy = da.getHeight(); } } } else { //Now draw the id strings for (int i = starty; i < endy; i++) { // Selected sequence colours if (av.getSelection().contains(da.getSequenceAt(i))) { currentColor = Color.lightGray; currentTextColor = Color.black; } else { currentColor = da.getSequenceAt(i).getColor(); currentTextColor = Color.black; } gg.setColor(currentColor); gg.fillRect(0, AlignmentUtil.getPixelHeight(starty, i, charHeight), getWidth(), charHeight); gg.setColor(currentTextColor); String string = da.getSequenceAt(i).getDisplayId(); gg.drawString(string, 0, AlignmentUtil.getPixelHeight(starty, i, charHeight) + charHeight - (charHeight / 5)); } } } public Dimension getLabelWidth() { if(getGraphics()==null) return null; FontMetrics fm = this.getGraphics().getFontMetrics(av.font); AlignmentI al = av.getAlignment(); int i = 0; int idWidth = 0; while (i < al.getHeight() && al.getSequenceAt(i) != null) { SequenceI s = al.getSequenceAt(i); String str = s.getDisplayId(); if (fm.stringWidth(str) > idWidth) idWidth = fm.stringWidth(str); i++; } return new Dimension(idWidth + 10,getHeight()); } public Dimension getPreferredSize() { return getLabelWidth(); } }