KickJava   Java API By Example, From Geeks To Geeks.

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


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.ItemPermissionDialog;
31 import org.apache.directory.ldapstudio.aciitemeditor.model.UserClassWrapper;
32 import org.apache.directory.shared.ldap.aci.GrantAndDenial;
33 import org.apache.directory.shared.ldap.aci.ItemPermission;
34 import org.apache.directory.shared.ldap.aci.UserClass;
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 item permissions.
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class ACIItemItemPermissionsComposite 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 item 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 item permissions, input of the table viewer */
91     private List JavaDoc<ItemPermissionWrapper> itemPermissionWrappers = new ArrayList JavaDoc<ItemPermissionWrapper>();
92
93     /**
94      * ItemPermissionWrappers 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 ItemPermissionWrapper
100     {
101         /** The item permission bean. */
102         private ItemPermission itemPermission;
103
104
105         /**
106          * Creates a new instance of ItemPermissionWrapper.
107          *
108          * @param itemClassClass
109          */

110         private ItemPermissionWrapper( ItemPermission itemPermission )
111         {
112             this.itemPermission = itemPermission;
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 ( itemPermission == null )
124             {
125                 return "<UNKNOWN>"; //$NON-NLS-1$
126
}
127             else
128             {
129                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
130                 if ( itemPermission.getPrecedence() > -1 )
131                 {
132                     buffer.append( '(' );
133                     buffer.append( itemPermission.getPrecedence() );
134                     buffer.append( ')' );
135                     buffer.append( ' ' );
136                 }
137                 for ( Iterator JavaDoc<UserClass> it = ( ( Collection JavaDoc<UserClass> ) itemPermission.getUserClasses() ).iterator(); it
138                     .hasNext(); )
139                 {
140                     UserClass uc = it.next();
141                     String JavaDoc s = UserClassWrapper.classToDisplayMap.get( uc.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> ) itemPermission
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      *
182      * Creates a new instance of ACIItemItemPermissionsComposite.
183      *
184      * @param parent
185      * @param style
186      */

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

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

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

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

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

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

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

387     public void setItemPermissions( Collection JavaDoc<ItemPermission> itemPermissions )
388     {
389         itemPermissionWrappers.clear();
390
391         for ( ItemPermission itemPermission : itemPermissions )
392         {
393             ItemPermissionWrapper itemPermissionWrapper = new ItemPermissionWrapper( itemPermission );
394
395             itemPermissionWrappers.add( itemPermissionWrapper );
396         }
397
398         tableViewer.refresh();
399     }
400
401
402     /**
403      * Returns the item permissions as selected by the user.
404      *
405      * @return the item permissions
406      */

407     public Collection JavaDoc<ItemPermission> getItemPermissions()
408     {
409         Collection JavaDoc<ItemPermission> itemPermissions = new ArrayList JavaDoc<ItemPermission>();
410
411         for ( ItemPermissionWrapper itemPermissionWrapper : itemPermissionWrappers )
412         {
413             itemPermissions.add( itemPermissionWrapper.itemPermission );
414         }
415
416         return itemPermissions;
417     }
418
419
420     /**
421      *
422      * @return the item permission that is selected in the table viewer, or null.
423      */

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

446     private void addItemPermission()
447     {
448         ItemPermissionDialog dialog = new ItemPermissionDialog( getShell(), null, context );
449         if ( dialog.open() == ItemPermissionDialog.OK && dialog.getItemPermission() != null )
450         {
451             ItemPermissionWrapper itemPermissionWrapper = new ItemPermissionWrapper( dialog.getItemPermission() );
452             itemPermissionWrappers.add( itemPermissionWrapper );
453
454             tableViewer.refresh();
455         }
456     }
457
458
459     /**
460      * Opens the ItemPermissionDialog with the currently selected
461      * item permission and puts the modified item permission into the list.
462      */

463     private void editItemPermission()
464     {
465         ItemPermissionWrapper oldItemPermissionWrapper = getSelectedItemPermissionWrapper();
466         if ( oldItemPermissionWrapper != null )
467         {
468             ItemPermissionDialog dialog = new ItemPermissionDialog( getShell(),
469                 oldItemPermissionWrapper.itemPermission, context );
470             if ( dialog.open() == ItemPermissionDialog.OK )
471             {
472                 oldItemPermissionWrapper.itemPermission = dialog.getItemPermission();
473                 tableViewer.refresh();
474             }
475         }
476     }
477
478
479     /**
480      * Deletes the currently selected item permission from list.
481      */

482     private void deleteItemPermission()
483     {
484         ItemPermissionWrapper itemPermissionWrapper = getSelectedItemPermissionWrapper();
485         if ( itemPermissionWrapper != null )
486         {
487             itemPermissionWrappers.remove( itemPermissionWrapper );
488             tableViewer.refresh();
489         }
490     }
491
492
493     /**
494      * Called when an item permission is selected in table viewer.
495      * Updates the enabled/disabled state of the buttons.
496      */

497     private void itemPermissionSelected()
498     {
499         ItemPermissionWrapper itemPermissionWrapper = getSelectedItemPermissionWrapper();
500
501         if ( itemPermissionWrapper == null )
502         {
503             editButton.setEnabled( false );
504             deleteButton.setEnabled( false );
505         }
506         else
507         {
508             editButton.setEnabled( true );
509             deleteButton.setEnabled( true );
510         }
511     }
512
513 }
514
Popular Tags