Skip to content

Commit

Permalink
Merge pull request #28 from saalfeldlab/misc/1.3.0
Browse files Browse the repository at this point in the history
Misc/1.3.0
  • Loading branch information
cmhulbert authored May 13, 2024
2 parents f351463 + ec9215c commit 492ee75
Show file tree
Hide file tree
Showing 20 changed files with 359 additions and 166 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>org.janelia.saalfeldlab</groupId>
<artifactId>saalfx</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>

<name>Saal FX</name>
<description>Saalfeld lab JavaFX tools and extensions</description>
Expand Down Expand Up @@ -110,7 +110,7 @@
<scm>
<connection>scm:git:git://github.com/saalfeldlab/saalfx</connection>
<developerConnection>scm:git:[email protected]:saalfeldlab/saalfx.git</developerConnection>
<tag>HEAD</tag>
<tag>saalfx-1.2.0</tag>
<url>https://github.com/saalfeldlab/saalfx</url>
</scm>

Expand Down Expand Up @@ -183,10 +183,10 @@
</dependency>

<!-- logging-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>io.github.oshai</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
<version>5.1.0</version>
</dependency>

<!-- fuzzy matching-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package org.janelia.saalfeldlab.control.mcu;

import io.github.oshai.kotlinlogging.KLogger;
import io.github.oshai.kotlinlogging.KotlinLogging;
import org.janelia.saalfeldlab.control.ButtonControl;

import javax.sound.midi.InvalidMidiDataException;
Expand All @@ -15,6 +17,8 @@
*/
public class MCUButtonControl extends MCUControl implements ButtonControl {

private static final KLogger LOG = KotlinLogging.INSTANCE.logger(() -> null);

public static final int TOGGLE_ON = 127;

public static final int TOGGLE_OFF = 0;
Expand All @@ -29,6 +33,7 @@ public class MCUButtonControl extends MCUControl implements ButtonControl {

private final ShortMessage ledMsg = new ShortMessage();


public MCUButtonControl(final int led, final Receiver rec) {

this.led = led;
Expand All @@ -47,11 +52,19 @@ public void display() {
ledMsg.setMessage(STATUS, led, value);
send(ledMsg);
} catch (final InvalidMidiDataException e) {
e.printStackTrace();
LOG.error(e, () -> null);
}
}
}

public void setValueSilently(final int value) {
var normValue = Math.min(127, Math.max(0, value));
if (normValue != this.value) {
this.value = normValue;
display();
}
}

@Override
public void setValue(final int value) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public int getMax() {
@Override
void update(final int data) {

setValue((0x40 & data) == 0 ? data + 0x40 : data - 0x40);
setValue(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package org.janelia.saalfeldlab.control.mcu;

import io.github.oshai.kotlinlogging.KLogger;
import io.github.oshai.kotlinlogging.KotlinLogging;
import org.janelia.saalfeldlab.control.VPotControl;

import javax.sound.midi.InvalidMidiDataException;
Expand All @@ -26,6 +28,8 @@
*/
public class MCUVPotControl extends MCUControl implements VPotControl {

private static KLogger LOG = KotlinLogging.INSTANCE.logger(() -> null);

public static final int MAX_STEP = 7;
private static final int STATUS = 0xb0;

Expand Down Expand Up @@ -84,7 +88,7 @@ public void display() {
ledMsg.setMessage(STATUS, led, ledCode | j);
send(ledMsg);
} catch (final InvalidMidiDataException e) {
e.printStackTrace();
LOG.error(e, () -> null);
}
} else {
final int k;
Expand Down Expand Up @@ -124,12 +128,12 @@ public void display() {
}
}, 200, TimeUnit.MILLISECONDS);
} catch (final InvalidMidiDataException e) {
e.printStackTrace();
LOG.error(e, () -> null);
}
}
}

