gt:             goalTransformer   = tFactory.newTransformer(getXSL("goal"));
context: package moise.tools;
  
  import java.awt.Dimension;
  import java.awt.EventQueue;
  import java.awt.Toolkit;
  import java.io.StringReader;
  import java.io.StringWriter;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Stack;
  import java.util.Vector;
  
  import javax.swing.DefaultComboBoxModel;
  import javax.swing.ImageIcon;
  import javax.swing.JComboBox;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.event.HyperlinkEvent;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.xml.transform.Source;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerConfigurationException;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.URIResolver;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.stream.StreamSource;
  
  import moise.oe.GoalInstance;
  import moise.oe.GroupInstance;
  import moise.oe.MissionPlayer;
  import moise.oe.OE;
  import moise.oe.OEAgent;
  import moise.oe.RolePlayer;
  import moise.oe.SchemeInstance;
  import moise.os.OS;
  import moise.os.fs.FS;
  import moise.os.fs.Goal;
  import moise.os.fs.Mission;
  import moise.os.fs.Scheme;
  import moise.os.ns.NS;
  import moise.os.ss.Group;
  import moise.os.ss.Role;
  import moise.os.ss.SS;
  import moise.xml.DOMUtils;
  
  /**
   *
   * @author  jomi
   */
  @SuppressWarnings("unchecked")
  public class SimOEFrame extends JFrame {
  
      private static final long serialVersionUID = 1L;
  
      SimOE         ag            = null;
      Object        currentObject = null; // which object is selected
  
      boolean       hasSim        = true;
  
      Stack<Object> history       = new Stack<Object>();
  
      TransformerFactory tFactory   = TransformerFactory.newInstance();
      Transformer osTransformer     = null;
      Transformer oeTransformer     = null;
      Transformer grSpecTransformer = null;
      Transformer schSpecTransformer = null;
      Transformer roleTransformer   = null;
      Transformer missionTransformer= null;
      Transformer goalTransformer   = null;
      Transformer ssTransformer     = null;
      Transformer fsTransformer     = null;
      Transformer dsTransformer     = null;
      Transformer agTransformer     = null;
      Transformer grTransformer     = null;
      Transformer schTransformer    = null;
      Transformer briefOE           = null;
  
      public SimOEFrame(SimOE ag) {
          this(ag, true);
      }
  
      public SimOEFrame(SimOE ag, boolean addSim) {
          super("Moise ("+ag.getName()+")");
          this.ag = ag;
          this.hasSim = addSim;
  
          // to load .xsl from the moise.jar
          tFactory.setURIResolver(new URIResolver() {
              public Source resolve(String href, String base) throws TransformerException {
                  return getXSL(href);
              }
          });
  
          initComponents(addSim);
      }
  
      public void centerScreen() {
          try {
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              setBounds(0, 0, 800, (int)(screenSize.height * 0.7)); //(int)(screenSize.width * 0.8)
              setLocation(screenSize.width / 2 - this.getWidth() / 2, screenSize.height / 2 - this.getHeight() / 2);
  
              EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      setVisible(true);
                  }
              });
          } catch (Exception e) {
              System.out.println("problem in setVisible!");
              e.printStackTrace();
          }
      }
  
      public void showOS() {
          try {
              currentObject = ag.getCurrentOS();
              pushHistory(currentObject);
              showCurrentObject();
          } catch (Exception e) {        }
      }
  
      protected void uptadeOSComps() {
          try {
              OSTreeModel osTreeModel = new OSTreeModel(osTree);
              osTreeModel.setOS( ag.getCurrentOS());
              osTree.setModel( osTreeModel.getModel() );
  
              Group grs = ag.getCurrentOS().getSS().getRootGrSpec();
              if (grs != null) {
                  mySetModelPreserPosSelectedIndex(grSpecsInGrCreation, grs.getAllSubGroupsTree());
              }
              mySetModelPreserPosSelectedIndex(schSpecInSchStart, ag.getCurrentOS().getFS().getSchemes());
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void showOE() {
          try {
              currentObject = ag.getCurrentOE();
              if (currentObject != null) {
                  pushHistory(currentObject);
                  showCurrentObject();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      protected void uptadeOEComps() {
          OE oe = ag.getCurrentOE();
          if (oe == null) return;
  
          try {
              OETreeModel oeTreeModel = new OETreeModel(oeTree);
              oeTreeModel.setOE( oe );
              oeTree.setModel( oeTreeModel.getModel() );
              if (hasSim) {
                  // rebuild the list of current groups
                  List allWithRoot = new ArrayList(oe.getAllSubGroupsTree());
                  allWithRoot.add("root");
  
                  mySetModelPreserPosSelectedIndex(grCreatedGroups, allWithRoot);
  
                  mySetModelPreserPosSelectedIndex(grInstancesInGroup, oe.getAllSubGroupsTree());
  
                  updateAgentRoleComponents();
  
                  // rebuild the list of current SCHs
                  mySetModelPreserPosSelectedIndex(schInstancesInMission, oe.getSchemes());
  
                  mySetModelPreserPosSelectedIndex(schInstancesInGoal, oe.getSchemes());
                  updateSchemeGoalsCB();
  
                  mySetModelPreserPosSelectedIndex(schInstanceInSchFinish, oe.getSchemes());
  
                  mySetModelPreserPosSelectedIndex(schInstanceInAbort, oe.getSchemes());
  
                  mySetModelPreserPosSelectedIndex(schInstanceInRespGr, oe.getSchemes());
                  updateSchemeRespGoals();
  
  
                  // missions
                  updateAgentMissionComponents();
  
                  // agNames
                  mySetModelPreserPosSelectedIndex(agNamesInRole, oe.getAgents());
  
                  mySetModelPreserPosSelectedIndex(agNamesInMission, oe.getAgents());
  
                  mySetModelPreserPosSelectedIndex(agNamesInRemoveAg, oe.getAgents());
  
                  mySetModelPreserPosSelectedIndex(agNamesInGoalInstance, oe.getAgents());
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  
      private void mySetModelPreserPosSelectedIndex(JComboBox cb, Collection c) {
          mySetModelPreserPosSelectedIndex(cb, new Vector(c));
      }
  
      private void mySetModelPreserPosSelectedIndex(JComboBox cb, Vector v) {
          int oldSize = cb.getModel().getSize();
          int oldPos = cb.getSelectedIndex();
          Collections.sort(v);
          cb.setModel(new DefaultComboBoxModel( v ));
          if (v.size() == oldSize) {
              cb.setSelectedIndex(oldPos);
          }
      }
  
      /**
       * set the combo box Goals for the selected scheme
       */
      protected void updateSchemeGoalsCB() {
          SchemeInstance sch = (SchemeInstance)schInstancesInGoal.getSelectedItem();
          if (sch != null) {
              mySetModelPreserPosSelectedIndex(goalInstanceInGoal, sch.getGoals());
              updateGoalsComponents();
          } else {
              setArgValue.setEnabled(false);
              setGoalState.setEnabled(false);
          }
      }
  
      /**
       * update the goal interface components
       */
      protected void updateGoalsComponents() {
          setArgValue.setEnabled(false);
          setGoalState.setEnabled(false);
          GoalInstance gi = (GoalInstance)goalInstanceInGoal.getSelectedItem();
          if (gi != null) {
              setGoalState.setEnabled(true);
  
              if (gi.getSpec().hasArguments()) {
                  Collection<String> args = gi.getSpec().getArguments().keySet();
                  if (args != null) {
                      mySetModelPreserPosSelectedIndex(goalArgListInGoal, args);
                      setArgValue.setEnabled(true);
                  }
                  updateGoalArgValue();
              }
  
              goalStateInGoal.setSelectedIndex(0);
              if (gi.isSatisfied()) {
                  goalStateInGoal.setSelectedIndex(1);
              }
              if (!gi.isEnabled()) {
                  goalStateInGoal.setSelectedIndex(2);
              }
          }
      }
  
      /**
       * update the goal argument value
       */
      protected void updateGoalArgValue() {
          GoalInstance gi = (GoalInstance)goalInstanceInGoal.getSelectedItem();
          String   selArg = (String)goalArgListInGoal.getSelectedItem();
          Object   argVal = gi.getArgumentValue(selArg);
          if (argVal == null) {
              argValueInGoal.setText("");
          } else {
              argValueInGoal.setText(argVal.toString());
          }
      }
  
      /**
       * set the combo box responsible groups for the selected scheme
       */
      protected void updateSchemeRespGoals() {
          addRespGr.setEnabled(false);
          remRespGr.setEnabled(false);
          SchemeInstance sch = (SchemeInstance)schInstanceInRespGr.getSelectedItem();
          if (sch != null) {
              Vector grs = new Vector(ag.getCurrentOE().getAllSubGroupsTree());
              grs.removeAll(sch.getResponsibleGroups());
              mySetModelPreserPosSelectedIndex( grInRespGr, grs);
              mySetModelPreserPosSelectedIndex( schGrsInRespGr, sch.getResponsibleGroups());
              if (grs.size() > 0) {
                  addRespGr.setEnabled(true);
              }
              if (sch.getResponsibleGroups().size() > 0) {
                  remRespGr.setEnabled(true);
              }
          }
      }
  
      /**
       * update the agent role interface components
       */
      protected void updateAgentRoleComponents() {
          Collection agRoles;
          Collection agGrs;
          if (adoptORgiveUpRole.getSelectedIndex() == 1) { // give up a role
              agRoles = new HashSet();
              agGrs   = new HashSet();
              OEAgent selAg = (OEAgent)agNamesInRole.getSelectedItem();
              if (selAg != null) {
                  Iterator i = selAg.getRoles().iterator();
                  while (i.hasNext()) {
                      RolePlayer rp = (RolePlayer)i.next();
                      agRoles.add( rp.getRole() );
                      agGrs.add( rp.getGroup() );
                  }
              }
          } else { // adopt role
              agRoles = ag.getCurrentOS().getSS().getRolesDef();
              agGrs   = ag.getCurrentOE().getAllSubGroupsTree();
          }
          mySetModelPreserPosSelectedIndex(agRolesInRole, agRoles);
          mySetModelPreserPosSelectedIndex(grInRole, agGrs);
          if (agRoles.size() == 0 || agGrs.size() == 0) {
              agAdoptRole.setEnabled(false);
          } else {
              agAdoptRole.setEnabled(true);
          }
      }
  
      /**
       * update the agent mission interface components
       */
      protected void updateAgentMissionComponents() {
          Collection agMissions;
          Collection agSchs;
          if (commitORuncommit.getSelectedIndex() == 1) { // give up a mission
              agMissions = new HashSet();
              agSchs     = new HashSet();
              OEAgent selAg = (OEAgent)agNamesInMission.getSelectedItem();
              if (selAg != null) {
                  Iterator i = selAg.getMissions().iterator();
                  while (i.hasNext()) {
                      MissionPlayer mp = (MissionPlayer)i.next();
                      agMissions.add( mp.getMission() );
                      agSchs.add( mp.getScheme() );
                  }
              }
          } else { // adopt mission
              agMissions = ag.getCurrentOS().getFS().getAllMissions();
              agSchs     = ag.getCurrentOE().getSchemes();
          }
          mySetModelPreserPosSelectedIndex(missionInMission, agMissions);
          mySetModelPreserPosSelectedIndex(schInstancesInMission, agSchs);
          if (agMissions.size() == 0 || agSchs.size() == 0) {
              okMission.setEnabled(false);
          } else {
              okMission.setEnabled(true);
          }
      }
  
  
      public void show(String s) {
          currentObject = s;
          pushHistory(currentObject);
          showCurrentObject();
      }
  
  
  
      protected Object getCurrentObject() {
          return currentObject;
      }
      protected void setCurrentObject(Object o ) {
          currentObject = o;
      }
  
      @SuppressWarnings("unused")
      protected void showCurrentObject() {
          if (currentObject == null) {
              textArea.setText("");
              return;
          }
          String type = showAs.getSelectedItem().toString();
          if (type.startsWith("brief"))
              type = "html";
  
          textArea.setContentType("text/"+type);
  
          try {
              // try String
              String s = (String) currentObject;
              if (s.equals("SS")) {
                  currentObject = ag.getCurrentOS().getSS();
                  showCurrentObject();
              } else if (s.equals("FS")) {
                  currentObject = ag.getCurrentOS().getFS();
                  showCurrentObject();
              } else if (s.equals("NS")) {
                  currentObject = ag.getCurrentOS().getNS();
                  showCurrentObject();
              } else if (s.equals("Roles") || s.equals("Groups") || s.equals("Missions") || s.equals("Goals & plans") || s.equals("Agents") || s.equals("Schemes")) {
                  return;
              } else {
                  textArea.setText(s);
              }
              return;
          } catch (Exception e) {}
  
          StringWriter so = new StringWriter();
  
          boolean isHtml = showAs.getSelectedItem().equals("html") || showAs.getSelectedItem().equals("brief-html");
  
          try {
              // try OE
              OE oe = (OE) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( ag.getOExml() );
              } else if (showAs.getSelectedItem().equals("html")) {
                  try {
                      getOETransformer().transform(new DOMSource( ag.getOE_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              } else if (showAs.getSelectedItem().equals("brief-html")) {
                  try {
                      getBriefOE().transform(new DOMSource( ag.getOE_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try agent
              OEAgent oeAg = (OEAgent) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(oeAg) );
              } else if (isHtml) {
                  try {
                      getAgTransformer().setParameter("agentId", oeAg.getId());
                      getAgTransformer().transform(new DOMSource( ag.getOE_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try scheme instance
              SchemeInstance sch = (SchemeInstance)currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(sch));
              } else if (isHtml) {
                  try {
                      getSCHTransformer().setParameter("schId", sch.getId());
                      getSCHTransformer().transform(new DOMSource( ag.getOE_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try group instance
              GroupInstance gr = (GroupInstance) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(gr));
              } else if (isHtml) {
                  try {
                      getGrTransformer().setParameter("groupId", gr.getId());
                      getGrTransformer().transform(new DOMSource( ag.getOE_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try OS
              OS os = (OS) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( ag.getOSxml() );
              } else if (isHtml) {
                  try {
                      getOSTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));//new StreamResult(so)); //new FileOutputStream("xxx.html")));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              //
              // try Role
              //
              Role role = (Role) currentObject;
              try {
                  String Rolexml = DOMUtils.roleDetails(role); //SSGenerateXML.generateFullRoleDescription(role, "");
                  if (showAs.getSelectedItem().equals("xml")) {
                      textArea.setText(Rolexml);
                  } else {
                      getRoleTransformer().setParameter("roleId", role.getId());
                      getRoleTransformer().transform(new StreamSource(new StringReader(Rolexml)), new StreamResult(so));
                  }
              } catch (Exception e) {
                  textArea.setText("Error="+e);
                  e.printStackTrace();
                  return;
              }
          } catch (Exception e) {}
  
          try {
              //
              // try Mission
              //
              Mission mission = (Mission) currentObject;
              try {
                  if (showAs.getSelectedItem().equals("xml")) {
                      textArea.setText( DOMUtils.dom2txt(mission));
                  } else {
                      try {
                          getMissionTransformer().setParameter("missionId", mission.getId());
                          getMissionTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                      } catch (Exception e) {
                          textArea.setText("Error="+e);
                          e.printStackTrace();
                          return;
                      }
                  }
              } catch (Exception e) {
                  textArea.setText("Error="+e);
                  e.printStackTrace();
                  return;
              }
          } catch (Exception e) {}
  
          try {
              //
              // try Goal
              //
              Goal goal = (Goal) currentObject;
              try {
                  if (showAs.getSelectedItem().equals("xml")) {
                      textArea.setText( DOMUtils.dom2txt(goal));
                  } else {
                      try {
                          getGoalTransformer().setParameter("goalId", goal.getId());
                          getGoalTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                      } catch (Exception e) {
                          textArea.setText("Error="+e);
                          e.printStackTrace();
                          return;
                      }
                  }
              } catch (Exception e) {
                  textArea.setText("Error="+e);
                  e.printStackTrace();
                  return;
              }
          } catch (Exception e) {}
  
          try {
              // try Group spec
              Group gr = (Group) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(gr));
  
              } else {
                  try {
                      getGrSpecTransformer().setParameter("grSpecId", gr.getId());
                      getGrSpecTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try scheme spec
              Scheme sch = (Scheme) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(sch));
  
              } else {
                  try {
                      getSchSpecTransformer().setParameter("schemeSpecId", sch.getId());
                      getSchSpecTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try SS
              SS ss = (SS) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(ss));
  
              } else {
                  try {
                      getSSTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try FS
              FS fs = (FS) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(fs));
  
              } else {
                  try {
                      getFSTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
          try {
              // try DS
              NS ds = (NS) currentObject;
              if (showAs.getSelectedItem().equals("xml")) {
                  textArea.setText( DOMUtils.dom2txt(ds));
  
              } else {
                  try {
                      getDSTransformer().transform(new DOMSource( ag.getOS_DOM()), new StreamResult(so));
                  } catch (Exception e) {
                      textArea.setText("Error="+e);
                      e.printStackTrace();
                      return;
                  }
              }
          } catch (Exception e) {}
  
  
          try {
              if (showAs.getSelectedItem().equals("html") || showAs.getSelectedItem().equals("brief-html")) {
                  textArea.setText(so.toString());
              }
              textArea.setCaretPosition(1);
          } catch (Exception e) {}
      }
  
  
      private StreamSource getXSL(String href) {
          try {
              if (!href.endsWith(".xsl")) {
                  href += ".xsl";
              }
              return new StreamSource(
                  SimOEFrame.class.getResource("/xml/"+href).openStream());
          } catch (Exception e) {
              System.err.println("Could not open "+href+" in XML resources!");
              return null;
          }
      }
  
      private Transformer getOSTransformer() throws TransformerConfigurationException {
          if (osTransformer == null) {
              osTransformer = tFactory.newTransformer(getXSL("os"));
          }
          return osTransformer;
      }
  
      private Transformer getGrSpecTransformer() throws TransformerConfigurationException {
          if (grSpecTransformer == null) {
              grSpecTransformer = tFactory.newTransformer(getXSL("groupSpec"));
          }
          return grSpecTransformer;
      }
  
      private Transformer getSchSpecTransformer() throws TransformerConfigurationException {
          if (schSpecTransformer == null) {
              schSpecTransformer = tFactory.newTransformer(getXSL("schemeSpec"));
          }
          return schSpecTransformer;
      }
  
      private Transformer getRoleTransformer() throws TransformerConfigurationException {
          if (roleTransformer == null) {
              roleTransformer   = tFactory.newTransformer(getXSL("role"));
          }
          return roleTransformer;
      }
  
      private Transformer getMissionTransformer() throws TransformerConfigurationException {
          if (missionTransformer == null) {
              missionTransformer   = tFactory.newTransformer(getXSL("mission"));
          }
          return missionTransformer;
      }
  
      private Transformer getGoalTransformer() throws TransformerConfigurationException {
          if (goalTransformer == null) {
  