KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > widgets > ACIItemUserPermissionsComposite


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.aciitemeditor.widgets;
21
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
29 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
30 import org.apache.directory.ldapstudio.aciitemeditor.dialogs.UserPermissionDialog;
31 import org.apache.directory.ldapstudio.aciitemeditor.model.ProtectedItemWrapper;
32 import org.apache.directory.shared.ldap.aci.GrantAndDenial;
33 import org.apache.directory.shared.ldap.aci.ProtectedItem;
34 import org.apache.directory.shared.ldap.aci.UserPermission;
35 import org.eclipse.jface.viewers.ArrayContentProvider;
36 import org.eclipse.jface.viewers.DoubleClickEvent;
37 import org.eclipse.jface.viewers.IDoubleClickListener;
38 import org.eclipse.jface.viewers.ISelectionChangedListener;
39 import org.eclipse.jface.viewers.IStructuredSelection;
40 import org.eclipse.jface.viewers.LabelProvider;
41 import org.eclipse.jface.viewers.SelectionChangedEvent;
42 import org.eclipse.jface.viewers.TableViewer;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.swt.widgets.Label;
51 import org.eclipse.swt.widgets.Table;
52
53
54 /**
55  * This composite contains GUI elements to add, edit and delete ACI user permissions.
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class ACIItemUserPermissionsComposite extends Composite
61 {
62
63     /** The context. */
64     private ACIItemValueWithContext context;
65
66     /** The inner composite for all the content */
67     private Composite composite = null;
68
69     /** The description label */
70     private Label label = null;
71
72     /** The table control for the table viewer */
73     private Table table = null;
74
75     /** The table viewer containing all user classes */
76     private TableViewer tableViewer = null;
77
78     /** The composite containing the buttons */
79     private Composite buttonComposite = null;
80
81     /** The add button */
82     private Button addButton = null;
83
84     /** The edit button */
85     private Button editButton = null;
86
87     /** The delete button */
88     private Button deleteButton = null;
89
90     /** The selected user permissions, also input of the table viewer */
91     List JavaDoc<UserPermissionWrapper> userPermissionWrappers = new ArrayList JavaDoc<UserPermissionWrapper>();
92
93     /**
94      * UserPermissionWrapper are used as input of the table viewer.
95      *
96      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
97      * @version $Rev$, $Date$
98      */

99     private class UserPermissionWrapper
100     {
101         /** The user permission bean. */
102         private UserPermission userPermission;
103
104
105         /**
106          * Creates a new instance of UserPermissionWrapper.
107          *
108          * @param userPermission the user permission
109          */

110         public UserPermissionWrapper( UserPermission userPermission )
111         {
112             this.userPermission = userPermission;
113         }
114
115
116         /**
117          * Returns a user-friedly string, displayed in the table.
118          *
119          * @return the string
120          */

121         public String JavaDoc toString()
122         {
123             if ( userPermission == null )
124             {
125                 return "<UNKNOWN>"; //$NON-NLS-1$
126
}
127             else
128             {
129                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
130                 if ( userPermission.getPrecedence() > -1 )
131                 {
132                     buffer.append( '(' );
133                     buffer.append( userPermission.getPrecedence() );
134                     buffer.append( ')' );
135                     buffer.append( ' ' );
136                 }
137                 for ( Iterator JavaDoc<ProtectedItem> it = ( ( Collection JavaDoc<ProtectedItem> ) userPermission.getProtectedItems() )
138                     .iterator(); it.hasNext(); )
139                 {
140                     ProtectedItem item = it.next();
141                     String JavaDoc s = ProtectedItemWrapper.classToDisplayMap.get( item.getClass() );
142                     buffer.append( s );
143
144                     if ( it.hasNext() )
145                     {
146                         buffer.append( ',' );
147                     }
148                 }
149                 buffer.append( ':' );
150                 buffer.append( ' ' );
151                 for ( Iterator JavaDoc<GrantAndDenial> it = ( ( Collection JavaDoc<GrantAndDenial> ) userPermission
152                     .getGrantsAndDenials() ).iterator(); it.hasNext(); )
153                 {
154                     GrantAndDenial gd = it.next();
155                     buffer.append( gd.isGrant() ? '+' : '-' );
156                     buffer.append( gd.getMicroOperation().getName() );
157
158                     if ( it.hasNext() )
159                     {
160                         buffer.append( ',' );
161                     }
162                 }
163
164                 String JavaDoc s = buffer.toString();
165                 s = s.replace( '\r', ' ' );
166                 s = s.replace( '\n', ' ' );
167                 if ( s.length() > 50 )
168                 {
169                     String JavaDoc temp = s;
170                     s = temp.substring( 0, 25 );
171                     s = s + "..."; //$NON-NLS-1$
172
s = s + temp.substring( temp.length() - 25, temp.length() );
173                 }
174                 return s;
175             }
176         }
177     }
178
179
180     /**
181      * Creates a new instance of ACIItemUserPermissionsComposite.
182      *
183      * @param parent
184      * @param style
185      */

186     public ACIItemUserPermissionsComposite( Composite parent, int style )
187     {
188         super( parent, style );
189
190         GridLayout layout = new GridLayout();
191         layout.horizontalSpacing = 0;
192         layout.verticalSpacing = 0;
193         layout.marginHeight = 0;
194         layout.marginWidth = 0;
195         setLayout( layout );
196
197         GridData layoutData = new GridData();
198         layoutData.horizontalAlignment = GridData.FILL;
199         layoutData.grabExcessHorizontalSpace = true;
200         layoutData.verticalAlignment = GridData.CENTER;
201         setLayoutData( layoutData );
202
203         createComposite();
204     }
205
206
207     /**
208      * This method initializes composite
209      *
210      */

211     private void createComposite()
212     {
213
214         GridData labelGridData = new GridData();
215         labelGridData.horizontalSpan = 2;
216         labelGridData.verticalAlignment = GridData.CENTER;
217         labelGridData.grabExcessHorizontalSpace = true;
218         labelGridData.horizontalAlignment = GridData.FILL;
219
220         GridLayout gridLayout = new GridLayout();
221         gridLayout.makeColumnsEqualWidth = false;
222         gridLayout.numColumns = 2;
223
224         GridData gridData = new GridData();
225         gridData.horizontalAlignment = GridData.FILL;
226         gridData.grabExcessHorizontalSpace = true;
227         gridData.verticalSpan = 1;
228         gridData.verticalAlignment = GridData.BEGINNING;
229
230         composite = new Composite( this, SWT.NONE );
231         composite.setLayoutData( gridData );
232         composite.setLayout( gridLayout );
233
234         label = new Label( composite, SWT.NONE );
235         label.setText( Messages.getString( "ACIItemUserPermissionsComposite.descripton" ) ); //$NON-NLS-1$
236
label.setLayoutData( labelGridData );
237
238         createTable();
239
240         createButtonComposite();
241     }
242
243
244     /**
245      * This method initializes table and table viewer
246      *
247      */

248     private void createTable()
249     {
250         GridData tableGridData = new GridData();
251         tableGridData.grabExcessHorizontalSpace = true;
252         tableGridData.verticalAlignment = GridData.FILL;
253         tableGridData.horizontalAlignment = GridData.FILL;
254         //tableGridData.heightHint = 100;
255

256         table = new Table( composite, SWT.BORDER );
257         table.setHeaderVisible( false );
258         table.setLayoutData( tableGridData );
259         table.setLinesVisible( false );
260         tableViewer = new TableViewer( table );
261         tableViewer.setContentProvider( new ArrayContentProvider() );
262         tableViewer.setLabelProvider( new LabelProvider() );
263         tableViewer.setInput( userPermissionWrappers );
264
265         tableViewer.addSelectionChangedListener( new ISelectionChangedListener()
266         {
267             public void selectionChanged( SelectionChangedEvent event )
268             {
269                 userPermissionSelected();
270             }
271         } );
272
273         tableViewer.addDoubleClickListener( new IDoubleClickListener()
274         {
275             public void doubleClick( DoubleClickEvent event )
276             {
277                 editUserPermission();
278             }
279         } );
280     }
281
282
283     /**
284      * This method initializes buttons
285      *
286      */

