Skip to content

Commit

Permalink
feat: bundle settings button, url fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Oct 30, 2024
1 parent dca16bb commit 60e9aab
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/crowdin/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
52 changes: 52 additions & 0 deletions src/main/java/com/crowdin/action/BundleSettingsAction.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
16 changes: 5 additions & 11 deletions src/main/java/com/crowdin/client/Crowdin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -344,16 +348,6 @@ public List<Bundle> 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");
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/client/CrowdinClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface CrowdinClient {

Long getProjectId();

String getBaseUrl();

Long addStorage(String fileName, InputStream content);

void updateSource(Long sourceId, UpdateFileRequest request);
Expand Down Expand Up @@ -99,6 +101,4 @@ public interface CrowdinClient {
Label addLabel(AddLabelRequest request);

List<Bundle> getBundles();

String getBundlesUrl(Project project);
}
57 changes: 47 additions & 10 deletions src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
});
}
Expand All @@ -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<String> getSelectedFiles() {
return FileTree.getFiles(this.selectedElement);
}

public void rebuildFileTree(String projectName, List<String> files) {
this.link = null;
public void rebuildFileTree(Project project, String baseUrl, List<String> files) {
this.baseUrl = baseUrl;
this.project = project;
this.isBundlesMode = false;
this.selectedElement = null;

Expand All @@ -115,6 +122,9 @@ public void rebuildFileTree(String projectName, List<String> 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;
}

Expand All @@ -123,12 +133,14 @@ public void rebuildFileTree(String projectName, List<String> 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<Bundle> bundles, String bundleInfoUrl) {
this.link = bundleInfoUrl;
public void rebuildBundlesTree(Project project, String baseUrl, List<Bundle> bundles) {
this.project = project;
this.baseUrl = baseUrl;
this.isBundlesMode = true;
this.selectedElement = null;

Expand All @@ -147,8 +159,9 @@ public void rebuildBundlesTree(String projectName, List<Bundle> 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();
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<group id="Crowdin.DownloadToolbar">
<action id="Crowdin.DownloadSources" icon="/icons/download-sources.svg" class="com.crowdin.action.DownloadSourcesAction" text="Download Sources" />
<action id="Crowdin.DownloadTranslations" icon="/icons/download.svg" class="com.crowdin.action.DownloadAction" text="Download Translations" />
<action id="Crowdin.BundleSettings" class="com.crowdin.action.BundleSettingsAction" text="Bundle Settings" />
<separator/>
<action id="Crowdin.RefreshDownloadAction" text="Refresh" class="com.crowdin.ui.panel.download.action.RefreshAction"/>
<action id="Crowdin.ExpandDownloadTree" text="Expand" class="com.crowdin.ui.panel.download.action.ExpandAction"/>
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/crowdin/client/MockCrowdin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,9 +225,4 @@ public Label addLabel(AddLabelRequest request) {
public List<Bundle> getBundles() {
return null;
}

@Override
public String getBundlesUrl(Project project) {
return null;
}
}

0 comments on commit 60e9aab

Please sign in to comment.