Skip to content

Commit

Permalink
#261 | Improved cleanup on Project dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
mlytvyn authored Mar 6, 2023
1 parent 30b1581 commit b3dc21f
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 256 deletions.
17 changes: 7 additions & 10 deletions resources/META-INF/plugin-internal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@
<applicationService serviceImplementation="com.intellij.idea.plugin.hybris.system.cockpitng.psi.provider.CngWidgetReferenceProvider"/>
<applicationService serviceImplementation="com.intellij.idea.plugin.hybris.system.cockpitng.psi.provider.CngWidgetSettingReferenceProvider"/>
<applicationService serviceImplementation="com.intellij.idea.plugin.hybris.system.cockpitng.psi.provider.CngWidgetConnectionWidgetIdReferenceProvider"/>

<psi.referenceContributor language="XML" implementation="com.intellij.idea.plugin.hybris.system.cockpitng.psi.contributor.CngReferenceContributor"/>
<psi.referenceContributor language="XML" implementation="com.intellij.idea.plugin.hybris.system.type.psi.contributor.ItemsXmlReferenceContributor"/>

<internalFileTemplate name="FlexibleSearch File"/>
<internalFileTemplate name="Impex File"/>

<projectService serviceImplementation="com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolesToolWindow"/>

<projectService serviceImplementation="com.intellij.idea.plugin.hybris.toolwindow.system.bean.BSToolWindow"/>
<projectService serviceImplementation="com.intellij.idea.plugin.hybris.toolwindow.system.type.TSToolWindow"/>

<projectService serviceInterface="com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowService"
serviceImplementation="com.intellij.idea.plugin.hybris.toolwindow.impl.HybrisToolWindowServiceImpl"/>
<projectService serviceImplementation="com.intellij.idea.plugin.hybris.toolwindow.system.bean.view.BSViewSettings"/>
<projectService serviceImplementation="com.intellij.idea.plugin.hybris.toolwindow.system.type.view.TSViewSettings"/>
<projectService serviceImplementation="com.intellij.idea.plugin.hybris.settings.HybrisProjectSettingsComponent"/>
Expand All @@ -64,10 +62,14 @@
instance="com.intellij.idea.plugin.hybris.settings.HybrisApplicationSettingsConfigurable"/>
<applicationConfigurable id="hybris.project.import.settings" parentId="hybris.settings"
instance="com.intellij.idea.plugin.hybris.settings.HybrisProjectImportApplicationSettingsConfigurable"/>

<projectConfigurable id="hybris.project.settings" parentId="hybris.settings" nonDefaultProject="true" dynamic="true"
provider="com.intellij.idea.plugin.hybris.settings.HybrisProjectSettingsConfigurableProvider"/>
<projectConfigurable id="hybris.project.remote_instances.settings" parentId="hybris.project.settings" nonDefaultProject="true" dynamic="true"
provider="com.intellij.idea.plugin.hybris.settings.HybrisProjectRemoteInstancesSettingsConfigurableProvider"/>

<toolWindow id="SAP Commerce" icon="/icons/toolWindowHybrisIcon.svg" anchor="bottom"
factoryClass="com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowFactory"/>
</extensions>

<project-components>
Expand Down Expand Up @@ -102,11 +104,6 @@
</action>
</actions>

<extensions defaultExtensionNs="com.intellij">
<toolWindow id="SAP Commerce" icon="/icons/toolWindowHybrisIcon.svg" anchor="bottom"
factoryClass="com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowFactory"/>
</extensions>

