From 1a366392c85745b3f287b956a9dc8fbb14487967 Mon Sep 17 00:00:00 2001 From: Karl Duderstadt Date: Mon, 31 Jul 2023 15:21:45 +0200 Subject: [PATCH] WIP add MoleculeArchiveSaveDialog for cloud storage --- .../commands/ImportCloudArchiveCommand.java | 6 +- ...ava => AbstractMoleculeArchiveDialog.java} | 133 ++++++------------ .../MoleculeArchiveOpenDialog.java | 79 +++++++++++ .../MoleculeArchiveSaveDialog.java | 83 +++++++++++ 4 files changed, 209 insertions(+), 92 deletions(-) rename src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/{MoleculeArchiveSelectorDialog.java => AbstractMoleculeArchiveDialog.java} (82%) create mode 100644 src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveOpenDialog.java create mode 100644 src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSaveDialog.java diff --git a/src/main/java/de/mpg/biochem/mars/molecule/commands/ImportCloudArchiveCommand.java b/src/main/java/de/mpg/biochem/mars/molecule/commands/ImportCloudArchiveCommand.java index 95b7a6d8..e03288b3 100644 --- a/src/main/java/de/mpg/biochem/mars/molecule/commands/ImportCloudArchiveCommand.java +++ b/src/main/java/de/mpg/biochem/mars/molecule/commands/ImportCloudArchiveCommand.java @@ -31,10 +31,9 @@ import de.mpg.biochem.mars.molecule.MoleculeArchive; import de.mpg.biochem.mars.molecule.MoleculeArchiveIOPlugin; +import de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector.MoleculeArchiveOpenDialog; import de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector.MoleculeArchiveSelection; -import de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector.MoleculeArchiveSelectorDialog; import de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector.MoleculeArchiveTreeCellRenderer; -import org.scijava.Context; import org.scijava.command.Command; import org.scijava.command.DynamicCommand; import org.scijava.menu.MenuConstants; @@ -46,7 +45,6 @@ import org.scijava.ui.UIService; import java.io.IOException; -import java.util.List; import java.util.function.Consumer; @Plugin(type = Command.class, label = "Open Virtual Store", menu = { @Menu( @@ -68,7 +66,7 @@ public class ImportCloudArchiveCommand extends DynamicCommand { @Override public void run() { - MoleculeArchiveSelectorDialog selectionDialog = new MoleculeArchiveSelectorDialog("", getContext()); + MoleculeArchiveOpenDialog selectionDialog = new MoleculeArchiveOpenDialog("", getContext()); selectionDialog.setTreeRenderer(new MoleculeArchiveTreeCellRenderer(true)); // Prevents NullPointerException selectionDialog.setContainerPathUpdateCallback(x -> {}); diff --git a/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSelectorDialog.java b/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/AbstractMoleculeArchiveDialog.java similarity index 82% rename from src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSelectorDialog.java rename to src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/AbstractMoleculeArchiveDialog.java index 610d7324..3ffc8a87 100644 --- a/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSelectorDialog.java +++ b/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/AbstractMoleculeArchiveDialog.java @@ -31,7 +31,6 @@ import de.mpg.biochem.mars.io.MoleculeArchiveIOFactory; import de.mpg.biochem.mars.io.MoleculeArchiveSource; import de.mpg.biochem.mars.io.MoleculeArchiveStorage; -import de.mpg.biochem.mars.molecule.commands.ImportCloudArchiveCommand; import ij.IJ; import org.scijava.Context; import org.scijava.plugin.Parameter; @@ -42,8 +41,6 @@ import javax.swing.*; import javax.swing.event.TreeExpansionEvent; -import javax.swing.event.TreeSelectionEvent; -import javax.swing.event.TreeSelectionListener; import javax.swing.event.TreeWillExpandListener; import javax.swing.tree.*; import java.awt.Container; @@ -59,70 +56,68 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Optional; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.function.Consumer; -import java.util.function.Function; import java.util.function.Supplier; -public class MoleculeArchiveSelectorDialog implements TreeWillExpandListener { +abstract public class AbstractMoleculeArchiveDialog implements TreeWillExpandListener { - private Consumer okCallback; + protected Consumer okCallback; - private JFrame dialog; + protected JFrame dialog; - private JTextField containerPathText; + protected JTextField containerPathText; - private JCheckBox virtualBox; + protected JTree containerTree; - private JCheckBox cropBox; + protected JList recentList; - private JTree containerTree; + protected List recentURLs; - private JList recentList; + protected JButton browseBtn; - private List recentURLs; + protected JButton detectBtn; - private JButton browseBtn; + protected JButton clearRecentBtn; - private JButton detectBtn; + protected JLabel messageLabel; - private JButton clearRecentBtn; + protected JButton okBtn; - private JLabel messageLabel; + protected JButton cancelBtn; - private JButton okBtn; + protected DefaultTreeModel treeModel; - private JButton cancelBtn; + protected String lastBrowsePath; - private DefaultTreeModel treeModel; + protected ExecutorService loaderExecutor; - private String lastBrowsePath; + protected final String initialContainerPath; - private ExecutorService loaderExecutor; + protected Consumer containerPathUpdateCallback; - private final String initialContainerPath; + protected Consumer cancelCallback; - private Consumer containerPathUpdateCallback; + protected TreeCellRenderer treeRenderer; - private Consumer cancelCallback; + protected MoleculeArchiveSwingTreeNode rootNode; - private TreeCellRenderer treeRenderer; + protected ExecutorService parseExec; - private MoleculeArchiveSwingTreeNode rootNode; + protected MoleculeArchiveSource source; - private ExecutorService parseExec; - - private MoleculeArchiveSource source; - - private final AlphanumericComparator comp = new AlphanumericComparator(Collator.getInstance()); + protected final AlphanumericComparator comp = new AlphanumericComparator(Collator.getInstance()); @Parameter - private PrefService prefService; + protected PrefService prefService; + + public AbstractMoleculeArchiveDialog(Context context) { + context.inject(this); + this.initialContainerPath = ""; + } - public MoleculeArchiveSelectorDialog(String url, Context context) { + public AbstractMoleculeArchiveDialog(String url, Context context) { context.inject(this); this.initialContainerPath = url; } @@ -170,7 +165,7 @@ public void run(final Consumer okCallback) { private static final int DEFAULT_BUTTON_PAD = 3; private static final int DEFAULT_MID_PAD = 5; - private JFrame buildDialog() { + protected JFrame buildDialog() { final int OUTER_PAD = DEFAULT_OUTER_PAD; final int BUTTON_PAD = DEFAULT_BUTTON_PAD; @@ -179,7 +174,7 @@ private JFrame buildDialog() { final int frameSizeX = UIScale.scale( 800 ); final int frameSizeY = UIScale.scale( 600 ); - dialog = new JFrame("Open Molecule Archive"); + dialog = new JFrame(); dialog.setPreferredSize(new Dimension(frameSizeX, frameSizeY)); dialog.setMinimumSize(dialog.getPreferredSize()); @@ -193,8 +188,6 @@ private JFrame buildDialog() { recentList = new JList(); recentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane listScroller = new JScrollPane(recentList); - recentURLs = prefService.getList(ImportCloudArchiveCommand.class, "recentOpenURLs"); - recentList.setListData(recentURLs.toArray(new String[0])); containerPathText = new JTextField(); containerPathText.setText(initialContainerPath); @@ -275,13 +268,7 @@ public void mouseClicked(MouseEvent evt) { containerTree.setMinimumSize(new Dimension(350, 230)); containerTree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); - containerTree.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - ok(); - } - } - }); + containerTree.addTreeWillExpandListener(this); if (treeRenderer != null) @@ -307,11 +294,11 @@ public void mouseClicked(MouseEvent e) { messageLabel = new JLabel(""); messageLabel.setVisible(false); - cbot.gridx = 2; + cbot.gridx = 0; cbot.anchor = GridBagConstraints.CENTER; browsePanel.add(messageLabel, cbot); - okBtn = new JButton("Open"); + okBtn = new JButton("Ok"); cbot.gridx = 4; cbot.ipadx = (int)(20); cbot.anchor = GridBagConstraints.EAST; @@ -337,13 +324,6 @@ public void mouseClicked(MouseEvent e) { return dialog; } - private void clearRecent() { - this.recentURLs = new ArrayList<>(); - recentList.setListData(new String[0]); - recentList.repaint(); - prefService.remove(ImportCloudArchiveCommand.class, "recentOpenURLs"); - } - public JTree getJTree() { return containerTree; } @@ -494,45 +474,18 @@ private void addNodePaths(MoleculeArchiveSwingTreeNode node) { } } - public void ok() { + public void close() { // stop parsing things if( parseExec != null ) parseExec.shutdownNow(); - String url; - // check if we can skip explicit dataset detection - if (containerTree.getSelectionCount() == 0) { - containerPathUpdateCallback.accept(getPath()); - url = getPath(); - } else { - // archive was selected by the user - String fullPath = ((MoleculeArchiveSwingTreeNode)containerTree.getLastSelectedPathComponent()).getPath(); - //String fullPath = (treePath.startsWith("/")) ? treePath : source.getGroupSeparator() + treePath; - if (fullPath.startsWith(getPath())) url = fullPath; - else { - String uri = (getPath().endsWith(source.getGroupSeparator())) ? getPath().substring(0, getPath().length()-1) : getPath(); - if (uri.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_ENDING) || uri.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_STORE_ENDING)) - url = uri; - else - url = uri + fullPath; - } - } - if (url.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_ENDING) || url.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_STORE_ENDING)) { - okCallback.accept(new MoleculeArchiveSelection(url)); - if (source != null) source.close(); - dialog.setVisible(false); - dialog.dispose(); - - if (recentURLs.contains(url)) - recentURLs.remove(recentURLs.indexOf(url)); - recentURLs.add(0, url); - prefService.put(ImportCloudArchiveCommand.class, "recentOpenURLs", recentURLs); - } + if (source != null) source.close(); + dialog.setVisible(false); + dialog.dispose(); } public void cancel() { - dialog.setVisible(false); - dialog.dispose(); + close(); if (cancelCallback != null) cancelCallback.accept(null); @@ -593,4 +546,8 @@ public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException { //No implementation required. } + + abstract public void clearRecent(); + + abstract public void ok(); } \ No newline at end of file diff --git a/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveOpenDialog.java b/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveOpenDialog.java new file mode 100644 index 00000000..2c6b6348 --- /dev/null +++ b/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveOpenDialog.java @@ -0,0 +1,79 @@ +package de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector; + +import de.mpg.biochem.mars.io.MoleculeArchiveSource; +import de.mpg.biochem.mars.molecule.commands.ImportCloudArchiveCommand; +import org.scijava.Context; + +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +public class MoleculeArchiveOpenDialog extends AbstractMoleculeArchiveDialog { + + public MoleculeArchiveOpenDialog(String url, Context context) { + super(url, context); + } + + public MoleculeArchiveOpenDialog(Context context) { + super(context); + } + + protected JFrame buildDialog() { + super.buildDialog(); + dialog.setTitle("Open Molecule Archive"); + + recentURLs = prefService.getList(MoleculeArchiveOpenDialog.class, "recentOpenURLs"); + recentList.setListData(recentURLs.toArray(new String[0])); + + okBtn.setText("Open"); + + containerTree.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + ok(); + } + } + }); + + return dialog; + } + + public void clearRecent() { + this.recentURLs = new ArrayList<>(); + recentList.setListData(new String[0]); + recentList.repaint(); + prefService.remove(MoleculeArchiveOpenDialog.class, "recentOpenURLs"); + } + + public void ok() { + String url; + // check if we can skip explicit dataset detection + if (containerTree.getSelectionCount() == 0) { + containerPathUpdateCallback.accept(getPath()); + url = getPath(); + } else { + // archive was selected by the user + String fullPath = ((MoleculeArchiveSwingTreeNode)containerTree.getLastSelectedPathComponent()).getPath(); + if (fullPath.startsWith("//")) fullPath = fullPath.substring(1, fullPath.length()); + + if (fullPath.startsWith(getPath())) url = fullPath; + else { + String uri = (getPath().endsWith(source.getGroupSeparator())) ? getPath().substring(0, getPath().length()-1) : getPath(); + if (uri.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_ENDING) || uri.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_STORE_ENDING)) + url = uri; + else + url = uri + fullPath; + } + } + if (url.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_ENDING) || url.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_STORE_ENDING)) { + okCallback.accept(new MoleculeArchiveSelection(url)); + + if (recentURLs.contains(url)) + recentURLs.remove(recentURLs.indexOf(url)); + recentURLs.add(0, url); + prefService.put(MoleculeArchiveOpenDialog.class, "recentOpenURLs", recentURLs); + close(); + } + } +} diff --git a/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSaveDialog.java b/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSaveDialog.java new file mode 100644 index 00000000..8d6c0308 --- /dev/null +++ b/src/main/java/de/mpg/biochem/mars/swingUI/MoleculeArchiveSelector/MoleculeArchiveSaveDialog.java @@ -0,0 +1,83 @@ +package de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector; + +import de.mpg.biochem.mars.io.MoleculeArchiveSource; +import de.mpg.biochem.mars.molecule.MoleculeArchiveWindow; +import de.mpg.biochem.mars.molecule.commands.ImportCloudArchiveCommand; +import org.scijava.Context; + +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +public class MoleculeArchiveSaveDialog extends AbstractMoleculeArchiveDialog { + + private MoleculeArchiveWindow archiveWindow; + + public MoleculeArchiveSaveDialog(String url, Context context, MoleculeArchiveWindow archiveWindow) { + super(url, context); + this.archiveWindow = archiveWindow; + } + + public MoleculeArchiveSaveDialog(Context context, MoleculeArchiveWindow archiveWindow) { + super(context); + this.archiveWindow = archiveWindow; + } + + protected JFrame buildDialog() { + super.buildDialog(); + dialog.setTitle("Save Molecule Archive"); + + recentURLs = prefService.getList(MoleculeArchiveSaveDialog.class, "recentSaveURLs"); + recentList.setListData(recentURLs.toArray(new String[0])); + + okBtn.setText("Save"); + + containerTree.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + String fullPath = ((MoleculeArchiveSwingTreeNode)containerTree.getLastSelectedPathComponent()).getPath(); + if (fullPath.startsWith("//")) fullPath = fullPath.substring(1, fullPath.length()); + + String url = ""; + if (fullPath.startsWith(getPath())) url = fullPath; + else { + String uri = (getPath().endsWith(source.getGroupSeparator())) ? getPath().substring(0, getPath().length()-1) : getPath(); + if (uri.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_ENDING) || uri.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_STORE_ENDING)) + url = uri; + else + url = uri + fullPath; + } + + containerPathText.setText(url); + } + } + }); + + return dialog; + } + + public void clearRecent() { + this.recentURLs = new ArrayList<>(); + recentList.setListData(new String[0]); + recentList.repaint(); + prefService.remove(MoleculeArchiveSaveDialog.class, "recentSaveURLs"); + } + + public void ok() { + String url; + // check if we can skip explicit dataset detection + containerPathUpdateCallback.accept(getPath()); + url = getPath(); + + if (url.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_ENDING) || url.endsWith("." + MoleculeArchiveSource.MOLECULE_ARCHIVE_STORE_ENDING)) { + okCallback.accept(new MoleculeArchiveSelection(url)); + + if (recentURLs.contains(url)) + recentURLs.remove(recentURLs.indexOf(url)); + recentURLs.add(0, url); + prefService.remove(MoleculeArchiveSaveDialog.class, "recentSaveURLs"); + close(); + } + } +}