Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/main/java/org/scijava/display/DefaultDisplayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
package org.scijava.display;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import org.scijava.Prioritized;
import org.scijava.display.event.DisplayActivatedEvent;
import org.scijava.display.event.DisplayCreatedEvent;
import org.scijava.display.event.DisplayDeletedEvent;
Expand Down Expand Up @@ -219,20 +221,28 @@ public Display<?> createDisplay(final String name, final Object o) {

@Override
public Display<?> createDisplayQuietly(final Object o) {
final List<Display<?>> matchingDisplays = getMatchingDisplays(o);
if(matchingDisplays.size() > 0) {
// use the display with the highest priority
Display<?> display = matchingDisplays.stream().max(Comparator.comparing(Prioritized::getPriority)).get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this. The displays should already always be returned from the PluginService in high-to-low priority. Did you observe otherwise?

display.display(o);
return display;
}
return null;
}

List<Display<?>> getMatchingDisplays(Object o) {
// get available display plugins from the plugin service
final List<PluginInfo<Display<?>>> displayPlugins = getDisplayPlugins();

final List<Display<?>> matchingDisplays = new ArrayList<>();
for (final PluginInfo<Display<?>> info : displayPlugins) {
final Display<?> display = pluginService.createInstance(info);
if (display == null) continue;
// display object using the first compatible Display
// TODO: how to handle multiple matches? prompt user with dialog box?
if (display.canDisplay(o)) {
display.display(o);
return display;
matchingDisplays.add(display);
}
}
return null;
return matchingDisplays;
}

// -- Event handlers --
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/scijava/display/CustomTextDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.scijava.display;

public class CustomTextDisplay extends AbstractDisplay<String> implements
TextDisplay
{

public CustomTextDisplay() {
super(String.class);
}

@Override
public void append(final String text) {
add(text);
}

}
21 changes: 21 additions & 0 deletions src/test/java/org/scijava/display/DisplayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@

import org.junit.Test;
import org.scijava.Context;
import org.scijava.Priority;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;

import java.util.List;

/**
* Unit tests for core {@link Display} classes.
Expand Down Expand Up @@ -132,4 +137,20 @@ public void testText() {
assertEquals(value, result);
}

@Test
public void testMultipleDisplaysPriorityMatch() {
final Context context = new Context(DisplayService.class);
PluginInfo<Display> info = PluginInfo.create(CustomTextDisplay.class, Display.class);
info.setPriority(Priority.HIGH);
context.service(PluginService.class).addPlugin(info);
final DefaultDisplayService displayService =
context.getService(DefaultDisplayService.class);
final String name = "Text";
final String value = "Hello";
List<Display<?>> matchingDisplays = displayService.getMatchingDisplays(value);
assertNotNull(matchingDisplays);
assertTrue(matchingDisplays.size() > 1);
Display<?> display = displayService.createDisplay(name, value);
assertEquals(CustomTextDisplay.class, display.getClass());
Copy link
Member

@imagejan imagejan Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to asserting that CustomTextDisplay gets chosen, can we also test that we actually do get multiple candidate Displays that are able to handle Strings? Otherwise the name of this method would be misleading, no?

}
}