void setValueSilently(final int value) {
public void setValueSilently(final int value) {

this.value = Math.min(max, Math.max(min, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ public static XTouchMiniMCUControlPanel build() throws InvalidMidiDataException,

public static void main(final String... args) throws InvalidMidiDataException, MidiUnavailableException, InterruptedException {

build();
var panel = build();
new Thread(() -> {
panel.getFaderControl(0).addListener(i -> System.out.println(i));
panel.getButtonControl(0).addListener(i -> System.out.println(i));
panel.getVPotControl(0).addListener(i -> System.out.println(i));
}).start();
while(true) {
Thread.sleep(20);
}
}
}
14 changes: 7 additions & 7 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/MenuFromHandlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
*/
package org.janelia.saalfeldlab.fx

import io.github.oshai.kotlinlogging.KotlinLogging
import javafx.event.ActionEvent
import javafx.scene.control.ContextMenu
import javafx.scene.control.Menu
import javafx.scene.control.MenuItem
import javafx.scene.control.SeparatorMenuItem
import javafx.util.Pair
import org.slf4j.LoggerFactory
import java.lang.invoke.MethodHandles
import java.util.Arrays
import java.util.Stack
import java.util.function.Consumer
Expand Down Expand Up @@ -67,10 +66,10 @@ class MenuFromHandlers @JvmOverloads constructor(entries: Collection<Pair<String
for (entry in entries) {
val elementPath = MenuPath(*entry.key.split(MENU_SPLIT.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray())
val parentPath = elementPath.parent()
LOG.debug("Adding element {} with parents {} ({})", elementPath, parentPath, entry.key)
LOG.debug { "Adding element $elementPath with parents $parentPath (${entry.key})" }
val mi = MenuItem(elementPath.elementsCopy[elementPath.elementsCopy.size - 1])
.also { it.setOnAction { e -> entry.value.accept(e) } }
LOG.debug("Menu item is mnemonic enabled: {}", mi.isMnemonicParsing)
LOG.debug { "Menu item is mnemonic enabled: ${mi.isMnemonicParsing}" }
if (parentPath.elementsCopy.isEmpty()) {
menu.items += mi
} else {
Expand Down Expand Up @@ -112,10 +111,11 @@ class MenuFromHandlers @JvmOverloads constructor(entries: Collection<Pair<String
for (entry in entries) {
val elementPath = MenuPath(*entry.key.split(MENU_SPLIT_REGEX).dropLastWhile { it.isEmpty() }.toTypedArray())
val parentPath = elementPath.parent()
LOG.debug("Adding element {} with parents {} ({})", elementPath, parentPath, entry.key)

LOG.debug { "Adding element $elementPath with parents $parentPath (${entry.key})" }
val mi = MenuItem(elementPath.elementsCopy[elementPath.elementsCopy.size - 1])
.also { it.setOnAction { e -> entry.value.accept(e) } }
LOG.debug("Menu item is mnemonic enabled: {}", mi.isMnemonicParsing)
LOG.debug { "Menu item is mnemonic enabled: ${mi.isMnemonicParsing}" }
if (parentPath.elementsCopy.isEmpty()) {
menu.items += mi
} else {
Expand Down Expand Up @@ -175,7 +175,7 @@ class MenuFromHandlers @JvmOverloads constructor(entries: Collection<Pair<String
// mnemonics might not work without alt modifier...
// https://bugs.openjdk.java.net/browse/JDK-8090026

private val LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass())
private val LOG = KotlinLogging.logger { }

private const val MENU_SPLIT = ">"

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/SaalFxStyle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.janelia.saalfeldlab.fx.ui.NumberField
object SaalFxStyle {
fun registerStylesheets(styleable : Scene) {
NumberField.registerStyleSheet(styleable)
MatchSelection.registStyleSheet(styleable)
MatchSelection.registerStyleSheet(styleable)
}
fun registerStylesheets(styleable : Parent) {
NumberField.registerStyleSheet(styleable)
MatchSelection.registStyleSheet(styleable)
MatchSelection.registerStyleSheet(styleable)
}
}
12 changes: 6 additions & 6 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/Tasks.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.janelia.saalfeldlab.fx

import com.google.common.util.concurrent.ThreadFactoryBuilder
import io.github.oshai.kotlinlogging.KotlinLogging
import javafx.beans.value.ChangeListener
import javafx.concurrent.Task
import javafx.concurrent.Worker
import javafx.concurrent.Worker.State.*
import javafx.concurrent.WorkerStateEvent
import javafx.event.EventHandler
import org.janelia.saalfeldlab.fx.util.InvokeOnJavaFXApplicationThread
import org.slf4j.LoggerFactory
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory
Expand Down Expand Up @@ -59,7 +59,7 @@ class UtilityTask<V>(private val onCall: (UtilityTask<V>) -> V) : Task<V>() {
private var onFailedSet = false

companion object {
private val LOG = LoggerFactory.getLogger(UtilityTask::class.java)
private val LOG = KotlinLogging.logger { }
}

override fun call(): V? {
Expand All @@ -68,11 +68,11 @@ class UtilityTask<V>(private val onCall: (UtilityTask<V>) -> V) : Task<V>() {
if (!onFailedSet) setDefaultOnFailed()
return onCall(this)
} catch (e: Exception) {
LOG.trace("Task Exception (cancelled=$isCancelled): ", e)
if (isCancelled) {
LOG.trace(e) { "Task Cancelled (cancelled=$isCancelled)" }
return null
}
throw e
throw RuntimeException(e)
}
}

Expand All @@ -82,7 +82,7 @@ class UtilityTask<V>(private val onCall: (UtilityTask<V>) -> V) : Task<V>() {

private fun setDefaultOnFailed() {
InvokeOnJavaFXApplicationThread {
this.onFailed { _, task -> LOG.error(task.exception.stackTraceToString()) }
this.onFailed { _, task -> LOG.error(task.exception) {"Task Failed"} }
}
}

Expand Down Expand Up @@ -232,7 +232,7 @@ class UtilityTask<V>(private val onCall: (UtilityTask<V>) -> V) : Task<V>() {
*/
@JvmOverloads
fun submit(executorService: ExecutorService = this.executorService) : UtilityTask<V> {
this.executorService = executorService;
this.executorService = executorService
this.executorService.submit(this)
return this
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/kotlin/org/janelia/saalfeldlab/fx/actions/Action.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.janelia.saalfeldlab.fx.actions

import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import javafx.event.Event
import javafx.event.EventHandler
import javafx.event.EventType
Expand All @@ -12,8 +14,6 @@ import org.janelia.saalfeldlab.fx.actions.Action.Companion.installAction
import org.janelia.saalfeldlab.fx.actions.Action.Companion.onAction
import org.janelia.saalfeldlab.fx.actions.Action.Companion.removeAction
import org.janelia.saalfeldlab.fx.event.KeyTracker
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.function.Consumer

/**
Expand Down Expand Up @@ -73,10 +73,10 @@ import java.util.function.Consumer
*/
open class Action<E : Event>(val eventType: EventType<E>) {

val logger: Logger by lazy {
open val logger: KLogger by lazy {
val simpleName = this::class.simpleName?.let { ".$it" } ?: ""
val name = ".${name ?: "event-${eventType.name}"}"
LoggerFactory.getLogger("saalfx.action$simpleName$name")
KotlinLogging.logger("saalfx.action$simpleName$name")
}

/**
Expand Down Expand Up @@ -179,12 +179,12 @@ open class Action<E : Event>(val eventType: EventType<E>) {
return keyTracker()?.run {
when {
keysDown!!.isEmpty() && !keysExclusive -> true
keysDown!!.isEmpty() -> noKeysActive().also { if (!it) logger.trace("expected no keys, but some were down") }
keysExclusive -> areOnlyTheseKeysDown(*keysDown!!.toTypedArray()).also { if (!it) logger.trace("expected only these keys: ${keysDown}, but active keys were: (${getActiveKeyCodes(true)}") }
else -> areKeysDown(*keysDown!!.toTypedArray()).also { if (!it) logger.trace("expected keys: $keysDown, but some were not down") }
keysDown!!.isEmpty() -> noKeysActive().also { if (!it) logger.trace { "expected no keys, but some were down" } }
keysExclusive -> areOnlyTheseKeysDown(*keysDown!!.toTypedArray()).also { if (!it) logger.trace { ("expected only these keys: ${keysDown}, but active keys were: (${getActiveKeyCodes(true)}") } }
else -> areKeysDown(*keysDown!!.toTypedArray()).also { if (!it) logger.trace { "expected keys: $keysDown, but some were not down" } }
}
} ?: let {
logger.trace("keyTracker is null")
logger.trace { "keyTracker is null" }
false
}

Expand All @@ -199,7 +199,7 @@ open class Action<E : Event>(val eventType: EventType<E>) {
val msg = description?.let {
"$it (${check::class.java})"
} ?: "(${check::class.java})"
logger.trace("Check: $msg did not pass ")
logger.trace { "Check: $msg did not pass" }
break
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ open class Action<E : Event>(val eventType: EventType<E>) {
fun verifyEventNotNull() {
verify("Event Isn't Null") {
if (it != null) true else {
logger.trace("$name not valid when event is null")
logger.trace { "$name not valid when event is null" }
false
}
}
Expand All @@ -305,17 +305,17 @@ open class Action<E : Event>(val eventType: EventType<E>) {
/* isValid(event) will only be true if event is E */
action(event)
} catch (e: Exception) {
logger.debug("Exception caught: ${e.message}")
logger.debug { "Exception caught: ${e.message}" }
exceptionHandler?.invoke(e) ?: throw e
}
if (consume) {
event?.consume()
}
logger.debug("completed successfully")
logger.debug { "completed successfully" }
true
} else {
if (!isConsumed) {
logger.trace("Event ($event) was invalid for this action")
logger.trace { ("Event ($event) was invalid for this action") }
}
false
}
Expand Down
Loading

0 comments on commit 492ee75

Please sign in to comment.