/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.appletgui; import java.awt.*; import java.awt.event.*; import jalview.analysis.*; import jalview.datamodel.*; import jalview.jbappletgui.*; import jalview.schemes.*; public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListener { AlignViewport av; OverviewPanel overviewPanel; SeqPanel seqPanel; IdPanel idPanel; IdwidthAdjuster idwidthAdjuster; public AlignFrame alignFrame; ScalePanel scalePanel; AnnotationPanel annotationPanel; AnnotationLabels alabels; // this value is set false when selection area being dragged boolean fastPaint = true; public AlignmentPanel(AlignFrame af, final AlignViewport av) { alignFrame = af; this.av = av; seqPanel = new SeqPanel(av, this); idPanel = new IdPanel(av, this); scalePanel = new ScalePanel(av, this); idwidthAdjuster = new IdwidthAdjuster(this); annotationPanel = new AnnotationPanel(this); alabels = new AnnotationLabels(this); idPanelHolder.add(idPanel, BorderLayout.CENTER); idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER); annotationScroller.add(annotationPanel); annotationSpaceFillerHolder.add(alabels, BorderLayout.CENTER); scalePanelHolder.add(scalePanel, BorderLayout.CENTER); seqPanelHolder.add(seqPanel, BorderLayout.CENTER); fontChanged(); setScrollValues(0, 0); hscroll.addAdjustmentListener(this); vscroll.addAdjustmentListener(this); seqPanel.seqCanvas.addKeyListener(new MyKeyAdapter()); idPanel.idCanvas.addKeyListener(new MyKeyAdapter()); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent evt) { setScrollValues(av.getStartRes(), av.getStartSeq()); repaint(); } }); Dimension d = calculateIdWidth(); idPanel.idCanvas.setSize(d); hscrollFillerPanel.setSize(d.width, annotationPanel.getSize().height); annotationScroller.setSize(annotationPanel.getSize()); idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height); annotationSpaceFillerHolder.setSize(d.width, annotationPanel.getSize().height); alabels.setSize(d.width, annotationPanel.getSize().height); } class MyKeyAdapter extends KeyAdapter { public void keyPressed(KeyEvent evt) { // System.out.println(evt.getKeyCode()); log. switch (evt.getKeyCode()) { case 27: // escape key av.setSelectionGroup(null); alignFrame.alignPanel.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: alignFrame.moveSelectedSequences(false); break; case KeyEvent.VK_UP: alignFrame.moveSelectedSequences(true); break; case KeyEvent.VK_F: if (evt.isControlDown()) { alignFrame.findMenuItem_actionPerformed(null); } break; case KeyEvent.VK_BACK_SPACE: case KeyEvent.VK_DELETE: alignFrame.cut_actionPerformed(null); break; } } } public void fontChanged() { // set idCanvas bufferedImage to null // to prevent drawing old image FontMetrics fm = getFontMetrics(av.getFont()); scalePanel.setSize(new Dimension(10, av.charHeight + fm.getDescent())); idwidthAdjuster.setSize(new Dimension(10, av.charHeight + fm.getDescent())); annotationPanel.adjustPanelHeight(); annotationPanel.repaint(); Dimension d = calculateIdWidth(); d.setSize(d.width + 4, seqPanel.seqCanvas.getSize().height); idPanel.idCanvas.setSize(d); hscrollFillerPanel.setSize(d); alignFrame.pack(); } public void setIdWidth(int w, int h) { idPanel.idCanvas.setSize(w, h); idPanelHolder.setSize(w, idPanelHolder.getSize().height); alabels.setSize(w, alabels.getSize().height); validate(); } Dimension calculateIdWidth() { Frame frame = new Frame(); frame.addNotify(); Graphics g = frame.getGraphics(); if (g == null) { Frame f = new Frame(); 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(frame.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, idPanel.idCanvas.getSize().height); } public void highlightSearchResults(int[] results) { seqPanel.seqCanvas.highlightSearchResults(results); // do we need to scroll the panel? if (results != null) { SequenceI seq = av.alignment.getSequenceAt(results[0]); int start = seq.findIndex(results[1]) - 1; int end = seq.findIndex(results[2]) - 1; if (av.getStartRes() > start || av.getEndRes() < end || (av.getStartSeq() > results[0] || av.getEndSeq() < results[0])) { setScrollValues(start, results[0]); } } } public OverviewPanel getOverviewPanel() { return overviewPanel; } public void setOverviewPanel(OverviewPanel op) { overviewPanel = op; } public void setAnnotationVisible(boolean b) { annotationSpaceFillerHolder.setVisible(b); annotationScroller.setVisible(b); validate(); repaint(); } public void setWrapAlignment(boolean wrap) { scalePanelHolder.setVisible(!wrap); hscroll.setVisible(!wrap); idwidthAdjuster.setVisible(!wrap); av.setShowAnnotation(!wrap); annotationScroller.setVisible(!wrap); annotationSpaceFillerHolder.setVisible(!wrap); idSpaceFillerPanel1.setVisible(!wrap); validate(); repaint(); } public void setColourScheme() { ColourSchemeI cs = av.getGlobalColourScheme(); if (av.getConservationSelected()) { Alignment al = (Alignment) av.getAlignment(); Conservation c = new Conservation("All", ResidueProperties.propHash, 3, al.getSequences(), 0, al.getWidth()); c.calculate(); c.verdict(false, av.ConsPercGaps); ConservationColourScheme ccs = new ConservationColourScheme(c, cs); av.setGlobalColourScheme(ccs); } repaint(); } int hextent = 0; int vextent = 0; // 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; } 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; } public void setScrollValues(int x, int y) { av.setStartRes(x); av.setStartSeq(y); av.setEndRes(x + seqPanel.seqCanvas.getSize().width / av.getCharWidth() - 1); hextent = seqPanel.seqCanvas.getSize().width / av.charWidth; vextent = seqPanel.seqCanvas.getSize().height / 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; } if (vextent + y > av.getAlignment().getHeight()) { y = av.getAlignment().getHeight() - vextent; } if (y < 0) { y = 0; } if (x < 0) { x = 0; } int endSeq = y + vextent; if (endSeq > av.alignment.getHeight()) { endSeq = av.alignment.getHeight(); } av.setEndSeq(endSeq); hscroll.setValues(x, hextent, 0, av.getAlignment().getWidth()); vscroll.setValues(y, vextent, 0, av.getAlignment().getHeight()); } public void adjustmentValueChanged(AdjustmentEvent evt) { int oldX = av.getStartRes(); int oldY = av.getStartSeq(); if (evt.getSource() == hscroll) { int x = hscroll.getValue(); av.setStartRes(x); av.setEndRes(x + seqPanel.seqCanvas.getSize().width / av.getCharWidth() - 1); } if (evt.getSource() == vscroll) { int offy = vscroll.getValue(); if (av.getWrapAlignment()) { int rowSize = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel. seqCanvas.getSize().width); av.setStartRes(vscroll.getValue() * rowSize); av.setEndRes( (vscroll.getValue() + 1) * rowSize); } else { av.setStartSeq(offy); av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height / av.getCharHeight()); } } if (overviewPanel != null) { overviewPanel.setBoxPosition(); } 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 update(Graphics g) { paint(g); } public void paint(Graphics g) { Dimension d = idPanel.idCanvas.getSize(); idPanel.idCanvas.setSize(d.width, seqPanel.seqCanvas.getSize().height); annotationSpaceFillerHolder.setSize(d.width, annotationPanel.getSize().height); alabels.setSize(d.width, annotationPanel.getSize().height); alabels.repaint(); idPanel.idCanvas.repaint(); seqPanel.seqCanvas.repaint(); scalePanel.repaint(); annotationPanel.repaint(); if (av.getWrapAlignment()) { int max = av.alignment.getWidth() / seqPanel.seqCanvas. getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width); vscroll.setMaximum(max); vscroll.setUnitIncrement(1); vscroll.setVisibleAmount(1); } else { setScrollValues(av.getStartRes(), av.getStartSeq()); } } }