IJSDK-681 Functional

This commit is contained in:
JohnHake 2020-03-01 20:56:10 -08:00
parent 373206dbd8
commit 61ff434aae
6 changed files with 17 additions and 177 deletions

View File

@ -1,6 +1,7 @@
# 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.
#
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,4 +1,4 @@
// 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.
rootProject.name = 'maxOpenPrj'
rootProject.name = 'maxOpenProjects'

View File

@ -1,6 +1,6 @@
// 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;
package org.intellij.sdk.maxOpenProjects;
/**
* Application service implementation to keep a running count of
@ -12,14 +12,14 @@ public class ProjectCountingService {
// The count of open projects must always be >= 0
private int myOpenProjectCount = 0;
public void incrProjectCount() {
myOpenProjectCount = verifyProjectCount(myOpenProjectCount);
public void incrProjectCount() {
validateProjectCount();
myOpenProjectCount++;
}
public void decrProjectCount() {
myOpenProjectCount--;
myOpenProjectCount = verifyProjectCount(myOpenProjectCount);
validateProjectCount();
}
public boolean projectLimitExceeded() {
@ -32,12 +32,9 @@ public class ProjectCountingService {
/**
* Anti-bugging to ensure the count of open projects never goes below zero.
* @param openProjectCount The count of currently open projects
* @return 0 if openProjectCount<0
* openProjectCount otherwise
*/
private int verifyProjectCount(int openProjectCount) {
return openProjectCount<0 ? 0 : openProjectCount;
private void validateProjectCount() {
myOpenProjectCount = Math.max(myOpenProjectCount, 0);
}
}

View File

