Skip to content

Commit

Permalink
Fix for drag-and-drop not working correctly for URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielsherry committed Sep 1, 2024
1 parent 5679e21 commit 50d81f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.awt.Dimension;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Optional;

import javax.swing.JLabel;
import javax.swing.JPanel;
Expand All @@ -17,18 +18,30 @@

public class ErrorDisplayPanel extends JPanel {

private Throwable t;

private Optional<Throwable> t;


public ErrorDisplayPanel(Throwable t) {
this(null, t);
}

public ErrorDisplayPanel(String msg, Throwable t) {
if (t == null) {
throw new RuntimeException("Error Panel cannot be shown without exception");

String errorMessage = "";
String errorType = "";

if (t != null) {
errorType = t.getClass().getSimpleName();
errorMessage = t.getMessage();
if (errorMessage == null) {
errorMessage = "";
}
}
this.t = t;

this.t = Optional.of(t);


setLayout(new BorderLayout(Spacing.huge, Spacing.huge));
setBorder(Spacing.bHuge());

Expand All @@ -43,17 +56,12 @@ public ErrorDisplayPanel(String msg, Throwable t) {

JPanel north = new JPanel(new BorderLayout(Spacing.huge, Spacing.huge));

String title = "<div style='font-size: 115%; padding-top: 0px; padding-bottom: 5px;'>Error of type " + t.getClass().getSimpleName() + "</div>";
String title = "<div style='font-size: 115%; padding-top: 0px; padding-bottom: 5px;'>Error of type " + errorType + "</div>";
if (msg == null) {
msg = "";
}
String longMessage = "";
if (t.getMessage() != null) {
longMessage = t.getMessage();
}
if (longMessage == null) {
longMessage = "";
}

String longMessage = errorMessage;
if (longMessage.length() > 100) {
longMessage = longMessage.substring(0, 99) + "…";
}
Expand All @@ -70,9 +78,10 @@ public ErrorDisplayPanel(String msg, Throwable t) {
}

public String getStackTrace() {
if (t.isEmpty()) return "";
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
t.get().printStackTrace(pw);
return sw.toString();
}

Expand Down
2 changes: 1 addition & 1 deletion SwingUI/src/main/java/org/peakaboo/ui/swing/Peakaboo.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public static TaskMonitor<List<File>> getUrlsAsync(List<URL> urls, Consumer<Opti
File f = FileDrop.getUrlAsFile(url, urlProgress);
files.add(f);
} catch (IOException e) {
PeakabooLog.get().log(Level.SEVERE, "Failed to download file " + url.toString());
PeakabooLog.get().log(Level.SEVERE, "Failed to download file " + url.toString(), e);
return null;
}
count.set(count.get()+1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,24 @@ public FileDrop.Listener getFileDropListener() {

@Override
public void urlsDropped(URL[] urls) {
PeakabooLog.get().log(Level.INFO, "Received dropped URLs: " + Arrays.toString(urls));
PlotCanvas.this.urlsDropped(urls);
}

@Override
public void filesDropped(File[] files) {
PlotCanvas.this.filesDropped(files);
PeakabooLog.get().log(Level.INFO, "Received dropped Files: " + Arrays.toString(files));
PlotCanvas.this.filesDropped(List.of(files));
}
};
}

void filesDropped(File[] files) {
plotPanel.load(Arrays.asList(files).stream().map(PathDataInputAdapter::new).collect(Collectors.toList()));
}

void urlsDropped(URL[] urls) {
try {

TaskMonitor<List<File>> monitor = Peakaboo.getUrlsAsync(Arrays.asList(urls), optfiles -> {
if (!optfiles.isPresent()) { return; }
PeakabooLog.get().log(Level.INFO, "Downloaded URLs to files: " + optfiles.get().toString());
filesDropped(optfiles.get());
});

Expand All @@ -209,9 +208,7 @@ void urlsDropped(URL[] urls) {


void filesDropped(List<File> files) {
if (files.size() == 1 && PluginManager.isPluginFile(files.get(0)) ) {
plotPanel.load(files.stream().map(PathDataInputAdapter::new).collect(Collectors.toList()));
}
plotPanel.load(files.stream().map(PathDataInputAdapter::new).collect(Collectors.toList()));
}


Expand Down

0 comments on commit 50d81f1

Please sign in to comment.