WindowJS - cross-mode alert(), confirm(), prompt() & other JS API for Java

edited November 2018 in Share Your Work

Comments

  • edited November 2018
  • edited November 2018
    /** 
     * No Repeat ID Input (v2.1)
     * by GoToLoop (2015/Sep/15)
     *
     * Forum.Processing.org/two/discussion/12532/
     * windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java#Item_2
     *
     * Forum.Processing.org/two/discussion/869/check-array-contents-with-arraylist
     *
     * Studio.ProcessingTogether.com/sp/pad/export/ro.9$Bjf6i21oXBw
     * Bl.ocks.org/GoToLoop/8017bc38b8c8e0a5b70b
     */
    
    import js.window;
    import java.util.List;
    
    final List<String> ids = new ArrayList<String>();
    {
      for (String s : window.Array("alert()", "confirm()", "prompt()"))  ids.add(s);
    }
    
    void draw() {
      for (final String id : ids)  print(id + " ");
      println(" ");
    
      final String id = window.prompt("Please enter new ID");
    
      if (id == null)  exit();
      else if (id.isEmpty())  window.alert("Empty ID Input!!!");
      else if (ids.contains(id))  window.alert("ID \"" + id + "\" exists already!");
      else {
        window.alert("ID \"" + id + "\" successfully added!!!");
        ids.add(id);
      }
    }
    
  • edited November 2018
    /**
     * Name:      WindowJS
     * Version:   1.2.1
     * Language:  Java 6
     *
     * Author:    GoToLoop
     * Date:      2015/Sep/15
     * License:   LGPL 2.1
     */
    
    // https://Gist.GitHub.com/GoToLoop/8017bc38b8c8e0a5b70b
    
    package js; // Uncomment "package js;" for ".java" and comment out for ".pde".
    
    import static javax.swing.JOptionPane.*;
    import java.lang.reflect.Array;
    
    //static // Uncomment "static" for ".pde" and comment out for ".java".
    public abstract class window {
      public static final window self = new window() {}, window = self;
      public static final Object undefined = null;
      public static final float NaN = Float.NaN;
      public static final float Infinity = Float.POSITIVE_INFINITY;
      public static String name = "";
    
      public static final Object alert() {
        return alert(null);
      }
    
      public static final Object alert(final Object msg) {
        showMessageDialog(null, msg);
        return null;
      }
    
      public static final boolean confirm() {
        return confirm(null);
      }
    
      public static final boolean confirm(final Object msg) {
        return showConfirmDialog(null, msg, "Confirm?", OK_CANCEL_OPTION)
          == OK_OPTION;
      }
    
      public static final String prompt() {
        return prompt(null);
      }
    
      public static final String prompt(final Object msg) {
        return showInputDialog(msg);
      }
    
      public static final String prompt(final Object msg, final Object val) {
        return showInputDialog(msg, val);
      }
    
      public static final boolean isNaN() {
        return true;
      }
    
      public static final boolean isNaN(final long val) {
        return false;
      }
    
      public static final boolean isNaN(final double val) {
        return val != val;
      }
    
      public static final boolean isNaN(final Object o) {
        if (o == null | o instanceof java.util.Date |
          o instanceof Boolean | o instanceof Character)  return false;
    
        if (o instanceof Number)  return isNaN(((Number) o).doubleValue());
    
        if (o.getClass().isArray()) {
          int len = Array.getLength(o);
          return len == 0? false : len > 1? true : isNaN(Array.get(o, 0));
        }
    
        final String s = o.toString().trim();
        if (s.isEmpty())  return false;
    
        try {
          return isNaN(Double.parseDouble(s));
        } 
        catch (final NumberFormatException e) {
          return true;
        }
      }
    
      public static final boolean isFinite() {
        return false;
      }
    
      public static final boolean isFinite(final long val) {
        return true;
      }
    
      public static final boolean isFinite(final double val) {
        return val != -Infinity & val != Infinity & val == val;
      }
    
      public static final boolean isFinite(final Object o) {
        if (o == null | o instanceof java.util.Date |
            o instanceof Boolean | o instanceof Character)  return true;
    
        if (o instanceof Number)  return isFinite(((Number) o).doubleValue());
    
        if (o.getClass().isArray()) {
          final int len = Array.getLength(o);
          return len == 0? true : len > 1? false : isFinite(Array.get(o, 0));
        }
    
        final String s = o.toString().trim();
        if (s.isEmpty())  return true;
    
        try {
          return isFinite(Double.parseDouble(s));
        } 
        catch (final NumberFormatException e) {
          return false;
        }
      }
    
      public static final String Date() {
        return new java.util.Date().toString();
      }
    
      public static final class Date extends java.util.Date {
        public static final long now() {
          return new java.util.Date().getTime();
        }
      }
    
      public static final Object[] Array(final int len) {
        return new Object[len];
      }
    
      @SafeVarargs public static final  T[] Array(final T... arr) {
        return arr == null? (T[]) new Object[0] : arr;
    }
    
  • edited March 2018

    Thanks for that!

    I am just working on it.

    I have a problem that when using confirm the msgbox is not in the foreground but behind the main window

    I have an mcve where this error does NOT occur (not helpful I know)

    But how can I make sure the msgbox gets in the foreground and has the focus? It's in P3D by the way.

    Thanks a ton as always!

    Chrisir ;-)

    /** 
     * 
     * by GoToLoop (2015/Sep/15)
     *
     * forum.Processing.org/two/discussion/12532/
     * windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java#Item_2
     *
     * forum.Processing.org/two/discussion/869/check-array-contents-with-arraylist
     *
     * studio.ProcessingTogether.com/sp/pad/export/ro.9$Bjf6i21oXBw
     * 
     * https : // forum.processing.org/two/discussion/12532/windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java
     *
     */
    
    
    
    void setup() {
      size(1333, 933);
      background(23);
    }
    
    void draw() {
      //
      background(23); 
      text("Hit mouse", 19, 19);
    }
    
    void mousePressed() {
      if (window.confirm("Do you really want to leave?")) { 
        println ("Yes");
        exit();
      }
    }
    
  • ah, it's because of P3D

    this mcve shows it (sometimes)

    solution...?

    void setup() {
      size(1333, 933, P3D);
      background(23);
    }
    
    void draw() {
      //
      background(23); 
      text("Hit mouse", 19, 19);
    }
    
    void mousePressed() {
      if (window.confirm("Do you really want to leave?")) { 
        println ("Yes");
        exit();
      }
    }
    
  • edited March 2018

    Sorry @Chrisir; but I've never tested it w/ other renderers yet. Just JAVA2D! ~:>

    This library simply invokes JOptionPane's static methods: showMessageDialog(), showConfirmDialog() & showInputDialog().

    Which respectively corresponds to JS' alert(), confirm() & prompt(). ~O)

    Therefore, that is simply a JOptionPane interaction glitch w/ Processing's OpenGL P3D renderer you've just found out AFAIK. :|

    As you can see, JOptionPane is a Swing/AWT class: 8-|
    import static javax.swing.JOptionPane.*;

    And almost n1 knows Processing doesn't behave that well when we use Swing/AWT classes. ^#(^

  • solution in P3D....?

  • edited March 2018

    @Chrisir, can you check out whether this Inputs class behaves correctly for all Processing's renderers: :-?
    https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1

    Aside from its readBoolean() method which directly invokes JOptionPane::showConfirmDialog(), everything else relies on a JDialog instance created outta a customized JOptionPane: :-B

    protected static final JDialog dialog = new JOptionPane(panel, QUESTION_MESSAGE) {
      @Override public void selectInitialValue() {
        field.requestFocusInWindow();
      }
    }
    .createDialog(null, TITLE);
    

    Notice its @Override method invokes method requestFocusInWindow() over a JTextField object. L-)

    Whether that's enough to force Processing's P3D and other renderers to always behave correctly related to foreground placement is your task now. #:-S

    Good luck! B-)

  • thanks!!

  • Thanks, I couldn't get it to run

    Do I need this library?

    No library found for iadt.creative.Inputs Libraries must be installed in a folder named 'libraries' inside the 'sketchbook' folder.

  • edited March 2018

    https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1

    • Just to make sure, I've copied & pasted "Test.pde" into the 1st main tab.
    • Then created a 2nd tab named exactly as "Inputs.java", and pasted Gist's "Inputs.java" there.
    • Hit CTRL+R and it compiled & run as it should under 64-bit PDE 3.3.5.
    • Indeed a red warning "No library found for iadt.creative.Inputs" appeared.
    • Just ignore it! :\">
  • Indeed a red warning "No library found for iadt.creative.Inputs" appeared.
    Just ignore it!

    I get similar warnings on some of my sketches that include local libraries which are not installed in the sketchbook libraries folder. The sketches still run fine.

    To remove the warnings you can (sometimes) also add the resource to sketchbook > libraries.

  • this works, I needed the class in test tab

    in the 2nd tab Inputs.java I had to comment out the package

    // package iadt.creative;
    

    I also receive huge bunch of warnings but it runs now in 2D

    /**
    
     https : // forum.processing.org/two/discussion/comment/119754#Comment_119754
    
    
     * Keyboard Input Library
     * Andrew Errity v0.2 (2015-Oct-01)
     * GoToLoop v1.0.2 (2015-Oct-22)
     *
     * https://Forum.Processing.org/two/discussion/13175/
     * do-whille-is-not-working-how-it-suppose-to#Item_12
     *
     * https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
     * https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
     */
    
    //import iadt.creative.Inputs.*;
    
    
    Inputs in1 = new Inputs();   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    void setup() {
      getSurface().setVisible(false);
    
      byte age;
      do {
        while ((age = in1.readByte("Enter your age:")) < 0);  //!!!!
        println("Your age is:", age);
      } while (in1.readBoolean("Again?"));     // !!!!!
    
      exit();
    }
    
    //
    
  • edited March 2018

    I also receive huge bunch of warnings...

    I've turned off warnings & auto code complete since the very beginning of P3. >:)

    In the 2nd tab "Inputs.java" I had to comment out the package.

    If you really had to, you're highly probably running a very old P3 version! 8-X

  • I also receive huge bunch of warnings...

    only when I enter a false age by the way!

    If you really had to, you're highly probably running a very old P3 version!

    no, I just installed 3.3.7 to make it work... ;-)

    here are some warnings:

    java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
        at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2062)
        at java.awt.Component.getLocationOnScreen(Component.java:2036)
        at javax.swing.text.JTextComponent$InputMethodRequestsHandler.getTextLocation(JTextComponent.java:4643)
        at sun.awt.im.InputMethodContext.getTextLocation(InputMethodContext.java:278)
        at sun.awt.windows.WInputMethod$1.run(WInputMethod.java:588)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
        at java.awt.EventQueue$4.run(EventQueue.java:733)
        at java.awt.EventQueue$4.run(EventQueue.java:731)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    
  • edited March 2018

    No, I just installed 3.3.7 to make it work...

    I'm still using PDE v3.3.5. And due to many folks reporting bugs for v3.3.6, I've decided to skip it completely. :-h

    I had high hopes for this latest v3.3.7. But after reading that huge list of warnings, I'm gonna hold on to v3.3.5 for a while again! :-\"

  • in P3D the InputBox is not in the foreground.

    Is there not a command for this...

    annoying...

  • edited March 2018

    Problem is, both renderers P2D & P3D are OpenGL-based wrapped by JOGL bindings: :-B
    https://JogAmp.org/

    Up till PDE v3.0a5, JOGL was somehow embedded as a Java's Applet. ~O)

    But after that, Processing's PApplet class isn't a subclass of Java's Applet anymore. :-&

    So I've got no idea how to control which component gets the right to be foreground:
    JOptionPane's Swing or P3D's JOGL. :-<

    You can check its API if you wish though: ^#(^
    https://JogAmp.org/deployment/jogamp-next/javadoc/jogl/javadoc/index.html?overview-summary.html

  • I would rather report it as a bug....

    but thank you!

    I had other difficulties in P3D with receiving keys etc. ...

Sign In or Register to comment.