|
|
@@ -1,18 +1,30 @@
|
|
|
package com.leanwo.management;
|
|
|
|
|
|
+import java.awt.AWTEvent;
|
|
|
+import java.awt.AWTException;
|
|
|
import java.awt.BorderLayout;
|
|
|
import java.awt.GridBagConstraints;
|
|
|
import java.awt.GridBagLayout;
|
|
|
+import java.awt.Image;
|
|
|
import java.awt.Insets;
|
|
|
+import java.awt.SystemTray;
|
|
|
import java.awt.Toolkit;
|
|
|
+import java.awt.TrayIcon;
|
|
|
import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
|
+import java.awt.event.MouseAdapter;
|
|
|
+import java.awt.event.MouseEvent;
|
|
|
+import java.awt.event.WindowAdapter;
|
|
|
+import java.awt.event.WindowEvent;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.net.URL;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
import javax.swing.BorderFactory;
|
|
|
import javax.swing.JButton;
|
|
|
import javax.swing.JFrame;
|
|
|
@@ -52,6 +64,9 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
private JPanel jPanel = null;
|
|
|
private Timer timer;
|
|
|
|
|
|
+ private TrayIcon trayIcon; // 托盘图标
|
|
|
+ private SystemTray systemTray; // 系统托盘
|
|
|
+
|
|
|
private Map<ApplicationSetting, JLabel> statusLabels = null;
|
|
|
|
|
|
public ServerManagmentJFrame(boolean isAutoRun) {
|
|
|
@@ -61,7 +76,11 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
this.add(jPanel, BorderLayout.NORTH);
|
|
|
|
|
|
this.setTitle("Prodog程序管理器");
|
|
|
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定按关闭时的操作,这里是关闭窗口,如果不设定,就什么也不会发生
|
|
|
+ // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设定按关闭时的操作,这里是关闭窗口,如果不设定,就什么也不会发生
|
|
|
+
|
|
|
+ //激活窗口事件
|
|
|
+ this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
|
|
|
+
|
|
|
// 设置窗体位置和大小
|
|
|
this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
|
|
|
|
|
|
@@ -73,12 +92,59 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
timer = new Timer(1000,this);
|
|
|
timer.start();
|
|
|
|
|
|
+ // 判断当前平台是否支持系统托盘
|
|
|
+ if(SystemTray.isSupported()) {
|
|
|
+ // 获得系统托盘的实例
|
|
|
+ systemTray = SystemTray.getSystemTray();
|
|
|
+ try {
|
|
|
+ URL path = ServerManagmentJFrame.class.getResource("prodog_16.png");
|
|
|
+ Image imgae = ImageIO.read(path);
|
|
|
+ trayIcon = new TrayIcon(imgae);
|
|
|
+ trayIcon.displayMessage("通知:", "程序最小化到系统托盘", TrayIcon.MessageType.INFO);
|
|
|
+ // 设置托盘的图标
|
|
|
+ systemTray.add(trayIcon);
|
|
|
+
|
|
|
+ trayIcon.addMouseListener(new MouseAdapter() {
|
|
|
+ public void mouseClicked(MouseEvent e) {
|
|
|
+ // 双击托盘窗口再现
|
|
|
+ if (e.getClickCount() == 2) {
|
|
|
+ boolean visible = isVisible();
|
|
|
+ // setExtendedState(Frame.NORMAL);
|
|
|
+ setVisible(!visible);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ this.addWindowListener(new WindowAdapter() {
|
|
|
+ public void windowIconified(WindowEvent e) {
|
|
|
+ // 窗口最小化时
|
|
|
+ setVisible(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ } catch (AWTException e2) {
|
|
|
+ e2.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 程序自动运行
|
|
|
if(isAutoRun) {
|
|
|
autoStart();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //重写这个方法
|
|
|
+ @Override
|
|
|
+ protected void processWindowEvent(WindowEvent e) {
|
|
|
+ if (e.getID() == WindowEvent.WINDOW_CLOSING) {
|
|
|
+ setVisible(false);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ super.processWindowEvent(e); //该语句会执行窗口事件的默认动作(如:隐藏)
|
|
|
+ }
|
|
|
+
|
|
|
public void generateViewFromJson() {
|
|
|
GridBagConstraints constrains = null;
|
|
|
|
|
|
@@ -166,7 +232,8 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
try {
|
|
|
- ExecuteWatchdog executeWatchdog = runProcess(setting.getStartBatFile());
|
|
|
+ String path = setting.getInstallPath() + File.separator + setting.getStartBatFile();
|
|
|
+ ExecuteWatchdog executeWatchdog = runProcess(path);
|
|
|
setting.setExecuteWatchdog(executeWatchdog);
|
|
|
} catch (IOException e1) {
|
|
|
e1.printStackTrace();
|
|
|
@@ -186,7 +253,8 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
try {
|
|
|
- ExecuteWatchdog executeWatchdog = runProcess(setting.getStopBatFile());
|
|
|
+ String path = setting.getInstallPath() + File.separator + setting.getStopBatFile();
|
|
|
+ ExecuteWatchdog executeWatchdog = runProcess(path);
|
|
|
setting.setExecuteWatchdog(null);
|
|
|
} catch (IOException e1) {
|
|
|
logger.error("程序启动失败", e1);
|
|
|
@@ -204,6 +272,11 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private ExecuteWatchdog runProcess(String command) throws IOException {
|
|
|
+ if(command == null || command.length() == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ logger.info("即将运行命令:" + command);
|
|
|
+
|
|
|
final CommandLine cmdLine = CommandLine.parse(command);
|
|
|
|
|
|
final ExecuteWatchdog watchDog = new ExecuteWatchdog(Long.MAX_VALUE);
|
|
|
@@ -267,12 +340,15 @@ public class ServerManagmentJFrame extends JFrame implements ActionListener {
|
|
|
final ApplicationSetting setting = settings.get(i);
|
|
|
ExecuteWatchdog executeWatchdog;
|
|
|
try {
|
|
|
- executeWatchdog = runProcess(setting.getStartBatFile());
|
|
|
- boolean isWatch = executeWatchdog.isWatching();
|
|
|
- if(! isWatch) {
|
|
|
- logger.error("程序" + setting.getName() + "启动失败");
|
|
|
+ String path = setting.getInstallPath() + File.separator + setting.getStartBatFile();
|
|
|
+ executeWatchdog = runProcess(path);
|
|
|
+ if(executeWatchdog != null) {
|
|
|
+ boolean isWatch = executeWatchdog.isWatching();
|
|
|
+ if(! isWatch) {
|
|
|
+ logger.error("程序" + setting.getName() + "启动失败");
|
|
|
+ }
|
|
|
+ setting.setExecuteWatchdog(executeWatchdog);
|
|
|
}
|
|
|
- setting.setExecuteWatchdog(executeWatchdog);
|
|
|
} catch (IOException e) {
|
|
|
logger.error("程序" + setting.getName() + "启动失败", e);
|
|
|
}
|