?? mainframe.java
字號:
/**
* <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 + -