From cde9e7e1db1e0eab9ad1a5a73c35529467f92eb2 Mon Sep 17 00:00:00 2001 From: skylarmt Date: Thu, 16 Apr 2015 20:13:26 -0600 Subject: [PATCH] Add filedialog() function, make everything work 100% with Python --- src/net/apocalypselabs/symat/Functions.java | 40 +++++++++++++++---- src/net/apocalypselabs/symat/functions.py | 18 ++++----- .../symat/resources/functions.txt | 3 +- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/net/apocalypselabs/symat/Functions.java b/src/net/apocalypselabs/symat/Functions.java index 22f87f1..88872f2 100644 --- a/src/net/apocalypselabs/symat/Functions.java +++ b/src/net/apocalypselabs/symat/Functions.java @@ -66,6 +66,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import java.util.prefs.Preferences; +import javax.swing.JFileChooser; import javax.swing.JOptionPane; import static net.apocalypselabs.symat.Main.API_URL; import org.matheclipse.core.eval.EvalUtilities; @@ -848,7 +849,10 @@ public class Functions { * @param max Maximum value, inclusive * @return random integer */ - public int rand(int min, int max) { + public double rand(int min, int max) { + if (min == 0 && max == 0) { + return rand(); + } return rng.nextInt((max - min) + 1) + min; } @@ -995,7 +999,9 @@ public class Functions { public void plot(String function) { showGraph(); - graphwin.graphFunction(function); + if (!function.equals("")) { + graphwin.graphFunction(function); + } } public void plot(double[] x, double[] y) { @@ -1023,15 +1029,20 @@ public class Functions { plot(f); } - public void plotname(String t) { - graphwin.setWindowTitle(t); - graphwin.setLabel(t); - } - public String plotname() { return graphwin.getTitle(); } + public String plotname(String t) { + if (t.equals("symatpythonnullplotname")) { + return graphwin.getTitle(); + } else { + graphwin.setWindowTitle(t); + graphwin.setLabel(t); + return ""; + } + } + public void plot() { showGraph(); } @@ -1061,6 +1072,21 @@ public class Functions { FileUtils.saveFile(content, path, false); } + /** + * Show a file dialog and return the path of the chosen file (or "" if + * canceled). + * + * @return + */ + public String filedialog() { + JFileChooser fc = new JFileChooser(); + int result = fc.showDialog(Main.maingui, "Choose"); + if (result == JFileChooser.APPROVE_OPTION) { + return fc.getSelectedFile().getPath(); + } + return ""; + } + public String md5sum(String data) { return FileUtils.MD5(data); } diff --git a/src/net/apocalypselabs/symat/functions.py b/src/net/apocalypselabs/symat/functions.py index d02b8ec..1dfe30a 100644 --- a/src/net/apocalypselabs/symat/functions.py +++ b/src/net/apocalypselabs/symat/functions.py @@ -14,14 +14,10 @@ def deg(num): return _.deg(num) def subs(fun,var): return _.subs(fun,var) -def plot(): - _.plot() -def plot(fun): +def plot(fun=""): _.plot(fun) -def plotname(fun): - _.plotname(fun) -def plotname(): - return _.plotname() +def plotname(fun='symatpythonnullplotname'): + return _.plotname(fun) def xlim(min,max): _.xlim(min,max) def plotclr(): @@ -50,9 +46,7 @@ def divide(*a): return _.divide(a) def mod(*a): return _.mod(a) -def rand(): - return _.rand() -def rand(min,max): +def rand(min=0,max=0): return _.rand(min,max) def randb(): return _.randb() @@ -93,4 +87,6 @@ def sech(a): def csch(a): return _.csch(a) def coth(a): - return _.coth(a) \ No newline at end of file + return _.coth(a) +def filedialog(): + return _.filedialog() \ No newline at end of file diff --git a/src/net/apocalypselabs/symat/resources/functions.txt b/src/net/apocalypselabs/symat/resources/functions.txt index 6c76e29..358d217 100644 --- a/src/net/apocalypselabs/symat/resources/functions.txt +++ b/src/net/apocalypselabs/symat/resources/functions.txt @@ -59,4 +59,5 @@ solve(f,'x',0)|Solve function f for 'x' when it equals 0. solve(f,'x')|Solve function f for 'x', assuming equal to 0. solve(f)|Solve function f, assuming 'x' and 0. printa(array)|Get array contents as text. -primes(n)|Find all prime numbers up to n. \ No newline at end of file +primes(n)|Find all prime numbers up to n. +filedialog()|Open a file chooser and return the chosen file path. \ No newline at end of file