+ /**
+ * Check for any annotations on the underlying dataset sequences (for the
+ * current selection group) which are not 'on the alignment'.If any are found,
+ * enable the option to add them to the alignment. The criteria for 'on the
+ * alignment' is finding an alignment annotation on the alignment, matched on
+ * calcId, label and sequenceRef.
+ *
+ * A tooltip is also constructed that displays the source (calcId) and type
+ * (label) of the annotations that can be added.
+ *
+ * @param menuItem
+ * @param forSequences
+ */
+ protected void configureReferenceAnnotationsMenu(
+ JMenuItem menuItem, List<SequenceI> forSequences)
+ {
+ menuItem.setText(MessageManager
+ .getString("label.add_reference_annotations"));
+ menuItem.setEnabled(false);
+ if (forSequences == null)
+ {
+ return;
+ }
+
+ /*
+ * Temporary store to hold distinct calcId / type pairs for the tooltip.
+ * Using TreeMap means calcIds are shown in alphabetical order.
+ */
+ Map<String, String> tipEntries = new TreeMap<String, String>();
+ StringBuilder tooltip = new StringBuilder(64);
+ tooltip.append(MessageManager.getString("label.add_annotations_for"));
+
+ /*
+ * For each sequence selected in the alignment, make a list of any
+ * annotations on the underlying dataset sequence which are not already on
+ * the alignment.
+ *
+ * Build a map of { alignmentSequence, <List of annotations to add> }
+ */
+ AlignmentI al = this.ap.av.getAlignment();
+ final Map<SequenceI, List<AlignmentAnnotation>> candidates = new LinkedHashMap<SequenceI, List<AlignmentAnnotation>>();
+ for (SequenceI seq : forSequences)
+ {
+ SequenceI dataset = seq.getDatasetSequence();
+ if (dataset == null)
+ {
+ continue;
+ }
+ AlignmentAnnotation[] datasetAnnotations = dataset.getAnnotation();
+ if (datasetAnnotations == null)
+ {
+ continue;
+ }
+ final List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
+ for (AlignmentAnnotation dsann : datasetAnnotations)
+ {
+ /*
+ * Find matching annotations on the alignment.
+ */
+ final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
+ .findAnnotations(seq, dsann.getCalcId(),
+ dsann.label);
+ if (!matchedAlignmentAnnotations.iterator().hasNext())
+ {
+ result.add(dsann);
+ tipEntries.put(dsann.getCalcId(), dsann.label);
+ }
+ }
+ /*
+ * Save any addable annotations for this sequence
+ */
+ if (!result.isEmpty())
+ {
+ candidates.put(seq, result);
+ }
+ }
+ if (!candidates.isEmpty())
+ {
+ /*
+ * Found annotations that could be added. Enable the menu item, and
+ * configure its tooltip and action.
+ */
+ menuItem.setEnabled(true);
+ for (String calcId : tipEntries.keySet())
+ {
+ tooltip.append("<br/>" + calcId + "/" + tipEntries.get(calcId));
+ }
+ String tooltipText = JvSwingUtils.wrapTooltip(true,
+ tooltip.toString());
+ menuItem.setToolTipText(tooltipText);
+
+ menuItem.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ addReferenceAnnotations_actionPerformed(candidates);
+ }
+ });
+ }
+ }
+
+ /**
+ * Add annotations to the sequences and to the alignment.
+ *
+ * @param candidates
+ * a map whose keys are sequences on the alignment, and values a list
+ * of annotations to add to each sequence
+ */
+ protected void addReferenceAnnotations_actionPerformed(
+ Map<SequenceI, List<AlignmentAnnotation>> candidates)
+ {
+ /*
+ * Add annotations at the top of the annotation, in the same order as their
+ * related sequences.
+ */
+ for (SequenceI seq : candidates.keySet())
+ {
+ for (AlignmentAnnotation ann : candidates.get(seq))
+ {
+ AlignmentAnnotation copyAnn = new AlignmentAnnotation(ann);
+ int startRes = 0;
+ int endRes = ann.annotations.length;
+ final SequenceGroup selectionGroup = this.ap.av.getSelectionGroup();
+ if (selectionGroup != null)
+ {
+ startRes = selectionGroup.getStartRes();
+ endRes = selectionGroup.getEndRes();
+ }
+ copyAnn.restrict(startRes, endRes);
+
+ /*
+ * Add to the sequence (sets copyAnn.datasetSequence), unless the
+ * original annotation is already on the sequence.
+ */
+ if (!seq.hasAnnotation(ann))
+ {
+ seq.addAlignmentAnnotation(copyAnn);
+ }
+ // adjust for gaps
+ copyAnn.adjustForAlignment();
+ // add to the alignment and set visible
+ this.ap.getAlignment().addAnnotation(copyAnn);
+ copyAnn.visible = true;
+ }
+ }
+ refresh();
+ }
+
+ protected void sequenceSelectionDetails_actionPerformed()
+ {
+ createSequenceDetailsReport(ap.av.getSequenceSelection());
+ }
+
+ protected void sequenceDetails_actionPerformed()
+ {
+ createSequenceDetailsReport(new SequenceI[]
+ { sequence });
+ }
+
+ public void createSequenceDetailsReport(SequenceI[] sequences)
+ {
+ CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
+ StringBuffer contents = new StringBuffer();
+ for (SequenceI seq : sequences)
+ {
+ contents.append("<p><h2>"
+ + MessageManager
+ .formatMessage(
+ "label.create_sequence_details_report_annotation_for",
+ new String[]
+ { seq.getDisplayId(true) }) + "</h2></p><p>");
+ new SequenceAnnotationReport(null)
+ .createSequenceAnnotationReport(
+ contents,
+ seq,
+ true,
+ true,
+ false,
+ (ap.getSeqPanel().seqCanvas.fr != null) ? ap
+ .getSeqPanel().seqCanvas.fr
+ .getMinMax()
+ : null);
+ contents.append("</p>");
+ }
+ cap.setText("<html>" + contents.toString() + "</html>");
+
+ Desktop.instance.addInternalFrame(cap, MessageManager.formatMessage(
+ "label.sequece_details_for",
+ (sequences.length == 1 ? new String[]
+ { sequences[0].getDisplayId(true) } : new String[]
+ { MessageManager.getString("label.selection") })), 500, 400);
+
+ }
+