00001
00002
00003
00004 package org.nees.rbnb;
00005
00006 import java.applet.Applet;
00007 import java.awt.*;
00008
00009 import java.awt.event.ActionEvent;
00010 import java.awt.event.ActionListener;
00011
00012 import com.rbnb.sapi.*;
00013
00017 public class CommandSourceApplet extends Applet {
00018
00019 private static final String SERVER = "localhost:3333";
00020 private static final String SOURCE_NAME = "Command";
00021 private static final String CHANNEL_NAME = "CommandData";
00022
00023 private String server = SERVER;
00024 private String sourceName = SOURCE_NAME;
00025 private String channelName = CHANNEL_NAME;
00026
00027 private Command[] commandArray = new Command[0];
00028 private Source source = null;
00029 private ChannelMap sMap;
00030 private int index;
00031 private boolean connected = false;
00032
00033 private void setArgs()
00034 {
00035 String server = SERVER;
00036 String sourceName = SOURCE_NAME;
00037 String channelName = CHANNEL_NAME;
00038 String param;
00039
00040 param = getParameter("server");
00041 if (param != null)
00042 server = param;
00043
00044 param = getParameter("source");
00045 if (param != null)
00046 sourceName = param;
00047
00048 param = getParameter("channel");
00049 if (param != null)
00050 channelName = param;
00051
00052 setArgs(server,sourceName,channelName);
00053
00054 commandArray = new Command[2];
00055 commandArray[0] = new Command("Stop","stop",null,null);
00056 commandArray[0] = new Command("Shake","skake",null,null);
00057
00058 param = getParameter("numberOfCommands");
00059 if (param != null)
00060 {
00061 try
00062 {
00063 int n = Integer.parseInt(param);
00064 commandArray = new Command[n];
00065 } catch (Exception ignore){}
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 }
00084
00085
00086 private void setArgs(String server_host, String source_name, String channel_name)
00087 {
00088 server = server_host;
00089 serverText.setText(server);
00090
00091 sourceName = source_name;
00092 sourceText.setText(sourceName);
00093
00094 channelName = channel_name;
00095 channelText.setText(channelName);
00096 }
00097
00098 private void setArgsFromTextFields()
00099 {
00100 setArgs (
00101 serverText.getText(),
00102 sourceText.getText(),
00103 channelText.getText()
00104 );
00105 }
00106
00107 private void openConnection()
00108 {
00109 if (connected) return;
00110
00111 try {
00112 disableStart();
00113 setArgsFromTextFields();
00114 messagePanel.message("Attempting connection to sever...");
00115 source=new Source();
00116 source.OpenRBNBConnection(server,sourceName);
00117 sMap = new ChannelMap();
00118 int index = sMap.Add(channelName);
00119 connected = true;
00120 messagePanel.message("Connection made to sever...");
00121 enableStart();
00122 start();
00123 connectButton.setLabel(DISCONNECT);
00124 connectButton.setActionCommand(connectButton.getLabel());
00125 } catch (SAPIException se)
00126 {
00127 messagePanel.message("SAPIException = " + se);
00128 }
00129 }
00130
00131 private void enableStart() {
00132 startStopButton.setEnabled(true);
00133 }
00134
00135 private void disableStart() {
00136 startStopButton.setEnabled(false);
00137 }
00138
00139 private void closeConnection()
00140 {
00141 if (!connected) return;
00142 stop();
00143 disableStart();
00144 connected = false;
00145 source.CloseRBNBConnection();
00146 connectButton.setLabel(CONNECT);
00147 connectButton.setActionCommand(connectButton.getLabel());
00148 }
00149 public void init() {
00150 setArgs();
00151 setLayout();
00152 repaint();
00153 messagePanel.message("initializing... ");
00154 messagePanel.message("Number of commands = " + commandArray.length);
00155 for (int i = 0; i < commandArray.length; i++)
00156 messagePanel.message("Command(" + i + ") = " + commandArray[i]);
00157 }
00158
00159 public void start() {
00160 messagePanel.message("starting... ");
00161 messagePanel.message("Command array length: " + commandArray.length);
00162 for (int i = 0; i < commandArray.length; i++)
00163 messagePanel.message("Command: " + commandArray[i]);
00164 startStopButton.setLabel(STOP);
00165 startStopButton.setActionCommand(startStopButton.getLabel());
00166 }
00167
00168 public void stop() {
00169 messagePanel.message("stopping... ");
00170 startStopButton.setLabel(START);
00171 startStopButton.setActionCommand(startStopButton.getLabel());
00172 }
00173
00174 public void destroy() {
00175 messagePanel.message("preparing for unloading...");
00176 closeConnection();
00177 }
00178
00179 private static final String CONNECT = "Connect";
00180 private static final String DISCONNECT = "Disconnect";
00181 private Button connectButton = new Button(CONNECT);
00182
00183 private static final String START = "Start";
00184 private static final String STOP = "Stop";
00185 private Button startStopButton = new Button(START);
00186
00187 private TextField serverText = new TextField(SERVER,40);
00188 private TextField sourceText = new TextField(SOURCE_NAME,40);
00189 private TextField channelText = new TextField(CHANNEL_NAME,40);
00190
00191 MessagePanel messagePanel = new MessagePanel();
00192
00193 private void setLayout()
00194 {
00195 setLayout(new BorderLayout());
00196
00197 Panel p = new Panel();
00198 p.setLayout(new GridLayout(3 + commandArray.length,2));
00199 p.add(serverText);
00200 p.add(new Label("Server Host : port"));
00201 p.add(sourceText);
00202 p.add(new Label("Source Name"));
00203 p.add(channelText);
00204 p.add(new Label("Channel Name"));
00205 for (int i = 0; i < commandArray.length; i++)
00206 {
00207 if (commandArray[i] == null)
00208 {
00209 p.add(new Button(commandArray[i].cover));
00210 Panel cp = new Panel();
00211 cp.add(new TextField(commandArray[i].value));
00212 cp.add(new Label(commandArray[i].label));
00213 p.add(cp);
00214 }
00215 }
00216 add("North",p);
00217
00218 p = new Panel();
00219 p.add(connectButton);
00220 p.add(startStopButton);
00221 add("South", p);
00222
00223 p = new Panel();
00224 p.add(messagePanel);
00225 add("Center", p);
00226
00227 startStopButton.setEnabled(false);
00228 startStopButton.setActionCommand(startStopButton.getLabel());
00229 startStopButton.addActionListener(
00230 new ActionListener() {
00231 public void actionPerformed(ActionEvent ev) {
00232 startStopAction(ev.getActionCommand());
00233 }
00234 }
00235 );
00236
00237 connectButton.setActionCommand(connectButton.getLabel());
00238 connectButton.addActionListener(
00239 new ActionListener() {
00240 public void actionPerformed(ActionEvent ev) {
00241 connectAction(ev.getActionCommand());
00242 }
00243 }
00244 );
00245
00246 }
00247
00248 private void startStopAction(String command)
00249 {
00250 if (command.equals(STOP))
00251 stop();
00252 else
00253 start();
00254 }
00255
00256 private void connectAction(String command)
00257 {
00258 if (command.equals(CONNECT))
00259 openConnection();
00260 else
00261 closeConnection();
00262 }
00263
00264 private class Command
00265 {
00266 String cover = "";
00267 String text = "";
00268 String label = "Value";
00269 String value = "";
00270
00271 Command(String c, String t, String l, String v)
00272 {
00273 if (c != null) cover = c;
00274 if (t != null) text = t;
00275 if (l != null) label = l;
00276 if (v != null) value = v;
00277 }
00278
00279 public String toString()
00280 {
00281 return "Command: " + cover + ", "
00282 + text + ", "
00283 + label + ", "
00284 + value;
00285 }
00286 }
00287
00288 }