diff --git a/sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip b/sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip index 9bed9830e..500d32a02 100644 Binary files a/sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip and b/sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip differ diff --git a/sources/net.sf.j2s.core/dist/swingjs/net.sf.j2s.core.jar b/sources/net.sf.j2s.core/dist/swingjs/net.sf.j2s.core.jar index 585026fb9..3c9d7c2f4 100644 Binary files a/sources/net.sf.j2s.core/dist/swingjs/net.sf.j2s.core.jar and b/sources/net.sf.j2s.core/dist/swingjs/net.sf.j2s.core.jar differ diff --git a/sources/net.sf.j2s.core/dist/swingjs/timestamp b/sources/net.sf.j2s.core/dist/swingjs/timestamp index 1c5c13209..e2abd2e3e 100644 --- a/sources/net.sf.j2s.core/dist/swingjs/timestamp +++ b/sources/net.sf.j2s.core/dist/swingjs/timestamp @@ -1 +1 @@ -20230202011832 +20230216124208 diff --git a/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/SwingJS-site.zip b/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/SwingJS-site.zip index 9bed9830e..500d32a02 100644 Binary files a/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/SwingJS-site.zip and b/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/SwingJS-site.zip differ diff --git a/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/net.sf.j2s.core.jar b/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/net.sf.j2s.core.jar index 585026fb9..3c9d7c2f4 100644 Binary files a/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/net.sf.j2s.core.jar and b/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/net.sf.j2s.core.jar differ diff --git a/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/timestamp b/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/timestamp index 1c5c13209..e2abd2e3e 100644 --- a/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/timestamp +++ b/sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/timestamp @@ -1 +1 @@ -20230202011832 +20230216124208 diff --git a/sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java b/sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java index 06d5e42f6..5f5c4d829 100644 --- a/sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java +++ b/sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java @@ -25,11 +25,12 @@ public class CorePlugin extends Plugin { * "net.sf.j2s.core.jar" not "net.sf.j2s.core.3.2.5" * */ - public static String VERSION = "3.3.1-v5"; + public static String VERSION = "3.3.1-v6"; // if you change the x.x.x number, be sure to also indicate that in // j2sApplet.js and also (Bob only) update.bat, update-clean.bat + // BH 2023.02.09 -- 3.3.1.v6 fixes j2s.excluded.paths needing /src/xxxx // BH 2022.06.27 -- 3.3.1-v5 fixes missing method annotations // BH 2022.01.17 -- 3.3.1-v4 fixes default interface methods referencing their own static fields // BH 2021.01.14 -- 3.3.1-v3 fixes missing finals for nested () -> {...} diff --git a/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java b/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java index 754bd0ef6..3ce9fa42e 100644 --- a/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java +++ b/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java @@ -428,10 +428,13 @@ private void initializeUsing(File j2sFile, int level) { } boolean excludeFile(IFile javaSource) { - String filePath = javaSource.getFullPath().toString(); + return excludeFile(javaSource.getFullPath().toString()); + } + + private boolean excludeFile(String filePath) { if (lstExcludedPaths != null) { for (int i = lstExcludedPaths.size(); --i >= 0;) - if (filePath.startsWith(lstExcludedPaths.get(i))) { + if (filePath.indexOf(lstExcludedPaths.get(i)) >= 0) { return true; } } @@ -746,6 +749,8 @@ private void copySiteResources(File from, File dest) { private void copyNonclassFiles(File dir, File target) { if (dir.equals(target)) return; + if (excludeFile(dir.getAbsolutePath().replace('\\','/') + "/")) + return; File[] files = dir.listFiles(filter); File f = null; if (files != null) diff --git a/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java b/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java index 3df2c6d29..c84d166b3 100644 --- a/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java +++ b/sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java @@ -134,6 +134,7 @@ import org.eclipse.jdt.core.dom.WildcardType; // TODO: superclass inheritance for JAXB XmlAccessorType +// TODO: Transpiler bug allows static String name, but JavaScript function().name is read-only and will be "clazz" //BH 2022.01.17 -- 3.3.1-v4 fixes default interface methods referencing their own static fields //BH 2021.01.14 -- 3.3.1-v3 fixes missing finals for nested () -> {...} diff --git a/sources/net.sf.j2s.java.core/dist/SwingJS-site.zip b/sources/net.sf.j2s.java.core/dist/SwingJS-site.zip index 9bed9830e..500d32a02 100644 Binary files a/sources/net.sf.j2s.java.core/dist/SwingJS-site.zip and b/sources/net.sf.j2s.java.core/dist/SwingJS-site.zip differ diff --git a/sources/net.sf.j2s.java.core/src/java/awt/Container.java b/sources/net.sf.j2s.java.core/src/java/awt/Container.java index a7850bc07..d3ae7899b 100644 --- a/sources/net.sf.j2s.java.core/src/java/awt/Container.java +++ b/sources/net.sf.j2s.java.core/src/java/awt/Container.java @@ -2447,7 +2447,8 @@ static class DropTargetEventTargetFilter implements EventTargetFilter { private DropTargetEventTargetFilter() {} - public boolean accept(final Component comp) { + @Override + public boolean accept(final Component comp) { DropTarget dt = comp.getDropTarget(); return dt != null && dt.isActive(); } diff --git a/sources/net.sf.j2s.java.core/src/java/util/Objects.java b/sources/net.sf.j2s.java.core/src/java/util/Objects.java index 0198356b0..51ed99f25 100644 --- a/sources/net.sf.j2s.java.core/src/java/util/Objects.java +++ b/sources/net.sf.j2s.java.core/src/java/util/Objects.java @@ -204,6 +204,44 @@ public static T requireNonNull(T obj) { return obj; } + /** + * Returns the first argument if it is non-{@code null} and + * otherwise returns the non-{@code null} second argument. + * + * @param obj an object + * @param defaultObj a non-{@code null} object to return if the first argument + * is {@code null} + * @param the type of the reference + * @return the first argument if it is non-{@code null} and + * otherwise the second argument if it is non-{@code null} + * @throws NullPointerException if both {@code obj} is null and + * {@code defaultObj} is {@code null} + * @since 9 + */ + public static T requireNonNullElse(T obj, T defaultObj) { + return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj"); + } + + /** + * Returns the first argument if it is non-{@code null} and otherwise + * returns the non-{@code null} value of {@code supplier.get()}. + * + * @param obj an object + * @param supplier of a non-{@code null} object to return if the first argument + * is {@code null} + * @param the type of the first argument and return type + * @return the first argument if it is non-{@code null} and otherwise + * the value from {@code supplier.get()} if it is non-{@code null} + * @throws NullPointerException if both {@code obj} is null and + * either the {@code supplier} is {@code null} or + * the {@code supplier.get()} value is {@code null} + * @since 9 + */ + public static T requireNonNullElseGet(T obj, Supplier supplier) { + return (obj != null) ? obj + : requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()"); + } + /** * Checks that the specified object reference is not {@code null} and * throws a customized {@link NullPointerException} if it is. This method diff --git a/sources/net.sf.j2s.java.core/src/javajs/async/AsyncFileChooser.java b/sources/net.sf.j2s.java.core/src/javajs/async/AsyncFileChooser.java index 080108da9..9352546bc 100644 --- a/sources/net.sf.j2s.java.core/src/javajs/async/AsyncFileChooser.java +++ b/sources/net.sf.j2s.java.core/src/javajs/async/AsyncFileChooser.java @@ -1,219 +1,267 @@ -package javajs.async; - - -import java.awt.Component; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.io.File; -import java.util.function.Function; - -import javax.swing.JFileChooser; -import javax.swing.JOptionPane; -import javax.swing.filechooser.FileSystemView; - -/** - * A simple Asynchronous file chooser for JavaScript and Java. - * - * Requires an OK runnable; JavaScript can return notification of cancel for - * file reading only, not saving. - * - * @author Bob Hanson - */ - -public class AsyncFileChooser extends JFileChooser implements PropertyChangeListener { - - private int optionSelected; - private Runnable ok, cancel; // sorry, no CANCEL in JavaScript for file open - private boolean isAsyncSave = true; - private static boolean notified; - - public AsyncFileChooser() { - super(); - } - - public AsyncFileChooser(File file) { - super(file); - } - - public AsyncFileChooser(File file, FileSystemView view) { - super(file, view); - } - - @Deprecated - @Override - public int showDialog(Component frame, String btnText) { - // This one can come from JFileChooser - default is OPEN - return super.showDialog(frame, btnText); - } - - private int err() { - try { - throw new java.lang.IllegalAccessException("Warning! AsyncFileChooser interface bypassed!"); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return JFileChooser.ERROR_OPTION; - } - - @Deprecated - @Override - public int showOpenDialog(Component frame) { - return err(); - } - - @Override - public int showSaveDialog(Component frame) { - isAsyncSave = false; - ok = cancel = null; - return super.showSaveDialog(frame); - } - - /** - * - * @param frame - * @param btnLabel "open" or "save" - * @param ok - * @param cancel must be null; JavaScript cannot capture a cancel from a file dialog - */ - public void showDialog(Component frame, String btnLabel, Runnable ok, Runnable cancel) { - this.ok = ok; - if (getDialogType() != JFileChooser.SAVE_DIALOG && cancel != null) - notifyCancel(); - process(super.showDialog(frame, btnLabel)); - } - - /** - * - * @param frame - * @param ok - * @param cancel must be null; JavaScript cannot capture a cancel from a file dialog - */ - public void showOpenDialog(Component frame, Runnable ok, Runnable cancel) { - this.ok = ok; - if (cancel != null) - notifyCancel(); - process(super.showOpenDialog(frame)); - } - - /** - * - * This just completes the set. It is not necessary for JavaScript, because JavaScript - * will just throw up a simple modal OK/Cancel message anyway. - * - * @param frame - * @param ok - * @param cancel must be null - */ - public void showSaveDialog(Component frame, Runnable ok, Runnable cancel) { - this.ok = ok; - this.cancel = cancel; - process(super.showSaveDialog(frame)); - } - - - /** - * Locate a file for input or output. Note that JavaScript will not return on cancel for OPEN_DIALOG. - * - * @param title The title for the dialog - * @param mode OPEN_DIALOG or SAVE_DIALOG - * @param processFile function to use when complete - */ - public static void getFileAsync(Component parent, String title, int mode, Function processFile) { - // BH no references to this method. So changing its signature for asynchonous use - // And it didn't do as advertised - ran System.exit(0) if canceled - // create and display a file dialog - AsyncFileChooser fc = new AsyncFileChooser(); - fc.setDialogTitle(title); - Runnable after = new Runnable() { - - @Override - public void run() { - processFile.apply(fc.getSelectedFile()); - } - - }; - if (mode == JFileChooser.OPEN_DIALOG) { - fc.showOpenDialog(parent, after, after); - } else { - fc.showSaveDialog(parent, after, after); - } - - } - - /** - * Run yes.run() if a file doesn't exist or if the user allows it, else run no.run() - * @param parent - * @param filename - * @param title - * @param yes (approved) - * @param no (optional) - */ - public static void checkReplaceFileAsync(Component parent, File outfile, String title, Runnable yes, Runnable no) { - if (outfile.exists()) { - AsyncDialog.showYesNoAsync(parent, - outfile + " exists. Replace it?", null, new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - switch (e.getID()) { - case JOptionPane.YES_OPTION: - yes.run(); - break; - default: - if (no != null) - no.run(); - break; - } - } - - }); - - } else { - yes.run(); - } - - } - - private void notifyCancel() { - if (!notified) { - System.err.println("developer note: JavaScript cannot fire a FileChooser CANCEL action"); - } - notified = true; - } - - @Override - public void propertyChange(PropertyChangeEvent evt) { - switch (evt.getPropertyName()) { - case "SelectedFile": - case "SelectedFiles": - - process(optionSelected = (evt.getNewValue() == null ? CANCEL_OPTION : APPROVE_OPTION)); - break; - } - } - - private void process(int ret) { - if (ret != -(-ret)) - return; // initial JavaScript return is NaN - optionSelected = ret; - File f = getSelectedFile(); - if (f == null) { - if (cancel != null) - cancel.run(); - } else { - if (ok != null) - ok.run(); - } - } - - public int getSelectedOption() { - return optionSelected; - } - - public static byte[] getFileBytes(File f) { - return /** @j2sNative f.秘bytes || */null; - } - -} +package javajs.async; + + +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.File; +import java.util.function.Consumer; +import java.util.function.Function; + +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileSystemView; + +/** + * A simple Asynchronous file chooser for JavaScript and Java. + * + * Requires an onOK runnable; JavaScript can return notification of onCancel for + * file reading on all platforms, maybe not for saving on all; now using window.focus. + * + * @author Bob Hanson + */ + +public class AsyncFileChooser extends JFileChooser implements PropertyChangeListener { + + private int optionSelected; + private Runnable onOK, onCancel; + @SuppressWarnings("unused") + private boolean isAsyncSave = true; + private Consumer whenDone; + + // private static boolean notified; + + public AsyncFileChooser() { + super(); + } + + public AsyncFileChooser(File file) { + super(file); + } + + public AsyncFileChooser(File file, FileSystemView view) { + super(file, view); + } + + @Deprecated + @Override + public int showDialog(Component frame, String btnText) { + // This one can come from JFileChooser - default is OPEN + return super.showDialog(frame, btnText); + } + + private int err() { + try { + throw new java.lang.IllegalAccessException("Warning! AsyncFileChooser interface bypassed!"); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return JFileChooser.ERROR_OPTION; + } + + @Deprecated + @Override + public int showOpenDialog(Component frame) { + return err(); + } + + @Override + public int showSaveDialog(Component frame) { + isAsyncSave = false; + onOK = onCancel = null; + return super.showSaveDialog(frame); + } + + /** + * + * @param frame + * @param btnLabel "open" or "save" + * @param onOK + * @param onCancel if the dialog is onCanceled + */ + public void showDialog(Component frame, String btnLabel, Runnable onOK, Runnable onCancel) { + this.onOK = onOK; + this.onCancel = onCancel; + process(super.showDialog(frame, btnLabel)); + } + + /** + * Convenience function for single-file return. + * Note that in JavaScript "onCancel" is not guaranteed on all platforms, as it + * relies on a JavaScript window.focus event that may not work on some devices. + * + * + * @param frame + * @param btnLabel "open" or "save" + * @param whenDone will return with a File object or null + */ + public void showDialog(Component frame, String btnLabel, Consumer whenDone) { + this.whenDone = whenDone; + setMultiSelectionEnabled(false); + process(super.showDialog(frame, btnLabel)); + } + + /** + * + * @param frame + * @param onOK + * @param onCancel must be null; JavaScript cannot capture a onCancel from a file dialog + */ + public void showOpenDialog(Component frame, Runnable onOK, Runnable onCancel) { + this.onOK = onOK; + this.onCancel = onCancel; + process(super.showOpenDialog(frame)); + } + + /** + * Convenience function for single-file return. + * Note that a null return is not guaranteed on all platforms, as it + * relies on a JavaScript window.focus event that may not work on some devices. + * + * + * @param frame + * @param whenDone will return with a File object or null + */ + public void showOpenDialog(Component frame, Consumer whenDone) { + this.whenDone = whenDone; + setMultiSelectionEnabled(false); + process(super.showOpenDialog(frame)); + } + + /** + * + * This just completes the set. It is not necessary for JavaScript, because JavaScript + * will just throw up a simple modal OK/Cancel message anyway. + * + * @param frame + * @param onOK + * @param onCancel must be null + */ + public void showSaveDialog(Component frame, Runnable onOK, Runnable onCancel) { + this.onOK = onOK; + this.onCancel = onCancel; + process(super.showSaveDialog(frame)); + } + + /** + * + * This just completes the set. It is not necessary for JavaScript, because JavaScript + * will just throw up a simple modal OK/Cancel message anyway. + * + * @param frame + * @param whenDone + */ + public void showSaveDialog(Component frame, Consumer whenDone) { + this.whenDone = whenDone; + process(super.showSaveDialog(frame)); + } + + + /** + * Locate a file for input or output. Note that JavaScript will not return on onCancel for OPEN_DIALOG. + * + * @param title The title for the dialog + * @param mode OPEN_DIALOG or SAVE_DIALOG + * @param processFile function to use when complete + */ + public static void getFileAsync(Component parent, String title, int mode, Function processFile) { + // BH no references to this method. So changing its signature for asynchonous use + // And it didn't do as advertised - ran System.exit(0) if onCanceled + // create and display a file dialog + AsyncFileChooser fc = new AsyncFileChooser(); + fc.setDialogTitle(title); + Runnable after = new Runnable() { + + @Override + public void run() { + processFile.apply(fc.getSelectedFile()); + } + + }; + if (mode == JFileChooser.OPEN_DIALOG) { + fc.showOpenDialog(parent, after, after); + } else { + fc.showSaveDialog(parent, after, after); + } + + } + + /** + * Run yes.run() if a file doesn't exist or if the user allows it, else run no.run() + * @param parent + * @param filename + * @param title + * @param yes (approved) + * @param no (optional) + */ + public static void checkReplaceFileAsync(Component parent, File outfile, String title, Runnable yes, Runnable no) { + if (outfile.exists()) { + AsyncDialog.showYesNoAsync(parent, + outfile + " exists. Replace it?", null, new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + switch (e.getID()) { + case JOptionPane.YES_OPTION: + yes.run(); + break; + default: + if (no != null) + no.run(); + break; + } + } + + }); + + } else { + yes.run(); + } + + } + +// private void notifyCancel() { +// if (!notified) { +// System.err.println("developer note: JavaScript cannot fire a FileChooser CANCEL action"); +// } +// notified = true; +// } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + switch (evt.getPropertyName()) { + case "SelectedFile": + case "SelectedFiles": + + process(optionSelected = (evt.getNewValue() == null ? CANCEL_OPTION : APPROVE_OPTION)); + break; + } + } + + private void process(int ret) { + if (ret != -(-ret)) + return; // initial JavaScript return is NaN + optionSelected = ret; + File f = getSelectedFile(); + if (whenDone != null) { + whenDone.accept(f); + } else if (f == null) { + if (onCancel != null) + onCancel.run(); + } else { + if (onOK != null) + onOK.run(); + } + } + + public int getSelectedOption() { + return optionSelected; + } + + public static byte[] getFileBytes(File f) { + return /** @j2sNative f.秘bytes || */null; + } + +} diff --git a/sources/net.sf.j2s.java.core/src/swingjs/api/js/HTML5Video.java b/sources/net.sf.j2s.java.core/src/swingjs/api/js/HTML5Video.java index b10c08bff..3b6ef3b85 100644 --- a/sources/net.sf.j2s.java.core/src/swingjs/api/js/HTML5Video.java +++ b/sources/net.sf.j2s.java.core/src/swingjs/api/js/HTML5Video.java @@ -187,7 +187,7 @@ public static void setProperty(HTML5Video jsvideo, String key, Object value) { * @param key * @return value or value boxed as Double or Boolean */ - @SuppressWarnings("unused") + @SuppressWarnings({ "unused", "null" }) public static Object getProperty(HTML5Video jsvideo, String key) { Object val = (/** @j2sNative 1? jsvideo[key] : */ null); @@ -253,7 +253,7 @@ public Void apply(Object jsevent) { * Remove action listener * * @param jsvideo the HTML5 video element - * @param listeners an array of event/listener epairs created by + * @param listeners an array of event/listener pairs created by * addActionListener */ public static void removeActionListener(HTML5Video jsvideo, Object[] listeners) { @@ -318,9 +318,17 @@ public static JDialog createDialog(Frame parent, Object source, int maxWidth, return createDialog(parent, source, maxWidth, true, whenReady); } + public static class HTML5VideoDialog extends JDialog { + + public HTML5VideoDialog(Frame parent) { + super(parent); + } + public JPanel controls; + + } public static JDialog createDialog(Frame parent, Object source, int maxWidth, boolean addControls, Function whenReady) { - JDialog dialog = new JDialog(parent); + HTML5VideoDialog dialog = new HTML5VideoDialog(parent); Container p = dialog.getContentPane(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); JLabel label = (source instanceof JLabel ? (JLabel) source : createLabel(source)); @@ -328,13 +336,21 @@ public static JDialog createDialog(Frame parent, Object source, int maxWidth, bo // not in Java! dialog.putClientProperty("jsvideo", label); p.add(label); label.setVisible(false); - if (addControls) - p.add(getControls(label)); + JPanel q = dialog.controls = getControls(label); + p.add(q); + q.setVisible(addControls); + label.putClientProperty("controls", q); dialog.setModal(false); dialog.pack(); dialog.setVisible(true); dialog.setVisible(false); HTML5Video jsvideo = (HTML5Video) label.getClientProperty("jsvideo"); + /** + * @j2sNative + * + * jsvideo.dialog = dialog; + * + */ Object[] j2sListener = HTML5Video.addActionListener(jsvideo, new ActionListener() { @Override @@ -439,6 +455,30 @@ public static int getFrameCount(HTML5Video jsvideo) { return (int) (getDuration(jsvideo) / 0.033334); } + public static void startVideo(HTML5Video jsvideo) { + @SuppressWarnings("unused") + HTML5VideoDialog d = /** @j2sNative jsvideo.dialog || */null; + try { + /** + * @j2sNative + * + * var promise = jsvideo.play(); + * if (promise !== undefined) { promise["catch"](function(){ + * d.controls.setVisible$Z(true);d.pack$(); + * if (!d.t) + * d.t = setTimeout(function(){ + * alert("Please press OK and then the play button.")} + * ,1000); }); } + * + */ + } catch (Throwable e) { + e.printStackTrace(); + } + + // TODO Auto-generated method stub + + } + // HTMLMediaElement properties // audioTracks diff --git a/sources/net.sf.j2s.java.core/src/test/Test_J8_lambdafinal.java b/sources/net.sf.j2s.java.core/src/test/Test_J8_lambdafinal.java new file mode 100644 index 000000000..f0a585efe --- /dev/null +++ b/sources/net.sf.j2s.java.core/src/test/Test_J8_lambdafinal.java @@ -0,0 +1,52 @@ +package test; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import java.util.function.BiPredicate; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.ToIntFunction; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import javax.swing.JButton; +import javax.swing.SwingUtilities; +import javax.swing.Timer; + +import test.baeldung.doublecolon.Computer; +import test.baeldung.doublecolon.MacbookPro; + +public class Test_J8_lambdafinal extends Test_ { + + + public static void main(String args[]) { + + testFinal(); + } + + private static void testFinal() { + + Runnable r = () -> { + for (String s : new String[] { "a", "b" }) { + Runnable r1 = () -> { + for (int i : new int[] { 0, 1, 2 }) { + System.out.println(i + " " + s); + } + + }; + r1.run(); + } + }; + r.run(); + } + +} diff --git a/sources/net.sf.j2s.java.core/src/test/Test_Java8.java b/sources/net.sf.j2s.java.core/src/test/Test_Java8.java index 73afb2849..af655c91f 100644 --- a/sources/net.sf.j2s.java.core/src/test/Test_Java8.java +++ b/sources/net.sf.j2s.java.core/src/test/Test_Java8.java @@ -221,6 +221,58 @@ public void actionPerformed(ActionEvent e) { }); } + private static void checkData(Object data) { + double[] datae = data instanceof String[] ? Stream.of((String[]) data).mapToDouble(Double::parseDouble).toArray() : + IntStream.of((int[]) data).mapToDouble(i ->i).toArray(); + System.out.println(Arrays.toString(datae)); + + } + + public DoubleStream values() { + return IntStream.range(0, 4) + .mapToDouble(this::get); + } + + + public double get(int row) { + return row; + } + + + private static void testToIntFunction(TestFunc tf, Function f) { + int val = f.apply(tf); + System.out.println("testToIntFunction =" + val); + } + + private static void testToIntFunction(Object tf) { + System.out.println("testToIntFunction = " + tf); + } + + private static void testFunction(String function, int i) { + + // Function + + int[][][] a; + + Function ifi = new Function() { + + @Override + public int[][][] apply(Integer t) { + System.out.println(function); + return new int[t.intValue()][][]; + } + }; + + a = ifi.apply(i); + System.out.println("a length is " + a.length); + + } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + // checking here that this is not qualified + } + public static void main(String[] args) { Test_Java8 t8b = new Test_Java8(); @@ -415,55 +467,5 @@ public void run() { System.out.println("Test_Java8 OK"); } - private static void checkData(Object data) { - double[] datae = data instanceof String[] ? Stream.of((String[]) data).mapToDouble(Double::parseDouble).toArray() : - IntStream.of((int[]) data).mapToDouble(i ->i).toArray(); - System.out.println(Arrays.toString(datae)); - - } - - public DoubleStream values() { - return IntStream.range(0, 4) - .mapToDouble(this::get); - } - - - public double get(int row) { - return row; - } - - private static void testToIntFunction(TestFunc tf, Function f) { - int val = f.apply(tf); - System.out.println("testToIntFunction =" + val); - } - - private static void testToIntFunction(Object tf) { - System.out.println("testToIntFunction = " + tf); - } - - private static void testFunction(String function, int i) { - - // Function - - int[][][] a; - - Function ifi = new Function() { - - @Override - public int[][][] apply(Integer t) { - System.out.println(function); - return new int[t.intValue()][][]; - } - }; - - a = ifi.apply(i); - System.out.println("a length is " + a.length); - - } - - @Override - public void propertyChange(PropertyChangeEvent evt) { - // checking here that this is not qualified - } } diff --git a/sources/net.sf.j2s.java.core/src/test/Test_Objects.java b/sources/net.sf.j2s.java.core/src/test/Test_Objects.java new file mode 100644 index 000000000..902a28aca --- /dev/null +++ b/sources/net.sf.j2s.java.core/src/test/Test_Objects.java @@ -0,0 +1,24 @@ +package test; + +import java.util.Objects; + +class Test_Objects extends Test_ { + + @SuppressWarnings("unused") + public static void main(String[] args) { + + Object x = ""; + Objects.requireNonNull(x); + if (/** @j2sNative true || */ + false) { + x = "ok"; + x = Objects.requireNonNullElse(x, "testing"); + assert (x == "ok"); + x = null; + x = Objects.requireNonNullElse(x, "testing"); + assert (x == "testing"); + } + System.out.println("Test_Objects OK"); + } + +} \ No newline at end of file diff --git a/sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js b/sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js index 51c10d98e..3cf9b4718 100644 --- a/sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js +++ b/sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js @@ -1,5 +1,6 @@ // j2sApplet.js BH = Bob Hanson hansonr@stolaf.edu +// BH 2023.02.04 adds support for file load cancel // BH 2023.01.10 j2sargs typo // BH 2022.08.27 fix frame resizing for browsers reporting noninteger pageX, pageY // BH 2022.06.23 implements J2S._lastAppletID @@ -1192,6 +1193,7 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) { if (--nfiles == 0) { J2S.$remove(id); J2S.$remove("_filereader_modalscreen"); + reader = null; fDone(data, file.name); } }; @@ -1202,13 +1204,25 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) { // These browers require that the user see and click the link. if (J2S._canClickFileReader) { var x = document.createElement("input"); + x.value = x.text = "xxxx"; x.type = "file"; if (isMultiple) x.setAttribute("multiple", "true"); - x.onchange = function(ev) { + x.addEventListener("change", function(ev) { + J2S._fileReaderCancelListener = null; + window.removeEventListener("focus", J2S._fileReaderCancelListener); (isMultiple ? readFiles(this.files) : readFile(this.files[0])); - }; + }, false); x.click(); + window.addEventListener("focus", J2S._fileReaderCancelListener = function(a){ + setTimeout(function() { + window.removeEventListener("focus", J2S._fileReaderCancelListener); + if (J2S._fileReaderCancelListener != null) { + J2S._fileReaderCancelListener = null; + fDone(null, null); + } + },500) + }); } else { var px = screen.width / 2 - 180; var py = screen.height / 2 - 40; @@ -1235,6 +1249,7 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) { J2S.$appEvent("#" + id + "_cancel", null, "click", function(evt) { J2S.$remove(id); J2S.$remove("_filereader_modalscreen"); + fDone(null, null); }); J2S.$css(J2S.$("#" + id), { display : "block" @@ -1981,7 +1996,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) { var xym = getXY(who, ev, 0); if (!xym) return false; - who.applet._processEvent(505, xym, ev);// MouseEvent.MOUSE_EXITED + who.applet._processEvent(505, xym, ev, who._frameViewer);// MouseEvent.MOUSE_EXITED return false; } diff --git a/sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js b/sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js index 128a16264..e2ff78aab 100644 --- a/sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js +++ b/sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js @@ -2398,15 +2398,12 @@ _Loader.loadPackageClasspath = function (pkg, base, isIndex, fSuccess, mode, pt) if (base) // critical for multiple applets map["@" + pkg] = base; if (isIndex && !isPkgDeclared && !J2S.getGlobal(pkg + ".registered")) { - // pkgRefCount++; + // the package idea has been deprecated + // the only package is core/package.js if (pkg == "java") pkg = "core" // JSmol -- moves java/package.js to core/package.js - _Loader.loadClass(pkg + ".package", function () { - // if (--pkgRefCount == 0) - // runtimeLoaded(); - // fSuccess && fSuccess(); - }, true, true, 1); - return; + // not really asynchronous + _Loader.loadClass(pkg + ".package", null, true, true, 1); } fSuccess && fSuccess(); }; @@ -2628,6 +2625,7 @@ Clazz._getClassCount = function() { } Clazz._4Name = function(clazzName, applet, state, asClazz, initialize, isQuiet) { + // applet and state always null in SwingJS var cl; if (clazzName.indexOf("[") == 0) { cl = getArrayClass(clazzName); @@ -2755,16 +2753,9 @@ _Loader.MODE_SCRIPT = 4; _Loader.MODE_XHR = 2; _Loader.MODE_SYNC = 1; -/** - * String mode: asynchronous modes: async(...).script, async(...).xhr, - * async(...).xmlhttprequest, script.async(...), xhr.async(...), - * xmlhttprequest.async(...), script - * - * synchronous modes: sync(...).xhr, sync(...).xmlhttprequest, xhr.sync(...), - * xmlhttprequest.sync(...), xmlhttprequest, xhr - * - * Integer mode: Script 4; XHR 2; SYNC bit 1; - */ +// Integer mode: Script 4; XHR 2; SYNC bit 1; +// async is currently ignored + /* public */ _Loader.setLoadingMode = function (mode, timeLag) { var async = true; @@ -2782,10 +2773,10 @@ _Loader.setLoadingMode = function (mode, timeLag) { else async = !(mode & _Loader.MODE_SYNC); } - isUsingXMLHttpRequest = ajax; - isAsynchronousLoading = async; - loadingTimeLag = (async && timeLag >= 0 ? timeLag: -1); - return async; + isUsingXMLHttpRequest = ajax; // ignored + isAsynchronousLoading = async; // ignored + loadingTimeLag = (async && timeLag >= 0 ? timeLag: -1); // ignored + return async; // will be false }; /* @@ -3405,7 +3396,7 @@ setps(Sys.err, function(s) {Con.consoleOutput(s, "red")}); Clazz._Loader.registerPackages("java", [ "io", "lang", "lang.reflect", "util" ]); -J2S.setGlobal("java.registered", true); +// old J2S.setGlobal("java.registered", true); // /////////////// special definitions of standard Java class methods // /////////// diff --git a/sources/net.sf.j2s.java.core/srcjs/swingjs2.js b/sources/net.sf.j2s.java.core/srcjs/swingjs2.js index 2f3cbdd90..4940f0fee 100644 --- a/sources/net.sf.j2s.java.core/srcjs/swingjs2.js +++ b/sources/net.sf.j2s.java.core/srcjs/swingjs2.js @@ -10686,6 +10686,7 @@ return jQuery; })(jQuery,document,"click mousemove mouseup touchmove touchend", "outjsmol"); // j2sApplet.js BH = Bob Hanson hansonr@stolaf.edu +// BH 2023.02.04 adds support for file load cancel // BH 2023.01.10 j2sargs typo // BH 2022.08.27 fix frame resizing for browsers reporting noninteger pageX, pageY // BH 2022.06.23 implements J2S._lastAppletID @@ -11878,6 +11879,7 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) { if (--nfiles == 0) { J2S.$remove(id); J2S.$remove("_filereader_modalscreen"); + reader = null; fDone(data, file.name); } }; @@ -11888,13 +11890,25 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) { // These browers require that the user see and click the link. if (J2S._canClickFileReader) { var x = document.createElement("input"); + x.value = x.text = "xxxx"; x.type = "file"; if (isMultiple) x.setAttribute("multiple", "true"); - x.onchange = function(ev) { + x.addEventListener("change", function(ev) { + J2S._fileReaderCancelListener = null; + window.removeEventListener("focus", J2S._fileReaderCancelListener); (isMultiple ? readFiles(this.files) : readFile(this.files[0])); - }; + }, false); x.click(); + window.addEventListener("focus", J2S._fileReaderCancelListener = function(a){ + setTimeout(function() { + window.removeEventListener("focus", J2S._fileReaderCancelListener); + if (J2S._fileReaderCancelListener != null) { + J2S._fileReaderCancelListener = null; + fDone(null, null); + } + },500) + }); } else { var px = screen.width / 2 - 180; var py = screen.height / 2 - 40; @@ -11921,6 +11935,7 @@ if (database == "_" && J2S._serverUrl.indexOf("//your.server.here/") >= 0) { J2S.$appEvent("#" + id + "_cancel", null, "click", function(evt) { J2S.$remove(id); J2S.$remove("_filereader_modalscreen"); + fDone(null, null); }); J2S.$css(J2S.$("#" + id), { display : "block" @@ -12667,7 +12682,7 @@ if (ev.keyCode == 9 && ev.target["data-focuscomponent"]) { var xym = getXY(who, ev, 0); if (!xym) return false; - who.applet._processEvent(505, xym, ev);// MouseEvent.MOUSE_EXITED + who.applet._processEvent(505, xym, ev, who._frameViewer);// MouseEvent.MOUSE_EXITED return false; } @@ -16437,15 +16452,12 @@ _Loader.loadPackageClasspath = function (pkg, base, isIndex, fSuccess, mode, pt) if (base) // critical for multiple applets map["@" + pkg] = base; if (isIndex && !isPkgDeclared && !J2S.getGlobal(pkg + ".registered")) { - // pkgRefCount++; + // the package idea has been deprecated + // the only package is core/package.js if (pkg == "java") pkg = "core" // JSmol -- moves java/package.js to core/package.js - _Loader.loadClass(pkg + ".package", function () { - // if (--pkgRefCount == 0) - // runtimeLoaded(); - // fSuccess && fSuccess(); - }, true, true, 1); - return; + // not really asynchronous + _Loader.loadClass(pkg + ".package", null, true, true, 1); } fSuccess && fSuccess(); }; @@ -16667,6 +16679,7 @@ Clazz._getClassCount = function() { } Clazz._4Name = function(clazzName, applet, state, asClazz, initialize, isQuiet) { + // applet and state always null in SwingJS var cl; if (clazzName.indexOf("[") == 0) { cl = getArrayClass(clazzName); @@ -16794,16 +16807,9 @@ _Loader.MODE_SCRIPT = 4; _Loader.MODE_XHR = 2; _Loader.MODE_SYNC = 1; -/** - * String mode: asynchronous modes: async(...).script, async(...).xhr, - * async(...).xmlhttprequest, script.async(...), xhr.async(...), - * xmlhttprequest.async(...), script - * - * synchronous modes: sync(...).xhr, sync(...).xmlhttprequest, xhr.sync(...), - * xmlhttprequest.sync(...), xmlhttprequest, xhr - * - * Integer mode: Script 4; XHR 2; SYNC bit 1; - */ +// Integer mode: Script 4; XHR 2; SYNC bit 1; +// async is currently ignored + /* public */ _Loader.setLoadingMode = function (mode, timeLag) { var async = true; @@ -16821,10 +16827,10 @@ _Loader.setLoadingMode = function (mode, timeLag) { else async = !(mode & _Loader.MODE_SYNC); } - isUsingXMLHttpRequest = ajax; - isAsynchronousLoading = async; - loadingTimeLag = (async && timeLag >= 0 ? timeLag: -1); - return async; + isUsingXMLHttpRequest = ajax; // ignored + isAsynchronousLoading = async; // ignored + loadingTimeLag = (async && timeLag >= 0 ? timeLag: -1); // ignored + return async; // will be false }; /* @@ -17444,7 +17450,7 @@ setps(Sys.err, function(s) {Con.consoleOutput(s, "red")}); Clazz._Loader.registerPackages("java", [ "io", "lang", "lang.reflect", "util" ]); -J2S.setGlobal("java.registered", true); +// old J2S.setGlobal("java.registered", true); // /////////////// special definitions of standard Java class methods // ///////////