1 19 20 package swingwtx.custom; 21 22 import swingwtx.swing.*; 23 24 import org.eclipse.swt.*; 25 import org.eclipse.swt.widgets.*; 26 27 40 public class JTaskTrayItem extends JComponent { 41 42 protected TrayItem ppeer = null; 43 protected Icon pIcon = null; 44 protected JPopupMenu popup = null; 45 46 public JTaskTrayItem() { this(null); } 47 public JTaskTrayItem(Icon icon) { this(icon, ""); } 48 public JTaskTrayItem(Icon icon, String tooltiptext) { this(icon, tooltiptext, null); } 49 public JTaskTrayItem(Icon icon, String tooltiptext, JPopupMenu menu) { 50 super(); 51 pIcon = icon; 52 pToolTipText = tooltiptext; 53 popup = menu; 54 } 55 56 60 public void setIcon(Icon icon) { 61 pIcon = icon; 62 final JTaskTrayItem pthis = this; 63 if (ppeer != null) 65 SwingUtilities.invokeSync(new Runnable () { 66 public void run() { 67 ppeer.setImage( SwingWTUtils.getSWTImageFromSwingIcon(pthis, pIcon) ); 68 } 69 }); 70 } 71 72 73 77 public void setToolTipText(String text) { 78 pToolTipText = text; 79 if (ppeer != null) 81 SwingUtilities.invokeSync(new Runnable () { 82 public void run() { 83 ppeer.setToolTipText(pToolTipText); 84 } 85 }); 86 } 87 88 92 public String getToolTipText() { 93 return pToolTipText; 94 } 95 96 100 public void setPopupMenu(JPopupMenu menu) { 101 popup = menu; 102 } 103 104 108 public JPopupMenu getPopupMenu() { 109 return popup; 110 } 111 112 116 public void setVisible(final boolean b) { 117 118 final JTaskTrayItem pthis = this; 119 SwingUtilities.invokeSync(new Runnable () { 120 public void run() { 121 122 if (b) { 123 if (ppeer != null) 124 ppeer.dispose(); 125 ppeer = new TrayItem(SwingWTUtils.getDisplay().getSystemTray(), SWT.NONE); 126 if (pIcon != null) ppeer.setImage( SwingWTUtils.getSWTImageFromSwingIcon(pthis, pIcon) ); 127 if (!pToolTipText.equals("")) ppeer.setToolTipText(pToolTipText); 128 registerEvents(); 129 } 130 else 131 { 132 if (ppeer != null) { 133 ppeer.dispose(); 134 ppeer = null; 135 } 136 } 137 138 } 139 }); 140 141 } 142 143 public void registerEvents() { 144 145 ppeer.addListener (SWT.Selection, new Listener () { 147 public void handleEvent (Event event) { 148 processActionEvent(0); 149 } 150 }); 151 152 ppeer.addListener (SWT.MenuDetect, new Listener () { 154 public void handleEvent (Event event) { 155 156 boolean needToCreate = popup == null; 158 if (popup.getSWTMenu() == null) 159 needToCreate = true; 160 if (needToCreate) 161 popup.setSwingWTParent( JOptionPane.getSharedFrame() ); 164 165 popup.getSWTMenu().setVisible(true); 167 } 168 }); 169 170 } 171 172 173 public void show() { setVisible(true); } 174 175 public void hide() { setVisible(false); } 176 177 } 178 | Popular Tags |