document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.AWTException;  
  2. import java.awt.Image;  
  3. import java.awt.MenuItem;  
  4. import java.awt.PopupMenu;  
  5. import java.awt.SystemTray;  
  6. import java.awt.TrayIcon;  
  7. import java.awt.event.ActionEvent;  
  8. import java.awt.event.ActionListener;  
  9. import java.awt.event.MouseEvent;  
  10. import java.awt.event.MouseListener;  
  11. import java.util.logging.Level;  
  12. import java.util.logging.Logger;  
  13. import javax.swing.ImageIcon;  
  14. import javax.swing.JFrame;  
  15. import javax.swing.JOptionPane;  
  16.  
  17. /**
  18. *
  19. * @author Charleston Anjos
  20. * @version 1.0 - 2013
  21. */  
  22. public class TaskBar {  
  23.  
  24.     //Instance new SystemTray  
  25.     private static SystemTray Tray = SystemTray.getSystemTray();  
  26.     private static TrayIcon TrayIconBar;  
  27.  
  28.     //variables  
  29.     private static String IconMain;  
  30.     private static JFrame frame;  
  31.     private static MouseListener mlOpcoes;  
  32.     private static String NameTask;  
  33.  
  34.     //variables the alteration image  
  35.     private static boolean AlterCountTray = false;      
  36.  
  37.     /**
  38.      *
  39.      * @param icon
  40.      * @param frame
  41.      * @param nameTask  
  42.      */  
  43.     public TaskBar(String icon, JFrame frame,String nameTask){  
  44.         this.IconMain = icon;  
  45.         this.frame = frame;  
  46.         this.NameTask = nameTask;  
  47.     }  
  48.  
  49.     public void instanceTask(){                  
  50.  
  51.         //alter image and instance task  
  52.         alterImageTray(IconMain);  
  53.  
  54.         //add events mouse  
  55.         //initEventsMouse();  
  56.  
  57.         //add icon in SystemTray  
  58.         try {            
  59.             Tray.add(TrayIconBar);  
  60.         } catch (AWTException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.  
  64.         AlterCountTray = true;  
  65.  
  66.     }  
  67.  
  68.     /**
  69.      *
  70.      * @param image
  71.      */  
  72.     public void alterImageTray(String image){  
  73.         Image imageIcon = new ImageIcon(  
  74.                 (this.getClass()  
  75.                      .getClassLoader()  
  76.                      .getResource(image + ".png")))//image get param  
  77.                      .getImage();  
  78.  
  79.         //if not exist instance create new  
  80.         if (this.AlterCountTray==false){  
  81.             TrayIconBar = new TrayIcon(imageIcon,NameTask,initPopUpMenu());  
  82.         }else{  
  83.             TrayIconBar.setImage(imageIcon);  
  84.         }  
  85.  
  86.         //image auto resize  
  87.         TrayIconBar.setImageAutoSize(true);  
  88.     }  
  89.  
  90.     /**
  91.      *
  92.      * @param imageOne
  93.      * @param imageTwo
  94.      * @param time
  95.      */  
  96.     //used with timerbean  
  97.     public void useSequenceImage(String imageOne, String imageTwo, int time){  
  98.         try {  
  99.  
  100.             alterImageTray(imageOne); //image footprint parameter changes  
  101.  
  102.             Thread.sleep(time); //time for change  
  103.  
  104.             alterImageTray(imageTwo); //image footprint parameter changes  
  105.              
  106.         } catch (InterruptedException ex) {  
  107.             Logger.getLogger(TaskBar.class.getName()).log(Level.SEVERE, null, ex);  
  108.         }  
  109.     }  
  110.  
  111.     private void initEventsMouse(){  
  112.         //instantiates a mouse listener for use user be in TrayIcon  
  113.             mlOpcoes = new MouseListener(){  
  114.  
  115.                 public void mouseClicked(MouseEvent e) {  
  116. //                     frame.setVisible(true);  
  117. //                     frame.setExtendedState(JFrame.MAXIMIZED_BOTH);  
  118.                 }  
  119.  
  120.                 public void mousePressed(MouseEvent e) {}  
  121.  
  122.                 public void mouseReleased(MouseEvent e) {  
  123.                     /*  if the mouse is clicked with the wheel mouse or with
  124.                      *  button right close the application  
  125.                     if(e.getButton() == 2 || e.getButton() == 3){
  126.                         //System.exit(0);
  127.                     }*/  
  128.                 }  
  129.  
  130.                 public void mouseEntered(MouseEvent e) {}  
  131.  
  132.                 public void mouseExited(MouseEvent e) {}  
  133.  
  134.             };  
  135.  
  136.          //add mouseListener in TrayIcon  
  137.         TrayIconBar.addMouseListener(mlOpcoes);  
  138.     }  
  139.  
  140.     /**
  141.      * @return PopUp
  142.      */  
  143.     private PopupMenu initPopUpMenu() {  
  144.  
  145.         PopupMenu popup = new PopupMenu();  
  146.          
  147.         MenuItem miAbout = new MenuItem("Sobre");  
  148.         miAbout.addActionListener(new ActionListener() {  
  149.             public void actionPerformed(ActionEvent e) {  
  150.                 JOptionPane.showMessageDialog(null, "TASK BAR\\n"  
  151.                         + "Versão 1.0\\n\\n"  
  152.                         + "Desenvolvido por Charleston Anjos\\n");  
  153.                          
  154.             }  
  155.         });  
  156.  
  157.         MenuItem miReturn = new MenuItem("Voltar");  
  158.         miReturn.addActionListener(new ActionListener() {  
  159.             public void actionPerformed(ActionEvent e) {  
  160.                 frame.setVisible(true);  
  161.                 frame.setExtendedState(JFrame.MAXIMIZED_BOTH);  
  162.             }  
  163.         });  
  164.  
  165.         MenuItem miExit = new MenuItem("Sair");  
  166.         miExit.addActionListener(new ActionListener() {  
  167.             public void actionPerformed(ActionEvent e) {                  
  168.                 System.exit(0);  
  169.             }  
  170.         });  
  171.          
  172.         popup.add(miAbout);  
  173.         popup.addSeparator();  
  174.         popup.add(miReturn);  
  175.         popup.addSeparator();  
  176.         popup.add(miExit);  
  177.  
  178.         return popup;  
  179.     }  
  180.  
  181.     /**
  182.      *
  183.      * @param title
  184.      * @param message
  185.      * @param type
  186.      */  
  187.     public void displayMessageTask(String title, String message, int type){  
  188.  
  189.         String style = "";  
  190.  
  191.         if (type==0) style = "NONE";  
  192.         if (type==1) style = "INFO";  
  193.         if (type==2) style = "ERROR";  
  194.         if (type==3) style = "WARNING";  
  195.  
  196.         TrayIconBar.displayMessage(title, message,TrayIcon.MessageType.valueOf(style));  
  197.     }      
  198. }
');