public void saveAs() {
    MostRecentFileDialog mrfd = new MostRecentFileDialog("org.concord.otviewer.saveotml");
    mrfd.setFilenameFilter("otml");

    if (currentUserFile != null) {
      mrfd.setCurrentDirectory(currentUserFile.getParentFile());
      mrfd.setSelectedFile(currentUserFile);
    }

    int retval = mrfd.showSaveDialog(getDialogParent());

    File file = null;
    if (retval == MostRecentFileDialog.APPROVE_OPTION) {
      file = mrfd.getSelectedFile();

      String fileName = file.getPath();
      currentUserFile = file;

      if (!fileName.toLowerCase().endsWith(".otml")) {
        currentUserFile = new File(currentUserFile.getAbsolutePath() + ".otml");
      }

      try {
        ExporterJDOM.export(currentUserFile, userDataDB.getRoot(), userDataDB);
        userDataDB.setDirty(false);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  public void newLayer() throws Exception {
    // need to make a brand new stateDB
    userDataDB = new XMLDatabase();
    otrunk.addDatabase(userDataDB);

    // System.out.println("otrunk: " + otrunk + " userDatabase: " +
    // userDataDB);
    OTObjectService objService = otrunk.createObjectService(userDataDB);

    OTStateRoot stateRoot = objService.createObject(OTStateRoot.class);
    userDataDB.setRoot(stateRoot.getGlobalId());
    stateRoot.setFormatVersionString("1.0");

    OTUserObject userObject = objService.createObject(OTUserObject.class);

    OTID userId = userObject.getUserId();

    OTObjectMap userStateMapMap = stateRoot.getUserMap();

    refMap = objService.createObject(OTReferenceMap.class);
    userStateMapMap.putObject(userId.toExternalForm(), refMap);
    refMap.setUser(userObject);

    otrunk.registerReferenceMap(refMap);

    userDataDB.setDirty(false);

    currentUserFile = null;
  }
  public void save() {
    if (currentUserFile == null || !currentUserFile.exists()) {
      saveAs();
      return;
    }

    if (currentUserFile.exists()) {
      try {
        ExporterJDOM.export(currentUserFile, userDataDB.getRoot(), userDataDB);
        userDataDB.setDirty(false);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  protected void load(URL url) throws Exception {
    currentUserUrl = url;
    userDataDB = new XMLDatabase(currentUserUrl);
    userDataDB.loadObjects();

    load(userDataDB);
  }
  protected void load(XMLDatabase db) throws Exception {
    initUserDb(db);

    // Set the username
    OTUser user = refMap.getUser();
    OTUserObject aUser = (OTUserObject) user;
    if (userName != null) {
      aUser.setName(userName);
    }

    otrunk.registerReferenceMap(getReferenceMap());

    // just loading the database shouldn't cause it to have changes that need
    // saving.  If the user name was set that would be change.
    userDataDB.setDirty(false);
  }
  protected void initUserDb(XMLDatabase userDataDb) throws Exception {
    this.userDataDB = userDataDb;
    otrunk.addDatabase(userDataDB);

    OTObjectService objService = otrunk.createObjectService(userDataDb);

    OTDataObject rootDO = userDataDb.getRoot();

    OTStateRoot stateRoot = (OTStateRoot) objService.getOTObject(rootDO.getGlobalId());

    OTObjectMap userMap = stateRoot.getUserMap();

    // find the user from this database.
    // this currently is the first user in the userMap
    Vector<String> keys = userMap.getObjectKeys();
    refMap = (OTReferenceMap) userMap.getObject(keys.get(0));
  }
 public void ignoreCurrentUnsavedChanges() {
   userDataDB.setDirty(false);
 }
 public boolean hasUnsavedChanges() {
   if (userDataDB == null) {
     return false;
   }
   return userDataDB.isDirty();
 }