import java.awt.Dimension; import java.awt.Toolkit; /** * TransactClient provides a GUI for access to Music DB via JavaTelnet. * @author Lars Appel * @version 0.2 */ public class TransactClient extends javax.swing.JFrame { // the server connection HostIO host; // the dataset objects MasterSet composersMaster; MasterSet albumsMaster; // the GUI componets ListPanel composersPanel; ListPanel albumsPanel; // the Transact prompt is somewhat strange (uses Esc sequences) static final String TRAN_PROMPT = "> \u001B&a+0C \u001B&a-1C"; /** Initializes the Form */ public TransactClient(HostIO host, MasterSet composers, MasterSet albums) { this.host = host; composersMaster = composers; albumsMaster = albums; initComponents (); composersPanel = new ListPanel(this, composersMaster); composersTab.add(composersPanel); albumsPanel = new ListPanel(this, albumsMaster); albumsTab.add(albumsPanel); pack (); Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); Dimension win = getSize(); setLocation((scr.width - win.width)/2, (scr.height - win.height)/2); } /** 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 FormEditor. */ private void initComponents () {//GEN-BEGIN:initComponents setTitle ("Transact Music Client 0.2"); addWindowListener (new java.awt.event.WindowAdapter () { public void windowClosing (java.awt.event.WindowEvent evt) { exitForm (evt); } } ); getContentPane ().setLayout (new java.awt.BorderLayout ()); jTabbedPane1 = new javax.swing.JTabbedPane (); composersTab = new javax.swing.JPanel (); composersTab.addComponentListener (new java.awt.event.ComponentAdapter () { public void componentShown (java.awt.event.ComponentEvent evt) { composersTabShown (evt); } } ); composersTab.setLayout (new java.awt.BorderLayout ()); jTabbedPane1.addTab ("Composers", composersTab); albumsTab = new javax.swing.JPanel (); albumsTab.addComponentListener (new java.awt.event.ComponentAdapter () { public void componentShown (java.awt.event.ComponentEvent evt) { albumsTabShown (evt); } } ); albumsTab.setLayout (new java.awt.BorderLayout ()); jTabbedPane1.addTab ("Albums", albumsTab); getContentPane ().add (jTabbedPane1, "Center"); }//GEN-END:initComponents private void albumsTabShown (java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_albumsTabShown // Add your handling code here: albumsMaster.fetchList(); }//GEN-LAST:event_albumsTabShown private void composersTabShown (java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_composersTabShown // Add your handling code here: composersMaster.fetchList(); }//GEN-LAST:event_composersTabShown /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm try { host.answerTo(TRAN_PROMPT, "exit"); host.answerTo(":", "bye"); host.skipUntilStartsWith("CPU="); host.close(); } catch (Exception e) { System.out.println("problems while trying to leave server"); } System.exit (0); }//GEN-LAST:event_exitForm // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JPanel composersTab; private javax.swing.JPanel albumsTab; // End of variables declaration//GEN-END:variables public static void main(java.lang.String[] args) { String hostName = (args.length > 0) ? args[0] : "my3000"; String helloCmd = "hello " + ((args.length > 1) ? args[1] : "user.demo"); System.out.println("Connecting to host..."); HostIO host = new HostIO(); try { host.open(hostName); host.answerTo("MPE/iX:", helloCmd); host.skipLines(1); // the echo for (;;) { while (host.readln().length() == 0) { // skip blank line(s) } if (host.getLine().indexOf("PASSWORD") >= 0) { String pw = HelloDialog.getPass(helloCmd, host.getLine()); if (pw == null) { System.out.println("user cancelled password dialog"); System.exit(0); } host.writeln(pw); // continue with loop in case of add'l password prompts } else { host.pushBack(); break; } } System.out.println("Launching server program..."); host.answerTo(":", "file music=music.pub"); host.answerTo(":", "run tMusic"); } catch (Exception e) { System.out.println("failed to launch server program"); System.exit(0); } System.out.println("Initializing data objects..."); MasterSet composersMaster = new MasterSet( new String[] {"Composer", "Birthplace", "born", "died"}, new int[] {16, 40, 16, 16}, new String[] {"Composer", "Birth", "Death", "Birthplace", "Comment"}, new int[] {16, 16, 16, 40, 40}, new String[] {"COMPOSERNAME> ", "BIRTH> ", "DEATH> ", "BIRTHPLACE> ", "COMMENT> "}, new int[] {0, 3, 1, 2}, host, TRAN_PROMPT, "list composers", "review composer", "add composer", "update composer", "delete composer" ); MasterSet albumsMaster = new MasterSet( new String[] {"Album Code", "Album Title", "Recording Co", "Recording Date"}, new int[] {10, 40, 16, 16}, new String[] {"Album Code", "Album Title", "Medium", "Album Cost", "Recording Company", "Recording Date", "Mfg Code", "Comment"}, new int[] {10, 40, 2, 8, 16, 16, 40, 40}, new String[] {"ALBUMCODE> ", "ALBUMTITLE> ", "MEDIUM> ", "ALBUMCOST> ", "RECORDINGCO> ", "DATERECORDED> ", "MFGCODE> ", "COMMENT> "}, new int[] {0, 1, 4, 5}, host, TRAN_PROMPT, "list albums", "review album", "add album", "update album", "delete album" ); System.out.println("Launching the GUI..."); new TransactClient(host, composersMaster, albumsMaster).show(); } }