⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 一个用于监控WEB服务器和数据库服务器的客户端程序。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * <p>Title: 卓博营运支撑系统</p>
 *
 * <p>Description: 为本企业内部运作提供支撑,为企业資源的E化管理提供解決方案</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: 卓博信息科技有限公司</p>
 *
 * @author Henry
 * @version 1.0
 */
package com.jobcn.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.*;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

import com.jeans.trayicon.TrayIconException;
import com.jeans.trayicon.TrayIconPopup;
import com.jeans.trayicon.TrayIconPopupSimpleItem;
import com.jeans.trayicon.WindowsTrayIcon;
import com.jobcn.control.*;

/**
 * MainFrame.java 
 * function: 系统主界面类
 * describe: 
 */

public class MainFrame extends JFrame {

	public MainFrame() {
		// 生成一个实例对象供外部调用时,将 outerInvoke 设置为 true.
		outerInvoke = true;
		// 生成系统主界面。
		createAndShowGUI();
		// 加入托盘图标。
		addTrayIcon();
	}
	
	public MainFrame(String title) {
		super(title);
		initializeUI();
		addListener();
	}
	
	private void addTrayIcon() {
		String appName = "TaskManageTray";//"TestTray";
		long result = WindowsTrayIcon.sendWindowsMessage(appName, 0);//1234);
		if (result != -1) {
			System.out.println("Already running other instance of " + appName
					+ " (returns: " + result + ")");
			JOptionPane.showMessageDialog(mainFrame, "已经有一个任务管理系统在运行!");
			exitSystem();
		}
		WindowsTrayIcon.initTrayIcon(appName);
		Image france = Constant.trayIcon.getImage();
		WindowsTrayIcon icon = null;
		try {
			icon = new WindowsTrayIcon(france, 16, 16);
			icon.setToolTipText(Constant.systemName);//设置托盘图标的提示信息 
			icon.setPopup(getTrayPopup());//给托盘图标加右键菜单 
			icon.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					/*if(mainFrame.getExtendedState()==Frame.ICONIFIED){
						mainFrame.setExtendedState(Frame.NORMAL);
						mainFrame.setVisible(true);
			        }
					else {
						mainFrame.setVisible(!mainFrame.isVisible());
					}

					mainFrame.toFront();*/
					
					
					if (!mainFrame.isShowing()) {
						mainFrame.setVisible(true);
					}
					else {
						mainFrame.setVisible(false);
					}
					if(mainFrame.getExtendedState()==Frame.ICONIFIED){
						mainFrame.setExtendedState(Frame.NORMAL);
						mainFrame.setVisible(true);
			        }
					mainFrame.toFront();
				}
			});//单击托盘图标时的动作 
			icon.setVisible(true);//托盘图标设置为显示状态 
		} catch (TrayIconException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (icon == null) {
			return;
		}
		WindowsTrayIcon.keepAlive();
	}
    
	public TrayIconPopup getTrayPopup() {//构造弹出菜单 
		TrayIconPopup popup = new TrayIconPopup();
		// 增加三个菜单项 
		TrayIconPopupSimpleItem item = new TrayIconPopupSimpleItem("打开主界面");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setVisible(true);
				mainFrame.toFront();
			}
		});
		popup.addMenuItem(item);
		item = new TrayIconPopupSimpleItem("关闭主界面");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.dispose();
			}
		});
		popup.addMenuItem(item);
		item = new TrayIconPopupSimpleItem("退出");
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exitSystem();
			}
		});
		popup.addMenuItem(item);
		return popup;
	}
	
	private void initializeUI() {
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(createMenuBar(), BorderLayout.NORTH);
		JToolBar toolBar = new JToolBar();
		runAllButton = new JButton("全部启动");//, icon);
		stopAllButton = new JButton("全部中止");//, icon);
		newTaskButton = new JButton("新建");
		editTaskButton = new JButton("编辑");
		delTaskButton = new JButton("删除");		
		saveTaskListButton = new JButton("保存");//, icon);
		clearTaskListButton = new JButton("清空");
		toolBar.add(newTaskButton);
		toolBar.add(editTaskButton);
		toolBar.add(delTaskButton);
		toolBar.add(saveTaskListButton);
		toolBar.add(clearTaskListButton);
		toolBar.add(runAllButton);
		toolBar.add(stopAllButton);
		toolBar.setFloatable(false);
        toolBar.setRollover(true);
		panel.add(toolBar, BorderLayout.SOUTH);
		panel.setOpaque(true);
		contentPane.add(panel, BorderLayout.NORTH);
        JSplitPane pane = new JSplitPane(
        		JSplitPane.VERTICAL_SPLIT, 
        		createTaskListTable(), 
        		createTaskListInfo()
        		);
        pane.setDividerLocation(200);
	    pane.setDividerSize(2);
	    contentPane.add(pane, BorderLayout.CENTER);
		fileChooser = new JFileChooser();
		fileChooser.setCurrentDirectory(new java.io.File(Constant.taskConfPath));
		filter = new XmlFilter();
		fileChooser.addChoosableFileFilter(filter);
		fileChooser.setVisible(true);
		taskDialog = new TaskDialog(this);
		proxy = new TaskProxy(this);
		historyDialog = new HistoryLog(this);
		aboutText = Constant.aboutText;
	}

	private JComponent createMenuBar() {
		JMenu taskMenu = new JMenu("任务");
		newTaskMenuItem = new JMenuItem("新建");
		loadTaskMenuItem = new JMenuItem("载入。。。");
		exitMenuItem = new JMenuItem("退出");
		taskMenu.add(newTaskMenuItem);
		taskMenu.add(loadTaskMenuItem);
		taskMenu.add(exitMenuItem);
		JMenu setupMenu = new JMenu("设置");
		JMenu systemMenuItem = new JMenu("系统");
		JMenu skinMenuItem = new JMenu("换肤");
		windowSkinMenuItem = new JMenuItem("Window风格");
		swingSkinMenuItem = new JMenuItem("Swing风格");
		skinMenuItem.add(windowSkinMenuItem);
		skinMenuItem.add(swingSkinMenuItem);
		mailMenuItem = new JMenuItem("邮件");
		systemMenuItem.add(skinMenuItem);
		systemMenuItem.add(mailMenuItem);
		setupMenu.add(systemMenuItem);
		JMenu helpMenu = new JMenu("帮助");
		aboutMenuItem = new JMenuItem("关于...");
		helpMenu.add(aboutMenuItem);
		JMenu logMenu = new JMenu("日志");
		clearLogItem = new JMenuItem("清空实时记录");
		logMenu.add(clearLogItem);
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(taskMenu);
		menuBar.add(setupMenu);
		menuBar.add(logMenu);
		menuBar.add(helpMenu);
		return menuBar;
	}

	private JComponent createTaskListTable() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		model = new MainFrameTableModel();
		table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    columnCount = table.getColumnCount();
		TableColumnModel colModel = table.getColumnModel();
		TableColumn[] columns = new TableColumn[columnCount];
		for (int i = 0; i < columnCount; i++) {
			columns[i] = colModel.getColumn(i);
			columns[i].setPreferredWidth(colWidths[i]);
		}
	    editor = table.getDefaultEditor(Boolean.class);
	    DefaultTableCellRenderer render = new DefaultTableCellRenderer() {
			public Component getTableCellRendererComponent(
					JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				JCheckBox checkBox = new JCheckBox();
				boolean b = ((Boolean) value).booleanValue();
				if (b) {
					checkBox.setBackground(Color.RED);
				}
				else {
					checkBox.setBackground(Color.WHITE);
				}
				checkBox.setSelected(b);
				checkBox.setHorizontalAlignment(JLabel.CENTER);
				return checkBox;
			}
		};
		String[] header = Constant.taskListData;
		TableColumn tc = table.getColumn(header[columnCount-1]);
		tc.setCellRenderer(render);

		JScrollPane scrollPane = new JScrollPane(table);
		panel.add(scrollPane);
		final JPanel panel_1 = new JPanel();
		panel_1.setLayout(new GridLayout());
		panel.add(panel_1, BorderLayout.SOUTH);
		final JLabel label = new JLabel();
		panel_1.add(label);
		TitledBorder tb = new TitledBorder("任务列表");
		tb.setTitleFont(Constant.systemFont);
		panel.setBorder(tb);
		return panel;
	}
	
	private JComponent createTaskListInfo() {
		JPanel p = new JPanel();
		p.setFont(Constant.systemFont);
		p.setLayout(new BorderLayout());
		TitledBorder tb = new TitledBorder("任务运行实时记录");
		tb.setTitleFont(Constant.systemFont);
		p.setBorder(tb);
		logInfo = new JTextPane();
		logInfo.setFont(Constant.systemFont);
		logInfo.setEditable(false);
		Style style = logInfo.addStyle("Red", null);
	    StyleConstants.setForeground(style, Color.RED);
	    JScrollPane logScrollPane = new JScrollPane(logInfo);
		p.add(logScrollPane);
		return p;
	}

	private void addListener() {
		newTaskButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.createNewTask();
				}
			}
		});
		editTaskButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.editTask(table.getSelectedRow(), 0);
				}
			}
		});
		delTaskButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.delTask();
				}
			}
		});
		saveTaskListButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.saveTaskList();
				}
			}
		});
		clearTaskListButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.clearTaskList();
				}
			}
		});
		runAllButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.runAllTask();
				}
			}
		});
		stopAllButton.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					proxy.stopAllTask();
				}
			}
		});
		table.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) { // 鼠标左键
					if (e.getClickCount() == 1 && table.getSelectedColumn() != table.getColumnCount()-1){
						
					}
					else if (e.getClickCount() == 2 && table.getSelectedColumn() != table.getColumnCount()-1){
						proxy.editTask(table.getSelectedRow(), 0);
					}
				}
				else if (e.getButton() == MouseEvent.BUTTON3){ // 鼠标右键
					final int rowNo = table.getSelectedRow();
					JMenuItem instantRun = new JMenuItem("立即执行");
					instantRun.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							proxy.runOrStopTask(rowNo, true, 1);
						}
					});

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -