Delays the creation of the UI of the keybindingschooser ConfigurationForm in order to save precious startup time and memory and because the UI in question isn't always necessary anyway.

cusax-fix
Lyubomir Marinov 18 years ago
parent 4b8e7fe646
commit 6f04d3bf48

@ -7,9 +7,9 @@
package net.java.sip.communicator.plugin.keybindingchooser;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.keybindings.KeybindingsService;
import net.java.sip.communicator.service.keybindings.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.Logger;
import net.java.sip.communicator.util.*;
import org.osgi.framework.*;
@ -22,7 +22,7 @@ public class KeybindingChooserActivator
implements BundleActivator
{
private static final Logger logger =
Logger.getLogger(KeybindingChooserActivator.class);
Logger.getLogger(KeybindingChooserActivator.class);
private static BundleContext bundleContext;
@ -30,6 +30,7 @@ public class KeybindingChooserActivator
/**
* Called when this bundle is started.
*
* @param context The execution context of the bundle being started.
*/
public void start(BundleContext context)
@ -38,41 +39,43 @@ public void start(BundleContext context)
logger.debug("Service Impl: " + getClass().getName() + " [ STARTED ]");
ServiceReference keybindingRef
= context.getServiceReference(KeybindingsService.class.getName());
ServiceReference keybindingRef =
context.getServiceReference(KeybindingsService.class.getName());
KeybindingsService keybingingsService
= (KeybindingsService) context.getService(keybindingRef);
KeybindingsService keybingingsService =
(KeybindingsService) context.getService(keybindingRef);
KeybindingsConfigForm keybindingsManager =
new KeybindingsConfigForm(keybingingsService);
new KeybindingsConfigForm(keybingingsService);
context.registerService(ConfigurationForm.class.getName(),
keybindingsManager,
null);
keybindingsManager, null);
}
/**
* Stops this bundles.
*/
public void stop(BundleContext arg0) throws Exception
{}
{
}
public static ResourceManagementService getResources()
{
if (resourcesService == null)
{
ServiceReference serviceReference =
bundleContext.getServiceReference(
ResourceManagementService.class.getName());
bundleContext
.getServiceReference(ResourceManagementService.class
.getName());
if(serviceReference == null)
if (serviceReference == null)
return null;
resourcesService = (ResourceManagementService)
bundleContext.getService(serviceReference);
resourcesService =
(ResourceManagementService) bundleContext
.getService(serviceReference);
}
return resourcesService;
}
}
}

@ -6,15 +6,7 @@
*/
package net.java.sip.communicator.plugin.keybindingchooser;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import chooser.*;
import net.java.sip.communicator.service.gui.ConfigurationForm;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.keybindings.*;
/**
@ -22,80 +14,27 @@
* configuration to configure the application keybindings.
*
* @author Damian Johnson
* @author Lubomir Marinov
*/
public class KeybindingsConfigForm
extends JPanel
implements ConfigurationForm
{
private static final long serialVersionUID = 0;
private HashMap <KeybindingSet, SIPChooser> choosers =
new HashMap <KeybindingSet, SIPChooser>();
private final KeybindingsService service;
public KeybindingsConfigForm(KeybindingsService service)
{
super(new BorderLayout());
setFocusable(true);
JTabbedPane chooserPanes = new JTabbedPane(JTabbedPane.LEFT);
// deselects entries awaiting input when focus is lost
this.addFocusListener(new FocusAdapter()
{
public void focusLost(FocusEvent event)
{
for (SIPChooser chooser : choosers.values())
{
chooser.setSelected(null);
}
}
});
for (KeybindingSet.Category category : KeybindingSet.Category.values())
{
KeybindingSet bindingSet = service.getBindings(category);
if (bindingSet == null) continue; // defaults failed to load
SIPChooser newChooser = new SIPChooser();
newChooser.putAllBindings(bindingSet.getBindings());
JPanel chooserWrapper = new JPanel(new BorderLayout());
chooserWrapper.add(newChooser, BorderLayout.NORTH);
JScrollPane scroller = new JScrollPane(chooserWrapper);
// adds listener that receives events to set bindings
this.addKeyListener(newChooser.makeAdaptor());
chooserPanes.addTab(getReadableConstant(category.toString()),
scroller);
this.choosers.put(bindingSet, newChooser);
}
add(chooserPanes);
JButton apply = new JButton("Apply");
apply.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
for (KeybindingSet set : choosers.keySet())
set.setBindings(choosers.get(set).getBindingMap());
}
});
JPanel bottomWrapper = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomWrapper.add(apply);
add(bottomWrapper, BorderLayout.SOUTH);
this.service = service;
}
/**
* Implements the <tt>ConfigurationForm.getTitle()</tt> method. Returns
* the title of this configuration form.
* Implements the <tt>ConfigurationForm.getTitle()</tt> method. Returns the
* title of this configuration form.
*/
public String getTitle()
{
return Resources.getString("keybindings");
}
/**
* Implements the <tt>ConfigurationForm.getIcon()</tt> method. Returns the
* icon of this configuration form.
@ -104,100 +43,18 @@ public byte[] getIcon()
{
return Resources.getImageInBytes("keybindingPluginIcon");
}
/**
* Implements the <tt>ConfigurationForm.getForm()</tt> method. Returns the
* component corresponding to this configuration form.
*/
public Object getForm()
{
return this;
}
/**
* Provides a more readable version of constant names. Spaces replace
* underscores and this changes the input to lowercase except the first
* letter of each word. For instance, "RARE_CARDS" would become "Rare
* Cards".
* @param input string to be converted
* @return reader friendly variant of constant name
*/
private static String getReadableConstant(String input)
{
char[] name = input.toCharArray();
boolean isStartOfWord = true;
for (int i = 0; i < name.length; ++i)
{
char chr = name[i];
if (chr == '_') name[i] = ' ';
else if (isStartOfWord) name[i] = Character.toUpperCase(chr);
else name[i] = Character.toLowerCase(chr);
isStartOfWord = chr == '_';
}
return new String(name);
}
/**
* Keybinding chooser with customized appearance and functionality for the
* SIP Communicator.
*/
private class SIPChooser
extends BindingChooser
{
private static final long serialVersionUID = 0;
// Provides mapping of UI labels to internal action names
private HashMap <String, String> actionLabels =
new HashMap <String, String>();
// Calls focus to the form so keyboard events are received
protected void onClick(MouseEvent event, BindingEntry entry,
BindingEntry.Field field)
{
super.onClick(event, entry, field);
KeybindingsConfigForm.this.requestFocus();
}
public boolean putBinding(BindingEntry newEntry, int index)
{
// Converts to I18N strings for UI
String actionInternal = newEntry.getAction();
String actionLabel = Resources.getString(actionInternal);
this.actionLabels.put(actionLabel, actionInternal);
newEntry.setAction(actionLabel);
// Overwrites the default entry layout to stretch shortcut field
newEntry.removeAll();
newEntry.setLayout(new BorderLayout());
JPanel left = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
left.add(newEntry.getField(BindingEntry.Field.INDENT));
left.add(newEntry.getField(BindingEntry.Field.ACTION));
newEntry.add(left, BorderLayout.WEST);
newEntry.add(newEntry.getField(BindingEntry.Field.SHORTCUT));
return super.putBinding(newEntry, index);
}
public LinkedHashMap <KeyStroke, String> getBindingMap()
{
// Translates I18N strings back to internal action labels
LinkedHashMap <KeyStroke, String> bindings =
new LinkedHashMap <KeyStroke, String>();
for (BindingEntry entry : super.getBindings())
{
bindings.put(entry.getShortcut(), this.actionLabels.get(entry
.getAction()));
}
return bindings;
}
return new KeybindingsConfigPanel(service);
}
public int getIndex()
{
return -1;
}
}
}

