1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-04 08:25:03 -05:00
filebot/source/net/filebot/ui/MainFrame.java

279 lines
8.2 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.ui;
2013-01-19 01:50:48 -05:00
import static java.awt.event.InputEvent.*;
import static java.awt.event.KeyEvent.*;
import static javax.swing.KeyStroke.*;
import static javax.swing.ScrollPaneConstants.*;
2015-03-24 08:39:05 -04:00
import static net.filebot.Settings.*;
2014-04-19 02:30:29 -04:00
import static net.filebot.ui.NotificationLogging.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog.ModalExclusionType;
import java.awt.FlowLayout;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
2013-01-19 01:50:48 -05:00
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
2013-01-19 01:50:48 -05:00
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
2014-04-19 02:30:29 -04:00
import net.filebot.Settings;
import net.filebot.cli.GroovyPad;
2014-08-05 07:55:28 -04:00
import net.filebot.mac.MacAppUtilities;
2014-04-19 02:30:29 -04:00
import net.filebot.ui.analyze.AnalyzePanelBuilder;
import net.filebot.ui.episodelist.EpisodeListPanelBuilder;
import net.filebot.ui.list.ListPanelBuilder;
import net.filebot.ui.rename.RenamePanelBuilder;
import net.filebot.ui.sfv.SfvPanelBuilder;
import net.filebot.ui.subtitle.SubtitlePanelBuilder;
import net.filebot.util.PreferencesMap.PreferencesEntry;
import net.filebot.util.ui.DefaultFancyListCellRenderer;
import net.filebot.util.ui.ShadowBorder;
2014-07-29 02:45:15 -04:00
import net.filebot.util.ui.SwingUI;
2014-07-24 07:31:24 -04:00
import net.miginfocom.swing.MigLayout;
import net.sf.ehcache.CacheManager;
public class MainFrame extends JFrame {
private JList selectionList = new PanelSelectionList(createPanelBuilders());
private HeaderPanel headerPanel = new HeaderPanel();
2013-12-30 02:37:01 -05:00
private static final PreferencesEntry<String> persistentSelectedPanel = Settings.forPackage(MainFrame.class).entry("panel.selected").defaultValue("0");
public MainFrame() {
super(Settings.isInstalled() ? Settings.getApplicationName() : Settings.getApplicationIdentifier());
try {
// restore selected panel
selectionList.setSelectedIndex(Integer.parseInt(persistentSelectedPanel.getValue()));
} catch (NumberFormatException e) {
// ignore
}
JScrollPane selectionListScrollPane = new JScrollPane(selectionList, VERTICAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_NEVER);
selectionListScrollPane.setOpaque(false);
2015-03-24 08:39:05 -04:00
if (isMacApp()) {
selectionListScrollPane.setBorder(new CompoundBorder(new ShadowBorder(), new LineBorder(new Color(0x809DB8), 1, false)));
} else {
selectionListScrollPane.setBorder(new CompoundBorder(new ShadowBorder(), selectionListScrollPane.getBorder()));
}
headerPanel.getTitleLabel().setBorder(new EmptyBorder(8, 90, 10, 0));
JComponent c = (JComponent) getContentPane();
2009-03-13 21:01:01 -04:00
c.setLayout(new MigLayout("insets 0, fill, hidemode 3", "95px[fill]", "fill"));
c.add(selectionListScrollPane, "pos 6px 10px n 100%-12px");
c.add(headerPanel, "growx, dock north");
// show initial panel
showPanel((PanelBuilder) selectionList.getSelectedValue());
selectionList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
showPanel((PanelBuilder) selectionList.getSelectedValue());
if (!e.getValueIsAdjusting()) {
persistentSelectedPanel.setValue(Integer.toString(selectionList.getSelectedIndex()));
}
}
});
setSize(980, 630);
2013-01-19 01:50:48 -05:00
// KEYBOARD SHORTCUTS
2014-07-29 02:45:15 -04:00
SwingUI.installAction(this.getRootPane(), getKeyStroke(VK_DELETE, CTRL_MASK | SHIFT_MASK), new AbstractAction("Clear Cache") {
2013-01-19 01:50:48 -05:00
@Override
public void actionPerformed(ActionEvent e) {
CacheManager.getInstance().clearAll();
UILogger.info("Cache has been cleared");
}
});
2014-07-29 02:45:15 -04:00
SwingUI.installAction(this.getRootPane(), getKeyStroke(VK_F5, 0), new AbstractAction("Run") {
@Override
public void actionPerformed(ActionEvent evt) {
try {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); // loading Groovy might take a while
GroovyPad pad = new GroovyPad();
pad.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {
MainFrame.this.setVisible(false);
};
public void windowClosing(WindowEvent e) {
MainFrame.this.setVisible(true);
};
});
pad.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
2014-08-02 15:04:25 -04:00
pad.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
pad.setLocationByPlatform(true);
pad.setVisible(true);
} catch (IOException e) {
Logger.getLogger(GroovyPad.class.getName()).log(Level.WARNING, e.getMessage(), e);
} finally {
setCursor(Cursor.getDefaultCursor());
}
}
});
}
public static PanelBuilder[] createPanelBuilders() {
2013-12-30 02:37:01 -05:00
return new PanelBuilder[] { new RenamePanelBuilder(), new EpisodeListPanelBuilder(), new SubtitlePanelBuilder(), new SfvPanelBuilder(), new AnalyzePanelBuilder(), new ListPanelBuilder() };
}
protected void showPanel(PanelBuilder selectedBuilder) {
2009-03-13 21:01:01 -04:00
final JComponent contentPane = (JComponent) getContentPane();
JComponent panel = null;
2009-03-13 21:01:01 -04:00
for (int i = 0; i < contentPane.getComponentCount(); i++) {
JComponent c = (JComponent) contentPane.getComponent(i);
PanelBuilder builder = (PanelBuilder) c.getClientProperty("panelBuilder");
if (builder != null) {
if (builder.equals(selectedBuilder)) {
panel = c;
2009-03-13 21:01:01 -04:00
} else {
c.setVisible(false);
}
}
}
if (panel == null) {
panel = selectedBuilder.create();
panel.setVisible(false); // invisible by default
panel.putClientProperty("panelBuilder", selectedBuilder);
2009-03-13 21:01:01 -04:00
contentPane.add(panel);
}
// make visible, ignore action is visible already
if (!panel.isVisible()) {
headerPanel.setTitle(selectedBuilder.getName());
panel.setVisible(true);
}
}
private static class PanelSelectionList extends JList {
private static final int SELECTDELAY_ON_DRAG_OVER = 300;
public PanelSelectionList(PanelBuilder[] builders) {
2009-05-17 11:09:09 -04:00
super(builders);
setCellRenderer(new PanelCellRenderer());
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setBorder(new EmptyBorder(4, 5, 4, 5));
// initialize "drag over" panel selection
new DropTarget(this, new DragDropListener());
}
private class DragDropListener extends DropTargetAdapter {
private boolean selectEnabled = false;
private Timer dragEnterTimer;
@Override
public void dragOver(DropTargetDragEvent dtde) {
if (selectEnabled) {
int index = locationToIndex(dtde.getLocation());
setSelectedIndex(index);
}
}
@Override
public void dragEnter(final DropTargetDragEvent dtde) {
2014-07-29 02:45:15 -04:00
dragEnterTimer = SwingUI.invokeLater(SELECTDELAY_ON_DRAG_OVER, new Runnable() {
@Override
public void run() {
selectEnabled = true;
// bring window to front when on dnd
2014-08-10 02:31:47 -04:00
if (Settings.isMacApp()) {
2014-08-05 07:55:28 -04:00
MacAppUtilities.requestForeground();
} else {
SwingUtilities.getWindowAncestor(((DropTarget) dtde.getSource()).getComponent()).toFront();
}
}
});
}
@Override
public void dragExit(DropTargetEvent dte) {
selectEnabled = false;
if (dragEnterTimer != null) {
dragEnterTimer.stop();
}
}
@Override
public void drop(DropTargetDropEvent dtde) {
}
}
}
private static class PanelCellRenderer extends DefaultFancyListCellRenderer {
public PanelCellRenderer() {
super(10, 0, new Color(0x163264));
// center labels in list
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
setHighlightingEnabled(false);
setVerticalTextPosition(SwingConstants.BOTTOM);
setHorizontalTextPosition(SwingConstants.CENTER);
}
@Override
public void configureListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.configureListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
PanelBuilder panel = (PanelBuilder) value;
setText(panel.getName());
setIcon(panel.getIcon());
}
}
}