fix selection handling in viewer and Sonarlint warnings

This commit is contained in:
Eyck Jentzsch 2021-01-09 13:43:11 +01:00
parent 9d2e2e7f64
commit abacae42d7
5 changed files with 58 additions and 57 deletions

View File

@ -23,6 +23,7 @@ import org.eclipse.swt.widgets.Control;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.RelationType;
import com.minres.scviewer.database.RelationTypeFactory;
import com.minres.scviewer.database.tx.ITx;
public interface IWaveformView extends PropertyChangeListener, ISelectionProvider{
@ -64,7 +65,7 @@ public interface IWaveformView extends PropertyChangeListener, ISelectionProvide
public List<TrackEntry> getStreamList();
public TrackEntry getEntryForStream(IWaveform source);
public TrackEntry getEntryFor(ITx source);
public List<Object> getElementsAt(Point pt);

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
@ -696,30 +697,36 @@ public class WaveformView implements IWaveformView {
boolean selectionChanged = false;
currentWaveformSelection.forEach(e->e.selected=false);
if (selection instanceof IStructuredSelection) {
if(((IStructuredSelection) selection).size()==0){
IStructuredSelection sel = (IStructuredSelection) selection;
if(sel.size()==0){
selectionChanged = currentTxSelection!=null||currentWaveformSelection!=null;
currentTxSelection = null;
currentWaveformSelection .clear();
} else {
if(!add) currentWaveformSelection.clear();
for(Object sel:((IStructuredSelection) selection).toArray()){
if (sel instanceof ITx){
ITx txSel = (ITx) sel;
TrackEntry trackEntry = getEntryForStream(txSel.getStream());
List<?> selList = sel.toList();
if(selList.get(0) instanceof ITx) {
ITx txSel = (ITx) selList.get(0);
TrackEntry trackEntry = selList.size()==2 && selList.get(1) instanceof TrackEntry?
(TrackEntry) selList.get(1):null;
if(trackEntry==null) {
trackEntry = getEntryFor(txSel);
if(trackEntry==null && addIfNeeded){
trackEntry=new TrackEntry(txSel.getStream(), styleProvider);
streams.add(trackEntry);
}
currentTxSelection = txSel;
currentWaveformSelection.clear();
currentWaveformSelection.add(trackEntry);
selectionChanged = true;
} else if (sel instanceof TrackEntry && !currentWaveformSelection.contains(sel)) {
currentWaveformSelection.add((TrackEntry)sel);
if(currentTxSelection!=null && !selectionChanged)
currentTxSelection=null;
selectionChanged = true;
}
currentTxSelection = txSel;
currentWaveformSelection.clear();
currentWaveformSelection.add(trackEntry);
selectionChanged = true;
} else if(selList.size()==1 && selList.get(0) instanceof TrackEntry) {
currentWaveformSelection.add((TrackEntry)selList.get(0));
if(currentTxSelection!=null)
currentTxSelection=null;
selectionChanged = true;
} else {
System.err.println("Invalid selection");
}
}
} else {
@ -773,7 +780,7 @@ public class WaveformView implements IWaveformView {
public void moveSelection(GotoDirection direction, RelationType relationType) {
if(currentWaveformSelection.size() !=1 && currentTxSelection==null) return;
TrackEntry selectedWaveform=currentWaveformSelection.size() == 1?
currentWaveformSelection.get(0) : getEntryForStream(currentTxSelection.getStream());
currentWaveformSelection.get(0) : getEntryFor(currentTxSelection);
if(selectedWaveform.waveform.getType()==WaveformType.TRANSACTION && currentTxSelection!=null) {
if(relationType.equals(IWaveformView.NEXT_PREV_IN_STREAM)){
ITx transaction = null;
@ -1167,9 +1174,13 @@ public class WaveformView implements IWaveformView {
});
}
public TrackEntry getEntryForStream(IWaveform source) {
for(TrackEntry trackEntry:streams)
if(trackEntry.waveform.isSame(source)) return trackEntry;
public TrackEntry getEntryFor(ITx source) {
Optional<TrackEntry> optGen = streams.stream().filter(e->source.getGenerator().equals(e.waveform)).findFirst();
if(optGen.isPresent())
return optGen.get();
Optional<TrackEntry> optStr = streams.stream().filter(e->source.getStream().equals(e.waveform)).findFirst();
if(optStr.isPresent())
return optStr.get();
return null;
}

View File

@ -10,7 +10,6 @@
*******************************************************************************/
package com.minres.scviewer.database;
// TODO: Auto-generated Javadoc
/**
* The Enum AssociationType.
*/

View File

@ -100,15 +100,9 @@ public class DesignBrowser {
/** The tree viewer. */
private TreeViewer treeViewer;
/** The name filter of the design browser tree. */
private Text treeNameFilter;
/** The attribute filter. */
StreamTTreeFilter treeAttributeFilter;
/** The name filter. */
private Text tableNameFilter;
/** The attribute filter. */
StreamTableFilter tableAttributeFilter;
@ -116,34 +110,30 @@ public class DesignBrowser {
private TableViewer txTableViewer;
/** The append all item. */
ToolItem appendItem, insertItem;
ToolItem appendItem;
ToolItem insertItem;
/** The other selection count. */
int thisSelectionCount=0, otherSelectionCount=0;
int thisSelectionCount=0;
int otherSelectionCount=0;
/** The waveform viewer part. */
private WaveformViewer waveformViewerPart;
/** The tree viewer pcl. */
private PropertyChangeListener treeViewerPCL = evt -> {
if("CHILDS".equals(evt.getPropertyName())){ //$NON-NLS-1$
treeViewer.getTree().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
treeViewer.refresh();
}
});
treeViewer.getTree().getDisplay().asyncExec(() -> treeViewer.refresh());
} else if("WAVEFORMS".equals(evt.getPropertyName())) {
treeViewer.getTree().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
IWaveformDb database = waveformViewerPart.getDatabase();
treeViewer.setInput(Arrays.asList(database.isLoaded()?new IWaveformDb[]{database}:new IWaveformDb[]{new LoadingWaveformDb()}));
}
treeViewer.getTree().getDisplay().asyncExec(() -> {
IWaveformDb database = waveformViewerPart.getDatabase();
treeViewer.setInput(Arrays.asList(database.isLoaded()?new IWaveformDb[]{database}:new IWaveformDb[]{new LoadingWaveformDb()}));
});
}
};
/** The waveform viewer part. */
private WaveformViewer waveformViewerPart;
/** The sash paint listener. */
protected PaintListener sashPaintListener= e -> {
int size=Math.min(e.width, e.height)-1;
@ -192,7 +182,7 @@ public class DesignBrowser {
public void createTreeViewerComposite(Composite parent) {
parent.setLayout(new GridLayout(1, false));
treeNameFilter = new Text(parent, SWT.BORDER);
Text treeNameFilter = new Text(parent, SWT.BORDER);
treeNameFilter.setMessage(Messages.DesignBrowser_3);
treeNameFilter.addModifyListener( e -> {
treeAttributeFilter.setSearchText(((Text) e.widget).getText());
@ -233,7 +223,7 @@ public class DesignBrowser {
public void createTableComposite(Composite parent) {
parent.setLayout(new GridLayout(1, false));
tableNameFilter = new Text(parent, SWT.BORDER);
Text tableNameFilter = new Text(parent, SWT.BORDER);
tableNameFilter.setMessage(Messages.DesignBrowser_2);
tableNameFilter.addModifyListener(e -> {
tableAttributeFilter.setSearchText(((Text) e.widget).getText());

View File

@ -181,17 +181,6 @@ public class TransactionDetails {
treeViewer.refresh();
}
});
// Add the type column
col2 = new TreeViewerColumn(treeViewer, SWT.NONE);
col2.getColumn().setText(Messages.TransactionDetails_2);
col2.getColumn().setResizable(true);
col2.setLabelProvider(new DelegatingStyledCellLabelProvider(new AttributeLabelProvider(waveformViewerPart, AttributeLabelProvider.TYPE)));
col2.getColumn().addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((TxAttributeViewerSorter) treeViewer.getComparator()).doSort(COLUMN_SECOND);
treeViewer.refresh();
}
});
// Add the value column
col3 = new TreeViewerColumn(treeViewer, SWT.NONE);
col3.getColumn().setText(Messages.TransactionDetails_3);
@ -203,6 +192,17 @@ public class TransactionDetails {
treeViewer.refresh();
}
});
// Add the type column
col2 = new TreeViewerColumn(treeViewer, SWT.NONE);
col2.getColumn().setText(Messages.TransactionDetails_2);
col2.getColumn().setResizable(true);
col2.setLabelProvider(new DelegatingStyledCellLabelProvider(new AttributeLabelProvider(waveformViewerPart, AttributeLabelProvider.TYPE)));
col2.getColumn().addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((TxAttributeViewerSorter) treeViewer.getComparator()).doSort(COLUMN_SECOND);
treeViewer.refresh();
}
});
// Pack the columns
// for (int i = 0, n = table.getColumnCount(); i < n; i++) {
// table.getColumn(i).pack();