Java Code Examples for com.intellij.openapi.application.ApplicationManager#getApplication()

The following examples show how to use com.intellij.openapi.application.ApplicationManager#getApplication() . 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: AbstractTreeBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void runBackgroundLoading(@Nonnull final Runnable runnable) {
  if (isDisposed()) return;

  final Application app = ApplicationManager.getApplication();
  if (app != null) {
    app.runReadAction(new TreeRunnable("AbstractTreeBuilder.runBackgroundLoading") {
      @Override
      public void perform() {
        runnable.run();
      }
    });
  }
  else {
    runnable.run();
  }
}
 
Example 2
Source File: KPXStartupNotification.java    From IntelliJ-Key-Promoter-X with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
  public void runActivity(@NotNull Project project) {
    if (ApplicationManager.getApplication().isUnitTestMode()) return;

    final Application application = ApplicationManager.getApplication();
    final KeyPromoterSettings settings = ServiceManager.getService(KeyPromoterSettings.class);
    final String installedVersion = settings.getInstalledVersion();

    final IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId("Key Promoter X"));
    if (installedVersion != null && plugin != null) {
      final int compare = VersionComparatorUtil.compare(installedVersion, plugin.getVersion());
//      if (true) { // TODO: Don't forget to remove that! For proofreading.
      if (compare < 0) {
        application.invokeLater(() -> KPXStartupDialog.showStartupDialog(project));
        settings.setInstalledVersion(plugin.getVersion());
      }
    }
  }
 
Example 3
Source File: DeprecatedVirtualFileSystem.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void startEventPropagation() {
  Application application = ApplicationManager.getApplication();
  if (application == null) {
    return;
  }

  application.getMessageBus().connect().subscribe(
          VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(myEventDispatcher.getMulticaster(), this));
}
 
Example 4
Source File: Messages.java    From consulo with Apache License 2.0 5 votes vote down vote up
@TestOnly
public static TestInputDialog setTestInputDialog(TestInputDialog newValue) {
  Application application = ApplicationManager.getApplication();
  if (application != null) {
    LOG.assertTrue(application.isUnitTestMode(), "This method is available for tests only");
  }
  TestInputDialog oldValue = ourTestInputImplementation;
  ourTestInputImplementation = newValue;
  return oldValue;
}
 
Example 5
Source File: HttpConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void runAboveAll(@Nonnull final Runnable runnable) {
  ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
  if (progressIndicator != null && progressIndicator.isModal()) {
    WaitForProgressToShow.runOrInvokeAndWaitAboveProgress(runnable);
  }
  else {
    Application app = ApplicationManager.getApplication();
    app.invokeAndWait(runnable, ModalityState.any());
  }
}
 
Example 6
Source File: CommonShortcuts.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CustomShortcutSet shortcutsById(String actionId) {
  Application application = ApplicationManager.getApplication();
  KeymapManager keymapManager = application == null ? null : application.getComponent(KeymapManager.class);
  if (keymapManager == null) {
    return new CustomShortcutSet(Shortcut.EMPTY_ARRAY);
  }
  return new CustomShortcutSet(keymapManager.getActiveKeymap().getShortcuts(actionId));
}
 
Example 7
Source File: PlatformUltraLiteTestFixture.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setUp() {
  final Application application = ApplicationManager.getApplication();
  if (application == null) {
    myAppDisposable = Disposable.newDisposable();
    ApplicationManager.setApplication(new MockApplication(myAppDisposable), myAppDisposable);
  }
}
 
Example 8
Source File: ModuleCompilerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void sortModules(final Project project, final List<Module> modules) {
  final Application application = ApplicationManager.getApplication();
  Runnable sort = new Runnable() {
    public void run() {
      Comparator<Module> comparator = ModuleManager.getInstance(project).moduleDependencyComparator();
      Collections.sort(modules, comparator);
    }
  };
  if (application.isDispatchThread()) {
    sort.run();
  }
  else {
    application.runReadAction(sort);
  }
}
 
Example 9
Source File: ProgressIndicatorUtils.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure the current EDT activity finishes in case it requires many write actions, with each being delayed a bit
 * by background thread read action (until its first checkCanceled call). Shouldn't be called from under read action.
 */
public static void yieldToPendingWriteActions() {
  Application application = ApplicationManager.getApplication();
  if (application.isReadAccessAllowed()) {
    throw new IllegalStateException("Mustn't be called from within read action");
  }
  if (application.isDispatchThread()) {
    throw new IllegalStateException("Mustn't be called from EDT");
  }
  Semaphore semaphore = new Semaphore(1);
  application.invokeLater(semaphore::up, ModalityState.any());
  awaitWithCheckCanceled(semaphore, ProgressIndicatorProvider.getGlobalProgressIndicator());
}
 
Example 10
Source File: Responses.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void addServer(HttpResponse response) {
  if (SERVER_HEADER_VALUE == null) {
    Application app = ApplicationManager.getApplication();
    if (app != null && !app.isDisposed()) {
      SERVER_HEADER_VALUE = ApplicationNamesInfo.getInstance().getFullProductName();
    }
  }
  if (SERVER_HEADER_VALUE != null) {
    response.headers().set("Server", SERVER_HEADER_VALUE);
  }
}
 
Example 11
Source File: Classloaders.java    From EclipseCodeFormatter with Apache License 2.0 4 votes vote down vote up
private static boolean isUnitTest() {
	return ApplicationManager.getApplication() == null || ApplicationManager.getApplication().isUnitTestMode();
}
 
Example 12
Source File: CppSupportSettings.java    From CppTools with Apache License 2.0 4 votes vote down vote up
public static CppSupportSettings getInstance() {
  final Application application = ApplicationManager.getApplication();
  if (application.isUnitTestMode()) return instance;
  return application.getComponent(CppSupportSettings.class);
}
 
Example 13
Source File: AbstractVcsHelperImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void run(@Nonnull final ProgressIndicator indicator) {
  final AsynchConsumer<List<CommittedChangeList>> appender = myDlg.getAppender();
  final BufferedListConsumer<CommittedChangeList> bufferedListConsumer = new BufferedListConsumer<>(10, appender, -1);

  final Application application = ApplicationManager.getApplication();
  try {
    myProvider.loadCommittedChanges(mySettings, myLocation, 0, new AsynchConsumer<CommittedChangeList>() {
      @Override
      public void consume(CommittedChangeList committedChangeList) {
        myRevisionsReturned = true;
        bufferedListConsumer.consumeOne(committedChangeList);
        if (myCanceled) {
          indicator.cancel();
        }
      }

      @Override
      public void finished() {
        bufferedListConsumer.flush();
        appender.finished();

        if (! myRevisionsReturned) {
          application.invokeLater(new Runnable() {
            @Override
            public void run() {
              myDlg.close(-1);
            }
          }, ModalityState.stateForComponent(myDlg.getWindow()));
        }
      }
    });
  }
  catch (VcsException e) {
    myExceptions.add(e);
    application.invokeLater(new Runnable() {
      @Override
      public void run() {
        myDlg.close(-1);
      }
    }, ModalityState.stateForComponent(myDlg.getWindow()));
  }
}
 
Example 14
Source File: ShowFeatureUsageStatisticsDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected JComponent createCenterPanel() {
  Splitter splitter = new Splitter(true);
  splitter.setShowDividerControls(true);

  ProductivityFeaturesRegistry registry = ProductivityFeaturesRegistry.getInstance();
  ArrayList<FeatureDescriptor> features = new ArrayList<FeatureDescriptor>();
  for (String id : registry.getFeatureIds()) {
    features.add(registry.getFeatureDescriptor(id));
  }
  final TableView table = new TableView<FeatureDescriptor>(new ListTableModel<FeatureDescriptor>(COLUMNS, features, 0));
  new TableViewSpeedSearch<FeatureDescriptor>(table) {
    @Override
    protected String getItemText(@Nonnull FeatureDescriptor element) {
      return element.getDisplayName();
    }
  };

  JPanel controlsPanel = new JPanel(new VerticalFlowLayout());


  Application app = ApplicationManager.getApplication();
  long uptime = System.currentTimeMillis() - app.getStartTime();
  long idleTime = app.getIdleTime();

  final String uptimeS = FeatureStatisticsBundle.message("feature.statistics.application.uptime",
                                                         ApplicationNamesInfo.getInstance().getFullProductName(),
                                                         DateFormatUtil.formatDuration(uptime));

  final String idleTimeS = FeatureStatisticsBundle.message("feature.statistics.application.idle.time",
                                                           DateFormatUtil.formatDuration(idleTime));

  String labelText = uptimeS + ", " + idleTimeS;
  CompletionStatistics stats = ((FeatureUsageTrackerImpl)FeatureUsageTracker.getInstance()).getCompletionStatistics();
  if (stats.dayCount > 0 && stats.sparedCharacters > 0) {
    String total = formatCharacterCount(stats.sparedCharacters, true);
    String perDay = formatCharacterCount(stats.sparedCharacters / stats.dayCount, false);
    labelText += "<br>Code completion has saved you from typing at least " + total + " since " + DateFormatUtil.formatDate(stats.startDate) +
                 " (~" + perDay + " per working day)";
  }

  CumulativeStatistics fstats = ((FeatureUsageTrackerImpl)FeatureUsageTracker.getInstance()).getFixesStats();
  if (fstats.dayCount > 0 && fstats.invocations > 0) {
    labelText +=
      "<br>Quick fixes have saved you from " + fstats.invocations + " possible bugs since " + DateFormatUtil.formatDate(fstats.startDate) +
      " (~" + fstats.invocations / fstats.dayCount + " per working day)";
  }

  controlsPanel.add(new JLabel("<html><body>" + labelText + "</body></html>"), BorderLayout.NORTH);

  JPanel topPanel = new JPanel(new BorderLayout());
  topPanel.add(controlsPanel, BorderLayout.NORTH);
  topPanel.add(ScrollPaneFactory.createScrollPane(table), BorderLayout.CENTER);

  splitter.setFirstComponent(topPanel);

  final JEditorPane browser = new JEditorPane(UIUtil.HTML_MIME, "");
  browser.setEditable(false);
  splitter.setSecondComponent(ScrollPaneFactory.createScrollPane(browser));

  table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
      Collection selection = table.getSelection();
      try {
        if (selection.isEmpty()) {
          browser.read(new StringReader(""), null);
        }
        else {
          FeatureDescriptor feature = (FeatureDescriptor)selection.iterator().next();
          TipUIUtil.openTipInBrowser(feature.getTipFileName(), browser, feature.getProvider());
        }
      }
      catch (IOException ex) {
        LOG.info(ex);
      }
    }
  });

  return splitter;
}
 
Example 15
Source File: WebApplication.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
static WebApplication getInstance() {
  return (WebApplication)ApplicationManager.getApplication();
}
 
Example 16
Source File: PlatformLiteFixture.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static MockApplicationEx getApplication() {
  return (MockApplicationEx)ApplicationManager.getApplication();
}
 
Example 17
Source File: ChooseLibrariesDialogBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected RootDescriptor(final Project project) {
  super(project, null, ApplicationManager.getApplication());
}
 
Example 18
Source File: PendingEventDispatcher.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void assertDispatchThread() {
  Application application = ApplicationManager.getApplication();
  if (myAssertDispatchThread && !application.isUnitTestMode()) {
    application.assertIsDispatchThread();
  }
}
 
Example 19
Source File: NativeFileWatcherImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Subclasses should override this method if they want to use custom logic to disable their file watcher.
 */
protected boolean isDisabled() {
  if (Boolean.getBoolean(PROPERTY_WATCHER_DISABLED)) return true;
  Application app = ApplicationManager.getApplication();
  return app.isCommandLine() || app.isUnitTestMode();
}
 
Example 20
Source File: NonProjectFileWritingAccessProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static Application getApp() {
  return ApplicationManager.getApplication();
}