/**
* Name:
Edison Chindrawaly
* File:
MyGenerateScript.java
* Desc:
GenerateScript.java calls MyGenerateScript.java
* to do most of the
work for creating a script.
* MyGenerateScript extends JInternalFrame
which
* is the internal window in the desktop environment
*
that is provided by GenerateScript.java
* Date: August 9, 2001
* Vers: 1.2
*/
import
javax.swing.*;
import javax.swing.border.*;
import
javax.swing.event.*;
import java.awt.event.*;
import
java.awt.*;
import java.util.*;
import java.io.*;
public
class MyGenerateScript extends JInternalFrame
{
static int openFrameCount = 0;
static final int xOffset = 10,
yOffset = 10;
private
JButton geneButton, exButton;
private ButtonGroup motionGroup = new ButtonGroup();
private ButtonGroup interestGroup = new
ButtonGroup();
private ButtonGroup
teamGroup = new ButtonGroup();
private ButtonGroup simulGroup = new ButtonGroup();
private JTextField dimText = new
JTextField();
private JTextField
objText = new JTextField();
private JTextField playText= new JTextField();
private String simulation =
"Simulation";
private
String tankRadio = "Tank_vs_Tank";
private String bomberRadio =
"Bomber_Plane_vs_Fortress";
private String dimensionText = "Dimension";
private String numberOfObject=
"Number_Of_Object";
private String numberOfPlayer= "Number_Of_Player";
private String generateButton =
"Generate";
private
String exitButton = "Cancel";
private String regionCombo = "Region";
private String fixedCombo = "Fixed";
private String dynamicCombo=
"Dynamic";
private
String senderCombo = "Sender";
private String stillRadio
= "Still";
private String moveRadio
= "Move";
private
String pubRadio = "Pub";
private String subRadio = "Sub";
private String pubsubRadio =
"PubSub";
private String
blueRadio = "Blue";
private String redRadio = "Red";
private String scheme =
"Scheme";
private String
motion = "Motion";
private String interest = "Interest";
private String team = "Team";
private StringBuffer commExec = new
StringBuffer();
private String[]
schemeCombo = { regionCombo,
fixedCombo,
dynamicCombo,
senderCombo };
private String[] motionRadio = { moveRadio,
stillRadio };
private String[]
interestRadio = { pubRadio, subRadio, pubsubRadio };
private String[] teamRadio = { blueRadio,
redRadio };
private String[]
simulCombo = { tankRadio, bomberRadio };
private JComboBox schemeBox= new JComboBox(schemeCombo);
private JComboBox simulBox = new
JComboBox(simulCombo);
private
Stack motionStack = new Stack();
private Stack interestStack = new Stack();
private Stack teamStack = new Stack();
private Stack schemeStack = new
Stack();
private Stack
dimensionStack = new Stack();
private Stack playerStack = new Stack();
private Stack objectStack = new
Stack();
private Stack simulStack
= new Stack();
private Vector
errorMessage = new Vector();
/**
* default constructor
*/
public
MyGenerateScript()
{
super("Script #" +
(++openFrameCount),
true,true,true,true);
setSize(400,320);
Font f12 = new Font("Arial",
Font.PLAIN, 12);
ButtonListener
listener = new ButtonListener();
setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
geneButton = new
JButton(generateButton);
geneButton.setFont(f12);
geneButton.addActionListener(listener);
exButton = new JButton(exitButton);
exButton.setFont(f12);
exButton.addActionListener(listener);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new
BorderLayout());
JPanel
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2,15,15));
buttonPanel.add(geneButton);
buttonPanel.add(exButton);
JPanel intermediate = new JPanel();
intermediate.setBorder(new
EmptyBorder(10,10,10,10));
intermediate.setLayout(new DialogLayout(20,5));
intermediate.add(new JLabel(dimensionText+":"));
intermediate.add(new
createTextField(dimText));
intermediate.add(new JLabel(numberOfObject+":"));
intermediate.add(new
createTextField(objText));
intermediate.add(new JLabel(numberOfPlayer+":"));
intermediate.add(new createTextField(playText));
intermediate.add(new
JLabel(scheme+":"));
intermediate.add(new createComboBox(schemeBox));
intermediate.add(new
JLabel(motion+":"));
intermediate.add(new createRadioButton(motionGroup,motionRadio));
intermediate.add(new
JLabel(interest+":"));
intermediate.add(new
createRadioButton(interestGroup,interestRadio));
intermediate.add(new
JLabel(team+":"));
intermediate.add(new createRadioButton(teamGroup,teamRadio));
intermediate.add(new
JLabel("Simulation:"));
intermediate.add(new createComboBox(simulBox));
intermediate.setBorder(BorderFactory.createTitledBorder("Input"));
mainPanel.add(intermediate,BorderLayout.NORTH);
mainPanel.add(buttonPanel,
BorderLayout.SOUTH);
getContentPane().add(mainPanel,BorderLayout.CENTER);
setVisible(true);
}
/**
* private class createRadioButton is to
create radio button for
* the
MyGenerateScript class. The default constructor takes
* ButtonGroup, and array of String as
parameter. Array of String
* would
make up the choices of for the button, while the ButtonGroup
* would contain all the buttons
created.
* @param ButtonGroup bg and String[] newRadio
* @return JPanel contains Radio
Buttons.
*/
private class createRadioButton extends
JPanel
{
public createRadioButton(ButtonGroup
bg,String[] newRadio)
{
int max = newRadio.length;
JRadioButton[] radioBut = new
JRadioButton[max];
setLayout(new
BoxLayout(this,BoxLayout.X_AXIS));
RadioButtonListener radioListen = new RadioButtonListener();
for(int i=0; i<max; i++)
{
radioBut[i] = new JRadioButton(newRadio[i]);
radioBut[i].setActionCommand(newRadio[i]);
radioBut[i].addActionListener(radioListen);
bg.add(radioBut[i]);
add(radioBut[i]);
}
}
}// end of private class
createRadioButton
/**
*
private class createComboBox is to create combo box
* for MyGenerateScript class.
*/
private class createComboBox extends JPanel
{
public createComboBox(JComboBox Choice)
{
setLayout(new
BoxLayout(this,BoxLayout.X_AXIS));
Choice.setEditable(true);
Choice.setAlignmentX(LEFT_ALIGNMENT);
Choice.setMaximumRowCount(5);
ComboListener cbl = new ComboListener();
Choice.addActionListener(cbl);
add(Choice);
}
}// end of private class createComboBox
/**
* private class createTextField is to create
text field
* for MyGenerateScript
class. The default constructor takes
* parameter of JTextField and add the DocumentListener's
class
* which has a function to
monitor the changes in the JTextField
* aside from the TextListener that listens to the event
changes.
* We need the redudancy
of the TextListener and DocumentListener
* to make the application user friendly.
* @param
JTextField
* @return JPanel
contains JTextField
*/
private class createTextField extends
JPanel
{
public createTextField(JTextField
text)
{
setLayout(new BoxLayout(this,
BoxLayout.X_AXIS));
TextListener
textListener = new TextListener();
text.setHorizontalAlignment(SwingConstants.LEFT);
text.addActionListener(textListener);
text.getDocument().addDocumentListener(new
DocumentListen());
add(text);
}
}
/**
* private class TextListener is to listen to
the text
* input, and push in the
result to the individual stacks
*
which already pre-determined. TextListener is listening
* for event changes --- whenever users hit
Enter key. TextListener
* pushes
in the result into the approriate stacks.
* @param ActionEvent
e
* @return none
*/
private class TextListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if((JTextField)e.getSource() == dimText)
{
System.out.println("Dimension: " + dimText.getText());
dimensionStack.push(dimText.getText());
}
else
if((JTextField)e.getSource() == playText)
{
System.out.println("Player
: " + playText.getText());
playerStack.push(playText.getText());
}
else if((JTextField)e.getSource() == objText)
{
System.out.println("Object
: " + objText.getText());
objectStack.push(objText.getText());
}
}
}
/**
* DocumentListen's class implements
DocumentListener interface.
*
DocumentListener listens to state changes of the Document instead
* of event changes only. Meaning, It listens
to the keystrokes that
* fill into
any text field or area that uses Document's interface.
* @param
DocumentEvent
* @return
none
*/
private class DocumentListen implements
DocumentListener
{
public void changedUpdate(DocumentEvent
de)
{ }
public void insertUpdate(DocumentEvent
de)
{
try
{
System.out.println("Document insertUpdate: " +
(de.getDocument()).getText(0,de.getDocument().getLength()));
}
catch(Exception e)
{
System.out.println("error: " + e);
}
}
public void
removeUpdate(DocumentEvent de)
{
}
}
/**
* ComboListener's class implements
ActionListener's interface.
* It
listens to the action performed on JComboBox, and push
* the result into the approriate stack.
* @param
ActionEvent
* @return
none
*/
private class ComboListener implements
ActionListener
{
public void actionPerformed(ActionEvent
e)
{
JComboBox jc =
(JComboBox)e.getSource();
String
source = (String)jc.getSelectedItem();
System.out.println("Combo : " + source);
if(jc == simulBox)
simulStack.push(source);
else
schemeStack.push(source);
}
}
/**
* RadioButtonListener's class listens to the
RadioButton's action
* and pushes
the results into the approriate stacks.
* @param ActionEvent
* @return none
*/
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String jrb = ((JRadioButton)e.getSource()).getText();
if(jrb.compareTo(moveRadio) == 0)
{
System.out.println("Motion: " +jrb);
motionStack.push(jrb);
}
else if(jrb.compareTo(stillRadio) == 0)
{
System.out.println("Motion: " + jrb);
motionStack.push(jrb);
}
else if(jrb.compareTo(pubRadio) == 0)
{
System.out.println("Interest: " + jrb);
interestStack.push(jrb);
}
else if(jrb.compareTo(subRadio) == 0)
{
System.out.println("Interest: " + jrb);
interestStack.push(jrb);
}
else if(jrb.compareTo(pubsubRadio)==0)
{
System.out.println("Interest: " + jrb);
interestStack.push(jrb);
}
else if(jrb.compareTo(blueRadio) == 0)
{
System.out.println("Team: " +
jrb);
teamStack.push(jrb);
}
else
if(jrb.compareTo(redRadio) == 0)
{
System.out.println("Team: " + jrb);
teamStack.push(jrb);
}
else
System.out.println("event? " + jrb);
}
}
/**
*
ButtonListener's class listen to the button and
* performs the approriate actions.
* @param ActionEvent
e
* @return none
*/
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
boolean errorFlag = false;
int errors = 0;
JButton
jb = (JButton)e.getSource();
StringBuffer message = new StringBuffer();
if(jb == geneButton)
{
// if((String)dimensionStack.peek() != dimText.getText())
dimensionStack.push(dimText.getText());
// if((String)playerStack.peek() !=
playText.getText())
playerStack.push(playText.getText());
// if((String)objectStack.peek() != objText.getText())
objectStack.push(objText.getText());
errorFlag = testAll();
if(errorFlag)
{
message.append("You will have to re-enter the field(s)
mention above again");
JOptionPane.showMessageDialog(null,
createErrorMessage(errorMessage,message.toString()),
"Error!",
JOptionPane.ERROR_MESSAGE);
errorMessage.removeAllElements();
message.delete(0,message.length());
return;
}
errorFlag = verifyValue();
if(errorFlag)
{
message.append("You will have enter
the valid value(s)!");
JOptionPane.showMessageDialog(null,
createErrorMessage(errorMessage,message.toString()),
"Error!",
JOptionPane.ERROR_MESSAGE);
errorMessage.removeAllElements();
message.delete(0,message.length());
return;
}
message.append("Please enter the program path: ");
String dirPath =
(String)JOptionPane.showInputDialog(null,
message,"Confirm program
path",
JOptionPane.INFORMATION_MESSAGE,
null,null,"cd /home/akp0013/ddm/script");
message.delete(0,message.length());
String filename =
generateIt(dirPath);
Execute myExecute = new Execute(commExec.toString());
message.append("The script named:
"+ filename + " is already executed");
message.append("\nThe InternalWindow
is now closing");
JOptionPane.showMessageDialog(null,message.toString(),"Closing
Confirmation",
JOptionPane.INFORMATION_MESSAGE);
}
dispose();
}
/**
* createErrorMessage's method is to create
error message given
* the Vector
that contains strings and String that contains message.
* @param
Vector contains all the string errors
* String contains
the message to be displayed
*
@return String to be displayed.
*/
public String
createErrorMessage(Vector inVector, String msg)
{
StringBuffer
msgHolder = new StringBuffer("Intercept error(s) in
field(s):\n");
int errors =
inVector.size();
for(int i=0;
i<errors; i++)
msgHolder.append("> " +
(String)inVector.elementAt(i)+"\n");
msgHolder.append(msg+"\n");
return msgHolder.toString();
}
/**
* verifyValue's method is to verify the
values of the user's input.
* This
method has a limitation on how to verify the user's inputs. As for now
* it only uses MAX_VALUE as the upper limit
on how big user may input.
*
MAX_VALUE is specified in Java API of Integer's class.
* @param
none
* @return boolean
false is no error detected else return true
*/
public boolean
verifyValue()
{
boolean error = false;
int dimension =
Integer.parseInt((String)dimensionStack.peek());
int players =
Integer.parseInt((String)playerStack.peek());
int objects =
Integer.parseInt((String)objectStack.peek());
if((dimension <= 0)||(dimension > Integer.MAX_VALUE))
{
errorMessage.addElement(dimensionText);
error = true;
}
if((players
<=0)||(players > Integer.MAX_VALUE))
{
errorMessage.addElement(numberOfPlayer);
error = true;
}
if((objects
<=0)||(objects > Integer.MAX_VALUE))
{
errorMessage.addElement(numberOfObject);
error = true;
}
return error;
}
/**
* testStack's method is to test whether the
stack is empty
* @param Stack
* @return boolean true if the stack is empty else return false
*/
public boolean testStack(Stack stack)
{
boolean flag =
false;
if(stack.empty())
flag = true;
return flag;
}
/**
* testAll's method is to test all the stacks
in the class.
* It uses
testStack's method to test the individual stacks.
* If one or more of the stacks is empty, it
would push in
* the string into
the Vector that contains all the errors for further
* error processing.
* @param
none
* @return boolean true
if error is encounter else return false
*/
public boolean testAll()
{
boolean flag = false;
if(testStack(motionStack))
{
System.out.println("motionStack is empty");
errorMessage.addElement(motion);
flag = true;
}
if(testStack(interestStack))
{
System.out.println("interestStack is empty");
errorMessage.addElement(interest);
flag = true;
}
if(testStack(teamStack))
{
System.out.println("teamStack is empty");
errorMessage.addElement(team);
flag = true;
}
if(testStack(schemeStack))
{
System.out.println("schemeStack
is empty");
errorMessage.addElement(scheme);
flag = true;
}
if(testStack(dimensionStack))
{
System.out.println("dimensionStack is empty");
errorMessage.addElement(dimensionText);
flag = true;
}
if(testStack(playerStack))
{
System.out.println("playerStack is empty");
errorMessage.addElement(numberOfPlayer);
flag = true;
}
if(testStack(objectStack))
{
System.out.println("objectStack is empty");
errorMessage.addElement(numberOfObject);
flag = true;
}
if(testStack(simulStack))
{
System.out.println("simulStack is empty");
errorMessage.addElement(simulation);
flag = true;
}
return flag;
}
/**
* generateIt's method is where the script
generation takes place.
* It forms
the script in here. Then it returns a filename of the script.
* @param
String directory or path of the program
* @return String filename
*/
public String
generateIt(String dirPath)
{
System.out.println("Generating script...");
String fileName =
simulStack.pop().toString()+openFrameCount+".sh";
String prog = "/usr/bin/ssh
";
String session =
"\"SESSIONNAME=session"+openFrameCount+";" +
"export
SESSIONNAME;NODEINFO=\"";
// String path = "cd
/home/akp0013/ddm/script;";
String path = dirPath;
String progName = "./amber_rti";
String dimen =
dimensionStack.pop().toString();
String obj = objectStack.pop().toString();
String sch =
schemeStack.pop().toString();
String mot = motionStack.pop().toString();
String inter =
interestStack.pop().toString();
String tem = teamStack.pop().toString();
String endIt = "\" & \n";
String nodeInfo = ";export NODEINFO;
";
Stack csp = new
Stack();
Stack copyPlayerStack =
playerStack;
int number =
(new Integer(playerStack.peek().toString())).intValue();
String num = String.valueOf(number);
StringBuffer copyBuffer = new
StringBuffer();
Stack seriesCSP
= new Stack();
Stack
storeBuffer = new Stack();
String tempStr = new String();
for(int i = number; i!=0 ; i--)
{
if(i>9)
{
tempStr = "csp" + i;
seriesCSP.push(tempStr);
csp.push(tempStr);
}
else
{
tempStr = "csp0" + i;
seriesCSP.push(tempStr);
csp.push(tempStr);
}
}
StringBuffer seriesIt =
new StringBuffer();
for(int
i=number;i!=0; i--)
if(i ==
1)
seriesIt.append((String)seriesCSP.pop());
else
seriesIt.append((String)seriesCSP.pop()+",");
int counter = 0;
int mid = number/2;
for(int i=number; i!=0; i--)
{
copyBuffer.append(prog+csp.pop()+" "+session+
num+":"+ seriesIt+":"+
String.valueOf(counter++)+"\""
+ nodeInfo + path +
progName+" " + dimen +" " + obj +" "+ sch+"
");
if((number ==
2)&&(i == number))
copyBuffer.append(moveRadio+" "+subRadio+"
"+blueRadio+" ");
else if((number == 2)&&(i == number-1))
copyBuffer.append(stillRadio+"
"+subRadio+" "+redRadio+" ");
else if((number >
2)&&(i>mid))
copyBuffer.append(mot+" "+ inter + " " +
redRadio);
else
copyBuffer.append(mot+" "+
inter + " " + blueRadio);
copyBuffer.append(endIt);
commExec.append(copyBuffer.toString());
System.out.println(copyBuffer.toString());
copyBuffer.delete(0,copyBuffer.length());
}
try
{
FileWriter newFile = new
FileWriter(fileName);
PrintWriter out = new PrintWriter(newFile);
out.println(commExec.toString());
out.close();
newFile.close();
}
catch(IOException e)
{
System.out.println("IOException: " + e);
}
return fileName;
}
}
}