Пример #1
0
  @Override
  public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    sourcePage.init(site, input);

    setPartNameForInput(input);

    IResource resource = ResourceUtil.getResource(input);
    if (resource != null) {
      resource.getWorkspace().addResourceChangeListener(this);
    }

    final IDocumentProvider docProvider = sourcePage.getDocumentProvider();
    IDocument document = docProvider.getDocument(input);
    try {
      model.loadFrom(new IDocumentWrapper(document));
      model.setProjectFile(Project.BNDFILE.equals(input.getName()));

      if (resource != null) {
        model.setBndResource(resource.getLocation().toFile());
      }
      // model.addPropertyChangeListener(modelListener);
    } catch (IOException e) {
      throw new PartInitException("Error reading editor input.", e);
    }

    // Ensure the field values are updated if the file content is replaced
    docProvider.addElementStateListener(
        new IElementStateListener() {
          public void elementMoved(Object originalElement, Object movedElement) {}

          public void elementDirtyStateChanged(Object element, boolean isDirty) {}

          public void elementDeleted(Object element) {}

          public void elementContentReplaced(Object element) {
            try {
              model.loadFrom(new IDocumentWrapper(docProvider.getDocument(element)));
            } catch (IOException e) {
              logger.logError("Error loading model from document.", e);
            }
          }

          public void elementContentAboutToBeReplaced(Object element) {}
        });
  }
Пример #2
0
 private void reallySave(IProgressMonitor monitor) {
   // Actually save, via the source editor
   try {
     boolean saveLocked = this.saving.compareAndSet(false, true);
     if (!saveLocked) {
       logger.logError("Tried to save while already saving", null);
       return;
     }
     sourcePage.doSave(monitor);
     updatePages();
   } finally {
     this.saving.set(false);
   }
 }
Пример #3
0
  public void resourceChanged(IResourceChangeEvent event) {
    IResource myResource = ResourceUtil.getResource(getEditorInput());

    IResourceDelta delta = event.getDelta();
    if (delta == null) return;
    IPath fullPath = myResource.getFullPath();
    delta = delta.findMember(fullPath);
    if (delta == null) return;

    // Delegate to any interested pages
    for (Object page : pages) {
      if (page instanceof IResourceChangeListener) {
        ((IResourceChangeListener) page).resourceChanged(event);
      }
    }

    // Close editor if file removed or switch to new location if file moved
    if (delta.getKind() == IResourceDelta.REMOVED) {
      if ((delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(delta.getMovedToPath());
        final FileEditorInput newInput = new FileEditorInput(file);

        setInput(newInput);
        Display display = getEditorSite().getShell().getDisplay();
        if (display != null) {
          SWTConcurrencyUtil.execForDisplay(
              display,
              true,
              new Runnable() {
                public void run() {
                  setPartNameForInput(newInput);
                  sourcePage.setInput(newInput);
                }
              });
        }
      } else {
        close(false);
      }

    }
    // File content updated externally => reload all pages
    else if ((delta.getKind() & IResourceDelta.CHANGED) > 0
        && (delta.getFlags() & IResourceDelta.CONTENT) > 0) {
      if (!saving.get()) {
        final IDocumentProvider docProvider = sourcePage.getDocumentProvider();
        final IDocument document = docProvider.getDocument(getEditorInput());
        SWTConcurrencyUtil.execForControl(
            getEditorSite().getShell(),
            true,
            new Runnable() {
              public void run() {
                try {
                  model.loadFrom(new IDocumentWrapper(document));
                  updatePages();
                } catch (IOException e) {
                  logger.logError("Failed to reload document", e);
                }
              }
            });
      }
    }
  }
Пример #4
0
  @Override
  public void doSave(IProgressMonitor monitor) {
    final Shell shell = getEditorSite().getShell();

    // Commit dirty pages
    if (sourcePage.isActive() && sourcePage.isDirty()) {
      sourcePage.commit(true);
    } else {
      commitPages(true);
      sourcePage.refresh();
    }

    ResolveMode resolveMode = getResolveMode();

    // If auto resolve, then resolve and save in background thread.
    if (resolveMode == ResolveMode.auto && !PlatformUI.getWorkbench().isClosing()) {
      final IFile file = ResourceUtil.getFile(getEditorInput());
      if (file == null) {
        MessageDialog.openError(
            shell,
            "Resolution Error",
            "Unable to run OBR resolution because the file is not in the workspace. NB.: the file will still be saved.");
        reallySave(monitor);
        return;
      }

      // Create resolver job and pre-validate
      final ResolveJob job = new ResolveJob(model);
      IStatus validation = job.validateBeforeRun();
      if (!validation.isOK()) {
        String message = "Unable to run the OBR resolver. NB.: the file will still be saved.";
        ErrorDialog.openError(
            shell,
            "Resolution Validation Problem",
            message,
            validation,
            IStatus.ERROR | IStatus.WARNING);
        reallySave(monitor);
        return;
      }

      // Add operation to perform at the end of resolution (i.e. display
      // results and actually save the file)
      final UIJob completionJob =
          new UIJob(shell.getDisplay(), "Display Resolution Results") {
            @Override
            public IStatus runInUIThread(IProgressMonitor monitor) {
              ResolutionResult result = job.getResolutionResult();
              ResolutionWizard wizard = new ResolutionWizard(model, file, result);
              if (result.getOutcome() != ResolutionResult.Outcome.Resolved
                  || !result.getResolve().getOptionalResources().isEmpty()) {
                WizardDialog dialog = new WizardDialog(shell, wizard);
                if (dialog.open() != Window.OK) {
                  if (!wizard.performFinish()) {
                    MessageDialog.openError(
                        shell,
                        "Error",
                        "Unable to store resolution results into Run Bundles list.");
                  }
                }
              } else {
                if (!wizard.performFinish()) {
                  MessageDialog.openError(
                      shell, "Error", "Unable to store resolution results into Run Bundles list.");
                }
              }
              reallySave(monitor);
              return Status.OK_STATUS;
            }
          };
      job.addJobChangeListener(
          new JobChangeAdapter() {
            @Override
            public void done(IJobChangeEvent event) {
              completionJob.schedule();
            }
          });

      // Start job
      job.setUser(true);
      job.schedule();
    } else {
      // Not auto-resolving, just save
      reallySave(monitor);
    }
  }
Пример #5
0
  void updatePages() {
    List<String> requiredPageIds = new LinkedList<String>();

    // Need to know the file and project names.
    Pair<String, String> fileAndProject = getFileAndProject(getEditorInput());
    String path = fileAndProject.getFirst();
    String projectName = fileAndProject.getSecond();

    if (isMainWorkspaceConfig(path, projectName)) {
      requiredPageIds.add(WORKSPACE_PAGE);
    } else if (isExtWorkspaceConfig(path, projectName)) {
      requiredPageIds.add(WORKSPACE_EXT_PAGE);
      setTitleImage(buildFileImg);
    } else if (path.endsWith(LaunchConstants.EXT_BNDRUN)) {
      requiredPageIds.addAll(getPagesBndRun());
    } else {
      requiredPageIds.addAll(getPagesBnd(path));
    }
    requiredPageIds.add(SOURCE_PAGE);

    // Remove pages no longer required and remember the rest in a map
    int i = 0;
    Map<String, IFormPage> pageCache = new HashMap<String, IFormPage>(requiredPageIds.size());
    while (i < getPageCount()) {
      IFormPage current = (IFormPage) pages.get(i);
      if (!requiredPageIds.contains(current.getId())) removePage(i);
      else {
        pageCache.put(current.getId(), current);
        i++;
      }
    }

    // Cache new pages
    for (String pageId : requiredPageIds) {
      if (!pageCache.containsKey(pageId)) {
        IFormPage page =
            SOURCE_PAGE.equals(pageId)
                ? sourcePage
                : pageFactories.get(pageId).createPage(this, model, pageId);
        pageCache.put(pageId, page);
      }
    }

    // Add pages back in
    int requiredPointer = 0;
    int existingPointer = 0;

    while (requiredPointer < requiredPageIds.size()) {
      try {
        String requiredId = requiredPageIds.get(requiredPointer);
        if (existingPointer >= getPageCount()) {
          if (SOURCE_PAGE.equals(requiredId)) addPage(sourcePage, getEditorInput());
          else addPage(pageCache.get(requiredId));
        } else {
          IFormPage existingPage = (IFormPage) pages.get(existingPointer);
          if (!requiredId.equals(existingPage.getId())) {
            if (SOURCE_PAGE.equals(requiredId))
              addPage(existingPointer, sourcePage, getEditorInput());
            else addPage(existingPointer, pageCache.get(requiredId));
          }
        }
        existingPointer++;
      } catch (PartInitException e) {
        logger.logError("Error adding page(s) to the editor.", e);
      }
      requiredPointer++;
    }

    // Set the source page title
    setPageText(sourcePage.getIndex(), "Source");
  }