X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FOverviewPanel.java;h=b3e3e66a73dc31d1cde3d803ea3ab61277db4f0f;hb=4a3def9f59cefe629c9a33d87483283aee085928;hp=e42d25f8b8daadea591b2661cc97f8222e0cad3d;hpb=d1707d4c26db76cfeb640f0dbeb3e3427fd40eb7;p=jalview.git diff --git a/src/jalview/gui/OverviewPanel.java b/src/jalview/gui/OverviewPanel.java index e42d25f..b3e3e66 100755 --- a/src/jalview/gui/OverviewPanel.java +++ b/src/jalview/gui/OverviewPanel.java @@ -20,22 +20,33 @@ */ package jalview.gui; -import jalview.datamodel.SequenceI; -import jalview.renderer.AnnotationRenderer; -import jalview.renderer.seqfeatures.FeatureColourFinder; +import jalview.bin.Cache; +import jalview.renderer.OverviewRenderer; +import jalview.util.MessageManager; +import jalview.util.Platform; import jalview.viewmodel.OverviewDimensions; +import jalview.viewmodel.OverviewDimensionsHideHidden; +import jalview.viewmodel.OverviewDimensionsShowHidden; +import jalview.viewmodel.ViewportListenerI; -import java.awt.Color; +import java.awt.BorderLayout; +import java.awt.Cursor; import java.awt.Dimension; -import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; -import java.awt.image.BufferedImage; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyVetoException; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JInternalFrame; import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; /** * Panel displaying an overview of the full alignment, with an interactive box @@ -44,34 +55,25 @@ import javax.swing.JPanel; * @author $author$ * @version $Revision$ */ -public class OverviewPanel extends JPanel implements Runnable +@SuppressWarnings("serial") +public class OverviewPanel extends JPanel + implements Runnable, ViewportListenerI { - private static final Color TRANS_GREY = new Color(100, 100, 100, 25); + protected OverviewDimensions od; - private final AnnotationRenderer renderer = new AnnotationRenderer(); + private OverviewCanvas oviewCanvas; - private OverviewDimensions od; - - private BufferedImage miniMe; - - private BufferedImage lastMiniMe = null; - - private AlignViewport av; + protected AlignViewport av; private AlignmentPanel ap; - // - private boolean resizing = false; + protected JCheckBoxMenuItem displayToggle; - // This is set true if the user resizes whilst - // the overview is being calculated - private boolean resizeAgain = false; + protected boolean showHidden = true; - // Can set different properties in this seqCanvas than - // main visible SeqCanvas - private SequenceRenderer sr; + protected boolean draggingBox = false; - jalview.renderer.seqfeatures.FeatureRenderer fr; + protected ProgressPanel progressPanel; /** * Creates a new OverviewPanel object. @@ -83,28 +85,63 @@ public class OverviewPanel extends JPanel implements Runnable { this.av = alPanel.av; this.ap = alPanel; - setLayout(null); - sr = new SequenceRenderer(av); - sr.renderGaps = false; - sr.forOverview = true; - fr = new FeatureRenderer(ap); + showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START, + false); + if (showHidden) + { + od = new OverviewDimensionsShowHidden(av.getRanges(), + (av.isShowAnnotation() + && av.getAlignmentConservationAnnotation() != null)); + } + else + { + od = new OverviewDimensionsHideHidden(av.getRanges(), + (av.isShowAnnotation() + && av.getAlignmentConservationAnnotation() != null)); + } + + setLayout(new BorderLayout()); + progressPanel = new ProgressPanel(OverviewRenderer.UPDATE, + MessageManager.getString("label.oview_calc"), getWidth()); + this.add(progressPanel, BorderLayout.SOUTH); + oviewCanvas = new OverviewCanvas(od, av, progressPanel); - od = new OverviewDimensions(av.getRanges(), - (av.isShowAnnotation() && av - .getAlignmentConservationAnnotation() != null)); + add(oviewCanvas, BorderLayout.CENTER); + + av.getRanges().addPropertyChangeListener(this); + + // without this the overview window does not size to fit the overview canvas + setPreferredSize(new Dimension(od.getWidth(), od.getHeight())); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent evt) { - if ((getWidth() != od.getWidth()) - || (getHeight() != (od.getHeight()))) + // Resize is called on the initial display of the overview. + // This code adjusts sizes to account for the progress bar if it has not + // already been accounted for, which triggers another resize call for + // the correct sizing, at which point the overview image is updated. + // (This avoids a double recalculation of the image.) + if (getWidth() == od.getWidth() && getHeight() == od.getHeight() + + progressPanel.getHeight()) { updateOverviewImage(); } + else + { + if ((getWidth() > 0) && (getHeight() > 0)) + { + od.setWidth(getWidth()); + od.setHeight(getHeight() - progressPanel.getHeight()); + } + + setPreferredSize(new Dimension(od.getWidth(), + od.getHeight() + progressPanel.getHeight())); + } } + }); addMouseMotionListener(new MouseMotionAdapter() @@ -112,14 +149,45 @@ public class OverviewPanel extends JPanel implements Runnable @Override public void mouseDragged(MouseEvent evt) { - if (!av.getWrapAlignment()) + if (!SwingUtilities.isRightMouseButton(evt)) + { + if (draggingBox) + { + // set the mouse position as a fixed point in the box + // and drag relative to that position + od.adjustViewportFromMouse(evt.getX(), evt.getY(), + av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); + } + else + { + od.updateViewportFromMouse(evt.getX(), evt.getY(), + av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); + } + } + } + + @Override + public void mouseMoved(MouseEvent evt) + { + if (od.isPositionInBox(evt.getX(), evt.getY())) + { + /* + * using HAND_CURSOR rather than DRAG_CURSOR + * as the latter is not supported on Mac + */ + getParent().setCursor( + Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + } + else { - od.updateViewportFromMouse(evt.getX(), evt.getY(), av - .getAlignment().getHiddenSequences(), av.getAlignment() - .getHiddenColumns(), av.getRanges()); - ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); + // reset cursor + getParent().setCursor( + Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); } } + }); addMouseListener(new MouseAdapter() @@ -127,191 +195,161 @@ public class OverviewPanel extends JPanel implements Runnable @Override public void mousePressed(MouseEvent evt) { - if (!av.getWrapAlignment()) + + if (Platform.isWinRightButton(evt)) + { + showPopupMenu(evt); + return; + } + if (SwingUtilities.isRightMouseButton(evt)) { - od.updateViewportFromMouse(evt.getX(), evt.getY(), av - .getAlignment().getHiddenSequences(), av.getAlignment() - .getHiddenColumns(), av.getRanges()); - ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); + return; + } + // don't do anything if the mouse press is in the overview's box + // (wait to see if it's a drag instead) + // otherwise update the viewport + if (!od.isPositionInBox(evt.getX(), evt.getY())) + { + draggingBox = false; + + // display drag cursor at mouse position + setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); + + od.updateViewportFromMouse(evt.getX(), evt.getY(), + av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); + getParent().setCursor( + Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + } + else + { + draggingBox = true; + od.setDragPoint(evt.getX(), evt.getY(), + av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); } } - }); - updateOverviewImage(); - } + @Override + public void mouseClicked(MouseEvent evt) + { + if (SwingUtilities.isRightMouseButton(evt)) + { + showPopupMenu(evt); + } + } - /** - * Updates the overview image when the related alignment panel is updated - */ - public void updateOverviewImage() - { - if (resizing) - { - resizeAgain = true; - return; - } + @Override + public void mouseReleased(MouseEvent evt) + { + draggingBox = false; + } - resizing = true; + }); - if ((getWidth() > 0) && (getHeight() > 0)) + /* + * Javascript does not call componentResized on initial display, + * so do the update here + */ + if (Platform.isJS()) { - od.setWidth(getWidth()); - od.setHeight(getHeight()); + updateOverviewImage(); } - - setPreferredSize(new Dimension(od.getWidth(), od.getHeight())); - - Thread thread = new Thread(this); - thread.start(); - repaint(); } - @Override - public void run() + /* + * Displays the popup menu and acts on user input + */ + protected void showPopupMenu(MouseEvent e) { - miniMe = null; - - if (av.isShowSequenceFeatures()) - { - fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer()); - } - - // why do we need to set preferred size again? was set in - // updateOverviewImage - setPreferredSize(new Dimension(od.getWidth(), od.getHeight())); - - miniMe = new BufferedImage(od.getWidth(), od.getHeight(), - BufferedImage.TYPE_INT_RGB); - - Graphics mg = miniMe.getGraphics(); - mg.setColor(Color.orange); - mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight()); - - // calculate sampleCol and sampleRow - // alignment width is max number of residues/bases - // alignment height is number of sequences - int alwidth = av.getAlignment().getWidth(); - int alheight = av.getAlignment().getAbsoluteHeight(); - - // sampleCol or sampleRow is the width/height allocated to each residue - // in particular, sometimes we may need more than one row/col of the - // BufferedImage allocated - // sampleCol is how much of a residue to assign to each pixel - // sampleRow is how many sequences to assign to each pixel - float sampleCol = alwidth / (float) od.getWidth(); - float sampleRow = alheight / (float) od.getSequencesHeight(); - - buildImage(sampleRow, sampleCol); - - // check for conservation annotation to make sure overview works for DNA too - if (av.isShowAnnotation() - && (av.getAlignmentConservationAnnotation() != null)) + JPopupMenu popup = new JPopupMenu(); + ActionListener menuListener = new ActionListener() { - renderer.updateFromAlignViewport(av); - for (int col = 0; col < od.getWidth() && !resizeAgain; col++) + @Override + public void actionPerformed(ActionEvent event) { - mg.translate(col, od.getSequencesHeight()); - renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(), - av.getAlignmentConservationAnnotation().annotations, - (int) (sampleCol) + 1, od.getGraphHeight(), - (int) (col * sampleCol), (int) (col * sampleCol) + 1); - mg.translate(-col, -od.getSequencesHeight()); - + // switch on/off the hidden columns view + toggleHiddenColumns(); + displayToggle.setSelected(showHidden); } - } - System.gc(); - - resizing = false; + }; + displayToggle = new JCheckBoxMenuItem( + MessageManager.getString("label.togglehidden")); + displayToggle.setEnabled(true); + displayToggle.setSelected(showHidden); + popup.add(displayToggle); + displayToggle.addActionListener(menuListener); + popup.show(this, e.getX(), e.getY()); + } - if (resizeAgain) + /* + * Toggle overview display between showing hidden columns and hiding hidden columns + */ + protected void toggleHiddenColumns() + { + if (showHidden) { - resizeAgain = false; - updateOverviewImage(); + showHidden = false; + od = new OverviewDimensionsHideHidden(av.getRanges(), + (av.isShowAnnotation() + && av.getAlignmentConservationAnnotation() != null)); } else { - lastMiniMe = miniMe; + showHidden = true; + od = new OverviewDimensionsShowHidden(av.getRanges(), + (av.isShowAnnotation() + && av.getAlignmentConservationAnnotation() != null)); } - + oviewCanvas.resetOviewDims(od); + updateOverviewImage(); setBoxPosition(); } - /* - * Build the overview panel image + /** + * Updates the overview image when the related alignment panel is updated */ - private void buildImage(float sampleRow, float sampleCol) + public void updateOverviewImage() { - int lastcol = -1; - int lastrow = -1; - int rgbColour = Color.white.getRGB(); - - SequenceI seq = null; - FeatureColourFinder finder = new FeatureColourFinder(fr); - - final boolean hasHiddenCols = av.hasHiddenColumns(); - boolean hiddenRow = false; - // get hidden row and hidden column map once at beginning. - // clone featureRenderer settings to avoid race conditions... if state is - // updated just need to refresh again - for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++) + if (oviewCanvas == null) { - boolean doCopy = true; - int currentrow = (int) (row * sampleRow); - if (currentrow != lastrow) - { - doCopy = false; - - lastrow = currentrow; - - // get the sequence which would be at alignment index 'lastrow' if no - // rows were hidden, and determine whether it is hidden or not - hiddenRow = av.getAlignment().isHidden(lastrow); - seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow); - } - - for (int col = 0; col < od.getWidth() && !resizeAgain; col++) - { - if (doCopy) - { - rgbColour = miniMe.getRGB(col, row - 1); - } - else if ((int) (col * sampleCol) != lastcol - || (int) (row * sampleRow) != lastrow) - { - lastcol = (int) (col * sampleCol); - rgbColour = getColumnColourFromSequence(seq, hiddenRow, - hasHiddenCols, lastcol, finder); - } - // else we just use the color we already have , so don't need to set it + /* + * panel has been disposed + */ + return; + } - miniMe.setRGB(col, row, rgbColour); - } + if ((getWidth() > 0) && (getHeight() > 0)) + { + od.setWidth(getWidth()); + od.setHeight(getHeight() - progressPanel.getHeight()); } - } - /* - * Find the colour of a sequence at a specified column position - */ - private int getColumnColourFromSequence( - jalview.datamodel.SequenceI seq, - boolean hiddenRow, boolean hasHiddenCols, int lastcol, - FeatureColourFinder finder) - { - Color color = Color.white; + setPreferredSize(new Dimension(od.getWidth(), + od.getHeight() + progressPanel.getHeight())); - if ((seq != null) && (seq.getLength() > lastcol)) + if (oviewCanvas.restartDraw()) { - color = sr.getResidueColour(seq, lastcol, finder); + return; } - if (hiddenRow - || (hasHiddenCols && !av.getAlignment().getHiddenColumns() - .isVisible(lastcol))) + Thread thread = new Thread(this, "UpdateOverview"); + thread.start(); + repaint(); + + } + + @Override + public void run() + { + if (oviewCanvas != null) { - color = color.darker().darker(); + oviewCanvas.draw(av.isShowSequenceFeatures(), + (av.isShowAnnotation() + && av.getAlignmentConservationAnnotation() != null), + ap.getSeqPanel().seqCanvas.getFeatureRenderer()); + setBoxPosition(); } - - return color.getRGB(); } /** @@ -319,43 +357,69 @@ public class OverviewPanel extends JPanel implements Runnable * changed * */ - public void setBoxPosition() + private void setBoxPositionOnly() { - od.setBoxPosition(av.getAlignment() -.getHiddenSequences(), av - .getAlignment().getHiddenColumns(), av.getRanges()); - repaint(); + if (od != null) + { + int oldX = od.getBoxX(); + int oldY = od.getBoxY(); + int oldWidth = od.getBoxWidth(); + int oldHeight = od.getBoxHeight(); + od.setBoxPosition(av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); + repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2); + repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(), + od.getBoxHeight()); + } } + private void setBoxPosition() + { + if (od != null) + { + od.setBoxPosition(av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); + repaint(); + } + } @Override - public void paintComponent(Graphics g) + public void propertyChange(PropertyChangeEvent evt) { - if (resizing || resizeAgain) + setBoxPositionOnly(); + } + + /** + * Removes this object as a property change listener, and nulls references + */ + protected void dispose() + { + try { - if (lastMiniMe == null) + if (av != null) { - g.setColor(Color.white); - g.fillRect(0, 0, getWidth(), getHeight()); + av.getRanges().removePropertyChangeListener(this); } - else - { - g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this); - } - g.setColor(TRANS_GREY); - g.fillRect(0, 0, getWidth(), getHeight()); - } - else if (lastMiniMe != null) + + oviewCanvas.dispose(); + + /* + * close the parent frame (which also removes it from the + * Desktop Windows menu) + */ + ((JInternalFrame) SwingUtilities + .getAncestorOfClass(JInternalFrame.class, (this))) + .setClosed(true); + } catch (PropertyVetoException e) { - g.drawImage(lastMiniMe, 0, 0, this); - if (lastMiniMe != miniMe) - { - g.setColor(TRANS_GREY); - g.fillRect(0, 0, getWidth(), getHeight()); - } + // ignore + } finally + { + progressPanel = null; + av = null; + oviewCanvas = null; + ap = null; + od = null; } - - g.setColor(Color.red); - od.drawBox(g); } }