aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/ZrtpConfigureTableModel.java
blob: 20d74bb2c05e49b494c02f4a86fdbc7682f56024 (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
/*
 * 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.impl.neomedia;

import java.util.*;

import javax.swing.table.*;

import gnu.java.zrtp.*;

public class ZrtpConfigureTableModel<T extends Enum<T>>
    extends AbstractTableModel
{
    private final ZrtpConfigure active;
    private final ZrtpConfigure inActive;
    
    // used to identify the Enum class when calling ZrtpConfigure methods.
    private final T algorithm;  
    private final Class<T> clazz;
    
    boolean onOff[];

    public ZrtpConfigureTableModel(T algo, ZrtpConfigure act,
            ZrtpConfigure inAct, String savedConf) {
        active = act;
        inActive = inAct;
        algorithm = algo;
        
        clazz = algorithm.getDeclaringClass();
        
        initialize(savedConf);
    }

    private void initialize(String savedConf) {

        // Get all enums constants of this Enum to process
        T enumValues[] = clazz.getEnumConstants();

        // first build a list of all available algorithms of this type
        ArrayList<String> fullList = new ArrayList<String>(enumValues.length);
        for (T sh : enumValues) {
            fullList.add(sh.name());
        }

        String savedAlgos[] = savedConf.split(";");
        // Configure saved algorithms as active, remove them from full list of
        // algos
        for (String str : savedAlgos) {
            try {
                T algoEnum = Enum.valueOf(clazz, str);
                if (algoEnum != null) {
                    active.addAlgo(algoEnum);
                    fullList.remove(str);
                }
            } catch (IllegalArgumentException e) {
                continue;
            }
        }
        // rest of algorithms are inactive
        for (String str : fullList) {
            T algoEnum = Enum.valueOf(clazz, str);
            if (algoEnum != null) {
                inActive.addAlgo(algoEnum);
            }
        }
    }

    public int getColumnCount() { 
        return 2;
    }
    
    public int getRowCount() { 
        return active.getNumConfiguredAlgos(algorithm) + inActive.getNumConfiguredAlgos(algorithm);
    }
    
    public Object getValueAt(int row, int col) {
        switch (col)
        {
        case 0:
            if (row >= active.getNumConfiguredAlgos(algorithm)) {
                return (new Boolean(false));
            }
            return (new Boolean(true));
        case 1:
            if (row >= active.getNumConfiguredAlgos(algorithm)) {
                row -= active.getNumConfiguredAlgos(algorithm);
                return (inActive.getAlgoAt(row, algorithm).name());
            }
            else 
                return (active.getAlgoAt(row, algorithm).name());
        default:
            return null;
        }

    }
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return (columnIndex == 0);
    }
    public Class<?> getColumnClass(int columnIndex)
    {
        return (columnIndex == 0) ? Boolean.class : super
            .getColumnClass(columnIndex);
    }
    
    public void setValueAt(Object value, int row, int column)
    {
        if ((column == 0) && (value instanceof Boolean))
        {
            if (row >= active.getNumConfiguredAlgos(algorithm)) {
                row -= active.getNumConfiguredAlgos(algorithm);
                active.addAlgo(inActive.getAlgoAt(row, algorithm));
                inActive.removeAlgo(inActive.getAlgoAt(row, algorithm));
            }
            else {
                inActive.addAlgo(active.getAlgoAt(row, algorithm));
                active.removeAlgo(active.getAlgoAt(row, algorithm));
            }
            fireTableRowsUpdated(0, getRowCount());
        }
    }

    /**
     * Move a Configuration entry up or down one position.
     * 
     * The "move up" is Converted to a "move down" with modified row index
     * and flags.
     * 
     * @param row
     *        Which row to move
     * @param up
     *        If true move up, else move down
     * @param upSave
     *        Because the functions converts a move up into a move down
     *        this flag shows what the caller intented. Needed to adjust
     *        an index return value.
     * @return new row index of entry
     */
    public int move(int row, boolean up, boolean upSave) {
        if (up) {
            if (row <= 0)
                throw new IllegalArgumentException("rowIndex");

            return move(row - 1, false, upSave) - 1;
        }
        T swap;
        if (row >= (getRowCount() - 1))
            throw new IllegalArgumentException("rowIndex");

        // Can't move down last last entry of active list.
//        if (row == active.getNumConfiguredPubKeys() - 1) {
//            // this was a "move up" of the first inActive member adjust index
//            return upSave ? row + 2 : row;
//        }

//        if (row >= active.getNumConfiguredPubKeys()) {
//            if (inActive.getNumConfiguredPubKeys() <= 1) {
//                return row + 2;
//            }
//            row -= active.getNumConfiguredPubKeys();
//            swap = inActive.getPubKeyAlgoAt(row);
//            inActive.removePubKeyAlgo(swap);
//            inActive.addPubKeyAlgoAt(row + 1, swap);
//            row++;                           // take active rows into account
//        }
//        else {
            if (active.getNumConfiguredAlgos(algorithm) <= 1) {
                return row;
            }
            swap = active.getAlgoAt(row, algorithm);
            active.removeAlgo(swap);
            active.addAlgoAt(row + 1, swap);
//        }

        fireTableRowsUpdated(0, getRowCount());
        return row + 1;
    }
    
    public boolean checkEnableUp(int row) {
        return (row < active.getNumConfiguredAlgos(algorithm));
    }

    public boolean checkEnableDown(int row) {
        return (row < active.getNumConfiguredAlgos(algorithm) - 1);
    }
    
    /**
     * Saves the ZrtpConfigure data for this algorithm to configure file
     */
    public void saveConfig() {
        StringBuffer algoStr = new StringBuffer();
        for (T sh: active.algos(algorithm)) {
            algoStr.append(sh.name());
            algoStr.append(';');
        }
        // save in configuration data using the appropriate key
    }
    
    /**
     * Sets the ZrtpConfigure data for this algorithm to a predefined set.
     * 
     * The caller prepared active ZrtpConfigureto contain a standard set of
     * algorithms. Get the names and construct a string, then call initialize
     * to setup the inActive ZrtpConfigure data.
     */
    public void setStandardConfig() {
        StringBuffer algoStr = new StringBuffer();
        for (T sh: active.algos(algorithm)) {
            algoStr.append(sh.name());
            algoStr.append(';');
        }
        initialize(algoStr.toString());
        fireTableRowsUpdated(0, getRowCount());
    }
}