public void onWho(ActionEvent actionEvent) { StandardDialog dlg = new StandardDialog(); dlg.setTitle("Your name and address"); String strName = prefs.get(NAME, server.ip + ":" + server.port); JTextField name = new JTextField(strName), ip = new JTextField(server.ip), port = new JTextField(server.port + ""); ip.setEditable(false); port.setEditable(false); LabelledItemPanel myContentPane = new LabelledItemPanel(); myContentPane.setBorder(BorderFactory.createEtchedBorder()); myContentPane.addItem("Your name", name); myContentPane.addItem("", new JLabel("Tell your buddy the follows:")); myContentPane.addItem("Your IP", ip); myContentPane.addItem("Your port", port); dlg.setContentPane(myContentPane); dlg.setSize(300, 300); GUIUtils.centerWithinScreen(dlg); dlg.setVisible(true); if (!dlg.hasUserCancelled()) { prefs.put(NAME, name.getText()); server.myName = name.getText(); } }
// add a buddy public void onAdd(ActionEvent actionEvent) { String strName = prefs.get(NAME, null); if (strName == null || strName.trim().length() == 0) { JOptionPane.showMessageDialog(this, "Please set your name first"); onWho(actionEvent); } if (strName == null || strName.trim().length() == 0) { return; } StandardDialog dlg = new StandardDialog(); dlg.setTitle("Buddy information"); JTextField ip = new JTextField(), port = new JTextField(); LabelledItemPanel myContentPane = new LabelledItemPanel(); myContentPane.setBorder(BorderFactory.createEtchedBorder()); myContentPane.addItem("", new JLabel("Please ask buddy his/her:")); myContentPane.addItem("IP", ip); myContentPane.addItem("port", port); dlg.setContentPane(myContentPane); dlg.setSize(300, 200); GUIUtils.centerWithinScreen(dlg); dlg.setVisible(true); if (!dlg.hasUserCancelled()) { String buddyHost = ip.getText(); String buddyPort = port.getText(); if (buddyHost.equals(server.ip) && buddyPort.equals(server.port + "")) { JOptionPane.showMessageDialog(this, "Well, why talk with yourself?"); return; } // ask buddy's name String buddyName = server.addBuddy(buddyHost, buddyPort); if (buddyName == null || MoAgent.FAIL.equals(buddyName)) { JOptionPane.showMessageDialog(this, "Buddy is not answering!"); } else { Buddy b = new Buddy(); b.name = buddyName; b.host = buddyHost; b.port = buddyPort; b.myID = server.myName; lstModel.addElement(b); } } }