From 4ecdefc14ea68628ea9e70cbc8ef083509a9cdb0 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Sun, 2 Jul 2017 16:24:50 +0800 Subject: [PATCH] Added searching log texts. Closes #69 --- .../java/org/jackhuang/hmcl/ui/LogWindow.form | 48 +++++-- .../java/org/jackhuang/hmcl/ui/LogWindow.java | 124 ++++++++++++++---- .../org/jackhuang/hmcl/util/log/Parser.java | 26 ---- .../org/jackhuang/hmcl/lang/I18N.lang | 2 + .../org/jackhuang/hmcl/lang/I18N.properties | 2 + .../org/jackhuang/hmcl/lang/I18N_vi.lang | 2 + .../jackhuang/hmcl/lang/I18N_vi.properties | 2 + .../org/jackhuang/hmcl/lang/I18N_zh.lang | 2 + .../jackhuang/hmcl/lang/I18N_zh.properties | 2 + .../org/jackhuang/hmcl/lang/I18N_zh_CN.lang | 2 + .../jackhuang/hmcl/lang/I18N_zh_CN.properties | 2 + 11 files changed, 148 insertions(+), 66 deletions(-) delete mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/util/log/Parser.java diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.form b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.form index 2291ba0f5..e79c27e5c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.form +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.form @@ -34,7 +34,7 @@ - + @@ -45,10 +45,14 @@ - + - + + + + + @@ -70,13 +74,15 @@ - + + + @@ -161,15 +167,20 @@ + + + - + - + + + @@ -192,27 +203,42 @@ - + - + - + - + - + + + + + + + + + + + + + + + + diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java index 810de1071..430807187 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LogWindow.java @@ -21,9 +21,11 @@ import java.io.PrintStream; import java.util.Deque; import java.util.LinkedList; import javax.swing.SwingUtilities; +import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; +import javax.swing.text.StyledDocument; import org.jackhuang.hmcl.util.C; import org.jackhuang.hmcl.api.HMCLog; import org.jackhuang.hmcl.util.log.Level; @@ -39,8 +41,9 @@ import org.jackhuang.hmcl.util.ui.SwingUtils; * @author huangyuhui */ public class LogWindow extends javax.swing.JFrame { + public static LogWindowOutputStream outputStream; - + NonFunction listener; /** @@ -56,8 +59,9 @@ public class LogWindow extends javax.swing.JFrame { SwingUtilities.invokeLater(() -> { setLocationRelativeTo(null); - txtLog.setEditable(false); + //txtLog.setEditable(false); txtLog.setFont(Settings.getInstance().getConsoleFont()); + cboShowLines.setSelectedItem(String.valueOf(Settings.getInstance().getLogLines())); }); } @@ -80,13 +84,15 @@ public class LogWindow extends javax.swing.JFrame { btnTerminateGame = new javax.swing.JButton(); pnlLog = new javax.swing.JScrollPane(); txtLog = new JLineWrapTextPane(); - jLabel1 = new javax.swing.JLabel(); + lblShowLines = new javax.swing.JLabel(); cboShowLines = new javax.swing.JComboBox<>(); btnDebug = new javax.swing.JToggleButton(); btnInfo = new javax.swing.JToggleButton(); btnWarn = new javax.swing.JToggleButton(); btnError = new javax.swing.JButton(); btnFatal = new javax.swing.JButton(); + txtSearch = new javax.swing.JTextField(); + lblSearch = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle(C.i18n("logwindow.title")); // NOI18N @@ -133,10 +139,11 @@ public class LogWindow extends javax.swing.JFrame { } }); + txtLog.setEditable(false); pnlLog.setViewportView(txtLog); - jLabel1.setText("显示行数"); - jLabel1.setToolTipText(""); + lblShowLines.setText(C.i18n("logwindow.show_lines")); // NOI18N + lblShowLines.setToolTipText(""); cboShowLines.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "100", "1000", "5000" })); cboShowLines.addItemListener(new java.awt.event.ItemListener() { @@ -145,15 +152,24 @@ public class LogWindow extends javax.swing.JFrame { } }); - btnDebug.setText("debugs"); + btnDebug.setText("0 debug"); - btnInfo.setText("infos"); + btnInfo.setText("0 info"); - btnWarn.setText("warns"); + btnWarn.setText("0 warn"); - btnError.setText("errors"); + btnError.setText("0 error"); - btnFatal.setText("fatals"); + btnFatal.setText("0 fatal"); + + txtSearch.setToolTipText(""); + txtSearch.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + txtSearchActionPerformed(evt); + } + }); + + lblSearch.setText(C.i18n("logwindow.search")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); @@ -164,7 +180,7 @@ public class LogWindow extends javax.swing.JFrame { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(btnContact) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 183, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 144, Short.MAX_VALUE) .addComponent(btnTerminateGame) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnCopy) @@ -174,10 +190,14 @@ public class LogWindow extends javax.swing.JFrame { .addComponent(btnClose)) .addComponent(lblCrash, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() - .addComponent(jLabel1) + .addComponent(lblShowLines) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cboShowLines, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(lblSearch) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(txtSearch) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnFatal) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnError) @@ -195,13 +215,15 @@ public class LogWindow extends javax.swing.JFrame { .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel1) + .addComponent(lblShowLines) .addComponent(cboShowLines, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnDebug) .addComponent(btnInfo) .addComponent(btnWarn) .addComponent(btnError) - .addComponent(btnFatal)) + .addComponent(btnFatal) + .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(lblSearch)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblCrash) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) @@ -251,10 +273,46 @@ public class LogWindow extends javax.swing.JFrame { Settings.getInstance().setLogLines(Integer.parseInt(cboShowLines.getSelectedItem().toString())); }//GEN-LAST:event_cboShowLinesItemStateChanged + private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed + if (evt.getSource() != txtSearch || txtSearch.getText().isEmpty()) + return; + try { + if (!s.equals(txtSearch.getText())) + pos = 0; + StyledDocument d = txtLog.getStyledDocument(); + s = txtSearch.getText(); + String content = d.getText(0, d.getLength()); + int x = content.indexOf(s, pos); + if (x == -1) { + pos = 0; + x = content.indexOf(s, pos); + } + if (x == -1) + return; + int len = s.length(); + pos = x + len; + SimpleAttributeSet set = new SimpleAttributeSet(d.getLogicalStyle(x).copyAttributes()); + StyleConstants.setBackground(set, txtLog.getSelectionColor()); + StyleConstants.setForeground(set, txtLog.getSelectedTextColor()); + d.setCharacterAttributes(x, len, set, true); + set = new SimpleAttributeSet(d.getLogicalStyle(lastX).copyAttributes()); + StyleConstants.setBackground(set, txtLog.getBackground()); + StyleConstants.setForeground(set, txtLog.getForeground()); + d.setCharacterAttributes(lastX, lastLen, set, true); + txtLog.select(x, pos); + lastX = x; + lastLen = len; + } catch (BadLocationException e) { + } + }//GEN-LAST:event_txtSearchActionPerformed + + String s = ""; + int pos = 0, lastX = 0, lastLen = 0; + void terminateGames() { ProcessMonitor.stopAll(); } - + int removedLength = 0; Deque offsets = new LinkedList<>(); int fatals = 0, errors = 0, warns = 0, infos = 0, debugs = 0; @@ -282,15 +340,25 @@ public class LogWindow extends javax.swing.JFrame { } catch (Exception ex) { HMCLog.err("Failed to insert \"" + newStatus + "\" to " + d.getLength(), ex); } - + switch (c) { - case FATAL: btnFatal.setText(++fatals + " fatals"); break; - case ERROR: btnError.setText(++errors + " errors"); break; - case WARN: btnWarn.setText(++warns + " warns"); break; - case INFO: btnInfo.setText(++infos + " infos"); break; - case DEBUG: btnDebug.setText(++debugs + " debugs"); break; + case FATAL: + btnFatal.setText(++fatals + " fatals"); + break; + case ERROR: + btnError.setText(++errors + " errors"); + break; + case WARN: + btnWarn.setText(++warns + " warns"); + break; + case INFO: + btnInfo.setText(++infos + " infos"); + break; + case DEBUG: + btnDebug.setText(++debugs + " debugs"); + break; } - + int maxLines = Integer.parseInt(cboShowLines.getSelectedItem().toString()); while (offsets.size() > maxLines) { int start = offsets.pollFirst(); @@ -298,7 +366,7 @@ public class LogWindow extends javax.swing.JFrame { try { d.remove(start - removedLength, end - start); // start - removedLength must become 0 removedLength = end; - } catch(Exception ignore) { + } catch (Exception ignore) { } } }); @@ -314,8 +382,6 @@ public class LogWindow extends javax.swing.JFrame { @Override public void setVisible(boolean b) { - cboShowLines.setSelectedItem(Settings.getInstance().getLogLines()); - txtLog.setFont(Settings.getInstance().getConsoleFont()); lblCrash.setVisible(false); btnContact.setVisible(false); @@ -323,8 +389,6 @@ public class LogWindow extends javax.swing.JFrame { } public void showAsCrashWindow(boolean out_date) { - cboShowLines.setSelectedItem(Settings.getInstance().getLogLines()); - txtLog.setFont(Settings.getInstance().getConsoleFont()); if (out_date) { lblCrash.setVisible(false); @@ -351,9 +415,11 @@ public class LogWindow extends javax.swing.JFrame { private javax.swing.JButton btnTerminateGame; private javax.swing.JToggleButton btnWarn; private javax.swing.JComboBox cboShowLines; - private javax.swing.JLabel jLabel1; private javax.swing.JLabel lblCrash; + private javax.swing.JLabel lblSearch; + private javax.swing.JLabel lblShowLines; private javax.swing.JScrollPane pnlLog; private javax.swing.JTextPane txtLog; + private javax.swing.JTextField txtSearch; // End of variables declaration//GEN-END:variables } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/log/Parser.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/log/Parser.java deleted file mode 100644 index 6ba5b2491..000000000 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/log/Parser.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Hello Minecraft! Launcher. - * Copyright (C) 2013 huangyuhui - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see {http://www.gnu.org/licenses/}. - */ -package org.jackhuang.hmcl.util.log; - -/** - * - * @author huang - */ -public class Parser { - -} diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.lang b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.lang index 84a79fc46..c29e8fbee 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.lang +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.lang @@ -392,6 +392,8 @@ update.found=(Found Update!) logwindow.terminate_game=Terminate Game logwindow.title=Log logwindow.contact=Contact Us +logwindow.show_lines=Show Lines +logwindow.search=Search selector.choose=Choose diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.properties b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.properties index e9b21b99a..557394066 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.properties +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N.properties @@ -392,6 +392,8 @@ update.found=(Found Update!) logwindow.terminate_game=Terminate Game logwindow.title=Log logwindow.contact=Contact Us +logwindow.show_lines=Show Lines +logwindow.search=Search selector.choose=Choose diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.lang b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.lang index b8a9af3f6..48c030f97 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.lang +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.lang @@ -390,6 +390,8 @@ update.found=(Đã tìm thấy bản cập nhật!) logwindow.terminate_game=Tắt Game logwindow.title=HMCL Error Log (Hãy đăng cái này lên forum!) logwindow.contact=Contact Us +logwindow.show_lines=Show Lines +logwindow.search=Search selector.choose=Chọn diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.properties b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.properties index 7152ce868..174ce305b 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.properties +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_vi.properties @@ -390,6 +390,8 @@ update.found=(\u0110\u00e3 t\u00ecm th\u1ea5y b\u1ea3n c\u1eadp nh\u1eadt!) logwindow.terminate_game=T\u1eaft Game logwindow.title=HMCL Error Log (H\u00e3y \u0111\u0103ng c\u00e1i n\u00e0y l\u00ean forum!) logwindow.contact=Contact Us +logwindow.show_lines=Show Lines +logwindow.search=Search selector.choose=Ch\u1ecdn diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.lang b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.lang index 158d41a4e..a0c882554 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.lang +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.lang @@ -392,6 +392,8 @@ update.found=(發現更新!) logwindow.terminate_game=結束遊戲進程 logwindow.title=日誌 logwindow.contact=聯繫我們 +logwindow.show_lines=顯示行數 +logwindow.search=查找 selector.choose=選擇 diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.properties b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.properties index dcf78019e..f2619b50d 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.properties +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh.properties @@ -392,6 +392,8 @@ update.found=(\u767c\u73fe\u66f4\u65b0!) logwindow.terminate_game=\u7d50\u675f\u904a\u6232\u9032\u7a0b logwindow.title=\u65e5\u8a8c logwindow.contact=\u806f\u7e6b\u6211\u5011 +logwindow.show_lines=\u986f\u793a\u884c\u6578 +logwindow.search=\u67e5\u627e selector.choose=\u9078\u64c7 diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.lang b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.lang index c041f9d99..dfd9683fe 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.lang +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.lang @@ -392,6 +392,8 @@ update.found=(发现更新!) logwindow.terminate_game=结束游戏进程 logwindow.title=日志 logwindow.contact=联系我们 +logwindow.show_lines=显示行数 +logwindow.search=查找 selector.choose=选择 diff --git a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.properties b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.properties index 12d8fb306..b87763306 100644 --- a/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.properties +++ b/HMCLCore/src/main/resources/org/jackhuang/hmcl/lang/I18N_zh_CN.properties @@ -392,6 +392,8 @@ update.found=(\u53d1\u73b0\u66f4\u65b0!) logwindow.terminate_game=\u7ed3\u675f\u6e38\u620f\u8fdb\u7a0b logwindow.title=\u65e5\u5fd7 logwindow.contact=\u8054\u7cfb\u6211\u4eec +logwindow.show_lines=\u663e\u793a\u884c\u6570 +logwindow.search=\u67e5\u627e selector.choose=\u9009\u62e9