From 124e4e90e495350c68ca6fbd85d651252a2956fd Mon Sep 17 00:00:00 2001 From: Damian Minkov Date: Mon, 14 Apr 2014 14:55:20 +0300 Subject: [PATCH] Optimizes account info plugin, do not load panels and listeners on startup, load them on demand. --- .../accountinfo/AccountDetailsPanel.java | 26 +++- .../AccountInfoMenuItemComponent.java | 89 ++++++++----- .../plugin/accountinfo/AccountInfoPanel.java | 121 +++++++++++------- 3 files changed, 151 insertions(+), 85 deletions(-) diff --git a/src/net/java/sip/communicator/plugin/accountinfo/AccountDetailsPanel.java b/src/net/java/sip/communicator/plugin/accountinfo/AccountDetailsPanel.java index d942b7828..afbb91bef 100644 --- a/src/net/java/sip/communicator/plugin/accountinfo/AccountDetailsPanel.java +++ b/src/net/java/sip/communicator/plugin/accountinfo/AccountDetailsPanel.java @@ -22,6 +22,7 @@ import net.java.sip.communicator.service.globaldisplaydetails.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.service.protocol.ServerStoredDetails.*; +import net.java.sip.communicator.plugin.accountinfo.AccountInfoMenuItemComponent.*; import net.java.sip.communicator.util.Logger; @@ -184,14 +185,22 @@ public class AccountDetailsPanel private JScrollPane mainScrollPane; + /** + * The parent dialog. + */ + private AccountInfoDialog dialog; + /** * Construct a panel containing all account details for the given protocol * provider. * * @param protocolProvider the protocol provider service */ - public AccountDetailsPanel(ProtocolProviderService protocolProvider) + public AccountDetailsPanel(AccountInfoDialog dialog, + ProtocolProviderService protocolProvider) { + this.dialog = dialog; + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setOpaque(false); this.setPreferredSize(new Dimension(600, 400)); @@ -645,7 +654,7 @@ public void replace(FilterBypass fb, int offs, @Override public void actionPerformed(ActionEvent evt) { - AccountInfoMenuItemComponent.dialog.setVisible(false); + dialog.close(false); mainScrollPane.getVerticalScrollBar().setValue(0); } }); @@ -829,6 +838,15 @@ else if (detail.getClass().equals(AboutMeDetail.class)) } } + /** + * Returns the provider we represent. + * @return + */ + public ProtocolProviderService getProtocolProvider() + { + return protocolProvider; + } + /** * Attempts to upload all ServerStoredDetails on the server using * OperationSetServerStoredAccountInfo @@ -1157,8 +1175,8 @@ public void actionPerformed(ActionEvent e) try { - AccountInfoMenuItemComponent.dialog.setVisible(false); - mainScrollPane.getVerticalScrollBar().setValue(0); + dialog.close(false); + //mainScrollPane.getVerticalScrollBar().setValue(0); accountInfoOpSet.save(); } catch (OperationFailedException e1) diff --git a/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoMenuItemComponent.java b/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoMenuItemComponent.java index 5ed03facc..f545c0b17 100644 --- a/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoMenuItemComponent.java +++ b/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoMenuItemComponent.java @@ -24,32 +24,12 @@ public class AccountInfoMenuItemComponent /** * The "Account Info" menu item. */ - JMenuItem accountInfoMenuItem; + private JMenuItem accountInfoMenuItem; /** - * The dialog that appears when "Account Info" menu item is clicked. + * Currently set account id if any. */ - static final SIPCommDialog dialog = new SIPCommDialog() { - /** - * Serial version UID. - */ - private static final long serialVersionUID = 1L; - - /** - * Presses programmatically the cancel button, when Esc key is pressed. - * - * @param isEscaped indicates if the Esc button was pressed on close - */ - protected void close(boolean isEscaped) - { - this.setVisible(false); - } - }; - - /** - * The main panel containing account information. - */ - static final AccountInfoPanel accountInfoPanel = new AccountInfoPanel(); + private AccountID accountID = null; /** * Initializes a new "Account Info" menu item. @@ -60,20 +40,14 @@ public AccountInfoMenuItemComponent(Container container, PluginComponentFactory parentFactory) { super(container, parentFactory); - - AccountInfoActivator.bundleContext.addServiceListener(accountInfoPanel); - - dialog.setPreferredSize(new java.awt.Dimension(600, 400)); - dialog.setTitle(Resources.getString("plugin.accountinfo.TITLE")); - dialog.add(accountInfoPanel); } public void setCurrentAccountID(AccountID accountID) { + this.accountID = accountID; + accountInfoMenuItem.setEnabled( accountID != null && accountID.isEnabled()); - accountInfoPanel.getAccountsComboBox().setSelectedItem( - accountInfoPanel.getAccountsTable().get(accountID)); } /** @@ -97,8 +71,10 @@ public Object getComponent() { public void actionPerformed(ActionEvent e) { + AccountInfoDialog dialog + = new AccountInfoDialog(accountID); + dialog.setVisible(true); - accountInfoPanel.setVisible(true); } }); } @@ -129,4 +105,53 @@ public int getPositionIndex() { return 0; } + + /** + * The dialog that appears when "Account Info" menu item is clicked. + */ + static class AccountInfoDialog + extends SIPCommDialog + { + private AccountInfoPanel accountInfoPanel; + + private AccountInfoDialog(AccountID accountID) + { + this.accountInfoPanel = new AccountInfoPanel(this); + + this.setPreferredSize(new java.awt.Dimension(600, 400)); + this.setTitle(Resources.getString("plugin.accountinfo.TITLE")); + + if(accountID != null) + { + accountInfoPanel.getAccountsComboBox().setSelectedItem( + accountInfoPanel.getAccountsTable().get(accountID)); + } + + this.add(accountInfoPanel); + } + + /** + * Presses programmatically the cancel button, when Esc key is pressed. + * + * @param isEscaped indicates if the Esc button was pressed on close + */ + @Override + protected void close(boolean isEscaped) + { + this.setVisible(false); + + accountInfoPanel.dispose(); + } + + @Override + public void setVisible(boolean isVisible) + { + if(isVisible) + { + accountInfoPanel.setVisible(true); + } + + super.setVisible(isVisible); + } + } } diff --git a/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoPanel.java b/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoPanel.java index 86fdbe30b..f6ba17ca8 100644 --- a/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoPanel.java +++ b/src/net/java/sip/communicator/plugin/accountinfo/AccountInfoPanel.java @@ -14,6 +14,7 @@ import net.java.sip.communicator.plugin.desktoputil.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.service.protocol.event.*; +import net.java.sip.communicator.plugin.accountinfo.AccountInfoMenuItemComponent.*; import org.osgi.framework.*; @@ -26,7 +27,8 @@ */ public class AccountInfoPanel extends TransparentPanel - implements ServiceListener + implements ServiceListener, + RegistrationStateChangeListener { /** * Serial version UID. @@ -58,13 +60,20 @@ public class AccountInfoPanel accountsTable = new HashMap(); + /** + * The parent dialog. + */ + private AccountInfoDialog dialog; + /** * Creates an instance of AccountInfoPanel that contains combo box * component with active user accounts and AccountDetailsPanel to * display and edit account information. */ - public AccountInfoPanel() + public AccountInfoPanel(AccountInfoDialog dialog) { + this.dialog = dialog; + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); accountsComboBox = new JComboBox(); @@ -112,8 +121,13 @@ public void itemStateChanged(ItemEvent e) add(centerPanel); } + /** + * Initialize. + */ private void init() { + AccountInfoActivator.bundleContext.addServiceListener(this); + for (ProtocolProviderFactory providerFactory : AccountInfoActivator .getProtocolProviderFactories().values()) { @@ -130,19 +144,34 @@ private void init() protocolProvider = (ProtocolProviderService)AccountInfoActivator .bundleContext.getService(serRef); - currentDetailsPanel = new AccountDetailsPanel(protocolProvider); + currentDetailsPanel = new AccountDetailsPanel( + dialog, + protocolProvider); accountsTable.put( protocolProvider.getAccountID(), currentDetailsPanel); accountsComboBox.addItem(currentDetailsPanel); - protocolProvider.addRegistrationStateChangeListener( - new RegistrationStateChangeListenerImpl()); + protocolProvider.addRegistrationStateChangeListener(this); } } } + /** + * Clears all listeners. + */ + public void dispose() + { + AccountInfoActivator.bundleContext.removeServiceListener(this); + + for(AccountDetailsPanel pan : accountsTable.values()) + { + pan.getProtocolProvider() + .removeRegistrationStateChangeListener(this); + } + } + /** * A custom renderer to display properly AccountDetailsPanel * in a combo box. @@ -179,57 +208,52 @@ public Component getListCellRendererComponent( } } - private class RegistrationStateChangeListenerImpl - implements RegistrationStateChangeListener + public void registrationStateChanged(final RegistrationStateChangeEvent evt) { - public void registrationStateChanged( - final RegistrationStateChangeEvent evt) + if(!SwingUtilities.isEventDispatchThread()) { - if(!SwingUtilities.isEventDispatchThread()) + SwingUtilities.invokeLater(new Runnable() { - SwingUtilities.invokeLater(new Runnable() + public void run() { - public void run() - { - registrationStateChanged(evt); - } - }); - return; - } + registrationStateChanged(evt); + } + }); + return; + } - ProtocolProviderService protocolProvider = evt.getProvider(); + ProtocolProviderService protocolProvider = evt.getProvider(); - if (evt.getNewState() == RegistrationState.REGISTERED) + if (evt.getNewState() == RegistrationState.REGISTERED) + { + if (accountsTable.containsKey(protocolProvider.getAccountID())) { - if (accountsTable.containsKey(protocolProvider.getAccountID())) - { - AccountDetailsPanel detailsPanel - = accountsTable.get(protocolProvider.getAccountID()); - detailsPanel.loadDetails(); - } - else - { - AccountDetailsPanel panel = - new AccountDetailsPanel(protocolProvider); - accountsTable.put(protocolProvider.getAccountID(), panel); - accountsComboBox.addItem(panel); - } + AccountDetailsPanel detailsPanel + = accountsTable.get(protocolProvider.getAccountID()); + detailsPanel.loadDetails(); } - else if (evt.getNewState() == RegistrationState.UNREGISTERING) + else { - AccountDetailsPanel panel - = accountsTable.get(protocolProvider.getAccountID()); - if (panel != null) + AccountDetailsPanel panel = + new AccountDetailsPanel(dialog, protocolProvider); + accountsTable.put(protocolProvider.getAccountID(), panel); + accountsComboBox.addItem(panel); + } + } + else if (evt.getNewState() == RegistrationState.UNREGISTERING) + { + AccountDetailsPanel panel + = accountsTable.get(protocolProvider.getAccountID()); + if (panel != null) + { + accountsTable.remove(protocolProvider.getAccountID()); + accountsComboBox.removeItem(panel); + if (currentDetailsPanel == panel) { - accountsTable.remove(protocolProvider.getAccountID()); - accountsComboBox.removeItem(panel); - if (currentDetailsPanel == panel) - { - currentDetailsPanel = null; - centerPanel.removeAll(); - centerPanel.revalidate(); - centerPanel.repaint(); - } + currentDetailsPanel = null; + centerPanel.removeAll(); + centerPanel.revalidate(); + centerPanel.repaint(); } } } @@ -276,11 +300,10 @@ public void run() if (accountsTable.get(protocolProvider.getAccountID()) == null) { AccountDetailsPanel panel = - new AccountDetailsPanel(protocolProvider); + new AccountDetailsPanel(dialog, protocolProvider); accountsTable.put(protocolProvider.getAccountID(), panel); accountsComboBox.addItem(panel); - protocolProvider.addRegistrationStateChangeListener( - new RegistrationStateChangeListenerImpl()); + protocolProvider.addRegistrationStateChangeListener(this); } } // If the protocol provider is being unregistered we have to remove