public List debugLastStates() {
    List states = new ArrayList();

    Set set = machine.getStateSet();
    if (set.isEmpty()) set = machine.getStartStates();

    Iterator iterator = set.iterator();
    while (iterator.hasNext()) {
      states.add(getState((String) iterator.next()));
    }

    return states;
  }
  public String check(String s) {
    String error = machine.check();
    if (error != null) {
      JOptionPane.showMessageDialog(
          null,
          Localized.getString("faCannotStart") + "\n" + error,
          Localized.getString("error"),
          JOptionPane.INFORMATION_MESSAGE,
          null);
      return null;
    }

    if (s == null)
      s =
          (String)
              JOptionPane.showInputDialog(
                  null,
                  Localized.getString("faParseString"),
                  Localized.getString("faStartTitle"),
                  JOptionPane.QUESTION_MESSAGE,
                  null,
                  null,
                  null);

    return s;
  }
  public void debugReset(String s) {
    String r = check(s);
    if (r == null) return;

    state = STATE_READY;
    machine.debugReset(r);
  }
  public boolean createLink(
      GElementFAState source,
      String sourceAnchorKey,
      GElementFAState target,
      String targetAnchorKey,
      int shape,
      Point mouse) {
    String pattern =
        (String)
            JOptionPane.showInputDialog(
                null,
                Localized.getString("faNewLinkMessage"),
                Localized.getString("faNewLinkTitle"),
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                null);
    if (pattern != null) {
      machine.addTransitionPattern(source.state.name, pattern, target.state.name);
      addElement(
          new GLink(
              source,
              sourceAnchorKey,
              target,
              targetAnchorKey,
              shape,
              pattern,
              mouse,
              GView.DEFAULT_LINK_FLATENESS));
    }

    return pattern != null;
  }
  public List debugLastTransitions() {
    List transitions = new ArrayList();

    Iterator iterator = machine.getLastTransitionSet().iterator();
    while (iterator.hasNext()) {
      transitions.add(getTransition((FATransition) iterator.next()));
    }

    return transitions;
  }
  public void reconstruct() {
    elements.clear();
    List stateNames = machine.getStateList();

    ListIterator iterator = stateNames.listIterator(stateNames.size());

    int x = 0;
    int y = 0;
    while (iterator.hasPrevious()) {
      addElement(new GElementFAState((FAState) iterator.previous(), 100 + x * 200, 50 + y * 200));
      x++;
      if (x > 4) {
        y++;
        x = 0;
      }
    }

    List transitions = machine.getTransitions().getTransitions();
    iterator = transitions.listIterator();
    while (iterator.hasNext()) {
      FATransition transition = (FATransition) iterator.next();

      GElementFAState s1 = getState(transition.s1);
      GElementFAState s2 = s1;
      if (transition.s1.equals(transition.s2) == false) s2 = getState(transition.s2);

      GLink link = getLink(s1, s2);
      if (link == null)
        addElement(
            new GLink(
                s1,
                GElementFAState.ANCHOR_CENTER,
                s2,
                GElementFAState.ANCHOR_CENTER,
                GLink.SHAPE_ARC,
                transition.symbol,
                20));
      else link.pattern = Tool.addSymbolToPattern(link.pattern, transition.symbol);
    }
  }
  /**
   * Called to convert FSM to LaTeX code. Once the Latex is generated, a popup window is created
   * from within the function.
   */
  public void toLatex() {
    List<FATransition> transitions = machine.getTransitionList();

    String latex = "\\begin{tikzpicture}[shorten >=1pt, node distance = 2.5cm, auto]\n";

    // Following block is for state data
    // TODO Insert positioning data

    List<GElementFAState> nodes = getStates();

    Vector2D scale = getScaleFactor(nodes);

    // latex += "minX " + this.left + " minY " + this.bottom + " maxX " +
    // this.right + " maxY " + this.top;

    latex += nodesToLatex(nodes, scale);

    Iterator<FATransition> it = transitions.iterator();

    List<String> transitionToLatex = new LinkedList();

    while (it.hasNext()) {
      FATransition transition = it.next();

      int node1 = getLatexId(transition.s1);
      int node2 = getLatexId(transition.s2);

      String s = new String();
      s = "\t\t(q" + node1 + ")";
      if (node1 == node2) {
        s += "\tedge [loop] node {" + transition.symbol + "}";
      } else {
        s += "\tedge node {" + transition.symbol + "}";
      }
      s += " (q" + node2 + ")";
      transitionToLatex.add(s);
    }

    latex += "\\path[->]";
    for (String s : transitionToLatex) {
      latex += "\n" + s;
    }
    latex += ";\n";

    // End the LaTeX FA element
    latex += "\\end{tikzpicture}";

    LaTexWindow w = new LaTexWindow(latex);
    w.setVisible(true);
  }
 public void removeState(GElementFAState s) {
   machine.removeState(s.state);
   removeElement(s);
   // Remove any other link which is using the state s
   ListIterator e = elements.listIterator();
   while (e.hasNext()) {
     GElement element = (GElement) e.next();
     if (element.getClass().equals(GLink.class)) {
       GLink link = (GLink) element;
       if (link.source == s || link.target == s) {
         removeElement(link);
         e = elements.listIterator();
       }
     }
   }
 }
  public void run(String s) {
    s = check(s);
    if (s == null) return;

    if (machine.accept(s))
      JOptionPane.showMessageDialog(
          null,
          Localized.getString("faAcceptString"),
          Localized.getString("automaton"),
          JOptionPane.INFORMATION_MESSAGE,
          null);
    else
      JOptionPane.showMessageDialog(
          null,
          Localized.getString("faRejectString"),
          Localized.getString("automaton"),
          JOptionPane.ERROR_MESSAGE,
          null);
  }
  public boolean editLink(GLink link) {
    String pattern =
        (String)
            JOptionPane.showInputDialog(
                null,
                Localized.getString("faEditLinkMessage"),
                Localized.getString("faEditLinkTitle"),
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                link.pattern);
    if (pattern != null) {
      removeLink(link);
      link.pattern = pattern;
      machine.addTransitionPattern(
          getState1(link).state.name, link.pattern, getState2(link).state.name);
      addElement(link);
    }

    return pattern != null;
  }
 public void addStateAtXY(String s, double x, double y) {
   FAState state = new FAState(s);
   machine.addState(state);
   addElement(new GElementFAState(state, x, y));
 }
 public Object[] getSymbols() {
   return machine.getSymbols().toArray();
 }
 public void clear() {
   machine.clear();
   elements.clear();
 }
 public void debugStepForward() {
   if (machine.debugStepForward()) state = STATE_PAUSED;
   else state = STATE_STOPPED;
 }
 public void removeLink(GLink link) {
   removeElement(link);
   machine.removeTransitionPattern(
       getState1(link).state.name, link.pattern, getState2(link).state.name);
 }