Inc Feedback

This commit is contained in:
JohnHake 2020-02-27 17:25:22 -08:00
parent c9f5a3f92c
commit 081823f5e8
5 changed files with 133 additions and 45 deletions

View File

@ -17,7 +17,6 @@ repositories {
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.3.3'
sameSinceUntilBuild = true
}
patchPluginXml {
version = project.version

View File

@ -12,10 +12,25 @@ import com.intellij.openapi.project.ProjectManager;
public class ProjectCountingService {
// Sets the maximum allowed number of opened projects.
private final int MAX_OPEN_PRJ_LIMIT = 3;
// The count of open projects must always be >= 0
private int openProjectCount = 0;
public boolean projLimitExceeded() {
ProjectManager prjMgr = ProjectManager.getInstance();
return prjMgr.getOpenProjects().length > MAX_OPEN_PRJ_LIMIT;
public void incrProjectCount() {
if (openProjectCount < 0) {
openProjectCount = 0;
}
openProjectCount++;
}
public void decrProjectCount() {
openProjectCount--;
if (openProjectCount < 0) {
openProjectCount = 0;
}
}
public boolean projectLimitExceeded() {
return openProjectCount > MAX_OPEN_PRJ_LIMIT;
}
}

View File

@ -0,0 +1,112 @@
// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.sdk.maxOpenPrj;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.CommonShortcuts;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
/**
* Listener to detect project open and close.
* Depends on the org.intellij.sdk.maxOpenPrj.ProjectCountingService
*/
public class ProjectOpenCloseListener implements ProjectManagerListener, Disposable {
/**
* Invoked on project open.
*
* @param project opening project
*/
@Override
public void projectOpened(@NotNull Project project) {
// Get the counting service
ProjectCountingService projectCountingService = ServiceManager.getService(ProjectCountingService.class);
// Increment the project count
projectCountingService.incrProjectCount();
// See if the total # of projects violates the limit.
if (projectCountingService.projectLimitExceeded()) {
// Transitioned to outside the limit
// Messages.showMessageDialog(
// "The number of open projects exceeds the SDK plugin max_opened_projects limit.",
// "Opening Project \"" + project.getName() + "\"",
// Messages.getErrorIcon());
showBalloon(project, "Opening Project \"" + project.getName() + "\"", "The number of open projects exceeds the SDK plugin max_opened_projects limit.");
}
}
/**
* Invoked on project close.
*
* @param project closing project
*/
@Override
public void projectClosed(@NotNull Project project) {
// Get the counting service
ProjectCountingService projectCountingService = ServiceManager.getService(ProjectCountingService.class);
// Was the count above the limit?
boolean previouslyOverCount = projectCountingService.projectLimitExceeded();
// Decrement the count because a project just closed
projectCountingService.decrProjectCount();
// See if the total # of projects no longer violates the limit.
if (!projectCountingService.projectLimitExceeded() && previouslyOverCount) {
// Transitioned from above the limit to within the limit.
// Messages.showMessageDialog(
// "The number of open projects does not exceed the SDK plugin max_opened_projects limit.",
// "\"" + project.getName() + "\" Has Been Closed",
// Messages.getErrorIcon());
showBalloon(project, "\"" + project.getName() + "\" Has Been Closed", "The number of open projects is below the SDK plugin max_opened_projects limit.");
}
}
private void showBalloon(Project project, String title, String message) {
JLabel component = new JLabel(message);
final Balloon balloon = JBPopupFactory.getInstance().createBalloonBuilder(component)
.setShadow(true)
.setAnimationCycle(200) // Was 0
.setHideOnClickOutside(true)
// .setHideOnKeyOutside(true)
.setHideOnAction(true) // was false
.setFillColor(UIUtil.getControlColor())
.setTitle(title) // Added
.setFadeoutTime(5000) // Added
.createBalloon();
// DumbAwareAction.create(e -> balloon.hide())
// .registerCustomShortcutSet(CommonShortcuts.ESCAPE, component);
Disposer.register(project, balloon);
// final Balloon.Position position = QuickEditAction.getBalloonPosition(editor);
// RelativePoint point = JBPopupFactory.getInstance().guessBestPopupLocation(editor);
// if (position == Balloon.Position.above) {
// final Point p = point.getPoint();
// point = new RelativePoint(point.getComponent(), new Point(p.x, p.y - editor.getLineHeight()));
// }
ProjectManager PM = ProjectManager.getInstance();
Project[] AllProjects = PM.getOpenProjects();
Project prevProject = AllProjects[AllProjects.length - 1];
final WindowManager manager = WindowManager.getInstance();
final JFrame frame = prevProject != null ? manager.getFrame(prevProject) : manager.findVisibleFrame();
JRootPane pane = frame.getRootPane();
balloon.showInCenterOf(pane);
}
/**
* Usually not invoked directly, see Disposable class javadoc.
*/
@Override
public void dispose() {
// noop
}
}

View File

@ -1,38 +0,0 @@
// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.sdk.maxOpenPrj;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
/**
* Listener to detect opening of projects.
*/
public class ProjectOpenListener implements ProjectManagerListener {
/**
* Invoked on project open.
*
* @param project opening project
*/
@Override
public void projectOpened(@NotNull Project project) {
// Get the counting service
ProjectCountingService prjCountingSrvc = ServiceManager.getService(ProjectCountingService.class);
// See if the total # of projects violates the limit.
if (prjCountingSrvc.projLimitExceeded()) {
Messages.showMessageDialog(
"The number of open projects exceeds the SDK plugin max_opened_projects limit. Close at least one open project and try again.",
"Cannot open project \"" + project.getName() + "\"",
Messages.getErrorIcon());
ProjectManager prjMgr = ProjectManager.getInstance();
boolean closedSuccessfully = prjMgr.closeProject(project);
System.out.println(String.format("Project closing was %s", closedSuccessfully));
}
}
}

View File

@ -10,7 +10,7 @@
<!-- The version of this plugin is set by build.gradle -->
<!-- The idea-version of this plugin is set by build.gradle -->
<!-- The idea-version of this plugin is overwritten by build.gradle -->
<idea-version since-build="193"/>
<!-- Product and plugin compatibility requirements -->
@ -25,7 +25,7 @@
<change-notes>
<![CDATA[
<ul>
<li><b>2.1.0</b> Remove project component and use listener instead.</li>
<li><b>2.1.0</b> Remove project component and use listener instead. No longer denies opening additional projets. Just notifies the user.</li>
<li><b>2.0.0</b> Convert to Gradle-based plugin.</li>
<li><b>1.0.0</b> Release 2018.3 and earlier.</li>
</ul>
@ -36,7 +36,7 @@
<vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor>
<applicationListeners>
<listener class="org.intellij.sdk.maxOpenPrj.ProjectOpenListener" topic="com.intellij.openapi.project.ProjectManagerListener"/>
<listener class="org.intellij.sdk.maxOpenPrj.ProjectOpenCloseListener" topic="com.intellij.openapi.project.ProjectManagerListener"/>
</applicationListeners>
<extensions defaultExtensionNs="com.intellij">