Skip to content
Snippets Groups Projects
Commit 7a031fdc authored by Piotr Gawron's avatar Piotr Gawron
Browse files

common functionality for match and apply coloring added

parent 4cabe7e5
No related branches found
No related tags found
1 merge request!171Resolve "data overlay should support element id"
......@@ -11,6 +11,7 @@ import org.apache.log4j.Logger;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.model.graphics.ArrowTypeData;
import lcsb.mapviewer.model.map.BioEntity;
......@@ -189,6 +190,22 @@ public class ColorModelCommand extends ModelCommand {
}
}
protected boolean match(BioEntity element, ColorSchema schema) {
if (schema.getElementId() != null && !schema.getElementId().isEmpty()) {
if (!element.getElementId().equalsIgnoreCase(schema.getElementId())) {
return false;
}
}
if (element instanceof Element) {
return match((Element) element, schema);
} else if (element instanceof Reaction) {
return match((Reaction) element, schema);
} else {
throw new InvalidArgumentException("Don't know how to handle object: " + element);
}
}
/**
* Checks if the coloring schema should be used for the {@link Element}.
*
......@@ -209,11 +226,6 @@ public class ColorModelCommand extends ModelCommand {
return false;
}
}
if (schema.getElementId() != null && !schema.getElementId().isEmpty()) {
if (!element.getElementId().equalsIgnoreCase(schema.getElementId())) {
return false;
}
}
if (schema.getTypes().size() > 0) {
boolean found = false;
for (Class<?> clazz : schema.getTypes()) {
......@@ -277,16 +289,12 @@ public class ColorModelCommand extends ModelCommand {
List<ColorSchema> result = new ArrayList<ColorSchema>();
for (ColorSchema schema : schemas) {
boolean found = false;
for (Element element : getModel().getElements()) {
for (BioEntity element : getModel().getBioEntities()) {
if (match(element, schema)) {
found = true;
}
}
for (Reaction reaction : getModel().getReactions()) {
if (match(reaction, schema)) {
found = true;
}
}
if (!found) {
result.add(schema);
}
......@@ -310,28 +318,18 @@ public class ColorModelCommand extends ModelCommand {
List<Model> models = new ArrayList<>();
models.add(getModel());
models.addAll(getModel().getSubmodels());
for (Model model2 : models) {
for (Model model : models) {
for (ColorSchema schema : schemas) {
for (Element element : model2.getElements()) {
for (BioEntity element : model.getBioEntities()) {
if (match(element, schema)) {
if (result.get(element) != null
&& !colorExtractor.getNormalizedColor(result.get(element)).equals(Color.WHITE)) {
throw new InvalidColorSchemaException(
eu.getElementTag(element) + "Element is colored by more than one rule.");
eu.getElementTag(element) + "BioEntity is colored by more than one rule.");
}
result.put(element, schema);
}
}
for (Reaction reaction : model2.getReactions()) {
if (match(reaction, schema)) {
if (result.get(reaction) != null
&& !colorExtractor.getNormalizedColor(result.get(reaction)).equals(Color.WHITE)) {
throw new InvalidColorSchemaException(
eu.getElementTag(reaction) + "Reaction is colored by more than one rule.");
}
result.put(reaction, schema);
}
}
}
}
return result;
......@@ -371,18 +369,12 @@ public class ColorModelCommand extends ModelCommand {
}
for (ColorSchema schema : schemas) {
for (Element element : result.getElements()) {
for (BioEntity element : result.getBioEntities()) {
if (match(element, schema)) {
schema.setMatches(schema.getMatches() + 1);
applyColor(element, schema);
}
}
for (Reaction reaction : result.getReactions()) {
if (match(reaction, schema)) {
schema.setMatches(schema.getMatches() + 1);
applyColor(reaction, schema);
}
}
}
if (top) {
......@@ -393,6 +385,16 @@ public class ColorModelCommand extends ModelCommand {
}
private void applyColor(BioEntity element, ColorSchema schema) throws InvalidColorSchemaException {
if (element instanceof Element) {
applyColor((Element) element, schema);
} else if (element instanceof Reaction) {
applyColor((Reaction) element, schema);
} else {
throw new InvalidArgumentException("Don't know how to handle: " + element);
}
}
@Override
protected void executeImplementation() throws CommandExecutionException {
try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment