Java Code Examples for consulo.disposer.Disposer#register()

The following examples show how to use consulo.disposer.Disposer#register() . 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: FindUIHelper.java    From consulo with Apache License 2.0 6 votes vote down vote up
private FindUI getOrCreateUI() {
  if (myUI == null) {
    JComponent component;
    FindPopupPanel panel = new FindPopupPanel(this);
    component = panel;
    myUI = panel;

    registerAction("ReplaceInPath", true, component, myUI);
    registerAction("FindInPath", false, component, myUI);
    Disposer.register(myUI.getDisposable(), this);
  }
  else {
    IdeEventQueue.getInstance().flushDelayedKeyEvents();
  }
  return myUI;
}
 
Example 2
Source File: TextEditorComponent.java    From consulo with Apache License 2.0 6 votes vote down vote up
public TextEditorComponent(@Nonnull final Project project, @Nonnull final VirtualFile file, @Nonnull final DesktopTextEditorImpl textEditor) {
  super(new BorderLayout(), textEditor);

  myProject = project;
  myFile = file;
  myTextEditor = textEditor;

  myDocument = FileDocumentManager.getInstance().getDocument(myFile);
  LOG.assertTrue(myDocument != null);
  myDocument.addDocumentListener(new MyDocumentListener(), this);

  myEditor = createEditor();
  add(myEditor.getComponent(), BorderLayout.CENTER);
  myModified = isModifiedImpl();
  myValid = isEditorValidImpl();
  LOG.assertTrue(myValid);

  MyVirtualFileListener myVirtualFileListener = new MyVirtualFileListener();
  myFile.getFileSystem().addVirtualFileListener(myVirtualFileListener);
  Disposer.register(this, () -> myFile.getFileSystem().removeVirtualFileListener(myVirtualFileListener));
  MessageBusConnection myConnection = project.getMessageBus().connect(this);
  myConnection.subscribe(FileTypeManager.TOPIC, new MyFileTypeListener());

  myEditorHighlighterUpdater = new EditorHighlighterUpdater(myProject, this, (EditorEx)myEditor, myFile);
}
 
Example 3
Source File: CodeStyleAbstractPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected CodeStyleAbstractPanel(@Nullable Language defaultLanguage,
                                 @Nullable CodeStyleSettings currentSettings,
                                 @Nonnull CodeStyleSettings settings)
{
  Disposer.register(this, myDiffCalculator);
  myCurrentSettings = currentSettings;
  mySettings = settings;
  myDefaultLanguage = defaultLanguage;
  myEditor = createEditor();

  if (myEditor != null) {
    myUpdateAlarm.setActivationComponent(myEditor.getComponent());
  }
  myUserActivityWatcher.addUserActivityListener(new UserActivityListener() {
    @Override
    public void stateChanged() {
      somethingChanged();
    }
  });

  updatePreview(true);
}
 
Example 4
Source File: TemplateLanguageStructureViewBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected TreeBasedStructureViewBuilder createMainBuilder(@Nonnull PsiFile psi) {
  //noinspection deprecation
  StructureViewComposite.StructureViewDescriptor descriptor = createMainView(null, psi);
  if (descriptor == null) return null;
  return new TreeBasedStructureViewBuilder() {
    @Nonnull
    @Override
    public StructureViewModel createStructureViewModel(@Nullable Editor editor) {
      Disposer.register(descriptor.structureModel, descriptor.structureView);
      return descriptor.structureModel;
    }

    @Nonnull
    @Override
    public StructureView createStructureView(FileEditor fileEditor, @Nonnull Project project) {
      return descriptor.structureView;
    }
  };
}
 
Example 5
Source File: ScopeTreeViewPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
public ScopeTreeViewPanel(final Project project) {
  super(new BorderLayout());
  myUpdateQueue.setPassThrough(false);  // we don't want passthrough mode, even in unit tests
  myProject = project;
  initTree();

  add(ScrollPaneFactory.createScrollPane(myTree), BorderLayout.CENTER);
  myDependencyValidationManager = DependencyValidationManager.getInstance(myProject);

  final UiNotifyConnector uiNotifyConnector = new UiNotifyConnector(myTree, myUpdateQueue);
  Disposer.register(this, myUpdateQueue);
  Disposer.register(this, uiNotifyConnector);

  if (isTreeShowing()) {
    myUpdateQueue.showNotify();
  }
}
 
Example 6
Source File: MouseDragHelper.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void start() {
  if (myGlassPane != null) return;

  new UiNotifyConnector(myDragComponent, new Activatable() {
    @Override
    public void showNotify() {
      attach();
    }

    @Override
    public void hideNotify() {
      detach(true);
    }
  });

  Disposer.register(myParentDisposable, this::stop);
}
 
Example 7
Source File: XDebugSessionTab.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void setSession(@Nonnull XDebugSessionImpl session, @Nullable ExecutionEnvironment environment, @Nullable Image icon) {
  myEnvironment = environment;
  mySession = session;
  mySessionData = session.getSessionData();
  myConsole = session.getConsoleView();

  AnAction[] restartActions;
  List<AnAction> restartActionsList = session.getRestartActions();
  if (ContainerUtil.isEmpty(restartActionsList)) {
    restartActions = AnAction.EMPTY_ARRAY;
  }
  else {
    restartActions = restartActionsList.toArray(new AnAction[restartActionsList.size()]);
  }

  myRunContentDescriptor =
          new RunContentDescriptor(myConsole, session.getDebugProcess().getProcessHandler(), myUi.getComponent(), session.getSessionName(), icon,
                                   myRebuildWatchesRunnable, restartActions);
  Disposer.register(myRunContentDescriptor, this);
  Disposer.register(myProject, myRunContentDescriptor);
}
 
Example 8
Source File: ViewStructureAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static FileStructurePopup createPopup(@Nonnull Project project, @Nonnull FileEditor fileEditor) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();
  StructureViewBuilder builder = fileEditor.getStructureViewBuilder();
  if (builder == null) return null;
  StructureView structureView;
  StructureViewModel treeModel;
  if (builder instanceof TreeBasedStructureViewBuilder) {
    structureView = null;
    treeModel = ((TreeBasedStructureViewBuilder)builder).createStructureViewModel(EditorUtil.getEditorEx(fileEditor));
  }
  else {
    structureView = builder.createStructureView(fileEditor, project);
    treeModel = createStructureViewModel(project, fileEditor, structureView);
  }
  if (treeModel instanceof PlaceHolder) {
    //noinspection unchecked
    ((PlaceHolder)treeModel).setPlace(TreeStructureUtil.PLACE);
  }
  FileStructurePopup popup = new FileStructurePopup(project, fileEditor, treeModel);
  if (structureView != null) Disposer.register(popup, structureView);
  return popup;
}
 
Example 9
Source File: ExpirationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean tryRegisterDisposable(Disposable parent, Disposable child) {
  if (!isDisposed(parent) && !isDisposing(parent)) {
    try {
      Disposer.register(parent, child);
      return true;
    }
    catch (IncorrectOperationException e) { // Sorry but Disposer.register() is inherently thread-unsafe
    }
  }

  return false;
}
 
Example 10
Source File: ScopeViewPane.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public JComponent createComponent() {
  myViewPanel = new ScopeTreeViewPanel(myProject);
  Disposer.register(this, myViewPanel);
  myViewPanel.initListeners();
  myViewPanel.selectScope(NamedScopesHolder.getScope(myProject, getSubId()));
  myTree = myViewPanel.getTree();
  PopupHandler.installPopupHandler(myTree, IdeActions.GROUP_SCOPE_VIEW_POPUP, ActionPlaces.SCOPE_VIEW_POPUP);
  enableDnD();

  return myViewPanel.getPanel();
}
 
Example 11
Source File: ActionMacroManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void showBalloon() {
  if (myBalloon != null) {
    Disposer.dispose(myBalloon);
    return;
  }

  myBalloon = JBPopupFactory.getInstance().createBalloonBuilder(myBalloonComponent).setAnimationCycle(200).setCloseButtonEnabled(true).setHideOnAction(false).setHideOnClickOutside(false)
          .setHideOnFrameResize(false).setHideOnKeyOutside(false).setSmallVariant(true).setShadow(true).createBalloon();

  Disposer.register(myBalloon, new Disposable() {
    @Override
    public void dispose() {
      myBalloon = null;
    }
  });

  myBalloon.addListener(new JBPopupAdapter() {
    @Override
    public void onClosed(LightweightWindowEvent event) {
      if (myBalloon != null) {
        Disposer.dispose(myBalloon);
      }
    }
  });

  myBalloon.show(new PositionTracker<Balloon>(myIcon) {
    @Override
    public RelativePoint recalculateLocation(Balloon object) {
      return new RelativePoint(myIcon, new Point(myIcon.getSize().width / 2, 4));
    }
  }, Balloon.Position.above);
}
 
Example 12
Source File: BaseTestsOutputConsoleView.java    From consulo with Apache License 2.0 5 votes vote down vote up
public BaseTestsOutputConsoleView(final TestConsoleProperties properties, final AbstractTestProxy unboundOutputRoot) {
  myProperties = properties;

  myConsole = new TestsConsoleBuilderImpl(properties.getProject(),
                                          myProperties.getScope(),
                                          !properties.isEditable(),
                                          properties.isUsePredefinedMessageFilter()).getConsole();
  myPrinter = new TestsOutputConsolePrinter(this, properties, unboundOutputRoot);
  myProperties.setConsole(this);

  Disposer.register(this, myProperties);
  Disposer.register(this, myConsole);
}
 
Example 13
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void install() {
  class Listener extends FileDocumentManagerAdapter implements ProjectEx.ProjectSaved {
    @Override
    public void beforeDocumentSaving(@Nonnull Document document) {
      if (document == myConsole.getEditorDocument()) {
        saveHistory();
      }
    }

    @Override
    public void saved(@Nonnull Project project) {
      saveHistory();
    }
  }
  Listener listener = new Listener();
  ApplicationManager.getApplication().getMessageBus().connect(myConsole).subscribe(ProjectEx.ProjectSaved.TOPIC, listener);
  myConsole.getProject().getMessageBus().connect(myConsole).subscribe(AppTopics.FILE_DOCUMENT_SYNC, listener);

  myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, this);
  Disposer.register(myConsole, new Disposable() {
    @Override
    public void dispose() {
      myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, null);
      saveHistory();
    }
  });
  if (myHelper.getModel().getHistorySize() == 0) {
    loadHistory(myHelper.getId());
  }
  configureActions();
  myLastSaveStamp = getCurrentTimeStamp();
}
 
Example 14
Source File: DisposerTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testOverrideParentDisposable() throws Exception {
  Disposer.register(myFolder1, myLeaf1);
  Disposer.register(myFolder2, myFolder1);
  Disposer.register(myRoot, myFolder1);

  Disposer.dispose(myFolder2);

  assertDisposed(myFolder2);
  assertFalse(myLeaf1.isDisposed());
  assertFalse(myFolder1.isDisposed());

  Disposer.dispose(myRoot);
  assertDisposed(myFolder1);
  assertDisposed(myLeaf1);
}
 
Example 15
Source File: DisposerTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testDisposalOfParentess2() throws Throwable {
  Disposer.register(myFolder1, myLeaf1);
  Disposer.register(myFolder2, myLeaf2);
  Disposer.register(myFolder1, myFolder2);

  Disposer.dispose(myFolder1);

  assertDisposed(myFolder1);
  assertDisposed(myFolder2);
  assertDisposed(myLeaf1);
  assertDisposed(myLeaf2);
}
 
Example 16
Source File: VfsRootAccess.java    From consulo with Apache License 2.0 4 votes vote down vote up
@TestOnly
public static void allowRootAccess(@Nonnull Disposable disposable, @Nonnull final String... roots) {
  if (roots.length == 0) return;
  allowRootAccess(roots);
  Disposer.register(disposable, () -> disallowRootAccess(roots));
}
 
