Commit bf77b949 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

allow to color twice with the same color

parent 8c785c7d
Pipeline #19974 passed with stage
in 12 minutes and 20 seconds
package lcsb.mapviewer.commands;
import java.awt.*;
import java.awt.Color;
import java.util.*;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -82,7 +81,8 @@ public class ColorModelCommand extends ModelCommand {
* thrown when {@link Reaction} was already colored by other schema
*/
void applyColor(Reaction reaction, ColorSchema schema) throws InvalidColorSchemaException {
if (!reaction.getReactants().get(0).getLine().getColor().equals(Color.BLACK)) {
if (!reaction.getReactants().get(0).getLine().getColor().equals(Color.BLACK)
&& !isEqualColoring(schema, reaction.getReactants().get(0).getLine().getColor())) {
throw new InvalidColorSchemaException(
"At least two rows try to set color to reaction: " + reaction.getIdReaction());
}
......@@ -162,13 +162,17 @@ public class ColorModelCommand extends ModelCommand {
*/
private void applyColor(Element element, ColorSchema schema) throws InvalidColorSchemaException {
if (element instanceof Species) {
if (!element.getColor().equals(Color.WHITE)) {
if (!element.getColor().equals(Color.WHITE) && !isEqualColoring(schema, element.getColor())) {
throw new InvalidColorSchemaException("At least two rows try to set color to element: " + element.getName());
}
element.setColor(colorExtractor.getNormalizedColor(schema));
}
}
private boolean isEqualColoring(ColorSchema schema, Color color) {
return Objects.equals(colorExtractor.getNormalizedColor(schema), color);
}
protected boolean match(BioEntity element, ColorSchema schema) {
if (schema.getElementId() != null && !schema.getElementId().isEmpty()) {
if (!element.getElementId().equalsIgnoreCase(schema.getElementId())) {
......@@ -295,7 +299,7 @@ public class ColorModelCommand extends ModelCommand {
for (BioEntity element : model.getBioEntities()) {
for (ColorSchema schema : schemas) {
if (match(element, schema)) {
if (result.get(element) != null
if (result.get(element) != null && !isEqualColoring(result.get(element), schema)
&& !colorExtractor.getNormalizedColor(result.get(element)).equals(Color.WHITE)) {
throw new InvalidColorSchemaException(
eu.getElementTag(element) + "BioEntity is colored by more than one rule.");
......@@ -308,6 +312,10 @@ public class ColorModelCommand extends ModelCommand {
return result;
}
private boolean isEqualColoring(ColorSchema colorSchema, ColorSchema colorSchema2) {
return isEqualColoring(colorSchema, colorExtractor.getNormalizedColor(colorSchema2));
}
@Override
protected void undoImplementation() throws CommandExecutionException {
throw new NotImplementedException();
......
......@@ -83,6 +83,25 @@ public class ColorModelCommandTest extends CommandTestFunctions {
assertEquals(1, missing.size());
}
@Test
public void testColorTheSameElementTwiceUsingDifferentSelector() throws Exception {
Model model = getModelForFile("testFiles/coloring_model.xml", true);
Collection<ColorSchema> schemas = new ArrayList<>();
ColorSchema schema = new GenericColorSchema();
schema.addMiriamData(new MiriamData(MiriamType.HGNC, "11138"));
schema.setValue(1.0);
schemas.add(schema);
schema = new GenericColorSchema();
schema.setName("SNCA");
schema.setValue(1.0);
schemas.add(schema);
ColorModelCommand factory = new ColorModelCommand(model, schemas, colorExtractor);
Map<BioEntity, ColorSchema> modifiedElements = factory.getModifiedElements();
assertEquals(1, modifiedElements.keySet().size());
}
@Test
public void testReactionColoring1() throws Exception {
Model model = getModelForFile("testFiles/reactions_to_color.xml", false);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment