X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAnnotationLabels.java;h=2517a9f99aca8b0fab5ec2a69218356310eb4e49;hb=943e92b0135861f6d400e10f12de8251222a9952;hp=d07cae270ebb4f259e8a21a7427f889fb8e9695e;hpb=3d0101179759ef157b088ea135423cd909512d9f;p=jalview.git diff --git a/src/jalview/gui/AnnotationLabels.java b/src/jalview/gui/AnnotationLabels.java index d07cae2..2517a9f 100755 --- a/src/jalview/gui/AnnotationLabels.java +++ b/src/jalview/gui/AnnotationLabels.java @@ -20,25 +20,28 @@ */ package jalview.gui; +import jalview.analysis.AlignSeq; import jalview.analysis.AlignmentUtils; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.Annotation; +import jalview.datamodel.HiddenColumns; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.io.FileFormat; import jalview.io.FormatAdapter; +import jalview.util.Comparison; import jalview.util.MessageManager; +import jalview.util.Platform; import java.awt.Color; +import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Image; -import java.awt.MediaTracker; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; @@ -48,11 +51,9 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.AffineTransform; -import java.awt.image.BufferedImage; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.regex.Pattern; +import java.util.Iterator; import javax.swing.JCheckBoxMenuItem; import javax.swing.JMenuItem; @@ -62,63 +63,75 @@ import javax.swing.SwingUtilities; import javax.swing.ToolTipManager; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * The panel that holds the labels for alignment annotations, providing + * tooltips, context menus, drag to reorder rows, and drag to adjust panel + * height */ public class AnnotationLabels extends JPanel implements MouseListener, MouseMotionListener, ActionListener { - private static final Pattern LEFT_ANGLE_BRACKET_PATTERN = Pattern - .compile("<"); + private static final String HTML_END_TAG = ""; + + private static final String HTML_START_TAG = ""; - String TOGGLE_LABELSCALE = MessageManager + /** + * width in pixels within which height adjuster arrows are shown and active + */ + private static final int HEIGHT_ADJUSTER_WIDTH = 50; + + /** + * height in pixels for allowing height adjuster to be active + */ + private static int HEIGHT_ADJUSTER_HEIGHT = 10; + + private static final Font font = new Font("Arial", Font.PLAIN, 11); + + private static final String TOGGLE_LABELSCALE = MessageManager .getString("label.scale_label_to_column"); - String ADDNEW = MessageManager.getString("label.add_new_row"); + private static final String ADDNEW = MessageManager + .getString("label.add_new_row"); - String EDITNAME = MessageManager + private static final String EDITNAME = MessageManager .getString("label.edit_label_description"); - String HIDE = MessageManager.getString("label.hide_row"); + private static final String HIDE = MessageManager + .getString("label.hide_row"); - String DELETE = MessageManager.getString("label.delete_row"); + private static final String DELETE = MessageManager + .getString("label.delete_row"); - String SHOWALL = MessageManager.getString("label.show_all_hidden_rows"); + private static final String SHOWALL = MessageManager + .getString("label.show_all_hidden_rows"); - String OUTPUT_TEXT = MessageManager.getString("label.export_annotation"); + private static final String OUTPUT_TEXT = MessageManager + .getString("label.export_annotation"); - String COPYCONS_SEQ = MessageManager + private static final String COPYCONS_SEQ = MessageManager .getString("label.copy_consensus_sequence"); - boolean resizePanel = false; - - Image image; + private final boolean debugRedraw = false; - AlignmentPanel ap; + private AlignmentPanel ap; AlignViewport av; - boolean resizing = false; - - MouseEvent dragEvent; + private MouseEvent dragEvent; - int oldY; + private int oldY; - int selectedRow; + private int selectedRow; private int scrollOffset = 0; - Font font = new Font("Arial", Font.PLAIN, 11); - private boolean hasHiddenRows; + private boolean resizePanel = false; + /** - * Creates a new AnnotationLabels object. + * Creates a new AnnotationLabels object * * @param ap - * DOCUMENT ME! */ public AnnotationLabels(AlignmentPanel ap) { @@ -126,30 +139,6 @@ public class AnnotationLabels extends JPanel av = ap.av; ToolTipManager.sharedInstance().registerComponent(this); - java.net.URL url = getClass().getResource("/images/idwidth.gif"); - Image temp = null; - - if (url != null) - { - temp = java.awt.Toolkit.getDefaultToolkit().createImage(url); - } - - try - { - MediaTracker mt = new MediaTracker(this); - mt.addImage(temp, 0); - mt.waitForID(0); - } catch (Exception ex) - { - } - - BufferedImage bi = new BufferedImage(temp.getHeight(this), - temp.getWidth(this), BufferedImage.TYPE_INT_RGB); - Graphics2D g = (Graphics2D) bi.getGraphics(); - g.rotate(Math.toRadians(90)); - g.drawImage(temp, 0, -bi.getWidth(this), this); - image = bi; - addMouseListener(this); addMouseMotionListener(this); addMouseWheelListener(ap.getAnnotationPanel()); @@ -266,9 +255,7 @@ public class AnnotationLabels extends JPanel } else if (evt.getActionCommand().equals(OUTPUT_TEXT)) { - new AnnotationExporter().exportAnnotations(ap, - new AlignmentAnnotation[] - { aa[selectedRow] }); + new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]); } else if (evt.getActionCommand().equals(COPYCONS_SEQ)) { @@ -607,10 +594,9 @@ public class AnnotationLabels extends JPanel } /** - * DOCUMENT ME! + * Reorders annotation rows after a drag of a label * * @param evt - * DOCUMENT ME! */ @Override public void mouseReleased(MouseEvent evt) @@ -625,6 +611,9 @@ public class AnnotationLabels extends JPanel getSelectedRow(evt.getY() - getScrollOffset()); int end = selectedRow; + /* + * if dragging to resize instead, start == end + */ if (start != end) { // Swap these annotations @@ -648,31 +637,13 @@ public class AnnotationLabels extends JPanel } /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mouseEntered(MouseEvent evt) - { - if (evt.getY() < 10) - { - resizePanel = true; - repaint(); - } - } - - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! + * Removes the height adjuster image on leaving the panel, unless currently + * dragging it */ @Override public void mouseExited(MouseEvent evt) { - if (dragEvent == null) + if (resizePanel && dragEvent == null) { resizePanel = false; repaint(); @@ -680,10 +651,11 @@ public class AnnotationLabels extends JPanel } /** - * DOCUMENT ME! + * A mouse drag may be either an adjustment of the panel height (if flag + * resizePanel is set on), or a reordering of the annotation rows. The former + * is dealt with by this method, the latter in mouseReleased. * * @param evt - * DOCUMENT ME! */ @Override public void mouseDragged(MouseEvent evt) @@ -705,7 +677,7 @@ public class AnnotationLabels extends JPanel d = ap.annotationSpaceFillerHolder.getPreferredSize(); ap.annotationSpaceFillerHolder .setPreferredSize(new Dimension(d.width, d.height - dif)); - ap.paintAlignment(true); + ap.paintAlignment(true, false); } ap.addNotify(); @@ -717,15 +689,14 @@ public class AnnotationLabels extends JPanel } /** - * DOCUMENT ME! + * Updates the tooltip as the mouse moves over the labels * * @param evt - * DOCUMENT ME! */ @Override public void mouseMoved(MouseEvent evt) { - resizePanel = evt.getY() < 10; + showOrHideAdjuster(evt); getSelectedRow(evt.getY() - getScrollOffset()); @@ -735,69 +706,89 @@ public class AnnotationLabels extends JPanel AlignmentAnnotation aa = ap.av.getAlignment() .getAlignmentAnnotation()[selectedRow]; - StringBuffer desc = new StringBuffer(); - if (aa.description != null - && !aa.description.equals("New description")) - { - // TODO: we could refactor and merge this code with the code in - // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature - // tooltips - desc.append(aa.getDescription(true).trim()); - // check to see if the description is an html fragment. - if (desc.length() < 6 || (desc.substring(0, 6).toLowerCase() - .indexOf("") < 0)) - { - // clean the description ready for embedding in html - desc = new StringBuffer(LEFT_ANGLE_BRACKET_PATTERN.matcher(desc) - .replaceAll("<")); - desc.insert(0, ""); - } - else - { - // remove terminating html if any - int i = desc.substring(desc.length() - 7).toLowerCase() - .lastIndexOf(""); - if (i > -1) - { - desc.setLength(desc.length() - 7 + i); - } - } - if (aa.hasScore()) - { - desc.append("
"); - } - // if (aa.hasProperties()) - // { - // desc.append(""); - // for (String prop : aa.getProperties()) - // { - // desc.append(""); - // } - // desc.append("
" + prop + "" - // + aa.getProperty(prop) + "
"); - // } - } - else + String desc = getTooltip(aa); + this.setToolTipText(desc); + } + } + + /** + * Answers a tooltip, formatted as html, containing the annotation description + * (prefixed by associated sequence id if applicable), and the annotation + * (non-positional) score if it has one. Answers null if neither description + * nor score is found. + * + * @param aa + * @return + */ + static String getTooltip(AlignmentAnnotation aa) + { + if (aa == null) + { + return null; + } + StringBuilder tooltip = new StringBuilder(); + if (aa.description != null && !aa.description.equals("New description")) + { + // TODO: we could refactor and merge this code with the code in + // jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature + // tooltips + String desc = aa.getDescription(true).trim(); + if (!desc.toLowerCase().startsWith(HTML_START_TAG)) { - // begin the tooltip's html fragment - desc.append(""); - if (aa.hasScore()) - { - // TODO: limit precision of score to avoid noise from imprecise - // doubles - // (64.7 becomes 64.7+/some tiny value). - desc.append(" Score: " + aa.score); - } + tooltip.append(HTML_START_TAG); + desc = desc.replace("<", "<"); } - if (desc.length() > 6) + else if (desc.toLowerCase().endsWith(HTML_END_TAG)) { - desc.append(""); - this.setToolTipText(desc.toString()); + desc = desc.substring(0, desc.length() - HTML_END_TAG.length()); } - else + tooltip.append(desc); + } + else + { + // begin the tooltip's html fragment + tooltip.append(HTML_START_TAG); + } + if (aa.hasScore()) + { + if (tooltip.length() > HTML_START_TAG.length()) { - this.setToolTipText(null); + tooltip.append("
"); } + // TODO: limit precision of score to avoid noise from imprecise + // doubles + // (64.7 becomes 64.7+/some tiny value). + tooltip.append(" Score: ").append(String.valueOf(aa.score)); + } + + if (tooltip.length() > HTML_START_TAG.length()) + { + return tooltip.append(HTML_END_TAG).toString(); + } + + /* + * nothing in the tooltip (except "") + */ + return null; + } + + /** + * Shows the height adjuster image if the mouse moves into the top left + * region, or hides it if the mouse leaves the regio + * + * @param evt + */ + protected void showOrHideAdjuster(MouseEvent evt) + { + boolean was = resizePanel; + resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT && evt.getX() < HEIGHT_ADJUSTER_WIDTH; + + if (resizePanel != was) + { + setCursor(Cursor.getPredefinedCursor( + resizePanel ? Cursor.S_RESIZE_CURSOR + : Cursor.DEFAULT_CURSOR)); + repaint(); } } @@ -820,11 +811,9 @@ public class AnnotationLabels extends JPanel // process modifiers SequenceGroup sg = ap.av.getSelectionGroup(); if (sg == null || sg == aa[selectedRow].groupRef - || !(jalview.util.Platform.isControlDown(evt) - || evt.isShiftDown())) + || !(Platform.isControlDown(evt) || evt.isShiftDown())) { - if (jalview.util.Platform.isControlDown(evt) - || evt.isShiftDown()) + if (Platform.isControlDown(evt) || evt.isShiftDown()) { // clone a new selection group from the associated group ap.av.setSelectionGroup( @@ -855,7 +844,7 @@ public class AnnotationLabels extends JPanel } } - ap.paintAlignment(false); + ap.paintAlignment(false, false); PaintRefresher.Refresh(ap, ap.av.getSequenceSetId()); ap.av.sendSelection(); } @@ -883,8 +872,7 @@ public class AnnotationLabels extends JPanel // we make a copy rather than edit the current selection if no // modifiers pressed // see Enhancement JAL-1557 - if (!(jalview.util.Platform.isControlDown(evt) - || evt.isShiftDown())) + if (!(Platform.isControlDown(evt) || evt.isShiftDown())) { sg = new SequenceGroup(sg); sg.clear(); @@ -892,7 +880,7 @@ public class AnnotationLabels extends JPanel } else { - if (jalview.util.Platform.isControlDown(evt)) + if (Platform.isControlDown(evt)) { sg.addOrRemove(aa[selectedRow].sequenceRef, true); } @@ -912,7 +900,7 @@ public class AnnotationLabels extends JPanel sg.addSequence(aa[selectedRow].sequenceRef, false); } ap.av.setSelectionGroup(sg); - ap.paintAlignment(false); + ap.paintAlignment(false, false); PaintRefresher.Refresh(ap, ap.av.getSequenceSetId()); ap.av.sendSelection(); } @@ -937,16 +925,17 @@ public class AnnotationLabels extends JPanel if (dseqs[0] == null) { dseqs[0] = new Sequence(sq); - dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps( - jalview.util.Comparison.GapChars, sq.getSequenceAsString())); + dseqs[0].setSequence(AlignSeq.extractGaps(Comparison.GapChars, + sq.getSequenceAsString())); sq.setDatasetSequence(dseqs[0]); } Alignment ds = new Alignment(dseqs); if (av.hasHiddenColumns()) { - omitHidden = av.getAlignment().getHiddenColumns() - .getVisibleSequenceStrings(0, sq.getLength(), seqs); + Iterator it = av.getAlignment().getHiddenColumns() + .getVisContigsIterator(0, sq.getLength(), false); + omitHidden = new String[] { sq.getSequenceStringFromIterator(it) }; } int[] alignmentStartEnd = new int[] { 0, ds.getWidth() - 1 }; @@ -962,12 +951,12 @@ public class AnnotationLabels extends JPanel Toolkit.getDefaultToolkit().getSystemClipboard() .setContents(new StringSelection(output), Desktop.instance); - ArrayList hiddenColumns = null; + HiddenColumns hiddenColumns = null; if (av.hasHiddenColumns()) { - hiddenColumns = av.getAlignment().getHiddenColumns() - .getHiddenColumnsCopy(); + hiddenColumns = new HiddenColumns( + av.getAlignment().getHiddenColumns()); } Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset @@ -1020,8 +1009,6 @@ public class AnnotationLabels extends JPanel drawComponent(g, false, width); } - private final boolean debugRedraw = false; - /** * Draw the full set of annotation Labels for the alignment at the given * cursor @@ -1204,11 +1191,7 @@ public class AnnotationLabels extends JPanel } } - if (resizePanel) - { - g.drawImage(image, 2, 0 - getScrollOffset(), this); - } - else if (dragEvent != null && aa != null) + if (!resizePanel && dragEvent != null && aa != null) { g.setColor(Color.lightGray); g.drawString(aa[selectedRow].label, dragEvent.getX(), @@ -1227,4 +1210,9 @@ public class AnnotationLabels extends JPanel { return scrollOffset; } + + @Override + public void mouseEntered(MouseEvent e) + { + } }