Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

CommandSourceApplet.java

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

Generated on Tue Aug 24 11:12:24 2004 for Data turbine for NEESGrid by doxygen 1.3.7