use of org.csstudio.display.builder.model.properties.NamedWidgetColor in project org.csstudio.display.builder by kasemir.
the class ColorUnitTest method testDefaultColors.
/**
* Test fetching named colors
* @throws Exception on error
*/
@Test
public void testDefaultColors() throws Exception {
final NamedWidgetColors colors = new NamedWidgetColors();
// Fetch default alarm color
NamedWidgetColor color = colors.getColor(NamedWidgetColors.ALARM_MAJOR).orElse(null);
System.out.println(color);
assertThat(color, not(nullValue()));
assertThat(color.getRed(), equalTo(255));
// Redefine, temporarily
colors.define(new NamedWidgetColor(color.getName(), 0, 0, 0));
color = colors.getColor(NamedWidgetColors.ALARM_MAJOR).orElse(null);
System.out.println(color);
assertThat(color.getRed(), equalTo(0));
// STOP is not known by default
color = colors.getColor("STOP").orElse(null);
assertThat(color, nullValue());
// Load STOP, also load MAJOR back to default
colors.read(new FileInputStream("../org.csstudio.display.builder.model/examples/color.def"));
color = colors.getColor(NamedWidgetColors.ALARM_MAJOR).orElse(null);
System.out.println(color);
assertThat(color, not(nullValue()));
assertThat(color.getRed(), equalTo(255));
color = colors.getColor("STOP").orElse(null);
System.out.println(color);
assertThat(color, not(nullValue()));
}
use of org.csstudio.display.builder.model.properties.NamedWidgetColor in project org.csstudio.display.builder by kasemir.
the class ColorUnitTest method testNamedColors.
/**
* Test fetching named colors
* @throws Exception on error
*/
@Test
public void testNamedColors() throws Exception {
final NamedWidgetColors colors = new NamedWidgetColors();
NamedWidgetColor color = colors.getColor(NamedWidgetColors.ALARM_MAJOR).orElse(null);
System.out.println(color);
assertThat(color, not(nullValue()));
assertThat(color.getRed(), equalTo(255));
color = colors.getColor("STOP").orElse(null);
assertThat(color, nullValue());
colors.read(new FileInputStream("../org.csstudio.display.builder.model/examples/color.def"));
color = colors.getColor("STOP").orElse(null);
System.out.println(color);
assertThat(color, not(nullValue()));
}
use of org.csstudio.display.builder.model.properties.NamedWidgetColor in project org.csstudio.display.builder by kasemir.
the class XMLUtil method getChildColor.
/**
* Given a parent element, locate color value of a child node.
*
* @param parent Parent element.
* @param name Name of child element.
* @return Value of child element, or empty result.
*/
public static Optional<WidgetColor> getChildColor(final Element parent, final String name) {
final Element child = getChildElement(parent, name);
if (child != null) {
Element colorElement = XMLUtil.getChildElement(child, XMLTags.COLOR);
if (colorElement == null) {
return Optional.empty();
}
WidgetColor color = null;
String colorName = colorElement.getAttribute(XMLTags.NAME);
try {
int red = getAttribute(colorElement, XMLTags.RED);
int green = getAttribute(colorElement, XMLTags.GREEN);
int blue = getAttribute(colorElement, XMLTags.BLUE);
String alphaString = colorElement.getAttribute(XMLTags.ALPHA);
int alpha = alphaString.isEmpty() ? 255 : Integer.parseInt(alphaString);
if (colorName.isEmpty()) {
// Plain color
color = new WidgetColor(red, green, blue, alpha);
} else {
color = WidgetColorService.getColors().resolve(new NamedWidgetColor(colorName, red, green, blue, alpha));
}
} catch (Exception ex) {
// Older legacy files had no red/green/blue info for named colors
logger.log(Level.WARNING, "Line " + XMLUtil.getLineInfo(child), ex);
if (colorName.isEmpty()) {
color = WidgetColorService.getColor(NamedWidgetColors.TEXT);
} else {
color = WidgetColorService.getColor(colorName);
}
}
return (color == null) ? Optional.empty() : Optional.of(color);
} else {
return Optional.empty();
}
}
use of org.csstudio.display.builder.model.properties.NamedWidgetColor in project org.csstudio.display.builder by kasemir.
the class NamedWidgetColors method parse.
@Override
protected void parse(final String name, final String value) throws Exception {
Optional<NamedWidgetColor> optionalColor = getColor(value);
if (optionalColor.isPresent()) {
NamedWidgetColor namedColor = optionalColor.get();
define(new NamedWidgetColor(name, namedColor.getRed(), namedColor.getGreen(), namedColor.getBlue(), namedColor.getAlpha()));
} else {
final StringTokenizer tokenizer = new StringTokenizer(value, ",");
try {
final int red = Integer.parseInt(tokenizer.nextToken().trim());
final int green = Integer.parseInt(tokenizer.nextToken().trim());
final int blue = Integer.parseInt(tokenizer.nextToken().trim());
final int alpha = tokenizer.hasMoreTokens() ? Integer.parseInt(tokenizer.nextToken().trim()) : 255;
define(new NamedWidgetColor(name, red, green, blue, alpha));
} catch (Throwable ex) {
throw new Exception("Cannot parse color '" + name + "' from '" + value + "'", ex);
}
}
}
use of org.csstudio.display.builder.model.properties.NamedWidgetColor in project org.csstudio.display.builder by kasemir.
the class ColorUnitTest method testColorService.
/**
* Test fetching named colors from service
*
* Time-based test, may occasionally fail because background thread doesn't get to run as expected
*
* @throws Exception on error
*/
@Test(timeout = 30000)
public void testColorService() throws Exception {
System.out.println("On " + Thread.currentThread().getName());
// Default colors do not include 'STOP'
NamedWidgetColors colors = WidgetColorService.getColors();
NamedWidgetColor color = colors.getColor("STOP").orElse(null);
assertThat(color, nullValue());
int delay_seconds = 10;
// Load colors, using a source with artificial delay
final DelayedStream slow_color_source = new DelayedStream("../org.csstudio.display.builder.model/examples/color.def");
WidgetColorService.loadColors(new String[] { "Slow file" }, file -> slow_color_source.call());
// Getting the colors is now delayed by the WidgetColorService.LOAD_DELAY
long start = System.currentTimeMillis();
colors = WidgetColorService.getColors();
double seconds = (System.currentTimeMillis() - start) / 1000.0;
System.out.println("Loading the slow color file took " + seconds + " seconds");
// Should get default names, because slow_color_source is not ready, yet
color = colors.getColor("STOP").orElse(null);
System.out.println("'STOP' should be null: " + color);
assertThat(color, nullValue());
// The file is still loading, and eventually we should get it
slow_color_source.proceed();
TimeUnit.SECONDS.sleep(delay_seconds);
start = System.currentTimeMillis();
colors = WidgetColorService.getColors();
seconds = (System.currentTimeMillis() - start) / 1000.0;
System.out.println("Fetching colors after the file got loaded took " + seconds + " seconds");
color = colors.getColor("STOP").orElse(null);
System.out.println(color);
assertThat(color, not(nullValue()));
}
Aggregations