/*
* SettingsWindow.java
*
* Created on 7. Mai 2008, 17:24
*/
package gui.windows;
import events.ColorEvent;
import events.HeightEvent;
import events.ServerEvent;
import framework.IATMCModel;
import framework.IATMClient;
import framework.IRadarColor;
import framework.IServer;
import java.io.File;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;
import java.util.TreeMap;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.colorchooser.*;
import logic.RadarColor;
import logic.Server;
/**
*
* @author DMerz
*/
public class SettingsWindow extends javax.swing.JFrame implements Observer {
File data = new File("data/settings.xml");
TreeMap<String, IRadarColor> colors = new TreeMap<String, IRadarColor>();
LinkedList<IServer> servers = new LinkedList<IServer>();
IATMClient atmClient;
IATMCModel atmcModel;
Object LastSelectedItem;
LinkedList<IRadarColor> newColors = new LinkedList<IRadarColor>();
/** Creates new form SettingsWindow */
public SettingsWindow(IATMClient client) {
atmClient = client;
atmcModel = client.getModel();
atmcModel.addNewObserver(this);
initComponents();
SettingsPanelColorChooser.setPreviewPanel(new JPanel());
refreshServers();
refreshColors();
refreshHeights();
}
/** 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() {
SettingsTabbedPane = new javax.swing.JTabbedPane();
SettingsPanelGeneral = new javax.swing.JPanel();
SettingsPanelGeneralTextFieldMinHeight = new javax.swing.JTextField();
SettingsPanelGeneralTextFieldMaxHeight = new javax.swing.JTextField();
SettingsPanelGeneralButtonMinHeight = new javax.swing.JButton();
SettingsPanelGeneralButtonMaxHeight = new javax.swing.JButton();
SettingsPanelGeneralSeparator1 = new javax.swing.JSeparator();
SettingsPanelGeneralServerListScrollPane = new javax.swing.JScrollPane();
SettingsPanelGeneralServerList = new javax.swing.JList();
SettingsPanelGeneralTextFieldServer = new javax.swing.JTextField();
SettingsPanelGeneralButtonAddServer = new javax.swing.JButton();
SettingsPanelGeneralButtonRemoveServer = new javax.swing.JButton();
SettingsPanelColors = new javax.swing.JPanel();
SettingsPanelColorChooser = new javax.swing.JColorChooser();
SettingsPanelColorListScrollPane = new javax.swing.JScrollPane();
SettingsPanelColorList = new javax.swing.JList();
SettingsPanelColorSetDefault = new javax.swing.JButton();
SettingsPanelColorSave = new javax.swing.JButton();
SettingsPanelColorAccept = new javax.swing.JButton();
setTitle("Settings");
SettingsPanelGeneralButtonMinHeight.setText("Confirm Min Height");
SettingsPanelGeneralButtonMinHeight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsPanelGeneralButtonMinHeightActionPerformed(evt);
}
});
SettingsPanelGeneralButtonMaxHeight.setText("Confirm Max Height");
SettingsPanelGeneralButtonMaxHeight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsPanelGeneralButtonMaxHeightActionPerformed(evt);
}
});
SettingsPanelGeneralSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
SettingsPanelGeneralServerList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
SettingsPanelGeneralServerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
SettingsPanelGeneralServerListScrollPane.setViewportView(SettingsPanelGeneralServerList);
SettingsPanelGeneralTextFieldServer.setText("server:port");
SettingsPanelGeneralButtonAddServer.setText("Add Server");
SettingsPanelGeneralButtonAddServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsPanelGeneralButtonAddServerActionPerformed(evt);
}
});
SettingsPanelGeneralButtonRemoveServer.setText("Remove Selected");
SettingsPanelGeneralButtonRemoveServer.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsPanelGeneralButtonRemoveServerActionPerformed(evt);
}
});
javax.swing.GroupLayout SettingsPanelGeneralLayout = new javax.swing.GroupLayout(SettingsPanelGeneral);
SettingsPanelGeneral.setLayout(SettingsPanelGeneralLayout);
SettingsPanelGeneralLayout.setHorizontalGroup(
SettingsPanelGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(SettingsPanelGeneralLayout.createSequentialGroup()
.addContainerGap()
.addGroup(SettingsPanelGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SettingsPanelGeneralServerListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralButtonMinHeight, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralTextFieldMinHeight, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralButtonMaxHeight, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralTextFieldMaxHeight, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralTextFieldServer, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralButtonAddServer, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
.addComponent(SettingsPanelGeneralButtonRemoveServer, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelGeneralSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(485, 485, 485))
);
SettingsPanelGeneralLayout.setVerticalGroup(
SettingsPanelGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(SettingsPanelGeneralLayout.createSequentialGroup()
.addGroup(SettingsPanelGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SettingsPanelGeneralSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
.addGroup(SettingsPanelGeneralLayout.createSequentialGroup()
.addContainerGap()
.addComponent(SettingsPanelGeneralTextFieldMinHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelGeneralButtonMinHeight)
.addGap(13, 13, 13)
.addComponent(SettingsPanelGeneralTextFieldMaxHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelGeneralButtonMaxHeight)
.addGap(12, 12, 12)
.addComponent(SettingsPanelGeneralTextFieldServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelGeneralButtonAddServer)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelGeneralServerListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelGeneralButtonRemoveServer)))
.addContainerGap())
);
SettingsTabbedPane.addTab("General", SettingsPanelGeneral);
SettingsPanelColorChooser.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
SettingsPanelColorChooser.setEnabled(false);
SettingsPanelColorChooser.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
SettingsPanelColorChooserPropertyChange(evt);
}
});
SettingsPanelColorList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
SettingsPanelColorList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
SettingsPanelColorList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
SettingsPanelColorListValueChanged(evt);
}
});
SettingsPanelColorListScrollPane.setViewportView(SettingsPanelColorList);
SettingsPanelColorSetDefault.setText("Default");
SettingsPanelColorSave.setText("Save");
SettingsPanelColorSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsPanelColorSaveActionPerformed(evt);
}
});
SettingsPanelColorAccept.setText("Accept");
SettingsPanelColorAccept.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SettingsPanelColorAcceptActionPerformed(evt);
}
});
javax.swing.GroupLayout SettingsPanelColorsLayout = new javax.swing.GroupLayout(SettingsPanelColors);
SettingsPanelColors.setLayout(SettingsPanelColorsLayout);
SettingsPanelColorsLayout.setHorizontalGroup(
SettingsPanelColorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SettingsPanelColorsLayout.createSequentialGroup()
.addContainerGap()
.addComponent(SettingsPanelColorListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(SettingsPanelColorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, SettingsPanelColorsLayout.createSequentialGroup()
.addComponent(SettingsPanelColorAccept)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelColorSetDefault, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelColorSave, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
.addContainerGap())
.addComponent(SettingsPanelColorChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
SettingsPanelColorsLayout.setVerticalGroup(
SettingsPanelColorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SettingsPanelColorsLayout.createSequentialGroup()
.addContainerGap(370, Short.MAX_VALUE)
.addGroup(SettingsPanelColorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SettingsPanelColorSave)
.addComponent(SettingsPanelColorSetDefault))
.addContainerGap())
.addGroup(SettingsPanelColorsLayout.createSequentialGroup()
.addContainerGap()
.addGroup(SettingsPanelColorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SettingsPanelColorListScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(SettingsPanelColorsLayout.createSequentialGroup()
.addComponent(SettingsPanelColorChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SettingsPanelColorAccept)))
.addContainerGap(11, Short.MAX_VALUE))
);
SettingsTabbedPane.addTab("Colors", SettingsPanelColors);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(SettingsTabbedPane))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(SettingsTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void SettingsPanelGeneralButtonMinHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsPanelGeneralButtonMinHeightActionPerformed
try {
int height = Integer.parseInt(SettingsPanelGeneralTextFieldMinHeight.getText());
atmcModel.setMinHeight(height);
} catch (NumberFormatException e) {
this.atmClient.addStatusMessage("Invalid Minimum Height (" + SettingsPanelGeneralTextFieldMinHeight.getText() + ")");
}
}//GEN-LAST:event_SettingsPanelGeneralButtonMinHeightActionPerformed
private void SettingsPanelGeneralButtonMaxHeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsPanelGeneralButtonMaxHeightActionPerformed
try {
int height = Integer.parseInt(SettingsPanelGeneralTextFieldMaxHeight.getText());
atmcModel.setMaxHeight(height);
} catch (NumberFormatException e) {
this.atmClient.addStatusMessage("Invalid Maximum Height (" + SettingsPanelGeneralTextFieldMaxHeight.getText() + ")");
}
}//GEN-LAST:event_SettingsPanelGeneralButtonMaxHeightActionPerformed
private void verifyServerInput(String input) throws IllegalArgumentException, NumberFormatException {
if (!input.contains(":")) {
throw new IllegalArgumentException();
}
String ip = input.split(":")[0];
String port = input.split(":")[1];
if (ip.length() <= 0) {
throw new IllegalArgumentException();
}
if (port.length() <= 0) {
throw new IllegalArgumentException();
}
}
private void SettingsPanelGeneralButtonAddServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsPanelGeneralButtonAddServerActionPerformed
try {
String serverIpPort = SettingsPanelGeneralTextFieldServer.getText();
verifyServerInput(serverIpPort);
String ip = serverIpPort.split(":")[0];
int port = Integer.parseInt(serverIpPort.split(":")[1]);
atmcModel.addServer(new Server(ip, port));
} catch (NumberFormatException e) {
atmClient.addStatusMessage("Invalid Server Input");
} catch (IllegalArgumentException e) {
atmClient.addStatusMessage("Invalid Server Input");
}
}//GEN-LAST:event_SettingsPanelGeneralButtonAddServerActionPerformed
private void SettingsPanelGeneralButtonRemoveServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsPanelGeneralButtonRemoveServerActionPerformed
if (SettingsPanelGeneralServerList.getSelectedValue() != null) {
String server = SettingsPanelGeneralServerList.getSelectedValue().toString();
String ip = server.split(":")[0];
int port = Integer.parseInt(server.split(":")[1]);
atmcModel.removeServer(new Server(ip, port));
}
}//GEN-LAST:event_SettingsPanelGeneralButtonRemoveServerActionPerformed
private void SettingsPanelColorListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_SettingsPanelColorListValueChanged
if (SettingsPanelColorList.getSelectedValue() != null) {
LastSelectedItem = SettingsPanelColorList.getSelectedValue();
SettingsPanelColorChooser.setColor(colors.get(LastSelectedItem.toString()).getColor());
}
}//GEN-LAST:event_SettingsPanelColorListValueChanged
private void SettingsPanelColorSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsPanelColorSaveActionPerformed
for (IRadarColor c : newColors) {
atmcModel.addColor(new RadarColor(c.getID(), c.getColor()));
}
}//GEN-LAST:event_SettingsPanelColorSaveActionPerformed
private void SettingsPanelColorChooserPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_SettingsPanelColorChooserPropertyChange
}//GEN-LAST:event_SettingsPanelColorChooserPropertyChange
private void SettingsPanelColorAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SettingsPanelColorAcceptActionPerformed
if (LastSelectedItem != null) {
newColors.add(new RadarColor(LastSelectedItem.toString(), SettingsPanelColorChooser.getColor()));
}
}//GEN-LAST:event_SettingsPanelColorAcceptActionPerformed
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton SettingsPanelColorAccept;
private javax.swing.JColorChooser SettingsPanelColorChooser;
private javax.swing.JList SettingsPanelColorList;
private javax.swing.JScrollPane SettingsPanelColorListScrollPane;
private javax.swing.JButton SettingsPanelColorSave;
private javax.swing.JButton SettingsPanelColorSetDefault;
private javax.swing.JPanel SettingsPanelColors;
private javax.swing.JPanel SettingsPanelGeneral;
private javax.swing.JButton SettingsPanelGeneralButtonAddServer;
private javax.swing.JButton SettingsPanelGeneralButtonMaxHeight;
private javax.swing.JButton SettingsPanelGeneralButtonMinHeight;
private javax.swing.JButton SettingsPanelGeneralButtonRemoveServer;
private javax.swing.JSeparator SettingsPanelGeneralSeparator1;
private javax.swing.JList SettingsPanelGeneralServerList;
private javax.swing.JScrollPane SettingsPanelGeneralServerListScrollPane;
private javax.swing.JTextField SettingsPanelGeneralTextFieldMaxHeight;
private javax.swing.JTextField SettingsPanelGeneralTextFieldMinHeight;
private javax.swing.JTextField SettingsPanelGeneralTextFieldServer;
private javax.swing.JTabbedPane SettingsTabbedPane;
// End of variables declaration//GEN-END:variables
private void refreshColors() {
System.out.println("Refreshing colors");
colors = atmcModel.getRadarColors();
DefaultListModel colorModel = new DefaultListModel();
for (String color : colors.keySet()) {
colorModel.addElement(color);
}
SettingsPanelColorList.setModel(colorModel);
SettingsPanelColorList.validate();
}
private void refreshServers() {
System.out.println("Refreshing Servers");
servers = atmcModel.getServers();
DefaultListModel serverModel = new DefaultListModel();
for (IServer server : servers) {
serverModel.addElement(server.toString());
}
SettingsPanelGeneralServerList.setModel(serverModel);
SettingsPanelGeneralServerList.validate();
}
private void refreshHeights() {
System.out.println("Refreshing Heights");
SettingsPanelGeneralTextFieldMaxHeight.setText(atmcModel.getMaxHeight() + "");
SettingsPanelGeneralTextFieldMinHeight.setText(atmcModel.getMinHeight() + "");
}
public void update(Observable o, Object arg) {
if (arg instanceof ColorEvent) {
refreshColors();
} else if (arg instanceof ServerEvent) {
refreshServers();
} else if (arg instanceof HeightEvent) {
refreshHeights();
}
}
}