Skip to content

Commit

Permalink
WIP add MoleculeArchiveSaveDialog for cloud storage
Browse files Browse the repository at this point in the history
  • Loading branch information
karlduderstadt committed Jul 31, 2023
1 parent a37f364 commit 1a36639
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 92 deletions.
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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 -> {});
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<MoleculeArchiveSelection> okCallback;
protected Consumer<MoleculeArchiveSelection> 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<String> recentURLs;

private JList recentList;
protected JButton browseBtn;

private List<String> 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<String> containerPathUpdateCallback;

private final String initialContainerPath;
protected Consumer<Void> cancelCallback;

private Consumer<String> containerPathUpdateCallback;
protected TreeCellRenderer treeRenderer;

private Consumer<Void> 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;
}
Expand Down Expand Up @@ -170,7 +165,7 @@ public void run(final Consumer<MoleculeArchiveSelection> 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;
Expand All @@ -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());

Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
@@ -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();
}
}
}

0 comments on commit 1a36639

Please sign in to comment.