Beispiel #1
0
  /**
   * Sadly, we have to restore all pydev editors to make the icons correct.
   *
   * <p>See https://bugs.eclipse.org/bugs/show_bug.cgi?id=308740
   */
  private boolean doRestoreAllPydevEditorsWithDifferentIcons() {
    if (!PyTitlePreferencesPage.useCustomInitIcon()) {
      return true; // Ok, nothing custom!
    }

    final List<IEditorReference> editorReferences = getCurrentEditorReferences();
    if (editorReferences == null) {
      // couldn't be gotten.
      return false;
    }
    // Update images

    RunInUiThread.async(
        new Runnable() {

          public void run() {
            for (IEditorReference iEditorReference : editorReferences) {
              try {
                if (iEditorReference != null) {
                  IPath pathFromInput = getPathFromInput(iEditorReference.getEditorInput());
                  if (pathFromInput != null) {
                    String lastSegment = pathFromInput.lastSegment();
                    if (lastSegment != null
                        && (lastSegment.startsWith("__init__.")
                            || PyTitlePreferencesPage.isDjangoModuleToDecorate(lastSegment))) {
                      iEditorReference.getEditor(true); // restore it.
                    }
                  }
                }
              } catch (PartInitException e) {
                // ignore
              }

              // Note, removed the code below -- just restoring the editor is enough and the
              // only way to make it work for now. See:
              // https://bugs.eclipse.org/bugs/show_bug.cgi?id=308740

              //			try {
              //				IEditorInput input = iEditorReference.getEditorInput();
              //				IPath path = getPathFromInput(input);
              //				updateImage(null, iEditorReference, path);
              //			} catch (PartInitException e) {
              //				//ignore
              //			}
            }
          }
        });

    return true;
  }
Beispiel #2
0
  /**
   * Sets the image of the passed editor reference. Will try to restore the editor for doing that.
   *
   * <p>See https://bugs.eclipse.org/bugs/show_bug.cgi?id=308740
   */
  private void setEditorReferenceImage(final IEditorReference iEditorReference, final Image image) {
    RunInUiThread.async(
        new Runnable() {

          public void run() {
            try {
              IEditorPart editor = iEditorReference.getEditor(true);
              if (editor instanceof PyEdit) {
                ((PyEdit) editor).setEditorImage(image);
              }

            } catch (Throwable e) {
              PydevPlugin.log(e);
            }
          }
        });
  }
Beispiel #3
0
  /**
   * @return the current editor references or null if no editor references are available.
   *     <p>Note that this method may be slow as it will need UI access (which is asynchronously
   *     gotten)
   */
  private List<IEditorReference> getCurrentEditorReferences() {
    final List<IEditorReference[]> editorReferencesFound = new ArrayList<IEditorReference[]>();

    RunInUiThread.async(
        new Runnable() {

          public void run() {
            IEditorReference[] found = null;
            try {
              IWorkbenchWindow workbenchWindow =
                  PlatformUI.getWorkbench().getActiveWorkbenchWindow();
              if (workbenchWindow == null) {
                return;
              }
              IWorkbenchPage activePage = workbenchWindow.getActivePage();
              if (activePage == null) {
                return;
              }
              found = activePage.getEditorReferences();
            } finally {
              editorReferencesFound.add(found);
            }
          }
        });
    while (editorReferencesFound.size() == 0) {
      synchronized (this) {
        try {
          wait(10);
        } catch (InterruptedException e) {
          // ignore
        }
      }
    }
    IEditorReference[] editorReferences = editorReferencesFound.get(0);
    if (editorReferences == null) {
      return null;
    }
    ArrayList<IEditorReference> ret = new ArrayList<IEditorReference>();
    for (IEditorReference iEditorReference : editorReferences) {
      if (!PyEdit.EDITOR_ID.equals(iEditorReference.getId())) {
        continue; // only analyze Pydev editors
      }
      ret.add(iEditorReference);
    }
    return ret;
  }
Beispiel #4
0
  /**
   * Sets the title of the passed editor reference. Will try to restore the editor for doing that.
   *
   * <p>See https://bugs.eclipse.org/bugs/show_bug.cgi?id=308740
   */
  private void setEditorReferenceTitle(final List<IEditorReference> refs, final String title) {
    if (refs == null) {
      return;
    }
    final int size = refs.size();
    if (size == 1) {
      for (final IEditorReference ref : refs) {

        if (title.equals(ref.getTitle())) {
          // Nothing to do if it's already the same.
          return;
        }

        RunInUiThread.async(
            new Runnable() {

              public void run() {
                try {
                  IEditorPart editor = ref.getEditor(true);
                  if (editor instanceof PyEdit) {
                    ((PyEdit) editor).setEditorTitle(title);
                  }
                } catch (Throwable e) {
                  PydevPlugin.log(e);
                }
              }
            });
      }
    } else if (size > 1) {

      RunInUiThread.async(
          new Runnable() {

            public void run() {
              try {
                final String key = "PyEditTitleEditorNumberSet";
                final Set<Integer> used = new HashSet<Integer>();
                final ArrayList<PyEdit> toSet = new ArrayList<PyEdit>();

                for (final IEditorReference ref : refs) {
                  PyEdit editor = (PyEdit) ref.getEditor(true);
                  Integer curr = (Integer) editor.cache.get(key);
                  if (curr != null) {
                    used.add(curr);
                    ((PyEdit) editor).setEditorTitle(title + " #" + (curr + 1));
                  } else {
                    toSet.add(editor);
                  }
                }

                // Calculate the next integer available
                final ICallback0<Integer> next =
                    new ICallback0<Integer>() {
                      private int last = 0;

                      public Integer call() {
                        for (; last < Integer.MAX_VALUE; last++) {
                          if (used.contains(last)) {
                            continue;
                          }
                          used.add(last);
                          return last;
                        }
                        throw new AssertionError("Should not get here");
                      }
                    };

                // If it got here in toSet, it still must be set!
                for (PyEdit editor : toSet) {
                  Integer i = next.call();
                  ((PyEdit) editor).setEditorTitle(title + " #" + (i + 1));
                }
              } catch (Throwable e) {
                PydevPlugin.log(e);
              }
            }
          });
    }
  }