Skip to content

Commit

Permalink
DLFS Browser updates for Convex Live demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 18, 2024
1 parent 92982d7 commit ca04cf9
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 27 deletions.
11 changes: 10 additions & 1 deletion convex-core/src/main/java/convex/core/util/SoftCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.ref.SoftReference;
import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Set;
import java.util.WeakHashMap;

Expand Down Expand Up @@ -34,6 +35,14 @@ public SoftReference<V> putReference(K key, SoftReference<V> valueRef) {

@Override
public Set<Entry<K, V>> entrySet() {
throw new UnsupportedOperationException();
HashSet<Entry<K, V>> result=new HashSet<Entry<K, V>>();

for (Entry<K, SoftReference<V>> e: cache.entrySet()) {
V val=e.getValue().get();
if (val!=null) {
result.add(new AbstractMap.SimpleEntry<>(e.getKey(),val));
}
}
return result;
}
}
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/util/ThreadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public static <T> CompletableFuture<java.util.List<T>> completeAll(java.util.Lis
}

/**
* Runs a (probably IO-bound) task in a virtual thrwead if available,
* @param task
* Runs a (probably IO-bound) task in a virtual thread if available,
* @param task Task to run
*/
public static void runVirtual(Runnable task) {
getVirtualExecutor().execute(task);
Expand Down
11 changes: 11 additions & 0 deletions convex-core/src/main/java/convex/dlfs/DLFSProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import convex.core.util.SoftCache;
Expand Down Expand Up @@ -52,6 +54,15 @@ public DLFileSystem getFileSystem(URI uri) {
return fs;
}

public Iterable<DLFileSystem> getFileSystems() {
ArrayList<DLFileSystem> systems=new ArrayList<>();
for (Entry<String, DLFileSystem> e: fileSystems.entrySet()) {
DLFileSystem fs=e.getValue();
if (fs!=null) systems.add(fs);
}
return systems;
}

@Override
public Path getPath(URI uri) {
return getFileSystem(uri).getPath(uri.getPath());
Expand Down
7 changes: 5 additions & 2 deletions convex-core/src/main/java/convex/dlfs/impl/DLFSLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public synchronized AVector<ACell> createFile(DLPath path) throws IOException {
if (parentNode==null) {
throw new FileNotFoundException("Parent directory does not exist: "+parent.toString());
}
if (DLFSNode.getDirectoryEntries(parentNode).containsKey(name)) {
throw new FileAlreadyExistsException(name.toString());
AVector<ACell> oldNode=DLFSNode.getDirectoryEntries(parentNode).get(name);
if (oldNode!=null) {
if (!DLFSNode.isTombstone(oldNode)) {
throw new FileAlreadyExistsException(name.toString());
}
}
AVector<ACell> newNode=DLFSNode.createEmptyFile(getTimestamp());
updateNode(path,newNode);
Expand Down
7 changes: 5 additions & 2 deletions convex-core/src/main/java/convex/dlfs/impl/DLFileChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public static DLFileChannel create(DLFileSystem fs, Set<? extends OpenOption> op
}
if (options.contains(StandardOpenOption.CREATE_NEW)) {
if (node!=null) {
throw new FileAlreadyExistsException(path.toString());
// can create over a tombstone
if(!DLFSNode.isTombstone(node)) {
throw new FileAlreadyExistsException(path.toString());
}
}
}
if (options.contains(StandardOpenOption.APPEND)) {
Expand All @@ -55,7 +58,7 @@ public static DLFileChannel create(DLFileSystem fs, Set<? extends OpenOption> op
}
}

if (node==null) {
if ((node==null)||DLFSNode.isTombstone(node)) {
if (readOnly) throw new NoSuchFileException(path.toString());
node=fs.createFile(path);
} else {
Expand Down
2 changes: 1 addition & 1 deletion convex-gui/src/main/java/convex/gui/PeerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static void launchPeerGUI(int peerNum, AKeyPair genesis, boolean topLevel
frame.setIconImage(Toolkit.getDefaultToolkit()
.getImage(PeerGUI.class.getResource("/images/Convex.png")));
frame.setBounds(200, 150, 1000, 800);
if (topLevel) frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit.closeIfFirstFrame(frame);

frame.getContentPane().add(manager, BorderLayout.CENTER);
frame.setVisible(true);
Expand Down
17 changes: 8 additions & 9 deletions convex-gui/src/main/java/convex/gui/components/AbstractGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import java.awt.EventQueue;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
Expand All @@ -17,30 +16,26 @@
@SuppressWarnings("serial")
public class AbstractGUI extends JPanel implements Runnable {

protected JFrame frame=null;
protected JFrame frame=new JFrame();

@Override
public void run() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
frame = new JFrame();
frame.setTitle(getTitle());
frame.setIconImage(Toolkit.getDefaultToolkit()
.getImage(MainGUI.class.getResource("/images/Convex.png")));
frame.setBounds(50, 50, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Toolkit.closeIfFirstFrame(frame);


frame.getContentPane().setLayout(new MigLayout());
frame.getContentPane().add(AbstractGUI.this, "dock center");
frame.setVisible(true);

frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent winEvt) {
// shut down peers gracefully
}
});

} catch (Exception e) {
e.printStackTrace();
Expand All @@ -56,5 +51,9 @@ public void windowClosing(WindowEvent winEvt) {
public String getTitle() {
return "Convex Desktop";
}

public JFrame getFrame() {
return frame;
}

}
70 changes: 69 additions & 1 deletion convex-gui/src/main/java/convex/gui/dlfs/DLFSBrowser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package convex.gui.dlfs;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import convex.dlfs.DLFS;
import convex.dlfs.DLPath;
Expand All @@ -13,11 +22,70 @@
public class DLFSBrowser extends AbstractGUI {

protected DLFSLocal drive;

protected static ArrayList<DLFSLocal> allDrives=new ArrayList<>();

public JMenuBar menuBar=new JMenuBar();
public JMenu fileMenu=new JMenu("File");
public JMenu driveMenu=new JMenu("Drive");
public JMenu helpMenu=new JMenu("Help");
protected DLFSPanel panel;

public DLFSBrowser(DLFSLocal drive) {
allDrives.add(drive);
setLayout(new MigLayout());
this.drive=drive;
add(new DLFSPanel(drive),"dock center");
panel=new DLFSPanel(drive);
add(panel,"dock center");

fileMenu.add(makeMenu("Delete",()->{
Path p=panel.fileList.getSelectedPath();
try {
Files.deleteIfExists(p);
} catch (IOException e) {
System.out.println("Can't delete "+p+ " : "+e.getMessage());
}
panel.refreshView();
}));
menuBar.add(fileMenu);

driveMenu.add(makeMenu("Clone",()->new DLFSBrowser(drive.clone()).run()));
driveMenu.add(makeMenu("Sync",()->{
for (DLFSLocal other: allDrives) {
if (other!=drive) {
System.out.println("Replicating!!");
drive.replicate(other);
}
}
panel.refreshView();
}));
menuBar.add(driveMenu);

menuBar.add(makeMenu("Sync!",()->{
for (DLFSLocal other: allDrives) {
if (other!=drive) {
System.out.println("Replicating!!");
drive.replicate(other);
}
}
panel.refreshView();
}));

getFrame().setJMenuBar(menuBar);

}

protected JMenuItem makeMenu(String name,Runnable op) {
JMenuItem mi= new JMenuItem(name);
mi.setAction(new AbstractAction(name) {

@Override
public void actionPerformed(ActionEvent e) {
op.run();
}

} );
return mi;
}


Expand Down
32 changes: 31 additions & 1 deletion convex-gui/src/main/java/convex/gui/dlfs/DLFSPanel.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package convex.gui.dlfs;

import java.awt.Dimension;
import java.nio.file.Path;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import convex.core.util.ThreadUtils;
import convex.dlfs.DLFileSystem;
import convex.dlfs.DLPath;
import convex.gui.components.CodeLabel;
Expand All @@ -29,6 +31,8 @@ public class DLFSPanel extends JPanel {

private DLPath selectedPath;

private PreviewPanel previewPanel;


public DLFSPanel(DLFileSystem dlfs) {
this.fileSystem=dlfs;
Expand All @@ -53,6 +57,7 @@ protected Path getTargetPath() {
setSelectedPath(p);
}
});
directoryTree.setPreferredSize(new Dimension(250,500));

fileList=new FileList(selectedPath,p->setSelectedPath(p));
fileList.setTransferHandler(new DnDTransferHandler(this) {
Expand All @@ -61,9 +66,19 @@ protected Path getTargetPath() {
return getSelectedPath();
}
});
fileList.addListSelectionListener(e->{
Path p=fileList.getSelectedPath();
previewPanel.setPath(p);
});
fileList.setPreferredSize(new Dimension(250,500));

JScrollPane listScrollPane=new JScrollPane(fileList);

JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(directoryTree), listScrollPane);
JSplitPane filesSplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(directoryTree), listScrollPane);

previewPanel=new PreviewPanel();

JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,filesSplitPane,previewPanel);
add(splitPane,"dock center");

pathLabel=new JLabel("/");
Expand All @@ -72,6 +87,17 @@ protected Path getTargetPath() {
add(infoLabel,"dock south");

directoryTree.setSelectionPath(directoryTree.getPathForRow(0));

ThreadUtils.runVirtual(()->{
try {
while (fileSystem.isOpen()) {
fileSystem.updateTimestamp();
Thread.sleep(100);
}
} catch (InterruptedException e) {
// finished
}
});
}

void setSelectedPath(Path newPath) {
Expand All @@ -95,4 +121,8 @@ public DLPath getSelectedPath() {
return selectedPath;
}

public void refreshView() {
setSelectedPath(selectedPath);
}

}
18 changes: 12 additions & 6 deletions convex-gui/src/main/java/convex/gui/dlfs/DLFSTreeCellRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,39 @@
@SuppressWarnings("serial")
public class DLFSTreeCellRenderer extends DefaultTreeCellRenderer {
private static final Color SELCOLOUR=new Color(30,70, 100);
private static final Color TEXTCOLOUR=new Color(200,200, 250);

public DLFSTreeCellRenderer() {
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setOpaque(false);
setBackgroundSelectionColor(SELCOLOUR);

this.setTextNonSelectionColor(TEXTCOLOUR);
this.setTextSelectionColor(Color.WHITE);
setOpaque(true);
// setOpenIcon(Toolkit.);
//setLeafIcon(Toolkit.CONVEX);
//setClosedIcon(Toolkit.CONVEX);
}

static FileSystemView fsView=FileSystemView.getFileSystemView();

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
Path path=(Path) ((DefaultMutableTreeNode)value).getUserObject();
if (selected) {
setOpaque(true);
setBackground(this.getBackgroundSelectionColor());
setBackground(SELCOLOUR);
} else {
setOpaque(false);
//this.setForeground(this.getTextNonSelectionColor());
setBackground(this.getBackgroundNonSelectionColor());
setBackground(null);
}
Path name=path.getFileName();
setText((name==null)?"DLFS Root":name.toString());

Icon icon = BrowserUtils.getFileIcon(path);

setIcon(icon);

return this;
}

Expand Down
5 changes: 3 additions & 2 deletions convex-gui/src/main/java/convex/gui/dlfs/DirectoryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public DirectoryTree(DLFileSystem dlfs) {
setEnabled(true);

DLFSTreeCellRenderer renderer=new DLFSTreeCellRenderer();
renderer.setBackgroundNonSelectionColor(getBackground());

setCellRenderer(renderer);
setShowsRootHandles(true);
setShowsRootHandles(false);
setRootVisible(true);
setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

addMouseListener(new MouseAdapter() {
Expand Down
Loading

0 comments on commit ca04cf9

Please sign in to comment.