diff --git a/bin/.gitignore b/bin/.gitignore index a876348..057c8e6 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,2 +1,3 @@ /main/ +/icons/ /util/ diff --git a/src/icons/icon-more-20.png b/src/icons/icon-more-20.png new file mode 100644 index 0000000..6d34b6c Binary files /dev/null and b/src/icons/icon-more-20.png differ diff --git a/src/main/MainWindow.java b/src/main/MainWindow.java index f0d90d4..c2f6399 100644 --- a/src/main/MainWindow.java +++ b/src/main/MainWindow.java @@ -22,6 +22,7 @@ import javax.swing.Box; import javax.swing.BoxLayout; +import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollBar; @@ -34,7 +35,11 @@ import util.MCLogFile; import util.MCLogLine; import util.OSFileSystem; -import java.awt.Checkbox; +import javax.swing.JMenu; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.SwingConstants; /** * @@ -50,20 +55,21 @@ public class MainWindow extends JFrame { private JTextArea outputTextField; private JScrollPane scrollPaneBottom; private JTextArea statusTextField; - private Checkbox onlyChatCheckBox; - private Button defaultButton; - private Button addFoldersButton; private JPanel panel_north; - private Component horizontalGlue; - private Component horizontalGlue_1; - private Component horizontalGlue_2; private TreeSet additionalFolderPaths = new TreeSet<>(); private LogRecords logRecords; private int tmpStauslength = 0; private JPanel panel_south; - private Component horizontalGlue_3; + private JMenuBar menuBar; + private JMenu mnOptionsMenu; + private JMenuItem addFoldersButtonMenuItem; + private JCheckBoxMenuItem onlyChatCheckBoxMenu; + private Component horizontalGlue_1; + private Button defaultButton; + private Component horizontalGlue_2; + private JCheckBoxMenuItem stripColorCodesCheckBoxMenu; /** * Create the frame. @@ -86,34 +92,34 @@ public MainWindow() { contentPane.add(panel_north, BorderLayout.NORTH); panel_north.setLayout(new BoxLayout(panel_north, BoxLayout.X_AXIS)); - horizontalGlue = Box.createHorizontalGlue(); - panel_north.add(horizontalGlue); + horizontalGlue_2 = Box.createHorizontalGlue(); + panel_north.add(horizontalGlue_2); defaultButton = new Button("Start analyzing currently known .minecraft folders"); - defaultButton.setBackground(new Color(240, 248, 255)); defaultButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { + public void actionPerformed(ActionEvent e) { startAnalysis(); } }); - panel_north.add(defaultButton); defaultButton.setForeground(Color.BLUE); defaultButton.setFont(new Font("Consolas", Font.PLAIN, 14)); - - horizontalGlue_3 = Box.createHorizontalGlue(); - panel_north.add(horizontalGlue_3); + defaultButton.setBackground(new Color(240, 248, 255)); + panel_north.add(defaultButton); - onlyChatCheckBox = new Checkbox("Only filter chat lines"); - onlyChatCheckBox.setState(true); - onlyChatCheckBox.setFont(new Font("Consolas", Font.PLAIN, 14)); - panel_north.add(onlyChatCheckBox); + menuBar = new JMenuBar(); + menuBar.setBorderPainted(false); + panel_north.add(menuBar); - horizontalGlue_2 = Box.createHorizontalGlue(); - panel_north.add(horizontalGlue_2); + mnOptionsMenu = new JMenu(""); + mnOptionsMenu.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/icon-more-20.png"))); + mnOptionsMenu.setSize(20, 20); + mnOptionsMenu.setHorizontalAlignment(SwingConstants.RIGHT); + mnOptionsMenu.setFont(new Font("Consolas", Font.PLAIN, 14)); + menuBar.add(mnOptionsMenu); - addFoldersButton = new Button("Add custom .minecraft folder locations"); - addFoldersButton.addActionListener(new ActionListener() { + addFoldersButtonMenuItem = new JMenuItem("Add custom .minecraft folder locations"); + addFoldersButtonMenuItem.setFont(new Font("Consolas", Font.PLAIN, 14)); + addFoldersButtonMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { EventQueue.invokeLater(new Runnable() { @@ -129,10 +135,17 @@ public void run() { }); } }); - addFoldersButton.setBackground(new Color(255, 240, 245)); - panel_north.add(addFoldersButton); - addFoldersButton.setForeground(new Color(153, 0, 102)); - addFoldersButton.setFont(new Font("Consolas", Font.PLAIN, 14)); + mnOptionsMenu.add(addFoldersButtonMenuItem); + + stripColorCodesCheckBoxMenu = new JCheckBoxMenuItem("Strip color codes"); + stripColorCodesCheckBoxMenu.setSelected(true); + stripColorCodesCheckBoxMenu.setFont(new Font("Consolas", Font.PLAIN, 14)); + mnOptionsMenu.add(stripColorCodesCheckBoxMenu); + + onlyChatCheckBoxMenu = new JCheckBoxMenuItem("Only filter chat lines"); + onlyChatCheckBoxMenu.setFont(new Font("Consolas", Font.PLAIN, 14)); + onlyChatCheckBoxMenu.setSelected(true); + mnOptionsMenu.add(onlyChatCheckBoxMenu); horizontalGlue_1 = Box.createHorizontalGlue(); panel_north.add(horizontalGlue_1); @@ -174,7 +187,19 @@ public void run() { scrollPaneBottom.setViewportView(statusTextField); } - private synchronized void analyze(boolean onlyChat) { + private void startAnalysis() { + defaultButton.setEnabled(false); + addFoldersButtonMenuItem.setEnabled(false); + Thread t0 = new Thread() { + @Override + public void run() { + mainWindow.analyze(onlyChatCheckBoxMenu.getState(), stripColorCodesCheckBoxMenu.getState()); + } + }; + t0.start(); + } + + private synchronized void analyze(boolean onlyChat, boolean stripColorCodes) { try { OSFileSystem fileSystem = new OSFileSystem(mainWindow); ArrayList minecraftLogFolders = fileSystem.lookForMinecraftLogFolders(); @@ -215,7 +240,7 @@ public int compare(File f1, File f2) { addStatusTemporaryly( "INFO: Loading " + fileCount + " files - " + (counter * 100 / fileCount) + "%"); try { - minecraftLogFile = new MCLogFile(logFile, getPreviousFileInFolder(counter, allFiles), onlyChat); + minecraftLogFile = new MCLogFile(logFile, getPreviousFileInFolder(counter, allFiles), onlyChat, stripColorCodes); if (minecraftLogFile.getPlayerName() != null) { lastLoginName = minecraftLogFile.getPlayerName(); playerNames.put(lastLoginName, playerNames.getOrDefault(lastLoginName, 0) + 1); @@ -251,7 +276,7 @@ public int compare(File f1, File f2) { setOutput(sb.toString()); addStatus("INFO: Found " + logRecords.size() + " results!"); defaultButton.setEnabled(true); - addFoldersButton.setEnabled(true); + addFoldersButtonMenuItem.setEnabled(true); } catch (Exception e) { addStatus("ERROR: " + e.toString()); StringBuilder sb = new StringBuilder(); @@ -281,18 +306,6 @@ public void run() { }); } - private void startAnalysis() { - defaultButton.setEnabled(false); - addFoldersButton.setEnabled(false); - Thread t0 = new Thread() { - @Override - public void run() { - mainWindow.analyze(onlyChatCheckBox.getState()); - } - }; - t0.start(); - } - /** * Adds a new line of text to the outputTextField * diff --git a/src/util/MCLogFile.java b/src/util/MCLogFile.java index edd848c..cd76fe1 100644 --- a/src/util/MCLogFile.java +++ b/src/util/MCLogFile.java @@ -16,19 +16,22 @@ public class MCLogFile { private static String tmpPlayerName; // needs to be static. Streams are weird... + private boolean stripColorCodes; private long creationTime; private Long startingTimeOfFile; private String playerName; private Stream linesStream; - public MCLogFile(File logFile, File previousLogFile, boolean onlyChat) throws FileNotFoundException, IOException { + public MCLogFile(File logFile, File previousLogFile, boolean onlyChat, boolean stripColorCodes) + throws FileNotFoundException, IOException { + this.stripColorCodes = stripColorCodes; creationTime = previousLogFile != null ? getCreationTime(previousLogFile) : getCreationTime(logFile) - 21600000; startingTimeOfFile = null; InputStream inputStream = new FileInputStream(logFile); if (logFile.getName().endsWith(".gz")) inputStream = new GZIPInputStream(inputStream); @SuppressWarnings("resource") - BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); + BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "ISO-8859-1")); final String userSettingLine = "\\[[0-9:]{8}\\] \\[Client thread/INFO\\]: Setting user: "; linesStream = br.lines().filter(a -> { if (tmpPlayerName == null && a.matches(userSettingLine + ".*")) @@ -53,7 +56,8 @@ public String getPlayerName() { public List filterLines(String logLineFilterRegex, String lastPlayerName) { List filteredlogLines = linesStream .map(a -> new MCLogLine(getTime(creationTime, a.substring(1, 9)), lastPlayerName, - a.replaceAll("\\[[0-9:]{8}\\] \\[Client thread/INFO\\]: \\[CHAT\\] ", "").trim())) + a.replaceAll("\\[[0-9:]{8}\\] \\[Client thread/INFO\\]: \\[CHAT\\] ", "").trim(), + stripColorCodes)) .filter(a -> a.getText().matches(logLineFilterRegex)).collect(Collectors.toList()); linesStream.close(); return filteredlogLines; diff --git a/src/util/MCLogLine.java b/src/util/MCLogLine.java index a917012..67e67ab 100644 --- a/src/util/MCLogLine.java +++ b/src/util/MCLogLine.java @@ -9,10 +9,10 @@ public class MCLogLine implements Comparable { private String playerName; private String text; - public MCLogLine(long creationTime, String playerName, String text) { + public MCLogLine(long creationTime, String playerName, String text, boolean stripColorCodes) { this.creationTime = creationTime; this.playerName = playerName; - this.text = text; + this.text = stripColorCodes ? text.replaceAll("§.", "") : text; } public long getCreationTime() {