Пример #1
0
  public static Ooaofooa getInstance(String id, boolean forceLoad) {
    Ooaofooa modelRoot = Ooaofooa.getInstance(id);

    if (forceLoad && isFileBasedID(id) && !modelRoot.isLoaded()) {
      String projectName = getProjectNameFromModelRootId(id);
      String fileName = getDomainFileNameFromModelRootId(id);
      IProject[] project_set = CorePlugin.getWorkspace().getRoot().getProjects();
      IFile content = null;
      for (int i = 0; i < project_set.length; ++i) {
        if (XtUMLNature.hasNature(project_set[i])) {
          if (project_set[i].getName().equals(projectName)) {
            IResource mdl_folder = project_set[i].findMember(MODELS_DIRNAME);
            IResource[] mdl_content = null;
            try {
              mdl_content = ((IFolder) mdl_folder).members();
            } catch (Exception e) {
              CorePlugin.logError("Problem accessing workspace ", e);
            }
            if (mdl_content != null) {
              for (int j = 0; j < mdl_content.length; ++j) {
                if (mdl_content[j] instanceof IFile) {
                  IFile selected = (IFile) mdl_content[j];
                  if (selected.getName().equals(fileName)) {
                    content = selected;
                    break;
                  }
                }
              }
            }
          }
        }
      }
      if (content != null) {
        modelRoot = getInstance(content, true);
        if (!modelRoot.loadedOk()) {
          modelRoot.delete();
          modelRoot = null;
        }
      }
    }

    return modelRoot;
  }
Пример #2
0
  /**
   * Loads (and returns) the instance of this class from the given file to which it previously had
   * been persisted.
   */
  private static Ooaofooa loadInstanceFromFile(IFile file) {
    // find the system-model that corresponds to the project
    // containing the given file
    final String projectName = file.getProject().getName();
    final SystemModel_c system =
        SystemModel_c.SystemModelInstances(
            Ooaofooa.getDefaultInstance(),
            new ClassQueryInterface_c() {
              public boolean evaluate(Object candidate) {
                SystemModel_c selected = (SystemModel_c) candidate;
                return selected.getName().equals(projectName);
              }
            })[0];

    // open an input-stream on the given file
    InputStream inStream = null;
    try {
      inStream = new FileInputStream(file.getLocation().toString());
    } catch (FileNotFoundException e) {
      CorePlugin.logError(
          "Internal error: failed to open " + file.getLocation().toString(), e); // $$NON-NLS-1$$
      return null;
    }

    // detm the instance's ID from the file's path
    final String id = createModelRootId(file.getProject(), file.getName(), false);

    // the enclosed could take a bit of time, so put up a busy indicator
    final InputStream finalInStream = inStream;
    BusyIndicator.showWhile(
        Display.getCurrent(),
        new Runnable() {
          public void run() {
            // load the instance from the stream and have it be associated
            // with the system-model found above
            String result =
                CorePlugin.getDefault()
                    .doLoad(system, id, finalInStream, new NullProgressMonitor(), false, false);
            if (!result.equals("")) {
              Ooaofooa modelRoot = getInstance(id, false);
              modelRoot.loadErrorMessage = result;
            }
          }
        });

    // close the input-stream from which the model was loaded
    try {
      inStream.close();
    } catch (IOException e) {
      CorePlugin.logError("Error closing stream after import", e);
    }

    // make sure the domain and file name are in sync
    Ooaofooa modelRoot = getInstance(id, false);
    if (modelRoot.loadedOk()) {
      Domain_c dom = Domain_c.DomainInstance(modelRoot);
      String fn = file.getName();
      String domName = fn.substring(0, fn.length() - MODELS_EXT.length() - 1);

      ModelRoot.disableChangeNotification();
      try {
        dom.setName(domName);
      } finally {
        ModelRoot.enableChangeNotification();
      }
    }

    // return the loaded instance
    return modelRoot;
  }