Skip to content

Commit

Permalink
Merge pull request #185 from couchbaselabs/threading_improvements
Browse files Browse the repository at this point in the history
refactoring swingutilities.invokelater
  • Loading branch information
deniswsrosa authored Mar 20, 2024
2 parents 6b3f483 + 7a338ef commit 4dc11d6
Show file tree
Hide file tree
Showing 35 changed files with 165 additions and 169 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ tasks {
}

patchPluginXml {
sinceBuild.set("230.*")
sinceBuild.set("232.*")
untilBuild.set("246.*")
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/couchbase/intellij/database/ActiveCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.couchbase.intellij.tree.overview.apis.ServerOverview;
import com.couchbase.intellij.utils.Subscribable;
import com.couchbase.intellij.workbench.Log;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.PerformInBackgroundOption;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
Expand All @@ -22,7 +23,6 @@
import org.jetbrains.annotations.VisibleForTesting;
import utils.CBConfigUtil;

import javax.swing.*;
import java.awt.*;
import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -173,7 +173,7 @@ public void run(@NotNull ProgressIndicator indicator) {
return;
}
Log.info("Disconnected from cluster " + savedCluster.getId());
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
try {
Messages.showErrorDialog(
String.format("Lost connection to cluster '%s'", savedCluster.getId()),
Expand Down Expand Up @@ -222,7 +222,7 @@ public void run(@NotNull ProgressIndicator indicator) {
});
INSTANCE.set(ActiveCluster.this);
} catch (Exception e) {
SwingUtilities.invokeLater(() -> Messages.showErrorDialog(
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(
String.format("Error while connecting to cluster '%s': \n %s", savedCluster.getId(), e.getMessage()),
"Failed to connect to cluster"
));
Expand Down Expand Up @@ -338,7 +338,7 @@ public Stream<? extends CouchbaseClusterEntity> getChild(String name) {
if (getChildren() == null) {
return Stream.empty();
}

return getChildren().stream()
.flatMap(b -> Stream.concat(
Stream.of(b),
Expand Down Expand Up @@ -383,7 +383,7 @@ public void run(@NotNull ProgressIndicator indicator) {
if (onComplete != null) {
onComplete.accept(e);
}
SwingUtilities.invokeLater(() -> Messages.showErrorDialog("Could not read cluster schema.", "Couchbase Connection Error"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog("Could not read cluster schema.", "Couchbase Connection Error"));
disconnect();
}
}
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/com/couchbase/intellij/database/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.jetbrains.annotations.Nullable;
import utils.IndexUtils;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.time.Duration;
Expand Down Expand Up @@ -88,7 +87,7 @@ public static void listBuckets(DefaultMutableTreeNode parentNode, Tree tree) {
parentNode.add(new DefaultMutableTreeNode(new NoResultsNodeDescriptor()));
}

SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(parentNode);
});

Expand Down Expand Up @@ -128,7 +127,7 @@ public static void listScopes(DefaultMutableTreeNode parentNode, Tree tree) {
parentNode.add(new DefaultMutableTreeNode(new NoResultsNodeDescriptor()));
}

SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(parentNode);
});
} catch (Exception e) {
Expand Down Expand Up @@ -169,7 +168,7 @@ public static void listCollections(DefaultMutableTreeNode parentNode, Tree tree)
} else {
parentNode.add(new DefaultMutableTreeNode(new NoResultsNodeDescriptor()));
}
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(parentNode);
});

Expand Down Expand Up @@ -244,7 +243,7 @@ public static void listDocuments(DefaultMutableTreeNode parentNode, Tree tree, i
} else if (newOffset == 0) {
parentNode.add(new DefaultMutableTreeNode(new NoResultsNodeDescriptor()));
}
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(parentNode);
});
}
Expand Down Expand Up @@ -296,7 +295,7 @@ private static void loadKVDocuments(DefaultMutableTreeNode parentNode, Tree tree
parentNode.add(new DefaultMutableTreeNode(new NoResultsNodeDescriptor()));
}

SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(parentNode);
});
}
Expand Down Expand Up @@ -340,18 +339,18 @@ public static void loadDocument(Project project, FileNodeDescriptor node, @Nulla
}

} catch (DocumentNotFoundException dnf) {
SwingUtilities.invokeLater(() -> Messages.showInfoMessage("<html>The document <strong>" + node.getId() + "</strong> doesn't exists anymore.</html>", "Couchbase Plugin Error"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("<html>The document <strong>" + node.getId() + "</strong> doesn't exists anymore.</html>", "Couchbase Plugin Error"));
return;

} catch (TimeoutException te) {
te.printStackTrace();
Log.error("Request to get the document " + node.getId() + " timed out.", te);
SwingUtilities.invokeLater(() -> Messages.showInfoMessage("<html>The request to get the document <strong>" + node.getId() + "</strong> timed out. Please try again or check your network connection.</html>", "Couchbase Plugin Error"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("<html>The request to get the document <strong>" + node.getId() + "</strong> timed out. Please try again or check your network connection.</html>", "Couchbase Plugin Error"));

return;
} catch (Exception e) {
Log.error("Could not load the document " + node.getId() + ".", e);
SwingUtilities.invokeLater(() -> Messages.showInfoMessage("<html>Could not load the document <strong>" + node.getId() + "</strong>. Please check the log for more.</html>", "Couchbase Plugin Error"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("<html>Could not load the document <strong>" + node.getId() + "</strong>. Please check the log for more.</html>", "Couchbase Plugin Error"));
return;
} finally {
if (tree != null) {
Expand Down Expand Up @@ -384,7 +383,7 @@ public static void loadDocument(Project project, FileNodeDescriptor node, @Nulla
tree.setPaintBusy(false);
}
Log.error("An error occurred while trying to load the file", e);
SwingUtilities.invokeLater(() -> Messages.showInfoMessage("<html>Could not load the document <strong>" + node.getId() + "</strong>. Please check the log for more.</html>", "Couchbase Plugin Error"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("<html>Could not load the document <strong>" + node.getId() + "</strong>. Please check the log for more.</html>", "Couchbase Plugin Error"));
}
}

Expand Down Expand Up @@ -414,7 +413,7 @@ public void run(@NotNull ProgressIndicator indicator) {
Log.debug("Could not infer the schema for " + colNode.getText());
}

SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
treeModel.nodeStructureChanged(parentNode);
});
}
Expand Down Expand Up @@ -532,7 +531,7 @@ public static void createPrimaryIndex(String bucket, String scope, String collec
try {
ActiveCluster.getInstance().get().bucket(bucket).scope(scope).collection(collection).queryIndexes().createPrimaryIndex();

SwingUtilities.invokeLater(() -> Messages.showInfoMessage("The primary index for the collection " + bucket + "." + scope + "." + collection + " was created successfully.", "Primary Index Creation"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("The primary index for the collection " + bucket + "." + scope + "." + collection + " was created successfully.", "Primary Index Creation"));
} catch (Exception e) {
Log.error(e);
e.printStackTrace();
Expand Down Expand Up @@ -563,7 +562,7 @@ public static void listIndexes(DefaultMutableTreeNode parentNode, Tree tree) {
} else {
parentNode.add(new DefaultMutableTreeNode(new NoResultsNodeDescriptor()));
}
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
((DefaultTreeModel) tree.getModel()).nodeStructureChanged(parentNode);
tree.setPaintBusy(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.couchbase.client.java.kv.GetResult;
import com.couchbase.intellij.database.ActiveCluster;
import com.intellij.json.JsonFileType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsSafe;
Expand All @@ -13,7 +14,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.*;
import java.util.Objects;

Expand Down Expand Up @@ -179,7 +179,7 @@ public void refresh(boolean asynchronous, boolean recursive, @Nullable Runnable
};

if (asynchronous) {
SwingUtilities.invokeLater(refresh);
ApplicationManager.getApplication().invokeLater(refresh);
} else {
refresh.run();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/couchbase/intellij/tools/DDLExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.couchbase.intellij.database.ActiveCluster;
import com.couchbase.intellij.database.DataLoader;
import com.couchbase.intellij.workbench.Log;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
Expand All @@ -14,7 +15,6 @@
import utils.IndexUtils;
import utils.TimeUtils;

import javax.swing.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
Expand Down Expand Up @@ -100,7 +100,7 @@ public void run(@NotNull ProgressIndicator indicator) {

Log.info("DDL file " + fullPath + " was exported successfully.");

SwingUtilities.invokeLater(() -> Messages.showInfoMessage("File exported successfully.", "DDL Export"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("File exported successfully.", "DDL Export"));

} catch (Exception e) {
Log.error("An error occurred while writing to export the DDL: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public void mouseEntered(MouseEvent e) {
}

public void tryToConnectToMongo(String mongoDBUri, JLabel errorLabel, Consumer<List<String>> callback) {
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
try {
List<String> databases = MongoConnection.testConnection(mongoDBUri);

Expand All @@ -500,7 +500,7 @@ public void tryToConnectToMongo(String mongoDBUri, JLabel errorLabel, Consumer<L
}

public void connectToMongo(String mongoDBUri, JLabel errorLabel, Consumer<List<String>> callback) {
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
try {
mongoConnection = new MongoConnection(mongoDBUri);
List<String> databases = mongoConnection.listDatabases();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.couchbase.intellij.tree.docfilter.DocumentFilterDialog;
import com.couchbase.intellij.workbench.Log;
import com.intellij.ide.ui.laf.darcula.ui.DarculaTextBorder;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
Expand Down Expand Up @@ -1244,7 +1245,7 @@ protected void readJsonOrCsvFile() {
if (!isJsonArray) {
boolean isLines = sampleJSONLines(datasetPath);
if (!isLines) {
SwingUtilities.invokeLater(() -> Messages.showInfoMessage("Your dateset is not in a JSON Array or JSON lines format", "Couchbase Plugin Error"));
ApplicationManager.getApplication().invokeLater(() -> Messages.showInfoMessage("Your dateset is not in a JSON Array or JSON lines format", "Couchbase Plugin Error"));
}
}
} else if (datasetPath.endsWith(CSV_FILE_EXTENSION)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.couchbase.intellij.persistence.SavedCluster;
import com.couchbase.intellij.tree.node.*;
import com.couchbase.intellij.workbench.Log;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -171,7 +172,7 @@ public void treeCollapsed(TreeExpansionEvent event) {
});
if (DataLoader.getSavedClusters() == null || DataLoader.getSavedClusters().isEmpty()) {
Timer timer = new Timer(10000, e -> {
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
try {
toolBarBuilder.showGotItTooltip();
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.couchbase.intellij.database.ActiveCluster;
import com.couchbase.intellij.tree.overview.apis.CouchbaseRestAPI;
import com.couchbase.intellij.tree.overview.apis.ServerOverview;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
Expand Down Expand Up @@ -470,7 +471,7 @@ private void readClusterInformation() {
}

this.pack();
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {

int locLeft = (int) Math.max(1, (Toolkit.getDefaultToolkit().getScreenSize().getWidth()) / 2 - getSize().getWidth() / 2);
int locTop = (int) Math.max(1, (Toolkit.getDefaultToolkit().getScreenSize().getHeight()) / 2 - getSize().getHeight() / 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.couchbase.intellij.tree.node.ConnectionNodeDescriptor;
import com.couchbase.intellij.tree.overview.NewConnectionBanner;
import com.couchbase.intellij.workbench.Log;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.IconLoader;
Expand Down Expand Up @@ -151,7 +152,7 @@ private void handleSaveConnection() {
return;
}
messageLabel.setText("Trying to Connect...");
SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
try {
if (!hasCorrectBucketConnection()) {
messageLabel.setText("");
Expand Down Expand Up @@ -209,7 +210,7 @@ private void handleTestConnection() {
testConnectionButton.setEnabled(false);
messageLabel.setText("Trying to Connect...");

SwingUtilities.invokeLater(() -> {
ApplicationManager.getApplication().invokeLater(() -> {
if (!defaultBucketTextField.getText().trim().isEmpty()) {
consoleScrollPane.setVisible(true);
thirdPanel.revalidate();
Expand Down
Loading

0 comments on commit 4dc11d6

Please sign in to comment.