Skip to content

Commit 6e04fb6

Browse files
support customized dependency code snippet in metadata and ui fix.
1 parent f440bcb commit 6e04fb6

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/azure-sdk-reference-book/src/main/java/com/microsoft/azure/toolkit/intellij/azuresdk/model/AzureSdkArtifactEntity.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,58 @@
1111
import lombok.Setter;
1212

1313
import java.util.Map;
14+
import java.util.Objects;
1415

1516
@Getter
1617
@Setter
1718
@NoArgsConstructor
1819
@JsonIgnoreProperties(ignoreUnknown = true)
1920
public class AzureSdkArtifactEntity {
20-
public static final String DEPENDENCY_TYPE_MAVEN = "Maven";
21-
public static final String DEPENDENCY_TYPE_GRADLE = "Gradle";
22-
2321
private String artifactId;
2422
private String groupId;
2523
private String versionGA;
2624
private String versionPreview;
2725
private String type;
2826
private Map<String, String> links;
27+
private Map<String, String> dependencyPattern;
28+
29+
public String getDependencySnippet(DependencyType type, String version) {
30+
final String strType = type.getName().toLowerCase();
31+
if (Objects.nonNull(dependencyPattern) && dependencyPattern.containsKey(strType)) {
32+
return dependencyPattern.get(strType);
33+
}
34+
return getDefaultDependencySnippet(type, version);
35+
}
2936

30-
public String generateDependencySnippet(String type, String version) {
31-
if (DEPENDENCY_TYPE_GRADLE.equals(type)) {
37+
private String getDefaultDependencySnippet(final DependencyType type, final String version) {
38+
if (DependencyType.GRADLE == type) {
3239
return String.format("implementation '%s:%s:%s'", this.groupId, this.artifactId, version);
3340
}
3441
return String.join("",
35-
"<dependency>\n",
36-
" <groupId>", this.groupId, "</groupId>\n",
37-
" <artifactId>", this.artifactId, "</artifactId>\n",
38-
" <version>", version, "</version>\n",
39-
"</dependency>"
42+
"<dependency>\n",
43+
" <groupId>", this.groupId, "</groupId>\n",
44+
" <artifactId>", this.artifactId, "</artifactId>\n",
45+
" <version>", version, "</version>\n",
46+
"</dependency>"
4047
);
4148
}
4249

43-
public String getLink(String type) {
44-
return this.links.get(type);
45-
}
46-
4750
public static class Type {
4851
public static final String SPRING = "spring";
4952
public static final String CLIENT = "client";
5053
public static final String MANAGEMENT = "mgmt";
5154
}
55+
56+
@Getter
57+
public enum DependencyType {
58+
MAVEN("Maven", "xml"), GRADLE("Gradle", "gradle");
59+
60+
private final String name;
61+
private final String fileExt;
62+
63+
DependencyType(final String name, final String fileExt) {
64+
this.name = name;
65+
this.fileExt = fileExt;
66+
}
67+
}
5268
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-sdk-reference-book/src/main/java/com/microsoft/azure/toolkit/intellij/azuresdk/referencebook/AzureSdkArtifactGroupPanel.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
import com.intellij.openapi.actionSystem.DefaultActionGroup;
1515
import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl;
1616
import com.intellij.openapi.editor.impl.DocumentImpl;
17+
import com.intellij.openapi.fileTypes.FileType;
18+
import com.intellij.openapi.fileTypes.ex.FileTypeManagerEx;
1719
import com.intellij.openapi.ide.CopyPasteManager;
1820
import com.intellij.openapi.project.Project;
1921
import com.intellij.openapi.project.ProjectManager;
2022
import com.intellij.ui.EditorTextField;
2123
import com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkArtifactEntity;
24+
import com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkArtifactEntity.DependencyType;
2225
import icons.GradleIcons;
2326
import icons.OpenapiIcons;
2427
import lombok.Getter;
@@ -32,6 +35,9 @@
3235
import java.util.List;
3336
import java.util.function.Consumer;
3437

38+
import static com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkArtifactEntity.DependencyType.GRADLE;
39+
import static com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkArtifactEntity.DependencyType.MAVEN;
40+
3541
public class AzureSdkArtifactGroupPanel {
3642
@Getter
3743
private JPanel contentPanel;
@@ -42,7 +48,7 @@ public class AzureSdkArtifactGroupPanel {
4248
private final List<AzureSdkArtifactDetailPanel> artifactPnls = new ArrayList<>();
4349
private AzureSdkArtifactEntity pkg;
4450
private String version;
45-
private String type;
51+
private DependencyType type = MAVEN;
4652

4753
public void setData(@Nonnull final List<? extends AzureSdkArtifactEntity> artifacts) {
4854
this.clear();
@@ -66,12 +72,13 @@ private void clear() {
6672
private void onPackageOrVersionSelected(AzureSdkArtifactEntity pkg, String version) {
6773
this.pkg = pkg;
6874
this.version = version;
69-
this.viewer.setText(pkg.generateDependencySnippet(type, version));
75+
this.viewer.setText(pkg.getDependencySnippet(type, version));
7076
}
7177

72-
private void onDependencyTypeSelected(String type) {
78+
private void onDependencyTypeSelected(DependencyType type) {
7379
this.type = type;
74-
this.viewer.setText(pkg.generateDependencySnippet(type, version));
80+
final FileType fileType = FileTypeManagerEx.getInstance().getFileTypeByExtension(type.getFileExt());
81+
this.viewer.setNewDocumentAndFileType(fileType, new DocumentImpl(pkg.getDependencySnippet(type, version)));
7582
}
7683

7784
private EditorTextField buildCodeViewer() {
@@ -130,34 +137,34 @@ private void createUIComponents() {
130137
* @see com.intellij.application.options.schemes.AbstractSchemesPanel
131138
*/
132139
private static class DependencyTypeSelector extends DefaultActionGroup {
133-
private final Consumer<? super String> onTypeSelected;
134-
private String selectedType;
140+
private final Consumer<? super DependencyType> onTypeSelected;
141+
private DependencyType selectedType;
135142

136-
private DependencyTypeSelector(Consumer<? super String> onTypeSelected) {
143+
private DependencyTypeSelector(Consumer<? super DependencyType> onTypeSelected) {
137144
super();
138145
setPopup(true);
139146
this.onTypeSelected = onTypeSelected;
140-
final AnAction maven = createAction(AzureSdkArtifactEntity.DEPENDENCY_TYPE_MAVEN, OpenapiIcons.RepositoryLibraryLogo, this::setSelectedType);
141-
final AnAction gradle = createAction(AzureSdkArtifactEntity.DEPENDENCY_TYPE_GRADLE, GradleIcons.Gradle, this::setSelectedType);
147+
final AnAction maven = createAction(MAVEN.getName(), OpenapiIcons.RepositoryLibraryLogo, () -> this.setSelectedType(MAVEN));
148+
final AnAction gradle = createAction(GRADLE.getName(), GradleIcons.Gradle, () -> this.setSelectedType(GRADLE));
142149
this.addAll(maven, gradle);
143150
}
144151

145-
private void setSelectedType(String type) {
152+
private void setSelectedType(DependencyType type) {
146153
this.selectedType = type;
147154
this.onTypeSelected.accept(type);
148155
}
149156

150157
@Override
151158
public void update(@NotNull final AnActionEvent e) {
152-
final Icon icon = AzureSdkArtifactEntity.DEPENDENCY_TYPE_GRADLE.equals(selectedType) ? GradleIcons.Gradle : OpenapiIcons.RepositoryLibraryLogo;
159+
final Icon icon = GRADLE == selectedType ? GradleIcons.Gradle : OpenapiIcons.RepositoryLibraryLogo;
153160
e.getPresentation().setIcon(icon);
154161
}
155162

156-
private AnAction createAction(final String type, final Icon icon, final Consumer<? super String> onTypeSelected) {
157-
return new AnAction(type, null, icon) {
163+
private AnAction createAction(final String name, final Icon icon, final Runnable onSelected) {
164+
return new AnAction(name, null, icon) {
158165
@Override
159166
public void actionPerformed(@NotNull final AnActionEvent e) {
160-
onTypeSelected.accept(type);
167+
onSelected.run();
161168
}
162169
};
163170
}

0 commit comments

Comments
 (0)