2009-06-16 50 views
2

我們有一個運行在tomcat上的內部web應用程序,基於Spring構建。 Web應用程序前端使用Flex構建。
我想創建一個跨平臺的系統托盤應用程序,它允許進入應用程序的主頁,並在服務器發生某些事情時顯示警報。使用什麼技術將systray前端編寫到webapp?

你會覺得什麼是最好的技術:

  • 系統托盤本身? Java Swing?
  • 服務器和系統托盤之間的通信?網絡服務? RSS訂閱?春季遠程? JMX通知?

問候,

維姆

+0

什麼是JRE的目標版本? v.6有廣泛的系統支持。我會讓系統託管應用程序通過刮取輪詢狀態頁面。 (因爲它很簡單,它會工作):D – 2009-06-16 19:09:47

回答

3

如果你想留在Java中,你有兩個選擇:

  • 使用Swing/AWT。請確保您使用的是Java 6及以上(你可以用你的應用程序安裝),因爲它有系統托盤支持(從API):

    TrayIcon trayIcon = null; 
    if (SystemTray.isSupported()) { 
        // get the SystemTray instance 
        SystemTray tray = SystemTray.getSystemTray(); 
        // load an image 
        Image image = Toolkit.getDefaultToolkit.getImage(""); 
        // create a action listener to listen for default action executed on 
        // the tray icon 
        ActionListener listener = new ActionListener() { 
         public void actionPerformed(ActionEvent e) { 
          // execute default action of the application 
          // ... 
         } 
        }; 
        // create a popup menu 
        PopupMenu popup = new PopupMenu(); 
        // create menu item for the default action 
        MenuItem defaultItem = new MenuItem(""); 
        defaultItem.addActionListener(listener); 
        popup.add(defaultItem); 
        ///... add other items 
        // construct a TrayIcon 
        trayIcon = new TrayIcon(image, "Tray Demo", popup); 
        // set the TrayIcon properties 
        trayIcon.addActionListener(listener); 
        // ... 
        // add the tray image 
        try { 
         tray.add(trayIcon); 
        } catch (AWTException e) { 
         System.err.println(e); 
        } 
        // ... 
    } else { 
        // disable tray option in your application or 
        // perform other actions 
        // ... 
    } 
    // ... 
    // some time later 
    // the application state has changed - update the image 
    if (trayIcon != null) { 
        trayIcon.setImage(updatedImage); 
    } 
    // ... 
    
  • 使用SWT/JFace。下面是一個例子(從here拍攝):

    public static void main(String[] args) { 
        Display display = new Display(); 
        Shell shell = new Shell(display); 
        Image image = new Image(display, 16, 16); 
        final Tray tray = display.getSystemTray(); 
        if (tray == null) { 
         System.out.println("The system tray is not available"); 
        } else { 
         final TrayItem item = new TrayItem(tray, SWT.NONE); 
         item.setToolTipText("SWT TrayItem"); 
         item.addListener(SWT.Show, new Listener() { 
          public void handleEvent(Event event) { 
           System.out.println("show"); 
          } 
         }); 
         item.addListener(SWT.Hide, new Listener() { 
          public void handleEvent(Event event) { 
           System.out.println("hide"); 
          } 
         }); 
         item.addListener(SWT.Selection, new Listener() { 
          public void handleEvent(Event event) { 
           System.out.println("selection"); 
          } 
         }); 
         item.addListener(SWT.DefaultSelection, new Listener() { 
          public void handleEvent(Event event) { 
           System.out.println("default selection"); 
          } 
         }); 
         final Menu menu = new Menu(shell, SWT.POP_UP); 
         for (int i = 0; i < 8; i++) { 
          MenuItem mi = new MenuItem(menu, SWT.PUSH); 
          mi.setText("Item" + i); 
          mi.addListener(SWT.Selection, new Listener() { 
           public void handleEvent(Event event) { 
            System.out.println("selection " + event.widget); 
           } 
          }); 
          if (i == 0) 
           menu.setDefaultItem(mi); 
         } 
         item.addListener(SWT.MenuDetect, new Listener() { 
          public void handleEvent(Event event) { 
           menu.setVisible(true); 
          } 
         }); 
         item.setImage(image); 
        } 
        shell.setBounds(50, 50, 300, 200); 
        shell.open(); 
        while (!shell.isDisposed()) { 
         if (!display.readAndDispatch()) 
          display.sleep(); 
        } 
        image.dispose(); 
        display.dispose(); 
    } 
    
1

使用Adobe AIR和BlazeDS或LCD,你可以輕鬆地構建這種類型的應用。

+0

是的,但我忘了一個重要的要求。系統托盤也應該能夠啓動任意應用程序。我已經讀過,由於沙箱限制,您無法從AIR啓動應用程序。 – 2009-06-17 10:52:38

+1

也許像Merapi這樣的事情會對此有所幫助? – 2009-06-17 17:48:57

+0

謝謝你,看起來很有趣。這是鏈接,如果人們想知道的話:http://merapiproject.net/ – 2009-06-18 07:53:47

0

我會去FreePascal。它本地編譯爲windows/mac/linux,因此你不需要依賴任何其他框架(.net,java,air)來安裝。只有一個可執行文件就是這樣。

+2

好吧,而不是「你想支持的每個單一平臺只有一個單一的可執行文件」,可能只有一組源代碼以及系統託管的本地API圖標是一切,但標準化。 – Fredrik 2009-06-16 19:30:51

0

我同意James:如果您在Flex中擁有投資和專有技術,那麼使用Air可以擴展這一點。

至於有效載荷 - 如果你只是需要不時彈出通知,RSS是要走的路。否則,由於它很容易設置,所以您可以推出自己的類似於XML REST的服務,並且可以長期爲您提供靈活性。