@ -1,22 +1,19 @@
// 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;
package org.intellij.sdk.maxOpenProjects;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.ui.Messages;
import org.intellij.sdk.utils.SdkBalloonHelper;
import org.jetbrains.annotations.NotNull;
/**
* Listener to detect project open and close.
* Depends on org.intellij.sdk.maxOpenPrj.ProjectCountingService
* Depends on org.intellij.sdk.utils.SdkBalloonHelper
* Depends on org.intellij.sdk.maxOpenProjects.ProjectCountingService
*/
public class ProjectOpenCloseListener implements ProjectManagerListener {
private static final String MAX_OPEN_PROJ_DISCLAIM = "\nThis is an IntelliJ Platform SDK demo message.\n\n";
/**
* Invoked on project open.
@ -35,9 +32,9 @@ public class ProjectOpenCloseListener implements ProjectManagerListener {
if (projectCountingService.projectLimitExceeded()) {
// Transitioned to outside the limit
String title = String.format("Opening Project \"%s\"", project.getName());
String message = MAX_OPEN_PROJ_DISCLAIM + "The number of open projects exceeds the SDK plugin max_opened_projects limit.";
SdkBalloonHelper balloonHelper = SdkBalloonHelper.getSdkBalloonHelper();
balloonHelper.showBalloon(project, title, message);
String message = "<br>The number of open projects exceeds the SDK plugin max_opened_projects limit.<br><br>" +
"This is not an error<br><br>";
Messages.showMessageDialog(project, message, title, Messages.getInformationIcon());
}
}
@ -52,18 +49,8 @@ public class ProjectOpenCloseListener implements ProjectManagerListener {
if (ApplicationManager.getApplication().isUnitTestMode()) return;
// 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 to within the limit.
String title = String.format("\"%s\" Has Been Closed", project.getName());
String message = MAX_OPEN_PROJ_DISCLAIM + "The number of open projects is below the SDK plugin max_opened_projects limit.";
SdkBalloonHelper balloonHelper = SdkBalloonHelper.getSdkBalloonHelper();
balloonHelper.showBalloon(project, title, message);
}
}
}

View File

@ -1,145 +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.utils;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
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.IdeFocusManager;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.openapi.wm.ex.WindowManagerEx;
import com.intellij.openapi.wm.impl.WindowManagerImpl;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
/**
* This is just a utility class to help communicate the state of this plugin using Popups
*
* @see <a href="https://www.jetbrains.org/intellij/sdk/docs/user_interface_components/popups.html">Popup documentation</a>
*/
public class SdkBalloonHelper {
public static SdkBalloonHelper getSdkBalloonHelper() {
return new SdkBalloonHelper();
}
/**
* This method constructs a Balloon-type popup and displays it in the middle of a Project window.
*
* @param project The Project to host the Balloon.
* @param title A short description of what the Balloon conveys
* @param message Detailed information to display in HTML format
*/
public void showBalloon(@Nullable Project project, @NotNull String title, @NotNull String message) {
// Create a component to hold the message in HTML format
JLabel component = new JLabel("<html>" + message + "</html>");
// Construct the balloon using the component
final Balloon balloon = JBPopupFactory.getInstance().createBalloonBuilder(component)
.setShadow(true)
.setHideOnClickOutside(true)
.setHideOnAction(false)
.setFillColor(UIUtil.getControlColor())
.setTitle(title)
.setFadeoutTime(6000)
.createBalloon();
// Ensure the project is open. If not, find a visible Project to display the balloon.
Project displayProject = findValidProject(project);
if (displayProject == null) return;
// Get the UI element to display the balloon
JRootPane pane = getProjectRootPane(displayProject);
if (pane == null) return;
// This is a special case for the message when closing a project.
// The closed project is no longer visible, so the next...
// Consequently register it for disposal with the project that will display it
// Disposer.register(displayProject, balloon);
// Show the balloon in the middle of the project's window - it will disappear per the animation
balloon.showInCenterOf(pane);
}
/**
* This function verifies that the provided Project is still open.
* If the Project is not open (closed, or in some state of disposal,)
* the function tries to find the next most-recently opened Project.
*
* @param dodgyProject The Project to be verified as open
* @return dodgyProject if it is verified as open,
* Or the last Project listed in ProjectManager's list of open Projects.
* Or null if no other Projects are open. (Edge case when IDE is closing.)
*/
@Nullable
private Project findValidProject(@Nullable Project dodgyProject) {
Project validProject = dodgyProject;
if ((dodgyProject == null) || !dodgyProject.isOpen()) {
// Find the next most-recently opened Project that is still open.
IdeFocusManager focusManager = IdeFocusManager.getGlobalInstance();
IdeFrame ideFrameFromFocusMgr = focusManager.getLastFocusedFrame();
Project focusMgrProject = ideFrameFromFocusMgr.getProject();
System.out.println("\n\nFocus Mgr last focused frame -> project: " + focusMgrProject.toString() + "\n");
final WindowManager windowManager = WindowManagerImpl.getInstance();
IdeFrame[] allIdeFrames = windowManager.getAllProjectFrames();
for (int i=0; i<allIdeFrames.length; i++) {
System.out.println(String.format("Win Mgr IdeFrames[%d] -> project: %s", i, allIdeFrames[i].getProject().toString()));
}
String outcome = String.format("\n\nUsing Win Mgr getIdeFrame(null): ");
String focusProject;
IdeFrame focusFrame = windowManager.getIdeFrame(null);
if (focusFrame != null) {
focusProject = String.format("%s", focusFrame.getProject().toString());
} else {
focusProject = String.format("No focused project found");
}
System.out.println(outcome + focusProject);
outcome = String.format("\n\nUsing Win Mgr findFrameFor(null): ");
focusFrame = ((WindowManagerImpl) windowManager).findFrameFor(null);
if (focusFrame != null) {
focusProject = String.format("%s", focusFrame.getProject().toString());
} else {
focusProject = String.format("No focused project found");
}
System.out.println(outcome + focusProject);
ProjectManager projectManager = ProjectManager.getInstance();
Project[] allProjects = projectManager.getOpenProjects();
validProject = allProjects.length > 0 ? allProjects[allProjects.length - 1] : null;
System.out.println("\n\nProjMgr allProjects[last]: " + validProject.toString());
}
return validProject;
}
/**
* This function gets the JRootPane for an open Project
*
* @param project The open Project
* @return A valid JRootPane for the Project
* Otherwise null
*/
@Nullable
private JRootPane getProjectRootPane(@Nullable Project project) {
JRootPane projectPane = null;
if ((project != null) && project.isOpen()) {
// Get the frame for the project, then the JRootPane
final WindowManager manager = WindowManager.getInstance();
final JFrame frame = manager.getFrame(project);
projectPane = frame != null ? frame.getRootPane() : null;
}
return projectPane;
}
}

View File

@ -3,7 +3,7 @@
<idea-plugin>
<!-- Unique id for this plugin. Must stay constant for the life of the plugin. -->
<id>org.intellij.sdk.maxOpenPrj</id>
<id>org.intellij.sdk.maxOpenProjects</id>
<!-- Text to display as name on Preferences/Settings | Plugin page -->
<name>SDK: Maximum Open Projects Sample</name>
@ -36,10 +36,10 @@
<vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor>
<applicationListeners>
<listener class="org.intellij.sdk.maxOpenPrj.ProjectOpenCloseListener" topic="com.intellij.openapi.project.ProjectManagerListener"/>
<listener class="org.intellij.sdk.maxOpenProjects.ProjectOpenCloseListener" topic="com.intellij.openapi.project.ProjectManagerListener"/>
</applicationListeners>
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="org.intellij.sdk.maxOpenPrj.ProjectCountingService"/>
<applicationService serviceImplementation="org.intellij.sdk.maxOpenProjects.ProjectCountingService"/>
</extensions>
</idea-plugin>