com.intellij.util.Producer Java Examples

The following examples show how to use com.intellij.util.Producer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PasteReferenceProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String getCopiedFqn(final DataContext context) {
  Producer<Transferable> producer = context.getData(PasteAction.TRANSFERABLE_PROVIDER);

  if (producer != null) {
    Transferable transferable = producer.produce();
    if (transferable != null) {
      try {
        return (String)transferable.getTransferData(CopyReferenceAction.ourFlavor);
      }
      catch (Exception ignored) { }
    }
    return null;
  }

  return CopyPasteManager.getInstance().getContents(CopyReferenceAction.ourFlavor);
}
 
Example #2
Source File: OnePixelDivider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void paint(Graphics g) {
  final Rectangle bounds = g.getClipBounds();
  if (mySplitter instanceof OnePixelSplitter) {
    final Producer<Insets> blindZone = ((OnePixelSplitter)mySplitter).getBlindZone();
    if (blindZone != null) {
      final Insets insets = blindZone.produce();
      if (insets != null) {
        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= insets.left + insets.right;
        bounds.height -= insets.top + insets.bottom;
        g.setColor(getBackground());
        g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        return;
      }
    }
  }
  super.paint(g);
}
 
Example #3
Source File: RemoteExternalSystemTaskManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void executeTasks(@Nonnull final ExternalSystemTaskId id,
                         @Nonnull final List<String> taskNames,
                         @Nonnull final String projectPath,
                         @Nullable final S settings,
                         @Nonnull final List<String> vmOptions,
                         @Nonnull final List<String> scriptParameters,
                         @Nullable final String debuggerSetup) throws RemoteException, ExternalSystemException
{
  execute(id, new Producer<Object>() {
    @Nullable
    @Override
    public Object produce() {
      myDelegate.executeTasks(
              id, taskNames, projectPath, settings, vmOptions, scriptParameters, debuggerSetup, getNotificationListener());
      return null;
    }
  });
}
 
Example #4
Source File: RemoteExternalSystemProjectResolverImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
@Override
public DataNode<ProjectData> resolveProjectInfo(@Nonnull final ExternalSystemTaskId id,
                                                @Nonnull final String projectPath,
                                                final boolean isPreviewMode,
                                                ExternalSystemExecutionSettings settings)
  throws ExternalSystemException, IllegalArgumentException, IllegalStateException
{
  return execute(id, new Producer<DataNode<ProjectData>>() {
    @javax.annotation.Nullable
    @Override
    public DataNode<ProjectData> produce() {
      return myDelegate.resolveProjectInfo(id, projectPath, isPreviewMode, getSettings(), getNotificationListener());
    }
  });
}
 
Example #5
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isContentChangeOnly() {
  return accessChanges(new Producer<Boolean>() {
    @Override
    public Boolean produce() {
      return myChanges.size() == 1 && getFirstChange() instanceof ContentChange;
    }
  });
}
 
Example #6
Source File: ZipUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void unzipWithProgressSynchronously(
  @Nullable Project project,
  @Nonnull String progressTitle,
  @Nonnull final File zipArchive,
  @Nonnull final File extractToDir) throws GeneratorException
{
  Outcome<Boolean> outcome = DownloadUtil.provideDataWithProgressSynchronously(
    project, progressTitle, "Unpacking ...",
    new Callable<Boolean>() {
      @Override
      public Boolean call() throws IOException {
        ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
        ZipInputStream stream = new ZipInputStream(new FileInputStream(zipArchive));
        unzip(progress, extractToDir, stream, null, null);
        return true;
      }
    },
    new Producer<Boolean>() {
      @Override
      public Boolean produce() {
        return false;
      }
    }
  );
  Boolean result = outcome.get();
  if (result == null) {
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    Exception e = outcome.getException();
    if (e != null) {
      throw new GeneratorException("Unpacking failed, downloaded archive is broken");
    }
    throw new GeneratorException("Unpacking was cancelled");
  }
}
 
Example #7
Source File: AbstractRemoteExternalSystemService.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected <T> T execute(@Nonnull ExternalSystemTaskId id, @Nonnull Producer<T> task) {
  Set<ExternalSystemTaskId> tasks = myTasksInProgress.get(id.getType());
  if (tasks == null) {
    myTasksInProgress.putIfAbsent(id.getType(), new HashSet<ExternalSystemTaskId>());
    tasks = myTasksInProgress.get(id.getType());
  }
  tasks.add(id);
  try {
    return task.produce();
  }
  finally {
    tasks.remove(id);
  }
}
 
