DevFaqInput

(Difference between revisions)
(Created page with 'This is an example on how to get inout from the Output Window. Just change what's done on the read method (listener implementation) to customize the behavior. Things that come to…')
m
 
(3 intermediate revisions not shown)
Line 7: Line 7:
package sample.component;
package sample.component;
 +
import com.dreamer.outputhandler.InputMonitor;
import com.dreamer.outputhandler.OutputHandler;
import com.dreamer.outputhandler.OutputHandler;
-
import java.io.IOException;
 
-
import org.jivesoftware.smack.util.ObservableReader;
 
import org.jivesoftware.smack.util.ReaderListener;
import org.jivesoftware.smack.util.ReaderListener;
-
import org.openide.ErrorManager;
 
import org.openide.modules.ModuleInstall;
import org.openide.modules.ModuleInstall;
import org.openide.windows.WindowManager;
import org.openide.windows.WindowManager;
Line 30: Line 28:
             public void run() {
             public void run() {
                 OutputHandler.output(normal, "Welcome! Type something below.");
                 OutputHandler.output(normal, "Welcome! Type something below.");
-
                 OutputHandler.setInputEnabled(normal, true);
+
                 //Create a monitor for the tab. This enables input in the tab as well.
-
                 ObservableReader reader = new ObservableReader(OutputHandler.getReader(normal));
+
                 InputMonitor monitor = OutputHandler.createMonitor(normal);
-
                 reader.addReaderListener(Installer.this);
+
                 //Add a listener to be notified.
-
                new Thread(new ProcessHandler(reader)).start();
+
                monitor.addListener(Installer.this);
             }
             }
         });
         });
Line 42: Line 40:
         OutputHandler.output(normal, "You typed: " + read);
         OutputHandler.output(normal, "You typed: " + read);
     }
     }
 +
}
 +
</pre>
-
    private static class ProcessHandler implements Runnable {
+
<b>Note: </b>This is feature is available on the [http://plugins.netbeans.org/plugin/39695/?show=true Output Handler plugin] version 1.2.
-
        private final ObservableReader reader;
+
== Example of using input reader in output window ==
-
        public ProcessHandler(ObservableReader reader) {
+
<pre>
-
            this.reader = reader;
+
package sample.component;
-
        }
+
-
         @Override
+
import java.awt.EventQueue;
-
        public void run() {
+
import java.io.BufferedReader;
-
             while (true) {
+
import java.io.IOException;
 +
import org.openide.util.Exceptions;
 +
import org.openide.util.RequestProcessor;
 +
import org.openide.windows.IOProvider;
 +
import org.openide.windows.InputOutput;
 +
import org.openide.windows.OnShowing;
 +
 
 +
@OnShowing
 +
public class OutputWindowReaderExample implements Runnable {
 +
 
 +
    /**
 +
    * Open Output Window and ask for some input.
 +
    */
 +
    public static void requestAnswerExample() throws IOException {
 +
         assert !EventQueue.isDispatchThread();
 +
        InputOutput io = IOProvider.getDefault().getIO("ioName", true);
 +
        io.select();
 +
        io.getOut().println("How are you?");
 +
        BufferedReader br = new BufferedReader(io.getIn());
 +
        String answer = br.readLine();
 +
        io.getOut().println("You are " + answer + " today");
 +
        br.close();
 +
        io.getOut().close();
 +
        io.getErr().close();
 +
    }
 +
 
 +
    /**
 +
    * Call requestAnswerExample from a background thread, after initialization
 +
    * of the user interface.
 +
    */
 +
    @Override
 +
    public void run() {
 +
        RequestProcessor.getDefault().post(new Runnable() {
 +
             @Override
 +
            public void run() {
                 try {
                 try {
-
                     while (!reader.ready()) {
+
                     requestAnswerExample();
-
                        try {
+
                 } catch (IOException ex) {
-
                            Thread.sleep(200);
+
                     Exceptions.printStackTrace(ex);
-
                        } catch (InterruptedException e) {
+
-
                            close();
+
-
                            return;
+
-
                        }
+
-
                    }
+
-
                    char[] buffer = new char[1024];
+
-
                    if (reader.read(buffer, 0, buffer.length) == -1
+
-
                            || Thread.currentThread().isInterrupted()) {
+
-
                        close();
+
-
                        return;
+
-
                    }
+
-
                 } catch (IOException ioe) {
+
-
                     ErrorManager.getDefault().notify(ioe);
+
-
                    return;
+
                 }
                 }
             }
             }
-
         }
+
         });
-
 
+
-
        private void close() {
+
-
            try {
+
-
                reader.close();
+
-
            } catch (IOException ioe) {
+
-
                ErrorManager.getDefault().notify(ioe);
+
-
            } finally {
+
-
            }
+
-
        }
+
     }
     }
}
}
</pre>
</pre>
 +
 +
<b>Note:</b> The module that contains this class depends on Window System API (org.openide.windows).

Current revision as of 11:51, 17 October 2012

This is an example on how to get inout from the Output Window. Just change what's done on the read method (listener implementation) to customize the behavior. Things that come to mind: chat client, text based games, etc...

/*
 * Enable/create the tabs we need for the component
 */
package sample.component;

import com.dreamer.outputhandler.InputMonitor;
import com.dreamer.outputhandler.OutputHandler;
import org.jivesoftware.smack.util.ReaderListener;
import org.openide.modules.ModuleInstall;
import org.openide.windows.WindowManager;

/**
 * Manages a module's lifecycle. Remember that an installer is optional and
 * often not needed at all.
 */
public class Installer extends ModuleInstall implements ReaderListener {

    private final String normal = "Output";

    @Override
    public void restored() {
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {

            @Override
            public void run() {
                OutputHandler.output(normal, "Welcome! Type something below.");
                //Create a monitor for the tab. This enables input in the tab as well.
                InputMonitor monitor = OutputHandler.createMonitor(normal);
                //Add a listener to be notified.
                monitor.addListener(Installer.this);
            }
        });
    }

    @Override
    public void read(String read) {
        OutputHandler.output(normal, "You typed: " + read);
    }
}

Note: This is feature is available on the Output Handler plugin version 1.2.

Example of using input reader in output window

package sample.component;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.windows.OnShowing;

@OnShowing
public class OutputWindowReaderExample implements Runnable {

    /**
     * Open Output Window and ask for some input.
     */
    public static void requestAnswerExample() throws IOException {
        assert !EventQueue.isDispatchThread();
        InputOutput io = IOProvider.getDefault().getIO("ioName", true);
        io.select();
        io.getOut().println("How are you?");
        BufferedReader br = new BufferedReader(io.getIn());
        String answer = br.readLine();
        io.getOut().println("You are " + answer + " today");
        br.close();
        io.getOut().close();
        io.getErr().close();
    }

    /**
     * Call requestAnswerExample from a background thread, after initialization
     * of the user interface.
     */
    @Override
    public void run() {
        RequestProcessor.getDefault().post(new Runnable() {
            @Override
            public void run() {
                try {
                    requestAnswerExample();
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        });
    }
}

Note: The module that contains this class depends on Window System API (org.openide.windows).

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo