Skip to content

Commit

Permalink
Merge branch '231.later'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonoas committed Jun 10, 2024
2 parents e48b754 + 1b2ad8b commit ff9373f
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/main/java/indi/bookmarkx/ui/pannel/BookmarksManagePanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package indi.bookmarkx.ui.pannel;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBScrollPane;
Expand All @@ -12,14 +17,14 @@
import indi.bookmarkx.ui.tree.BookmarkTree;
import indi.bookmarkx.ui.tree.BookmarkTreeNode;
import indi.bookmarkx.utils.PersistenceUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.util.concurrent.ExecutionException;
import java.awt.BorderLayout;

/**
* 标签目录面板
Expand All @@ -29,22 +34,17 @@
*/
public class BookmarksManagePanel extends JPanel {

private final BookmarkTree tree;
private static final Logger LOG = Logger.getInstance(BookmarksManagePanel.class);

private final Project project;
private final BookmarkTree tree;

private final BookmarkArrayListTable bookmarkArrayListTable;
/**
* 标记 tree 是否已经从持久化文件加载完成
*/
private volatile boolean treeLoaded = false;

private BookmarksManagePanel(Project project) {

this.project = project;

bookmarkArrayListTable = BookmarkArrayListTable.getInstance(project);

tree = new BookmarkTree(project);

setLayout(new BorderLayout());
Expand All @@ -70,7 +70,7 @@ public void reInit(Project project) {
}

private void loadTree(Project project) {
new TreeLoadWorker(project, tree).execute();
ProgressManager.getInstance().run(new TreeLoadTask(project, tree));
}

public void prev() {
Expand Down Expand Up @@ -106,35 +106,37 @@ public static BookmarksManagePanel create(Project project) {
return new BookmarksManagePanel(project);
}

public class TreeLoadWorker extends SwingWorker<DefaultTreeModel, Void> {
class TreeLoadTask extends Task.Backgroundable {

private final Project project;

private final BookmarkTree tree;
private DefaultTreeModel treeModel;

TreeLoadWorker(Project project, BookmarkTree tree) {
this.tree = tree;
public TreeLoadTask(Project project, BookmarkTree tree) {
super(project, "Loading Tree Data");
this.project = project;
this.tree = tree;
}

@Override
protected DefaultTreeModel doInBackground() throws Exception {
MyPersistent persistent = MyPersistent.getInstance(project);
BookmarkPO rootPO = persistent.getState();
BookmarkTreeNode root = PersistenceUtil.generateTreeNode(rootPO, project);
return new DefaultTreeModel(root);
public void run(@NotNull ProgressIndicator indicator) {
try {
MyPersistent persistent = MyPersistent.getInstance(project);
BookmarkPO rootPO = persistent.getState();
BookmarkTreeNode root = PersistenceUtil.generateTreeNode(rootPO, project);
treeModel = new DefaultTreeModel(root);
} catch (Exception e) {
// 错误处理
LOG.error("初始化标签树失败", e);
}
LOG.info("初始化标签树成功");
}

@Override
protected void done() {
DefaultTreeModel treeModel;
try {
treeModel = get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
public void onSuccess() {
if (treeModel == null) {
return;
}

treeModel.addTreeModelListener(new TreeModelListener() {
@Override
public void treeNodesChanged(TreeModelEvent e) {
Expand All @@ -157,23 +159,21 @@ public void treeStructureChanged(TreeModelEvent e) {
}

private void persistenceSave() {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
BookmarksManager manager = BookmarksManager.getInstance(project);
manager.persistentSave();
return null;
}
}.execute();
ApplicationManager.getApplication().executeOnPooledThread(() -> {
BookmarksManager manager = BookmarksManager.getInstance(project);
manager.persistentSave();
});
}
});

tree.setModel(treeModel);
treeModel.nodeStructureChanged((TreeNode) treeModel.getRoot());
ApplicationManager.getApplication().invokeLater(() -> {
tree.setModel(treeModel);
treeModel.nodeStructureChanged((TreeNode) treeModel.getRoot());

BookmarkArrayListTable bookmarkArrayListTable = BookmarkArrayListTable.getInstance(project);
bookmarkArrayListTable.initData(tree);
treeLoaded = true;
BookmarkArrayListTable bookmarkArrayListTable = BookmarkArrayListTable.getInstance(project);
bookmarkArrayListTable.initData(tree);
treeLoaded = true;
});
}
}

Expand Down

0 comments on commit ff9373f

Please sign in to comment.