From 5e8ec77d921ff2d604811e5e4ba7e9211b0f48de Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 7 Jun 2017 07:58:34 +0100 Subject: [PATCH] JAL-2591 simplifying hidden columns usage --- src/jalview/appletgui/AlignFrame.java | 4 +- src/jalview/appletgui/AnnotationColumnChooser.java | 6 +- src/jalview/appletgui/AnnotationLabels.java | 10 +-- src/jalview/appletgui/ScalePanel.java | 3 +- src/jalview/appletgui/SeqCanvas.java | 2 +- src/jalview/datamodel/HiddenColumns.java | 84 +++++++++++++++++--- src/jalview/gui/AlignFrame.java | 38 ++++----- src/jalview/gui/AnnotationColumnChooser.java | 6 +- src/jalview/gui/AnnotationLabels.java | 9 +-- src/jalview/gui/Jalview2XML.java | 5 +- src/jalview/gui/ScalePanel.java | 3 +- src/jalview/gui/SeqCanvas.java | 6 +- src/jalview/io/AnnotationFile.java | 13 ++- src/jalview/io/JSONFile.java | 17 ++-- src/jalview/util/MappingUtils.java | 10 +-- 15 files changed, 124 insertions(+), 92 deletions(-) diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index 2eed311..02d30bb 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -1798,7 +1798,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, synchronized void slideSequences(boolean right, int size) { - List sg = new Vector(); + List sg = new Vector<>(); if (viewport.cursorMode) { sg.add(viewport.getAlignment().getSequenceAt( @@ -1911,7 +1911,7 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, SequenceGroup sg = viewport.getSelectionGroup(); copiedSequences = new StringBuffer(); - Map orderedSeqs = new HashMap(); + Map orderedSeqs = new HashMap<>(); for (int i = 0; i < sg.getSize(); i++) { SequenceI seq = sg.getSequenceAt(i); diff --git a/src/jalview/appletgui/AnnotationColumnChooser.java b/src/jalview/appletgui/AnnotationColumnChooser.java index 0a4748c..c49a5f3 100644 --- a/src/jalview/appletgui/AnnotationColumnChooser.java +++ b/src/jalview/appletgui/AnnotationColumnChooser.java @@ -298,11 +298,9 @@ public class AnnotationColumnChooser extends AnnotationRowFilter implements HiddenColumns oldHidden = av .getAnnotationColumnSelectionState() .getOldHiddenColumns(); - if (oldHidden != null - && !oldHidden.getHiddenRegions().isEmpty()) + if (oldHidden != null) { - for (Iterator itr = oldHidden.getHiddenRegions() - .iterator(); itr.hasNext();) + for (Iterator itr = oldHidden.iterator(); itr.hasNext();) { int positions[] = itr.next(); av.hideColumns(positions[0], positions[1]); diff --git a/src/jalview/appletgui/AnnotationLabels.java b/src/jalview/appletgui/AnnotationLabels.java index 307301d..9ba9cce 100755 --- a/src/jalview/appletgui/AnnotationLabels.java +++ b/src/jalview/appletgui/AnnotationLabels.java @@ -50,7 +50,6 @@ import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.Arrays; import java.util.Collections; -import java.util.Vector; public class AnnotationLabels extends Panel implements ActionListener, MouseListener, MouseMotionListener @@ -838,13 +837,8 @@ public class AnnotationLabels extends Panel implements ActionListener, + sq.getSequenceAsString() + "\n"); if (av.hasHiddenColumns()) { - jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector(); - for (int[] region : av.getAlignment().getHiddenColumns() - .getHiddenRegions()) - { - jalview.appletgui.AlignFrame.copiedHiddenColumns - .addElement(new int[] { region[0], region[1] }); - } + av.getAlignment().getHiddenColumns().getHiddenColumnsCopy( + jalview.appletgui.AlignFrame.copiedHiddenColumns); } } diff --git a/src/jalview/appletgui/ScalePanel.java b/src/jalview/appletgui/ScalePanel.java index ec3e246..7ead681 100755 --- a/src/jalview/appletgui/ScalePanel.java +++ b/src/jalview/appletgui/ScalePanel.java @@ -337,8 +337,7 @@ public class ScalePanel extends Panel implements MouseMotionListener, res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res); reveal = null; - for (int[] region : av.getAlignment().getHiddenColumns() - .getHiddenRegions()) + for (int[] region : av.getAlignment().getHiddenColumns()) { if (res + 1 == region[0] || res - 1 == region[1]) { diff --git a/src/jalview/appletgui/SeqCanvas.java b/src/jalview/appletgui/SeqCanvas.java index 46a908e..b96c491 100755 --- a/src/jalview/appletgui/SeqCanvas.java +++ b/src/jalview/appletgui/SeqCanvas.java @@ -570,7 +570,7 @@ public class SeqCanvas extends Panel implements ViewportListenerI if (av.hasHiddenColumns()) { HiddenColumns hidden = av.getAlignment().getHiddenColumns(); - for (int[] region : hidden.getHiddenRegions()) + for (int[] region : hidden) { int hideStart = region[0]; int hideEnd = region[1]; diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index da0b854..9722c0a 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -6,11 +6,12 @@ import jalview.util.ShiftList; import java.util.ArrayList; import java.util.BitSet; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Vector; import java.util.concurrent.locks.ReentrantReadWriteLock; -public class HiddenColumns +public class HiddenColumns implements Iterable { private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); @@ -567,18 +568,7 @@ public class HiddenColumns { if (copy.hiddenColumns != null) { - hiddenColumns = new Vector<>(copy.hiddenColumns.size()); - for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++) - { - int[] rh, cp; - rh = copy.hiddenColumns.elementAt(i); - if (rh != null) - { - cp = new int[rh.length]; - System.arraycopy(rh, 0, cp, 0, rh.length); - hiddenColumns.addElement(cp); - } - } + hiddenColumns = copy.copyHiddenRegions(); } } } @@ -588,6 +578,64 @@ public class HiddenColumns } } + private Vector copyHiddenRegions() + { + Vector copy = new Vector<>(hiddenColumns.size()); + for (int i = 0, j = hiddenColumns.size(); i < j; i++) + { + int[] rh, cp; + rh = hiddenColumns.elementAt(i); + if (rh != null) + { + cp = new int[rh.length]; + System.arraycopy(rh, 0, cp, 0, rh.length); + copy.addElement(cp); + } + } + return copy; + } + + private ArrayList copyHiddenRegionsToArrayList() + { + ArrayList copy = new ArrayList<>(hiddenColumns.size()); + for (int i = 0, j = hiddenColumns.size(); i < j; i++) + { + int[] rh, cp; + rh = hiddenColumns.elementAt(i); + if (rh != null) + { + cp = new int[rh.length]; + System.arraycopy(rh, 0, cp, 0, rh.length); + copy.add(cp); + } + } + return copy; + } + + public void getHiddenColumnsCopy(Vector copy) + { + try + { + lock.readLock().lock(); + copy = copyHiddenRegions(); + } finally + { + lock.readLock().unlock(); + } + } + + public void getHiddenColumnsCopy(ArrayList copy) + { + try + { + lock.readLock().lock(); + copy = copyHiddenRegionsToArrayList(); + } finally + { + lock.readLock().unlock(); + } + } + /** * propagate shift in alignment columns to column selection * @@ -1539,4 +1587,14 @@ public class HiddenColumns } } + @Override + public Iterator iterator() + { + if (hiddenColumns == null) + { + return Collections. emptyList().iterator(); + } + return hiddenColumns.iterator(); + } + } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index a9a970f..5ec9db9 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -167,7 +167,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, public AlignViewControllerI avc; - List alignPanels = new ArrayList(); + List alignPanels = new ArrayList<>(); /** * Last format used to load or save alignments in this window @@ -396,8 +396,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, addKeyListener(); - final List selviews = new ArrayList(); - final List origview = new ArrayList(); + final List selviews = new ArrayList<>(); + final List origview = new ArrayList<>(); final String menuLabel = MessageManager .getString("label.copy_format_from"); ViewSelectionMenu vsel = new ViewSelectionMenu(menuLabel, @@ -410,7 +410,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, origview.clear(); origview.add(alignPanel); // make an array of all alignment panels except for this one - List aps = new ArrayList( + List aps = new ArrayList<>( Arrays.asList(Desktop.getAlignmentPanels(null))); aps.remove(AlignFrame.this.alignPanel); return aps.toArray(new AlignmentPanel[aps.size()]); @@ -1732,7 +1732,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, synchronized void slideSequences(boolean right, int size) { - List sg = new ArrayList(); + List sg = new ArrayList<>(); if (viewport.cursorMode) { sg.add(viewport.getAlignment().getSequenceAt( @@ -1751,7 +1751,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, return; } - List invertGroup = new ArrayList(); + List invertGroup = new ArrayList<>(); for (SequenceI seq : viewport.getAlignment().getSequences()) { @@ -1884,9 +1884,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, ArrayList hiddenColumns = null; if (viewport.hasHiddenColumns()) { - hiddenColumns = new ArrayList(); - int hiddenOffset = viewport.getSelectionGroup().getStartRes(), hiddenCutoff = viewport - .getSelectionGroup().getEndRes(); + hiddenColumns = new ArrayList<>(); + int hiddenOffset = viewport.getSelectionGroup().getStartRes(), + hiddenCutoff = viewport.getSelectionGroup().getEndRes(); for (int[] region : viewport.getAlignment().getHiddenColumns() .getHiddenRegions()) { @@ -1994,7 +1994,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } int alwidth = 0; - ArrayList newGraphGroups = new ArrayList(); + ArrayList newGraphGroups = new ArrayList<>(); int fgroup = -1; if (newAlignment) @@ -2826,7 +2826,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, */ protected List getExistingViewNames(List comps) { - List existingNames = new ArrayList(); + List existingNames = new ArrayList<>(); for (Component comp : comps) { if (comp instanceof AlignmentPanel) @@ -3760,7 +3760,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, List comps = PaintRefresher.components.get(viewport .getSequenceSetId()); - List treePanels = new ArrayList(); + List treePanels = new ArrayList<>(); for (Component comp : comps) { if (comp instanceof TreePanel) @@ -4020,7 +4020,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, @Override public void run() { - final List legacyItems = new ArrayList(); + final List legacyItems = new ArrayList<>(); try { // System.err.println("Building ws menu again " @@ -4035,7 +4035,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, // TODO: group services by location as well as function and/or // introduce // object broker mechanism. - final Vector wsmenu = new Vector(); + final Vector wsmenu = new Vector<>(); final IProgressIndicator af = me; /* @@ -4403,8 +4403,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, // Java's Transferable for native dnd evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); Transferable t = evt.getTransferable(); - List files = new ArrayList(); - List protocols = new ArrayList(); + List files = new ArrayList<>(); + List protocols = new ArrayList<>(); try { @@ -4424,8 +4424,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, /** * Object[] { String,SequenceI} */ - ArrayList filesmatched = new ArrayList(); - ArrayList filesnotmatched = new ArrayList(); + ArrayList filesmatched = new ArrayList<>(); + ArrayList filesnotmatched = new ArrayList<>(); for (int i = 0; i < files.size(); i++) { String file = files.get(i).toString(); @@ -5416,7 +5416,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, { return; } - List cdnaSeqs = new ArrayList(); + List cdnaSeqs = new ArrayList<>(); for (SequenceI aaSeq : alignment.getSequences()) { for (AlignedCodonFrame acf : mappings) diff --git a/src/jalview/gui/AnnotationColumnChooser.java b/src/jalview/gui/AnnotationColumnChooser.java index 5122f29..405b43b 100644 --- a/src/jalview/gui/AnnotationColumnChooser.java +++ b/src/jalview/gui/AnnotationColumnChooser.java @@ -244,11 +244,9 @@ public class AnnotationColumnChooser extends AnnotationRowFilter implements HiddenColumns oldHidden = av .getAnnotationColumnSelectionState() .getOldHiddenColumns(); - if (oldHidden != null - && !oldHidden.getHiddenRegions().isEmpty()) + if (oldHidden != null) { - for (Iterator itr = oldHidden.getHiddenRegions() - .iterator(); itr.hasNext();) + for (Iterator itr = oldHidden.iterator(); itr.hasNext();) { int positions[] = itr.next(); av.hideColumns(positions[0], positions[1]); diff --git a/src/jalview/gui/AnnotationLabels.java b/src/jalview/gui/AnnotationLabels.java index 8ca1a4e..07a5ad9 100755 --- a/src/jalview/gui/AnnotationLabels.java +++ b/src/jalview/gui/AnnotationLabels.java @@ -967,14 +967,11 @@ public class AnnotationLabels extends JPanel implements MouseListener, .setContents(new StringSelection(output), Desktop.instance); ArrayList hiddenColumns = null; + if (av.hasHiddenColumns()) { - hiddenColumns = new ArrayList(); - for (int[] region : av.getAlignment().getHiddenColumns() - .getHiddenRegions()) - { - hiddenColumns.add(new int[] { region[0], region[1] }); - } + av.getAlignment().getHiddenColumns() + .getHiddenColumnsCopy(hiddenColumns); } Desktop.jalviewClipboard = new Object[] { seqs, ds, // what is the dataset diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 1675bf0..637b590 100644 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -1421,11 +1421,8 @@ public class Jalview2XML } else { - for (int c = 0; c < hidden.getHiddenRegions() - .size(); c++) + for (int[] region : hidden) { - int[] region = hidden.getHiddenRegions() - .get(c); HiddenColumns hc = new HiddenColumns(); hc.setStart(region[0]); hc.setEnd(region[1]); diff --git a/src/jalview/gui/ScalePanel.java b/src/jalview/gui/ScalePanel.java index 8a24937..81befd8 100755 --- a/src/jalview/gui/ScalePanel.java +++ b/src/jalview/gui/ScalePanel.java @@ -403,8 +403,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener, res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res); - for (int[] region : av.getAlignment().getHiddenColumns() - .getHiddenRegions()) + for (int[] region : av.getAlignment().getHiddenColumns()) { if (res + 1 == region[0] || res - 1 == region[1]) { diff --git a/src/jalview/gui/SeqCanvas.java b/src/jalview/gui/SeqCanvas.java index 0e5e1b8..6f3ff58 100755 --- a/src/jalview/gui/SeqCanvas.java +++ b/src/jalview/gui/SeqCanvas.java @@ -40,7 +40,6 @@ import java.awt.RenderingHints; import java.awt.Shape; import java.awt.image.BufferedImage; import java.beans.PropertyChangeEvent; -import java.util.List; import javax.swing.JComponent; @@ -662,14 +661,11 @@ public class SeqCanvas extends JComponent implements ViewportListenerI } else { - List regions = av.getAlignment().getHiddenColumns() - .getHiddenRegions(); - int screenY = 0; int blockStart = startRes; int blockEnd = endRes; - for (int[] region : regions) + for (int[] region : av.getAlignment().getHiddenColumns()) { int hideStart = region[0]; int hideEnd = region[1]; diff --git a/src/jalview/io/AnnotationFile.java b/src/jalview/io/AnnotationFile.java index c3e71da..2b78cb6 100755 --- a/src/jalview/io/AnnotationFile.java +++ b/src/jalview/io/AnnotationFile.java @@ -171,9 +171,8 @@ public class AnnotationFile if (cs != null && cs.hasHiddenColumns()) { text.append("VIEW_HIDECOLS\t"); - List hc = cs.getHiddenRegions(); boolean comma = false; - for (int[] r : hc) + for (int[] r : cs.getHiddenRegions()) { if (!comma) { @@ -202,8 +201,8 @@ public class AnnotationFile StringBuffer colours = new StringBuffer(); StringBuffer graphLine = new StringBuffer(); StringBuffer rowprops = new StringBuffer(); - Hashtable graphGroup = new Hashtable(); - Hashtable graphGroup_refs = new Hashtable(); + Hashtable graphGroup = new Hashtable<>(); + Hashtable graphGroup_refs = new Hashtable<>(); BitSet graphGroupSeen = new BitSet(); java.awt.Color color; @@ -748,8 +747,8 @@ public class AnnotationFile BufferedReader in) throws Exception { nlinesread = 0; - ArrayList combineAnnotation_calls = new ArrayList(); - ArrayList deferredAnnotation_calls = new ArrayList(); + ArrayList combineAnnotation_calls = new ArrayList<>(); + ArrayList deferredAnnotation_calls = new ArrayList<>(); boolean modified = false; String groupRef = null; Hashtable groupRefRows = new Hashtable(); @@ -1112,7 +1111,7 @@ public class AnnotationFile modified = true; } // Resolve the groupRefs - Hashtable groupRefLookup = new Hashtable(); + Hashtable groupRefLookup = new Hashtable<>(); Enumeration en = groupRefRows.keys(); while (en.hasMoreElements()) diff --git a/src/jalview/io/JSONFile.java b/src/jalview/io/JSONFile.java index 816346a..a48ee33 100644 --- a/src/jalview/io/JSONFile.java +++ b/src/jalview/io/JSONFile.java @@ -279,11 +279,8 @@ public class JSONFile extends AlignFile implements ComplexAlignFile // hidden column business if (getViewport().hasHiddenColumns()) { - List hiddenCols = getViewport().getAlignment() - .getHiddenColumns() - .getHiddenRegions(); StringBuilder hiddenColsBuilder = new StringBuilder(); - for (int[] range : hiddenCols) + for (int[] range : getViewport().getAlignment().getHiddenColumns()) { hiddenColsBuilder.append(";").append(range[0]).append("-") .append(range[1]); @@ -323,7 +320,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile SequenceI[] sqs, FeatureRenderer fr) { displayedFeatures = (fr == null) ? null : fr.getFeaturesDisplayed(); - List sequenceFeaturesPojo = new ArrayList(); + List sequenceFeaturesPojo = new ArrayList<>(); if (sqs == null) { return sequenceFeaturesPojo; @@ -374,7 +371,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile public static List annotationToJsonPojo( Vector annotations) { - List jsonAnnotations = new ArrayList(); + List jsonAnnotations = new ArrayList<>(); if (annotations == null) { return jsonAnnotations; @@ -471,8 +468,8 @@ public class JSONFile extends AlignFile implements ComplexAlignFile parseHiddenCols(jvSettingsJsonObj); } - hiddenSequences = new ArrayList(); - seqMap = new Hashtable(); + hiddenSequences = new ArrayList<>(); + seqMap = new Hashtable<>(); for (Iterator sequenceIter = seqJsonArray.iterator(); sequenceIter .hasNext();) { @@ -515,7 +512,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile int endRes = Integer.valueOf(seqGrpObj.get("endRes").toString()); JSONArray sequenceRefs = (JSONArray) seqGrpObj.get("sequenceRefs"); - ArrayList grpSeqs = new ArrayList(); + ArrayList grpSeqs = new ArrayList<>(); if (sequenceRefs.size() > 0) { Iterator seqHashIter = sequenceRefs.iterator(); @@ -649,7 +646,7 @@ public class JSONFile extends AlignFile implements ComplexAlignFile public void parseHiddenSeqRefsAsList(JSONObject jvSettingsJson) { - hiddenSeqRefs = new ArrayList(); + hiddenSeqRefs = new ArrayList<>(); String hiddenSeqs = (String) jvSettingsJson.get("hiddenSeqs"); if (hiddenSeqs != null && !hiddenSeqs.isEmpty()) { diff --git a/src/jalview/util/MappingUtils.java b/src/jalview/util/MappingUtils.java index 926ccc7..f75286a 100644 --- a/src/jalview/util/MappingUtils.java +++ b/src/jalview/util/MappingUtils.java @@ -108,7 +108,7 @@ public final class MappingUtils * Cache a copy of the target sequences so we can mimic successive edits on * them. This lets us compute mappings for all edits in the set. */ - Map targetCopies = new HashMap(); + Map targetCopies = new HashMap<>(); for (SequenceI seq : mapTo.getSequences()) { SequenceI ds = seq.getDatasetSequence(); @@ -433,7 +433,7 @@ public final class MappingUtils boolean undo, AlignmentI mapTo, List mappings) { SequenceI[] sortOrder = command.getSequenceOrder(undo); - List mappedOrder = new ArrayList(); + List mappedOrder = new ArrayList<>(); int j = 0; /* @@ -540,7 +540,7 @@ public final class MappingUtils toSequences, fromGapChar); } - for (int[] hidden : hiddencols.getHiddenRegions()) + for (int[] hidden : hiddencols) { mapHiddenColumns(hidden, codonFrames, newHidden, fromSequences, toSequences, fromGapChar); @@ -696,7 +696,7 @@ public final class MappingUtils public static List findCodonsFor(SequenceI seq, int col, List mappings) { - List result = new ArrayList(); + List result = new ArrayList<>(); int dsPos = seq.findPosition(col); for (AlignedCodonFrame mapping : mappings) { @@ -775,7 +775,7 @@ public final class MappingUtils SequenceI sequence, List mappings, List filterList) { - List result = new ArrayList(); + List result = new ArrayList<>(); if (sequence == null || mappings == null) { return result; -- 1.7.10.2