Use Light Service in max_open_projects code sample

This commit is contained in:
Karol Lewandowski 2023-04-05 16:13:04 +02:00
parent a25e24cbcd
commit ce89421f49
3 changed files with 13 additions and 39 deletions

View File

@ -4,18 +4,10 @@
## Quickstart
Maximum Open Projects Sample implements a `ProjectManagerListener` with two methods applied to check if the current projects have been opened or closed.
Each method refers to the `ProjectCountingService` service registered as an `com.intellij.applicationService` extension point.
Each method refers to the `ProjectCountingService` [light service][docs:plugin_services:light_services].
It provides methods to increase and decrease the global counter of the currently opened projects in the IDE.
After opening each one, a message dialog is presented to the user with the current number.
### Extension Points
| Name | Implementation | Extension Point Class |
|-----------------------------------|-------------------------------------------------------|-----------------------|
| `com.intellij.applicationService` | [ProjectCountingService][file:ProjectCountingService] | n/a |
*Reference: [Plugin Extension Points in IntelliJ SDK Docs][docs:ep]*
### Application Listeners
| Name | Implementation | Listener Class |
@ -26,7 +18,7 @@ After opening each one, a message dialog is presented to the user with the curre
[docs]: https://plugins.jetbrains.com/docs/intellij/
[docs:plugin_services]: https://plugins.jetbrains.com/docs/intellij/plugin-services.html
[docs:ep]: https://plugins.jetbrains.com/docs/intellij/plugin-extensions.html
[docs:plugin_services:light_services]: https://plugins.jetbrains.com/docs/intellij/plugin-services.html#light-services
[docs:listeners]: https://plugins.jetbrains.com/docs/intellij/plugin-listeners.html
[file:ProjectCountingService]: ./src/main/java/org/intellij/sdk/maxOpenProjects/ProjectCountingService.java

View File

@ -1,45 +1,31 @@
// Copyright 2000-2022 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.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.intellij.sdk.maxOpenProjects;
import com.intellij.openapi.components.Service;
/**
* Application service implementation to keep a running count of how many projects are open at a given time.
*/
public class ProjectCountingService {
@Service
public final class ProjectCountingService {
/**
* Sets the maximum allowed number of opened projects.
*/
private final static int MAX_OPEN_PRJ_LIMIT = 3;
private final static int MAX_OPEN_PROJECTS_LIMIT = 3;
/**
* The count of open projects must always be >= 0.
*/
private int myOpenProjectCount = 0;
public void incrProjectCount() {
validateProjectCount();
myOpenProjectCount++;
}
public void decrProjectCount() {
myOpenProjectCount--;
validateProjectCount();
if (myOpenProjectCount > 0) {
myOpenProjectCount--;
}
}
public boolean projectLimitExceeded() {
return myOpenProjectCount > MAX_OPEN_PRJ_LIMIT;
}
public int getProjectCount() {
return myOpenProjectCount;
}
/**
* Anti-bugging to ensure the count of open projects never goes below zero.
*/
private void validateProjectCount() {
myOpenProjectCount = Math.max(myOpenProjectCount, 0);
return myOpenProjectCount > MAX_OPEN_PROJECTS_LIMIT;
}
}

View File

@ -1,4 +1,4 @@
<!-- Copyright 2000-2023 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. -->
<!-- Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
@ -40,8 +40,4 @@
topic="com.intellij.openapi.project.ProjectManagerListener"/>
</applicationListeners>
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="org.intellij.sdk.maxOpenProjects.ProjectCountingService"/>
</extensions>
</idea-plugin>