diff --git a/src/main/java/uppaal/ColorUtil.java b/src/main/java/uppaal/ColorUtil.java new file mode 100644 index 0000000..4b44de6 --- /dev/null +++ b/src/main/java/uppaal/ColorUtil.java @@ -0,0 +1,36 @@ +package uppaal; + +import org.jdom.Element; + +import java.awt.Color; + +class ColorUtil { + /** + * Tries to find the color of the XML `element`. + * Returns `null` if a color is not found. + * @param element The XML element with a possible `color` attribute. + * @return Color + */ + static Color findColor(Element element) { + String color = element.getAttributeValue("color"); + try { + return Color.decode(color); + } catch (NumberFormatException | NullPointerException e) { + return null; + } + } + + /** + * Converts a `Color` to a hexadecimal color string (`#RRGGBB`) + * @param color The Color to convert. + * @return String + * @throws NullPointerException if `color` is `null` + */ + static String toHexString(Color color) throws NullPointerException { + String hexColor = Integer.toHexString(color.getRGB() & 0xffffff); + if (hexColor.length() < 6) { + hexColor = "000000".substring(0, 6 - hexColor.length()) + hexColor; + } + return "#" + hexColor; + } +} \ No newline at end of file diff --git a/src/main/java/uppaal/Location.java b/src/main/java/uppaal/Location.java index b764815..4426725 100644 --- a/src/main/java/uppaal/Location.java +++ b/src/main/java/uppaal/Location.java @@ -1,5 +1,6 @@ package uppaal; +import java.awt.Color; import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; @@ -50,6 +51,7 @@ protected static int getNewID(){ private ExponentialRate expRate; private Name name; private LocationType type; + private Color color; /* * todo: ideally, a separate type should be made for branchpoints */ @@ -62,6 +64,7 @@ public Location(Automaton automaton) { public Location(Automaton automaton, Element locationElement) { super(locationElement); // get coordinates type = getLocationType(locationElement); + color = ColorUtil.findColor(locationElement); @SuppressWarnings("unchecked") List children = locationElement.getChildren(); for(Element child: children){ @@ -153,7 +156,12 @@ public Element generateXMLElement() { default: break; } - } + } + + if (color!=null) { + result.setAttribute("color", ColorUtil.toHexString(color)); + } + return result; } @@ -192,7 +200,11 @@ public Name getName() { public LocationType getType() { return type; } - + + public Color getColor() { + return color; + } + public void setBranchPointLocation(boolean isBranchPoint) { this.branchPointLocation = isBranchPoint; } @@ -249,6 +261,10 @@ public void setType(LocationType type) { this.type = type; } + public void setColor(Color color) { + this.color = color; + } + @Override public String toString(){ return name.toString(); diff --git a/src/main/java/uppaal/Transition.java b/src/main/java/uppaal/Transition.java index 33cedd7..75aea6f 100644 --- a/src/main/java/uppaal/Transition.java +++ b/src/main/java/uppaal/Transition.java @@ -1,5 +1,6 @@ package uppaal; +import java.awt.Color; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -39,7 +40,8 @@ public void setPositioned(boolean positioned) { private List nails = new ArrayList(); private Update update; - + private Color color; + public int removeStumpAngles(double angleThreshold, double lengthThreshold){ // dot product: Ax * Bx + Ay * By // angle = acos(dot(v1, v2)) @@ -126,6 +128,7 @@ public Transition(Automaton automaton, Element transitionElement) { assert dbg_matchresult; destination = automaton.id_locationMap.get("id" + matcher.group(1)); destination.addIncomingTransition(this); + color = ColorUtil.findColor(transitionElement); @SuppressWarnings("unchecked") List children = transitionElement.getChildren(); for(Element child: children){ @@ -216,8 +219,15 @@ public void addUpdate(Update update){ else this.update.add(update); } - - + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + /** * Creates an XML Element object corresponding to the Transition object * @return XML Element @@ -247,7 +257,9 @@ Element generateXMLElement(){ result.addContent(this.update.generateXMLElement()); if(prob!=null) result.addContent(this.prob.generateXMLElement()); - + if (color!=null) + result.setAttribute("color", ColorUtil.toHexString(color)); + for (Nail nail : nails) { result.addContent(nail.generateXMLElement()); }