PihatonttuMain.java
上传用户:kyckim
上传日期:2007-12-11
资源大小:332k
文件大小:4k
- package Pihatonttu;
- import org.jdesktop.jdic.tray.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class PihatonttuMain implements ActionListener {
-
- static final String proxyName = "Pihatonttu Proxy";
- static final String version = "2007-07-14";
-
- static PihatonttuMain pihatonttuMain;
- static LogFrame logFrame;
- static Server server;
-
- static SystemTray tray = SystemTray.getDefaultSystemTray();
- static TrayIcon trayIcon;
- static final ImageIcon normalIcon = new ImageIcon(ClassLoader.getSystemResource("images/tray.gif"));
- static final ImageIcon connectedIcon = new ImageIcon(ClassLoader.getSystemResource("images/traycon.gif"));
-
- static String com;
- static String proxy;
-
- PihatonttuMain(String com, String proxy){
- JPopupMenu menu;
- JMenuItem menuItem;
-
- this.com = com;
- this.proxy = proxy;
-
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- showErrorDialog("Cannnot create GUI.");
- }
- if( Integer.parseInt(System.getProperty("java.version").substring(2,3)) >=5 )
- System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
- menu = new JPopupMenu("A Menu");
-
- menuItem = new JMenuItem("Abort Connection");
- menuItem.addActionListener(this);
- menu.add(menuItem);
-
- menuItem = new JMenuItem("Clear Log");
- menuItem.addActionListener(this);
- menu.add(menuItem);
-
- menu.addSeparator();
-
- menuItem = new JMenuItem("About");
- menuItem.addActionListener(this);
- menu.add(menuItem);
-
- menuItem = new JMenuItem("Quit");
- menuItem.addActionListener(this);
- menu.add(menuItem);
-
- trayIcon = new org.jdesktop.jdic.tray.TrayIcon(normalIcon, "Pihatonttu Proxy", menu);
-
- trayIcon.setIconAutoSize(true);
- trayIcon.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- logFrame.showLog();
- }
- });
-
- tray.addTrayIcon(trayIcon);
- logFrame = new LogFrame();
- logFrame.addElement(proxyName + " (" + version + ") started.");
- logFrame.showLog();
- server = new Server(com, proxy);
- server.start();
- }
-
- public void actionPerformed(ActionEvent e) {
- JMenuItem source = (JMenuItem) (e.getSource());
- String s = source.getText();
- if (s.equalsIgnoreCase("Abort Connection")) {
- try {
- server.exitServer();
- logFrame.addElement("Restarting...");
- Thread.sleep(3*1000);
- server = new Server(com, proxy);
- server.start();
- logFrame.addElementFraction("Restarting... Done.");
- } catch(InterruptedException ie) {
- }
- }
- if (s.equalsIgnoreCase("Clear Log")) {
- logFrame.clear();
- logFrame.addElement(proxyName + " (" + version + ") started.");
- }
- if (s.equalsIgnoreCase("About")) {
- showAboutDialog();
- }
- if(s.equalsIgnoreCase("Quit")) {
- logFrame.addElement(proxyName + " stopped");
- server.exitServer();
- try {
- Thread.sleep(1*1000);
- } catch(InterruptedException ie) {
- }
- System.exit(0);
- }
- }
-
- public static void showErrorDialog(String error) {
- JOptionPane.showMessageDialog(null, error, "Problem occured",JOptionPane.ERROR_MESSAGE);
- }
-
- public static void showAboutDialog() {
- JOptionPane.showMessageDialog(null,
- proxyName + " (" + version + ") nnCreated 2007 by Hiisi (hiisi.proxy@gmail.com)nVisit http://hiisi-proxy.blogspot.com/ for details.",
- "About Pihatonttu Proxy", JOptionPane.INFORMATION_MESSAGE);
- }
-
- public static void togleIcon(boolean state) {
- if(state) {
- trayIcon.setIcon(connectedIcon);
- } else {
- trayIcon.setIcon(normalIcon);
- }
- }
-
- public static void main(String[] args) {
- new PihatonttuMain(args[0], args[1]);
- }
- }