aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/keybindings/KeybindingSetImpl.java
blob: 576bcf97ae3830664668685f8b7532931b4c8b2a (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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.keybindings;

import java.io.*;
import java.util.*;

import javax.swing.*;

import net.java.sip.communicator.service.keybindings.*;

/**
 * Default implementation for the wrapper of keybinding sets.
 *
 * @author Damian Johnson
 */
class KeybindingSetImpl
    extends KeybindingSet
{
    private LinkedHashMap<KeyStroke, String> bindings;

    private Category category;

    /**
     * Destination where custom bindings are saved, null if it couldn't be
     * secured.
     */
    private File customFile;

    /**
     * Flag indicating that the associated service has been stopped.
     */
    private boolean isInvalidated = false;

    KeybindingSetImpl(Map<KeyStroke, String> initial, Category category,
        File saveDst)
    {
        this.bindings = new LinkedHashMap<KeyStroke, String>(initial);
        this.category = category;
        this.customFile = saveDst;
    }

    /**
     * Provides current keybinding mappings.
     *
     * @return mapping of keystrokes to the string representation of the actions
     *         they perform
     */
    public LinkedHashMap<KeyStroke, String> getBindings()
    {
        return new LinkedHashMap<KeyStroke, String>(this.bindings);
    }

    /**
     * Resets the bindings and notifies the observer's listeners if they've
     * changed. If the bindings can be written then they will be.
     *
     * @param newBindings new keybindings to be held
     */
    public void setBindings(Map<KeyStroke, String> newBindings)
    {
        if (!this.bindings.equals(newBindings))
        {
            this.bindings = new LinkedHashMap<KeyStroke, String>(newBindings);
            setChanged();
            notifyObservers(this);
        }
    }

    /**
     * Provides the portion of the UI to which the bindings belong.
     *
     * @return binding category
     */
    public Category getCategory()
    {
        return this.category;
    }

    /**
     * Provides if the keybindings can be written when changed or not.
     *
     * @return true if bindings can be written when changed, false otherwise
     */
    boolean isWritable()
    {
        return !this.isInvalidated && this.customFile != null;
    }

    /**
     * Provides the file where custom bindings are to be saved.
     *
     * @return custom bindings save destination
     */
    File getCustomFile()
    {
        return this.customFile;
    }

    /**
     * Invalidates reference to custom output, preventing further writes.
     */
    void invalidate()
    {
        this.isInvalidated = true;
    }
}