Finished implementing support for XML server list saving.

This commit is contained in:
StevenLawson 2014-08-20 13:35:04 -04:00
parent 0f25e91082
commit 737abc7697
6 changed files with 293 additions and 264 deletions

View file

@ -2,21 +2,22 @@ package me.StevenLawson.BukkitTelnetClient;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.logging.Level;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer; import javax.xml.transform.*;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.w3c.dom.*; import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class BTC_ConfigLoader public class BTC_ConfigLoader
{ {
private static final String SETTINGS_FILE = "settings.xml"; private static final String SETTINGS_FILE = "settings.xml";
private final List<PlayerCommandEntry> playerCommands = new ArrayList<>(); private final List<PlayerCommandEntry> playerCommands = new ArrayList<>();
private final List<ServerEntry> servers = new ArrayList<>(); private final Set<ServerEntry> servers = new HashSet<>();
public BTC_ConfigLoader() public BTC_ConfigLoader()
{ {
@ -44,10 +45,6 @@ public class BTC_ConfigLoader
final List<ServerEntry> oldServers = importOldConfig(); final List<ServerEntry> oldServers = importOldConfig();
this.servers.addAll(oldServers); this.servers.addAll(oldServers);
final HashSet<ServerEntry> uniqueServers = new HashSet<>(this.servers);
this.servers.clear();
this.servers.addAll(uniqueServers);
generateXML(settings); generateXML(settings);
if (verbose) if (verbose)
@ -77,10 +74,6 @@ public class BTC_ConfigLoader
public boolean save() public boolean save()
{ {
final HashSet<ServerEntry> uniqueServers = new HashSet<>(this.servers);
this.servers.clear();
this.servers.addAll(uniqueServers);
return generateXML(new File(SETTINGS_FILE)); return generateXML(new File(SETTINGS_FILE));
} }
@ -89,9 +82,9 @@ public class BTC_ConfigLoader
return this.playerCommands; return this.playerCommands;
} }
public List<ServerEntry> getServers() public Set<ServerEntry> getServers()
{ {
return this.servers; return servers;
} }
private boolean generateXML(final File file) private boolean generateXML(final File file)
@ -114,9 +107,9 @@ public class BTC_ConfigLoader
return true; return true;
} }
catch (Exception ex) catch (IllegalArgumentException | ParserConfigurationException | TransformerException | DOMException ex)
{ {
ex.printStackTrace(); BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex);
} }
return false; return false;
@ -143,10 +136,11 @@ public class BTC_ConfigLoader
hadErrors = true; hadErrors = true;
} }
} }
catch (Exception ex) catch (IOException | ParserConfigurationException | SAXException ex)
{ {
ex.printStackTrace();
hadErrors = true; hadErrors = true;
BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex);
} }
return hadErrors; return hadErrors;
@ -165,7 +159,7 @@ public class BTC_ConfigLoader
} }
catch (IOException ex) catch (IOException ex)
{ {
ex.printStackTrace(); BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex);
} }
} }
@ -187,7 +181,7 @@ public class BTC_ConfigLoader
while ((line = in.readLine()) != null) while ((line = in.readLine()) != null)
{ {
line = line.trim(); line = line.trim();
oldServers.add(new ServerEntry("legacy", line)); oldServers.add(new ServerEntry("legacy", line, false));
} }
} }
@ -196,192 +190,9 @@ public class BTC_ConfigLoader
} }
catch (IOException ex) catch (IOException ex)
{ {
ex.printStackTrace(); BukkitTelnetClient.LOGGER.log(Level.SEVERE, null, ex);
} }
return oldServers; return oldServers;
} }
public static class PlayerCommandEntry
{
private final String name;
private final String format;
public PlayerCommandEntry(String name, String format)
{
this.name = name;
this.format = format;
}
public String getFormat()
{
return format;
}
public String getName()
{
return name;
}
public static Element listToXML(final List<PlayerCommandEntry> playerCommands, final Document doc)
{
final Element plcElement = doc.createElement("playerCommands");
for (final PlayerCommandEntry command : playerCommands)
{
final Element commandElement = doc.createElement("playerCommand");
plcElement.appendChild(commandElement);
final Element commandName = doc.createElement("name");
commandName.appendChild(doc.createTextNode(command.getName()));
commandElement.appendChild(commandName);
final Element commandFormat = doc.createElement("format");
commandFormat.appendChild(doc.createTextNode(command.getFormat()));
commandElement.appendChild(commandFormat);
}
return plcElement;
}
public static boolean xmlToList(final List<PlayerCommandEntry> playerCommands, final Document doc)
{
NodeList playerCommandNodes = doc.getDocumentElement().getElementsByTagName("playerCommands");
if (playerCommandNodes.getLength() < 1)
{
return false;
}
playerCommandNodes = playerCommandNodes.item(0).getChildNodes();
playerCommands.clear();
for (int i = 0; i < playerCommandNodes.getLength(); i++)
{
final Node node = playerCommandNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
final Element element = (Element) node;
final PlayerCommandEntry command = new PlayerCommandEntry(
element.getElementsByTagName("name").item(0).getTextContent(),
element.getElementsByTagName("format").item(0).getTextContent()
);
playerCommands.add(command);
}
}
return true;
}
}
public static class ServerEntry
{
private final String name;
private final String address;
public ServerEntry(final String name, final String address)
{
this.name = name;
this.address = address;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
@Override
public int hashCode()
{
int hash = 7;
hash = 67 * hash + Objects.hashCode(this.name);
hash = 67 * hash + Objects.hashCode(this.address);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ServerEntry other = (ServerEntry) obj;
if (!Objects.equals(this.name, other.name))
{
return false;
}
if (!Objects.equals(this.address, other.address))
{
return false;
}
return true;
}
public static Element listToXML(final List<ServerEntry> servers, final Document doc)
{
final Element serversElement = doc.createElement("servers");
for (final ServerEntry command : servers)
{
final Element commandElement = doc.createElement("server");
serversElement.appendChild(commandElement);
final Element serverName = doc.createElement("name");
serverName.appendChild(doc.createTextNode(command.getName()));
commandElement.appendChild(serverName);
final Element serverAddress = doc.createElement("address");
serverAddress.appendChild(doc.createTextNode(command.getAddress()));
commandElement.appendChild(serverAddress);
}
return serversElement;
}
public static boolean xmlToList(final List<ServerEntry> servers, final Document doc)
{
NodeList serverNodes = doc.getDocumentElement().getElementsByTagName("servers");
if (serverNodes.getLength() < 1)
{
return false;
}
serverNodes = serverNodes.item(0).getChildNodes();
servers.clear();
for (int i = 0; i < serverNodes.getLength(); i++)
{
final Node node = serverNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
final Element element = (Element) node;
final ServerEntry server = new ServerEntry(
element.getElementsByTagName("name").item(0).getTextContent(),
element.getElementsByTagName("address").item(0).getTextContent()
);
servers.add(server);
}
}
return true;
}
}
} }