Example 17
Source File: HighlightTestInfo.java    From consulo with Apache License 2.0 4 votes vote down vote up
public HighlightTestInfo(@Nonnull Disposable parentDisposable, @NonNls @Nonnull String... filePaths) {
  this.filePaths = filePaths;
  // disposer here for catching the case of not calling test()
  Disposer.register(parentDisposable, this);
  myPlace = parentDisposable.toString();
}
 
Example 18
Source File: DesktopBalloonLayoutImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void add(@Nonnull final Balloon balloon, @Nullable Object layoutData) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  Balloon merge = merge(layoutData);
  if (merge == null) {
    if (!myBalloons.isEmpty() && myBalloons.size() == getVisibleCount()) {
      remove(myBalloons.get(0));
    }
    myBalloons.add(balloon);
  }
  else {
    int index = myBalloons.indexOf(merge);
    remove(merge);
    myBalloons.add(index, balloon);
  }
  if (layoutData instanceof BalloonLayoutData) {
    BalloonLayoutData balloonLayoutData = (BalloonLayoutData)layoutData;
    balloonLayoutData.closeAll = myCloseAll;
    balloonLayoutData.doLayout = myLayoutRunnable;
    myLayoutData.put(balloon, balloonLayoutData);
  }
  Disposer.register(balloon, new Disposable() {
    public void dispose() {
      clearNMore(balloon);
      remove(balloon, false);
      queueRelayout();
    }
  });

  if (myLafListener == null && layoutData != null) {
    myLafListener = new LafManagerListener() {
      @Override
      public void lookAndFeelChanged(LafManager source) {
        for (BalloonLayoutData layoutData : myLayoutData.values()) {
          if (layoutData.lafHandler != null) {
            layoutData.lafHandler.run();
          }
        }
      }
    };
    LafManager.getInstance().addLafManagerListener(myLafListener);
  }

  calculateSize();
  relayout();
  if (!balloon.isDisposed()) {
    balloon.show(myLayeredPane);
  }
  fireRelayout();
}
 
Example 19
Source File: DiffMarkup.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void addDisposable(@Nonnull Disposable disposable) {
  Disposer.register(this, disposable);
  myDisposables.add(disposable);
}
 
Example 20
Source File: CopyrightManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Inject
public CopyrightManager(@Nonnull Project project,
                        @Nonnull final EditorFactory editorFactory,
                        @Nonnull final Application application,
                        @Nonnull final FileDocumentManager fileDocumentManager,
                        @Nonnull final ProjectRootManager projectRootManager,
                        @Nonnull final PsiManager psiManager,
                        @Nonnull StartupManager startupManager) {
  myProject = project;
  if (myProject.isDefault()) {
    return;
  }

  final NewFileTracker newFileTracker = NewFileTracker.getInstance();
  Disposer.register(myProject, newFileTracker::clear);
  startupManager.runWhenProjectIsInitialized(new Runnable() {
    @Override
    public void run() {
      DocumentListener listener = new DocumentAdapter() {
        @Override
        public void documentChanged(DocumentEvent e) {
          final Document document = e.getDocument();
          final VirtualFile virtualFile = fileDocumentManager.getFile(document);
          if (virtualFile == null) return;
          if (!newFileTracker.poll(virtualFile)) return;
          if (!CopyrightUpdaters.hasExtension(virtualFile)) return;
          final Module module = projectRootManager.getFileIndex().getModuleForFile(virtualFile);
          if (module == null) return;
          final PsiFile file = psiManager.findFile(virtualFile);
          if (file == null) return;
          application.invokeLater(new Runnable() {
            @Override
            public void run() {
              if (myProject.isDisposed()) return;
              if (file.isValid() && file.isWritable()) {
                final CopyrightProfile opts = getCopyrightOptions(file);
                if (opts != null) {
                  new UpdateCopyrightProcessor(myProject, module, file).run();
                }
              }
            }
          }, ModalityState.NON_MODAL, myProject.getDisposed());
        }
      };
      editorFactory.getEventMulticaster().addDocumentListener(listener, myProject);
    }
  });
}