/
AbstractMoleculeArchiveDialog.java
553 lines (441 loc) · 18.8 KB
/
AbstractMoleculeArchiveDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*-
* #%L
* Molecule Archive Suite (Mars) - core data storage and processing algorithms.
* %%
* Copyright (C) 2018 - 2023 Karl Duderstadt
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package de.mpg.biochem.mars.swingUI.MoleculeArchiveSelector;
import de.mpg.biochem.mars.io.MoleculeArchiveIOFactory;
import de.mpg.biochem.mars.io.MoleculeArchiveSource;
import de.mpg.biochem.mars.io.MoleculeArchiveStorage;
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;
import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Supplier;
abstract public class AbstractMoleculeArchiveDialog implements TreeWillExpandListener {
protected Consumer<MoleculeArchiveSelection> okCallback;
protected JFrame dialog;
protected JTextField containerPathText;
protected JTree containerTree;
protected JList recentList;
protected List<String> recentURLs;
protected JButton browseBtn;
protected JButton detectBtn;
protected JButton clearRecentBtn;
protected JLabel messageLabel;
protected JButton okBtn;
protected JButton cancelBtn;
protected DefaultTreeModel treeModel;
protected String lastBrowsePath;
protected ExecutorService loaderExecutor;
protected final String initialContainerPath;
protected Consumer<String> containerPathUpdateCallback;
protected Consumer<Void> cancelCallback;
protected TreeCellRenderer treeRenderer;
protected MoleculeArchiveSwingTreeNode rootNode;
protected ExecutorService parseExec;
protected MoleculeArchiveSource source;
protected final AlphanumericComparator comp = new AlphanumericComparator(Collator.getInstance());
@Parameter
protected PrefService prefService;
public AbstractMoleculeArchiveDialog(Context context) {
context.inject(this);
this.initialContainerPath = "";
}
public AbstractMoleculeArchiveDialog(String url, Context context) {
context.inject(this);
this.initialContainerPath = url;
}
public void setTreeRenderer(final TreeCellRenderer treeRenderer) {
this.treeRenderer = treeRenderer;
}
public void setCancelCallback(final Consumer<Void> cancelCallback) {
this.cancelCallback = cancelCallback;
}
public void setContainerPathUpdateCallback(final Consumer<String> containerPathUpdateCallback) {
this.containerPathUpdateCallback = containerPathUpdateCallback;
}
public void setMessage(final String message) {
messageLabel.setText(message);
}
public String getPath() {
return containerPathText.getText().trim();
}
public void run(final Consumer<MoleculeArchiveSelection> okCallback) {
this.okCallback = okCallback;
dialog = buildDialog();
browseBtn.addActionListener(e -> openContainer(this::openBrowseDialog));
detectBtn.addActionListener(e -> openContainer(() -> getPath()));
clearRecentBtn.addActionListener(e -> clearRecent());
// ok and cancel buttons
okBtn.addActionListener(e -> ok());
cancelBtn.addActionListener(e -> cancel());
dialog.setVisible(true);
}
private static final int DEFAULT_OUTER_PAD = 8;
private static final int DEFAULT_BUTTON_PAD = 3;
private static final int DEFAULT_MID_PAD = 5;
protected JFrame buildDialog() {
final int OUTER_PAD = DEFAULT_OUTER_PAD;
final int BUTTON_PAD = DEFAULT_BUTTON_PAD;
final int MID_PAD = DEFAULT_MID_PAD;
final int frameSizeX = UIScale.scale( 800 );
final int frameSizeY = UIScale.scale( 600 );
dialog = new JFrame();
dialog.setPreferredSize(new Dimension(frameSizeX, frameSizeY));
dialog.setMinimumSize(dialog.getPreferredSize());
final Container pane = dialog.getContentPane();
//final JTabbedPane tabs = new JTabbedPane();
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);
containerPathText = new JTextField();
containerPathText.setText(initialContainerPath);
containerPathText.setPreferredSize(new Dimension(frameSizeX / 3, containerPathText.getPreferredSize().height));
containerPathText.addActionListener(e -> openContainer(() -> getPath()));
recentList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
// Double-click detected
containerPathText.setText((String) list.getSelectedValue());
}
}
});
final GridBagConstraints ctxt = new GridBagConstraints();
ctxt.gridx = 0;
ctxt.gridy = 0;
ctxt.gridwidth = 3;
ctxt.gridheight = 1;
ctxt.weightx = 1.0;
ctxt.weighty = 0.0;
ctxt.fill = GridBagConstraints.HORIZONTAL;
ctxt.insets = new Insets(OUTER_PAD, OUTER_PAD, MID_PAD, BUTTON_PAD);
browsePanel.add(containerPathText, ctxt);
browseBtn = new JButton("Browse local");
final GridBagConstraints cbrowse = new GridBagConstraints();
cbrowse.gridx = 3;
cbrowse.gridy = 0;
cbrowse.gridwidth = 1;
cbrowse.gridheight = 1;
cbrowse.weightx = 0.0;
cbrowse.weighty = 0.0;
cbrowse.fill = GridBagConstraints.HORIZONTAL;
cbrowse.insets = new Insets(OUTER_PAD, BUTTON_PAD, MID_PAD, BUTTON_PAD);
browsePanel.add(browseBtn, cbrowse);
detectBtn = new JButton("Load path");
final GridBagConstraints cdetect = new GridBagConstraints();
cdetect.gridx = 4;
cdetect.gridy = 0;
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);
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;
ctree.gridy = 1;
ctree.gridwidth = 6;
ctree.gridheight = 3;
ctree.weightx = 1.0;
ctree.weighty = 1.0;
ctree.ipadx = 0;
ctree.ipady = 0;
ctree.insets = new Insets(0, OUTER_PAD, 0, OUTER_PAD);
ctree.fill = GridBagConstraints.BOTH;
treeModel = new DefaultTreeModel(null);
containerTree = new JTree(treeModel);
containerTree.setMinimumSize(new Dimension(350, 230));
containerTree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
containerTree.addTreeWillExpandListener(this);
if (treeRenderer != null)
containerTree.setCellRenderer(treeRenderer);
final JScrollPane treeScroller = new JScrollPane(containerTree);
treeScroller.setViewportView(containerTree);
treeScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
final JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScroller, listScroller);
browsePanel.add(split, ctree);
// bottom button
final GridBagConstraints cbot = new GridBagConstraints();
cbot.gridx = 0;
cbot.gridy = 4;
cbot.gridwidth = 1;
cbot.gridheight = 1;
cbot.weightx = 0.0;
cbot.weighty = 0.0;
cbot.insets = new Insets(OUTER_PAD, OUTER_PAD, OUTER_PAD, OUTER_PAD);
cbot.anchor = GridBagConstraints.CENTER;
messageLabel = new JLabel("");
messageLabel.setVisible(false);
cbot.gridx = 0;
cbot.anchor = GridBagConstraints.CENTER;
browsePanel.add(messageLabel, cbot);
okBtn = new JButton("Ok");
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);
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);
browsePanel.add(cancelBtn, cbot);
//containerTree.addMouseListener( new MarsNodePopupMenu(this).getPopupListener() );
dialog.pack();
SwingUtilities.invokeLater(() -> split.setDividerLocation(split.getSize().width
- split.getInsets().right
- split.getDividerSize()
- 300));
return dialog;
}
public JTree getJTree() {
return containerTree;
}
private String openBrowseDialog() {
final JFileChooser fileChooser = new JFileChooser();
/*
* Need to allow files so h5 containers can be opened,
* and directories so that filesystem n5's and zarrs can be opened.
*/
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (lastBrowsePath != null && !lastBrowsePath.isEmpty())
fileChooser.setCurrentDirectory(new File(lastBrowsePath));
else if (initialContainerPath != null && !initialContainerPath.isEmpty())
fileChooser.setCurrentDirectory(new File(initialContainerPath));
else if (IJ.getInstance() != null) {
File f = null;
final String currDir = IJ.getDirectory("current");
final String homeDir = IJ.getDirectory("home");
if( currDir != null )
f = new File( currDir );
else if( homeDir != null )
f = new File( homeDir );
fileChooser.setCurrentDirectory(f);
}
final int ret = fileChooser.showOpenDialog(dialog);
if (ret != JFileChooser.APPROVE_OPTION)
return null;
final String path = fileChooser.getSelectedFile().getAbsolutePath();
containerPathText.setText(path);
lastBrowsePath = path;
// callback after browse as well
containerPathUpdateCallback.accept(path);
return path;
}
private void openContainer(final Supplier<String> opener) {
SwingUtilities.invokeLater(() -> {
messageLabel.setText("Building tree...");
messageLabel.setVisible(true);
messageLabel.repaint();
});
final String path = opener.get();
containerPathUpdateCallback.accept(path);
if (path == null) {
messageLabel.setVisible(false);
dialog.repaint();
return;
}
source = null;
try {
source = new MoleculeArchiveIOFactory().openSource(path);
} catch (IOException e) { e.printStackTrace(); }
if (source == null) {
messageLabel.setVisible(false);
dialog.repaint();
return;
}
final String rootPath = source.getPath();
if (loaderExecutor == null) {
loaderExecutor = Executors.newCachedThreadPool();
}
final String[] pathParts = path.split( source.getGroupSeparator() );
final String rootName = pathParts[ pathParts.length - 1 ];
if( treeRenderer != null && treeRenderer instanceof MoleculeArchiveTreeCellRenderer )
((MoleculeArchiveTreeCellRenderer)treeRenderer ).setRootName(rootName);
rootNode = new MoleculeArchiveSwingTreeNode( rootPath, treeModel );
rootNode.setLeaf(false);
treeModel.setRoot(rootNode);
containerTree.setEnabled(true);
containerTree.repaint();
final MoleculeArchiveSource finalSource = source;
parseExec = Executors.newSingleThreadExecutor();
parseExec.submit(() -> {
try {
SwingUtilities.invokeLater(() -> {
messageLabel.setText("Loading...");
messageLabel.repaint();
});
addNodePaths(rootNode);
containerTree.expandRow( 0 );
SwingUtilities.invokeLater(() -> {
messageLabel.setText("Done");
messageLabel.repaint();
});
Thread.sleep(1000);
SwingUtilities.invokeLater(() -> {
messageLabel.setText("");
messageLabel.setVisible(false);
messageLabel.repaint();
});
}
catch (InterruptedException e) { }
});
}
private void addNodePaths(MoleculeArchiveSwingTreeNode node) {
if (node.isLeaf()) return;
if (node.getChildCount() > 0) return;
try {
String treePath = node.getPath();
String fullPath = (treePath.startsWith("/")) ? treePath : source.getGroupSeparator() + treePath;
String[] directoryPaths = source.listDirectories(fullPath);
for (String dir : directoryPaths) {
String[] parts = dir.split("/");
MoleculeArchiveSwingTreeNode child = node.addChildPath(parts[parts.length - 1]);
if (dir.endsWith("." + MoleculeArchiveStorage.MOLECULE_ARCHIVE_STORE_ENDING)
|| dir.endsWith("." + MoleculeArchiveStorage.N5_DATASET_DIRECTORY_ENDING))
child.setLeaf(true);
else
child.setLeaf(false);
}
String[] filePaths = source.listFiles(fullPath);
for (String file : filePaths) {
String[] parts = file.split("/");
MoleculeArchiveSwingTreeNode child = node.addChildPath(parts[parts.length - 1]);
child.setLeaf(true);
}
sortRecursive(node);
//containerTree.repaint();
} catch (IOException error) {
throw new RuntimeException(error);
}
}
public void close() {
// stop parsing things
if( parseExec != null )
parseExec.shutdownNow();
if (source != null) source.close();
dialog.setVisible(false);
dialog.dispose();
}
public void cancel() {
close();
if (cancelCallback != null)
cancelCallback.accept(null);
}
private void sortRecursive( final MoleculeArchiveSwingTreeNode node )
{
if( node != null ) {
final List<MoleculeArchiveTreeNode> children = node.childrenList();
if( !children.isEmpty())
{
children.sort(Comparator.comparing(MoleculeArchiveTreeNode::toString, comp));
}
treeModel.nodeStructureChanged(node);
for( MoleculeArchiveTreeNode child : children )
sortRecursive( (MoleculeArchiveSwingTreeNode)child );
}
}
private static String normalDatasetName(final String fullPath, final String groupSeparator) {
return fullPath.replaceAll("(^" + groupSeparator + "*)|(" + groupSeparator + "*$)", "");
}
private static boolean pathsEqual( final String a, final String b )
{
return normalDatasetName( a, "/" ).equals( normalDatasetName( b, "/" ) );
}
@Override
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
parseExec = Executors.newSingleThreadExecutor();
parseExec.submit(() -> {
try {
SwingUtilities.invokeLater(() -> {
messageLabel.setText("Loading...");
messageLabel.repaint();
});
addNodePaths((MoleculeArchiveSwingTreeNode) event.getPath().getLastPathComponent());
SwingUtilities.invokeLater(() -> {
messageLabel.setText("Done");
messageLabel.repaint();
});
Thread.sleep(1000);
SwingUtilities.invokeLater(() -> {
messageLabel.setText("");
messageLabel.setVisible(false);
messageLabel.repaint();
});
}
catch (InterruptedException e) { }
});
}
@Override
public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
//No implementation required.
}
abstract public void clearRecent();
abstract public void ok();
}