*/
package jalview.appletgui;
+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.CheckboxMenuItem;
+import java.awt.Cursor;
import java.awt.Dimension;
-import java.awt.Frame;
-import java.awt.Graphics;
-import java.awt.Image;
import java.awt.Panel;
+import java.awt.PopupMenu;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
+import java.awt.event.InputEvent;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
+import java.beans.PropertyChangeEvent;
public class OverviewPanel extends Panel implements Runnable,
- MouseMotionListener, MouseListener
+ MouseMotionListener, MouseListener, ViewportListenerI
{
private OverviewDimensions od;
- private Image miniMe;
-
- private Image offscreen;
+ private OverviewCanvas oviewCanvas;
private AlignViewport av;
private AlignmentPanel ap;
- private boolean resizing = false;
-
- // This is set true if the user resizes whilst
- // the overview is being calculated
- private boolean resizeAgain = false;
+ private boolean showHidden = true;
- // Can set different properties in this seqCanvas than
- // main visible SeqCanvas
- private SequenceRenderer sr;
+ private boolean updateRunning = false;
- private FeatureRenderer fr;
+ private boolean draggingBox = false;
- private Frame nullFrame;
-
- public OverviewPanel(AlignmentPanel ap)
+ public OverviewPanel(AlignmentPanel alPanel)
{
- this.av = ap.av;
- this.ap = ap;
+ this.av = alPanel.av;
+ this.ap = alPanel;
setLayout(null);
- nullFrame = new Frame();
- nullFrame.addNotify();
-
- sr = new SequenceRenderer(av);
- sr.graphics = nullFrame.getGraphics();
- sr.renderGaps = false;
- sr.forOverview = true;
- fr = new FeatureRenderer(av);
-
- boolean showAnnotation = true;
- // TODO: in applet this was getSequenceConsensusHash()
- // check if it makes any functional difference: hconsensus or conservation
- if (av.getAlignmentConservationAnnotation() == null)
- {
- showAnnotation = false;
- }
- od = new OverviewDimensions(av.getPosProps(), showAnnotation);
+ od = new OverviewDimensionsShowHidden(av.getRanges(),
+ (av.isShowAnnotation() && av.getSequenceConsensusHash() != null));
+
+ oviewCanvas = new OverviewCanvas(od, av);
+ setLayout(new BorderLayout());
+ add(oviewCanvas, BorderLayout.CENTER);
setSize(new Dimension(od.getWidth(), od.getHeight()));
+
+ av.getRanges().addPropertyChangeListener(this);
+
addComponentListener(new ComponentAdapter()
{
@Override
public void mouseClicked(MouseEvent evt)
{
+ if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
+ {
+ showPopupMenu(evt);
+ }
}
@Override
public void mouseMoved(MouseEvent evt)
{
+ if (od.isPositionInBox(evt.getX(), evt.getY()))
+ {
+ // display drag cursor at mouse position
+ setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
+ }
+ else
+ {
+ // reset cursor
+ setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
+ }
}
@Override
public void mousePressed(MouseEvent evt)
{
- mouseAction(evt);
+ if ((evt.getModifiers()
+ & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
+ {
+ if (!Platform.isAMac())
+ {
+ showPopupMenu(evt);
+ }
+ }
+ else
+ {
+ if (!od.isPositionInBox(evt.getX(), evt.getY()))
+ {
+ // 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
+ od.updateViewportFromMouse(evt.getX(), evt.getY(),
+ av.getAlignment().getHiddenSequences(),
+ av.getAlignment().getHiddenColumns());
+ }
+ else
+ {
+ draggingBox = true;
+ od.setDragPoint(evt.getX(), evt.getY(),
+ av.getAlignment().getHiddenSequences(),
+ av.getAlignment().getHiddenColumns());
+ }
+ }
}
@Override
public void mouseReleased(MouseEvent evt)
{
- mouseAction(evt);
+ if (draggingBox)
+ {
+ draggingBox = false;
+ }
}
@Override
public void mouseDragged(MouseEvent evt)
{
- mouseAction(evt);
- }
-
- private void mouseAction(MouseEvent evt)
- {
- od.updateViewportFromMouse(evt.getX(), evt.getY(), av.getAlignment()
- .getHiddenSequences(), av.getColumnSelection(), av
- .getPosProps());
- ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
- ap.paintAlignment(false);
+ if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
+ {
+ if (!Platform.isAMac())
+ {
+ showPopupMenu(evt);
+ }
+ }
+ else
+ {
+ 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());
+ }
+ ap.paintAlignment(false);
+ }
}
/**
- * DOCUMENT ME!
+ * Updates the overview image when the related alignment panel is updated
*/
public void updateOverviewImage()
{
- if (resizing)
+ if (oviewCanvas == null)
{
- resizeAgain = true;
+ /*
+ * panel has been disposed
+ */
return;
}
- if (av.isShowSequenceFeatures())
+ if ((getSize().width > 0) && (getSize().height > 0))
{
- fr.transferSettings(ap.seqPanel.seqCanvas.fr);
+ od.setWidth(getSize().width);
+ od.setHeight(getSize().height);
}
+ setSize(new Dimension(od.getWidth(), od.getHeight()));
- resizing = true;
-
- if ((getWidth() > 0) && (getHeight() > 0))
+ synchronized (this)
{
- od.setWidth(getWidth()); // width = getWidth();
- od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
- }
- setSize(new Dimension(od.getWidth(), od.getHeight()));
+ if (updateRunning)
+ {
+ oviewCanvas.restartDraw();
+ return;
+ }
+ updateRunning = true;
+ }
Thread thread = new Thread(this);
thread.start();
repaint();
+ updateRunning = false;
}
@Override
public void run()
{
- miniMe = null;
-
- if (av.isShowSequenceFeatures())
- {
- fr.transferSettings(ap.seqPanel.seqCanvas.getFeatureRenderer());
- }
-
- if (getSize().width > 0 && getSize().height > 0)
- {
- od.setWidth(getSize().width);
- od.setHeight(getSize().height - od.getGraphHeight());
- }
-
- setSize(new Dimension(od.getWidth(), od.getHeight()));
-
- miniMe = nullFrame.createImage(od.getWidth(), od.getHeight());
- offscreen = nullFrame.createImage(od.getWidth(), od.getHeight());
-
- Graphics mg = miniMe.getGraphics();
-
- // od.updateScales();
-
- int alwidth = av.getAlignment().getWidth();
- int alheight = av.getAlignment().getAbsoluteHeight();
- float sampleCol = alwidth / (float) od.getWidth();
- float sampleRow = alheight / (float) od.getSequencesHeight();
-
- buildImage(sampleRow, sampleCol, mg);
-
- if (av.getAlignmentConservationAnnotation() != null)
- {
- for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
- {
- mg.translate(col, od.getSequencesHeight());
- ap.annotationPanel.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());
- }
- }
- System.gc();
-
- resizing = false;
-
+ oviewCanvas.draw(av.isShowSequenceFeatures(),
+ (av.isShowAnnotation() && av
+ .getAlignmentConservationAnnotation() != null),
+ ap.seqPanel.seqCanvas.getFeatureRenderer());
setBoxPosition();
-
- if (resizeAgain)
- {
- resizeAgain = false;
- updateOverviewImage();
- }
}
- private void buildImage(float sampleRow, float sampleCol, Graphics mg)
+ /**
+ * Update the overview panel box when the associated alignment panel is
+ * changed
+ *
+ */
+ private void setBoxPosition()
{
- int lastcol = 0;
- int lastrow = 0;
- int xstart = 0;
- int ystart = 0;
- Color color = Color.yellow;
- int sameRow = 0;
- int sameCol = 0;
-
- jalview.datamodel.SequenceI seq = null;
-
- final boolean hasHiddenCols = av.hasHiddenColumns();
- boolean hiddenRow = false;
+ od.setBoxPosition(av.getAlignment()
+.getHiddenSequences(), av
+ .getAlignment().getHiddenColumns());
+ repaint();
+ }
- for (int row = 0; row <= od.getSequencesHeight() && !resizeAgain; row++)
+ /*
+ * Displays the popup menu and acts on user input
+ */
+ private void showPopupMenu(MouseEvent e)
+ {
+ PopupMenu popup = new PopupMenu();
+ ItemListener menuListener = new ItemListener()
{
- if ((int) (row * sampleRow) == lastrow)
+ @Override
+ public void itemStateChanged(ItemEvent e)
{
- sameRow++;
+ toggleHiddenColumns();
}
- else
- {
- // get the sequence which would be at alignment index 'lastrow' if no
- // columns 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(); col++)
- {
- if ((int) (col * sampleCol) == lastcol
- && (int) (row * sampleRow) == lastrow)
- {
- sameCol++;
- }
- else
- {
- lastcol = (int) (col * sampleCol);
-
- color = getColumnColourFromSequence(seq, hiddenRow,
- hasHiddenCols, lastcol);
-
- mg.setColor(color);
- if (sameCol == 1 && sameRow == 1)
- {
- mg.drawLine(xstart, ystart, xstart, ystart);
- }
- else
- {
- mg.fillRect(xstart, ystart, sameCol, sameRow);
- }
-
- xstart = col;
- sameCol = 1;
- }
- }
- lastrow = (int) (row * sampleRow);
- ystart = row;
- sameRow = 1;
- }
- }
+ };
+ CheckboxMenuItem item = new CheckboxMenuItem(
+ MessageManager.getString("label.togglehidden"));
+ item.setState(showHidden);
+ popup.add(item);
+ item.addItemListener(menuListener);
+ this.add(popup);
+ popup.show(this, e.getX(), e.getY());
+ }
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ setBoxPosition();
}
/*
- * Find the colour of a sequence at a specified column position
+ * Toggle overview display between showing hidden columns and hiding hidden columns
*/
- private Color getColumnColourFromSequence(
- jalview.datamodel.SequenceI seq, boolean hiddenRow,
- boolean hasHiddenCols, int lastcol)
+ private void toggleHiddenColumns()
{
- Color color;
- if (seq.getLength() > lastcol)
+ if (showHidden)
{
- color = sr.getResidueBoxColour(seq, lastcol);
-
- if (av.isShowSequenceFeatures())
- {
- color = fr.findFeatureColour(color, seq, lastcol);
- }
+ showHidden = false;
+ od = new OverviewDimensionsHideHidden(av.getRanges(),
+ (av.isShowAnnotation() && av
+ .getAlignmentConservationAnnotation() != null));
}
else
{
- color = Color.white; // White
+ showHidden = true;
+ od = new OverviewDimensionsShowHidden(av.getRanges(),
+ (av.isShowAnnotation() && av
+ .getAlignmentConservationAnnotation() != null));
}
-
- if (hiddenRow
- || (hasHiddenCols && !av.getColumnSelection()
- .isVisible(lastcol)))
- {
- color = color.darker().darker();
- }
- return color;
+ oviewCanvas.resetOviewDims(od);
+ updateOverviewImage();
}
/**
- * Update the overview panel box when the associated alignment panel is
- * changed
- *
+ * Removes this object as a property change listener, and nulls references
*/
- public void setBoxPosition()
+ protected void dispose()
{
- od.setBoxPosition(av.getAlignment()
- .getHiddenSequences(), av.getColumnSelection(), av.getPosProps());
- repaint();
- }
-
- @Override
- public void update(Graphics g)
- {
- paint(g);
- }
-
- @Override
- public void paint(Graphics g)
- {
- Graphics og = offscreen.getGraphics();
- if (miniMe != null)
+ try
+ {
+ av.getRanges().removePropertyChangeListener(this);
+ } finally
{
- og.drawImage(miniMe, 0, 0, this);
- og.setColor(Color.red);
- od.drawBox(og);
- g.drawImage(offscreen, 0, 0, this);
+ av = null;
+ oviewCanvas = null;
+ ap = null;
+ od = null;
}
}
-
}