View file

@ -2,9 +2,7 @@ package me.StevenLawson.BukkitTelnetClient;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import javax.swing.Timer; import javax.swing.Timer;

View file

@ -156,15 +156,15 @@
<StringArray count="0"/> <StringArray count="0"/>
</Property> </Property>
</Properties> </Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;ServerEntry&gt;"/>
</AuxValues>
</Component> </Component>
<Component class="javax.swing.JCheckBox" name="chkAutoScroll"> <Component class="javax.swing.JCheckBox" name="chkAutoScroll">
<Properties> <Properties>
<Property name="selected" type="boolean" value="true"/> <Property name="selected" type="boolean" value="true"/>
<Property name="text" type="java.lang.String" value="AutoScroll"/> <Property name="text" type="java.lang.String" value="AutoScroll"/>
</Properties> </Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="chkAutoScrollActionPerformed"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JTextField" name="txtCommand"> <Component class="javax.swing.JTextField" name="txtCommand">
<Properties> <Properties>

View file

@ -1,41 +1,16 @@
package me.StevenLawson.BukkitTelnetClient; package me.StevenLawson.BukkitTelnetClient;
import java.awt.EventQueue; import java.awt.*;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent; import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.URL; import java.net.URL;
import java.util.Iterator; import java.util.*;
import java.util.LinkedList; import javax.swing.*;
import java.util.Map;
import java.util.logging.Level;
import javax.swing.JCheckBox;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException; import javax.swing.text.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class BTC_MainPanel extends javax.swing.JFrame public class BTC_MainPanel extends javax.swing.JFrame
{ {
@ -442,28 +417,55 @@ public class BTC_MainPanel extends javax.swing.JFrame
public final void loadServerList() public final void loadServerList()
{ {
txtServer.removeAllItems(); txtServer.removeAllItems();
for (final ServerEntry serverEntry : BukkitTelnetClient.config.getServers())
for (BTC_ConfigLoader.ServerEntry serverEntry : BukkitTelnetClient.config.getServers())
{ {
txtServer.addItem(serverEntry.getAddress()); txtServer.addItem(serverEntry);
if (serverEntry.isLastUsed())
{
txtServer.setSelectedItem(serverEntry);
}
} }
} }
public final void saveServersAndTriggerConnect() public final void saveServersAndTriggerConnect()
{ {
final String selectedServer = (String) txtServer.getSelectedItem(); final Object selectedItem = txtServer.getSelectedItem();
if (selectedServer == null || selectedServer.isEmpty()) ServerEntry entry;
if (selectedItem instanceof ServerEntry)
{ {
writeToConsole("Invalid server address."); entry = (ServerEntry) selectedItem;
return; }
else
{
String serverName = JOptionPane.showInputDialog(this, "Enter server name:", "Server Name", JOptionPane.PLAIN_MESSAGE).trim();
if (serverName.isEmpty())
{
serverName = "Unnamed";
}
entry = new ServerEntry(serverName, selectedItem.toString());
BukkitTelnetClient.config.getServers().add(entry);
} }
BukkitTelnetClient.config.getServers().add(new BTC_ConfigLoader.ServerEntry("legacy", selectedServer)); for (final ServerEntry existingEntry : BukkitTelnetClient.config.getServers())
{
if (entry.equals(existingEntry))
{
entry = existingEntry;
}
existingEntry.setLastUsed(false);
}
entry.setLastUsed(true);
BukkitTelnetClient.config.save(); BukkitTelnetClient.config.save();
connectionManager.triggerConnect(selectedServer); loadServerList();
connectionManager.triggerConnect(entry.getAddress());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -477,7 +479,7 @@ public class BTC_MainPanel extends javax.swing.JFrame
mainOutput = new javax.swing.JTextPane(); mainOutput = new javax.swing.JTextPane();
btnDisconnect = new javax.swing.JButton(); btnDisconnect = new javax.swing.JButton();
btnSend = new javax.swing.JButton(); btnSend = new javax.swing.JButton();
txtServer = new javax.swing.JComboBox(); txtServer = new javax.swing.JComboBox<ServerEntry>();
chkAutoScroll = new javax.swing.JCheckBox(); chkAutoScroll = new javax.swing.JCheckBox();
txtCommand = new javax.swing.JTextField(); txtCommand = new javax.swing.JTextField();
btnConnect = new javax.swing.JButton(); btnConnect = new javax.swing.JButton();
@ -525,13 +527,6 @@ public class BTC_MainPanel extends javax.swing.JFrame
chkAutoScroll.setSelected(true); chkAutoScroll.setSelected(true);
chkAutoScroll.setText("AutoScroll"); chkAutoScroll.setText("AutoScroll");
chkAutoScroll.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
chkAutoScrollActionPerformed(evt);
}
});
txtCommand.setEnabled(false); txtCommand.setEnabled(false);
txtCommand.addKeyListener(new java.awt.event.KeyAdapter() txtCommand.addKeyListener(new java.awt.event.KeyAdapter()
@ -736,11 +731,6 @@ public class BTC_MainPanel extends javax.swing.JFrame
} }
}//GEN-LAST:event_txtCommandKeyPressed }//GEN-LAST:event_txtCommandKeyPressed
private void chkAutoScrollActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_chkAutoScrollActionPerformed
{//GEN-HEADEREND:event_chkAutoScrollActionPerformed
// updateTextPane("");
}//GEN-LAST:event_chkAutoScrollActionPerformed
private void btnConnectActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnConnectActionPerformed private void btnConnectActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnConnectActionPerformed
{//GEN-HEADEREND:event_btnConnectActionPerformed {//GEN-HEADEREND:event_btnConnectActionPerformed
if (!btnConnect.isEnabled()) if (!btnConnect.isEnabled())
@ -796,7 +786,7 @@ public class BTC_MainPanel extends javax.swing.JFrame
private javax.swing.JSplitPane splitPane; private javax.swing.JSplitPane splitPane;
private javax.swing.JTable tblPlayers; private javax.swing.JTable tblPlayers;
private javax.swing.JTextField txtCommand; private javax.swing.JTextField txtCommand;
private javax.swing.JComboBox txtServer; private javax.swing.JComboBox<ServerEntry> txtServer;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
public javax.swing.JButton getBtnConnect() public javax.swing.JButton getBtnConnect()
@ -824,7 +814,7 @@ public class BTC_MainPanel extends javax.swing.JFrame
return txtCommand; return txtCommand;
} }
public javax.swing.JComboBox getTxtServer() public javax.swing.JComboBox<ServerEntry> getTxtServer()
{ {
return txtServer; return txtServer;
} }

