Search in sources :

Example 6 with CN.getCurrentForm

use of com.codename1.ui.CN.getCurrentForm in project CodenameOne by codenameone.

the class CodenameOneImplementation method captureAudio.

/**
 * Captures a audio and notifies with the raw data when available
 * @param response callback for the resulting data
 */
public void captureAudio(final MediaRecorderBuilder recordingOptions, final com.codename1.ui.events.ActionListener response) {
    final MediaRecorderBuilder builder = recordingOptions == null ? new MediaRecorderBuilder() : recordingOptions;
    if (!builder.isRedirectToAudioBuffer() && builder.getPath() == null) {
        builder.path(new com.codename1.io.File("tmpaudio.wav").getAbsolutePath());
    }
    if (!builder.isRedirectToAudioBuffer() && builder.getMimeType() == null) {
        builder.mimeType("audio/wav");
    }
    System.out.println("in captureAudio " + recordingOptions.isRedirectToAudioBuffer());
    final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
    final Sheet sheet = new Sheet(null, "Record Audio");
    sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
    sheet.getContentPane().add(com.codename1.ui.layouts.BorderLayout.CENTER, cmp);
    cmp.addActionListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            switch(cmp.getState()) {
                case Accepted:
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    response.actionPerformed(new com.codename1.ui.events.ActionEvent(builder.getPath()));
                                }
                            });
                        }
                    });
                    break;
                case Canceled:
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    if (fs.exists(builder.getPath())) {
                        FileSystemStorage.getInstance().delete(builder.getPath());
                    }
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    response.actionPerformed(new com.codename1.ui.events.ActionEvent(null));
                                }
                            });
                        }
                    });
                    break;
            }
        }
    });
    sheet.addCloseListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            if (cmp.getState() != AudioRecorderComponent.RecorderState.Accepted && cmp.getState() != AudioRecorderComponent.RecorderState.Canceled) {
                FileSystemStorage fs = FileSystemStorage.getInstance();
                if (fs.exists(builder.getPath())) {
                    FileSystemStorage.getInstance().delete(builder.getPath());
                }
                CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                    public void run() {
                        response.actionPerformed(new com.codename1.ui.events.ActionEvent(null));
                    }
                });
            }
        }
    });
    sheet.show();
// capture(response, new String[] {"wav", "mp3", "aac"}, "*.wav;*.mp3;*.aac");
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) ActionEvent(com.codename1.ui.events.ActionEvent) FileSystemStorage(com.codename1.io.FileSystemStorage) ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) com.codename1.ui(com.codename1.ui) AudioRecorderComponent(com.codename1.components.AudioRecorderComponent)

Example 7 with CN.getCurrentForm

use of com.codename1.ui.CN.getCurrentForm in project CodenameOne by codenameone.

the class LoadingTextAnimationSample method showForm.

private void showForm() {
    Form f = new Form("Hello", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_SCALE));
    Form prev = CN.getCurrentForm();
    Toolbar tb = new Toolbar();
    f.setToolbar(tb);
    tb.addCommandToLeftBar("Back", null, evt -> {
        prev.showBack();
    });
    SpanLabel profileText = new SpanLabel();
    profileText.setText("placeholder");
    f.add(BorderLayout.CENTER, profileText);
    // Replace the label by a CircleProgress to indicate that it is loading.
    LoadingTextAnimation.markComponentLoading(profileText);
    Button next = new Button("Next");
    next.addActionListener(e -> {
        showLabelTest();
    });
    f.add(BorderLayout.SOUTH, next);
    AsyncResource<MyData> request = fetchDataAsync();
    request.ready(data -> {
        profileText.setText(data.getProfileText());
        // Replace the progress with the nameLabel now that
        // it is ready, using a fade transition
        LoadingTextAnimation.markComponentReady(profileText, CommonTransitions.createFade(300));
    });
    f.show();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) SpanLabel(com.codename1.components.SpanLabel) Toolbar(com.codename1.ui.Toolbar)

Example 8 with CN.getCurrentForm

use of com.codename1.ui.CN.getCurrentForm in project CodenameOne by codenameone.

the class CSSWatcher method watch.

