diff --git a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/TrackEntry.java b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/TrackEntry.java index b41ace91..97c79f40 100644 --- a/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/TrackEntry.java +++ b/plugins/com.minres.scviewer.database.ui.swt/src/com/minres/scviewer/database/ui/TrackEntry.java @@ -134,6 +134,20 @@ public class TrackEntry { if (siblings == null) return null; List members = siblings.stream() .filter(entry -> selected.stream().anyMatch(track -> track == entry)).toList(); + TrackEntry owner = parentOf(roots, siblings); + if (owner != null && owner.isBitVector()) { + List copies = new ArrayList<>(); + for (TrackEntry member : members) { + TrackEntry copy = new TrackEntry(member.waveform, styles); + copy.valueDisplay = member.valueDisplay; + copy.waveDisplay = member.waveDisplay; + copies.add(copy); + } + TrackEntry group = group(name, copies, styles); + List destination = siblingsOf(roots, owner); + destination.add(destination.indexOf(owner) + 1, group); + return group; + } int index = siblings.indexOf(members.get(0)); TrackEntry group = group(name, members, styles); siblings.removeAll(members); @@ -141,6 +155,16 @@ public class TrackEntry { return group; } + /** Find the display parent owning a particular child list. */ + private static TrackEntry parentOf(List roots, List children) { + for (TrackEntry entry : roots) { + if (entry.waveforms == children) return entry; + TrackEntry parent = parentOf(entry.waveforms, children); + if (parent != null) return parent; + } + return null; + } + public static boolean ungroupTracks(List roots, TrackEntry group) { List siblings = siblingsOf(roots, group); if (!group.isGroup() || siblings == null) return false; diff --git a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TrackGroupingTest.java b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TrackGroupingTest.java index 43530689..04542ac7 100644 --- a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TrackGroupingTest.java +++ b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TrackGroupingTest.java @@ -18,6 +18,76 @@ import com.minres.scviewer.database.ui.TrackEntry; import com.minres.scviewer.database.ui.TrackEntry.HierState; public class TrackGroupingTest { + @Test + public void groupingRecordedAndCombinedVectorBitsCopiesThemAfterTheVector() { + var source = new BitVectorExpansionTest.Signal("bus", 3) { + @Override public com.minres.scviewer.database.IEventList getEvents() { + throw new AssertionError("Grouping must not read signal events"); + } + }; + var combined = new com.minres.scviewer.database.CombinedBitVectorWaveform("combined", List.of( + new com.minres.scviewer.database.CombinedBitVectorWaveform.SourceBit(source, 2), + new com.minres.scviewer.database.CombinedBitVectorWaveform.SourceBit(source, 1), + new com.minres.scviewer.database.CombinedBitVectorWaveform.SourceBit(source, 0))); + for (var waveform : List.of(source, combined)) { + TrackEntry bus = new TrackEntry(waveform, null); + bus.setExpanded(true); + var bits = List.copyOf(bus.waveforms); + bits.get(0).valueDisplay = TrackEntry.ValueDisplay.SIGNED; + bits.get(2).waveDisplay = TrackEntry.WaveDisplay.STEP_WISE; + TrackEntry tail = new TrackEntry(new EmptyWaveform("tail"), null); + var roots = new java.util.ArrayList<>(List.of(bus, tail)); + + TrackEntry group = TrackEntry.groupTracks(roots, List.of(bits.get(2), bits.get(0)), "bits", null); + assertEquals(List.of(bus, group, tail), roots); + assertEquals(bits, bus.waveforms); + assertEquals(2, group.waveforms.size()); + for (int index = 0; index < 2; index++) { + TrackEntry original = bits.get(index == 0 ? 0 : 2); + TrackEntry copy = group.waveforms.get(index); + assertNotSame(original, copy); + assertSame(original.waveform, copy.waveform); + assertEquals(original.valueDisplay, copy.valueDisplay); + assertEquals(original.waveDisplay, copy.waveDisplay); + } + group.waveforms.get(0).valueDisplay = TrackEntry.ValueDisplay.BINARY; + assertEquals(TrackEntry.ValueDisplay.SIGNED, bits.get(0).valueDisplay); + assertTrue(TrackEntry.ungroupTracks(roots, group)); + assertEquals(bits, bus.waveforms); + assertEquals(List.of(bus, group.waveforms.get(0), group.waveforms.get(1), tail), roots); + } + } + + @Test + public void groupingBitsOfNestedVectorPlacesCopiesInItsParentGroup() { + TrackEntry bus = new TrackEntry(new BitVectorExpansionTest.Signal("bus", 2), null); + bus.setExpanded(true); + var bits = List.copyOf(bus.waveforms); + TrackEntry tail = new TrackEntry(new EmptyWaveform("tail"), null); + TrackEntry outer = TrackEntry.group("outer", List.of(bus, tail), null); + var roots = new java.util.ArrayList<>(List.of(outer)); + TrackEntry group = TrackEntry.groupTracks(roots, bits, "bits", null); + assertEquals(List.of(outer), roots); + assertEquals(List.of(bus, group, tail), outer.waveforms); + assertEquals(bits, bus.waveforms); + bus.setExpanded(false); + bus.setExpanded(true); + assertEquals(bits, bus.waveforms); + assertNotSame(bits.get(0), group.waveforms.get(0)); + } + + @Test + public void groupingStandaloneBitTracksMovesTheirExistingEntries() { + var source = new BitVectorExpansionTest.Signal("bus", 2); + TrackEntry first = new TrackEntry(new com.minres.scviewer.database.ui.BitWaveform(source, 1), null); + TrackEntry second = new TrackEntry(new com.minres.scviewer.database.ui.BitWaveform(source, 0), null); + var roots = new java.util.ArrayList<>(List.of(first, second)); + TrackEntry group = TrackEntry.groupTracks(roots, List.of(second, first), "bits", null); + assertEquals(List.of(group), roots); + assertSame(first, group.waveforms.get(0)); + assertSame(second, group.waveforms.get(1)); + } + @Test public void deletingNestedSeparatorLeavesItsGroupAndOtherMembersIntact() { TrackEntry separator = new TrackEntry(new EmptyWaveform(), null); diff --git a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/WaveformScrollTest.java b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/WaveformScrollTest.java index 2d006f16..a074165f 100644 --- a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/WaveformScrollTest.java +++ b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/WaveformScrollTest.java @@ -465,6 +465,32 @@ public class WaveformScrollTest { assertEquals(List.of(bus), view.getStreamList()); } + @Test + public void groupingExpandedBitsKeepsTheVectorIntactAndSelectsTheNewGroup() { + var source = new BitVectorExpansionTest.Signal("bus", 3); + TrackEntry bus = new TrackEntry(source, null); + bus.setExpanded(true); + view.getStreamList().add(bus); + view.update(); + var bits = List.copyOf(bus.waveforms); + view.setSelection(new StructuredSelection(List.of(bits.get(2), bits.get(0)))); + TrackEntry group = view.groupSelectedTracks("bits"); + assertNotNull(group); + assertEquals(List.of(bus, group), view.getStreamList()); + assertEquals(bits, bus.waveforms); + assertSame(group, ((StructuredSelection) view.getSelection()).getFirstElement()); + assertSame(bits.get(0).waveform, group.waveforms.get(0).waveform); + assertSame(bits.get(2).waveform, group.waveforms.get(1).waveform); + assertNotSame(bits.get(0), group.waveforms.get(0)); + assertNotSame(bits.get(2), group.waveforms.get(1)); + view.setGroupExpanded(bus, false); + view.setGroupExpanded(bus, true); + assertEquals(bits, bus.waveforms); + view.ungroupTracks(group); + assertEquals(bits, bus.waveforms); + assertEquals(List.of(bus, group.waveforms.get(0), group.waveforms.get(1)), view.getStreamList()); + } + private Point origin() { try { return (Point) canvas.getClass().getMethod("getOrigin").invoke(canvas); } catch (ReflectiveOperationException e) { throw new AssertionError(e); }