From f7210c1748950fdbacd1db5e5e758effe0fb217f Mon Sep 17 00:00:00 2001 From: Lyubomir Marinov Date: Fri, 28 Nov 2008 11:39:07 +0000 Subject: [PATCH] Fixes a problem on Mac OS X which causes the MainFrame to not be hidden when it's closed and the dialog which warns that the application will not quit appears. --- .../communicator/impl/gui/GuiActivator.java | 39 ++-- .../gui/customcontrols/MessageDialog.java | 40 ++-- .../communicator/impl/gui/main/MainFrame.java | 155 +++++++------- .../account/AccountsConfigurationForm.java | 48 +---- .../impl/gui/main/login/LoginManager.java | 201 ++++++++---------- 5 files changed, 221 insertions(+), 262 deletions(-) diff --git a/src/net/java/sip/communicator/impl/gui/GuiActivator.java b/src/net/java/sip/communicator/impl/gui/GuiActivator.java index ede53aee8..d239bdef8 100644 --- a/src/net/java/sip/communicator/impl/gui/GuiActivator.java +++ b/src/net/java/sip/communicator/impl/gui/GuiActivator.java @@ -8,8 +8,6 @@ import java.util.*; -import javax.swing.*; - import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.service.audionotifier.*; import net.java.sip.communicator.service.browserlauncher.*; @@ -62,7 +60,8 @@ public class GuiActivator implements BundleActivator private static KeybindingsService keybindingsService; - private static Map providerFactoriesMap = new Hashtable(); + private static final Map providerFactoriesMap = + new Hashtable(); public static boolean isStarted = false; @@ -85,12 +84,12 @@ public void start(BundleContext bundleContext) try { // Create the ui service - this.uiService = new UIServiceImpl(); + uiService = new UIServiceImpl(); logger.info("UI Service...[ STARTED ]"); bundleContext.registerService( UIService.class.getName(), - this.uiService, null); + uiService, null); logger.info("UI Service ...[REGISTERED]"); @@ -133,29 +132,35 @@ public void stop(BundleContext bundleContext) throws Exception /** * Returns all ProtocolProviderFactorys obtained from the bundle * context. + * * @return all ProtocolProviderFactorys obtained from the bundle - * context + * context */ - public static Map getProtocolProviderFactories() { + public static Map getProtocolProviderFactories() + { ServiceReference[] serRefs = null; - try { - //get all registered provider factories - serRefs = bundleContext.getServiceReferences( + try + { + // get all registered provider factories + serRefs = + bundleContext.getServiceReferences( ProtocolProviderFactory.class.getName(), null); - } catch (InvalidSyntaxException e) { + } + catch (InvalidSyntaxException e) + { logger.error("LoginManager : " + e); } - for (int i = 0; i < serRefs.length; i++) { + for (int i = 0; i < serRefs.length; i++) + { - ProtocolProviderFactory providerFactory - = (ProtocolProviderFactory) bundleContext - .getService(serRefs[i]); + ProtocolProviderFactory providerFactory = + (ProtocolProviderFactory) bundleContext.getService(serRefs[i]); - providerFactoriesMap.put(serRefs[i].getProperty( - ProtocolProviderFactory.PROTOCOL), + providerFactoriesMap + .put(serRefs[i].getProperty(ProtocolProviderFactory.PROTOCOL), providerFactory); } diff --git a/src/net/java/sip/communicator/impl/gui/customcontrols/MessageDialog.java b/src/net/java/sip/communicator/impl/gui/customcontrols/MessageDialog.java index 4e049c259..0424271ed 100644 --- a/src/net/java/sip/communicator/impl/gui/customcontrols/MessageDialog.java +++ b/src/net/java/sip/communicator/impl/gui/customcontrols/MessageDialog.java @@ -8,6 +8,7 @@ import java.awt.*; import java.awt.event.*; +import java.lang.reflect.*; import javax.swing.*; @@ -70,8 +71,6 @@ public class MessageDialog * checked. */ public static final int OK_DONT_ASK_CODE = 2; - - private Object lock = new Object(); /** * Creates an instance of MessageDialog by specifying the @@ -222,22 +221,38 @@ public void setMessage(String message) */ public int showDialog() { - this.pack(); - - this.setVisible(true); - - synchronized (lock) + if (!SwingUtilities.isEventDispatchThread()) { + final int[] returnCodes = new int[1]; + Exception exception = null; try { - lock.wait(); + SwingUtilities.invokeAndWait(new Runnable() + { + public void run() + { + returnCodes[0] = showDialog(); + } + }); + } + catch (InterruptedException ex) + { + exception = ex; } - catch (InterruptedException e) + catch (InvocationTargetException ex) { - e.printStackTrace(); + exception = ex; } + if (exception != null) + throw new UndeclaredThrowableException(exception); + return returnCodes[0]; } + pack(); + + setModal(true); + setVisible(true); + return returnCode; } @@ -265,11 +280,6 @@ public void actionPerformed(ActionEvent e) this.returnCode = CANCEL_RETURN_CODE; } - synchronized (lock) - { - lock.notify(); - } - this.dispose(); } diff --git a/src/net/java/sip/communicator/impl/gui/main/MainFrame.java b/src/net/java/sip/communicator/impl/gui/main/MainFrame.java index 3671e6200..b62d53b8f 100755 --- a/src/net/java/sip/communicator/impl/gui/main/MainFrame.java +++ b/src/net/java/sip/communicator/impl/gui/main/MainFrame.java @@ -86,7 +86,8 @@ public class MainFrame providerContactHandlers = new Hashtable(); - private Hashtable nativePluginsTable = new Hashtable(); + private final Map nativePluginsTable = + new Hashtable(); private JPanel pluginPanelNorth = new JPanel(); private JPanel pluginPanelSouth = new JPanel(); @@ -113,7 +114,22 @@ public MainFrame() menu = new MainMenu(this); - this.addWindowListener(new MainFrameWindowAdapter()); + /* + * Before closing the application window saves the current size and + * position through the ConfigurationService. + */ + this.addWindowListener(new WindowAdapter() + { + public void windowClosed(WindowEvent event) + { + MainFrame.this.windowClosed(event); + } + + public void windowClosing(WindowEvent event) + { + MainFrame.this.windowClosing(event); + } + }); this.initTitleFont(); @@ -642,68 +658,6 @@ public MetaContactGroup getGroupByID(String metaUID) .getContactList().getGroupByID(metaUID); } - /** - * Before closing the application window saves the current size and position - * through the ConfigurationService. - */ - public class MainFrameWindowAdapter extends WindowAdapter - { - public void windowClosing(WindowEvent e) - { - if(!GuiActivator.getUIService().getExitOnMainWindowClose()) - { - new Thread() - { - public void run() - { - if(ConfigurationManager.isQuitWarningShown()) - { - MessageDialog dialog - = new MessageDialog( - MainFrame.this, - Messages.getI18NString("close").getText(), - Messages.getI18NString("hideMainWindow") - .getText(), - false); - - int returnCode = dialog.showDialog(); - - if (returnCode == MessageDialog.OK_DONT_ASK_CODE) - { - ConfigurationManager - .setQuitWarningShown(false); - } - } - } - }.start(); - - ConfigurationManager.setApplicationVisible(false); - } - } - - public void windowClosed(WindowEvent e) - { - if(GuiActivator.getUIService().getExitOnMainWindowClose()) - { - try - { - GuiActivator.bundleContext.getBundle(0).stop(); - } - catch (BundleException ex) - { - logger.error("Failed to gently shutdown Felix", ex); - System.exit(0); - } - //stopping a bundle doesn't leave the time to the felix thread to - //properly end all bundles and call their Activator.stop() methods. - //if this causes problems don't uncomment the following line but - //try and see why felix isn't exiting (suggesting: is it running - //in embedded mode?) - //System.exit(0); - } - } - } - /** * Returns the panel containing the ContactList. * @return ContactListPanel the panel containing the ContactList @@ -1178,8 +1132,7 @@ public void pluginComponentRemoved(PluginComponentEvent event) { if (nativePluginsTable.containsKey(pluginComponent)) { - final Component c - = (Component) nativePluginsTable.get(pluginComponent); + final Component c = nativePluginsTable.get(pluginComponent); final Object finalConstraints = constraints; @@ -1273,27 +1226,23 @@ public void paintComponent(Graphics g) */ private void removeNativePlugins() { - Iterator pluginIterator = nativePluginsTable.entrySet().iterator(); - Object constraints; - while (pluginIterator.hasNext()) + for (Map.Entry entry : nativePluginsTable + .entrySet()) { - Map.Entry entry - = (Map.Entry) pluginIterator.next(); + PluginComponent pluginComponent = entry.getKey(); + Component c = entry.getValue(); - PluginComponent pluginComponent = (PluginComponent) entry.getKey(); - Component c = (Component) entry.getValue(); - - constraints = UIServiceImpl - .getBorderLayoutConstraintsFromContainer( - pluginComponent.getConstraints()); + constraints = + UIServiceImpl + .getBorderLayoutConstraintsFromContainer(pluginComponent + .getConstraints()); if (constraints == null) constraints = BorderLayout.SOUTH; - this.removePluginComponent( c, - pluginComponent.getContainer(), - constraints); + this.removePluginComponent(c, pluginComponent.getContainer(), + constraints); this.getContentPane().repaint(); } @@ -1496,6 +1445,52 @@ public String getCurrentPhoneNumber() */ public void setParams(Object[] windowParams) {} + protected void windowClosed(WindowEvent event) + { + if(GuiActivator.getUIService().getExitOnMainWindowClose()) + { + try + { + GuiActivator.bundleContext.getBundle(0).stop(); + } + catch (BundleException ex) + { + logger.error("Failed to gently shutdown Felix", ex); + System.exit(0); + } + //stopping a bundle doesn't leave the time to the felix thread to + //properly end all bundles and call their Activator.stop() methods. + //if this causes problems don't uncomment the following line but + //try and see why felix isn't exiting (suggesting: is it running + //in embedded mode?) + //System.exit(0); + } + } + + protected void windowClosing(WindowEvent event) + { + if (!GuiActivator.getUIService().getExitOnMainWindowClose()) + { + SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + if (ConfigurationManager.isQuitWarningShown()) + { + MessageDialog dialog = + new MessageDialog(null, Messages.getI18NString( + "close").getText(), Messages.getI18NString( + "hideMainWindow").getText(), false); + + if (dialog.showDialog() == MessageDialog.OK_DONT_ASK_CODE) + ConfigurationManager.setQuitWarningShown(false); + } + } + }); + + ConfigurationManager.setApplicationVisible(false); + } + } /** * Initializes the more actions panel. diff --git a/src/net/java/sip/communicator/impl/gui/main/account/AccountsConfigurationForm.java b/src/net/java/sip/communicator/impl/gui/main/account/AccountsConfigurationForm.java index 13be147c5..67af35e72 100644 --- a/src/net/java/sip/communicator/impl/gui/main/account/AccountsConfigurationForm.java +++ b/src/net/java/sip/communicator/impl/gui/main/account/AccountsConfigurationForm.java @@ -15,7 +15,6 @@ import javax.swing.*; import net.java.sip.communicator.impl.gui.*; -import net.java.sip.communicator.impl.gui.customcontrols.*; import net.java.sip.communicator.impl.gui.i18n.*; import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.service.configuration.*; @@ -59,7 +58,8 @@ public class AccountsConfigurationForm private JButton saveButton = new JButton(saveString.getText()); - private Hashtable accounts = new Hashtable(); + private final Map accounts = + new Hashtable(); /** * Creates an instance of AccountsConfigurationForm. @@ -102,30 +102,17 @@ private void accountsInit() this.accountsPanel.setLayout(new BoxLayout(accountsPanel, BoxLayout.Y_AXIS)); - Set set = GuiActivator.getProtocolProviderFactories().entrySet(); - Iterator iter = set.iterator(); - - while (iter.hasNext()) + for (ProtocolProviderFactory providerFactory : GuiActivator + .getProtocolProviderFactories().values()) { - Map.Entry entry = (Map.Entry) iter.next(); - - ProtocolProviderFactory providerFactory = - (ProtocolProviderFactory) entry.getValue(); - - ArrayList accountsList = providerFactory.getRegisteredAccounts(); - - AccountID accountID; ServiceReference serRef; ProtocolProviderService protocolProvider; - for (int i = 0; i < accountsList.size(); i++) + for (AccountID accountID : providerFactory.getRegisteredAccounts()) { - accountID = (AccountID) accountsList.get(i); - boolean isHidden = - (accountID.getAccountProperties() - .get(ProtocolProviderFactory.IS_PROTOCOL_HIDDEN) - != null); + (accountID.getAccountProperties().get( + ProtocolProviderFactory.IS_PROTOCOL_HIDDEN) != null); if (isHidden) continue; @@ -295,24 +282,18 @@ public void actionPerformed(ActionEvent e) String prefix = "net.java.sip.communicator.impl.gui.accounts"; - List accounts = + List accounts = configService .getPropertyNamesByPrefix(prefix, true); - Iterator accountsIter = accounts.iterator(); - - while (accountsIter.hasNext()) + for (String accountRootPropName : accounts) { - String accountRootPropName = - (String) accountsIter.next(); - String accountUID = configService.getString(accountRootPropName); if (accountUID.equals(protocolProvider .getAccountID().getAccountUniqueID())) { - configService.setProperty(accountRootPropName, null); break; @@ -371,7 +352,7 @@ public Object getForm() /** * Handles the ActionEvent triggered when user clicks on on the * buttons. Shows the account registration wizard when user clicks on "New". - * + * * @param evt the action event that has just occurred. */ public void actionPerformed(ActionEvent evt) @@ -387,15 +368,8 @@ public void actionPerformed(ActionEvent evt) } else if (sourceButton.equals(saveButton)) { - Enumeration accountPanels = accounts.elements(); - - while (accountPanels.hasMoreElements()) - { - AccountPanel accountPanel - = (AccountPanel) accountPanels.nextElement(); - + for (AccountPanel accountPanel : accounts.values()) accountPanel.save(); - } } } diff --git a/src/net/java/sip/communicator/impl/gui/main/login/LoginManager.java b/src/net/java/sip/communicator/impl/gui/main/login/LoginManager.java index f4fdad479..bae1cd9b3 100644 --- a/src/net/java/sip/communicator/impl/gui/main/login/LoginManager.java +++ b/src/net/java/sip/communicator/impl/gui/main/login/LoginManager.java @@ -4,11 +4,8 @@ * Distributable under LGPL license. * See terms of license at gnu.org. */ - package net.java.sip.communicator.impl.gui.main.login; -import java.util.*; - import net.java.sip.communicator.impl.gui.*; import net.java.sip.communicator.impl.gui.customcontrols.*; import net.java.sip.communicator.impl.gui.i18n.*; @@ -88,51 +85,31 @@ public void logoff(ProtocolProviderService protocolProvider) */ public void runLogin(MainFrame parent) { - Set set = GuiActivator.getProtocolProviderFactories().entrySet(); - Iterator iter = set.iterator(); - - boolean hasRegisteredAccounts = false; - - while (iter.hasNext()) + for (ProtocolProviderFactory providerFactory : GuiActivator + .getProtocolProviderFactories().values()) { - Map.Entry entry = (Map.Entry) iter.next(); - - ProtocolProviderFactory providerFactory - = (ProtocolProviderFactory) entry.getValue(); - - ArrayList accountsList = providerFactory.getRegisteredAccounts(); - - AccountID accountID; ServiceReference serRef; ProtocolProviderService protocolProvider; - for (int i = 0; i < accountsList.size(); i++) + for (AccountID accountID : providerFactory.getRegisteredAccounts()) { - accountID = (AccountID) accountsList.get(i); - - boolean isHidden = - accountID.getAccountProperties() - .get(ProtocolProviderFactory.IS_PROTOCOL_HIDDEN) != null; - - if(!isHidden) - hasRegisteredAccounts = true; - serRef = providerFactory.getProviderForAccount(accountID); - protocolProvider = (ProtocolProviderService) GuiActivator - .bundleContext.getService(serRef); + protocolProvider = + (ProtocolProviderService) GuiActivator.bundleContext + .getService(serRef); protocolProvider.addRegistrationStateChangeListener(this); this.mainFrame.addProtocolProvider(protocolProvider); - Object status = this.mainFrame - .getProtocolProviderLastStatus(protocolProvider); + Object status = + this.mainFrame + .getProtocolProviderLastStatus(protocolProvider); if (status == null || status.equals(Constants.ONLINE_STATUS) - || ((status instanceof PresenceStatus) - && (((PresenceStatus) status) + || ((status instanceof PresenceStatus) && (((PresenceStatus) status) .getStatus() >= PresenceStatus.ONLINE_THRESHOLD))) { this.login(protocolProvider); @@ -396,9 +373,9 @@ public void setManuallyDisconnected(boolean manuallyDisconnected) private class RegisterProvider extends Thread { - ProtocolProviderService protocolProvider; + private final ProtocolProviderService protocolProvider; - SecurityAuthority secAuth; + private final SecurityAuthority secAuth; RegisterProvider(ProtocolProviderService protocolProvider, SecurityAuthority secAuth) @@ -419,99 +396,97 @@ public void run() } catch (OperationFailedException ex) { - int errorCode = ex.getErrorCode(); + handleOperationFailedException(ex); + } + catch (Throwable ex) + { + logger.error("Failed to register protocol provider. ", ex); - String errorMessage = ""; + AccountID accountID = protocolProvider.getAccountID(); + new ErrorDialog(mainFrame, Messages.getI18NString("error") + .getText(), Messages.getI18NString("loginGeneralError", + new String[] + { accountID.getUserID(), accountID.getService() }) + .getText()).showDialog(); + } + } - if (errorCode == OperationFailedException.GENERAL_ERROR) - { - logger.error("Provider could not be registered" - + " due to the following general error: ", ex); + private void handleOperationFailedException(OperationFailedException ex) + { + String errorMessage = ""; - errorMessage = Messages.getI18NString("loginGeneralError", - new String[]{ - protocolProvider.getAccountID().getUserID(), - protocolProvider.getAccountID().getService() - }).getText(); + switch (ex.getErrorCode()) + { + case OperationFailedException.GENERAL_ERROR: + { + logger.error("Provider could not be registered" + + " due to the following general error: ", ex); - new ErrorDialog(mainFrame, - Messages.getI18NString("error").getText(), - errorMessage, - ex).showDialog(); - } - else if (errorCode == OperationFailedException.INTERNAL_ERROR) - { - logger.error("Provider could not be registered" - + " due to the following internal error: ", ex); + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + Messages.getI18NString("loginGeneralError", new String[] + { accountID.getUserID(), accountID.getService() }) + .getText(); - errorMessage = Messages.getI18NString("loginInternalError", - new String[]{ - protocolProvider.getAccountID().getUserID(), - protocolProvider.getAccountID().getService() - }).getText(); + new ErrorDialog(mainFrame, Messages.getI18NString("error") + .getText(), errorMessage, ex).showDialog(); + } + break; + case OperationFailedException.INTERNAL_ERROR: + { + logger.error("Provider could not be registered" + + " due to the following internal error: ", ex); - new ErrorDialog(mainFrame, - Messages.getI18NString("error").getText(), - errorMessage, - ex).showDialog(); - } - else if (errorCode == OperationFailedException.NETWORK_FAILURE) - { - logger.error("Provider could not be registered" - + " due to a network failure: " + ex); + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + Messages.getI18NString("loginInternalError", new String[] + { accountID.getUserID(), accountID.getService() }) + .getText(); - errorMessage = Messages.getI18NString("loginNetworkError", - new String[]{ - protocolProvider.getAccountID().getUserID(), - protocolProvider.getAccountID().getService() - }).getText(); + new ErrorDialog(mainFrame, Messages.getI18NString("error") + .getText(), errorMessage, ex).showDialog(); + } + break; + case OperationFailedException.NETWORK_FAILURE: + { + logger.error("Provider could not be registered" + + " due to a network failure: " + ex); - int result = new MessageDialog(null, - Messages.getI18NString("error").getText(), - errorMessage, - Messages.getI18NString("retry").getText(), - false).showDialog(); - - if (result == MessageDialog.OK_RETURN_CODE) - { - login(protocolProvider); - } - } - else if (errorCode - == OperationFailedException.INVALID_ACCOUNT_PROPERTIES) - { - logger.error("Provider could not be registered" - + " due to an invalid account property: ", ex); + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + Messages.getI18NString("loginNetworkError", new String[] + { accountID.getUserID(), accountID.getService() }) + .getText(); - errorMessage = Messages.getI18NString("loginInvalidPropsError", - new String[]{ - protocolProvider.getAccountID().getUserID(), - protocolProvider.getAccountID().getService() - }).getText(); + int result = + new MessageDialog(null, Messages.getI18NString("error") + .getText(), errorMessage, Messages.getI18NString( + "retry").getText(), false).showDialog(); - new ErrorDialog(mainFrame, - Messages.getI18NString("error").getText(), - errorMessage, - ex).showDialog(); - } - else + if (result == MessageDialog.OK_RETURN_CODE) { - logger.error("Provider could not be registered.", ex); + login(protocolProvider); } } - catch (Throwable ex) + break; + case OperationFailedException.INVALID_ACCOUNT_PROPERTIES: { - logger.error("Failed to register protocol provider. ", ex); + logger.error("Provider could not be registered" + + " due to an invalid account property: ", ex); - new ErrorDialog( - mainFrame, - Messages.getI18NString("error").getText(), - Messages.getI18NString("loginGeneralError", - new String[]{ - protocolProvider.getAccountID().getUserID(), - protocolProvider.getAccountID().getService() - }).getText()) - .showDialog(); + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + Messages.getI18NString("loginInvalidPropsError", + new String[] + { accountID.getUserID(), accountID.getService() }) + .getText(); + + new ErrorDialog(mainFrame, Messages.getI18NString("error") + .getText(), errorMessage, ex).showDialog(); + } + break; + default: + logger.error("Provider could not be registered.", ex); } } }