Make expanded window resizable

This commit is contained in:
iqqu 2017-10-25 16:31:02 -04:00
parent 4ae15d4ea6
commit eb68b63999

View File

@ -1,32 +1,14 @@
package com.rarchives.ripme.ui; package com.rarchives.ripme.ui;
import java.awt.CheckboxMenuItem; import java.awt.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType; import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
@ -78,7 +60,7 @@ import org.apache.log4j.Logger;
import com.rarchives.ripme.ripper.AbstractRipper; import com.rarchives.ripme.ripper.AbstractRipper;
import com.rarchives.ripme.utils.RipUtils; import com.rarchives.ripme.utils.RipUtils;
import com.rarchives.ripme.utils.Utils; import com.rarchives.ripme.utils.Utils;
import java.awt.AWTException;
import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UnsupportedLookAndFeelException;
/** /**
@ -154,14 +136,15 @@ public final class MainWindow implements Runnable, RipStatusHandler {
public MainWindow() { public MainWindow() {
mainFrame = new JFrame("RipMe v" + UpdateUtils.getThisJarVersion()); mainFrame = new JFrame("RipMe v" + UpdateUtils.getThisJarVersion());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setResizable(false);
mainFrame.setLayout(new GridBagLayout()); mainFrame.setLayout(new GridBagLayout());
createUI(mainFrame.getContentPane()); createUI(mainFrame.getContentPane());
pack();
loadHistory(); loadHistory();
setupHandlers(); setupHandlers();
Thread shutdownThread = new Thread(() -> shutdownCleanup()); Thread shutdownThread = new Thread(this::shutdownCleanup);
Runtime.getRuntime().addShutdownHook(shutdownThread); Runtime.getRuntime().addShutdownHook(shutdownThread);
if (Utils.getConfigBoolean("auto.update", true)) { if (Utils.getConfigBoolean("auto.update", true)) {
@ -224,7 +207,26 @@ public final class MainWindow implements Runnable, RipStatusHandler {
} }
private void pack() { private void pack() {
SwingUtilities.invokeLater(() -> mainFrame.pack()); SwingUtilities.invokeLater(() -> {
boolean collapsed = isCollapsed();
if (!collapsed) {
mainFrame.setResizable(true);
}
Dimension preferredSize = mainFrame.getPreferredSize();
mainFrame.setMinimumSize(preferredSize);
if (collapsed) {
mainFrame.setResizable(false);
mainFrame.setSize(preferredSize);
}
});
}
private boolean isCollapsed() {
return (!logPanel.isVisible() &&
!historyPanel.isVisible() &&
!queuePanel.isVisible() &&
!configurationPanel.isVisible()
);
} }
private void createUI(Container pane) { private void createUI(Container pane) {
@ -235,9 +237,10 @@ public final class MainWindow implements Runnable, RipStatusHandler {
EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5); EmptyBorder emptyBorder = new EmptyBorder(5, 5, 5, 5);
GridBagConstraints gbc = new GridBagConstraints(); GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH; gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 2; gbc.ipadx = 2; gbc.gridx = 0; gbc.weightx = 1; gbc.ipadx = 2; gbc.gridx = 0;
gbc.weighty = 2; gbc.ipady = 2; gbc.gridy = 0; gbc.weighty = 0; gbc.ipady = 2; gbc.gridy = 0;
gbc.anchor = GridBagConstraints.PAGE_START;
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -258,11 +261,15 @@ public final class MainWindow implements Runnable, RipStatusHandler {
JPanel ripPanel = new JPanel(new GridBagLayout()); JPanel ripPanel = new JPanel(new GridBagLayout());
ripPanel.setBorder(emptyBorder); ripPanel.setBorder(emptyBorder);
gbc.weightx = 0;
gbc.gridx = 0; ripPanel.add(new JLabel("URL:", JLabel.RIGHT), gbc); gbc.gridx = 0; ripPanel.add(new JLabel("URL:", JLabel.RIGHT), gbc);
gbc.weightx = 1;
gbc.gridx = 1; ripPanel.add(ripTextfield, gbc); gbc.gridx = 1; ripPanel.add(ripTextfield, gbc);
gbc.weightx = 0;
gbc.gridx = 2; ripPanel.add(ripButton, gbc); gbc.gridx = 2; ripPanel.add(ripButton, gbc);
gbc.gridx = 3; ripPanel.add(stopButton, gbc); gbc.gridx = 3; ripPanel.add(stopButton, gbc);
gbc.fill = GridBagConstraints.BOTH; gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
statusLabel = new JLabel("Inactive"); statusLabel = new JLabel("Inactive");
statusLabel.setHorizontalAlignment(JLabel.CENTER); statusLabel.setHorizontalAlignment(JLabel.CENTER);
@ -286,6 +293,10 @@ public final class MainWindow implements Runnable, RipStatusHandler {
optionHistory = new JButton("History"); optionHistory = new JButton("History");
optionQueue = new JButton("Queue"); optionQueue = new JButton("Queue");
optionConfiguration = new JButton("Configuration"); optionConfiguration = new JButton("Configuration");
optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
try { try {
Image icon; Image icon;
icon = ImageIO.read(getClass().getClassLoader().getResource("comment.png")); icon = ImageIO.read(getClass().getClassLoader().getResource("comment.png"));
@ -304,11 +315,17 @@ public final class MainWindow implements Runnable, RipStatusHandler {
logPanel = new JPanel(new GridBagLayout()); logPanel = new JPanel(new GridBagLayout());
logPanel.setBorder(emptyBorder); logPanel.setBorder(emptyBorder);
logText = new JTextPaneNoWrap(); logText = new JTextPane();
logText.setEditable(false);
JScrollPane logTextScroll = new JScrollPane(logText); JScrollPane logTextScroll = new JScrollPane(logText);
logTextScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
logPanel.setVisible(false); logPanel.setVisible(false);
logPanel.setPreferredSize(new Dimension(300, 250)); logPanel.setPreferredSize(new Dimension(300, 250));
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
logPanel.add(logTextScroll, gbc); logPanel.add(logTextScroll, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 0;
historyPanel = new JPanel(new GridBagLayout()); historyPanel = new JPanel(new GridBagLayout());
historyPanel.setBorder(emptyBorder); historyPanel.setBorder(emptyBorder);
@ -373,8 +390,11 @@ public final class MainWindow implements Runnable, RipStatusHandler {
gbc.gridx = 0; gbc.gridx = 0;
// History List Panel // History List Panel
JPanel historyTablePanel = new JPanel(new GridBagLayout()); JPanel historyTablePanel = new JPanel(new GridBagLayout());
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
historyTablePanel.add(historyTableScrollPane, gbc); historyTablePanel.add(historyTableScrollPane, gbc);
gbc.ipady = 180; gbc.ipady = 180;
gbc.gridy = 0;
historyPanel.add(historyTablePanel, gbc); historyPanel.add(historyTablePanel, gbc);
gbc.ipady = 0; gbc.ipady = 0;
JPanel historyButtonPanel = new JPanel(new GridBagLayout()); JPanel historyButtonPanel = new JPanel(new GridBagLayout());
@ -384,6 +404,8 @@ public final class MainWindow implements Runnable, RipStatusHandler {
gbc.gridx = 1; historyButtonPanel.add(historyButtonClear, gbc); gbc.gridx = 1; historyButtonPanel.add(historyButtonClear, gbc);
gbc.gridx = 2; historyButtonPanel.add(historyButtonRerip, gbc); gbc.gridx = 2; historyButtonPanel.add(historyButtonRerip, gbc);
gbc.gridy = 1; gbc.gridx = 0; gbc.gridy = 1; gbc.gridx = 0;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
historyPanel.add(historyButtonPanel, gbc); historyPanel.add(historyButtonPanel, gbc);
queuePanel = new JPanel(new GridBagLayout()); queuePanel = new JPanel(new GridBagLayout());
@ -407,14 +429,17 @@ public final class MainWindow implements Runnable, RipStatusHandler {
} }
gbc.gridx = 0; gbc.gridx = 0;
JPanel queueListPanel = new JPanel(new GridBagLayout()); JPanel queueListPanel = new JPanel(new GridBagLayout());
gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 1;
queueListPanel.add(queueListScroll, gbc); queueListPanel.add(queueListScroll, gbc);
queuePanel.add(queueListPanel, gbc); queuePanel.add(queueListPanel, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 0;
gbc.ipady = 0; gbc.ipady = 0;
configurationPanel = new JPanel(new GridBagLayout()); configurationPanel = new JPanel(new GridBagLayout());
configurationPanel.setBorder(emptyBorder); configurationPanel.setBorder(emptyBorder);
configurationPanel.setVisible(false); configurationPanel.setVisible(false);
configurationPanel.setPreferredSize(new Dimension(300, 250));
// TODO Configuration components // TODO Configuration components
configUpdateButton = new JButton("Check for updates"); configUpdateButton = new JButton("Check for updates");
configUpdateLabel = new JLabel("Current version: " + UpdateUtils.getThisJarVersion(), JLabel.RIGHT); configUpdateLabel = new JLabel("Current version: " + UpdateUtils.getThisJarVersion(), JLabel.RIGHT);
@ -497,14 +522,19 @@ public final class MainWindow implements Runnable, RipStatusHandler {
gbc.gridy = 11; gbc.gridx = 0; configurationPanel.add(configSaveDirLabel, gbc); gbc.gridy = 11; gbc.gridx = 0; configurationPanel.add(configSaveDirLabel, gbc);
gbc.gridx = 1; configurationPanel.add(configSaveDirButton, gbc); gbc.gridx = 1; configurationPanel.add(configSaveDirButton, gbc);
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.gridy = 0; pane.add(ripPanel, gbc); gbc.gridy = 0; pane.add(ripPanel, gbc);
gbc.gridy = 1; pane.add(statusPanel, gbc); gbc.gridy = 1; pane.add(statusPanel, gbc);
gbc.gridy = 2; pane.add(progressPanel, gbc); gbc.gridy = 2; pane.add(progressPanel, gbc);
gbc.gridy = 3; pane.add(optionsPanel, gbc); gbc.gridy = 3; pane.add(optionsPanel, gbc);
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 4; pane.add(logPanel, gbc); gbc.gridy = 4; pane.add(logPanel, gbc);
gbc.gridy = 5; pane.add(historyPanel, gbc); gbc.gridy = 5; pane.add(historyPanel, gbc);
gbc.gridy = 5; pane.add(queuePanel, gbc); gbc.gridy = 5; pane.add(queuePanel, gbc);
gbc.gridy = 5; pane.add(configurationPanel, gbc); gbc.gridy = 5; pane.add(configurationPanel, gbc);
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
} }
private void setupHandlers() { private void setupHandlers() {
@ -536,7 +566,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
AbstractRipper ripper = AbstractRipper.getRipper(url); AbstractRipper ripper = AbstractRipper.getRipper(url);
statusWithColor(ripper.getHost() + " album detected", Color.GREEN); statusWithColor(ripper.getHost() + " album detected", Color.GREEN);
} catch (Exception e) { } catch (Exception e) {
statusWithColor("Can't rip this URL: "+e.getMessage(), Color.RED); statusWithColor("Can't rip this URL: " + e.getMessage(), Color.RED);
} }
} }
}); });
@ -558,7 +588,11 @@ public final class MainWindow implements Runnable, RipStatusHandler {
historyPanel.setVisible(false); historyPanel.setVisible(false);
queuePanel.setVisible(false); queuePanel.setVisible(false);
configurationPanel.setVisible(false); configurationPanel.setVisible(false);
if (logPanel.isVisible()) {
optionLog.setFont(optionLog.getFont().deriveFont(Font.BOLD)); optionLog.setFont(optionLog.getFont().deriveFont(Font.BOLD));
} else {
optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
}
optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
@ -570,7 +604,11 @@ public final class MainWindow implements Runnable, RipStatusHandler {
queuePanel.setVisible(false); queuePanel.setVisible(false);
configurationPanel.setVisible(false); configurationPanel.setVisible(false);
optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
if (historyPanel.isVisible()) {
optionHistory.setFont(optionLog.getFont().deriveFont(Font.BOLD)); optionHistory.setFont(optionLog.getFont().deriveFont(Font.BOLD));
} else {
optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
}
optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
pack(); pack();
@ -582,7 +620,11 @@ public final class MainWindow implements Runnable, RipStatusHandler {
configurationPanel.setVisible(false); configurationPanel.setVisible(false);
optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
if (queuePanel.isVisible()) {
optionQueue.setFont(optionLog.getFont().deriveFont(Font.BOLD)); optionQueue.setFont(optionLog.getFont().deriveFont(Font.BOLD));
} else {
optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
}
optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
pack(); pack();
}); });
@ -594,7 +636,11 @@ public final class MainWindow implements Runnable, RipStatusHandler {
optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionLog.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionHistory.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN)); optionQueue.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
if (configurationPanel.isVisible()) {
optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.BOLD)); optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.BOLD));
} else {
optionConfiguration.setFont(optionLog.getFont().deriveFont(Font.PLAIN));
}
pack(); pack();
}); });
historyButtonRemove.addActionListener(event -> { historyButtonRemove.addActionListener(event -> {
@ -869,8 +915,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
mainFrame.setAlwaysOnTop(true); mainFrame.setAlwaysOnTop(true);
mainFrame.setAlwaysOnTop(false); mainFrame.setAlwaysOnTop(false);
trayMenuMain.setLabel("Hide"); trayMenuMain.setLabel("Hide");
} } else {
else {
mainFrame.setVisible(false); mainFrame.setVisible(false);
trayMenuMain.setLabel("Show"); trayMenuMain.setLabel("Show");
} }
@ -906,8 +951,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
"RipMe - history load failure", "RipMe - history load failure",
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
} }
} } else {
else {
logger.info("Loading history from configuration"); logger.info("Loading history from configuration");
HISTORY.fromList(Utils.getConfigList("download.history")); HISTORY.fromList(Utils.getConfigList("download.history"));
if (HISTORY.toList().size() == 0) { if (HISTORY.toList().size() == 0) {
@ -956,8 +1000,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
String nextAlbum = (String) queueListModel.remove(0); String nextAlbum = (String) queueListModel.remove(0);
if (queueListModel.isEmpty()) { if (queueListModel.isEmpty()) {
optionQueue.setText("Queue"); optionQueue.setText("Queue");
} } else {
else {
optionQueue.setText("Queue (" + queueListModel.size() + ")"); optionQueue.setText("Queue (" + queueListModel.size() + ")");
} }
Thread t = ripAlbum(nextAlbum); Thread t = ripAlbum(nextAlbum);
@ -968,8 +1011,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
logger.error("Interrupted while waiting to rip next album", ie); logger.error("Interrupted while waiting to rip next album", ie);
} }
ripNextAlbum(); ripNextAlbum();
} } else {
else {
t.start(); t.start();
} }
} }
@ -1038,8 +1080,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
if (!queueListModel.contains(ripTextfield.getText()) && !ripTextfield.getText().equals("")) { if (!queueListModel.contains(ripTextfield.getText()) && !ripTextfield.getText().equals("")) {
queueListModel.add(queueListModel.size(), ripTextfield.getText()); queueListModel.add(queueListModel.size(), ripTextfield.getText());
ripTextfield.setText(""); ripTextfield.setText("");
} } else {
else {
if (!isRipping) { if (!isRipping) {
ripNextAlbum(); ripNextAlbum();
} }
@ -1076,11 +1117,13 @@ public final class MainWindow implements Runnable, RipStatusHandler {
case LOADING_RESOURCE: case LOADING_RESOURCE:
case DOWNLOAD_STARTED: case DOWNLOAD_STARTED:
if (logger.isEnabledFor(Level.INFO)) { if (logger.isEnabledFor(Level.INFO)) {
appendLog( "Downloading " + msg.getObject(), Color.BLACK); appendLog("Downloading " + msg.getObject(), Color.BLACK);
} }
break; break;
case DOWNLOAD_COMPLETE: case DOWNLOAD_COMPLETE:
appendLog( "Downloaded " + msg.getObject(), Color.GREEN); if (logger.isEnabledFor(Level.INFO)) {
appendLog("Downloaded " + msg.getObject(), Color.GREEN);
}
break; break;
case DOWNLOAD_ERRORED: case DOWNLOAD_ERRORED:
if (logger.isEnabledFor(Level.ERROR)) { if (logger.isEnabledFor(Level.ERROR)) {
@ -1088,7 +1131,9 @@ public final class MainWindow implements Runnable, RipStatusHandler {
} }
break; break;
case DOWNLOAD_WARN: case DOWNLOAD_WARN:
if (logger.isEnabledFor(Level.WARN)) {
appendLog((String) msg.getObject(), Color.ORANGE); appendLog((String) msg.getObject(), Color.ORANGE);
}
break; break;
case RIP_ERRORED: case RIP_ERRORED:
@ -1111,8 +1156,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
HistoryEntry entry = HISTORY.getEntryByURL(url); HistoryEntry entry = HISTORY.getEntryByURL(url);
entry.count = rsc.count; entry.count = rsc.count;
entry.modifiedDate = new Date(); entry.modifiedDate = new Date();
} } else {
else {
HistoryEntry entry = new HistoryEntry(); HistoryEntry entry = new HistoryEntry();
entry.url = url; entry.url = url;
entry.dir = rsc.getDir(); entry.dir = rsc.getDir();
@ -1139,7 +1183,7 @@ public final class MainWindow implements Runnable, RipStatusHandler {
Image folderIcon = ImageIO.read(getClass().getClassLoader().getResource("folder.png")); Image folderIcon = ImageIO.read(getClass().getClassLoader().getResource("folder.png"));
openButton.setIcon(new ImageIcon(folderIcon)); openButton.setIcon(new ImageIcon(folderIcon));
} catch (Exception e) { } } catch (Exception e) { }
appendLog( "Rip complete, saved to " + f.getAbsolutePath(), Color.GREEN); appendLog("Rip complete, saved to " + f.getAbsolutePath(), Color.GREEN);
openButton.setActionCommand(f.toString()); openButton.setActionCommand(f.toString());
openButton.addActionListener(event -> { openButton.addActionListener(event -> {
try { try {
@ -1165,16 +1209,6 @@ public final class MainWindow implements Runnable, RipStatusHandler {
SwingUtilities.invokeLater(event); SwingUtilities.invokeLater(event);
} }
/** Simple TextPane that allows horizontal scrolling. */
class JTextPaneNoWrap extends JTextPane {
private static final long serialVersionUID = 1L;
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
}
public static void ripAlbumStatic(String url) { public static void ripAlbumStatic(String url) {
ripTextfield.setText(url.trim()); ripTextfield.setText(url.trim());
ripButton.doClick(); ripButton.doClick();