public static void runInUI(@Nullable Shell shell, @NotNull Runnable runnable) { final Display display = shell == null || shell.isDisposed() ? Display.getDefault() : shell.getDisplay(); if (display.getThread() != Thread.currentThread()) { display.syncExec(runnable); } else { runnable.run(); } }
/** Tray icon handler based on SWT */ public class SwtGatewayTray implements DavGatewayTrayInterface { protected SwtGatewayTray() {} SettingsFrame settingsFrame; AboutFrame aboutFrame; private static TrayItem trayItem; private static java.awt.Image awtImage; private static Image image; private static Image image2; private static Image inactiveImage; private static Display display; private static Shell shell; private LogBrokerMonitor logBrokerMonitor; private boolean isActive = true; private boolean isReady; private final Thread mainThread = Thread.currentThread(); /** * Return AWT Image icon for frame title. * * @return frame icon */ public java.awt.Image getFrameIcon() { return awtImage; } /** Switch tray icon between active and standby icon. */ public void switchIcon() { isActive = true; display.syncExec( new Runnable() { public void run() { if (trayItem.getImage().equals(image)) { trayItem.setImage(image2); } else { trayItem.setImage(image); } } }); } /** Set tray icon to inactive (network down) */ public void resetIcon() { display.syncExec( new Runnable() { public void run() { trayItem.setImage(image); } }); } /** Set tray icon to inactive (network down) */ public void inactiveIcon() { isActive = false; display.syncExec( new Runnable() { public void run() { trayItem.setImage(inactiveImage); } }); } /** * Check if current tray status is inactive (network down). * * @return true if inactive */ public boolean isActive() { return isActive; } /** * Log and display balloon message according to log level. * * @param message text message * @param level log level */ public void displayMessage(final String message, final Level level) { if (trayItem != null) { display.asyncExec( new Runnable() { public void run() { int messageType = 0; if (level.equals(Level.INFO)) { messageType = SWT.ICON_INFORMATION; } else if (level.equals(Level.WARN)) { messageType = SWT.ICON_WARNING; } else if (level.equals(Level.ERROR)) { messageType = SWT.ICON_ERROR; } if (messageType != 0) { final ToolTip toolTip = new ToolTip(shell, SWT.BALLOON | messageType); toolTip.setText(BundleMessage.format("UI_DAVMAIL_GATEWAY")); toolTip.setMessage(message); trayItem.setToolTip(toolTip); toolTip.setVisible(true); } trayItem.setToolTipText(BundleMessage.format("UI_DAVMAIL_GATEWAY") + '\n' + message); } }); } } /** * Load image with current class loader. * * @param fileName image resource file name * @return image */ public static Image loadSwtImage(String fileName) { Image result = null; try { ClassLoader classloader = DavGatewayTray.class.getClassLoader(); URL imageUrl = classloader.getResource(fileName); result = new Image(display, imageUrl.openStream()); } catch (IOException e) { DavGatewayTray.warn(new BundleMessage("LOG_UNABLE_TO_LOAD_IMAGE"), e); } return result; } /** Create tray icon and register frame listeners. */ public void init() { // register error handler to avoid application crash on concurrent X access from SWT and AWT try { OS.gdk_error_trap_push(); } catch (NoClassDefFoundError e) { // ignore } final String systemLookAndFeelClassName = UIManager.getSystemLookAndFeelClassName(); try { // workaround for bug when SWT and AWT both try to access Gtk if (systemLookAndFeelClassName.indexOf("gtk") >= 0) { System.setProperty("swing.defaultlaf", UIManager.getCrossPlatformLookAndFeelClassName()); } else { System.setProperty("swing.defaultlaf", systemLookAndFeelClassName); } } catch (Exception e) { DavGatewayTray.warn(new BundleMessage("LOG_UNABLE_TO_SET_LOOK_AND_FEEL")); } new Thread("SWT") { @Override public void run() { try { display = new Display(); shell = new Shell(display); final Tray tray = display.getSystemTray(); if (tray != null) { trayItem = new TrayItem(tray, SWT.NONE); trayItem.setToolTipText(BundleMessage.format("UI_DAVMAIL_GATEWAY")); awtImage = DavGatewayTray.loadImage(AwtGatewayTray.TRAY_PNG); image = loadSwtImage(AwtGatewayTray.TRAY_PNG); image2 = loadSwtImage(AwtGatewayTray.TRAY_ACTIVE_PNG); inactiveImage = loadSwtImage(AwtGatewayTray.TRAY_INACTIVE_PNG); trayItem.setImage(image); // create a popup menu final Menu popup = new Menu(shell, SWT.POP_UP); trayItem.addListener( SWT.MenuDetect, new Listener() { public void handleEvent(Event event) { display.asyncExec( new Runnable() { public void run() { popup.setVisible(true); } }); } }); MenuItem aboutItem = new MenuItem(popup, SWT.PUSH); aboutItem.setText(BundleMessage.format("UI_ABOUT")); aboutItem.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { display.asyncExec( new Runnable() { public void run() { if (aboutFrame == null) { aboutFrame = new AboutFrame(); } aboutFrame.update(); aboutFrame.setVisible(true); aboutFrame.toFront(); aboutFrame.requestFocus(); } }); } }); trayItem.addListener( SWT.DefaultSelection, new Listener() { public void handleEvent(Event event) { display.asyncExec( new Runnable() { public void run() { // create frame on first call if (settingsFrame == null) { settingsFrame = new SettingsFrame(); } settingsFrame.reload(); settingsFrame.setVisible(true); settingsFrame.toFront(); settingsFrame.requestFocus(); } }); } }); // create menu item for the default action MenuItem defaultItem = new MenuItem(popup, SWT.PUSH); defaultItem.setText(BundleMessage.format("UI_SETTINGS")); defaultItem.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { display.asyncExec( new Runnable() { public void run() { // create frame on first call if (settingsFrame == null) { settingsFrame = new SettingsFrame(); } settingsFrame.reload(); settingsFrame.setVisible(true); settingsFrame.toFront(); settingsFrame.requestFocus(); } }); } }); MenuItem logItem = new MenuItem(popup, SWT.PUSH); logItem.setText(BundleMessage.format("UI_SHOW_LOGS")); logItem.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { display.asyncExec( new Runnable() { public void run() { Logger rootLogger = Logger.getRootLogger(); LF5Appender lf5Appender = (LF5Appender) rootLogger.getAppender("LF5Appender"); if (lf5Appender == null) { logBrokerMonitor = new LogBrokerMonitor(LogLevel.getLog4JLevels()) { @Override protected void closeAfterConfirm() { hide(); } }; lf5Appender = new LF5Appender(logBrokerMonitor); lf5Appender.setName("LF5Appender"); rootLogger.addAppender(lf5Appender); } lf5Appender.getLogBrokerMonitor().show(); } }); } }); MenuItem exitItem = new MenuItem(popup, SWT.PUSH); exitItem.setText(BundleMessage.format("UI_EXIT")); exitItem.addListener( SWT.Selection, new Listener() { public void handleEvent(Event event) { DavGateway.stop(); } }); // display settings frame on first start if (Settings.isFirstStart()) { // create frame on first call if (settingsFrame == null) { settingsFrame = new SettingsFrame(); } settingsFrame.setVisible(true); settingsFrame.toFront(); settingsFrame.requestFocus(); } synchronized (mainThread) { // ready isReady = true; mainThread.notifyAll(); } while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } dispose(); } } catch (Exception exc) { DavGatewayTray.error(exc); } // make sure we do exit System.exit(0); } }.start(); while (true) { // wait for SWT init try { synchronized (mainThread) { if (isReady) { break; } mainThread.wait(1000); } } catch (InterruptedException e) { DavGatewayTray.error(new BundleMessage("LOG_ERROR_WAITING_FOR_SWT_INIT"), e); } } } public void dispose() { shell.dispose(); if (trayItem != null) { trayItem.dispose(); trayItem = null; } if (image != null) { image.dispose(); } if (image2 != null) { image2.dispose(); } try { if (!display.isDisposed()) { display.dispose(); } } catch (Exception e) { // already disposed } // dispose AWT frames if (settingsFrame != null) { settingsFrame.dispose(); } if (aboutFrame != null) { aboutFrame.dispose(); } if (logBrokerMonitor != null) { logBrokerMonitor.dispose(); } } }
public synchronized void createDialog() { Composite c = this.getContents(); prepareDialog(c, getShell()); final Color color = new Color(getShell().getDisplay(), 255, 255, 225); // Buttons Composite buttonBar = new Composite(c, SWT.NONE); GridData gd = new GridData(); gd.horizontalSpan = this.m_columnCount; // check for plugins List plugins = this.getPlugins(this.getConfiguration().getProperty("pluginlist", "")); GridLayout gl = new GridLayout(Math.max(5, (plugins.size() + 2)), false); gl.marginRight = 20; buttonBar.setLayout(gl); buttonBar.setLayoutData(gd); buttonBar.setBackground(color); // check for active reject service IService rejectService = this.getRuntime().getServiceFactory().getService("Reject"); if (rejectService != null && rejectService.isEnabled()) { final HyperLink reject = new HyperLink(buttonBar, SWT.LEFT); reject.setBackground(color); reject.setText( this.getI18nManager() .getString(this.getNamespace(), "reject", "label", this.getLanguage())); reject.pack(); reject.addMouseListener( new MouseAdapter() { public void mouseDown(MouseEvent e) { if (e.button == 1) { reject.setEnabled(false); reject.setText( getI18nManager().getString(getNamespace(), "rejected", "label", getLanguage())); reject.setUnderline(color); reject.setActiveUnderline(color); reject.setHoverUnderline(color); reject.pack(true); IEventBroker eventBroker = getRuntime().getEventBroker(); if (m_call != null) m_call.setAttribute( getRuntime() .getCallFactory() .createAttribute( IJAMConst.ATTRIBUTE_NAME_CALLSTATUS, IJAMConst.ATTRIBUTE_VALUE_REJECTED)); eventBroker.send( ExtendedBalloonDialog.this, eventBroker.createEvent(IEventConst.EVENT_TYPE_CALLREJECTED, m_call)); } } }); } if (this.isAssignement() && !this.isCliredCaller()) { final IDialogPlugin assignPlugin = new AssignPlugin(); assignPlugin.setDialog(this); HyperLink hl = new HyperLink(buttonBar, SWT.LEFT); hl.setText(assignPlugin.getLabel()); hl.setBackground(color); hl.pack(); hl.addMouseListener( new MouseAdapter() { public void mouseDown(MouseEvent e) { if (e.button == 1) { assignPlugin.run(); } } }); } // add plugins String classString = null; for (int i = 0, j = plugins.size(); i < j; i++) { classString = this.getConfiguration().getProperty((String) plugins.get(i)); if (classString != null && classString.trim().length() > 0) { try { Class classObject = Thread.currentThread().getContextClassLoader().loadClass(classString); final IDialogPlugin plugin = (IDialogPlugin) classObject.newInstance(); plugin.setDialog(this); plugin.setID((String) plugins.get(i)); if (plugin.isEnabled()) { HyperLink hl = new HyperLink(buttonBar, SWT.LEFT); hl.setText(plugin.getLabel()); hl.setBackground(color); hl.pack(); hl.addMouseListener( new MouseAdapter() { public void mouseDown(MouseEvent e) { if (e.button == 1) { plugin.run(); } } }); } } catch (ClassNotFoundException e) { this.m_logger.warning("Class not found: " + classString); } catch (InstantiationException e) { this.m_logger.log(Level.SEVERE, e.getMessage(), e); } catch (IllegalAccessException e) { this.m_logger.log(Level.SEVERE, e.getMessage(), e); } } } color.dispose(); c.pack(); }
protected String[] getAuthenticationDialog(final String realm, final String location) { final Display display = SWTThread.getInstance().getDisplay(); if (display.isDisposed()) { return (null); } final AESemaphore sem = new AESemaphore("SWTAuth"); final authDialog[] dialog = new authDialog[1]; TOTorrent torrent = TorrentUtils.getTLSTorrent(); final boolean is_tracker; final String details; if (torrent == null) { is_tracker = false; details = TorrentUtils.getTLSDescription(); } else { details = TorrentUtils.getLocalisedName(torrent); is_tracker = true; } try { if (display.getThread() == Thread.currentThread()) { dialog[0] = new authDialog(sem, display, realm, is_tracker, location, details); while (!(display.isDisposed() || sem.isReleasedForever())) { if (!display.readAndDispatch()) { display.sleep(); } } if (display.isDisposed()) { return (null); } } else { display.asyncExec( new AERunnable() { public void runSupport() { dialog[0] = new authDialog(sem, display, realm, is_tracker, location, details); } }); } } catch (Throwable e) { Debug.printStackTrace(e); return (null); } sem.reserve(); String user = dialog[0].getUsername(); String pw = dialog[0].getPassword(); String persist = dialog[0].savePassword() ? "true" : "false"; if (user == null) { return (null); } return (new String[] {user, pw == null ? "" : pw, persist}); }