Skip to content

Commit fe99a0a

Browse files
authored
Add command: java.runtime (#172)
* Add command: java.runtime Signed-off-by: Rome Li <[email protected]> * Add CSP Signed-off-by: Rome Li <[email protected]>
1 parent 9f15b6b commit fe99a0a

File tree

4 files changed

+185
-2
lines changed

4 files changed

+185
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"command": "java.runtime",
56-
"title": "Java: Runtime"
56+
"title": "Java: Configure Java Runtime"
5757
}
5858
]
5959
},

src/java-runtime/assets/index.html

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="Content-Security-Policy" content="default-src 'unsafe-inline'">
7+
<title>Configure Java Runtime</title>
8+
</head>
9+
10+
<body>
11+
<div class="container mt-5 mb-5">
12+
<div class="row mb-3 d-none">
13+
<div class="col">
14+
<h1>Configure Java Runtime</h1>
15+
</div>
16+
</div>
17+
<div class="row mb-3" id="javaRuntimePanel">
18+
<div class="col">
19+
<div class="card">
20+
<div class="card-body">
21+
<h4 class="alert-heading">Java Development Kit Required</h4>
22+
<p>
23+
Java Development Kit (JDK) 8 or later is required for developing Java applications.
24+
</p>
25+
<p>
26+
⚠️ No JDK installation was detected. Please follow the links below to download and install JDK:
27+
</p>
28+
<ul class="nav nav-tabs mb-3" id="jdkSourceTab" role="tablist">
29+
<li class="nav-item">
30+
<a class="nav-link active" id="adoptOpenJdkTab" data-toggle="tab" href="#adoptOpenJdkPanel" role="tab" aria-controls="adoptOpenJdkPanel" aria-selected="true" title="Reliable source of OpenJDK binaries for all platforms">AdoptOpenJDK</a>
31+
</li>
32+
<li class="nav-item">
33+
<a class="nav-link" id="otherJdkTab" data-toggle="tab" href="#otherJdkPanel" role="tab" aria-controls="otherJdkPanel" aria-selected="false" title="Other choices">Others</a>
34+
</li>
35+
</ul>
36+
<div class="tab-content" id="myTabContent">
37+
<div class="tab-pane fade show active" id="adoptOpenJdkPanel" role="tabpanel" aria-labelledby="adoptOpenJdkTab">
38+
<form>
39+
<div class="form-row align-items-center">
40+
<div class="form-group col mb-1">
41+
<h6 class="text-capitalize mb-2">
42+
Download for
43+
<span id="jdkOs">windows</span>
44+
<span id="jdkArch">x64</span>
45+
</h6>
46+
</div>
47+
</div>
48+
<div class="form-row">
49+
<div class="form-group col-sm-6">
50+
<label>JDK Version:</label>
51+
<div class="form-check">
52+
<input class="form-check-input" type="radio" name="jdkVersion" id="openjdk8Radio" value="openjdk8">
53+
<label class="form-check-label" for="openjdk8Radio">
54+
OpenJDK 8
55+
</label>
56+
</div>
57+
<div class="form-check">
58+
<input class="form-check-input" type="radio" name="jdkVersion" id="openjdk11Radio" value="openjdk11" checked>
59+
<label class="form-check-label" for="openjdk11Radio">
60+
OpenJDK 11
61+
</label>
62+
</div>
63+
<div class="form-check">
64+
<input class="form-check-input" type="radio" name="jdkVersion" id="openjdk12Radio" value="openjdk12">
65+
<label class="form-check-label" for="openjdk12Radio">
66+
OpenJDK 12 (Technical Preview)
67+
</label>
68+
</div>
69+
</div>
70+
<div class="form-group col-sm-6">
71+
<label>JVM:</label>
72+
<div class="form-check">
73+
<input class="form-check-input" type="radio" name="jvmImpl" id="hotspotRadio" value="hotspot" checked>
74+
<label class="form-check-label" for="hotspotRadio">
75+
Hotspot (Recommended)
76+
</label>
77+
</div>
78+
<div class="form-check">
79+
<input class="form-check-input" type="radio" name="jvmImpl" id="openj9Radio" value="openj9">
80+
<label class="form-check-label" for="openj9Radio">
81+
OpenJ9
82+
</label>
83+
</div>
84+
</div>
85+
</div>
86+
<div class="form-row">
87+
<div class="form-group col-sm-12">
88+
<div class="spinner-border spinner-border-sm" role="status" id="jdkSpinner">
89+
<span class="sr-only">Loading...</span>
90+
</div>
91+
<a class="btn btn-primary btn-lg d-none" id="jdkDownloadLink" href="command:java.helper.openUrl" role="button" title="Download JDK">
92+
Download
93+
<br>
94+
<sub id="jdkReleaseName">release</sub><sub> | </sub><sub id="jdkDownloadSize">size</sub>
95+
</a>
96+
</div>
97+
</div>
98+
</form>
99+
</div>
100+
<div class="tab-pane fade" id="otherJdkPanel" role="tabpanel" aria-labelledby="otherJdkTab">
101+
<ul id="jdkProviderList" class="list-unstyled">
102+
<li><a href="command:java.helper.openUrl?%22https%3A%2F%2Fwww.azul.com%2Fdownloads%2Fazure-only%2Fzulu%2F%22" title="Recommended for Microsoft Azure Cloud and Azure Stack applications">Azul Zulu Enterprise build of OpenJDK for Azure</a></li>
103+
<li><a href="command:java.helper.openUrl?%22https%3A%2F%2Fdevelopers.redhat.com%2Fproducts%2Fopenjdk%2Fdownload%22" title="Red Hat build of OpenJDK">Red Hat build of OpenJDK</a></li>
104+
<li><a href="command:java.helper.openUrl?%22https%3A%2F%2Fjdk.java.net%2F%22" title="OpenJDK by Oracle">Oracle OpenJDK</a></li>
105+
</ul>
106+
</div>
107+
</div>
108+
<p>When you are finished, please reload Visual Studio Code to activate Java language server.</p>
109+
<p class="mb-0">
110+
<a class="btn btn-primary" href="command:workbench.action.reloadWindow" role="button" title="Reload Visual Studio Code">Reload Window</a>
111+
<a class="btn btn-secondary" href="command:java.helper.openUrl?%22https%3A%2F%2Fgithub.com%2Fredhat-developer%2Fvscode-java%23setting-the-jdk%22" role="button" title="More info on how JDK is resolved">Having Trouble?</a>
112+
</p>
113+
</div>
114+
</div>
115+
</div>
116+
</div>
117+
</div>
118+
</body>
119+
120+
</html>

