Search in sources :

Example 46 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class WrapSwingUtilities method invokeLater.

@Signature(@Arg(value = "callback", type = HintType.CALLABLE))
public static Memory invokeLater(final Environment env, Memory... args) {
    final Invoker invoker = Invoker.valueOf(env, null, args[0]);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            ClassLoader old = thread.getContextClassLoader();
            thread.setUncaughtExceptionHandler(buildUncaughtExceptionHandler(env));
            try {
                thread.setContextClassLoader(env.scope.getClassLoader());
                invoker.callNoThrow();
            } finally {
                thread.setContextClassLoader(old);
            }
        }
    });
    return Memory.NULL;
}
Also used : Invoker(php.runtime.invoke.Invoker)

Example 47 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class WrapUIReader method onTranslate.

@Signature(@Arg(value = "handler", type = HintType.CALLABLE, optional = @Optional("NULL")))
public Memory onTranslate(final Environment env, Memory... args) {
    if (args[0].isNull())
        reader.setTranslateHandler(null);
    else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        UIReader.TranslateHandler handler = new UIReader.TranslateHandler() {

            @Override
            public Value onTranslate(Component component, Value var) {
                return new Value(invoker.callNoThrow(new ObjectMemory(UIElement.of(env, component)), var == null ? Memory.NULL : new StringMemory(var.asString())).toString());
            }
        };
        reader.setTranslateHandler(handler);
    }
    return Memory.NULL;
}
Also used : UIReader(org.develnext.jphp.swing.loader.UIReader) Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) Value(org.develnext.jphp.swing.loader.support.Value) StringMemory(php.runtime.memory.StringMemory)

Example 48 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class WrapUIReader method onRead.

@Signature(@Arg(value = "handler", type = HintType.CALLABLE, optional = @Optional("NULL")))
public Memory onRead(final Environment env, Memory... args) {
    if (args[0].isNull())
        reader.setReadHandler(null);
    else {
        final Invoker invoker = Invoker.valueOf(env, null, args[0]);
        UIReader.ReadHandler handler = new UIReader.ReadHandler() {

            @Override
            public void onRead(Component component, String var) {
                invoker.callNoThrow(new ObjectMemory(UIElement.of(env, component)), var == null ? Memory.NULL : new StringMemory(var));
            }
        };
        reader.setReadHandler(handler);
    }
    return Memory.NULL;
}
Also used : UIReader(org.develnext.jphp.swing.loader.UIReader) Invoker(php.runtime.invoke.Invoker) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory)

Example 49 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class UIFileChooser method onFileView.

@Signature({ @Arg(value = "field"), @Arg(value = "callback", type = HintType.CALLABLE, optional = @Optional("null")) })
public Memory onFileView(final Environment env, final Memory... args) {
    final FileView oldFileView = component.getFileView();
    final Invoker invoker = Invoker.valueOf(env, null, args[1]);
    final String field = args[0].toString().toLowerCase();
    component.setFileView(new FileView() {

        @Override
        public String getName(File f) {
            if (invoker != null && field.equals("name")) {
                Memory r = invoker.callNoThrow(new ObjectMemory(new FileObject(env, f)));
                if (!r.isNull())
                    return r.toString();
            }
            return oldFileView == null ? super.getName(f) : oldFileView.getName(f);
        }

        @Override
        public String getDescription(File f) {
            if (invoker != null && field.equals("description")) {
                Memory r = invoker.callNoThrow(new ObjectMemory(new FileObject(env, f)));
                if (!r.isNull())
                    return r.toString();
            }
            return oldFileView == null ? super.getDescription(f) : oldFileView.getDescription(f);
        }

        @Override
        public String getTypeDescription(File f) {
            return super.getTypeDescription(f);
        }

        @Override
        public Icon getIcon(File f) {
            if (invoker != null && field.equals("icon")) {
                Memory r = invoker.callNoThrow(new ObjectMemory(new FileObject(env, f)));
                if (r.instanceOf(WrapImage.class))
                    return r.toObject(WrapImage.class).getImageIcon();
            }
            return oldFileView == null ? super.getIcon(f) : oldFileView.getIcon(f);
        }

        @Override
        public Boolean isTraversable(File f) {
            if (invoker != null && field.equals("traversable")) {
                Memory r = invoker.callNoThrow(new ObjectMemory(new FileObject(env, f)));
                if (!r.isNull())
                    return r.toBoolean();
            }
            return oldFileView == null ? super.isTraversable(f) : oldFileView.isTraversable(f);
        }
    });
    return Memory.NULL;
}
Also used : FileView(javax.swing.filechooser.FileView) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) FileObject(php.runtime.ext.core.classes.stream.FileObject) WrapImage(org.develnext.jphp.swing.classes.WrapImage) File(java.io.File)

Example 50 with Invoker

use of php.runtime.invoke.Invoker in project jphp by jphp-compiler.

the class Stream method eachLine.

@Signature({ @Arg(value = "callback", type = HintType.CALLABLE), @Arg(value = "encoding", optional = @Optional("null")) })
public Memory eachLine(Environment env, Memory... args) throws Throwable {
    Invoker invoker = Invoker.create(env, args[0]);
    InputStream is = getInputStream(env, this);
    Scanner scanner = new Scanner(is, args[1].isNull() ? env.getDefaultCharset().name() : args[1].toString());
    int count = 0;
    while (scanner.hasNextLine()) {
        Memory call = invoker.call(StringMemory.valueOf(scanner.nextLine()));
        if (call.toBoolean()) {
            break;
        }
        count++;
    }
    return LongMemory.valueOf(count);
}
Also used : Scanner(java.util.Scanner) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) LongMemory(php.runtime.memory.LongMemory) ObjectMemory(php.runtime.memory.ObjectMemory) BinaryMemory(php.runtime.memory.BinaryMemory) StringMemory(php.runtime.memory.StringMemory)

Aggregations

Invoker (php.runtime.invoke.Invoker)50 Memory (php.runtime.Memory)21 ArrayMemory (php.runtime.memory.ArrayMemory)13 ForeachIterator (php.runtime.lang.ForeachIterator)12 LongMemory (php.runtime.memory.LongMemory)9 ObjectMemory (php.runtime.memory.ObjectMemory)9 KeyValueMemory (php.runtime.memory.KeyValueMemory)7 StringMemory (php.runtime.memory.StringMemory)7 Environment (php.runtime.env.Environment)6 ReferenceMemory (php.runtime.memory.ReferenceMemory)5 SplClassLoader (php.runtime.env.SplClassLoader)4 File (java.io.File)2 MemorySerializer (org.develnext.jphp.json.gson.MemorySerializer)2 UIReader (org.develnext.jphp.swing.loader.UIReader)2 TraceInfo (php.runtime.env.TraceInfo)2 FileObject (php.runtime.ext.core.classes.stream.FileObject)2 ActionEvent (java.awt.event.ActionEvent)1 MessageDigest (java.security.MessageDigest)1 Scanner (java.util.Scanner)1 FileFilter (javax.swing.filechooser.FileFilter)1