From 60e9aabd9a953255eab64bbf2346a65842c104b8 Mon Sep 17 00:00:00 2001 From: Yevheniy Oliynyk Date: Wed, 30 Oct 2024 09:52:29 +0100 Subject: [PATCH] feat: bundle settings button, url fixes --- src/main/java/com/crowdin/Constants.java | 1 + .../crowdin/action/BundleSettingsAction.java | 52 +++++++++++++++++ src/main/java/com/crowdin/client/Crowdin.java | 16 ++---- .../com/crowdin/client/CrowdinClient.java | 4 +- .../ui/panel/download/DownloadWindow.java | 57 +++++++++++++++---- .../panel/download/action/RefreshAction.java | 5 +- src/main/resources/META-INF/plugin.xml | 1 + .../java/com/crowdin/client/MockCrowdin.java | 10 ++-- 8 files changed, 115 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/crowdin/action/BundleSettingsAction.java diff --git a/src/main/java/com/crowdin/Constants.java b/src/main/java/com/crowdin/Constants.java index 621f10e..d5402dd 100644 --- a/src/main/java/com/crowdin/Constants.java +++ b/src/main/java/com/crowdin/Constants.java @@ -22,5 +22,6 @@ public final class Constants { public static final String DOWNLOAD_REFRESH_ACTION = "Crowdin.RefreshDownloadAction"; public static final String DOWNLOAD_TRANSLATIONS_ACTION = "Crowdin.DownloadTranslations"; public static final String DOWNLOAD_SOURCES_ACTION = "Crowdin.DownloadSources"; + public static final String BUNDLE_SETTINGS_ACTION = "Crowdin.BundleSettings"; public static final String PROGRESS_GROUP_FILES_BY_FILE_ACTION = "Crowdin.GroupByFiles"; } diff --git a/src/main/java/com/crowdin/action/BundleSettingsAction.java b/src/main/java/com/crowdin/action/BundleSettingsAction.java new file mode 100644 index 0000000..a29f4b9 --- /dev/null +++ b/src/main/java/com/crowdin/action/BundleSettingsAction.java @@ -0,0 +1,52 @@ +package com.crowdin.action; + +import com.crowdin.service.ProjectService; +import com.crowdin.ui.panel.download.DownloadWindow; +import com.intellij.icons.AllIcons; +import com.intellij.ide.BrowserUtil; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; + +import static com.crowdin.Constants.TOOLWINDOW_ID; + +public class BundleSettingsAction extends AnAction { + + private boolean enabled = false; + private boolean visible = false; + private String text = ""; + + public BundleSettingsAction() { + super("Bundle Settings", "Bundle Settings", AllIcons.General.Settings); + } + + @Override + public void update(@NotNull AnActionEvent e) { + if (e.getPlace().equals(TOOLWINDOW_ID)) { + this.enabled = e.getPresentation().isEnabled(); + this.visible = e.getPresentation().isVisible(); + this.text = e.getPresentation().getText(); + } + e.getPresentation().setEnabled(enabled); + e.getPresentation().setVisible(visible); + e.getPresentation().setText(text); + } + + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + if (project == null) { + return; + } + + DownloadWindow window = project.getService(ProjectService.class).getDownloadWindow(); + if (window == null || window.getSelectedBundle() == null) { + return; + } + + String link = window.buildBundleUrl(); + BrowserUtil.browse(link); + } + +} diff --git a/src/main/java/com/crowdin/client/Crowdin.java b/src/main/java/com/crowdin/client/Crowdin.java index 2eaa4c6..bc16932 100644 --- a/src/main/java/com/crowdin/client/Crowdin.java +++ b/src/main/java/com/crowdin/client/Crowdin.java @@ -8,7 +8,6 @@ import com.crowdin.client.labels.model.AddLabelRequest; import com.crowdin.client.labels.model.Label; import com.crowdin.client.languages.model.Language; -import com.crowdin.client.projectsgroups.model.Project; import com.crowdin.client.sourcefiles.model.*; import com.crowdin.client.sourcestrings.model.SourceString; import com.crowdin.client.sourcestrings.model.UploadStringsProgress; @@ -61,6 +60,11 @@ public Long getProjectId() { return this.projectId; } + @Override + public String getBaseUrl() { + return this.baseUrl; + } + @Override public Long addStorage(String fileName, InputStream content) { return executeRequest(() -> this.client.getStorageApi() @@ -344,16 +348,6 @@ public List getBundles() { .collect(Collectors.toList()); } - @Override - public String getBundlesUrl(Project project) { - if (this.baseUrl != null) { - String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/"; - return base + "u/projects/" + project.getId() + "/translations#bundles"; - } else { - return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles"; - } - } - private boolean concurrentIssue(Exception error) { return this.codeExists(error, "notUnique") || this.codeExists(error, "parallelCreation"); } diff --git a/src/main/java/com/crowdin/client/CrowdinClient.java b/src/main/java/com/crowdin/client/CrowdinClient.java index d75eeb5..43584de 100644 --- a/src/main/java/com/crowdin/client/CrowdinClient.java +++ b/src/main/java/com/crowdin/client/CrowdinClient.java @@ -38,6 +38,8 @@ public interface CrowdinClient { Long getProjectId(); + String getBaseUrl(); + Long addStorage(String fileName, InputStream content); void updateSource(Long sourceId, UpdateFileRequest request); @@ -99,6 +101,4 @@ public interface CrowdinClient { Label addLabel(AddLabelRequest request); List getBundles(); - - String getBundlesUrl(Project project); } diff --git a/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java b/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java index 9177edd..72f5aba 100644 --- a/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java +++ b/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java @@ -1,6 +1,7 @@ package com.crowdin.ui.panel.download; import com.crowdin.client.bundles.model.Bundle; +import com.crowdin.client.projectsgroups.model.Project; import com.crowdin.ui.panel.ContentTab; import com.crowdin.ui.panel.CrowdinPanelWindowFactory; import com.crowdin.ui.tree.CellData; @@ -24,8 +25,7 @@ import java.util.List; import java.util.Optional; -import static com.crowdin.Constants.DOWNLOAD_SOURCES_ACTION; -import static com.crowdin.Constants.DOWNLOAD_TRANSLATIONS_ACTION; +import static com.crowdin.Constants.*; public class DownloadWindow implements ContentTab { @@ -38,7 +38,8 @@ public class DownloadWindow implements ContentTab { private final JPanel placeholderPanel = new JPanel(new BorderLayout()); - private String link; + private String baseUrl; + private Project project; public DownloadWindow() { this.placeholder.setComponentStyle(UIUtil.ComponentStyle.LARGE); @@ -61,6 +62,7 @@ public DownloadWindow() { this.placeholder2.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { + var link = buildBundleUrl(); if (link != null) { BrowserUtil.browse(link); } @@ -86,8 +88,10 @@ public void mousePressed(MouseEvent e) { if (cell.isBundle()) { CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_TRANSLATIONS_ACTION, "Download bundle", true, true); + CrowdinPanelWindowFactory.updateToolbar(BUNDLE_SETTINGS_ACTION, "Bundle settings", true, true); } else { CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_TRANSLATIONS_ACTION, "Select bundle to download", true, false); + CrowdinPanelWindowFactory.updateToolbar(BUNDLE_SETTINGS_ACTION, "Select bundle to view settings", true, false); } }); } @@ -98,15 +102,18 @@ public JPanel getContent() { } public Bundle getSelectedBundle() { - return CellRenderer.getData(this.selectedElement).getBundle(); + return Optional.ofNullable(this.selectedElement) + .map(e -> CellRenderer.getData(e).getBundle()) + .orElse(null); } public List getSelectedFiles() { return FileTree.getFiles(this.selectedElement); } - public void rebuildFileTree(String projectName, List files) { - this.link = null; + public void rebuildFileTree(Project project, String baseUrl, List files) { + this.baseUrl = baseUrl; + this.project = project; this.isBundlesMode = false; this.selectedElement = null; @@ -115,6 +122,9 @@ public void rebuildFileTree(String projectName, List files) { this.placeholder.setText("No files found matching your configuration"); this.placeholder2.setVisible(false); this.placeholderPanel.setVisible(true); + CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_SOURCES_ACTION, "Download Sources", true, false); + CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_TRANSLATIONS_ACTION, "Download Translations", true, false); + CrowdinPanelWindowFactory.updateToolbar(BUNDLE_SETTINGS_ACTION, "", false, false); return; } @@ -123,12 +133,14 @@ public void rebuildFileTree(String projectName, List files) { CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_SOURCES_ACTION, "Download Sources", true, true); CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_TRANSLATIONS_ACTION, "Download Translations", true, true); - this.tree.setModel(new DefaultTreeModel(FileTree.buildTree(projectName, files))); + CrowdinPanelWindowFactory.updateToolbar(BUNDLE_SETTINGS_ACTION, "", false, false); + this.tree.setModel(new DefaultTreeModel(FileTree.buildTree(this.project.getName(), files))); FileTree.expandAll(tree); } - public void rebuildBundlesTree(String projectName, List bundles, String bundleInfoUrl) { - this.link = bundleInfoUrl; + public void rebuildBundlesTree(Project project, String baseUrl, List bundles) { + this.project = project; + this.baseUrl = baseUrl; this.isBundlesMode = true; this.selectedElement = null; @@ -147,8 +159,9 @@ public void rebuildBundlesTree(String projectName, List bundles, String CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_SOURCES_ACTION, "", false, false); CrowdinPanelWindowFactory.updateToolbar(DOWNLOAD_TRANSLATIONS_ACTION, "Select bundle to download", true, false); + CrowdinPanelWindowFactory.updateToolbar(BUNDLE_SETTINGS_ACTION, "Select bundle to view settings", true, false); - DefaultMutableTreeNode root = new DefaultMutableTreeNode(CellData.root(projectName)); + DefaultMutableTreeNode root = new DefaultMutableTreeNode(CellData.root(this.project.getName())); bundles.forEach(bundle -> root.add(new DefaultMutableTreeNode(CellData.bundle(bundle)))); this.tree.setModel(new DefaultTreeModel(root)); expandAll(); @@ -161,4 +174,28 @@ public void expandAll() { public void collapseAll() { FileTree.collapseAll(this.tree); } + + public String buildBundleUrl() { + if (!this.isBundlesMode) { + return null; + } + + var bundle = this.getSelectedBundle(); + + if (bundle == null) { + if (this.baseUrl != null) { + String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/"; + return base + "u/projects/" + project.getId() + "/download"; + } else { + return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles"; + } + } + + if (this.baseUrl != null) { + String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/"; + return base + "u/projects/" + project.getId() + "/translations/bundle/" + bundle.getId(); + } else { + return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles:" + bundle.getId(); + } + } } diff --git a/src/main/java/com/crowdin/ui/panel/download/action/RefreshAction.java b/src/main/java/com/crowdin/ui/panel/download/action/RefreshAction.java index 33de59d..b2571cd 100644 --- a/src/main/java/com/crowdin/ui/panel/download/action/RefreshAction.java +++ b/src/main/java/com/crowdin/ui/panel/download/action/RefreshAction.java @@ -64,9 +64,8 @@ protected void performInBackground(@NotNull AnActionEvent e, @NotNull ProgressIn } if (context.get().crowdinProjectCache.isStringsBased()) { - String url = context.get().crowdin.getBundlesUrl(context.get().crowdinProjectCache.getProject()); ApplicationManager.getApplication() - .invokeAndWait(() -> window.rebuildBundlesTree(context.get().crowdinProjectCache.getProject().getName(), context.get().crowdinProjectCache.getBundles(), url)); + .invokeAndWait(() -> window.rebuildBundlesTree(context.get().crowdinProjectCache.getProject(), context.get().crowdin.getBaseUrl(), context.get().crowdinProjectCache.getBundles())); return; } @@ -91,7 +90,7 @@ protected void performInBackground(@NotNull AnActionEvent e, @NotNull ProgressIn } ApplicationManager.getApplication() - .invokeAndWait(() -> window.rebuildFileTree(context.get().crowdinProjectCache.getProject().getName(), files)); + .invokeAndWait(() -> window.rebuildFileTree(context.get().crowdinProjectCache.getProject(), context.get().crowdin.getBaseUrl(), files)); } catch (ProcessCanceledException ex) { throw ex; } catch (Exception ex) { diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index b59d351..16f8772 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -65,6 +65,7 @@ + diff --git a/src/test/java/com/crowdin/client/MockCrowdin.java b/src/test/java/com/crowdin/client/MockCrowdin.java index e80e042..64acc51 100644 --- a/src/test/java/com/crowdin/client/MockCrowdin.java +++ b/src/test/java/com/crowdin/client/MockCrowdin.java @@ -64,6 +64,11 @@ public Long getProjectId() { return project != null ? project.getId() : null; } + @Override + public String getBaseUrl() { + return null; + } + @Override public Long addStorage(String fileName, InputStream content) { return null; @@ -220,9 +225,4 @@ public Label addLabel(AddLabelRequest request) { public List getBundles() { return null; } - - @Override - public String getBundlesUrl(Project project) { - return null; - } }