@ -0,0 +1,176 @@
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.keybindingchooser;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import chooser.*;
import net.java.sip.communicator.service.keybindings.*;
/**
* The <tt>ConfigurationForm</tt> that would be added to the settings
* configuration to configure the application keybindings.
*
* @author Damian Johnson
* @author Lubomir Marinov
*/
public class KeybindingsConfigPanel
extends JPanel
{
private static final long serialVersionUID = 0;
private HashMap<KeybindingSet, SIPChooser> choosers =
new HashMap<KeybindingSet, SIPChooser>();
public KeybindingsConfigPanel(KeybindingsService service)
{
super(new BorderLayout());
setFocusable(true);
JTabbedPane chooserPanes = new JTabbedPane(JTabbedPane.LEFT);
// deselects entries awaiting input when focus is lost
this.addFocusListener(new FocusAdapter()
{
public void focusLost(FocusEvent event)
{
for (SIPChooser chooser : choosers.values())
{
chooser.setSelected(null);
}
}
});
for (KeybindingSet.Category category : KeybindingSet.Category.values())
{
KeybindingSet bindingSet = service.getBindings(category);
if (bindingSet == null)
continue; // defaults failed to load
SIPChooser newChooser = new SIPChooser();
newChooser.putAllBindings(bindingSet.getBindings());
JPanel chooserWrapper = new JPanel(new BorderLayout());
chooserWrapper.add(newChooser, BorderLayout.NORTH);
JScrollPane scroller = new JScrollPane(chooserWrapper);
// adds listener that receives events to set bindings
this.addKeyListener(newChooser.makeAdaptor());
chooserPanes.addTab(getReadableConstant(category.toString()),
scroller);
this.choosers.put(bindingSet, newChooser);
}
add(chooserPanes);
JButton apply = new JButton("Apply");
apply.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
for (KeybindingSet set : choosers.keySet())
set.setBindings(choosers.get(set).getBindingMap());
}
});
JPanel bottomWrapper = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomWrapper.add(apply);
add(bottomWrapper, BorderLayout.SOUTH);
}
/**
* Provides a more readable version of constant names. Spaces replace
* underscores and this changes the input to lowercase except the first
* letter of each word. For instance, "RARE_CARDS" would become "Rare
* Cards".
*
* @param input string to be converted
* @return reader friendly variant of constant name
*/
private static String getReadableConstant(String input)
{
char[] name = input.toCharArray();
boolean isStartOfWord = true;
for (int i = 0; i < name.length; ++i)
{
char chr = name[i];
if (chr == '_')
name[i] = ' ';
else if (isStartOfWord)
name[i] = Character.toUpperCase(chr);
else
name[i] = Character.toLowerCase(chr);
isStartOfWord = chr == '_';
}
return new String(name);
}
/**
* Keybinding chooser with customized appearance and functionality for the
* SIP Communicator.
*/
private class SIPChooser
extends BindingChooser
{
private static final long serialVersionUID = 0;
// Provides mapping of UI labels to internal action names
private HashMap<String, String> actionLabels =
new HashMap<String, String>();
// Calls focus to the form so keyboard events are received
protected void onClick(MouseEvent event, BindingEntry entry,
BindingEntry.Field field)
{
super.onClick(event, entry, field);
KeybindingsConfigPanel.this.requestFocus();
}
public boolean putBinding(BindingEntry newEntry, int index)
{
// Converts to I18N strings for UI
String actionInternal = newEntry.getAction();
String actionLabel = Resources.getString(actionInternal);
this.actionLabels.put(actionLabel, actionInternal);
newEntry.setAction(actionLabel);
// Overwrites the default entry layout to stretch shortcut field
newEntry.removeAll();
newEntry.setLayout(new BorderLayout());
JPanel left = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
left.add(newEntry.getField(BindingEntry.Field.INDENT));
left.add(newEntry.getField(BindingEntry.Field.ACTION));
newEntry.add(left, BorderLayout.WEST);
newEntry.add(newEntry.getField(BindingEntry.Field.SHORTCUT));
return super.putBinding(newEntry, index);
}
public LinkedHashMap<KeyStroke, String> getBindingMap()
{
// Translates I18N strings back to internal action labels
LinkedHashMap<KeyStroke, String> bindings =
new LinkedHashMap<KeyStroke, String>();
for (BindingEntry entry : super.getBindings())
{
bindings.put(entry.getShortcut(), this.actionLabels.get(entry
.getAction()));
}
return bindings;
}
}
}

@ -4,7 +4,6 @@
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.keybindingchooser;
import java.io.*;
@ -19,7 +18,7 @@
*/
public class Resources
{
private static Logger log = Logger.getLogger(Resources.class);
private static Logger logger = Logger.getLogger(Resources.class);
/**
* Returns an internationalized string corresponding to the given key.
@ -40,12 +39,13 @@ public static String getString(String key)
*/
public static byte[] getImageInBytes(String imageID)
{
InputStream in = KeybindingChooserActivator.
getResources().getImageInputStream(imageID);
if(in == null)
InputStream in =
KeybindingChooserActivator.getResources().getImageInputStream(
imageID);
if (in == null)
return null;
byte[] image = null;
try
@ -55,7 +55,7 @@ public static byte[] getImageInBytes(String imageID)
}
catch (IOException e)
{
log.error("Failed to load image:" + imageID, e);
logger.error("Failed to load image:" + imageID, e);
}
return image;

Loading…
Cancel
Save