mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
Draft
This commit is contained in:
parent
50088bb8db
commit
c9f5a3f92c
@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'org.intellij.sdk'
|
||||
version '2.0.0'
|
||||
version '2.1.0'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
@ -17,7 +17,7 @@ repositories {
|
||||
// See https://github.com/JetBrains/gradle-intellij-plugin/
|
||||
intellij {
|
||||
version '2019.3.3'
|
||||
updateSinceUntilBuild = false
|
||||
sameSinceUntilBuild = true
|
||||
}
|
||||
patchPluginXml {
|
||||
version = project.version
|
||||
|
@ -1,55 +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.*;
|
||||
import com.intellij.openapi.project.*;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: Alexey.Chursin
|
||||
* Date: Aug 13, 2010
|
||||
* Time: 3:50:25 PM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class MaxProject implements ProjectComponent {
|
||||
public MaxProject(Project project) {
|
||||
}
|
||||
|
||||
public void initComponent() {
|
||||
// TODO: insert component initialization logic here
|
||||
}
|
||||
|
||||
public void disposeComponent() {
|
||||
// TODO: insert component disposal logic here
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getComponentName() {
|
||||
return "MaxProject";
|
||||
}
|
||||
|
||||
public void projectOpened() {
|
||||
// called when project is opened
|
||||
MyCounter CommandCounter = ServiceManager.getService(MyCounter.class);
|
||||
|
||||
if (CommandCounter.IncreaseCounter() == -1) {
|
||||
Messages.showMessageDialog(
|
||||
"The maximum number of opened projects exceeds " + String.valueOf(CommandCounter.MaxCount) +
|
||||
" projects!", "Error", Messages.getErrorIcon());
|
||||
ProjectManager PM = ProjectManager.getInstance();
|
||||
Project[] AllProjects = PM.getOpenProjects();
|
||||
Project project = AllProjects[AllProjects.length - 1];
|
||||
PM.closeProject(project);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void projectClosed() {
|
||||
// called when project is being closed
|
||||
MyCounter CommandCounter = ServiceManager.getService(MyCounter.class);
|
||||
CommandCounter.DecreaseCounter();
|
||||
}
|
||||
}
|
@ -1,32 +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;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: Alexey.Chursin
|
||||
* Date: Aug 13, 2010
|
||||
* Time: 3:49:33 PM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class MyCounter {
|
||||
private int Count = 0;
|
||||
// Sets the maximum allowed number of opened projects.
|
||||
public final int MaxCount = 3;
|
||||
|
||||
public MyCounter() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
public int IncreaseCounter() {
|
||||
Count++;
|
||||
return (Count > MaxCount) ? -1 : Count;
|
||||
}
|
||||
|
||||
public int DecreaseCounter() {
|
||||
Count--;
|
||||
return (Count > MaxCount) ? -1 : Count;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
// 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.project.Project;
|
||||
import com.intellij.openapi.project.ProjectManager;
|
||||
|
||||
/**
|
||||
* Application service implementation to keep a running count of
|
||||
* how many projects are open at a given time.
|
||||
*/
|
||||
public class ProjectCountingService {
|
||||
// Sets the maximum allowed number of opened projects.
|
||||
private final int MAX_OPEN_PRJ_LIMIT = 3;
|
||||
|
||||
public boolean projLimitExceeded() {
|
||||
ProjectManager prjMgr = ProjectManager.getInstance();
|
||||
return prjMgr.getOpenProjects().length > MAX_OPEN_PRJ_LIMIT;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -8,11 +8,10 @@
|
||||
<!-- Text to display as name on Preferences/Settings | Plugin page -->
|
||||
<name>SDK: Maximum Open Projects Sample</name>
|
||||
|
||||
<!-- The version of this plugin -->
|
||||
<version>2.0.0</version>
|
||||
<!-- The version of this plugin is set by build.gradle -->
|
||||
|
||||
<!-- Compatible with the following versions of IntelliJ Platform -->
|
||||
<idea-version since-build="191"/>
|
||||
<!-- The idea-version of this plugin is set by build.gradle -->
|
||||
<idea-version since-build="193"/>
|
||||
|
||||
<!-- Product and plugin compatibility requirements -->
|
||||
<depends>com.intellij.modules.platform</depends>
|
||||
@ -20,12 +19,13 @@
|
||||
<!-- Text to display as description on Preferences/Settings | Plugin page -->
|
||||
<description>
|
||||
<![CDATA[
|
||||
Demonstrates adding project and application components. Shows warning dialog when more than 3 open projects is exceeded.
|
||||
Demonstrates adding application services and listeners. Shows warning dialog when more than 3 open projects is exceeded.
|
||||
]]>
|
||||
</description>
|
||||
<change-notes>
|
||||
<![CDATA[
|
||||
<ul>
|
||||
<li><b>2.1.0</b> Remove project component and use listener instead.</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>
|
||||
@ -35,14 +35,11 @@
|
||||
<!-- Text to display as company information on Preferences/Settings | Plugin page -->
|
||||
<vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor>
|
||||
|
||||
<project-components>
|
||||
<component>
|
||||
<implementation-class>org.intellij.sdk.maxOpenPrj.MaxProject</implementation-class>
|
||||
</component>
|
||||
</project-components>
|
||||
<applicationListeners>
|
||||
<listener class="org.intellij.sdk.maxOpenPrj.ProjectOpenListener" topic="com.intellij.openapi.project.ProjectManagerListener"/>
|
||||
</applicationListeners>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<applicationService serviceInterface="org.intellij.sdk.maxOpenPrj.MyCounter"
|
||||
serviceImplementation="org.intellij.sdk.maxOpenPrj.MyCounter"/>
|
||||
<applicationService serviceImplementation="org.intellij.sdk.maxOpenPrj.ProjectCountingService"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
Loading…
x
Reference in New Issue
Block a user