private void watch() throws IOException {
    if (pulseSocket == null || pulseSocket.isClosed()) {
        // If the the Simulator is killed then the shutdown hook doesn't run
        // so we need an alternative way for the ResourceEditorApp to know that
        // the parent is dead so that it will close itself.
        // So we create a ServerSocket to serve as a "pulse".
        // We pass the port to the ResourceEditorApp so that it can connect.
        // When the socket is disconnected for any reason, the ResourceEditor app will exit.
        pulseSocket = new ServerSocket(0);
        pulseThread = new Thread(new Runnable() {

            public void run() {
                while (!closing) {
                    try {
                        Socket clientSocket = pulseSocket.accept();
                    } catch (IOException ex) {
                        Logger.getLogger(CSSWatcher.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        pulseThread.setDaemon(true);
    }
    File javaBin = new File(System.getProperty("java.home"), "bin/java");
    final File srcFile = new File("css", "theme.css");
    String overrideInputs = System.getProperty("codename1.css.compiler.args.input", null);
    if (!srcFile.exists() && overrideInputs == null) {
        // System.out.println("No theme.css file found.  CSSWatcher canceled");
        return;
    } else {
        if (overrideInputs == null) {
            System.out.println("Found theme.css file.  Watching for changes...");
        } else {
            if (overrideInputs.trim().isEmpty()) {
                System.out.println("CSS file " + overrideInputs + " does not exist.  Not activating CSS watcher");
                return;
            }
            System.out.println("Watching CSS files for changes: [" + overrideInputs + "]");
        }
    }
    File resDir = JavaSEPort.getSourceResourcesDir();
    File destFile = new File(resDir, "theme.res");
    String overrideOutputs = System.getProperty("codename1.css.compiler.args.output", null);
    if (overrideOutputs != null) {
        destFile = new File(overrideOutputs);
    }
    File userHome = new File(System.getProperty("user.home"));
    File cn1Home = new File(userHome, ".codenameone");
    File designerJar = new File(cn1Home, "designer_1.jar");
    if (System.getProperty("codename1.designer.jar", null) != null) {
        designerJar = new File(System.getProperty("codename1.designer.jar", null));
    }
    String cefDir = System.getProperty("cef.dir", cn1Home + File.separator + "cef");
    // List<String> args = new ArrayList<String>();
    ProcessBuilder pb = new ProcessBuilder(javaBin.getAbsolutePath(), "-jar", "-Dcli=true", "-Dcef.dir=" + cefDir, "-Dparent.port=" + pulseSocket.getLocalPort(), designerJar.getAbsolutePath(), "-css");
    List<String> args = pb.command();
    if (overrideInputs != null) {
        args.add("-input");
        args.add(overrideInputs);
        args.add("-output");
        args.add(overrideOutputs);
        args.add("-merge");
        args.add(System.getProperty("codename1.css.compiler.args.merge", null));
        args.add("-watch");
    } else {
        args.add(srcFile.getAbsolutePath());
        args.add(destFile.getAbsolutePath());
        args.add("-watch");
        args.add("-Dprism.order=sw");
    }
    Process p = pb.start();
    if (childProcess != null) {
        try {
            childProcess.destroyForcibly();
        } catch (Throwable t) {
        }
    }
    childProcess = p;
    String line;
    OutputStream stdin = p.getOutputStream();
    final InputStream stderr = p.getErrorStream();
    final BufferedReader errorReader = new BufferedReader(new InputStreamReader(stderr));
    new Thread(new Runnable() {

        public void run() {
            while (true) {
                int reloadVersion = Integer.parseInt(System.getProperty("reload.simulator.count", "0"));
                if (reloadVersion != simulatorReloadVersion) {
                    stop();
                    break;
                }
                try {
                    String l = errorReader.readLine();
                    if (l != null) {
                        System.err.println("CSS> " + l);
                    } else {
                        break;
                    }
                } catch (IOException ex) {
                    Logger.getLogger(CSSWatcher.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }).start();
    InputStream stdout = p.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
    final File fDestFile = destFile;
    final String fOverrideInputs = overrideInputs;
    while (true) {
        try {
            int reloadVersion = Integer.parseInt(System.getProperty("reload.simulator.count", "0"));
            if (reloadVersion != simulatorReloadVersion) {
                stop();
                return;
            }
            String l = reader.readLine();
            if (l == null) {
                if (!p.isAlive()) {
                    watchThread = null;
                    start();
                    break;
                }
            }
            System.out.println("CSS> " + l);
            if ("::refresh::".equals(l)) {
                Display.getInstance().callSerially(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            if (fOverrideInputs != null) {
                                System.out.println("CSS File " + fOverrideInputs + " has been updated.  Reloading styles from " + fDestFile);
                            } else {
                                System.out.println("CSS File " + srcFile + " has been updated.  Reloading styles from " + fDestFile);
                            }
                            Resources res = Resources.open(new FileInputStream(fDestFile));
                            UIManager.getInstance().addThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
                            Form f = CN.getCurrentForm();
                            if (f != null) {
                                f.refreshTheme();
                                f.revalidate();
                            }
                        } catch (Exception err) {
                            err.printStackTrace();
                        }
                    }
                });
            }
        } catch (Throwable t) {
            if (closing) {
                return;
            }
            t.printStackTrace();
            if (!p.isAlive()) {
                watchThread = null;
                start();
                break;
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Form(com.codename1.ui.Form) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Resources(com.codename1.ui.util.Resources) File(java.io.File) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 9 with CN.getCurrentForm

use of com.codename1.ui.CN.getCurrentForm in project CodenameOne by codenameone.

the class CircleProgressSample method showForm.

private void showForm() {
    Form f = new Form("Hello", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
    Form prev = CN.getCurrentForm();
    Toolbar tb = new Toolbar();
    f.setToolbar(tb);
    tb.addCommandToLeftBar("Back", null, evt -> {
        prev.showBack();
    });
    Label nameLabel = new Label();
    $(nameLabel).setAlignment(CENTER);
    nameLabel.setText("placeholder");
    f.add(BorderLayout.CENTER, nameLabel);
    // Replace the label by a CircleProgress to indicate that it is loading.
    CircleProgress.markComponentLoading(nameLabel).getStyle().setFgColor(0xff0000);
    AsyncResource<MyData> request = fetchDataAsync();
    request.ready(data -> {
        nameLabel.setText(data.getName());
        // Replace the progress with the nameLabel now that
        // it is ready, using a fade transition
        CircleProgress.markComponentReady(nameLabel, CommonTransitions.createFade(300));
    });
    f.show();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) Label(com.codename1.ui.Label) Toolbar(com.codename1.ui.Toolbar)

Example 10 with CN.getCurrentForm

use of com.codename1.ui.CN.getCurrentForm in project CodenameOne by codenameone.

the class AudioRecorderComponentSample method recordAudio.

private AsyncResource<String> recordAudio() {
    AsyncResource<String> out = new AsyncResource<>();
    String mime = MediaManager.getAvailableRecordingMimeTypes()[0];
    String ext = mime.indexOf("mp3") != -1 ? "mp3" : mime.indexOf("wav") != -1 ? "wav" : mime.indexOf("aiff") != -1 ? "aiff" : "aac";
    MediaRecorderBuilder builder = new MediaRecorderBuilder().path(new File("myaudio." + ext).getAbsolutePath()).mimeType(mime);
    final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
    final Sheet sheet = new Sheet(null, "Record Audio");
    sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
    sheet.getContentPane().add(com.codename1.ui.layouts.BorderLayout.CENTER, cmp);
    cmp.addActionListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            switch(cmp.getState()) {
                case Accepted:
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    out.complete(builder.getPath());
                                }
                            });
                        }
                    });
                    break;
                case Canceled:
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    if (fs.exists(builder.getPath())) {
                        FileSystemStorage.getInstance().delete(builder.getPath());
                    }
                    CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                        public void run() {
                            sheet.back();
                            sheet.addCloseListener(new ActionListener() {

                                @Override
                                public void actionPerformed(ActionEvent evt) {
                                    sheet.removeCloseListener(this);
                                    out.complete(null);
                                }
                            });
                        }
                    });
                    break;
            }
        }
    });
    sheet.addCloseListener(new com.codename1.ui.events.ActionListener() {

        @Override
        public void actionPerformed(com.codename1.ui.events.ActionEvent e) {
            if (cmp.getState() != AudioRecorderComponent.RecorderState.Accepted && cmp.getState() != AudioRecorderComponent.RecorderState.Canceled) {
                FileSystemStorage fs = FileSystemStorage.getInstance();
                if (fs.exists(builder.getPath())) {
                    FileSystemStorage.getInstance().delete(builder.getPath());
                }
                CN.getCurrentForm().getAnimationManager().flushAnimation(new Runnable() {

                    public void run() {
                        out.complete(null);
                    }
                });
            }
        }
    });
    sheet.show();
    return out;
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) ActionEvent(com.codename1.ui.events.ActionEvent) FileSystemStorage(com.codename1.io.FileSystemStorage) ActionEvent(com.codename1.ui.events.ActionEvent) ActionListener(com.codename1.ui.events.ActionListener) ActionListener(com.codename1.ui.events.ActionListener) AudioRecorderComponent(com.codename1.components.AudioRecorderComponent) AsyncResource(com.codename1.util.AsyncResource) File(com.codename1.io.File) Sheet(com.codename1.ui.Sheet)

Aggregations

Form (com.codename1.ui.Form)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 Toolbar (com.codename1.ui.Toolbar)3 ActionEvent (com.codename1.ui.events.ActionEvent)3 AudioRecorderComponent (com.codename1.components.AudioRecorderComponent)2 FileSystemStorage (com.codename1.io.FileSystemStorage)2 MediaRecorderBuilder (com.codename1.media.MediaRecorderBuilder)2 Button (com.codename1.ui.Button)2 Label (com.codename1.ui.Label)2 ActionListener (com.codename1.ui.events.ActionListener)2 Rectangle (com.codename1.ui.geom.Rectangle)2 Style (com.codename1.ui.plaf.Style)2 SpanLabel (com.codename1.components.SpanLabel)1 File (com.codename1.io.File)1 com.codename1.ui (com.codename1.ui)1 CN.getCurrentForm (com.codename1.ui.CN.getCurrentForm)1 Command (com.codename1.ui.Command)1 ComponentClosure (com.codename1.ui.ComponentSelector.ComponentClosure)1 Container (com.codename1.ui.Container)1 Sheet (com.codename1.ui.Sheet)1