Skip to content

Commit

Permalink
Minimize to tray on window closing #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Gülle (Windows) committed Jan 1, 2017
1 parent 3d2da02 commit 49416f1
Showing 1 changed file with 123 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.midi_automator.view.frames;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
Expand All @@ -13,7 +14,12 @@
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
Expand All @@ -22,10 +28,12 @@
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
Expand All @@ -44,6 +52,7 @@
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
Expand Down Expand Up @@ -111,10 +120,12 @@ public class MainFrame extends JFrame {
public static final String MENU_ITEM_EXPORT = "Export...";
public static final String MENU_ITEM_EXIT = "Exit";
public static final String MENU_ITEM_PREFERENCES = "Preferences";
public static final String MENU_ITEM_OPEN_MIDI_AUTOMATOR = "Open...";
public static final String NAME_MENU_ITEM_IMPORT = "import";
public static final String NAME_MENU_ITEM_EXPORT = "export";
public static final String NAME_MENU_ITEM_PREFERENCES = "preferences";
public static final String NAME_MENU_ITEM_EXIT = "exit";
public static final String NAME_MENU_ITEM_OPEN_MIDI_AUTOMATOR = "open midi automator";
public static final String NAME_PREV_BUTTON = "previous button";
public static final String NAME_NEXT_BUTTON = "next button";
public static final String NAME_FILE_LIST = "file list";
Expand All @@ -123,20 +134,24 @@ public class MainFrame extends JFrame {
public static final String NAME_MIDI_OUT_DETECT_LABEL = "midi OUT label";
public static final String NAME_MIDI_IMPORT_FILECHOOSER = "import file chooser";
public static final String NAME_MIDI_EXPORT_FILECHOOSER = "export file chooser";
public static final String NAME_TRAY = "MIDI Automator";

private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem importMenuItem;
private JMenuItem exportMenuItem;
private JMenuItem exitMenuItem;
private JMenuItem preferencesMenuItem;
private PopupMenu trayPopupMenu;
private MenuItem restoreMainFrameMenuItem;
private BlinkingJLabel midiINdetect;
private BlinkingJLabel midiOUTdetect;
private HTMLLabel infoLabel;
private JScrollPane fileListScrollPane;
private CacheableBlinkableToolTipJList<IToolTipItem> fileList = new CacheableBlinkableToolTipJList<IToolTipItem>();
private CacheableJButton prevButton = new CacheableJButton();
private CacheableJButton nextButton = new CacheableJButton();
private TrayIcon trayIcon;

private KeyListener globalKeyListener = new GlobalKeyListener();
private PopupListener popupListener = new PopupListener();
Expand All @@ -149,6 +164,7 @@ public class MainFrame extends JFrame {

private int lastSelectedIndex;
private boolean popupWasShown;
private boolean exiting;

public static final int MIDI_DETECT_BLINK_RATE = 200;
public static final Color MIDI_DETECT_COLOR = Color.YELLOW;
Expand Down Expand Up @@ -220,6 +236,9 @@ public void init() {
icons.add(new ImageIcon(icon256).getImage());
setIconImages(icons);

// Tray
createTray(icon16);

iconPathPrev = resources.getImagePath() + File.separator
+ "arrow_prev.png";
iconPathNext = resources.getImagePath() + File.separator
Expand Down Expand Up @@ -260,11 +279,20 @@ public void init() {

createSwitchButtons(footerPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setAlwaysOnTop(true);

setVisible(true);

addWindowListener(new WindowHideListener(this));
}

public boolean isExiting() {
return exiting;
}

public void setExiting(boolean exiting) {
this.exiting = exiting;
}

@Override
Expand Down Expand Up @@ -616,7 +644,7 @@ private void createMenuItems() {
exitMenuItem = new JMenuItem(MENU_ITEM_EXIT);
exitMenuItem.setName(NAME_MENU_ITEM_EXIT);
exitMenuItem.setEnabled(true);
exitMenuItem.addActionListener(new ExitAction());
exitMenuItem.addActionListener(new ExitAction(this));
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
ActionEvent.ALT_MASK));
}
Expand Down Expand Up @@ -684,6 +712,38 @@ private void createMidiDetect(Container parent) {
parent.add(detectorPanel);
}

/**
* Creates a tray icon
*
* @param iconFileName
* The path to the icon file
*/
private void createTray(String iconFileName) {

if (SystemTray.isSupported()) {

SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage(iconFileName);

ActionListener mainFrameRestoreAction = new MainFrameRestoreAction(
this);

trayPopupMenu = new PopupMenu();
restoreMainFrameMenuItem = new MenuItem(
MENU_ITEM_OPEN_MIDI_AUTOMATOR);
restoreMainFrameMenuItem.addActionListener(mainFrameRestoreAction);
trayPopupMenu.add(restoreMainFrameMenuItem);
trayIcon = new TrayIcon(image, NAME_TRAY, trayPopupMenu);
trayIcon.addActionListener(mainFrameRestoreAction);

try {
tray.add(trayIcon);
} catch (AWTException e) {
log.error("Error on adding tray icon.", e);
}
}
}

/**
* Creates the list of files to open in the middle
*
Expand Down Expand Up @@ -879,6 +939,36 @@ public void mouseClicked(MouseEvent me) {
}
}

/**
* Hides the window and displays a dialog that the program was just hidden.
*
* @author aguelle
*
*/
class WindowHideListener extends WindowAdapter {

private final String DIALOG_MESSAGE = "The program window will be minimized to the system tray.";
private MainFrame programFrame;

public WindowHideListener(MainFrame programFrame) {
this.programFrame = programFrame;
}

@Override
public void windowClosing(WindowEvent e) {

if (!programFrame.isExiting()) {
// Show dialog
JFrame frame = new JFrame();
frame.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(frame, DIALOG_MESSAGE);

// hide window
e.getWindow().setVisible(false);
}
}
}

/**
* Closes all open frames and exits.
*
Expand All @@ -888,10 +978,17 @@ public void mouseClicked(MouseEvent me) {
class ExitAction extends AbstractAction {

private static final long serialVersionUID = 1L;
private MainFrame programFrame;

public ExitAction(MainFrame programFrame) {
this.programFrame = programFrame;
}

@Override
public void actionPerformed(ActionEvent e) {

programFrame.setExiting(true);
programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
presenter.close();

// Find the active frame before creating and dispatching the event
Expand Down Expand Up @@ -941,6 +1038,29 @@ public void actionPerformed(ActionEvent e) {
}
}

/**
* Restores the Main Frame.
*
* @author aguelle
*
*/
class MainFrameRestoreAction extends AbstractAction {

private static final long serialVersionUID = 1L;
private JFrame programFrame;

public MainFrameRestoreAction(JFrame programFrame) {
super();
this.programFrame = programFrame;
}

@Override
public void actionPerformed(ActionEvent e) {
programFrame.setState(Frame.NORMAL);
programFrame.setVisible(true);
}
}

/**
* Exports the set list and the preferences to a zipped file.
*
Expand Down

0 comments on commit 49416f1

Please sign in to comment.