/* * loadMachine.java * * Created on December 7, 2002, 2:28 PM */ package JSE; import java.io.*; /* * @author S */ public class Machine extends javax.swing.JFrame { /** Creates new form loadMachine */ private ReadMachine rm; private DataInputStream in; private LoadedMachine mach[]; private int loadedNum; private String path, machName, prot, fn; public Machine() { initComponents(); mach = new LoadedMachine[2]; loadedNum = 0; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtProtoName = new javax.swing.JTextField(); txtMachineName = new javax.swing.JTextField(); cmdBtnCancel = new javax.swing.JButton(); cmdBtnEnter = new javax.swing.JButton(); setTitle("Machine"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jPanel1.setPreferredSize(new java.awt.Dimension(600, 200)); jLabel1.setText("Load Machine: Full Machine Name is ."); jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1)); jLabel2.setText("Please Enter Protocol_Name:"); jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 70, -1, -1)); jLabel3.setText("Please Enter Machine Name:"); jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 100, -1, -1)); jPanel1.add(txtProtoName, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 70, 310, 20)); txtMachineName.setMaximumSize(new java.awt.Dimension(4, 20)); txtMachineName.setName("null"); jPanel1.add(txtMachineName, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 100, 310, -1)); cmdBtnCancel.setText("Cancel"); cmdBtnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdBtnCancelActionPerformed(evt); } }); jPanel1.add(cmdBtnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 150, -1, -1)); cmdBtnEnter.setText("Submit"); cmdBtnEnter.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdBtnEnterActionPerformed(evt); } }); jPanel1.add(cmdBtnEnter, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 150, -1, -1)); getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH); pack(); }//GEN-END:initComponents private void cmdBtnEnterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBtnEnterActionPerformed // Add your handling code here: boolean result; File dir1 = new File ("."); //if ( cmdBtnEnter.getText().compareTo("Load") == 0 ) //{ //Open Machine File rm = new ReadMachine(); try{ path = dir1.getCanonicalPath(); machName = getMachineName(); prot = getProtocolName(); fn = path + "\\" + prot + "." + machName; result = rm.openFile(fn); if ( result == false ) { txtMachineName.setText("File Open Failed: " + fn); } else{ in = rm.getInFile(); loadIt(); } }catch(Exception e){ txtMachineName.setText("Failed"); } //} }//GEN-LAST:event_cmdBtnEnterActionPerformed public void loadIt() { int numStates; int initState; int numFinalStates; int numEdges = 0; int finalStIds[]; int i, j; int thisMach = 0; State thisState; char c; //t.toString().valueOf(); //in.skip(' '); //Only Load 2 machines at one time, 3rd machine loaded overwrites first if ( loadedNum == 0 || loadedNum == 2 ){ thisMach = 0; loadedNum = 1; } else if ( loadedNum == 1 ){ thisMach = 1; loadedNum = 2; } try{ //Load from File //c = in.readChar(); numStates = in.readInt(); initState = in.readInt(); numFinalStates = in.readInt(); finalStIds = new int[numFinalStates]; //Reading in Final States for ( i = 0; i < numFinalStates; i++) { finalStIds[i] = in.readInt(); } mach[thisMach] = new LoadedMachine(machName, numStates, initState, numFinalStates); //Build States for ( i = 0; i < numStates; i++) { numEdges = in.readInt(); thisState = mach[thisMach].getState(i); thisState.setNumEdges(numEdges); for ( j = 0; j < numEdges; j++){ thisState.getEdge(j).setcommSign(in.readChar()); thisState.getEdge(j).setLetter(in.readChar()); thisState.getEdge(j).setToState(mach[thisMach].getState((in.readInt() - 1))); } } //Set FinalStates for ( i = 0; i < numFinalStates; i++) { mach[thisMach].setFinalState(i, (finalStIds[i]-1)); } txtMachineName.setText("Loaded"); }catch (Exception e){ txtMachineName.setText("Not Loaded"); } } public void drawIt() { } public LoadedMachine getMachine(int mNum) { return mach[mNum]; } public void unLoadMachines() { //Set mach[] = Null Destroy them //Clear the Drawings } public String getMachineName() { String tempName; tempName = txtMachineName.getText(); return tempName; } public String getProtocolName() { String tempName; tempName = txtProtoName.getText(); return tempName; } public void setBtnEnterText(String newName) { cmdBtnEnter.setText(newName); } private void cmdBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdBtnCancelActionPerformed // Add your handling code here: }//GEN-LAST:event_cmdBtnCancelActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new Machine().show(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField txtProtoName; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel2; private javax.swing.JButton cmdBtnEnter; private javax.swing.JPanel jPanel1; private javax.swing.JButton cmdBtnCancel; private javax.swing.JTextField txtMachineName; // End of variables declaration//GEN-END:variables }