287     private void createButtonComposite()
288     {
289         GridData deleteButtonGridData = new GridData();
290         deleteButtonGridData.horizontalAlignment = GridData.FILL;
291         deleteButtonGridData.grabExcessHorizontalSpace = false;
292         deleteButtonGridData.verticalAlignment = GridData.BEGINNING;
293         deleteButtonGridData.widthHint = Activator.getButtonWidth( this );
294
295         GridData editButtonGridData = new GridData();
296         editButtonGridData.horizontalAlignment = GridData.FILL;
297         editButtonGridData.grabExcessHorizontalSpace = false;
298         editButtonGridData.verticalAlignment = GridData.BEGINNING;
299         editButtonGridData.widthHint = Activator.getButtonWidth( this );
300
301         GridData addButtonGridData = new GridData();
302         addButtonGridData.horizontalAlignment = GridData.FILL;
303         addButtonGridData.grabExcessHorizontalSpace = false;
304         addButtonGridData.verticalAlignment = GridData.BEGINNING;
305         addButtonGridData.widthHint = Activator.getButtonWidth( this );
306
307         GridLayout gridLayout = new GridLayout();
308         gridLayout.marginWidth = 0;
309         gridLayout.marginHeight = 0;
310         GridData gridData = new GridData();
311         gridData.horizontalAlignment = GridData.CENTER;
312         gridData.grabExcessHorizontalSpace = false;
313         gridData.grabExcessVerticalSpace = false;
314         gridData.verticalAlignment = GridData.FILL;
315
316         buttonComposite = new Composite( composite, SWT.NONE );
317         buttonComposite.setLayoutData( gridData );
318         buttonComposite.setLayout( gridLayout );
319
320         addButton = new Button( buttonComposite, SWT.NONE );
321         addButton.setText( Messages.getString( "ACIItemUserPermissionsComposite.add.button" ) ); //$NON-NLS-1$
322
addButton.setLayoutData( addButtonGridData );
323         addButton.addSelectionListener( new SelectionAdapter()
324         {
325             public void widgetSelected( SelectionEvent e )
326             {
327                 addUserPermission();
328             }
329         } );
330
331         editButton = new Button( buttonComposite, SWT.NONE );
332         editButton.setText( Messages.getString( "ACIItemUserPermissionsComposite.edit.button" ) ); //$NON-NLS-1$
333
editButton.setLayoutData( editButtonGridData );
334         editButton.addSelectionListener( new SelectionAdapter()
335         {
336             public void widgetSelected( SelectionEvent e )
337             {
338                 editUserPermission();
339             }
340         } );
341         editButton.setEnabled( false );
342
343         deleteButton = new Button( buttonComposite, SWT.NONE );
344         deleteButton.setText( Messages.getString( "ACIItemUserPermissionsComposite.delete.button" ) ); //$NON-NLS-1$
345
deleteButton.setLayoutData( deleteButtonGridData );
346         deleteButton.addSelectionListener( new SelectionAdapter()
347         {
348             public void widgetSelected( SelectionEvent e )
349             {
350                 deleteUserPermission();
351             }
352         } );
353         deleteButton.setEnabled( false );
354
355     }
356
357
358     /**
359      * Shows or hides this composite.
360      *
361      * @param visible true if visible
362      */

363     public void setVisible( boolean visible )
364     {
365         super.setVisible( visible );
366         ( ( GridData ) getLayoutData() ).heightHint = visible ? -1 : 0;
367     }
368
369
370     /**
371      * Sets the context.
372      *
373      * @param context the context
374      */

375     public void setContext( ACIItemValueWithContext context )
376     {
377         this.context = context;
378     }
379
380
381     /**
382      * Sets the user permissions.
383      *
384      * @param userPermissions
385      */

386     public void setUserPermissions( Collection JavaDoc<UserPermission> userPermissions )
387     {
388         userPermissionWrappers.clear();
389
390         for ( UserPermission userPermission : userPermissions )
391         {
392             UserPermissionWrapper userPermissionWrapper = new UserPermissionWrapper( userPermission );
393
394             userPermissionWrappers.add( userPermissionWrapper );
395         }
396
397         tableViewer.refresh();
398     }
399
400
401     /**
402      * Returns the user permissions as selected by the user.
403      *
404      * @return the user permissions
405      */

406     public Collection JavaDoc<UserPermission> getUserPermissions()
407     {
408         Collection JavaDoc<UserPermission> userPermissions = new ArrayList JavaDoc<UserPermission>();
409
410         for ( UserPermissionWrapper userPermissionWrapper : userPermissionWrappers )
411         {
412             userPermissions.add( userPermissionWrapper.userPermission );
413         }
414
415         return userPermissions;
416     }
417
418
419     /**
420      *
421      * @return the user permission that is selected in the table viewer, or null.
422      */

423     private UserPermissionWrapper getSelectedUserPermissionWrapper()
424     {
425         UserPermissionWrapper userPermissionWrapper = null;
426
427         IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection();
428         if ( !selection.isEmpty() )
429         {
430             Object JavaDoc element = selection.getFirstElement();
431             if ( element instanceof UserPermissionWrapper )
432             {
433                 userPermissionWrapper = ( UserPermissionWrapper ) element;
434             }
435         }
436
437         return userPermissionWrapper;
438     }
439
440
441     /**
442      * Opens the UserPermissionDialog and adds the composed
443      * user permission to the list.
444      */

445     private void addUserPermission()
446     {
447         UserPermissionDialog dialog = new UserPermissionDialog( getShell(), null, context );
448         if ( dialog.open() == UserPermissionDialog.OK && dialog.getUserPermission() != null )
449         {
450             UserPermissionWrapper userPermissionWrapper = new UserPermissionWrapper( dialog.getUserPermission() );
451             userPermissionWrappers.add( userPermissionWrapper );
452
453             tableViewer.refresh();
454         }
455     }
456
457
458     /**
459      * Opens the UserPermissionDialog with the currently selected
460      * user permission and puts the modified user permission into the list.
461      */

462     private void editUserPermission()
463     {
464         UserPermissionWrapper oldUserPermissionWrapper = getSelectedUserPermissionWrapper();
465         if ( oldUserPermissionWrapper != null )
466         {
467             UserPermissionDialog dialog = new UserPermissionDialog( getShell(),
468                 oldUserPermissionWrapper.userPermission, context );
469             if ( dialog.open() == UserPermissionDialog.OK )
470             {
471                 oldUserPermissionWrapper.userPermission = dialog.getUserPermission();
472                 tableViewer.refresh();
473             }
474         }
475     }
476
477
478     /**
479      * Deletes the currently selected user permission from list.
480      */

481     private void deleteUserPermission()
482     {
483         UserPermissionWrapper userPermissionWrapper = getSelectedUserPermissionWrapper();
484         if ( userPermissionWrapper != null )
485         {
486             userPermissionWrappers.remove( userPermissionWrapper );
487             tableViewer.refresh();
488         }
489     }
490
491
492     /**
493      * Called when an user permission is selected in table viewer.
494      * Updates the enabled/disabled state of the buttons.
495      */

496     private void userPermissionSelected()
497     {
498         UserPermissionWrapper userPermissionWrapper = getSelectedUserPermissionWrapper();
499
500         if ( userPermissionWrapper == null )
501         {
502             editButton.setEnabled( false );
503             deleteButton.setEnabled( false );
504         }
505         else
506         {
507             editButton.setEnabled( true );
508             deleteButton.setEnabled( true );
509         }
510     }
511
512 }
513
Popular Tags