DevFaqInput

Revision as of 14:03, 2 August 2011 by Javydreamercsw (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

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.OutputHandler;
import java.io.IOException;
import org.jivesoftware.smack.util.ObservableReader;
import org.jivesoftware.smack.util.ReaderListener;
import org.openide.ErrorManager;
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.");
                OutputHandler.setInputEnabled(normal, true);
                ObservableReader reader = new ObservableReader(OutputHandler.getReader(normal));
                reader.addReaderListener(Installer.this);
                new Thread(new ProcessHandler(reader)).start();
            }
        });
    }

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

    private static class ProcessHandler implements Runnable {

        private final ObservableReader reader;

        public ProcessHandler(ObservableReader reader) {
            this.reader = reader;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    while (!reader.ready()) {
                        try {
                            Thread.sleep(200);
                        } 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 {
            }
        }
    }
}
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