View Javadoc

1   package org.lsst.ccs.plugin.jas3.tutorial;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStreamReader;
6   import java.io.PrintWriter;
7   import java.util.Timer;
8   import java.util.TimerTask;
9   import java.util.logging.Level;
10  import java.util.logging.Logger;
11  import org.freehep.application.mdi.PageEvent;
12  import org.freehep.application.mdi.PageListener;
13  import org.freehep.application.studio.Studio;
14  import org.freehep.jas.plugin.console.Console;
15  import org.freehep.jas.plugin.console.ConsoleInputStream;
16  import org.freehep.jas.plugin.console.ConsoleOutputStream;
17  import org.freehep.jas.plugin.console.ConsoleService;
18  
19  /**
20   * A class which illustrates how to create input, output and input/output
21   * consoles.
22   *
23   * @author tonyj
24   */
25  class TutorialConsole {
26  
27      private static final Logger logger = Logger.getLogger(TutorialPlugin.class.getName());
28      private final Studio studio;
29  
30      TutorialConsole(Studio studio) {
31          this.studio = studio;
32      }
33  
34      void createOutputConsole() {
35          // The basic JAS console functionality is provided by the console
36          // plugin. We need to look that up before we can open a console.
37          ConsoleService consoleService = (ConsoleService) studio.getLookup().lookup(ConsoleService.class);
38          // Although the console plugin is included with JAS we shold always protect
39          // against the possibility of it not being installed
40          if (consoleService != null) {
41              Console console = consoleService.createConsole("Output Console", null);
42              sendOutputToConsole(console, consoleService);
43          }
44      }
45  
46      void createInputConsole() {
47          // The basic JAS console functionality is provided by the console
48          // plugin. We need to look that up before we can open a console.
49          ConsoleService consoleService = (ConsoleService) studio.getLookup().lookup(ConsoleService.class);
50          // Although the console plugin is included with JAS we shold always protect
51          // against the possibility of it not being installed
52          if (consoleService != null) {
53              // Create a new console, and get an input string for reading the users input.
54              final Console console = consoleService.createConsole("Input Console", null);
55              readInputFromConsole(console);
56          }
57      }
58  
59      void createInputOutputConsole() {
60          // The basic JAS console functionality is provided by the console
61          // plugin. We need to look that up before we can open a console.
62          ConsoleService consoleService = (ConsoleService) studio.getLookup().lookup(ConsoleService.class);
63          // Although the console plugin is included with JAS we shold always protect
64          // against the possibility of it not being installed
65          if (consoleService != null) {
66              final Console console = consoleService.createConsole("Input/Output Console", null);
67              sendOutputToConsole(console, consoleService);
68              readInputFromConsole(console);
69          }
70      }
71  
72      private void sendOutputToConsole(Console console, ConsoleService consoleService) {
73          // Create a PrintWriter that can be used to send output to the console
74          // Note that the output stream created by the console is thread safe, so it is OK
75          // to print to it from any thread (such as the Timer thread created below).
76          final ConsoleOutputStream consoleOutputStream = console.getOutputStream(null);
77          final PrintWriter pw = new PrintWriter(consoleOutputStream, true);
78          // Just as a demo, we will generate some tick messages to go to the newly created console
79          Timer timer = new Timer();
80          final TimerTask timerTask = new TimerTask() {
81              private int tick = 0;
82  
83              @Override
84              public void run() {
85                  tick++;
86                  pw.printf("tick %d\n", tick);
87                  // Log it too, so we can verify that the ticks stop when the console
88                  // is closed.
89                  logger.info(String.format("tick %d\n", tick));
90              }
91          };
92          timer.scheduleAtFixedRate(timerTask, 1000, 1000);
93          // If we don't arrange for the timer to be cancelled when the console is closed, it will 
94          // run forever. So we attach a listener to detect when the console is closed, and when it is
95          // fired cancel the timer task.
96          consoleService.getPageContextForConsole(console).addPageListener(new PageListener() {
97              @Override
98              public void pageChanged(PageEvent pe) {
99                  if (pe.getID() == PageEvent.PAGECLOSED) {
100                     timerTask.cancel();
101                     logger.info("Console closed, timertask canceled");
102                 }
103             }
104         });
105     }
106 
107     private void readInputFromConsole(final Console console) {
108         // Get an input stream for reading the users input.
109         String initialPrompt = "CCS> ";
110         final ConsoleInputStream inputStream = console.getInputStream(initialPrompt);
111         // We need to create a background thread that will wait for input
112         Thread thread = new Thread() {
113             @Override
114             public void run() {
115                 try {
116                     BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
117                     for (;;) {
118                         String line = in.readLine();
119                         if (line == null) {
120                             // Indicates the input stream has been closed as the console has been closed
121                             break;
122                         }
123                         if ("exit".equals(line)) {
124                             // Close the console progamatically. A side effect will be that the
125                             // ConsoleInputStream will be closed, and the thread will exit due to 
126                             // the check above.
127                             console.close();
128                         } else if (line.startsWith("setPrompt")) {
129                             String newPrompt = line.substring(10);
130                             inputStream.setOneTimePrompt(newPrompt);
131                         } else if (line.startsWith("changePrompt")) {
132                             String newPrompt = line.substring(13);
133                             inputStream.setPrompt(newPrompt);
134                         }
135                         logger.log(Level.INFO, "Read line: {0}", line);
136                     }
137                     logger.info("Exiting since console has been closed");
138                 } catch (IOException ex) {
139                     logger.log(Level.SEVERE, "Error reading from console", ex);
140                 }
141             }
142         };
143         thread.start();
144     }
145 }