+ * Add annotation types to 'Show annotations' and/or 'Hide annotations' menus.
+ * "All" is added first, followed by a separator. Then add any annotation
+ * types associated with the current selection. Separate menus are built for
+ * the selected sequence group (if any), and the selected sequence.
+ * <p>
+ * Some annotation rows are always rendered together - these can be identified
+ * by a common graphGroup property > -1. Only one of each group will be marked
+ * as visible (to avoid duplication of the display). For such groups we add a
+ * composite type name, e.g.
+ * <p>
+ * IUPredWS (Long), IUPredWS (Short)
+ *
+ * @param seq
+ */
+ protected void buildAnnotationTypesMenus(JMenu showMenu, JMenu hideMenu,
+ List<SequenceI> forSequences)
+ {
+ showMenu.removeAll();
+ hideMenu.removeAll();
+
+ final List<String> all = Arrays.asList(ALL_ANNOTATIONS);
+ addAnnotationTypeToShowHide(showMenu, forSequences, "", all, true, true);
+ addAnnotationTypeToShowHide(hideMenu, forSequences, "", all, true,
+ false);
+ showMenu.addSeparator();
+ hideMenu.addSeparator();
+
+ final AlignmentAnnotation[] annotations = ap.getAlignment()
+ .getAlignmentAnnotation();
+
+ /*
+ * Find shown/hidden annotations types, distinguished by source (calcId),
+ * and grouped by graphGroup. Using LinkedHashMap means we will retrieve in
+ * the insertion order, which is the order of the annotations on the
+ * alignment.
+ */
+ Map<String, List<List<String>>> shownTypes = new LinkedHashMap<String, List<List<String>>>();
+ Map<String, List<List<String>>> hiddenTypes = new LinkedHashMap<String, List<List<String>>>();
+ AlignmentAnnotationUtils.getShownHiddenTypes(shownTypes,
+ hiddenTypes,
+ AlignmentAnnotationUtils.asList(annotations),
+ forSequences);
+
+ for (String calcId : hiddenTypes.keySet())
+ {
+ for (List<String> type : hiddenTypes.get(calcId))
+ {
+ addAnnotationTypeToShowHide(showMenu, forSequences,
+ calcId, type, false, true);
+ }
+ }
+ // grey out 'show annotations' if none are hidden
+ showMenu.setEnabled(!hiddenTypes.isEmpty());
+
+ for (String calcId : shownTypes.keySet())
+ {
+ for (List<String> type : shownTypes.get(calcId))
+ {
+ addAnnotationTypeToShowHide(hideMenu, forSequences,
+ calcId, type, false, false);
+ }
+ }
+ // grey out 'hide annotations' if none are shown
+ hideMenu.setEnabled(!shownTypes.isEmpty());
+ }
+
+ /**
+ * Returns a list of sequences - either the current selection group (if there
+ * is one), else the specified single sequence.
+ *
+ * @param seq
+ * @return
+ */
+ protected List<SequenceI> getSequenceScope(SequenceI seq)
+ {
+ List<SequenceI> forSequences = null;
+ final SequenceGroup selectionGroup = ap.av.getSelectionGroup();
+ if (selectionGroup != null && selectionGroup.getSize() > 0)
+ {
+ forSequences = selectionGroup.getSequences();
+ }
+ else
+ {
+ forSequences = seq == null ? Collections.<SequenceI> emptyList()
+ : Arrays.asList(seq);
+ }
+ return forSequences;
+ }
+
+ /**
+ * Add one annotation type to the 'Show Annotations' or 'Hide Annotations'
+ * menus.
+ *
+ * @param showOrHideMenu
+ * the menu to add to
+ * @param forSequences
+ * the sequences whose annotations may be shown or hidden
+ * @param calcId
+ * @param types
+ * the label to add
+ * @param allTypes
+ * if true this is a special label meaning 'All'
+ * @param actionIsShow
+ * if true, the select menu item action is to show the annotation
+ * type, else hide
+ */
+ protected void addAnnotationTypeToShowHide(JMenu showOrHideMenu,
+ final List<SequenceI> forSequences, String calcId,
+ final List<String> types, final boolean allTypes,
+ final boolean actionIsShow)
+ {
+ String label = types.toString(); // [a, b, c]
+ label = label.substring(1, label.length() - 1);
+ final JMenuItem item = new JMenuItem(label);
+ item.setToolTipText(calcId);
+ item.addActionListener(new java.awt.event.ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ showHideAnnotation_actionPerformed(types, forSequences, allTypes,
+ actionIsShow);
+ }
+ });
+ showOrHideMenu.add(item);
+ }
+
+ /**
+ * Action on selecting a list of annotation type (or the 'all types' values)
+ * to show or hide for the specified sequences.
+ *
+ * @param types
+ * @param forSequences
+ * @param anyType
+ * @param doShow
+ */
+ protected void showHideAnnotation_actionPerformed(
+ Collection<String> types, List<SequenceI> forSequences,
+ boolean anyType, boolean doShow)
+ {
+ for (AlignmentAnnotation aa : ap.getAlignment()
+ .getAlignmentAnnotation())
+ {
+ if (anyType || types.contains(aa.label))
+ {
+ if ((aa.sequenceRef != null)
+ && forSequences.contains(aa.sequenceRef))
+ {
+ aa.visible = doShow;
+ }
+ }
+ }
+ refresh();
+ }
+
+ private void buildGroupURLMenu(SequenceGroup sg, Vector groupLinks)
+ {
+
+ // TODO: usability: thread off the generation of group url content so root
+ // menu appears asap
+ // sequence only URLs
+ // ID/regex match URLs
+ groupLinksMenu = new JMenu(
+ MessageManager.getString("action.group_link"));
+ JMenu[] linkMenus = new JMenu[]
+ { null, new JMenu(MessageManager.getString("action.ids")),
+ new JMenu(MessageManager.getString("action.sequences")),
+ new JMenu(MessageManager.getString("action.ids_sequences")) }; // three
+ // types
+ // of url
+ // that
+ // might
+ // be
+ // created.
+ SequenceI[] seqs = ap.av.getSelectionAsNewSequence();
+ String[][] idandseqs = GroupUrlLink.formStrings(seqs);
+ Hashtable commonDbrefs = new Hashtable();
+ for (int sq = 0; sq < seqs.length; sq++)
+ {
+
+ int start = seqs[sq].findPosition(sg.getStartRes()), end = seqs[sq]
+ .findPosition(sg.getEndRes());
+ // just collect ids from dataset sequence
+ // TODO: check if IDs collected from selecton group intersects with the
+ // current selection, too
+ SequenceI sqi = seqs[sq];
+ while (sqi.getDatasetSequence() != null)
+ {
+ sqi = sqi.getDatasetSequence();
+ }
+ DBRefEntry[] dbr = sqi.getDBRef();
+ if (dbr != null && dbr.length > 0)
+ {
+ for (int d = 0; d < dbr.length; d++)
+ {
+ String src = dbr[d].getSource(); // jalview.util.DBRefUtils.getCanonicalName(dbr[d].getSource()).toUpperCase();
+ Object[] sarray = (Object[]) commonDbrefs.get(src);
+ if (sarray == null)
+ {
+ sarray = new Object[2];
+ sarray[0] = new int[]
+ { 0 };
+ sarray[1] = new String[seqs.length];
+
+ commonDbrefs.put(src, sarray);
+ }
+
+ if (((String[]) sarray[1])[sq] == null)
+ {
+ if (!dbr[d].hasMap()
+ || (dbr[d].getMap().locateMappedRange(start, end) != null))
+ {
+ ((String[]) sarray[1])[sq] = dbr[d].getAccessionId();
+ ((int[]) sarray[0])[0]++;
+ }
+ }
+ }
+ }
+ }
+ // now create group links for all distinct ID/sequence sets.
+ boolean addMenu = false; // indicates if there are any group links to give
+ // to user
+ for (int i = 0; i < groupLinks.size(); i++)
+ {
+ String link = groupLinks.elementAt(i).toString();
+ GroupUrlLink urlLink = null;
+ try
+ {
+ urlLink = new GroupUrlLink(link);
+ } catch (Exception foo)
+ {
+ jalview.bin.Cache.log.error("Exception for GroupURLLink '" + link
+ + "'", foo);
+ continue;
+ }
+ ;
+ if (!urlLink.isValid())
+ {
+ jalview.bin.Cache.log.error(urlLink.getInvalidMessage());
+ continue;
+ }
+ final String label = urlLink.getLabel();
+ boolean usingNames = false;
+ // Now see which parts of the group apply for this URL
+ String ltarget = urlLink.getTarget(); // jalview.util.DBRefUtils.getCanonicalName(urlLink.getTarget());
+ Object[] idset = (Object[]) commonDbrefs.get(ltarget.toUpperCase());
+ String[] seqstr, ids; // input to makeUrl
+ if (idset != null)
+ {
+ int numinput = ((int[]) idset[0])[0];
+ String[] allids = ((String[]) idset[1]);
+ seqstr = new String[numinput];
+ ids = new String[numinput];
+ for (int sq = 0, idcount = 0; sq < seqs.length; sq++)
+ {
+ if (allids[sq] != null)
+ {
+ ids[idcount] = allids[sq];
+ seqstr[idcount++] = idandseqs[1][sq];
+ }
+ }
+ }
+ else
+ {
+ // just use the id/seq set
+ seqstr = idandseqs[1];
+ ids = idandseqs[0];
+ usingNames = true;
+ }
+ // and try and make the groupURL!
+
+ Object[] urlset = null;
+ try
+ {
+ urlset = urlLink.makeUrlStubs(ids, seqstr,
+ "FromJalview" + System.currentTimeMillis(), false);
+ } catch (UrlStringTooLongException e)
+ {
+ }
+ if (urlset != null)
+ {
+ int type = urlLink.getGroupURLType() & 3;
+ // System.out.println(urlLink.getGroupURLType()
+ // +" "+((String[])urlset[3])[0]);
+ // first two bits ofurlLink type bitfield are sequenceids and sequences
+ // TODO: FUTURE: ensure the groupURL menu structure can be generalised
+ addshowLink(linkMenus[type], label
+ + (((type & 1) == 1) ? ("("
+ + (usingNames ? "Names" : ltarget) + ")") : ""),
+ urlLink, urlset);
+ addMenu = true;
+ }
+ }
+ if (addMenu)
+ {
+ groupLinksMenu = new JMenu(
+ MessageManager.getString("action.group_link"));
+ for (int m = 0; m < linkMenus.length; m++)
+ {
+ if (linkMenus[m] != null
+ && linkMenus[m].getMenuComponentCount() > 0)
+ {
+ groupLinksMenu.add(linkMenus[m]);
+ }
+ }
+
+ groupMenu.add(groupLinksMenu);
+ }
+ }
+
+ /**