Skip to content

Commit

Permalink
better SPI for Viewer in the lphy extension (popsizefunc) LinguaPhylo…
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Apr 5, 2024
1 parent 8425ee0 commit b04331c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 80 deletions.
9 changes: 6 additions & 3 deletions popsizefunc-lphy-studio/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
requires transitive popsizefunc.lphy;
requires transitive lphystudio;

// LPhy extensions
uses lphy.core.spi.Extension;
// Viewer SPI
uses lphystudio.app.graphicalmodelpanel.viewer.Viewer;
// declare what service interface the provider intends to use
provides lphy.core.spi.Extension with popsizefunc.lphystudio.spi.PopSizeFuncViewerImpl;
provides lphystudio.app.graphicalmodelpanel.viewer.Viewer with popsizefunc.lphystudio.viewer.PopSizeFuncViewer;

// Note: to adapt with the system not using Java module but using class path,
// they need to be declared inside META-INF/services/lphystudio.app.graphicalmodelpanel.viewer.Viewer as well.

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package popsizefunc.lphystudio.viewer;

import lphy.core.model.Value;
import lphystudio.app.graphicalmodelpanel.viewer.Viewer;
import popsizefunc.lphy.evolution.popsize.PopulationFunction;

import javax.swing.*;

public class PopSizeFuncViewer implements Viewer {

/**
* Required by ServiceLoader.
*/
public PopSizeFuncViewer() {
}

@Override
public boolean match(Object value) {
return value instanceof PopulationFunction ||
(value instanceof Value && ((Value) value).value() instanceof PopulationFunction);
}

@Override
public JComponent getViewer(Object value) {
//TODO create PopulationFunctionComponent((Value<PopulationFunction>) value)
if (value instanceof PopulationFunction) {
return new JLabel("PopulationFunction TODO : " + value.toString());
}
return new JLabel("Value<PopulationFunction> TODO : " + ((Value<PopulationFunction>) value).value().toString());
}
@Override
public String toString() {
return "Pop-size function Viewer";
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package popsizefunc.lphystudio.app;
package popsizefunc.lphystudio.viewer;

import javax.swing.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This is important for deploying service providers on the class path
# https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/ServiceLoader.html
# META-INF/services must be under src for Intellij
# Class requires a public no-args constructor

# the core uses hard core to register all internal Viewers,
# but extensions need to declare what service interface the provider intends to use for new Viewers.
# for example, lphystudio.app.graphicalmodelpanel.viewer.???Viewer

popsizefunc.lphystudio.viewer.PopSizeFuncViewer

0 comments on commit b04331c

Please sign in to comment.