diff --git a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/internal/util/TransactionQuery.java b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/internal/util/TransactionQuery.java index 72492929..62ecae6d 100644 --- a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/internal/util/TransactionQuery.java +++ b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/internal/util/TransactionQuery.java @@ -15,7 +15,7 @@ import com.minres.scviewer.database.tx.ITx; import com.minres.scviewer.database.tx.ITransactionProvider; import com.minres.scviewer.database.tx.TransactionLists; -/** Cached transaction headers; attribute values are visited only on explicit request. */ +/** Cached transaction headers and property discovery for a selected track. */ public final class TransactionQuery { public record Attribute(String name, DataType type) {} private final IWaveform waveform; diff --git a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionList.java b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionList.java index d15cb483..eda686e7 100644 --- a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionList.java +++ b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionList.java @@ -18,7 +18,6 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; @@ -40,7 +39,6 @@ public class TransactionList extends Composite { private final ComboViewer viewProperty; private final Text searchValue; private final TableViewer tableViewer; - private final Button loadProperties; private final AttributeLabelProvider valueProvider; private final LatestJob> rowsJob; private record PropertyDiscovery(List attributes, RuntimeException error) {} @@ -52,25 +50,24 @@ public class TransactionList extends Composite { private IWaveform stream; private TrackEntry track; private TransactionQuery query; + private boolean propertiesRequested; + private String lastSelectedDisplayProperty; + private boolean restoringDisplayProperty; public TransactionList(Composite parent, int style, WaveformViewer waveformViewer) { super(parent, style); parent.setLayout(new FillLayout()); - setLayout(new GridLayout(6, false)); + setLayout(new GridLayout(5, false)); label("Property to match:"); searchProperty = propertiesCombo(); searchValue = new Text(this, SWT.BORDER); searchValue.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); label("Property to show:"); viewProperty = propertiesCombo(); - loadProperties = new Button(this, SWT.PUSH); - loadProperties.setText("Load properties"); - loadProperties.setToolTipText("Discover property names in this track; values are otherwise loaded only when needed."); - loadProperties.setEnabled(false); tableViewer = new TableViewer(this, SWT.VIRTUAL | SWT.FULL_SELECTION | SWT.BORDER); tableViewer.setUseHashlookup(true); - tableViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 6, 1)); + tableViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1)); tableViewer.getTable().setHeaderVisible(true); tableViewer.getTable().setLinesVisible(true); tableViewer.setContentProvider(new ILazyContentProvider() { @@ -94,26 +91,44 @@ public class TransactionList extends Composite { }); attributesJob = new LatestJob<>(task -> { if (!display.isDisposed()) display.asyncExec(task); }, result -> { if (isDisposed()) return; - loadProperties.setEnabled(query != null); - loadProperties.setText("Load properties"); if (result.error() != null) { MessageDialog.openError(getShell(), "Cannot load properties", result.error().toString()); return; } Object selected = ((IStructuredSelection) searchProperty.getSelection()).getFirstElement(); - String displayedProperty = valueProvider.getShowProp(); - attributes = result.attributes(); + String displayedProperty = lastSelectedDisplayProperty != null + ? lastSelectedDisplayProperty : valueProvider.getShowProp(); + // A details-panel search may add a known property while discovery is running. + java.util.Map known = new java.util.TreeMap<>(); + attributes.forEach(attribute -> known.put(attribute.name(), attribute)); + result.attributes().forEach(attribute -> known.put(attribute.name(), attribute)); + attributes = List.copyOf(known.values()); searchProperty.setInput(attributes); viewProperty.setInput(attributes); - if (attributes.contains(selected)) searchProperty.setSelection(new StructuredSelection(selected)); + if (selected != null && attributes.contains(selected)) searchProperty.setSelection(new StructuredSelection(selected)); else if (!attributes.isEmpty()) searchProperty.setSelection(new StructuredSelection(attributes.get(0))); - attributes.stream().filter(attribute -> attribute.name().equals(displayedProperty)).findFirst() - .ifPresent(attribute -> viewProperty.setSelection(new StructuredSelection(attribute))); - loadProperties.setEnabled(query != null); + Attribute displayed = attributes.stream().filter(attribute -> attribute.name().equals(displayedProperty)) + .findFirst().orElse(attributes.isEmpty() ? null : attributes.get(0)); + if (displayed != null) showProperty(displayed); + else { + valueProvider.setShowProp(null); + tableViewer.refresh(); + } }); addListener(SWT.Show, event -> { getDisplay().timerExec(-1, applyFilter); - refreshTransactions(); + // SWT sends Show before updating the control's visibility flag. + display.asyncExec(() -> { + if (isDisposed() || !getVisible()) return; + refreshTransactions(); + discoverProperties(); + }); + }); + addListener(SWT.Hide, event -> { + getDisplay().timerExec(-1, applyFilter); + rowsJob.cancel(); + attributesJob.cancel(); + propertiesRequested = false; }); searchValue.addModifyListener(event -> requestFilter(200)); searchProperty.addSelectionChangedListener(event -> { @@ -124,21 +139,11 @@ public class TransactionList extends Composite { }); viewProperty.addSelectionChangedListener(event -> { if (event.getSelection() instanceof IStructuredSelection selected && selected.getFirstElement() instanceof Attribute attribute) { + if (!restoringDisplayProperty) lastSelectedDisplayProperty = attribute.name(); valueProvider.setShowProp(attribute.name()); tableViewer.refresh(); } }); - loadProperties.addListener(SWT.Selection, event -> { - TransactionQuery requested = query; - if (requested == null) return; - loadProperties.setEnabled(false); - loadProperties.setText("Loading properties…"); - attributesJob.schedule("Discover transaction properties", monitor -> { - try { return new PropertyDiscovery(requested.discoverAttributes(monitor::isCanceled), null); } - catch (OperationCanceledException e) { throw e; } - catch (RuntimeException e) { return new PropertyDiscovery(List.of(), e); } - }); - }); tableViewer.addSelectionChangedListener(event -> { if (event.getSelection() instanceof IStructuredSelection selected && selected.getFirstElement() instanceof ITx tx) waveformViewer.setSelection(WaveformSelection.of(tx, track)); @@ -150,6 +155,26 @@ public class TransactionList extends Composite { }); } + /** Automatic defaults must not replace the user's preference when a stream lacks it. */ + private void showProperty(Attribute property) { + restoringDisplayProperty = true; + try { viewProperty.setSelection(new StructuredSelection(property)); } + finally { restoringDisplayProperty = false; } + } + + private void discoverProperties() { + TransactionQuery requested = query; + if (requested == null || !getVisible() || propertiesRequested) return; + propertiesRequested = true; + attributesJob.schedule("Discover transaction properties", monitor -> { + try { + List discovered = requested.discoverAttributes(monitor::isCanceled); + return new PropertyDiscovery(discovered, null); + } catch (OperationCanceledException e) { throw e; } + catch (RuntimeException e) { return new PropertyDiscovery(List.of(), e); } + }); + } + private void label(String text) { new Label(this, SWT.NONE).setText(text); } private ComboViewer propertiesCombo() { @@ -191,9 +216,9 @@ public class TransactionList extends Composite { filter.setSearchProp(null, null); tableViewer.setInput(rows); tableViewer.setItemCount(0); - loadProperties.setEnabled(query != null); - loadProperties.setText("Load properties"); + propertiesRequested = false; requestFilter(0); + discoverProperties(); } private void requestFilter(int delay) { @@ -230,6 +255,7 @@ public class TransactionList extends Composite { viewProperty.setInput(attributes); } searchProperty.setSelection(new StructuredSelection(property)); + if (valueProvider.getShowProp() == null) showProperty(property); filter.setSearchProp(name, type); searchValue.setText(value); requestFilter(0); diff --git a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionListView.java b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionListView.java index 324557b4..eeaf7e80 100644 --- a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionListView.java +++ b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/TransactionListView.java @@ -1,16 +1,9 @@ package com.minres.scviewer.e4.application.parts; import javax.annotation.PostConstruct; -import javax.inject.Inject; -import javax.inject.Named; import org.eclipse.e4.core.di.annotations.Optional; -import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.e4.ui.di.Focus; -import org.eclipse.e4.ui.model.application.ui.basic.MPart; -import org.eclipse.e4.ui.services.IServiceConstants; -import org.eclipse.e4.ui.workbench.modeling.EPartService; -import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; @@ -18,21 +11,8 @@ import org.eclipse.swt.widgets.Composite; import com.minres.scviewer.database.ui.WaveformSelection; public class TransactionListView { - private WaveformViewer waveformViewer; - private TransactionList transactionList; - - /** The event broker. */ - @Inject IEventBroker eventBroker; - /** The selection service. */ - @Inject ESelectionService selectionService; - - /** - * Create the composite. - * @param parent - * @param style - */ /** * Creates the composite. * @@ -40,7 +20,6 @@ public class TransactionListView { */ @PostConstruct public void createComposite(final Composite parent, @Optional WaveformViewer waveformViewer) { - this.waveformViewer=waveformViewer; transactionList = new TransactionList(parent, SWT.BORDER, waveformViewer); } @@ -50,17 +29,14 @@ public class TransactionListView { transactionList.setFocus(); } /** - * Sets the selection. + * Receives selections directly from the owning waveform pane, independently of + * the workbench's active part and global selection injection. * * @param selection the new selection */ - @Inject - public void setSelection(@Named(IServiceConstants.ACTIVE_SELECTION) @Optional IStructuredSelection selection, EPartService partService){ - // only react if selection is actually from the WaveformViewer and nothing else - MPart part = partService.getActivePart(); - if( part == null || ! (part.getObject() instanceof WaveformViewer ) || part.getObject() != waveformViewer) - return; - transactionList.setInput(WaveformSelection.from(selection).track()); + public void setSelection(IStructuredSelection selection) { + if (transactionList != null && !transactionList.isDisposed()) + transactionList.setInput(WaveformSelection.from(selection).track()); } public TransactionList getControl() { diff --git a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/WaveformViewer.java b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/WaveformViewer.java index 07ec87fd..2806c777 100644 --- a/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/WaveformViewer.java +++ b/plugins/com.minres.scviewer.e4.application/src/com/minres/scviewer/e4/application/parts/WaveformViewer.java @@ -267,8 +267,9 @@ public class WaveformViewer implements IPreferenceChangeListener, DisposeListene }); waveformPane.addSelectionChangedListener(event -> { - if (event.getSelection() instanceof IStructuredSelection) { - selectionService.setSelection(event.getSelection()); + if (event.getSelection() instanceof IStructuredSelection selection) { + transactionList.setSelection(selection); + selectionService.setSelection(selection); } }); diff --git a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/internal/util/TransactionQueryTest.java b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/internal/util/TransactionQueryTest.java index 83ba25ff..679faf7e 100644 --- a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/internal/util/TransactionQueryTest.java +++ b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/internal/util/TransactionQueryTest.java @@ -34,6 +34,30 @@ public class TransactionQueryTest { }); } + @Test + public void discoveryIncludesLatePropertiesAndCachesTheCompleteSchema() { + AtomicInteger reads = new AtomicInteger(); + Waveform wave = new Waveform(); + ITxAttribute lateAttribute = (ITxAttribute) Proxy.newProxyInstance(getClass().getClassLoader(), + new Class[]{ITxAttribute.class}, (p, m, a) -> switch (m.getName()) { + case "getName" -> "late"; + case "getDataType" -> DataType.STRING; + default -> "later value"; + }); + ITx late = (ITx) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ITx.class}, + (p, m, a) -> { + if (m.getName().equals("getAttributes")) { reads.incrementAndGet(); return List.of(lateAttribute); } + return 3L; + }); + wave.transactions = List.of(transaction(1, reads), transaction(2, reads), late); + TransactionQuery query = new TransactionQuery(wave); + assertEquals(List.of(new TransactionQuery.Attribute("field", DataType.STRING), + new TransactionQuery.Attribute("late", DataType.STRING)), query.discoverAttributes(() -> false)); + assertEquals(3, reads.get()); + query.discoverAttributes(() -> false); + assertEquals(3, reads.get()); + } + @Test public void headerQueriesAndIndexedRestorationNeverReadAttributesOrEvents() { AtomicInteger reads = new AtomicInteger(); diff --git a/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TransactionListPropertiesTest.java b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TransactionListPropertiesTest.java new file mode 100644 index 00000000..ab17af47 --- /dev/null +++ b/tests/com.minres.scviewer.e4.application.test/src/com/minres/scviewer/e4/application/parts/TransactionListPropertiesTest.java @@ -0,0 +1,216 @@ +package com.minres.scviewer.e4.application.parts; + +import static org.junit.Assert.*; +import static org.junit.Assume.assumeTrue; + +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.BooleanSupplier; + +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Table; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.minres.scviewer.database.DataType; +import com.minres.scviewer.database.EmptyWaveform; +import com.minres.scviewer.database.IEventList; +import com.minres.scviewer.database.WaveformType; +import com.minres.scviewer.database.tx.ITx; +import com.minres.scviewer.database.tx.ITxAttribute; +import com.minres.scviewer.database.tx.ITransactionProvider; +import com.minres.scviewer.database.ui.TrackEntry; + +public class TransactionListPropertiesTest { + private static class Stream extends EmptyWaveform implements ITransactionProvider { + List transactions; + @Override public WaveformType getType() { return WaveformType.STREAM; } + @Override public List getTransactionList() { return transactions; } + @Override public ITx getTransaction(long id) { return transactions.stream().filter(tx -> tx.getId() == id).findFirst().orElse(null); } + @Override public IEventList getEvents() { throw new AssertionError("Property discovery must use indexed headers"); } + } + + private Display display; + private Shell shell; + private WaveformViewer viewer; + private TransactionList list; + private TransactionListView listView; + private Combo search; + private Combo shown; + private Table table; + + @Before + public void createControls() { + assumeTrue("GTK integration tests require DISPLAY", !"gtk".equals(SWT.getPlatform()) || System.getenv("DISPLAY") != null); + display = new Display(); + shell = new Shell(display); + shell.setSize(1000, 500); + viewer = new WaveformViewer() { + @Override public String getScaledTime(Long time) { return time.toString(); } + }; + listView = new TransactionListView(); + listView.createComposite(shell, viewer); + list = listView.getControl(); + var combos = Arrays.stream(list.getChildren()).filter(Combo.class::isInstance).map(Combo.class::cast).toList(); + search = combos.get(0); shown = combos.get(1); + table = Arrays.stream(list.getChildren()).filter(Table.class::isInstance).map(Table.class::cast).findFirst().orElseThrow(); + assertFalse(Arrays.stream(list.getChildren()).anyMatch(Button.class::isInstance)); + shell.layout(true, true); + } + + @After + public void disposeControls() { + if (display == null) return; + try { + if (shell != null && !shell.isDisposed()) shell.dispose(); + if (viewer != null) viewer.closeDatabase(); + while (display.readAndDispatch()) {} + } finally { display.dispose(); } + } + + private void await(BooleanSupplier condition) throws Exception { + long deadline = System.nanoTime() + 10_000_000_000L; + while (!condition.getAsBoolean()) { + while (display.readAndDispatch()) {} + if (System.nanoTime() > deadline) fail("Timed out waiting for transaction properties"); + Thread.sleep(5); + } + } + + private ITxAttribute attribute(String name, String value) { + return (ITxAttribute) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ITxAttribute.class}, + (p, m, a) -> switch (m.getName()) { + case "getName" -> name; + case "getDataType" -> DataType.STRING; + case "getValue" -> value; + default -> null; + }); + } + + private ITx transaction(long id, ITxAttribute... attributes) { + return (ITx) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ITx.class}, + (p, m, a) -> switch (m.getName()) { + case "getId" -> id; + case "getBeginTime" -> 0L; + case "getEndTime" -> 1L; + case "getAttributes" -> List.of(attributes); + case "hashCode" -> System.identityHashCode(p); + case "equals" -> p == a[0]; + case "toString" -> "tx#" + id; + default -> null; + }); + } + + private void input(ITx... transactions) { + Stream stream = new Stream(); + stream.transactions = List.of(transactions); + listView.setSelection(new StructuredSelection(new TrackEntry(stream, null))); + } + + @Test + public void automaticDiscoveryPreservesPropertyChosenByDetailsSearch() throws Exception { + List transactions = new ArrayList<>(); + for (int i = 0; i < 32; i++) transactions.add(transaction(i, attribute("alpha", "first"))); + transactions.add(transaction(32, attribute("late", "last"))); + input(transactions.toArray(new ITx[0])); + list.setSearchProps("late", DataType.STRING, "last"); + await(() -> shown.getItemCount() == 2 && table.getItemCount() == 1); + assertEquals("late [STRING]", search.getText()); + assertEquals("late [STRING]", shown.getText()); + assertEquals("last", table.getItem(0).getText(2)); + } + + @Test + public void selectingTrackAutomaticallyShowsPropertyNamesAndValues() throws Exception { + input(transaction(1, attribute("address", "value"))); + await(() -> shown.getSelectionIndex() >= 0 && table.getItemCount() == 1); + assertEquals("address [STRING]", shown.getText()); + assertEquals("address [STRING]", search.getText()); + assertEquals("value", table.getItem(0).getText(2)); + } + + @Test + public void automaticDiscoveryFindsPropertiesBeyondTheFirst32Transactions() throws Exception { + List transactions = new ArrayList<>(); + for (int i = 0; i < 32; i++) transactions.add(transaction(i, attribute("alpha", "first"), attribute("beta", "second"))); + transactions.add(transaction(32, attribute("late", "last"))); + input(transactions.toArray(new ITx[0])); + await(() -> shown.getItemCount() == 3 && table.getItemCount() == 33); + shown.select(1); shown.notifyListeners(SWT.Selection, new Event()); + assertEquals("second", table.getItem(0).getText(2)); + assertTrue(Arrays.asList(shown.getItems()).contains("late [STRING]")); + list.setVisible(false); + list.setVisible(true); + await(() -> table.getItemCount() == 33); + assertEquals("beta [STRING]", shown.getText()); + } + + @Test + public void remembersDisplayPropertyAcrossStreamsAndMissingProperties() throws Exception { + input(transaction(1, attribute("alpha", "first"), attribute("beta", "chosen"))); + await(() -> shown.getItemCount() == 2 && table.getItemCount() == 1); + shown.select(1); shown.notifyListeners(SWT.Selection, new Event()); + assertEquals("chosen", table.getItem(0).getText(2)); + + // The preferred property is no longer the second entry in the new stream. + input(transaction(2, attribute("aardvark", "a"), attribute("alpha", "b"), attribute("beta", "restored"))); + await(() -> shown.getItemCount() == 3 && table.getItemCount() == 1); + assertEquals("beta [STRING]", shown.getText()); + assertEquals("restored", table.getItem(0).getText(2)); + + // A temporary fallback and a separator must not erase the user's preference. + input(transaction(3, attribute("other", "fallback"))); + await(() -> shown.getText().equals("other [STRING]") && table.getItemCount() == 1); + assertEquals("fallback", table.getItem(0).getText(2)); + listView.setSelection(new StructuredSelection(new TrackEntry(new EmptyWaveform(), null))); + input(transaction(4, attribute("alpha", "default"), attribute("beta", "remembered"))); + await(() -> shown.getItemCount() == 2 && table.getItemCount() == 1); + assertEquals("beta [STRING]", shown.getText()); + assertEquals("remembered", table.getItem(0).getText(2)); + + // A subsequent explicit choice replaces the remembered preference. + shown.select(0); shown.notifyListeners(SWT.Selection, new Event()); + input(transaction(5, attribute("aardvark", "default"), attribute("alpha", "new preference"), attribute("beta", "old"))); + await(() -> shown.getItemCount() == 3 && table.getItemCount() == 1); + assertEquals("alpha [STRING]", shown.getText()); + assertEquals("new preference", table.getItem(0).getText(2)); + } + + @Test + public void hiddenViewDefersLoadingUntilShown() throws Exception { + list.setVisible(false); + input(transaction(1, attribute("address", "value"))); + // Pump queued work: selecting a track while hidden must not start discovery. + for (int i = 0; i < 10; i++) { + while (display.readAndDispatch()) {} + Thread.sleep(5); + } + assertEquals(0, shown.getItemCount()); + assertEquals(0, table.getItemCount()); + list.setVisible(true); + await(() -> shown.getItemCount() == 1 && table.getItemCount() == 1); + assertEquals("address [STRING]", shown.getText()); + assertEquals("value", table.getItem(0).getText(2)); + } + + @Test + public void switchingTracksReplacesPropertiesAndSeparatorClearsThem() throws Exception { + input(transaction(1, attribute("old", "old value"))); + input(transaction(2, attribute("new", "new value"))); + await(() -> shown.getText().equals("new [STRING]") && table.getItemCount() == 1); + assertEquals("new value", table.getItem(0).getText(2)); + listView.setSelection(new StructuredSelection(new TrackEntry(new EmptyWaveform(), null))); + assertEquals(0, shown.getItemCount()); + assertEquals(0, search.getItemCount()); + assertEquals(0, table.getItemCount()); + } +}