Example #8
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void accessChanges(@Nonnull final Runnable func) {
  accessChanges(new Producer<Object>() {
    @Override
    public Object produce() {
      func.run();
      return null;
    }
  });
}
 
Example #9
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
private <T> T accessChanges(@Nonnull Producer<T> func) {
  if (isLocked) {
    //noinspection ConstantConditions
    return func.produce();
  }

  synchronized (myChanges) {
    //noinspection ConstantConditions
    return func.produce();
  }
}
 
Example #10
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public String toString() {
  return accessChanges(new Producer<String>() {
    @Override
    public String produce() {
      return myChanges.toString();
    }
  });
}
 
Example #11
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public List<String> getAffectedPaths() {
  return accessChanges(new Producer<List<String>>() {
    @Override
    public List<String> produce() {
      List<String> result = new SmartList<String>();
      for (Change each : myChanges) {
        if (each instanceof StructuralChange) {
          result.add(((StructuralChange)each).getPath());
        }
      }
      return result;
    }
  });
}
 
Example #12
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Change getLastChange() {
  return accessChanges(new Producer<Change>() {
    @Override
    public Change produce() {
      return myChanges.get(myChanges.size() - 1);
    }
  });
}
 
Example #13
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Change getFirstChange() {
  return accessChanges(new Producer<Change>() {
    @Override
    public Change produce() {
      return myChanges.get(0);
    }
  });
}
 
Example #14
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isLabelOnly() {
  return accessChanges(new Producer<Boolean>() {
    @Override
    public Boolean produce() {
      return myChanges.size() == 1 && getFirstChange() instanceof PutLabelChange;
    }
  });
}
 
Example #15
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public List<Content> getContentsToPurge() {
  return accessChanges(new Producer<List<Content>>() {
    @Override
    public List<Content> produce() {
      List<Content> result = new ArrayList<Content>();
      for (Change c : myChanges) {
        result.addAll(c.getContentsToPurge());
      }
      return result;
    }
  });
}
 
Example #16
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean affectsPath(final String paths) {
  return accessChanges(new Producer<Boolean>() {
    @Override
    public Boolean produce() {
      for (Change c : myChanges) {
        if (c.affectsPath(paths)) return true;
      }
      return false;
    }
  });
}
 
Example #17
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isCreationalFor(final String path) {
  return accessChanges(new Producer<Boolean>() {
    @Override
    public Boolean produce() {
      for (Change c : myChanges) {
        if (c.isCreationalFor(path)) return true;
      }
      return false;
    }
  });
}
 
Example #18
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isEmpty() {
  return accessChanges(new Producer<Boolean>() {
    @Override
    public Boolean produce() {
      return myChanges.isEmpty();
    }
  });
}
 
Example #19
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public List<Change> getChanges() {
  return accessChanges(new Producer<List<Change>>() {
    @Override
    public List<Change> produce() {
      if (isLocked) return myChanges;
      return Collections.unmodifiableList(new ArrayList<Change>(myChanges));
    }
  });
}
 
Example #20
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public int getLabelColor() {
  return accessChanges(new Producer<Integer>() {
    @Override
    public Integer produce() {
      for (Change each : myChanges) {
        if (each instanceof PutSystemLabelChange) {
          return ((PutSystemLabelChange)each).getColor();
        }
      }
      return -1;
    }
  });
}
 
Example #21
Source File: ChangeSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
public String getLabel() {
  return accessChanges(new Producer<String>() {
    @Override
    public String produce() {
      for (Change each : myChanges) {
        if (each instanceof PutLabelChange) {
          return ((PutLabelChange)each).getName();
        }
      }
      return null;
    }
  });
}
 
Example #22
Source File: EditorModificationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static Transferable getTransferable(Producer<Transferable> producer) {
  Transferable content = null;
  if (producer != null) {
    content = producer.produce();
  }
  else {
    CopyPasteManager manager = CopyPasteManager.getInstance();
    if (manager.areDataFlavorsAvailable(DataFlavor.stringFlavor)) {
      content = manager.getContents();
    }
  }
  return content;
}
 
Example #23
Source File: EditorModificationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Transferable getContentsToPasteToEditor(@Nullable Producer<Transferable> producer) {
  if (producer == null) {
    CopyPasteManager manager = CopyPasteManager.getInstance();
    return manager.areDataFlavorsAvailable(DataFlavor.stringFlavor) ? manager.getContents() : null;
  }
  else {
    return producer.produce();
  }
}
 