src/java-runtime/assets/index.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import $ = require("jquery");
5+
import "../../assets/vscode.scss";
6+
import "bootstrap/js/src/tab";
7+
import bytes = require("bytes");
8+
9+
window.addEventListener("message", event => {
10+
if (event.data.command === "showJavaRuntimePanel") {
11+
$("#javaRuntimePanel").removeClass("d-none");
12+
} else if (event.data.command === "applyJdkInfo") {
13+
applyJdkInfo(event.data.jdkInfo);
14+
}
15+
});
16+
17+
function applyJdkInfo(jdkInfo: any) {
18+
let binary = jdkInfo.binaries[0];
19+
let downloadLink = binary.installer_link || binary.binary_link;
20+
$("#jdkOs").text(binary.os);
21+
$("#jdkArch").text(binary.architecture);
22+
$("#jdkReleaseName").text(jdkInfo.release_name);
23+
$("#jdkDownloadSize").text(bytes(binary.binary_size, {unitSeparator: " "}));
24+
25+
let encodedLink = `command:java.helper.openUrl?${encodeURIComponent(JSON.stringify(downloadLink))}`;
26+
$("#jdkDownloadLink").attr("href", encodedLink);
27+
28+
$("#jdkSpinner").addClass("d-none");
29+
$("#jdkDownloadLink").removeClass("d-none");
30+
}
31+
32+
declare function acquireVsCodeApi(): any;
33+
const vscode = acquireVsCodeApi();
34+
35+
function requestJdkInfo(jdkVersion: string, jvmImpl: string) {
36+
vscode.postMessage({
37+
command: "requestJdkInfo",
38+
jdkVersion: jdkVersion,
39+
jvmImpl: jvmImpl
40+
});
41+
}
42+
43+
$("input[type=radio]").change(() => {
44+
$("#jdkSpinner").removeClass("d-none");
45+
$("#jdkDownloadLink").addClass("d-none");
46+
requestJdkInfo($("input[name=jdkVersion]:checked").val() + "", $("input[name=jvmImpl]:checked").val() + "");
47+
});

src/java-runtime/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function javaRuntimeCmdHandler(context: vscode.ExtensionContext, op
1919
return;
2020
}
2121

22-
javaRuntimeView = vscode.window.createWebviewPanel("java.runtime", "Java Runtime", {
22+
javaRuntimeView = vscode.window.createWebviewPanel("java.runtime", "Configure Java Runtime", {
2323
viewColumn: vscode.ViewColumn.One,
2424
}, {
2525
enableScripts: true,
@@ -40,6 +40,22 @@ async function initializeJavaRuntimeView(context: vscode.ExtensionContext, webvi
4040
webviewPanel.webview.html = await loadTextFromFile(resourceUri);
4141

4242
context.subscriptions.push(webviewPanel.onDidDispose(onDisposeCallback));
43+
context.subscriptions.push(webviewPanel.webview.onDidReceiveMessage(async (e) => {
44+
if (e.command === "requestJdkInfo") {
45+
let jdkInfo = await suggestOpenJdk(e.jdkVersion, e.jvmImpl);
46+
applyJdkInfo(jdkInfo);
47+
}
48+
}));
49+
50+
function applyJdkInfo(jdkInfo: any) {
51+
webviewPanel.webview.postMessage({
52+
command: "applyJdkInfo",
53+
jdkInfo: jdkInfo
54+
});
55+
}
56+
57+
let jdkInfo = await suggestOpenJdk();
58+
applyJdkInfo(jdkInfo);
4359
}
4460

4561
export class JavaRuntimeViewSerializer implements vscode.WebviewPanelSerializer {

0 commit comments

Comments
 (0)