View file

@ -0,0 +1,77 @@
package me.StevenLawson.BukkitTelnetClient;
import java.util.List;
import org.w3c.dom.*;
public class PlayerCommandEntry
{
private final String name;
private final String format;
public PlayerCommandEntry(String name, String format)
{
this.name = name;
this.format = format;
}
public String getFormat()
{
return format;
}
public String getName()
{
return name;
}
public static Element listToXML(final List<PlayerCommandEntry> playerCommands, final Document doc)
{
final Element plcElement = doc.createElement("playerCommands");
for (final PlayerCommandEntry command : playerCommands)
{
final Element commandElement = doc.createElement("playerCommand");
plcElement.appendChild(commandElement);
final Element commandName = doc.createElement("name");
commandName.appendChild(doc.createTextNode(command.getName()));
commandElement.appendChild(commandName);
final Element commandFormat = doc.createElement("format");
commandFormat.appendChild(doc.createTextNode(command.getFormat()));
commandElement.appendChild(commandFormat);
}
return plcElement;
}
public static boolean xmlToList(final List<PlayerCommandEntry> playerCommands, final Document doc)
{
NodeList playerCommandNodes = doc.getDocumentElement().getElementsByTagName("playerCommands");
if (playerCommandNodes.getLength() < 1)
{
return false;
}
playerCommandNodes = playerCommandNodes.item(0).getChildNodes();
playerCommands.clear();
for (int i = 0; i < playerCommandNodes.getLength(); i++)
{
final Node node = playerCommandNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
final Element element = (Element) node;
final PlayerCommandEntry command = new PlayerCommandEntry(
element.getElementsByTagName("name").item(0).getTextContent(),
element.getElementsByTagName("format").item(0).getTextContent()
);
playerCommands.add(command);
}
}
return true;
}
}