Example #24
Source File: NotificationTypeReference.java    From ok-gradle with Apache License 2.0 4 votes vote down vote up
NotificationTypeReference(@NotNull Class<T> clazz, @NotNull Producer<T> constructor) {
  myClazz = clazz;
  myConstructor = constructor;
}
 
Example #25
Source File: NotificationTypeReference.java    From ok-gradle with Apache License 2.0 4 votes vote down vote up
@NotNull
public Producer<T> getConstructor() {
  return myConstructor;
}
 
Example #26
Source File: PasteHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final Editor editor, final DataContext dataContext, @Nullable final Producer<Transferable> producer) {
  final Transferable transferable = EditorModificationUtil.getContentsToPasteToEditor(producer);
  if (transferable == null) return;

  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;

  final Document document = editor.getDocument();
  if (!FileDocumentManager.getInstance().requestWriting(document, dataContext.getData(CommonDataKeys.PROJECT))) {
    return;
  }

  DataContext context = new DataContext() {
    @Override
    public Object getData(@NonNls Key dataId) {
      return PasteAction.TRANSFERABLE_PROVIDER == dataId ? new Producer<Transferable>() {
        @Nullable
        @Override
        public Transferable produce() {
          return transferable;
        }
      } : dataContext.getData(dataId);
    }
  };

  final Project project = editor.getProject();
  if (project == null || editor.isColumnMode() || editor.getCaretModel().getCaretCount() > 1) {
    if (myOriginalHandler != null) {
      myOriginalHandler.execute(editor, null, context);
    }
    return;
  }

  final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
  if (file == null) {
    if (myOriginalHandler != null) {
      myOriginalHandler.execute(editor, null, context);
    }
    return;
  }

  DumbService.getInstance(project).setAlternativeResolveEnabled(true);
  document.startGuardedBlockChecking();
  try {
    for (PasteProvider provider : Extensions.getExtensions(EP_NAME)) {
      if (provider.isPasteEnabled(context)) {
        provider.performPaste(context);
        return;
      }
    }
    doPaste(editor, project, file, document, transferable);
  }
  catch (ReadOnlyFragmentModificationException e) {
    EditorActionManager.getInstance().getReadonlyFragmentModificationHandler(document).handle(e);
  }
  finally {
    document.stopGuardedBlockChecking();
    DumbService.getInstance(project).setAlternativeResolveEnabled(false);
  }
}
 
Example #27
Source File: DownloadUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static <V> Outcome<V> provideDataWithProgressSynchronously(
        @Nullable Project project,
        @Nonnull String progressTitle,
        @Nonnull final String actionShortDescription,
        @Nonnull final Callable<V> supplier,
        @Nullable Producer<Boolean> tryAgainProvider)
{
  int attemptNumber = 1;
  while (true) {
    final Ref<V> dataRef = Ref.create(null);
    final Ref<Exception> innerExceptionRef = Ref.create(null);
    boolean completed = ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
      @Override
      public void run() {
        ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        indicator.setText(actionShortDescription);
        try {
          V data = supplier.call();
          dataRef.set(data);
        }
        catch (Exception ex) {
          innerExceptionRef.set(ex);
        }
      }
    }, progressTitle, true, project);
    if (!completed) {
      return Outcome.createAsCancelled();
    }
    Exception latestInnerException = innerExceptionRef.get();
    if (latestInnerException == null) {
      return Outcome.createNormal(dataRef.get());
    }
    LOG.warn("[attempt#" + attemptNumber + "] Can not '" + actionShortDescription + "'", latestInnerException);
    boolean onceMore = false;
    if (tryAgainProvider != null) {
      onceMore = Boolean.TRUE.equals(tryAgainProvider.produce());
    }
    if (!onceMore) {
      return Outcome.createAsException(latestInnerException);
    }
    attemptNumber++;
  }
}
 
Example #28
Source File: BasePasteHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected Transferable getContentsToPaste(Editor editor, DataContext dataContext) {
  Producer<Transferable> producer = dataContext.getData(PasteAction.TRANSFERABLE_PROVIDER);
  return EditorModificationUtil.getContentsToPasteToEditor(producer);
}
 
Example #29
Source File: PasteImageHandler.java    From pasteimages with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext, Producer<Transferable> producer) {
    Caret caret = editor.getCaretModel().getPrimaryCaret();
    doExecute(editor, caret, dataContext);
}
 
Example #30
Source File: OnePixelSplitter.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setBlindZone(Producer<Insets> blindZone) {
  myDivider.setOpaque(blindZone == null);
  myBlindZone = blindZone;
}