package com.ytec.jdap.panel;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import com.ytec.jdap.application.AppManagerToolkit;
import com.ytec.jdap.common.Constants;
import com.ytec.jdap.common.Global;
import com.ytec.jdap.config.ConfigMamager;
import com.ytec.jdap.model.Application;
import com.ytec.jdap.panel.util.AppTable;
import com.ytec.jdap.process.ProcessTookit;
/**
*
* <pre>
* Title:������������
* Description: ������������
* </pre>
*
* @author CaiJiuFa
* @version 1.00.00
*
* <pre>
* �ļ�¼
* �ĺ�汾: ���ˣ� ������: ������:
* </pre>
*/
public class JDapFrame extends BaseFrame {
private static final long serialVersionUID = -4123477140019870558L;
JButton sysConfigButton = new JButton("ϵͳ����");
JButton appPublishButton = new JButton("����Ӧ��");
JButton removeButton = new JButton("�");
JButton appStartButton = new JButton("����");
JButton appStopButton = new JButton("ֹͣ");
JButton restartButton = new JButton("����");
JButton refreshButton = new JButton("ˢ��");
public AppTable table;
public JScrollPane scrollpane;
static TrayIcon trayIcon = null; // ����ͼ��
static SystemTray tray = null; // ������ϵͳ���̵�ʵ��
SysConfigFrame configFrame = null;
AppPublishFrame publishFrame = null;
public JDapFrame() {
if (SystemTray.isSupported()) {// �������ϵͳ֧������
this.tray();
}
Container con = getContentPane();
con.setLayout(new BorderLayout());
JPanel jp1 = new JPanel();
jp1.setLayout(new FlowLayout());
jp1.add(sysConfigButton);
jp1.add(appPublishButton);
jp1.add(removeButton);
jp1.add(appStartButton);
jp1.add(appStopButton);
jp1.add(restartButton);
jp1.add(refreshButton);
con.add(jp1, BorderLayout.NORTH);
table = new AppTable(this);
scrollpane = new JScrollPane(table);
scrollpane.setPreferredSize(new Dimension(500, 400));
con.add(scrollpane, BorderLayout.CENTER);
sysConfigButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
configFrame();
}
});
final JDapFrame thiz = this;
appPublishButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
publishFrame(thiz, null);
}
});
appStartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < table.getRowCount(); i++) {
Boolean isSelect = (Boolean) table.getValueAt(i, 0);
String appId = (String) table.getValueAt(i, 1);
if (isSelect == true) {
try {
Application application = ConfigMamager.getReader().getAppById(appId);
if (Application.RUNNING.equals(application.getState())) {
continue;
}
application.setState(Application.RUNNING);
AppManagerToolkit.getAppManager().startApp(application);
} catch (Exception ex) {
sb.append(ex.getMessage());
continue;
}
}
}
table.repaintTable();
if (sb.length() > 0) {
BaseFrame.alert(sb.toString());
}
} catch (Exception ex) {
BaseFrame.alert(ex.getMessage());
}
}
});
appStopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < table.getRowCount(); i++) {
Boolean isSelect = (Boolean) table.getValueAt(i, 0);
if (isSelect == true) {
try {
String appId = (String) table.getValueAt(i, 1);
Application application = ConfigMamager.getReader().getAppById(appId);
if (Application.STOPED.equals(application.getState())) {
continue;
}
application.setState(Application.STOPED);
AppManagerToolkit.getAppManager().stopApp(application);
} catch (Exception ex) {
sb.append(ex.getMessage());
continue;
}
}
}
table.repaintTable();
if (sb.length() > 0) {
BaseFrame.alert(sb.toString());
}
} catch (Exception ex) {
BaseFrame.alert(ex.getMessage());
}
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
List<String> appIds = new ArrayList<String>();
for (int i = 0; i < table.getRowCount(); i++) {
Boolean isSelect = (Boolean) table.getValueAt(i, 0);
String appId = (String) table.getValueAt(i, 1);
if (isSelect == true) {
String status;
status = ConfigMamager.getReader().getAppById(appId).getState();
if (Application.RUNNING.equals(status)) {
BaseFrame.alert("ж��֮ǰ����ֹͣӦ��!");
return;
}
appIds.add(appId);
}
}
if (appIds.size() == 0) {
BaseFrame.alert("��ѡ����Ҫж�ص�Ӧ�ã�");
return;
}
ConfigMamager.getReader().removeApp(appIds);
table.repaintTable();
} catch (Exception e1) {
BaseFrame.alert(e1.getMessage());
}
}
});
restartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < table.getRowCount(); i++) {
Boolean isSelect = (Boolean) table.getValueAt(i, 0);
if (isSelect == true) {
try {
String appId = (String) table.getValueAt(i, 1);
Application application = ConfigMamager.getReader().getAppById(appId);
application.setState(Application.STOPED);
AppManagerToolkit.getAppManager().stopApp(application);
application.setState(Application.RUNNING);
AppManagerToolkit.getAppManager().startApp(application);
} catch (Exception ex) {
sb.append(ex.getMessage());
continue;
}
}
}
table.repaintTable();
if (sb.length() > 0) {
BaseFrame.alert(sb.toString());
}
} catch (Exception ex) {
BaseFrame.alert(ex.getMessage());
}
}
});
refreshButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
List<Application> appList = ConfigMamager.getReader().getAppList();
Application modifyApp = new Application();
for (Iterator<Application> it = appList.iterator(); it.hasNext();) {
Application app = (Application) it.next();
String pid = ProcessTookit.getIProcessManager().getPIDByAppId(app.getId());
if (pid == null || ProcessTookit.getIProcessManager().getMemByPID(pid) == null) {
app.setState(Application.STOPED);
modifyApp.addBrother(app);
}
}
if (modifyApp.hasBrother()) {// ��������
AppManagerToolkit.getAppManager().stopApp(modifyApp.getBrother());
ConfigMamager.getReader().modifyApp(modifyApp);
ConfigMamager.getReader().refreshData();
table.repaintTable();
}
} catch (Exception e1) {
BaseFrame.alert(e1.getMessage());
}
}
});
setFrameLocation(this, 550, 300);
this.setTitle(Constants.TITLE);
this.setIconImage(new ImageIcon(Constants.IMAGE_TITLE).getImage());
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
/**
* ϵͳ����
*/
public void tray() {
tray = SystemTray.getSystemTray(); // ��ñ�����ϵͳ���̵�ʵ��
ImageIcon icon = new ImageIcon(Constants.IMAGE_ICON); // ��Ҫ��ʾ�������е�ͼ��
PopupMenu pop = new PopupMenu(); // ����һ���Ҽ�����ʽ�˵�
MenuItem show = new MenuItem("����");
MenuItem exit = new MenuItem("�˳�����");
pop.add(show);
pop.add(exit);
trayIcon = new TrayIcon(icon.getImage(), Constants.TITLE, pop);
final JDapFrame thiz = this;
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {// ������������������������ͼ����˫��ʱ��Ĭ����ʾ����
if (e.getClickCount() == 2) {// ���˫��
if (authenticate(thiz)) {
setExtendedState(JFrame.NORMAL);
setVisible(true); // ��ʾ����
toFront();
}
}
}
});
show.addActionListener(new ActionListener() {// ��������˵�������ʾ����
public void actionPerformed(ActionEvent e) {
if (authenticate(thiz)) {
setExtendedState(JFrame.NORMAL);
setVisible(true); // ��ʾ����
toFront();
}
}
});
exit.addActionListener(new ActionListener() {// ������˳����˵����˳�����
public void actionPerformed(ActionEvent e) {
if(!authenticate(thiz))
return ;
if (Global.AppProcess.size() == 0 || BaseFrame.confirm("�˳���ֹͣ����Ӧ�ã�ȷ����?") == JOptionPane.YES_OPTION) {
try {
AppManagerToolkit.getAppManager().stopAll();// ֹͣ����Ӧ��
tray.remove(trayIcon);// ��ϵͳ������ʵ�����Ƴ�����ͼ��
System.exit(0); // �˳�����
} catch (Exception ex) {
BaseFrame.alert(ex.getMessage());
}
}
}
});
try {
tray.add(trayIcon);
} catch (AWTException e1) {
BaseFrame.alert(e1.getMessage());
}
}
/**
* ϵͳ���ý���
*/
public void configFrame() {
configFrame = new SysConfigFrame();
setFrameLocation(configFrame, 400, 200);
configFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
configFrame.setResizable(false);
configFrame.setVisible(true);
}
/**
* Ӧ�÷�������
*
* @param appId
*/
public void publishFrame(BaseFrame frame, String appId) {
publishFrame = new AppPublishFrame(appId, this.table);
setFrameLocation(publishFrame, 420, 250);
publishFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
publishFrame.setVisible(true);
publishFrame.setResizable(false);
}
}