View file

@ -0,0 +1,153 @@
package me.StevenLawson.BukkitTelnetClient;
import java.util.*;
import org.w3c.dom.*;
public class ServerEntry
{
private String name;
private String address;
private boolean lastUsed = false;
public ServerEntry(final String name, final String address)
{
this.name = name;
this.address = address;
}
public ServerEntry(final String name, final String address, final boolean lastUsed)
{
this.name = name;
this.address = address;
this.lastUsed = lastUsed;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public boolean isLastUsed()
{
return lastUsed;
}
public void setLastUsed(boolean lastUsed)
{
this.lastUsed = lastUsed;
}
@Override
public int hashCode()
{
int hash = 7;
hash = 67 * hash + Objects.hashCode(this.name);
hash = 67 * hash + Objects.hashCode(this.address);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ServerEntry other = (ServerEntry) obj;
if (!Objects.equals(this.getName(), other.getName()))
{
return false;
}
if (!Objects.equals(this.getAddress(), other.getAddress()))
{
return false;
}
return true;
}
public static Element listToXML(final Set<ServerEntry> servers, final Document doc)
{
final Element serversElement = doc.createElement("servers");
for (final ServerEntry command : servers)
{
final Element commandElement = doc.createElement("server");
serversElement.appendChild(commandElement);
final Element serverName = doc.createElement("name");
serverName.appendChild(doc.createTextNode(command.getName()));
commandElement.appendChild(serverName);
final Element serverAddress = doc.createElement("address");
serverAddress.appendChild(doc.createTextNode(command.getAddress()));
commandElement.appendChild(serverAddress);
final Element serverLastUsed = doc.createElement("lastUsed");
serverLastUsed.appendChild(doc.createTextNode(Boolean.toString(command.isLastUsed())));
commandElement.appendChild(serverLastUsed);
}
return serversElement;
}
public static boolean xmlToList(final Set<ServerEntry> servers, final Document doc)
{
NodeList serverNodes = doc.getDocumentElement().getElementsByTagName("servers");
if (serverNodes.getLength() < 1)
{
return false;
}
serverNodes = serverNodes.item(0).getChildNodes();
servers.clear();
for (int i = 0; i < serverNodes.getLength(); i++)
{
final Node node = serverNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
final Element element = (Element) node;
final ServerEntry server = new ServerEntry(
element.getElementsByTagName("name").item(0).getTextContent(),
element.getElementsByTagName("address").item(0).getTextContent(),
Boolean.valueOf(element.getElementsByTagName("lastUsed").item(0).getTextContent())
);
servers.add(server);
}
}
return true;
}
@Override
public String toString()
{
return String.format("%s (%s)", this.name, this.address);
}
}