package jsetool.gui; import java.awt.Color; import java.awt.Paint; import java.io.*; import java.util.Iterator; import jsetool.data.ExplorationState; /** * @author Mike Lawson * * @since Oct 23, 2004 * * The GlobalState class is a view construct that displays an exploration state, * encapsulated from the model's ExplorationStateLeveler, as defined in our * class. * * */ public class GlobalState extends LabeledState implements Externalizable { protected ExplorationState es = null; protected boolean isHighlightedAsTarget = false; protected Paint highlightAsTargetPaint; /** * Default constructs, required for de-serialization. * */ public GlobalState() { decorateStateLabel(); decorateStateColors(); } /** * Constructs a new GlobalState view object for the given ExplorationState * * @param es * the ExplorationState to be drawn. */ public GlobalState(ExplorationState es) { super(es.toString()); this.es = es; decorateStateLabel(); decorateStateColors(); } /** * Turns on or off the highlight for this global state. * * If this global state has any "next" states, they are highlighted as well. * * @param doHighlight * */ public void setHighlight(boolean doHighlight) { // It's important that we just return if the flags already match. if (doHighlight == getIsHighlighted()) return; super.setHighlight(doHighlight); // For all connected States, highlight or not as a target. for (Iterator nextStateIterator = es.nextStateIterator(); nextStateIterator.hasNext();) { ExplorationState.NextStateContainer nextState = (ExplorationState.NextStateContainer) nextStateIterator.next(); GlobalState gs = (GlobalState) nextState.getExplorationState().getAssociatedAttribute(); gs.setHighlightedAsTarget(doHighlight); } } /** * Called by another global state if this global state should be highlight in * conjunction with it. * * @param isHighlightedAsTarget */ public void setHighlightedAsTarget(boolean isHighlightedAsTarget) { this.isHighlightedAsTarget = isHighlightedAsTarget; } /** * Get the associated ExplorationState * * @return ExplorationState */ public ExplorationState getExplorationState() { return es; } /** * Extends LabeledObject's read external to read the associated state. * @param in * @throws IOException */ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { super.readExternal(in); es = (ExplorationState)in.readObject(); } /** * Extends LabeledObject's write external to write out the associated state. * @param out * @throws IOException */ public void writeExternal(ObjectOutput out) throws IOException { super.writeExternal(out); out.writeObject(es); } /** * Sets the label of the state. * */ protected void decorateStateLabel() { if (es == null) return; // required for de-serialization // Change the label of deadlocked // or unspecified reception states. if (es.isStateIsDeadlocked()) { setStateLabel(getStateLabel() + "(D)"); } else if (es.isStateIsUnspecifiedReception()) { setStateLabel(getStateLabel() + "(U)"); } } /** * Sets up the colors that this state will be drawn. * */ protected void decorateStateColors() { highlightAsTargetPaint = Color.GREEN; if (es == null) return; // required for de-serialization // Change the label of deadlocked // or unspecified reception states. if (es.isStateIsDeadlocked()) { super.setNormalPaint(Color.RED); } else if (es.isStateIsUnspecifiedReception()) { super.setNormalPaint(Color.ORANGE); } } /** * Overrides the super's method and returns the highlight as a target paint if * necessary for this global state. * * @return Paint */ protected Paint getPaintColor() { if (isHighlightedAsTarget) { return highlightAsTargetPaint; } else { return super.getPaintColor(); } } }