Contextual adapters must throw instead of returning null
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / monitor / ObtainedColorAdapter.java
1 package org.simantics.modeling.ui.diagram.monitor;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 import org.simantics.common.color.Color;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.RelationContext;
10 import org.simantics.db.Resource;
11 import org.simantics.db.Statement;
12 import org.simantics.db.common.adaption.SimpleContextualAdapter;
13 import org.simantics.db.exception.AdaptionException;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.property.OrderedResource;
16 import org.simantics.diagram.stubs.DiagramResource;
17 import org.simantics.operation.Layer0X;
18
19 public class ObtainedColorAdapter extends SimpleContextualAdapter<Color, RelationContext> {
20
21         @Override
22         public Color adapt(ReadGraph graph, Resource source, RelationContext context) throws DatabaseException {
23                 
24                 Layer0X L0X = Layer0X.getInstance(graph);
25                 DiagramResource DIA = DiagramResource.getInstance(graph);
26                 ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();
27                 for(Statement stm : graph.getStatements(context.getSubject(), L0X.ObtainsProperty)) {
28                         Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);
29                         order.add(new OrderedResource(position, stm.getObject()));
30                 }
31                 if (order.size() > 1)
32                         Collections.sort(order);
33                 for(OrderedResource or : order) {
34                         Color result = graph.getPossibleRelatedAdapter(or.r, DIA.HasColor, Color.class);
35                         if(result != null) return result;
36                 }
37                 
38                 throw new AdaptionException("No obtained color available");
39                 
40         }
41
42 }