Skip to content

Commit

Permalink
Event code cleanup (#1427)
Browse files Browse the repository at this point in the history
* Cleanup the IGV events

* Cleanup some of the event code by taking advantage of newer java 17 features
  - make most event types records and delete some unused events / fields
  - fix some intellij complaints in event usage / IGVEventBus
* fix a double repaint bug in AlignmentTrack due to a missing break

* Cleanup ViewChange and delete ZoomChange

* Make IGV events all implement sealed interface IGVEvent

This isn't really a functional change but makes it a bit easier to find/understand what
event types are available.
  • Loading branch information
lbergelson authored and jrobinso committed Dec 4, 2023
1 parent 5deb9e9 commit aac2b39
Show file tree
Hide file tree
Showing 42 changed files with 133 additions and 323 deletions.
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
exports org.broad.igv;
exports org.broad.igv.tools;
exports org.broad.igv.ui;
exports org.broad.igv.event;
exports org.broad.igv.jbrowse;
exports org.broad.igv.logging;
exports org.broad.igv.util.liftover;
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/org/broad/igv/event/AlignmentTrackEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@
* @author Jim Robinson
* @date 12/2/11
*/
public class AlignmentTrackEvent {

public record AlignmentTrackEvent(Type type) implements IGVEvent{
public enum Type {ALLELE_THRESHOLD, RELOAD, REFRESH}

private Object source;
private Type type;
private boolean booleanValue;

public AlignmentTrackEvent(Object source, Type type) {
this.source = source;
this.type = type;
}

public Type getType() {
return type;
}
}
}
13 changes: 1 addition & 12 deletions src/main/java/org/broad/igv/event/DataLoadedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,4 @@
* User: jacob
* Date: 2013-Feb-06
*/
public class DataLoadedEvent {

public final ReferenceFrame referenceFrame;

public DataLoadedEvent(ReferenceFrame referenceFrame){
this.referenceFrame = referenceFrame;
}

public ReferenceFrame getReferenceFrame() {
return referenceFrame;
}
}
public record DataLoadedEvent(ReferenceFrame referenceFrame) implements IGVEvent {}
9 changes: 1 addition & 8 deletions src/main/java/org/broad/igv/event/GenomeChangeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,4 @@
/**
* Created by jrobinso on 1/7/17.
*/
public class GenomeChangeEvent {

public Genome genome;

public GenomeChangeEvent(Genome genome) {
this.genome = genome;
}
}
public record GenomeChangeEvent(Genome genome) implements IGVEvent {}
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/event/GenomeResetEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/**
* Created by jrobinso on 2/7/17.
*/
public class GenomeResetEvent {
public final class GenomeResetEvent implements IGVEvent {
}
23 changes: 23 additions & 0 deletions src/main/java/org/broad/igv/event/IGVEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.broad.igv.event;

import org.broad.igv.oauth.OAuthProvider;
import org.broad.igv.prefs.PreferencesChangeEvent;
import org.broad.igv.sam.InsertionSelectionEvent;
import org.broad.igv.ui.panel.FrameManager;

public sealed interface IGVEvent
permits
AlignmentTrackEvent,
DataLoadedEvent,
GenomeChangeEvent,
GenomeResetEvent,
RefreshEvent,
StopEvent,
TrackGroupEvent,
ViewChange,
OAuthProvider.AuthStateEvent,
PreferencesChangeEvent,
InsertionSelectionEvent,
FrameManager.ChangeEvent {

}
15 changes: 4 additions & 11 deletions src/main/java/org/broad/igv/event/IGVEventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
*/
public class IGVEventBus {

static final Logger log = LogManager.getLogger(IGVEventBus.class);

Map<Class, Set<IGVEventObserver>> observerMap;
final Map<Class<?>, Set<IGVEventObserver>> observerMap;

private static IGVEventBus instance;

Expand All @@ -54,13 +52,8 @@ public IGVEventBus() {
this.observerMap = new HashMap<>();
}

public synchronized void subscribe(Class eventClass, IGVEventObserver observer) {

Set<IGVEventObserver> observerSet = observerMap.get(eventClass);
if (observerSet == null) {
observerSet = Collections.newSetFromMap(new WeakHashMap<IGVEventObserver, Boolean>());
observerMap.put(eventClass, observerSet);
}
public synchronized void subscribe(Class<?> eventClass, IGVEventObserver observer) {
Set<IGVEventObserver> observerSet = observerMap.computeIfAbsent(eventClass, k -> Collections.newSetFromMap(new WeakHashMap<>()));
observerSet.add(observer);
}

Expand All @@ -74,7 +67,7 @@ public synchronized void unsubscribe(IGVEventObserver observer) {
}
}

public void post(Object event) {
public void post(IGVEvent event) {
Set<IGVEventObserver> observerSet = observerMap.get(event.getClass());
if (observerSet != null) {
// Make a copy in case original is modified during loop
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/event/IGVEventObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
*/
public interface IGVEventObserver {

void receiveEvent(Object event);
void receiveEvent(IGVEvent event);

}
33 changes: 0 additions & 33 deletions src/main/java/org/broad/igv/event/ReferenceFrameEvent.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/event/RefreshEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/**
* Created by jrobinso on 6/24/17.
*/
public class RefreshEvent {
public final class RefreshEvent implements IGVEvent {
}
2 changes: 1 addition & 1 deletion src/main/java/org/broad/igv/event/StopEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/**
* Created by jrobinso on 6/24/17.
*/
public class StopEvent {
public final class StopEvent implements IGVEvent{
}
10 changes: 1 addition & 9 deletions src/main/java/org/broad/igv/event/TrackGroupEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,5 @@
* @author Jim Robinson
* @date 10/28/11
*/
public class TrackGroupEvent {

Object source;

public TrackGroupEvent(Object source) {
this.source = source;
}


public final class TrackGroupEvent implements IGVEvent {
}
36 changes: 12 additions & 24 deletions src/main/java/org/broad/igv/event/ViewChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,34 @@
* User: jacob
* Date: 2013-Jan-30
*/
public class ViewChange {
public final class ViewChange implements IGVEvent{

public enum Type {ChromosomeChange, LocusChange}

boolean recordHistory = false;
public Type type;
public String chrName;
public double start;
public double end;
final boolean recordHistory;
final public Type type;
final public String chrName;
final public double start;
final public double end;

private ViewChange(Type type) {
this.type = type;
}

private ViewChange(Type type, String chrName) {
this.type = type;
this.chrName = chrName;
}

private ViewChange(Type type, String chrName, double start, double end) {
private ViewChange(Type type, String chrName, double start, double end, boolean recordHistory) {
this.type = type;
this.chrName = chrName;
this.start = start;
this.end = end;
this.recordHistory = recordHistory;
}

public boolean recordHistory() {
return this.recordHistory;
}

public void setRecordHistory(boolean recordHistory) {
this.recordHistory = recordHistory;
}

public static ViewChange ChromosomeChangeResult(String chrName) {
return new ViewChange(Type.ChromosomeChange, chrName);
public static ViewChange ChromosomeChangeResult(String chrName, boolean recordHistory) {
return new ViewChange(Type.ChromosomeChange, chrName, 0.0, 0.0, recordHistory);
}

public static ViewChange LocusChangeResult(String chrName, double start, double end) {
return new ViewChange(Type.LocusChange, chrName, start, end);
public static ViewChange LocusChangeResult(String chrName, double start, double end, boolean recordHistory) {
return new ViewChange(Type.LocusChange, chrName, start, end, recordHistory);
}

}
64 changes: 0 additions & 64 deletions src/main/java/org/broad/igv/event/ZoomChange.java

This file was deleted.

29 changes: 3 additions & 26 deletions src/main/java/org/broad/igv/oauth/OAuthProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import org.broad.igv.event.IGVEvent;
import org.broad.igv.logging.*;
import org.broad.igv.Globals;
import org.broad.igv.batch.CommandListener;
import org.broad.igv.event.IGVEventBus;
import org.broad.igv.prefs.PreferencesManager;
Expand All @@ -21,7 +21,6 @@
import java.awt.*;
import java.io.IOException;
import java.net.*;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.*;

Expand Down Expand Up @@ -441,30 +440,8 @@ public String getAuthProvider() {
return authProvider;
}

public static class AuthStateEvent {
boolean authenticated;
String authProvider;
String userName;

// Assuming that if this event is called, we are indeed autz/authn'd
public AuthStateEvent(boolean authenticated, String authProvider, String userName) {
this.authenticated = authenticated;
this.authProvider = authProvider;
this.userName = userName;
}

public boolean isAuthenticated() {
return authenticated;
}

public String getAuthProvider() {
return authProvider;
}

public String getUserName() {
return userName;
}
}
// Assuming that if this event is called, we are indeed autz/authn'd
public record AuthStateEvent(boolean authenticated, String authProvider, String userName) implements IGVEvent {}
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/broad/igv/prefs/IGVPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,14 @@ private void checkForAlignmentChanges(Map<String, String> updatedPreferenceMap)
}

if (reloadSAM) {
IGVEventBus.getInstance().post(new AlignmentTrackEvent(this, AlignmentTrackEvent.Type.RELOAD));
IGVEventBus.getInstance().post(new AlignmentTrackEvent(AlignmentTrackEvent.Type.RELOAD));
}
// A reload is harsher than a refresh; only send the weaker request if the stronger one is not sent.
if (!reloadSAM && refreshSAM) {
IGVEventBus.getInstance().post(new AlignmentTrackEvent(this, AlignmentTrackEvent.Type.REFRESH));
IGVEventBus.getInstance().post(new AlignmentTrackEvent(AlignmentTrackEvent.Type.REFRESH));
}
if (updatedPreferenceMap.containsKey(SAM_ALLELE_THRESHOLD)) {
IGVEventBus.getInstance().post(new AlignmentTrackEvent(this, AlignmentTrackEvent.Type.ALLELE_THRESHOLD));
IGVEventBus.getInstance().post(new AlignmentTrackEvent(AlignmentTrackEvent.Type.ALLELE_THRESHOLD));
}
}

Expand Down
Loading

0 comments on commit aac2b39

Please sign in to comment.