Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/uppaal/ColorUtil.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 18 additions & 2 deletions src/main/java/uppaal/Location.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uppaal;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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<Element> children = locationElement.getChildren();
for(Element child: children){
Expand Down Expand Up @@ -153,7 +156,12 @@ public Element generateXMLElement() {
default:
break;
}
}
}

if (color!=null) {
result.setAttribute("color", ColorUtil.toHexString(color));
}

return result;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/uppaal/Transition.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uppaal;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -39,7 +40,8 @@ public void setPositioned(boolean positioned) {

private List<Nail> nails = new ArrayList<Nail>();
private Update update;

private Color color;

public int removeStumpAngles(double angleThreshold, double lengthThreshold){
// dot product: Ax * Bx + Ay * By
// angle = acos(dot(v1, v2))
Expand Down Expand Up @@ -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<Element> children = transitionElement.getChildren();
for(Element child: children){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
Expand Down