aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/keybindingchooser/KeybindingsConfigPanel.java
blob: 941db8119ad190e02b9b00ef2486b4ac2bb9c834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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 org.osgi.framework.*;

import net.java.sip.communicator.plugin.keybindingchooser.chooser.*;
import net.java.sip.communicator.service.keybindings.*;
import net.java.sip.communicator.util.swing.*;

/**
 * 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 TransparentPanel
{
    private static KeybindingsService getKeybindingsService()
    {
        BundleContext bundleContext =
            KeybindingChooserActivator.getBundleContext();
        ServiceReference keybindingRef =
            bundleContext.getServiceReference(KeybindingsService.class
                .getName());

        return (KeybindingsService) bundleContext.getService(keybindingRef);
    }

    private static final long serialVersionUID = 0;

    private final HashMap<KeybindingSet, SIPChooser> choosers =
        new HashMap<KeybindingSet, SIPChooser>();

    public KeybindingsConfigPanel()
    {
        super(new BorderLayout());

        KeybindingsService service = getKeybindingsService();

        setFocusable(true);
        JTabbedPane chooserPanes = new JTabbedPane();

        // 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 TransparentPanel(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(
            KeybindingChooserActivator.getResources()
                .getI18NString("service.gui.APPLY"));

        apply.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                for (KeybindingSet set : choosers.keySet())
                    set.setBindings(choosers.get(set).getBindingMap());
            }
        });

        JPanel bottomWrapper =
            new TransparentPanel(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 = getI18NString(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 TransparentPanel(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);
        }

        /**
         * Gets the internationalized string corresponding to a specific key
         * given in its plugin-specific format. The key is translated to the
         * global format of the ReouseceManagementService and the translated key
         * is used to retrieve the string from the resource files.
         * 
         * @param key the key of the string to be retrieved given in its
         *            plugin-specific format
         * @return the internationalized string corresponding to a specific key
         *         given in its plugin-specific format
         */
        private String getI18NString(String key)
        {
            StringBuffer newKey = new StringBuffer();
            newKey.append("plugin.keybindings.");
            char[] keyChars = key.toCharArray();
            for (char keyChar : keyChars)
            {
                if (Character.isLowerCase(keyChar))
                    newKey.append(Character.toUpperCase(keyChar));
                else if (Character.isUpperCase(keyChar))
                {
                    newKey.append('_');
                    newKey.append(keyChar);
                }
                else
                    newKey.append('_');
            }

            return
                KeybindingChooserActivator.getResources().getI18NString(
                    newKey.toString());
        }

        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;
        }
    }
}