<extensionPoints>
<extensionPoint
qualifiedName="com.intellij.idea.plugin.hybris.consoleProvider"
Expand Down
1 change: 1 addition & 0 deletions resources/META-INF/plugin-release-info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<li><i>Feature:</i> IDEA modules by default will be stored in the <code>/.idea/idea-modules</code> (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/259" target="_blank" rel="nofollow">#259</a>)</li>
<li><i>Bug Fix:</i> Console is not releasing Document on Project dispose (<a href="https://github.com/epam/sap-commerce-intellij-idea-plugin/pull/260" target="_blank" rel="nofollow">#260</a>)</li>
<li><i>Bug Fix:</i> Fixed Ant targets registration after Project Refresh</li>
<li><i>Other:</i> Improved cleanup on Project dispose</li>
<li><i>Other:</i> Added registration of the latest Ant targets</li>
<li><i>Other:</i> Updated to Kotlin 1.8</li>
<li><i>Other:</i> Removed custom <code>rt-ant</code> support, it did not work at all</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.idea.plugin.hybris.common.utils.HybrisI18NBundleUtils;
import com.intellij.idea.plugin.hybris.tools.remote.console.HybrisConsole;
import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolesPanel;
import com.intellij.idea.plugin.hybris.tools.remote.console.view.HybrisConsolesToolWindow;
import com.intellij.idea.plugin.hybris.toolwindow.CopyFileToHybrisConsoleDialog;
import com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowFactory;
import com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
Expand All @@ -40,11 +39,7 @@

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

import static com.intellij.idea.plugin.hybris.common.HybrisConstants.DIALOG_TITLE;
Expand All @@ -55,21 +50,21 @@ public final class CopyFileToHybrisConsoleUtils {
private CopyFileToHybrisConsoleUtils() {
}

private static <R, T> Optional<T> cast(@NotNull R obj, Class<T> clazz) {
private static <R, T> Optional<T> cast(@NotNull final R obj, final Class<T> clazz) {
if (clazz.isAssignableFrom(obj.getClass())) {
return Optional.ofNullable((T) obj);
} else {
return Optional.empty();
}
}

public static void copySelectedFilesToHybrisConsole(Project project, String consoleTitle, String dialogTitle) {
var hybrisConsole = getHybrisConsole(project, consoleTitle);
public static void copySelectedFilesToHybrisConsole(final Project project, final String consoleTitle, final String dialogTitle) {
final var hybrisConsole = HybrisToolWindowService.Companion.getInstance(project).getConsolesPanel().findConsole(consoleTitle);
if (hybrisConsole != null) {
boolean isConsoleNotEmpty = StringUtils.isNotEmpty(getTextFromHybrisConsole(project, hybrisConsole));
String query = getQueryFromSelectedFiles(project);
final boolean isConsoleNotEmpty = StringUtils.isNotEmpty(getTextFromHybrisConsole(project, hybrisConsole));
final String query = getQueryFromSelectedFiles(project);
if (isConsoleNotEmpty) {
var consoleDialog = new CopyFileToHybrisConsoleDialog(
final var consoleDialog = new CopyFileToHybrisConsoleDialog(
project,
getDialogTitleFromProperties(dialogTitle)
);
Expand All @@ -80,18 +75,18 @@ public static void copySelectedFilesToHybrisConsole(Project project, String cons
}
}

public static boolean isRequiredSingleFileExtension(Project project, String fileExtension) {
var fileExtensions = getFileExtensions(project);
public static boolean isRequiredSingleFileExtension(final Project project, final String fileExtension) {
final var fileExtensions = getFileExtensions(project);
return fileExtensions.size() == 1 && fileExtensions.get(0).equals(fileExtension);
}

public static boolean isRequiredMultipleFileExtension(Project project, String fileExtension) {
var fileExtensions = getFileExtensions(project);
public static boolean isRequiredMultipleFileExtension(final Project project, final String fileExtension) {
final var fileExtensions = getFileExtensions(project);
return !fileExtensions.isEmpty() && fileExtensions.stream().allMatch(fileExtension::equals);
}

private static List<String> getFileExtensions(Project project) {
var extensions = new ArrayList<String>();
private static List<String> getFileExtensions(final Project project) {
final var extensions = new ArrayList<String>();
for (var virtualFile : getSelectedFiles(project)) {
if (virtualFile.isDirectory()) {
return Collections.emptyList();
Expand All @@ -103,23 +98,18 @@ private static List<String> getFileExtensions(Project project) {
return extensions;
}

private static HybrisConsole getHybrisConsole(Project project, String consoleTitle) {
var hybrisConsolePanel = HybrisConsolesToolWindow.Companion.getInstance(project).getConsolesPanel();
return hybrisConsolePanel.findConsole(consoleTitle);
}

private static String getQueryFromSelectedFiles(Project project) {
private static String getQueryFromSelectedFiles(final Project project) {
return getSelectedFiles(project).stream()
.map(virtualFile -> getPsiFileNode(project, virtualFile))
.map(PsiElement::getText)
.collect(Collectors.joining(lineSeparator()));
}

private static PsiFile getPsiFileNode(Project project, VirtualFile virtualFile) {
private static PsiFile getPsiFileNode(final Project project, final VirtualFile virtualFile) {
return PsiManager.getInstance(project).findFile(virtualFile);
}

private static List<VirtualFile> getSelectedFiles(Project project) {
private static List<VirtualFile> getSelectedFiles(final Project project) {
return Arrays.stream(getSelectedTreePaths(project))
.map(CopyFileToHybrisConsoleUtils::getVirtualFile)
.filter(Optional::isPresent)
Expand All @@ -135,52 +125,33 @@ private static Optional<VirtualFile> getVirtualFile(final TreePath treePath) {
}

private static TreePath[] getSelectedTreePaths(final Project project) {
var currentProjectViewPane = ProjectView.getInstance(project).getCurrentProjectViewPane();
var selectionPaths = currentProjectViewPane.getSelectionPaths();
final var currentProjectViewPane = ProjectView.getInstance(project).getCurrentProjectViewPane();
final var selectionPaths = currentProjectViewPane.getSelectionPaths();
return ObjectUtils.getIfNull(selectionPaths, () -> new TreePath[0]);
}

private static HybrisConsolesPanel getHybrisConsolePanel(Project project) {
return HybrisConsolesToolWindow.Companion.getInstance(project).getConsolesPanel();
private static String getTextFromHybrisConsole(final Project project, final HybrisConsole hybrisConsole) {
final var helper = new LanguageConsoleImpl.Helper(project, hybrisConsole.getVirtualFile());
final var consoleExecutionEditor = new ConsoleExecutionEditor(helper);
final var text = consoleExecutionEditor.getDocument().getText();
Disposer.dispose(consoleExecutionEditor);
return text;
}

private static String getTextFromHybrisConsole(Project project, HybrisConsole hybrisConsole) {
var helper = new LanguageConsoleImpl.Helper(project, hybrisConsole.getVirtualFile());
var consoleExecutionEditor = new ConsoleExecutionEditor(helper);
return consoleExecutionEditor.getDocument().getText();
}

private static void copyToHybrisConsole(Project project, String consoleTitle, String query) {
var hybrisConsolePanel = getHybrisConsolePanel(project);
var hybrisConsole = hybrisConsolePanel.findConsole(consoleTitle);
private static void copyToHybrisConsole(final Project project, final String consoleTitle, final String query) {
final var consolesPanel = HybrisToolWindowService.Companion.getInstance(project).getConsolesPanel();
final var hybrisConsole = consolesPanel.findConsole(consoleTitle);
if (hybrisConsole != null) {
setActiveConsolesTab(project);
final var toolWindowService = HybrisToolWindowService.Companion.getInstance(project);
toolWindowService.activateToolWindow();
toolWindowService.activateToolWindowTab(HybrisToolWindowFactory.CONSOLES_ID);
consolesPanel.setActiveConsole(hybrisConsole);
hybrisConsole.clear();
hybrisConsole.setInputText(query);
hybrisConsolePanel.setActiveConsole(hybrisConsole);
openHybrisConsole(project);
}
}

private static void setActiveConsolesTab(final Project project) {
Optional.ofNullable(ToolWindowManager.getInstance(project).getToolWindow(HybrisToolWindowFactory.ID))
.ifPresent(toolWindow -> {
final var contentManager = toolWindow.getContentManager();
final var consolesTab = contentManager.findContent(HybrisConsolesToolWindow.ID);
if (consolesTab != null) {
contentManager.setSelectedContent(consolesTab);
}
});
}

private static void openHybrisConsole(Project project) {
var toolWindow = ToolWindowManager.getInstance(project).getToolWindow(HybrisToolWindowFactory.ID);
if (toolWindow != null) {
toolWindow.activate(null);
}
}

private static String getDialogTitleFromProperties(String fileExtension) {
private static String getDialogTitleFromProperties(final String fileExtension) {
return HybrisI18NBundleUtils.message(DIALOG_TITLE + fileExtension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public void configure(
return;
}

final var messageBus = project.getMessageBus().connect();

final var projectDataImportListener = new ProjectDataImportListener() {

@Override
Expand All @@ -58,13 +60,14 @@ public void onImportFinished(final String projectPath) {
final var module = ModuleManager.getInstance(project)
.findModuleByName(projectPath.substring(projectPath.lastIndexOf('/') + 1));
updateModuleSettings(project, module);
messageBus.dispose();
}
});
}
}
};

project.getMessageBus().connect().subscribe(
messageBus.subscribe(
ProjectDataImportListener.TOPIC,
projectDataImportListener
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.intellij.openapi.roots.ui.configuration.ModulesProvider;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.model.MavenConstants;
import org.jetbrains.idea.maven.project.MavenImportListener;
Expand Down Expand Up @@ -79,8 +80,9 @@ public void configure(
.toList();

if (mavenImportListener == null) {
mavenImportListener = new HybrisMavenImportListener(project);
project.getMessageBus().connect().subscribe(MavenImportListener.TOPIC, mavenImportListener);
final var messageBus = project.getMessageBus().connect();
mavenImportListener = new HybrisMavenImportListener(project, messageBus);
messageBus.subscribe(MavenImportListener.TOPIC, mavenImportListener);
}
mavenImportListener.setMavenModulesConfig(mavenProjectFiles, mavenModules, configuratorFactory);

Expand Down Expand Up @@ -121,12 +123,14 @@ private Predicate<MavenProjectBuilder> isProjectPathValid(final @NotNull List<Ma
static class HybrisMavenImportListener implements MavenImportListener {

private final Project project;
private final MessageBusConnection messageBus;
private List<VirtualFile> pomList;
private List<MavenModuleDescriptor> mavenModules;
private ConfiguratorFactory configuratorFactory;

public HybrisMavenImportListener(final Project project) {
public HybrisMavenImportListener(final Project project, final MessageBusConnection messageBus) {
this.project = project;
this.messageBus = messageBus;
}

@Override
Expand All @@ -143,6 +147,7 @@ public void importFinished(
importedProjects,
newModules
);
messageBus.dispose();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ import com.intellij.idea.plugin.hybris.project.descriptors.HybrisModuleDescripto
import com.intellij.idea.plugin.hybris.project.descriptors.HybrisProjectDescriptor
import com.intellij.idea.plugin.hybris.settings.HybrisDeveloperSpecificProjectSettingsListener
import com.intellij.idea.plugin.hybris.settings.HybrisProjectSettingsComponent
import com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowFactory
import com.intellij.idea.plugin.hybris.toolwindow.HybrisToolWindowService
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ui.configuration.IdeaProjectSettingsService
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.removeUserData
import com.intellij.openapi.wm.ToolWindowManager

private const val OPEN_SETTINGS_DIALOG = "hybrisProjectImportOpenSettingsDialog"
private const val SYNC_PROJECT_SETTINGS = "hybrisProjectImportSyncProjectSettings"
Expand All @@ -43,7 +42,7 @@ class HybrisProjectImportStartupActivity : ProjectActivity {
if (!HybrisProjectSettingsComponent.getInstance(project).isHybrisProject()) return

RunOnceUtil.runOnceForProject(project, "afterHybrisProjectImport") {
activateToolWindow(project)
HybrisToolWindowService.getInstance(project).activateToolWindow()

project.getUserData(openSettingsKey)
?.let {
Expand All @@ -69,22 +68,16 @@ class HybrisProjectImportStartupActivity : ProjectActivity {

}

private fun activateToolWindow(project: Project) = ToolWindowManager.getInstance(project).getToolWindow(HybrisToolWindowFactory.ID)
?.let {
ApplicationManager.getApplication().invokeLater {
it.isAvailable = true
it.activate(null, true)
}
}

// ensure the dialog is shown after all startup activities are done
private fun openSettingsForProject(project: Project) = ApplicationManager.getApplication().invokeLater({
IdeaProjectSettingsService.getInstance(project).openProjectSettings()
}, ModalityState.NON_MODAL, project.disposed)

private fun syncProjectSettingsForProject(project: Project) {
project.messageBus.syncPublisher(HybrisDeveloperSpecificProjectSettingsListener.TOPIC).hacConnectionSettingsChanged()
project.messageBus.syncPublisher(HybrisDeveloperSpecificProjectSettingsListener.TOPIC).solrConnectionSettingsChanged()
with (project.messageBus.syncPublisher(HybrisDeveloperSpecificProjectSettingsListener.TOPIC)) {
hacConnectionSettingsChanged()
solrConnectionSettingsChanged()
}
}


Expand Down
Loading

0 comments on commit b3dc21f

Please sign in to comment.