Skip to content

Commit

Permalink
changed conponents to services to adhere to the new plugin coding sta…
Browse files Browse the repository at this point in the history
…ndards of openapi // also removed a memory leak in the resolver
  • Loading branch information
guymahieu committed Feb 9, 2020
1 parent 8970f45 commit 0d4b492
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 306 deletions.
71 changes: 28 additions & 43 deletions src/main/java/org/clarent/ivyidea/AbstractResolveAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,44 @@
public abstract class AbstractResolveAction extends AnAction {

protected void updateIntellijModel(final Module module, final List<ResolvedDependency> dependencies) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
final IntellijModuleWrapper moduleWrapper = IntellijModuleWrapper.forModule(module);
try {
moduleWrapper.updateDependencies(dependencies);
} finally {
moduleWrapper.close();
}
}
});
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
try (IntellijModuleWrapper moduleWrapper = IntellijModuleWrapper.forModule(module)) {
moduleWrapper.updateDependencies(dependencies);
}
});
}));
}

protected void clearConsole(final Project project) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
IntellijUtils.getConsoleView(project).clear();
}
});
ApplicationManager.getApplication().invokeLater(() -> IntellijUtils.getConsoleView(project).clear());
}

protected void reportProblems(final Module module, final List<ResolveProblem> problems) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
final IvyIdeaFacetConfiguration ivyIdeaFacetConfiguration = IvyIdeaFacetConfiguration.getInstance(module);
if (ivyIdeaFacetConfiguration == null) {
throw new RuntimeException("Internal error: module " + module.getName() + " does not seem to be have an IvyIDEA facet, but was included in the resolve process anyway.");
}
final ConsoleView consoleView = IntellijUtils.getConsoleView(module.getProject());
String configsForModule;
if (ivyIdeaFacetConfiguration.isOnlyResolveSelectedConfigs()) {
final Set<String> configs = ivyIdeaFacetConfiguration.getConfigsToResolve();
if (configs == null || configs.size() == 0) {
configsForModule = "[No configurations selected!]";
} else {
configsForModule = configs.toString();
}
ApplicationManager.getApplication().invokeLater(() -> {
final IvyIdeaFacetConfiguration ivyIdeaFacetConfiguration = IvyIdeaFacetConfiguration.getInstance(module);
if (ivyIdeaFacetConfiguration == null) {
throw new RuntimeException("Internal error: module " + module.getName() + " does not seem to be have an IvyIDEA facet, but was included in the resolve process anyway.");
}
final ConsoleView consoleView = IntellijUtils.getConsoleView(module.getProject());
String configsForModule;
if (ivyIdeaFacetConfiguration.isOnlyResolveSelectedConfigs()) {
final Set<String> configs = ivyIdeaFacetConfiguration.getConfigsToResolve();
if (configs == null || configs.size() == 0) {
configsForModule = "[No configurations selected!]";
} else {
configsForModule = "[All configurations]";
configsForModule = configs.toString();
}
if (problems.isEmpty()) {
consoleView.print("No problems detected during resolve for module '" + module.getName() + "' " + configsForModule + ".\n", ConsoleViewContentType.NORMAL_OUTPUT);
} else {
consoleView.print("Problems for module '" + module.getName() + " " + configsForModule + "':" + '\n', ConsoleViewContentType.NORMAL_OUTPUT);
for (ResolveProblem resolveProblem : problems) {
consoleView.print("\t" + resolveProblem.toString() + '\n', ConsoleViewContentType.ERROR_OUTPUT);
}
// Make sure the toolwindow becomes visible if there were problems
IntellijUtils.getToolWindow(module.getProject()).show(null);
} else {
configsForModule = "[All configurations]";
}
if (problems.isEmpty()) {
consoleView.print("No problems detected during resolve for module '" + module.getName() + "' " + configsForModule + ".\n", ConsoleViewContentType.NORMAL_OUTPUT);
} else {
consoleView.print("Problems for module '" + module.getName() + " " + configsForModule + "':" + '\n', ConsoleViewContentType.NORMAL_OUTPUT);
for (ResolveProblem resolveProblem : problems) {
consoleView.print("\t" + resolveProblem.toString() + '\n', ConsoleViewContentType.ERROR_OUTPUT);
}
// Make sure the tool window becomes visible if there were problems
IntellijUtils.getToolWindow(module.getProject()).show(null);
}
});
}
Expand Down
17 changes: 5 additions & 12 deletions src/main/java/org/clarent/ivyidea/intellij/IntellijUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import com.intellij.execution.ui.ConsoleView;
import com.intellij.facet.FacetManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
Expand All @@ -41,31 +40,25 @@ public class IntellijUtils {
@NotNull
public static Module[] getAllModulesWithIvyIdeaFacet(Project project) {
final Module[] allModules = ModuleManager.getInstance(project).getModules();
final List<Module> result = new ArrayList<Module>();
final List<Module> result = new ArrayList<>();
for (Module module : allModules) {
if (containsIvyIdeaFacet(module)) {
result.add(module);
}
}
return result.toArray(new Module[result.size()]);
return result.toArray(new Module[0]);
}

public static boolean containsIvyIdeaFacet(@Nullable Module module) {
return module != null && FacetManager.getInstance(module).getFacetByType(IvyIdeaFacetType.ID) != null;
}

@NotNull
public static FileType getXmlFileType() {
return FileTypeManager.getInstance().getFileTypeByExtension("xml");
}


public static ConsoleView getConsoleView(Project project) {
return ((ToolWindowRegistrationComponent) project.getComponent(ToolWindowRegistrationComponent.COMPONENT_NAME)).getConsole();
return ServiceManager.getService(project, IvyIdeaConsoleService.class).getConsoleView();
}

public static ToolWindow getToolWindow(Project project) {
return ToolWindowManager.getInstance(project).getToolWindow(ToolWindowRegistrationComponent.TOOLWINDOW_ID);
return ToolWindowManager.getInstance(project).getToolWindow(IvyIdeaConsoleToolWindowFactory.TOOLWINDOW_ID);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.clarent.ivyidea.intellij;

import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.project.Project;

public class IvyIdeaConsoleService {

private final ConsoleView consoleView;

public IvyIdeaConsoleService(Project project) {
consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole();
}

public ConsoleView getConsoleView() {
return consoleView;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2010 Guy Mahieu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.clarent.ivyidea.intellij;

import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

/**
* @author Guy Mahieu
*/

public class IvyIdeaConsoleToolWindowFactory implements ToolWindowFactory, DumbAware {

public static final String TOOLWINDOW_ID = "IvyIDEA";

@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
ConsoleView console = ServiceManager.getService(project, IvyIdeaConsoleService.class).getConsoleView();
Content content = ServiceManager.getService(ContentFactory.class).createContent(console.getComponent(), "Console", true);
toolWindow.getContentManager().addContent(content);
}

@Override
public boolean isDoNotActivateOnStart() {
// Start with ivyidea toolwindow closed
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,29 @@
package org.clarent.ivyidea.intellij;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.clarent.ivyidea.config.model.IvyIdeaProjectSettings;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/**
* @author Guy Mahieu
*/

@State(
name = IvyIdeaProjectComponent.COMPONENT_NAME,
storages = {@Storage("$PROJECT_FILE$")}
)
public class IvyIdeaProjectComponent implements ProjectComponent, PersistentStateComponent<IvyIdeaProjectSettings> {
public class IvyIdeaProjectComponent implements PersistentStateComponent<IvyIdeaProjectSettings> {

public static final String COMPONENT_NAME = "IvyIDEA.ProjectSettings";

private final Project project;
private final IvyIdeaProjectSettings internalState;

public IvyIdeaProjectComponent(Project project) {
this.project = project;
public IvyIdeaProjectComponent() {
this.internalState = new IvyIdeaProjectSettings();
}

public void projectOpened() {
}

public void projectClosed() {
}

@NonNls
@NotNull
public String getComponentName() {
return COMPONENT_NAME;
}

public void initComponent() {
}

public void disposeComponent() {
}

@NotNull
public IvyIdeaProjectSettings getState() {
return internalState;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public class IvyIdeaFacetType extends FacetType<IvyIdeaFacet, IvyIdeaFacetConfiguration> {

public static final FacetTypeId<IvyIdeaFacet> ID = new FacetTypeId<IvyIdeaFacet>("IvyIDEA");
public static final FacetTypeId<IvyIdeaFacet> ID = new FacetTypeId<>("IvyIDEA");

public static IvyIdeaFacetType getInstance() {
return findInstance(IvyIdeaFacetType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public void readExternal(Element propertiesSettingsElement) throws InvalidDataEx
final Element propertiesFilesElement = propertiesSettingsElement.getChild("propertiesFiles");
List<String> fileNames = new ArrayList<String>();
if (propertiesFilesElement != null) {
setIncludeProjectLevelPropertiesFiles(Boolean.valueOf(propertiesFilesElement.getAttributeValue("includeProjectLevelPropertiesFiles", Boolean.TRUE.toString())));
@SuppressWarnings("unchecked")
final List<Element> propertiesFileNames = (List<Element>) propertiesFilesElement.getChildren("fileName");
setIncludeProjectLevelPropertiesFiles(Boolean.parseBoolean(propertiesFilesElement.getAttributeValue("includeProjectLevelPropertiesFiles", Boolean.TRUE.toString())));
final List<Element> propertiesFileNames = propertiesFilesElement.getChildren("fileName");
for (Element element : propertiesFileNames) {
fileNames.add(element.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.intellij.ui.BooleanTableCellEditor;
import com.intellij.ui.BooleanTableCellRenderer;
import com.intellij.util.ui.Table;
import com.intellij.ui.table.JBTable;
import org.apache.ivy.core.module.descriptor.Configuration;

import javax.swing.*;
Expand All @@ -36,7 +36,7 @@
* @author Guy Mahieu
*/

public class ConfigurationSelectionTable extends Table {
public class ConfigurationSelectionTable extends JBTable {

private boolean editable = true;

Expand Down
Loading

0 comments on commit 0d4b492

Please sign in to comment.