com.intellij.util.messages.Topic Java Examples

The following examples show how to use com.intellij.util.messages.Topic. 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: MessageBusConnectionImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
<L> void subscribe(@Nonnull Topic<L> topic, @Nonnull List<Object> handlers) {
  boolean notifyBusAboutTopic = false;
  synchronized (myPendingMessages) {
    Object currentHandler = mySubscriptions.get(topic);
    if (currentHandler == null) {
      mySubscriptions = mySubscriptions.plus(topic, handlers);
      notifyBusAboutTopic = true;
    }
    else if (currentHandler instanceof List<?>) {
      //noinspection unchecked
      ((List<Object>)currentHandler).addAll(handlers);
    }
    else {
      List<Object> newList = new ArrayList<>(handlers.size() + 1);
      newList.add(currentHandler);
      newList.addAll(handlers);
      mySubscriptions = mySubscriptions.plus(topic, newList);
    }
  }

  if (notifyBusAboutTopic) {
    myBus.notifyOnSubscription(this, topic);
  }
}
 
Example #2
Source File: MessageBusConnectionImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public <L> void subscribe(@Nonnull Topic<L> topic, @Nonnull L handler) {
  boolean notifyBusAboutTopic = false;
  synchronized (myPendingMessages) {
    Object currentHandler = mySubscriptions.get(topic);
    if (currentHandler == null) {
      mySubscriptions = mySubscriptions.plus(topic, handler);
      notifyBusAboutTopic = true;
    }
    else if (currentHandler instanceof List<?>) {
      //noinspection unchecked
      ((List<L>)currentHandler).add(handler);
    }
    else {
      List<Object> newList = new ArrayList<>();
      newList.add(currentHandler);
      newList.add(handler);
      mySubscriptions = mySubscriptions.plus(topic, newList);
    }
  }

  if (notifyBusAboutTopic) {
    myBus.notifyOnSubscription(this, topic);
  }
}
 
Example #3
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void calcSubscribers(@Nonnull Topic<?> topic, @Nonnull List<? super MessageBusConnectionImpl> result) {
  final List<MessageBusConnectionImpl> topicSubscribers = mySubscribers.get(topic);
  if (topicSubscribers != null) {
    result.addAll(topicSubscribers);
  }

  Topic.BroadcastDirection direction = topic.getBroadcastDirection();

  if (direction == Topic.BroadcastDirection.TO_CHILDREN) {
    for (MessageBusImpl childBus : myChildBuses) {
      childBus.calcSubscribers(topic, result);
    }
  }

  if (direction == Topic.BroadcastDirection.TO_PARENT && myParentBus != null) {
    myParentBus.calcSubscribers(topic, result);
  }
}
 
Example #4
Source File: SingleThreadedMessageBus.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@NotNull
@Override
public <L> L syncPublisher(@NotNull final Topic<L> topic) {
    L publisher = (L) publishers.get(topic);
    if (publisher == null) {
        Class<L> listenerClass = topic.getListenerClass();
        publisher = (L) Proxy.newProxyInstance(listenerClass.getClassLoader(), new Class<?>[] {
                listenerClass
        }, (proxy, method, args) -> {
            for (InnerMessageBusConnection connection : connections) {
                connection.invoke(topic, method, args);
            }
            return null;
        });
        publishers.put(topic, publisher);
    }
    return publisher;
}
 
Example #5
Source File: IdeaMocksExtension.java    From GitToolBox with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) {
  IdeaMocksImpl ideaMocks = new IdeaMocksImpl();
  Project project = mock(Project.class);
  MessageBus messageBus = mock(MessageBus.class);
  when(project.getMessageBus()).thenReturn(messageBus);
  when(messageBus.syncPublisher(any(Topic.class))).thenAnswer(invocation -> {
    Topic topic = invocation.getArgument(0);
    Class<?> listenerClass = topic.getListenerClass();
    if (ideaMocks.hasMockListener(listenerClass)) {
      return ideaMocks.getMockListener(listenerClass);
    } else {
      return ideaMocks.mockListener(listenerClass);
    }
  });
  Store store = context.getStore(NS);
  ParameterHolder holder = ParameterHolder.getHolder(store);
  holder.register(Project.class, Suppliers.ofInstance(project));
  holder.register(MessageBus.class, Suppliers.ofInstance(messageBus));
  holder.register(IdeaMocks.class, Suppliers.ofInstance(ideaMocks));
}
 
Example #6
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public <L> L syncPublisher(@Nonnull Topic<L> topic) {
  checkNotDisposed();
  @SuppressWarnings("unchecked") L publisher = (L)myPublishers.get(topic);
  if (publisher != null) {
    return publisher;
  }

  Class<L> listenerClass = topic.getListenerClass();

  if (myTopicClassToListenerClass.isEmpty()) {
    Object newInstance = Proxy.newProxyInstance(listenerClass.getClassLoader(), new Class[]{listenerClass}, createTopicHandler(topic));
    Object prev = myPublishers.putIfAbsent(topic, newInstance);
    //noinspection unchecked
    return (L)(prev == null ? newInstance : prev);
  }
  else {
    // remove is atomic operation, so, even if topic concurrently created and our topic instance will be not used, still, listeners will be added,
    // but problem is that if another topic will be returned earlier, then these listeners will not get fired event
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (topic) {
      return subscribeLazyListeners(topic, listenerClass);
    }
  }
}
 
Example #7
Source File: HaskellToolsConfigurable.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
Tool(Project project, String command, ToolKey key, TextFieldWithBrowseButton pathField,
     RawCommandLineEditor flagsField, JButton autoFindButton, JTextField versionField, String versionParam,
     @Nullable Topic<SettingsChangeNotifier> topic) {
    this.project = project;
    this.command = command;
    this.key = key;
    this.pathField = pathField;
    this.flagsField = flagsField;
    this.versionField = versionField;
    this.versionParam = versionParam;
    this.autoFindButton = autoFindButton;
    this.topic = topic;
    this.publisher = topic == null ? null : project.getMessageBus().syncPublisher(topic);

    this.propertyFields = Arrays.asList(
            new PropertyField(key.pathKey, pathField),
            new PropertyField(key.flagsKey, flagsField));

    GuiUtil.addFolderListener(pathField, command);
    GuiUtil.addApplyPathAction(autoFindButton, pathField, command);
    updateVersion();
}
 
Example #8
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
void notifyOnSubscription(@Nonnull MessageBusConnectionImpl connection, @Nonnull Topic<?> topic) {
  checkNotDisposed();
  List<MessageBusConnectionImpl> topicSubscribers = mySubscribers.get(topic);
  if (topicSubscribers == null) {
    topicSubscribers = ContainerUtil.createLockFreeCopyOnWriteList();
    topicSubscribers = ConcurrencyUtil.cacheOrGet(mySubscribers, topic, topicSubscribers);
  }

  topicSubscribers.add(connection);

  myRootBus.clearSubscriberCache();
}
 
Example #9
Source File: MessageBusUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T> void invokeLaterIfNeededOnSyncPublisher(final Project project, final Topic<T> topic, final Consumer<T> listener) {
  final Application application = ApplicationManager.getApplication();
  final Runnable runnable = createPublisherRunnable(project, topic, listener);
  if (application.isDispatchThread()) {
    runnable.run();
  } else {
    application.invokeLater(runnable);
  }
}
 
Example #10
Source File: MessageBusUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static <T> Runnable createPublisherRunnable(final Project project, final Topic<T> topic, final Consumer<T> listener) {
  return new Runnable() {
    @Override
    public void run() {
      if (project.isDisposed()) throw new ProcessCanceledException();
      listener.consume(project.getMessageBus().syncPublisher(topic));
    }
  };
}
 
Example #11
Source File: MessageBusUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T> void runOnSyncPublisher(final Project project, final Topic<T> topic, final Consumer<T> listener) {
  final Application application = ApplicationManager.getApplication();
  final Runnable runnable = createPublisherRunnable(project, topic, listener);
  if (application.isDispatchThread()) {
    runnable.run();
  } else {
    application.runReadAction(runnable);
  }
}
 
Example #12
Source File: BackgroundTaskUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps {@link MessageBus#syncPublisher(Topic)} in a dispose check,
 * and throws a {@link ProcessCanceledException} if the application is disposed,
 * instead of throwing an assertion which would happen otherwise.
 *
 * @see #syncPublisher(Project, Topic)
 */
@Nonnull
public static <L> L syncPublisher(@Nonnull Topic<L> topic) throws ProcessCanceledException {
  ThrowableComputable<L,RuntimeException> action = () -> {
    if (ApplicationManager.getApplication().isDisposed()) throw new ProcessCanceledException();
    return ApplicationManager.getApplication().getMessageBus().syncPublisher(topic);
  };
  return AccessRule.read(action);
}
 
Example #13
Source File: BackgroundTaskUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps {@link MessageBus#syncPublisher(Topic)} in a dispose check,
 * and throws a {@link ProcessCanceledException} if the project is disposed,
 * instead of throwing an assertion which would happen otherwise.
 *
 * @see #syncPublisher(Topic)
 */
@Nonnull
public static <L> L syncPublisher(@Nonnull Project project, @Nonnull Topic<L> topic) throws ProcessCanceledException {
  ThrowableComputable<L, RuntimeException> action = () -> {
    if (project.isDisposed()) throw new ProcessCanceledException();
    return project.getMessageBus().syncPublisher(topic);
  };
  return AccessRule.read(action);
}
 
Example #14
Source File: MessageBusConnectionImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public <L> void subscribe(@Nonnull Topic<L> topic) throws IllegalStateException {
  MessageHandler defaultHandler = myDefaultHandler;
  if (defaultHandler == null) {
    throw new IllegalStateException("Connection must have default handler installed prior to any anonymous subscriptions. " + "Target topic: " + topic);
  }
  if (topic.getListenerClass().isInstance(defaultHandler)) {
    throw new IllegalStateException(
            "Can't subscribe to the topic '" + topic + "'. Default handler has incompatible type - expected: '" + topic.getListenerClass() + "', actual: '" + defaultHandler.getClass() + "'");
  }

  //noinspection unchecked
  subscribe(topic, (L)defaultHandler);
}
 
Example #15
Source File: MessageBusConnectionImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
boolean containsMessage(@Nonnull Topic<?> topic) {
  Queue<Message> pendingMessages = myPendingMessages.get();
  if (pendingMessages.isEmpty()) return false;

  for (Message message : pendingMessages) {
    if (message.getTopic() == topic) {
      return true;
    }
  }
  return false;
}
 
Example #16
Source File: SingleThreadedMessageBus.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Override
public <L> void subscribe(@NotNull Topic<L> topic)
        throws IllegalStateException {
    if (defaultHandler == null) {
        throw new IllegalStateException();
    }
    handlers.putIfAbsent(topic, new ArrayList<>());
    handlers.get(topic).add(defaultHandler);
}
 
Example #17
Source File: SingleThreadedMessageBus.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
void invoke(Topic<?> topic, Method m, Object[] args) {
    List<Object> listeners = handlers.get(topic);
    if (listeners != null) {
        for (Object listener : listeners) {
            try {
                m.invoke(listener, args);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new IllegalStateException(e);
            }
        }
    }
}
 
Example #18
Source File: SingleThreadedMessageBus.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Deprecated
@NotNull
// v183 - removed this method.
//@Override
public <L> L asyncPublisher(@NotNull Topic<L> topic) {
    return syncPublisher(topic);
}
 
Example #19
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private <L> InvocationHandler createTopicHandler(@Nonnull Topic<L> topic) {
  return (proxy, method, args) -> {
    if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
      return EventDispatcher.handleObjectMethod(proxy, args, method.getName());
    }
    sendMessage(new Message(topic, method, args));
    return NA;
  };
}
 
Example #20
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasUndeliveredEvents(@Nonnull Topic<?> topic) {
  if (myDisposed) return false;
  if (!isDispatchingAnything()) return false;

  for (MessageBusConnectionImpl connection : getTopicSubscribers(topic)) {
    if (connection.containsMessage(topic)) {
      return true;
    }
  }
  return false;
}
 
Example #21
Source File: AbstractCacheMessage.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
static <E extends AbstractCacheUpdateEvent<E>> void abstractAddListener(
        @NotNull MessageBusClient.ApplicationClient client, @NotNull Topic<TopicListener<E>> topic,
        @NotNull String cacheId,
        @NotNull AbstractCacheUpdateEvent.Visitor<E> visitor) {
    VisitEventListener<E> wrapped = new VisitEventListener<>(cacheId, visitor);
    Class<? extends TopicListener<E>> cz = getTopicListenerClass();

    addTopicListener(client, topic, wrapped, cz, cacheId);
}
 
Example #22
Source File: ProjectMessage.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@NotNull
protected static <L> L getListener(@NotNull Project project, @NotNull Topic<L> topic, @NotNull L defaultListener) {
    L listener = getListener(project, topic);
    if (listener == null) {
        listener = defaultListener;
    }
    return listener;
}
 
Example #23
Source File: ProjectMessage.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Nullable
protected static <L> L getListener(@NotNull Project project, @NotNull Topic<L> topic) {
    if (canSendMessage(project)) {
        return project.getMessageBus().syncPublisher(topic);
    }
    return null;
}
 
Example #24
Source File: MessageBusImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private List<MessageBusConnectionImpl> getTopicSubscribers(@Nonnull Topic<?> topic) {
  List<MessageBusConnectionImpl> topicSubscribers = mySubscriberCache.get(topic);
  if (topicSubscribers == null) {
    topicSubscribers = new ArrayList<>();
    calcSubscribers(topic, topicSubscribers);
    mySubscriberCache.put(topic, topicSubscribers);
    myRootBus.myClearedSubscribersCache = false;
  }
  return topicSubscribers;
}
 
Example #25
Source File: ApplicationMessage.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@NotNull
protected static <L> L getListener(@NotNull Topic<L> topic, @NotNull L defaultListener) {
    L listener = getListener(topic);
    if (listener == null) {
        listener = defaultListener;
    }
    return listener;
}
 
Example #26
Source File: ApplicationMessage.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Nullable
protected static <L> L getListener(@NotNull Topic<L> topic) {
    if (canSendMessage()) {
        return ApplicationManager.getApplication().getMessageBus().syncPublisher(topic);
    }
    return null;
}
 
Example #27
Source File: MessageListenerList.java    From consulo with Apache License 2.0 4 votes vote down vote up
public MessageListenerList(@Nonnull MessageBus messageBus, @Nonnull Topic<T> topic) {
  myTopic = topic;
  myMessageBus = messageBus;
}
 
Example #28
Source File: Message.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public Topic getTopic() {
  return myTopic;
}
 
Example #29
Source File: Message.java    From consulo with Apache License 2.0 4 votes vote down vote up
public Message(@Nonnull Topic topic, @Nonnull Method listenerMethod, Object[] args) {
  myTopic = topic;
  listenerMethod.setAccessible(true);
  myListenerMethod = listenerMethod;
  myArgs = args;
}
 
Example #30
Source File: AbstractExternalSystemSettings.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected AbstractExternalSystemSettings(@Nonnull Topic<L> topic, @Nonnull Project project) {
  myChangesTopic = topic;
  myProject = project;
  Disposer.register(project, this);
}