Eliminated use of System.out.

Organized some methods/classes.
This commit is contained in:
StevenLawson 2014-08-17 13:52:11 -04:00
parent e2eaacd1e0
commit ad0ec69ac9
6 changed files with 249 additions and 287 deletions

View file

@ -6,7 +6,6 @@ import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Map;
import java.util.logging.Level;
import javax.swing.Timer;
@ -21,8 +20,6 @@ public class BTC_ConnectionManager
private int port;
private boolean canDoDisconnect = false;
private String loginName;
private final ByteArrayOutputStream consoleBuffer = new ByteArrayOutputStream();
private final PrintStream consoleStream = new PrintStream(consoleBuffer);
public BTC_ConnectionManager()
{
@ -37,7 +34,7 @@ public class BTC_ConnectionManager
btc.getTxtServer().setEnabled(false);
btc.getBtnDisconnect().setEnabled(true);
System.out.println("Connecting to " + hostname + ":" + port + "...");
btc.writeToConsole("Connecting to " + hostname + ":" + port + "...\n");
this.hostname = hostname;
this.port = port;
@ -102,7 +99,7 @@ public class BTC_ConnectionManager
updateTitle(false);
System.out.println("\nDisconnected.");
btc.writeToConsole("\nDisconnected.\n");
}
public void sendCommand(final String text)
@ -116,7 +113,7 @@ public class BTC_ConnectionManager
{
if (verbose)
{
consoleStream.format("%s\r\n", text);
BTC_MainPanel.CONSOLE_STREAM.format("%s\r\n", text);
}
this.telnetClient.getOutputStream().write((text + "\n").getBytes());
@ -152,11 +149,12 @@ public class BTC_ConnectionManager
this.connectThread = new Thread(new Runnable()
{
private final BTC_MainPanel btc = BukkitTelnetClient.mainPanel;
@Override
public void run()
{
final BTC_MainPanel btc = BukkitTelnetClient.mainPanel;
final ByteArrayOutputStream consoleBuffer = new ByteArrayOutputStream();
try
{
BTC_ConnectionManager.this.telnetClient.connect(hostname, port);
@ -200,7 +198,7 @@ public class BTC_ConnectionManager
}
else
{
final Map<String, BTC_PlayerListDecoder.PlayerInfo> playerList = BTC_PlayerListDecoder.checkForPlayerListMessage(line);
final Map<String, PlayerInfo> playerList = BTC_PlayerListDecoder.checkForPlayerListMessage(line);
if (playerList != null)
{
btc.updatePlayerList(playerList);
@ -209,7 +207,7 @@ public class BTC_ConnectionManager
{
if (!BTC_FormatHandler.skipLine(line))
{
System.out.print(line);
btc.writeToConsole(line);
}
}
}
@ -223,7 +221,7 @@ public class BTC_ConnectionManager
final String line = consoleBuffer.toString();
if (line.endsWith("Username: ") || line.endsWith("Password: "))
{
System.out.print(consoleBuffer.toString());
btc.writeToConsole(line);
consoleBuffer.reset();
}
}

View file

@ -269,7 +269,6 @@
</Column>
</TableColumnModel>
</Property>
<Property name="rowSelectionAllowed" type="boolean" value="true"/>
<Property name="selectionModel" type="javax.swing.ListSelectionModel" editor="org.netbeans.modules.form.editors2.JTableSelectionModelEditor">
<JTableSelectionModel selectionMode="0"/>
</Property>

View file

@ -10,10 +10,12 @@ 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.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
@ -35,13 +37,19 @@ public class BTC_MainPanel extends javax.swing.JFrame
{
private static final String SERVERS_FILE_NAME = "btc_servers.cfg";
public static final ByteArrayOutputStream CONSOLE = new ByteArrayOutputStream();
public static final PrintStream CONSOLE_STREAM = new PrintStream(CONSOLE);
private final BTC_ConnectionManager connectionManager = new BTC_ConnectionManager();
private final LinkedList<String> serverList = new LinkedList<>();
public BTC_MainPanel()
{
initComponents();
}
public void setup()
{
this.txtServer.getEditor().getEditorComponent().addKeyListener(new KeyAdapter()
{
@Override
@ -66,31 +74,143 @@ public class BTC_MainPanel extends javax.swing.JFrame
setupTablePopup();
this.connectionManager.updateTitle(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static final class CommandMenuItem extends JMenuItem
public final void updateTextPane(final String text)
{
private final ServerCommand command;
private final BTC_PlayerListDecoder.PlayerInfo player;
public CommandMenuItem(String text, ServerCommand command, BTC_PlayerListDecoder.PlayerInfo player)
SwingUtilities.invokeLater(new Runnable()
{
super(text);
this.command = command;
this.player = player;
@Override
public void run()
{
final StyledDocument styledDocument = mainOutput.getStyledDocument();
try
{
styledDocument.insertString(
styledDocument.getLength(),
text,
StyleContext.getDefaultStyleContext().addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, BTC_FormatHandler.getColor(text))
);
}
catch (BadLocationException ex)
{
throw new RuntimeException(ex);
}
if (BTC_MainPanel.this.chkAutoScroll.isSelected() && BTC_MainPanel.this.mainOutput.getSelectedText() == null)
{
BTC_MainPanel.this.mainOutput.setCaretPosition(styledDocument.getLength() - 1);
}
}
});
}
public final void updateConsole()
{
final String data = CONSOLE.toString();
CONSOLE.reset();
final String[] lines = data.split("\\n");
for (String line : lines)
{
updateTextPane(line + '\n');
}
}
public final void writeToConsole(String line)
{
CONSOLE_STREAM.append(line);
updateConsole();
}
public final PlayerInfo getSelectedPlayer()
{
String name = null;
String ip = null;
String displayName = null;
final JTable table = BTC_MainPanel.this.tblPlayers;
final DefaultTableModel model = (DefaultTableModel) table.getModel();
final int selectedRow = table.getSelectedRow();
if (selectedRow < 0)
{
return null;
}
public ServerCommand getCommand()
for (int col = 0; col <= 2; col++)
{
return command;
final int modelRow = table.convertRowIndexToModel(selectedRow);
final int modelCol = table.convertColumnIndexToModel(col);
final String colName = model.getColumnName(modelCol);
final Object value = model.getValueAt(modelRow, modelCol);
if (null != colName)
{
switch (colName)
{
case "Name":
{
name = value.toString();
break;
}
case "IP":
{
ip = value.toString();
break;
}
case "Display Name":
{
displayName = value.toString();
break;
}
}
}
}
public BTC_PlayerListDecoder.PlayerInfo getPlayer()
if (name != null && ip != null & displayName != null)
{
return player;
return new PlayerInfo(name, ip, displayName);
}
else
{
return null;
}
}
public final void updatePlayerList(final Map<String, PlayerInfo> playerList)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
final JTable table = BTC_MainPanel.this.tblPlayers;
final DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
final Iterator<Map.Entry<String, PlayerInfo>> it = playerList.entrySet().iterator();
while (it.hasNext())
{
final Map.Entry<String, PlayerInfo> entry = it.next();
PlayerInfo playerInfo = entry.getValue();
model.addRow(new Object[]
{
playerInfo.getName(),
playerInfo.getDisplayName(),
playerInfo.getIp()
});
}
}
});
}
public static enum ServerCommand
@ -124,6 +244,29 @@ public class BTC_MainPanel extends javax.swing.JFrame
}
}
public static final class CommandMenuItem extends JMenuItem
{
private final ServerCommand command;
private final PlayerInfo player;
public CommandMenuItem(String text, ServerCommand command, PlayerInfo player)
{
super(text);
this.command = command;
this.player = player;
}
public ServerCommand getCommand()
{
return command;
}
public PlayerInfo getPlayer()
{
return player;
}
}
public final void setupTablePopup()
{
this.tblPlayers.addMouseListener(new MouseAdapter()
@ -150,7 +293,7 @@ public class BTC_MainPanel extends javax.swing.JFrame
}
if (mouseEvent.isPopupTrigger() && mouseEvent.getComponent() instanceof JTable)
{
final BTC_PlayerListDecoder.PlayerInfo player = getSelectedPlayer();
final PlayerInfo player = getSelectedPlayer();
if (player != null)
{
final JPopupMenu popup = new JPopupMenu(player.getName());
@ -170,11 +313,11 @@ public class BTC_MainPanel extends javax.swing.JFrame
if (_source instanceof CommandMenuItem)
{
final CommandMenuItem source = (CommandMenuItem) _source;
final BTC_PlayerListDecoder.PlayerInfo _player = source.getPlayer();
final PlayerInfo _player = source.getPlayer();
final ServerCommand _command = source.getCommand();
final String output = String.format(_command.getCommandFormat(), _player.getName());
BTC_MainPanel.this.getConnectionManager().sendDelayedCommand(output, true, 100);
BTC_MainPanel.this.connectionManager.sendDelayedCommand(output, true, 100);
}
}
};
@ -193,128 +336,6 @@ public class BTC_MainPanel extends javax.swing.JFrame
});
}
public final void updateTextPane(final String text)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
final StyledDocument styledDocument = mainOutput.getStyledDocument();
try
{
styledDocument.insertString(
styledDocument.getLength(),
text,
StyleContext.getDefaultStyleContext().addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, BTC_FormatHandler.getColor(text))
);
}
catch (BadLocationException ex)
{
throw new RuntimeException(ex);
}
if (BTC_MainPanel.this.chkAutoScroll.isSelected() && BTC_MainPanel.this.mainOutput.getSelectedText() == null)
{
BTC_MainPanel.this.mainOutput.setCaretPosition(styledDocument.getLength() - 1);
}
}
});
}
public BTC_PlayerListDecoder.PlayerInfo getSelectedPlayer()
{
String name = null;
String ip = null;
String displayName = null;
final JTable table = BTC_MainPanel.this.tblPlayers;
final DefaultTableModel model = (DefaultTableModel) table.getModel();
int selectedRow = table.getSelectedRow();
if (selectedRow < 0)
{
return null;
}
for (int col = 0; col <= 2; col++)
{
int modelRow = table.convertRowIndexToModel(selectedRow);
int modelCol = table.convertColumnIndexToModel(col);
String colName = model.getColumnName(modelCol);
Object value = model.getValueAt(modelRow, modelCol);
if (null != colName)
{
switch (colName)
{
case "Name":
{
name = value.toString();
break;
}
case "IP":
{
ip = value.toString();
break;
}
case "Display Name":
{
displayName = value.toString();
break;
}
}
}
}
if (name != null && ip != null & displayName != null)
{
return new BTC_PlayerListDecoder.PlayerInfo(name, ip, displayName);
}
else
{
return null;
}
}
public final void updatePlayerList(final Map<String, BTC_PlayerListDecoder.PlayerInfo> playerList)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
final JTable table = BTC_MainPanel.this.tblPlayers;
final DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
final Iterator<Map.Entry<String, BTC_PlayerListDecoder.PlayerInfo>> it = playerList.entrySet().iterator();
while (it.hasNext())
{
final Map.Entry<String, BTC_PlayerListDecoder.PlayerInfo> entry = it.next();
BTC_PlayerListDecoder.PlayerInfo playerInfo = entry.getValue();
model.addRow(new Object[]
{
playerInfo.getName(),
playerInfo.getDisplayName(),
playerInfo.getIp()
});
}
}
});
}
public void updateConsole()
{
final String data = BukkitTelnetClient.CONSOLE.toString();
BukkitTelnetClient.CONSOLE.reset();
BTC_MainPanel.this.updateTextPane(data);
}
public final void loadServerList()
{
try
@ -322,10 +343,10 @@ public class BTC_MainPanel extends javax.swing.JFrame
serverList.clear();
txtServer.removeAllItems();
File file = new File(SERVERS_FILE_NAME);
final File file = new File(SERVERS_FILE_NAME);
if (file.exists())
{
try (BufferedReader in = new BufferedReader(new FileReader(file)))
try (final BufferedReader in = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = in.readLine()) != null)
@ -345,25 +366,25 @@ public class BTC_MainPanel extends javax.swing.JFrame
public final void saveServersAndTriggerConnect()
{
String selected_server = (String) txtServer.getSelectedItem();
final String selectedServer = (String) txtServer.getSelectedItem();
if (selected_server == null || selected_server.isEmpty())
if (selectedServer == null || selectedServer.isEmpty())
{
System.out.println("Invalid server address.");
writeToConsole("Invalid server address.\n");
return;
}
try
{
if (serverList.contains(selected_server))
if (serverList.contains(selectedServer))
{
serverList.remove(selected_server);
serverList.remove(selectedServer);
}
serverList.addFirst(selected_server);
try (BufferedWriter out = new BufferedWriter(new FileWriter(new File(SERVERS_FILE_NAME))))
serverList.addFirst(selectedServer);
try (final BufferedWriter out = new BufferedWriter(new FileWriter(new File(SERVERS_FILE_NAME))))
{
for (String server : serverList)
for (final String server : serverList)
{
out.write(server + '\n');
}
@ -376,7 +397,7 @@ public class BTC_MainPanel extends javax.swing.JFrame
loadServerList();
connectionManager.triggerConnect(selected_server);
connectionManager.triggerConnect(selectedServer);
}
@SuppressWarnings("unchecked")
@ -762,9 +783,4 @@ public class BTC_MainPanel extends javax.swing.JFrame
{
return chkShowChatOnly;
}
public BTC_ConnectionManager getConnectionManager()
{
return connectionManager;
}
}

View file

@ -5,8 +5,6 @@ import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -62,72 +60,6 @@ public class BTC_PlayerListDecoder
return null;
}
public static final class PlayerInfo
{
private final String name;
private final String ip;
private final String displayName;
public PlayerInfo(String name, String ip, String displayName)
{
this.name = name;
this.ip = ip;
this.displayName = displayName;
}
public String getName()
{
return name;
}
public String getIp()
{
return ip;
}
public String getDisplayName()
{
return displayName;
}
@Override
public int hashCode()
{
return new HashCodeBuilder(17, 31).
append(name).
append(ip).
append(displayName).
toHashCode();
}
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof PlayerInfo))
{
return false;
}
if (obj == this)
{
return true;
}
PlayerInfo rhs = (PlayerInfo) obj;
return new EqualsBuilder().
append(name, rhs.name).
append(ip, rhs.ip).
append(displayName, rhs.displayName).
isEquals();
}
@Override
public String toString()
{
return String.format("%s[Name: %s, Display Name: %s, IP: %s]", this.getClass().toString(), name, displayName, ip);
}
}
public static final class JSONArrayIterable implements Iterable<JSONObject>
{
private final JSONArray array;

View file

@ -1,70 +1,17 @@
package me.StevenLawson.BukkitTelnetClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.commons.io.output.TeeOutputStream;
public class BukkitTelnetClient
{
public static final String VERSION_STRING = "v2.0.3";
public static final Logger LOGGER = Logger.getLogger(BukkitTelnetClient.class.getName());
public static final ByteArrayOutputStream CONSOLE = new ByteArrayOutputStream();
public static BTC_MainPanel mainPanel = null;
public static void main(String args[])
{
final PrintStream guiConsole = new PrintStream(CONSOLE, true)
{
@Override
public void write(byte[] bytes) throws IOException
{
super.write(bytes);
if (mainPanel != null)
{
mainPanel.updateConsole();
}
}
@Override
public void write(int i)
{
super.write(i);
if (mainPanel != null)
{
mainPanel.updateConsole();
}
}
@Override
public void write(byte[] bytes, int i, int i1)
{
super.write(bytes, i, i1);
if (mainPanel != null)
{
mainPanel.updateConsole();
}
}
@Override
public void flush()
{
super.flush();
if (mainPanel != null)
{
mainPanel.updateConsole();
}
}
};
System.setOut(new PrintStream(new TeeOutputStream(System.out, guiConsole)));
System.setErr(new PrintStream(new TeeOutputStream(System.err, guiConsole)));
findAndSetLookAndFeel("Windows");
java.awt.EventQueue.invokeLater(new Runnable()
@ -73,7 +20,7 @@ public class BukkitTelnetClient
public void run()
{
mainPanel = new BTC_MainPanel();
mainPanel.getConnectionManager().updateTitle(false);
mainPanel.setup();
}
});
}

View file

@ -0,0 +1,70 @@
package me.StevenLawson.BukkitTelnetClient;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public final class PlayerInfo
{
private final String name;
private final String ip;
private final String displayName;
public PlayerInfo(String name, String ip, String displayName)
{
this.name = name;
this.ip = ip;
this.displayName = displayName;
}
public String getName()
{
return name;
}
public String getIp()
{
return ip;
}
public String getDisplayName()
{
return displayName;
}
@Override
public int hashCode()
{
return new HashCodeBuilder(17, 31).
append(name).
append(ip).
append(displayName).
toHashCode();
}
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof PlayerInfo))
{
return false;
}
if (obj == this)
{
return true;
}
PlayerInfo rhs = (PlayerInfo) obj;
return new EqualsBuilder().
append(name, rhs.name).
append(ip, rhs.ip).
append(displayName, rhs.displayName).
isEquals();
}
@Override
public String toString()
{
return String.format("%s[Name: %s, Display Name: %s, IP: %s]", this.getClass().toString(), name, displayName, ip);
}
}