Skip to content

Commit

Permalink
WIP add clear recent button, recent in split view
Browse files Browse the repository at this point in the history
  • Loading branch information
karlduderstadt committed Jul 30, 2023
1 parent 4b4715f commit a37f364
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 37 deletions.
Expand Up @@ -34,6 +34,7 @@
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 Down Expand Up @@ -67,24 +68,17 @@ public class ImportCloudArchiveCommand extends DynamicCommand {

@Override
public void run() {
MoleculeArchiveSelectorDialog selectionDialog = new MoleculeArchiveSelectorDialog("");
MoleculeArchiveSelectorDialog selectionDialog = new MoleculeArchiveSelectorDialog("", getContext());
selectionDialog.setTreeRenderer(new MoleculeArchiveTreeCellRenderer(true));
// Prevents NullPointerException
selectionDialog.setContainerPathUpdateCallback(x -> {});
List<String> recentOpenURLs = prefService.getList(ImportCloudArchiveCommand.class, "recentOpenURLs");
selectionDialog.setRecentURLList(recentOpenURLs.toArray(new String[0]));

final Consumer<MoleculeArchiveSelection> callback = (MoleculeArchiveSelection dataSelection) -> {
final MoleculeArchiveIOPlugin moleculeArchiveIOPlugin =
new MoleculeArchiveIOPlugin();
moleculeArchiveIOPlugin.setContext(getContext());

try {
if (recentOpenURLs.contains(dataSelection.url))
recentOpenURLs.remove(recentOpenURLs.indexOf(dataSelection.url));
recentOpenURLs.add(0, dataSelection.url);
prefService.put(ImportCloudArchiveCommand.class, "recentOpenURLs", recentOpenURLs);

MoleculeArchive<?, ?, ?, ?> archive = moleculeArchiveIOPlugin.open(dataSelection.url);

final boolean newStyleIO = optionsService.getOptions(
Expand Down
Expand Up @@ -31,7 +31,11 @@
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;
import org.scijava.prefs.PrefService;
import se.sawano.java.text.AlphanumericComparator;

import com.formdev.flatlaf.util.UIScale;
Expand All @@ -52,6 +56,7 @@
import java.io.File;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand All @@ -78,13 +83,15 @@ public class MoleculeArchiveSelectorDialog implements TreeWillExpandListener {

private JList recentList;

private List<String> recentURLs;

private JButton browseBtn;

private JButton detectBtn;

private JLabel messageLabel;
private JButton clearRecentBtn;

private String[] recentURLs;
private JLabel messageLabel;

private JButton okBtn;

Expand Down Expand Up @@ -112,7 +119,11 @@ public class MoleculeArchiveSelectorDialog implements TreeWillExpandListener {

private final AlphanumericComparator comp = new AlphanumericComparator(Collator.getInstance());

public MoleculeArchiveSelectorDialog(String url) {
@Parameter
private PrefService prefService;

public MoleculeArchiveSelectorDialog(String url, Context context) {
context.inject(this);
this.initialContainerPath = url;
}

Expand All @@ -121,10 +132,6 @@ public void setTreeRenderer(final TreeCellRenderer treeRenderer) {
this.treeRenderer = treeRenderer;
}

public void setRecentURLList(String[] list) {
this.recentURLs = list;
}

public void setCancelCallback(final Consumer<Void> cancelCallback) {

this.cancelCallback = cancelCallback;
Expand All @@ -151,6 +158,7 @@ public void run(final Consumer<MoleculeArchiveSelection> okCallback) {

browseBtn.addActionListener(e -> openContainer(this::openBrowseDialog));
detectBtn.addActionListener(e -> openContainer(() -> getPath()));
clearRecentBtn.addActionListener(e -> clearRecent());

// ok and cancel buttons
okBtn.addActionListener(e -> ok());
Expand All @@ -168,26 +176,25 @@ private JFrame buildDialog() {
final int BUTTON_PAD = DEFAULT_BUTTON_PAD;
final int MID_PAD = DEFAULT_MID_PAD;

final int frameSizeX = UIScale.scale( 600 );
final int frameSizeY = UIScale.scale( 400 );
final int frameSizeX = UIScale.scale( 800 );
final int frameSizeY = UIScale.scale( 600 );

dialog = new JFrame("Open Molecule Archive");
dialog.setPreferredSize(new Dimension(frameSizeX, frameSizeY));
dialog.setMinimumSize(dialog.getPreferredSize());

final Container pane = dialog.getContentPane();
final JTabbedPane tabs = new JTabbedPane();
pane.add( tabs );
//final JTabbedPane tabs = new JTabbedPane();

final JPanel panel = new JPanel(false);
panel.setLayout(new GridBagLayout());
tabs.addTab("Browse", panel);
final JPanel browsePanel = new JPanel(false);
browsePanel.setLayout(new GridBagLayout());
pane.add( browsePanel );

recentList = new JList();
recentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane listScroller = new JScrollPane(recentList);
if (this.recentURLs != null) recentList.setListData(recentURLs);
tabs.addTab("Recent", recentList );
recentURLs = prefService.getList(ImportCloudArchiveCommand.class, "recentOpenURLs");
recentList.setListData(recentURLs.toArray(new String[0]));

containerPathText = new JTextField();
containerPathText.setText(initialContainerPath);
Expand All @@ -213,9 +220,9 @@ public void mouseClicked(MouseEvent evt) {
ctxt.weighty = 0.0;
ctxt.fill = GridBagConstraints.HORIZONTAL;
ctxt.insets = new Insets(OUTER_PAD, OUTER_PAD, MID_PAD, BUTTON_PAD);
panel.add(containerPathText, ctxt);
browsePanel.add(containerPathText, ctxt);

browseBtn = new JButton("Browse");
browseBtn = new JButton("Browse local");
final GridBagConstraints cbrowse = new GridBagConstraints();
cbrowse.gridx = 3;
cbrowse.gridy = 0;
Expand All @@ -225,19 +232,31 @@ public void mouseClicked(MouseEvent evt) {
cbrowse.weighty = 0.0;
cbrowse.fill = GridBagConstraints.HORIZONTAL;
cbrowse.insets = new Insets(OUTER_PAD, BUTTON_PAD, MID_PAD, BUTTON_PAD);
panel.add(browseBtn, cbrowse);
browsePanel.add(browseBtn, cbrowse);

detectBtn = new JButton("Load path");
final GridBagConstraints cdetect = new GridBagConstraints();
cdetect.gridx = 4;
cdetect.gridy = 0;
cdetect.gridwidth = 2;
cdetect.gridwidth = 1;
cdetect.gridheight = 1;
cdetect.weightx = 0.0;
cdetect.weighty = 0.0;
cdetect.fill = GridBagConstraints.HORIZONTAL;
cdetect.insets = new Insets(OUTER_PAD, BUTTON_PAD, MID_PAD, OUTER_PAD);
panel.add(detectBtn, cdetect);
browsePanel.add(detectBtn, cdetect);

clearRecentBtn = new JButton("Clear recent");
final GridBagConstraints cClear = new GridBagConstraints();
cClear.gridx = 5;
cClear.gridy = 0;
cClear.gridwidth = 1;
cClear.gridheight = 1;
cClear.weightx = 0.0;
cClear.weighty = 0.0;
cClear.fill = GridBagConstraints.HORIZONTAL;
cClear.insets = new Insets(OUTER_PAD, BUTTON_PAD, MID_PAD, OUTER_PAD);
browsePanel.add(clearRecentBtn, cClear);

final GridBagConstraints ctree = new GridBagConstraints();
ctree.gridx = 0;
Expand Down Expand Up @@ -271,7 +290,9 @@ public void mouseClicked(MouseEvent e) {
final JScrollPane treeScroller = new JScrollPane(containerTree);
treeScroller.setViewportView(containerTree);
treeScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.add(treeScroller, ctree);

final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroller, listScroller);
browsePanel.add(split, ctree);

// bottom button
final GridBagConstraints cbot = new GridBagConstraints();
Expand All @@ -288,30 +309,41 @@ public void mouseClicked(MouseEvent e) {
messageLabel.setVisible(false);
cbot.gridx = 2;
cbot.anchor = GridBagConstraints.CENTER;
panel.add(messageLabel, cbot);
browsePanel.add(messageLabel, cbot);

okBtn = new JButton("OK");
okBtn = new JButton("Open");
cbot.gridx = 4;
cbot.ipadx = (int)(20);
cbot.anchor = GridBagConstraints.EAST;
cbot.fill = GridBagConstraints.HORIZONTAL;
cbot.insets = new Insets(MID_PAD, OUTER_PAD, OUTER_PAD, BUTTON_PAD);
panel.add(okBtn, cbot);
browsePanel.add(okBtn, cbot);

cancelBtn = new JButton("Cancel");
cbot.gridx = 5;
cbot.ipadx = 0;
cbot.anchor = GridBagConstraints.EAST;
cbot.fill = GridBagConstraints.HORIZONTAL;
cbot.insets = new Insets(MID_PAD, BUTTON_PAD, OUTER_PAD, OUTER_PAD);
panel.add(cancelBtn, cbot);
browsePanel.add(cancelBtn, cbot);

containerTree.addMouseListener( new MarsNodePopupMenu(this).getPopupListener() );
//containerTree.addMouseListener( new MarsNodePopupMenu(this).getPopupListener() );

dialog.pack();
SwingUtilities.invokeLater(() -> split.setDividerLocation(split.getSize().width
- split.getInsets().right
- split.getDividerSize()
- 300));
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 @@ -490,6 +522,11 @@ public void ok() {
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);
}
}

Expand Down
Expand Up @@ -137,8 +137,8 @@ public MoleculeArchiveSwingTreeNode addChildPath( final String path ) {

add( child );

if( treeModel != null)
treeModel.nodesWereInserted( this, new int[]{childrenList().size() - 1 });
//if( treeModel != null)
// treeModel.nodesWereInserted( this, new int[]{childrenList().size() - 1 });
}
return (MoleculeArchiveSwingTreeNode) child;
}
Expand Down

0 comments on commit a37f364

Please sign in to comment.