Skip to content

Commit

Permalink
Support additional fields added in ImageJ 1.54
Browse files Browse the repository at this point in the history
addFileField
addButton

Tests have been added to demonstrate these also work:

addImageChoice
addEnumChoice

These methods delegate to addChoice but auto populate the array of
choices.
  • Loading branch information
aherbert committed Jun 27, 2023
1 parent 9716159 commit 02b431d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,24 @@ public TextField addFilenameField(final String label, String defaultText, int co
return tf;
}

@Override
public void addFileField(String label, String defaultPath) {
// Replace ij.gui.GenericDialog method with this implementation
addFilenameField(label, defaultPath);
}

@Override
public void addFileField(String label, String defaultPath, int columns) {
// Replace ij.gui.GenericDialog method with this implementation
addFilenameField(label, defaultPath, columns);
}

@Override
public void addButton(String label, ActionListener listener) {
// Replace ij.gui.GenericDialog method with this implementation
addAndGetButton(label, listener);
}

/**
* Adds the directory field.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@

package uk.ac.sussex.gdsc.core.ij.gui;

import ij.IJ;
import ij.ImagePlus;
import ij.Macro;
import ij.gui.GenericDialog;
import java.awt.Color;
import java.time.Duration;
import org.junit.jupiter.api.Assertions;
Expand All @@ -39,6 +42,10 @@
@SuppressWarnings({"javadoc"})
class ExtendedGenericDialogTest {

enum TestEnum {
A, B, C, D
}

@Test
@DisabledIfHeadless
void checkNotifyOptionCollectedListeners() {
Expand Down Expand Up @@ -127,4 +134,50 @@ void checkAddHexField() {
Assertions.assertArrayEquals(b2, gd.getNextHexBytes());
});
}

/**
* Check {@link GenericDialog#addImageChoice(String, String)} is supported without any overrides.
*/
@Test
@DisabledIfHeadless
void checkAddImageChoice() {
Assertions.assertTimeoutPreemptively(Duration.ofMillis(1000), () -> {
// Run without showing the dialog.
// This requires manipulation of the thread name and setting of macro options.
Thread.currentThread().setName("Run$_ test");
Macro.setOptions("something");

final ExtendedGenericDialog gd = new ExtendedGenericDialog("Test");
String name1 = "test1";
ImagePlus imp1 = IJ.createImage(name1, 3, 4, 1, 8);
imp1.show();
gd.addImageChoice("image1", name1);
gd.showDialog();
Assertions.assertEquals(imp1, gd.getNextImage());
imp1.close();
});
}

/**
* Check {@link GenericDialog#addEnumChoice(String, Enum)} is supported without any overrides.
*/
@Test
@DisabledIfHeadless
void checkAddEnumChoice() {
Assertions.assertTimeoutPreemptively(Duration.ofMillis(1000), () -> {
// Run without showing the dialog.
// This requires manipulation of the thread name and setting of macro options.
Thread.currentThread().setName("Run$_ test");
Macro.setOptions("something");

final ExtendedGenericDialog gd = new ExtendedGenericDialog("Test");
TestEnum ll1 = TestEnum.B;
TestEnum ll2 = TestEnum.C;
gd.addEnumChoice("enum1", ll1);
gd.addEnumChoice("enum2", new TestEnum[] {TestEnum.A, ll2}, ll2);
gd.showDialog();
Assertions.assertEquals(ll1, gd.getNextEnumChoice(TestEnum.class));
Assertions.assertEquals(ll2, gd.getNextEnumChoice(ll2.getDeclaringClass()));
});
}
}

0 comments on commit 02b431d

Please sign in to comment.