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 ## Quickstart
Maximum Open Projects Sample implements a `ProjectManagerListener` with two methods applied to check if the current projects have been opened or closed. 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. 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. 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 ### Application Listeners
| Name | Implementation | Listener Class | | 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]: https://plugins.jetbrains.com/docs/intellij/
[docs:plugin_services]: https://plugins.jetbrains.com/docs/intellij/plugin-services.html [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 [docs:listeners]: https://plugins.jetbrains.com/docs/intellij/plugin-listeners.html
[file:ProjectCountingService]: ./src/main/java/org/intellij/sdk/maxOpenProjects/ProjectCountingService.java [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; 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. * 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 {
/** private final static int MAX_OPEN_PROJECTS_LIMIT = 3;
* Sets the maximum allowed number of opened projects.
*/
private final static int MAX_OPEN_PRJ_LIMIT = 3;
/**
* The count of open projects must always be >= 0.
*/
private int myOpenProjectCount = 0; private int myOpenProjectCount = 0;
public void incrProjectCount() { public void incrProjectCount() {
validateProjectCount();
myOpenProjectCount++; myOpenProjectCount++;
} }
public void decrProjectCount() { public void decrProjectCount() {
if (myOpenProjectCount > 0) {
myOpenProjectCount--; myOpenProjectCount--;
validateProjectCount(); }
} }
public boolean projectLimitExceeded() { public boolean projectLimitExceeded() {
return myOpenProjectCount > MAX_OPEN_PRJ_LIMIT; return myOpenProjectCount > MAX_OPEN_PROJECTS_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);
} }
} }

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 --> <!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin> <idea-plugin>
@ -40,8 +40,4 @@
topic="com.intellij.openapi.project.ProjectManagerListener"/> topic="com.intellij.openapi.project.ProjectManagerListener"/>
</applicationListeners> </applicationListeners>
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="org.intellij.sdk.maxOpenProjects.ProjectCountingService"/>
</extensions>
</idea-plugin> </idea-plugin>