📄 mainremindui.java
字号:
package com.liming.remind.ui;
import java.awt.AWTException;
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.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import com.liming.remind.common.RemindThread;
import com.liming.remind.common.RemindUtils;
import com.liming.remind.dao.RemindDAO;
import com.liming.remind.dto.Remind;
import com.liming.remind.exception.MyArrayIndexOutOfBoundsException;
import com.liming.remind.exception.MyFileNotFoundException;
import com.liming.remind.exception.MyIOException;
/**
* Function : Main Remind UI
*
* @author Liming
* @time Dec 19, 2008 8:47:29 PM
* @version 1.0
*/
public class MainRemindUI extends JFrame {
/**
* Field : serialVersionUID
*/
private static final long serialVersionUID = -3124826728019316962L;
// System Tray
// Set Tray Icon
private TrayIcon trayIcon = null;
// Set Tray
private SystemTray tray = null;
// RemindDAO
private RemindDAO remindDAO;
// Remind Table
private RemindTable remindTable;
/**
* Constructor method to initialize fields
*/
public MainRemindUI() {
// Judge The JDK whether support Tray
if (SystemTray.isSupported()) {
this.getRemindTray();
}
// Set Tray listener
// Add Tray to windows
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
tray.add(trayIcon);
dispose();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
public void windowIconified(WindowEvent e) {
try {
tray.add(trayIcon);
dispose();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
// Get WindowsLookAndFeel
RemindUtils.getWindowsLookAndFeel(this);
// Get remindDAO
try {
this.remindDAO = new RemindDAO();
} catch (MyFileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "Sorry, I can't find file.",
"System Tips", JOptionPane.YES_OPTION, new ImageIcon(
"res/tip.gif"));
System.exit(1);
} catch (MyIOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, some IO error happened to it.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
} catch (MyArrayIndexOutOfBoundsException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, the size is out of the quantity of the Array",
"System Tips", JOptionPane.YES_OPTION, new ImageIcon(
"res/tip.gif"));
System.exit(1);
}
// Get Remind Table
remindTable = new RemindTable(remindDAO);
// Get Content Pane
JPanel panel = (JPanel) getContentPane();
// Table Popus
initTablePopus();
// Add table to JPanel
panel.add(new JScrollPane(remindTable));
// Set title
this.setTitle("Remind-2008 (Brj Edition)");
// Set Size
this.setSize(500, 200);
// Set soft Icon
this.setIconImage(new ImageIcon("res/brj.gif").getImage());
// Can not be resized
this.setResizable(false);
// Set the window to the center of the screen
RemindUtils.setUIMiddleOfTheWindow(this);
// Start Remind Thread
new RemindThread(remindDAO.getRemindsList()).start();
}
/**
* Function : System Tray
*/
private void getRemindTray() {
tray = SystemTray.getSystemTray();
ImageIcon icon = new ImageIcon("res/brj.gif");
PopupMenu pop = new PopupMenu();
MenuItem show = new MenuItem("Show Remind");
MenuItem exit = new MenuItem("Exit Remind");
pop.add(show);
pop.add(exit);
trayIcon = new TrayIcon(icon.getImage(), "Remind(brj-Editon)", pop);
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
tray.remove(trayIcon);
setExtendedState(JFrame.NORMAL);
setVisible(true);
toFront();
}
}
});
show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
setExtendedState(JFrame.NORMAL);
setVisible(true);
toFront();
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
/**
* Function : Initialize the Table Popus
*/
private void initTablePopus() {
remindTable.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int row = remindTable.rowAtPoint(e.getPoint());
remindTable.setRowSelectionInterval(row, row);
if (e.isPopupTrigger()) {
showPopusMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showPopusMenu(e);
}
}
});
}
/**
* Function : Set where to place popus
*
* @param e
*/
private void showPopusMenu(MouseEvent e) {
Remind remind = remindTable.getSelectedRemind();
JPopupMenu popupMenu = null;
if (remind == null) {
popupMenu = getNoRemindPopupMenu();
} else {
popupMenu = getPopupMenu();
}
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
/**
* Function : Get PopupMenu with no remind at that row
*
* @return
*/
private JPopupMenu getNoRemindPopupMenu() {
JPopupMenu jm = new JPopupMenu();
// Information about Author
JMenuItem about = new JMenuItem("About Author");
about.setActionCommand("about");
about.addActionListener(new ButtonListener());
// Blog about author
JMenuItem blog = new JMenuItem("Author Blog");
blog.setActionCommand("blog");
blog.addActionListener(new ButtonListener());
// Add one Remind
JMenuItem add = new JMenuItem("Add Remind");
add.setActionCommand("add");
add.addActionListener(new ButtonListener());
jm.add(about);
jm.add(blog);
jm.add(add);
return jm;
}
/**
* Function : Get PopupMenu
*
* @return
*/
private JPopupMenu getPopupMenu() {
JPopupMenu jm = new JPopupMenu();
// Information about Author
JMenuItem about = new JMenuItem("About Author");
about.setActionCommand("about");
about.addActionListener(new ButtonListener());
// Blog about author
JMenuItem blog = new JMenuItem("Author Blog");
blog.setActionCommand("blog");
blog.addActionListener(new ButtonListener());
// Delete the selected one
JMenuItem delete = new JMenuItem("Delete This");
delete.setActionCommand("delete");
delete.addActionListener(new ButtonListener());
// Add one Remind
JMenuItem add = new JMenuItem("Add Remind");
add.setActionCommand("add");
add.addActionListener(new ButtonListener());
// Delete All Reminds
JMenuItem clear = new JMenuItem("Clear Reminds");
clear.setActionCommand("clear");
clear.addActionListener(new ButtonListener());
jm.add(about);
jm.add(blog);
jm.add(add);
jm.add(delete);
jm.add(clear);
return jm;
}
/**
* Function : Get author Information
*/
private void getAuthorInfo() {
String myInformation = "<html>About Me:<font color=green><ul><li>Name: liming</li>"
+ "<li>Sex: male</li><li>Occupation: Java</li>"
+ "<li>Hobby: Java OpenSource</li>"
+ "<li>qq: 295361921</li>"
+ "<li>Email: bzrjlm@163.com</li>"
+ "<li>Motto: I will be better with you!</li></font><html>";
JOptionPane.showMessageDialog(null, myInformation, "About Me",
JOptionPane.YES_OPTION, new ImageIcon("res/me.jpg"));
}
/**
* Function : Visit author's blog
*/
private void visitAuthorBlog() {
try {
String cmd = null;
cmd = "rundll32 url.dll,FileProtocolHandler "
+ "http://hi.baidu.com/qq295361921";
Process p = Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Can not visit The Blog of author", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
}
}
/**
* Function : Add one Remind
*/
private void addRemind() {
AddRemindUI addRemindUI = null;
addRemindUI = new AddRemindUI();
addRemindUI.setVisible(true);
if (addRemindUI.isRemindNull() == false) {
int id = RemindUtils.getMaxIdFromList(remindDAO.getRemindsList());
Remind remind = addRemindUI.getRemind();
remind.setId(id);
try {
remindDAO.addRemind(remind);
remindTable.getModel().fireTableDataChanged();
} catch (MyFileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, I can't find file.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
} catch (MyIOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, some IO error happened to it.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
}
}
}
/**
* Function : Delete one Remind
*/
private void deleteRemind() {
int i = JOptionPane.showConfirmDialog(null,
"Are you sure to delete it?", "System Tips",
JOptionPane.YES_NO_OPTION);
if (i == 0) {
Remind remind = remindTable.getSelectedRemind();
try {
remindDAO.deleteRemind(remind);
remindTable.getModel().fireTableDataChanged();
} catch (MyFileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, I can't find file.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
} catch (MyIOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, some IO error happened to it.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
}
}
}
/**
* Function : Delete all Reminds
*/
private void clear() {
int i = JOptionPane.showConfirmDialog(null, "Are you sure to clear?",
"System Tips", JOptionPane.YES_NO_OPTION);
if (i == 0) {
try {
remindDAO.deleteAllReminds();
remindTable.getModel().fireTableDataChanged();
} catch (MyFileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, I can't find file.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
} catch (MyIOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null,
"Sorry, some IO error happened to it.", "System Tips",
JOptionPane.YES_OPTION, new ImageIcon("res/tip.gif"));
System.exit(1);
}
}
}
/**
* Function : The inner class to handle the click events
*
* @author Liming
* @time Dec 19, 2008 7:17:28 PM
* @version 1.0
*/
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// About Author
if (e.getActionCommand().equals("about")) {
getAuthorInfo();
}
// Visit author blog
if (e.getActionCommand().equals("blog")) {
visitAuthorBlog();
}
// Add one Remind
if (e.getActionCommand().equals("add")) {
addRemind();
}
// Delete selected one
if (e.getActionCommand().equals("delete")) {
deleteRemind();
}
// Delete All Reminds
if (e.getActionCommand().equals("clear")) {
clear();
}
}
}
/**
* Function : Reminder Entrance
*
* @param agrs
*/
public static void main(String agrs[]) {
new MainRemindUI().setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -