/*
* ConnectTab.java
*
* Created on 25. Februar 2008, 19:29
*/
package gui.tabs;
import events.ServerEvent;
import framework.IATMCModel;
import framework.IATMClient;
import framework.IConnectTab;
import framework.IServer;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultComboBoxModel;
/**
*
* @author m0ng85
*/
public class ConnectTab extends javax.swing.JPanel implements IConnectTab, Observer {
private IATMClient atmClient;
private IATMCModel atmcModel;
private String[] functions;
/** Creates new form ConnectTab */
public ConnectTab(IATMClient atmc) {
atmClient = atmc;
atmcModel = atmc.getModel();
atmcModel.addNewObserver(this);
initComponents();
initPresetChooser();
setFunctionChooser(false);
nicknameInput.setText(System.getProperty("user.name"));
}
private void initPresetChooser() {
presetChooser.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
final ItemEvent fe = e;
if (!e.getItem().toString().equals("")) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
serverInput.setText(fe.getItem().toString().split(":")[0]);
portInput.setText(fe.getItem().toString().split(":")[1]);
}
});
}
}
});
presetChooser.removeAllItems();
presetChooser.addItem("");
LinkedList<IServer> servers = atmcModel.getServers();
for (IServer s : servers) {
presetChooser.addItem( s.getIP() + ":" + s.getPort());
}
}
private boolean inputVerified(String name, String port, String server) {
if (name.equals("")) {
atmClient.addStatusMessage("Invalid Nickname");
return false;
} else if (name.length() > 12) {
atmClient.addStatusMessage("Nickname too long");
return false;
} else if (server.equals("")) {
atmClient.addStatusMessage("Invalid Server");
return false;
} else {
try {
int portInt = Integer.parseInt(port);
return true;
} catch (NumberFormatException e) {
atmClient.addStatusMessage("Invalid Port");
return false;
}
}
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
connectionSettings = new javax.swing.JPanel();
nickname = new javax.swing.JLabel();
server = new javax.swing.JLabel();
port = new javax.swing.JLabel();
preset = new javax.swing.JLabel();
nicknameInput = new javax.swing.JTextField();
serverInput = new javax.swing.JTextField();
portInput = new javax.swing.JTextField();
presetChooser = new javax.swing.JComboBox();
connectButton = new javax.swing.JButton();
functionSettings = new javax.swing.JPanel();
function = new javax.swing.JLabel();
functionChooser = new javax.swing.JComboBox();
chooseButton = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
funcDisplay = new javax.swing.JLabel();
connectionSettings.setBorder(javax.swing.BorderFactory.createTitledBorder("Step 1 - Connection Settings"));
nickname.setText("Nickname");
server.setText("Server");
port.setText("Port");
preset.setText("Preset");
serverInput.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
serverInputActionPerformed(evt);
}
});
portInput.setText("4242");
presetChooser.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
presetChooserActionPerformed(evt);
}
});
connectButton.setText("Connect");
connectButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
connectButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout connectionSettingsLayout = new javax.swing.GroupLayout(connectionSettings);
connectionSettings.setLayout(connectionSettingsLayout);
connectionSettingsLayout.setHorizontalGroup(
connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(connectionSettingsLayout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(server)
.addComponent(nickname)
.addComponent(port)
.addComponent(preset))
.addGap(55, 55, 55)
.addGroup(connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(connectButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(presetChooser, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(portInput, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(serverInput, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nicknameInput, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE))
.addContainerGap(113, Short.MAX_VALUE))
);
connectionSettingsLayout.setVerticalGroup(
connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(connectionSettingsLayout.createSequentialGroup()
.addContainerGap()
.addGroup(connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nickname)
.addComponent(nicknameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(serverInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(server))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(port)
.addComponent(portInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(connectionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(presetChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(preset))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(connectButton)
.addContainerGap(15, Short.MAX_VALUE))
);
functionSettings.setBorder(javax.swing.BorderFactory.createTitledBorder("Step 2 - Choose Function"));
function.setText("Function");
functionChooser.setMinimumSize(new java.awt.Dimension(123, 20));
functionChooser.setPreferredSize(new java.awt.Dimension(123, 22));
chooseButton.setText("Choose");
chooseButton.setMaximumSize(new java.awt.Dimension(75, 23));
chooseButton.setMinimumSize(new java.awt.Dimension(75, 23));
chooseButton.setPreferredSize(new java.awt.Dimension(75, 23));
chooseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chooseButtonActionPerformed(evt);
}
});
jLabel1.setText("Current: ");
funcDisplay.setText("viewer");
javax.swing.GroupLayout functionSettingsLayout = new javax.swing.GroupLayout(functionSettings);
functionSettings.setLayout(functionSettingsLayout);
functionSettingsLayout.setHorizontalGroup(
functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(functionSettingsLayout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(function)
.addComponent(jLabel1))
.addGap(58, 58, 58)
.addGroup(functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(funcDisplay)
.addGroup(functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(chooseButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(functionChooser, javax.swing.GroupLayout.Alignment.TRAILING, 0, 136, Short.MAX_VALUE)))
.addContainerGap(109, Short.MAX_VALUE))
);
functionSettingsLayout.setVerticalGroup(
functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(functionSettingsLayout.createSequentialGroup()
.addContainerGap()
.addGroup(functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(function)
.addComponent(functionChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chooseButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(functionSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(funcDisplay))
.addContainerGap(17, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(functionSettings, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(connectionSettings, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(connectionSettings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(functionSettings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
atmClient.getModel().setNick(nicknameInput.getText());
if (connectButton.getText().equals("Connect") && inputVerified(nicknameInput.getText(), portInput.getText(), serverInput.getText())) {
if (atmClient.getModel().connect(serverInput.getText(), Integer.parseInt(portInput.getText()), nicknameInput.getText())) {
setFunctionChooser(true);
initPresetChooser();
atmClient.addStatusMessage("Connection established to " + serverInput.getText() + ":" + portInput.getText());
} else {
setFunctionChooser(false);
atmClient.addStatusMessage("Connection error");
}
} else {
atmClient.getModel().disconnect();
setFunctionChooser(false);
atmClient.addStatusMessage("Connection closed");
}
}//GEN-LAST:event_connectButtonActionPerformed
private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
String func = (String) functionChooser.getSelectedItem();
atmClient.getModel().setFunction(func);
funcDisplay.setText(func);
}//GEN-LAST:event_chooseButtonActionPerformed
private void presetChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_presetChooserActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_presetChooserActionPerformed
private void serverInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverInputActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_serverInputActionPerformed
public void setFunctionChooser(final boolean func) {
final ConnectTab tmp = this;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
function.setEnabled(func);
functionChooser.setEnabled(func);
chooseButton.setEnabled(func);
if (func) {
connectButton.setText("Disconnect");
atmClient.setTabs(true);
} else {
connectButton.setText("Connect");
atmClient.setTabs(false);
}
nicknameInput.setEnabled(!func);
serverInput.setEnabled(!func);
portInput.setEnabled(!func);
presetChooser.setEnabled(!func);
tmp.validate();
}
});
}
public void setFuncs(String[] funcs) {
functions = funcs;
functionChooser.setModel(new DefaultComboBoxModel(funcs));
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton chooseButton;
private javax.swing.JButton connectButton;
private javax.swing.JPanel connectionSettings;
private javax.swing.JLabel funcDisplay;
private javax.swing.JLabel function;
private javax.swing.JComboBox functionChooser;
private javax.swing.JPanel functionSettings;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel nickname;
private javax.swing.JTextField nicknameInput;
private javax.swing.JLabel port;
private javax.swing.JTextField portInput;
private javax.swing.JLabel preset;
private javax.swing.JComboBox presetChooser;
private javax.swing.JLabel server;
private javax.swing.JTextField serverInput;
// End of variables declaration//GEN-END:variables
private void refreshServers() {
initPresetChooser();
}
public void update(Observable o, Object arg) {
if (arg instanceof ServerEvent) {
refreshServers();
}
}
}