~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/roledit/ResEditorNestedRole.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation version 2 of the License.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 * END COPYRIGHT BLOCK **/
 
19
 
 
20
package com.netscape.admin.dirserv.roledit;
 
21
 
 
22
import java.util.*;
 
23
import java.awt.*;
 
24
import java.awt.event.*;
 
25
import javax.swing.*;
 
26
import javax.swing.event.*;
 
27
import javax.swing.text.*;
 
28
import com.netscape.management.nmclf.*;
 
29
import com.netscape.management.client.console.*;
 
30
import com.netscape.management.client.util.*;
 
31
import com.netscape.management.client.ug.*;
 
32
import netscape.ldap.*;
 
33
import netscape.ldap.util.DN;
 
34
import com.netscape.admin.dirserv.DSUtil;
 
35
import com.netscape.admin.dirserv.panel.UIFactory;
 
36
 
 
37
/**
 
38
 * ResEditorNestedRole is a plugin for the ResourceEditor.
 
39
 * It is used to edit information specific to nsnesteddroledefinition entries.
 
40
 */
 
41
public class ResEditorNestedRole extends JPanel
 
42
                               implements IResourceEditorPage,
 
43
                                          Observer,
 
44
                                          ActionListener,
 
45
                                          ListSelectionListener {
 
46
    /**
 
47
     * Constructor
 
48
     */
 
49
    public ResEditorNestedRole() {
 
50
        _id = _resource.getString(_section, "id");  
 
51
    }
 
52
 
 
53
 
 
54
        /**
 
55
     * Implements the Observer interface.
 
56
     */
 
57
        public void update(Observable o, Object arg) {
 
58
        if ((o instanceof ResourcePageObservable) == false) {
 
59
            return;
 
60
        }
 
61
        ResourcePageObservable observable = (ResourcePageObservable) o;
 
62
        if (arg instanceof String) {
 
63
            String argString = (String) arg;
 
64
            if (argString.equalsIgnoreCase(NSROLEDN_ATTR)) {
 
65
                loadCache();
 
66
            }
 
67
        }
 
68
    }
 
69
 
 
70
 
 
71
    /**
 
72
     * Implements the IResourceEditorPage interface.
 
73
     * Here we build the UI components, search roles from the directory
 
74
     * and initialize the UI components with them.
 
75
     */
 
76
    public void initialize(ResourcePageObservable observable,
 
77
                           ResourceEditor parent) {
 
78
        _parent = parent;
 
79
        _observable = observable;
 
80
        _roleCache = new RoleCache(true);
 
81
 
 
82
                // Build and layout the components if not already done
 
83
                if (_table == null) {
 
84
                JLabel label = new JLabel(_resource.getString(_section, "label"));
 
85
                        
 
86
                _table = new SuiTable();
 
87
                        label.setLabelFor(_table);
 
88
                _table.setModel(_roleCache.getTableModel());
 
89
                _table.setAutoResizeMode(_table.AUTO_RESIZE_ALL_COLUMNS);
 
90
                _table.getSelectionModel().addListSelectionListener(this);
 
91
                        _table.setPreferredScrollableViewportSize(new Dimension(10, 10));
 
92
                        JScrollPane scrollPane = SuiTable.createScrollPaneForTable(_table);
 
93
                scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
 
94
 
 
95
                _addButton = UIFactory.makeJButton( this, _section, "addButton", _resource);                    
 
96
                _removeButton = UIFactory.makeJButton( this, _section, "removeButton", _resource);
 
97
 
 
98
                JButtonFactory.resizeGroup(_addButton, _removeButton);
 
99
 
 
100
                Box buttonBox = new Box(BoxLayout.X_AXIS);
 
101
                buttonBox.add(Box.createHorizontalGlue());
 
102
                buttonBox.add(_addButton);
 
103
                buttonBox.add(Box.createHorizontalStrut(SuiLookAndFeel.SEPARATED_COMPONENT_SPACE));
 
104
                buttonBox.add(_removeButton);
 
105
 
 
106
                setLayout(new GridBagLayout());
 
107
                GridBagUtil.constrain(this, label,
 
108
                                  0, 0, 1, 1,
 
109
                                  1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
 
110
                                  SuiLookAndFeel.VERT_WINDOW_INSET, SuiLookAndFeel.HORIZ_WINDOW_INSET,
 
111
                                  0, SuiLookAndFeel.HORIZ_WINDOW_INSET);
 
112
                GridBagUtil.constrain(this, scrollPane,
 
113
                                  0, 1, 1, 1,
 
114
                                  1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,
 
115
                                  0, SuiLookAndFeel.HORIZ_WINDOW_INSET,
 
116
                                  0, SuiLookAndFeel.HORIZ_WINDOW_INSET);
 
117
                GridBagUtil.constrain(this, buttonBox,
 
118
                                  0, 2, 1, 1,
 
119
                                  0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE,
 
120
                                  SuiLookAndFeel.SEPARATED_COMPONENT_SPACE, SuiLookAndFeel.COMPONENT_SPACE,
 
121
                                  SuiLookAndFeel.VERT_WINDOW_INSET, SuiLookAndFeel.HORIZ_WINDOW_INSET);
 
122
                }
 
123
                
 
124
                // Load values from the observable
 
125
        loadCache();
 
126
        valueChanged(null); // To update the state of _removeButton
 
127
    }
 
128
 
 
129
 
 
130
        /**
 
131
     * Load the nested roles in the cache.
 
132
     */
 
133
    void loadCache() {
 
134
    
 
135
        //
 
136
        // Empty the cache
 
137
        //
 
138
        _roleCache.clear();
 
139
        _roleCache.enableRecording(false);
 
140
        
 
141
        //
 
142
        // Fill the cache with the roles in the nsroledn attribute of the entry.
 
143
        //
 
144
        try {
 
145
            LDAPConnection ldc = _parent.getConsoleInfo().getLDAPConnection();
 
146
            Vector roleDNs = _observable.get(NSROLEDN_ATTR);
 
147
            Enumeration e = roleDNs.elements() ;
 
148
            while (e.hasMoreElements()) {
 
149
                    String dn = (String)e.nextElement();
 
150
                _roleCache.add(dn, ldc);
 
151
                    }
 
152
        }
 
153
        catch(LDAPException e) {
 
154
                Debug.println("ResEditorNestedRole.loadCache: " + e + " while loading the roles");
 
155
        }
 
156
        _roleCache.enableRecording(true);
 
157
    }
 
158
 
 
159
 
 
160
    /**
 
161
     * Implements the IResourceEditorPage interface.
 
162
     */
 
163
    public String getID() {
 
164
        return _id;
 
165
    }
 
166
 
 
167
 
 
168
    /**
 
169
     * Implements the IResourceEditorPage interface.
 
170
     * We have nothing to do here.
 
171
     */
 
172
    public boolean afterSave(ResourcePageObservable observable) throws Exception {
 
173
        return true;
 
174
    }
 
175
 
 
176
 
 
177
    /**
 
178
     * Implements the IResourceEditorPage interface. 
 
179
     * Save the list of nested roles (if modified) in the observable object.
 
180
     */
 
181
    public boolean save(ResourcePageObservable observable) throws Exception {
 
182
 
 
183
        if (!_roleCache.isRecordingEmpty()) {
 
184
                Vector roleDNs = _roleCache.getRoleDNs();
 
185
            if (roleDNs == null) {
 
186
                        _observable.delete(NSROLEDN_ATTR) ;
 
187
            }
 
188
            else {
 
189
                _observable.replace(NSROLEDN_ATTR, roleDNs);
 
190
            }
 
191
            _roleCache.enableRecording(false);
 
192
            _roleCache.enableRecording(true); // This clears the change records
 
193
        }
 
194
        
 
195
        return true;
 
196
    }
 
197
 
 
198
 
 
199
    /**
 
200
     * Implements the IResourceEditorPage interface. 
 
201
     */
 
202
    public void clear() {
 
203
    }
 
204
 
 
205
 
 
206
    /**
 
207
     * Implements the IResourceEditorPage interface. 
 
208
     */
 
209
    public void reset() {
 
210
    }
 
211
 
 
212
 
 
213
    /**
 
214
     * Implements the IResourceEditorPage interface. 
 
215
     */
 
216
    public void setDefault() {
 
217
    }
 
218
 
 
219
 
 
220
    /**
 
221
     * Implements the IResourceEditorPage interface. 
 
222
     * Check if some changes have been recorded in the role cache.
 
223
     */
 
224
    public boolean isModified() {
 
225
        return !_roleCache.isRecordingEmpty();
 
226
    }
 
227
 
 
228
 
 
229
    /**
 
230
     * Implements the IResourceEditorPage interface. 
 
231
     * Problem here: the 'modified' state is controled only 
 
232
     * by the role cache.
 
233
     */
 
234
    public void setModified(boolean value) {
 
235
        if (value == true) 
 
236
                throw new IllegalArgumentException("Can't set to true");
 
237
    }
 
238
 
 
239
 
 
240
    /**
 
241
     * Implements the IResourceEditorPage interface. 
 
242
     */
 
243
    public boolean isReadOnly() {
 
244
        return _isReadOnly;
 
245
    }
 
246
 
 
247
 
 
248
    /**
 
249
     * Implements the IResourceEditorPage interface. 
 
250
     */
 
251
    public void setReadOnly(boolean value) {
 
252
        _isReadOnly = value;
 
253
    }
 
254
 
 
255
 
 
256
    /**
 
257
     * Implements the IResourceEditorPage interface. 
 
258
     */
 
259
    public void setEnable(boolean value) {
 
260
        _isEnabled = value;
 
261
    }
 
262
 
 
263
 
 
264
    /**
 
265
     * Implements the IResourceEditorPage interface. 
 
266
     * At least one role must be nested.
 
267
     */
 
268
    public boolean isComplete() {
 
269
        boolean result = true;
 
270
        
 
271
        if (_roleCache.isEmpty()) {
 
272
            JOptionPane.showMessageDialog(null,
 
273
                    _resource.getString(_section, "IncompleteText"),
 
274
                    _resource.getString(_section, "IncompleteTitle"), 
 
275
                    JOptionPane.ERROR_MESSAGE);
 
276
            ModalDialogUtil.sleep();
 
277
            result = false;
 
278
        }
 
279
 
 
280
        return result;
 
281
    }
 
282
 
 
283
 
 
284
    /**
 
285
     * Implements the IResourceEditorPage interface. 
 
286
     */
 
287
    public String getDisplayName() {
 
288
        return _id;
 
289
    }
 
290
 
 
291
 
 
292
    /**
 
293
     * Implements the IResourceEditorPage interface. 
 
294
     */
 
295
    public void help() {
 
296
                DSUtil.help("configuration-role-member-nested-help");
 
297
    }
 
298
 
 
299
 
 
300
    /**
 
301
         * Implements the ActionListener interface.
 
302
         */
 
303
    public void actionPerformed(ActionEvent e) 
 
304
    {
 
305
                _parent.setBusyCursor(true);
 
306
        if (e.getSource() == _addButton) {
 
307
                actionAdd();
 
308
        }
 
309
        else if (e.getSource() == _removeButton) {
 
310
                actionRemove();
 
311
        }
 
312
                _parent.setBusyCursor(false);
 
313
        }
 
314
    
 
315
    /**
 
316
     * Process a click on Add
 
317
     */
 
318
    void actionAdd() {          
 
319
                ConsoleInfo info = _parent.getConsoleInfo();
 
320
        Vector selectedRoles = null;
 
321
    
 
322
 
 
323
                // Build the exclude list for the role picker.
 
324
                // We exclude :
 
325
                //              - the roles which are already nested
 
326
                //              - the role being edited (not useful to nest a role inside itself)
 
327
                //
 
328
                Vector excludeRoles = _roleCache.getRoleDNs();
 
329
                String observedRole = _observable.getDN();
 
330
                if (observedRole != null) 
 
331
                        excludeRoles.addElement(observedRole);
 
332
                        
 
333
        //
 
334
        // Popup the role picker.
 
335
        // The picker is instructed to show all kinds of role.
 
336
        //
 
337
        try {
 
338
                String baseDn;
 
339
                if (_observable.isNewUser())
 
340
                baseDn = _observable.getCreateBaseDN();
 
341
            else
 
342
                baseDn = RolePickerDialog.makeParentDn(_observable.getDN());
 
343
            RolePickerDialog picker = new RolePickerDialog(_parent.getFrame(), info);
 
344
            picker.loadWithNestableRoles(baseDn, excludeRoles.elements());
 
345
                        picker.allowMultipleSelection(true);
 
346
            picker.showModal();
 
347
            if (!picker.isCancel())
 
348
                selectedRoles = picker.getSelectedRoles();
 
349
        }
 
350
        catch(LDAPException e) {
 
351
            Debug.println("ResEditorNestedRole.actionAdd: reading roles failed with " + e);
 
352
                        // We popup a dialog to warn the user
 
353
                        DSUtil.showLDAPErrorDialog(_parent.getFrame(), e, "fetching-server-unavailable");
 
354
        }
 
355
 
 
356
        //
 
357
        // Adds the roles in _managedRoleCache
 
358
        //
 
359
        if (selectedRoles != null) {
 
360
                try {
 
361
                    LDAPConnection ldc = info.getLDAPConnection();
 
362
                    Enumeration e = selectedRoles.elements();
 
363
                while (e.hasMoreElements()) {
 
364
                        String roleDn = (String)e.nextElement();
 
365
                                        DN roleDN = new DN(roleDn);
 
366
                    if ( !_roleCache.contains(roleDN) )
 
367
                        _roleCache.add(roleDn, ldc);
 
368
                }
 
369
            }
 
370
            catch(LDAPException e) {
 
371
                Debug.println("RoleEditorDialog.actionAdd: reading roles failed with " + e);
 
372
                            // We popup a dialog to warn the user
 
373
                                DSUtil.showLDAPErrorDialog(_parent.getFrame(), e,
 
374
                                                                                                 "updating-directory-title");
 
375
            }
 
376
        }               
 
377
 
 
378
    }
 
379
    
 
380
    /**
 
381
     * Process a click on Remove
 
382
     */
 
383
    void actionRemove() {
 
384
        int roleCount = _table.getModel().getRowCount();
 
385
        ListSelectionModel selModel = _table.getSelectionModel();
 
386
        
 
387
        // 600538 Multiple elements from a vector must be deleted starting
 
388
        // from the highest index as vector indexes change after each delete
 
389
        for (int i = (roleCount - 1); i >= 0; i--) {
 
390
                if (selModel.isSelectedIndex(i)) {
 
391
                _roleCache.remove(i);
 
392
            }
 
393
        }
 
394
    }
 
395
    
 
396
 
 
397
    /**
 
398
     * Implements ListSelectionListener.
 
399
     * Called when the user clicks in the table.
 
400
     * We update the tooltip text and the state of _removeButton
 
401
     */
 
402
    public void valueChanged(ListSelectionEvent e) {
 
403
        // Note: initialize() invokes this method with e == null
 
404
                
 
405
        ListSelectionModel m = _table.getSelectionModel() ;
 
406
        int min = m.getMinSelectionIndex() ;
 
407
        int max = m.getMaxSelectionIndex() ;
 
408
        
 
409
        // Update the tooltip text of the tab
 
410
        if ((min == -1) || (max - min + 1 > 1) || (_table.getRowCount() <= 0)) {
 
411
                // The selection is empty or multiple or the table is empty
 
412
                _table.setToolTipText(null);
 
413
        }
 
414
        else {
 
415
            _table.setToolTipText(_roleCache.getRoleDN(min));
 
416
        }
 
417
        
 
418
        // _removeButton is disabled if the selection is empty: if we use
 
419
                // the tab and the table gets the focus JTable.getSelectedRowCount() is 1
 
420
                // (even if the row count is 0 !!)
 
421
        _removeButton.setEnabled((_table.getSelectedRowCount() > 0) &&
 
422
                                                                 (_table.getRowCount() > 0));
 
423
    }
 
424
    
 
425
    
 
426
        // State variables
 
427
    ResourceEditor _parent;
 
428
    ResourcePageObservable _observable;
 
429
    String _id;
 
430
        RoleCache _roleCache;
 
431
    boolean _isReadOnly = false;
 
432
    boolean _isEnabled = true;
 
433
 
 
434
        // Components
 
435
        SuiTable _table;
 
436
    JButton _addButton;
 
437
    JButton _removeButton;
 
438
 
 
439
        // I18N
 
440
        static ResourceSet _resource = DSUtil._resource;
 
441
        private static final String _section = "nestedRolePage";
 
442
 
 
443
        // LDAP schema
 
444
    private static final String NSROLEDN_ATTR = "nsRoleDN";
 
445
    private static final String NSROLE_ATTR = "nsRole";
 
446
    private static final String[] ROLE_ATTRS = { NSROLEDN_ATTR };
 
447
}