From 6a6cfaf439997c33ee495a128041cf888d7ae4c9 Mon Sep 17 00:00:00 2001 From: "Samuel P. Bowman" Date: Fri, 13 Jul 2018 13:09:20 -0400 Subject: [PATCH] Added wraparound for the log window and fixed spelling in comments. --- pom.xml | 5 +++ .../ripme/ripper/AbstractRipper.java | 4 +-- .../com/rarchives/ripme/ui/MainWindow.java | 35 +++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 9de7c444..25f842b1 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,11 @@ commons-io 1.3.2 + + org.apache.commons + commons-lang3 + 3.7 + org.apache.httpcomponents httpclient diff --git a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java index 94ac1ec9..eb210895 100644 --- a/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java +++ b/src/main/java/com/rarchives/ripme/ripper/AbstractRipper.java @@ -299,7 +299,7 @@ public abstract class AbstractRipper * Prefix to prepend to the saved filename. * @param subdirectory * Sub-directory of the working directory to save the images to. - * @return True on success, flase on failure. + * @return True on success, false on failure. */ protected boolean addURLToDownload(URL url, String prefix, String subdirectory) { return addURLToDownload(url, prefix, subdirectory, null, null, null); @@ -316,7 +316,7 @@ public abstract class AbstractRipper * URL to download * @param prefix * Text to append to saved filename. - * @return True on success, flase on failure. + * @return True on success, false on failure. */ protected boolean addURLToDownload(URL url, String prefix) { // Use empty subdirectory diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index e0114fea..f25d1320 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -25,6 +25,7 @@ import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; +import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; @@ -50,6 +51,7 @@ import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; +import org.apache.commons.lang3.ArrayUtils; import org.apache.log4j.ConsoleAppender; import org.apache.log4j.FileAppender; import org.apache.log4j.Level; @@ -150,6 +152,26 @@ public final class MainWindow implements Runnable, RipStatusHandler { } } + private static int getAverageFontWidth(JComponent component) { + int[] widths = component.getFontMetrics(component.getFont()).getWidths(); + return Collections.max(Arrays.asList(ArrayUtils.toObject(widths))); + } + + private static void insertWrappedString(JComponent parent, StyledDocument document, String string, SimpleAttributeSet s) + throws BadLocationException { + StringBuilder resultString = new StringBuilder(); + int maxCharsToFit = parent.getWidth() / getAverageFontWidth(parent); + int i = 0; + while(i < string.length()/maxCharsToFit) { + if(i > 0) resultString.append(string.substring(i*maxCharsToFit-2, i*maxCharsToFit)); + resultString.append(string.substring(i*maxCharsToFit, (i+1)*maxCharsToFit-2)); + resultString.append("\\\n"); + i++; + } + resultString.append(string.substring(string.length()-(string.length()%maxCharsToFit))); + resultString.append("\n"); + document.insertString(document.getLength(), resultString.toString(), s); + } private static void addCheckboxListener(JCheckBox checkBox, String configString) { checkBox.addActionListener(arg0 -> { @@ -241,7 +263,14 @@ public final class MainWindow implements Runnable, RipStatusHandler { private void statusWithColor(String text, Color color) { statusLabel.setForeground(color); - statusLabel.setText(text); + statusLabel.setToolTipText(text); + int averageWidth = getAverageFontWidth(statusLabel); + int numCharsToFit = statusLabel.getWidth() / averageWidth; + if(text.length() > (mainFrame.getWidth() / averageWidth)) { + statusLabel.setText(text.substring(0, numCharsToFit-6) + "..."); + } else { + statusLabel.setText(text); + } pack(); } @@ -308,6 +337,7 @@ public final class MainWindow implements Runnable, RipStatusHandler { gbc.weightx = 1; statusLabel = new JLabel(rb.getString("inactive")); + statusLabel.setToolTipText(rb.getString("inactive")); statusLabel.setHorizontalAlignment(JLabel.CENTER); openButton = new JButton(); openButton.setVisible(false); @@ -353,6 +383,7 @@ public final class MainWindow implements Runnable, RipStatusHandler { logPanel.setBorder(emptyBorder); logText = new JTextPane(); logText.setEditable(false); + logText.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); JScrollPane logTextScroll = new JScrollPane(logText); logTextScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); logPanel.setVisible(false); @@ -1012,7 +1043,7 @@ public final class MainWindow implements Runnable, RipStatusHandler { StyledDocument sd = logText.getStyledDocument(); try { synchronized (this) { - sd.insertString(sd.getLength(), text + "\n", sas); + insertWrappedString(logText, sd, text, sas); } } catch (BadLocationException e) { }