KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.ParseException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
30 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
31 import org.apache.directory.ldapstudio.aciitemeditor.dialogs.MultiValuedDialog;
32 import org.apache.directory.ldapstudio.aciitemeditor.model.UserClassWrapper;
33 import org.apache.directory.ldapstudio.aciitemeditor.model.UserClassWrapperFactory;
34 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
35 import org.apache.directory.shared.ldap.aci.ProtectedItem;
36 import org.apache.directory.shared.ldap.aci.UserClass;
37 import org.eclipse.jface.viewers.ArrayContentProvider;
38 import org.eclipse.jface.viewers.CheckStateChangedEvent;
39 import org.eclipse.jface.viewers.CheckboxTableViewer;
40 import org.eclipse.jface.viewers.DoubleClickEvent;
41 import org.eclipse.jface.viewers.ICheckStateListener;
42 import org.eclipse.jface.viewers.IDoubleClickListener;
43 import org.eclipse.jface.viewers.ISelectionChangedListener;
44 import org.eclipse.jface.viewers.IStructuredSelection;
45 import org.eclipse.jface.viewers.LabelProvider;
46 import org.eclipse.jface.viewers.SelectionChangedEvent;
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.events.SelectionAdapter;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.graphics.Image;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.swt.widgets.Table;
57
58
59 /**
60  * This composite contains GUI elements to edit ACI item user classes.
61  *
62  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
63  * @version $Rev$, $Date$
64  */

65 public class ACIItemUserClassesComposite extends Composite
66 {
67
68     /** The context. */
69     private ACIItemValueWithContext context;
70
71     /** The inner composite for all the content */
72     private Composite composite = null;
73
74     /** The description label */
75     private Label label = null;
76
77     /** The table control for the table viewer */
78     private Table table = null;
79
80     /** The table viewer containing all user classes */
81     private CheckboxTableViewer tableViewer = null;
82
83     /** The composite containing the buttons */
84     private Composite buttonComposite = null;
85
86     /** The edit button */
87     private Button editButton = null;
88
89     /** The select all button */
90     private Button selectAllButton = null;
91
92     /** The deselect all button */
93     private Button deselectAllButton = null;
94
95     /** The reverse button */
96     private Button reverseSelectionButton = null;
97
98     /** The possible user classes, used as input for the table viewer */
99     private UserClassWrapper[] userClassWrappers = UserClassWrapperFactory.createUserClassWrappers();
100
101
102     /**
103      * Creates a new instance of ACIItemUserClassesComposite.
104      *
105      * @param parent
106      * @param style
107      */

108     public ACIItemUserClassesComposite( Composite parent, int style )
109     {
110         super( parent, style );
111
112         GridLayout layout = new GridLayout();
113         layout.horizontalSpacing = 0;
114         layout.verticalSpacing = 0;
115         layout.marginHeight = 0;
116         layout.marginWidth = 0;
117         setLayout( layout );
118
119         GridData layoutData = new GridData();
120         layoutData.horizontalAlignment = GridData.FILL;
121         layoutData.grabExcessHorizontalSpace = true;
122         layoutData.verticalAlignment = GridData.CENTER;
123         setLayoutData( layoutData );
124
125         createComposite();
126     }
127
128
129     /**
130      * This method initializes composite
131      *
132      */

133     private void createComposite()
134     {
135
136         GridData labelGridData = new GridData();
137         labelGridData.horizontalSpan = 2;
138         labelGridData.verticalAlignment = GridData.CENTER;
139         labelGridData.grabExcessHorizontalSpace = true;
140         labelGridData.horizontalAlignment = GridData.FILL;
141
142         GridLayout gridLayout = new GridLayout();
143         gridLayout.makeColumnsEqualWidth = false;
144         gridLayout.numColumns = 2;
145
146         GridData gridData = new GridData();
147         gridData.horizontalAlignment = GridData.FILL;
148         gridData.grabExcessHorizontalSpace = true;
149         gridData.verticalSpan = 1;
150         gridData.verticalAlignment = GridData.BEGINNING;
151
152         composite = new Composite( this, SWT.NONE );
153         composite.setLayoutData( gridData );
154         composite.setLayout( gridLayout );
155
156         label = new Label( composite, SWT.NONE );
157         label.setText( Messages.getString( "ACIItemUserClassesComposite.description" ) ); //$NON-NLS-1$
158
label.setLayoutData( labelGridData );
159
160         createTable();
161
162         createButtonComposite();
163     }
164
165
166     /**
167      * This method initializes table and table viewer
168      *
169      */

170     private void createTable()
171     {
172         GridData tableGridData = new GridData();
173         tableGridData.grabExcessHorizontalSpace = true;
174         tableGridData.verticalAlignment = GridData.FILL;
175         tableGridData.horizontalAlignment = GridData.FILL;
176         //tableGridData.heightHint = 100;
177

178         table = new Table( composite, SWT.BORDER | SWT.CHECK );
179         table.setHeaderVisible( false );
180         table.setLayoutData( tableGridData );
181         table.setLinesVisible( false );
182         tableViewer = new CheckboxTableViewer( table );
183         tableViewer.setContentProvider( new ArrayContentProvider() );
184         tableViewer.setLabelProvider( new UserClassesLabelProvider() );
185         tableViewer.setInput( userClassWrappers );
186
187         tableViewer.addSelectionChangedListener( new ISelectionChangedListener()
188         {
189             public void selectionChanged( SelectionChangedEvent event )
190             {
191                 userClassSelected();
192             }
193         } );
194         tableViewer.addCheckStateListener( new ICheckStateListener()
195         {
196             public void checkStateChanged( CheckStateChangedEvent event )
197             {
198                 userClassChecked();
199             }
200         } );
201         tableViewer.addDoubleClickListener( new IDoubleClickListener()
202         {
203             public void doubleClick( DoubleClickEvent event )
204             {
205                 if ( editButton.isEnabled() )
206                 {
207                     editUserClass();
208                 }
209             }
210         } );
211     }
212
213
214     /**
215      * This method initializes buttons
216      *
217      */

218     private void createButtonComposite()
219     {
220         GridData reverseSelectionButtonGridData = new GridData();
221         reverseSelectionButtonGridData.horizontalAlignment = GridData.FILL;
222         reverseSelectionButtonGridData.grabExcessHorizontalSpace = false;
223         reverseSelectionButtonGridData.verticalAlignment = GridData.BEGINNING;
224         reverseSelectionButtonGridData.widthHint = Activator.getButtonWidth( this );
225
226         GridData deselectAllButtonGridData = new GridData();
227         deselectAllButtonGridData.horizontalAlignment = GridData.FILL;
228         deselectAllButtonGridData.grabExcessHorizontalSpace = false;
229         deselectAllButtonGridData.verticalAlignment = GridData.BEGINNING;
230         deselectAllButtonGridData.widthHint = Activator.getButtonWidth( this );
231
232         GridData selectAllButtonGridData = new GridData();
233         selectAllButtonGridData.horizontalAlignment = GridData.FILL;
234         selectAllButtonGridData.grabExcessHorizontalSpace = false;
235         selectAllButtonGridData.verticalAlignment = GridData.BEGINNING;
236         selectAllButtonGridData.widthHint = Activator.getButtonWidth( this );
237
238         GridData editButtonGridData = new GridData();
239         editButtonGridData.horizontalAlignment = GridData.FILL;
240         editButtonGridData.grabExcessHorizontalSpace = false;
241         editButtonGridData.verticalAlignment = GridData.BEGINNING;
242         editButtonGridData.widthHint = Activator.getButtonWidth( this );
243
244         GridLayout gridLayout = new GridLayout();
245         gridLayout.marginWidth = 0;
246         gridLayout.marginHeight = 0;
247         GridData gridData = new GridData();
248         gridData.horizontalAlignment = GridData.CENTER;
249         gridData.grabExcessHorizontalSpace = false;
250         gridData.grabExcessVerticalSpace = false;
251         gridData.verticalAlignment = GridData.FILL;
252
253         buttonComposite = new Composite( composite, SWT.NONE );
254         buttonComposite.setLayoutData( gridData );
255         buttonComposite.setLayout( gridLayout );
256
257         editButton = new Button( buttonComposite, SWT.NONE );
258         editButton.setText( Messages.getString( "ACIItemUserClassesComposite.edit.button" ) ); //$NON-NLS-1$
259
editButton.setLayoutData( editButtonGridData );
260         editButton.addSelectionListener( new SelectionAdapter()
261         {
262             public void widgetSelected( SelectionEvent e )
263             {
264                 editUserClass();
265             }
266         } );
267         editButton.setEnabled( false );
268
269         selectAllButton = new Button( buttonComposite, SWT.NONE );
270         selectAllButton.setText( Messages.getString( "ACIItemUserClassesComposite.selectAll.button" ) ); //$NON-NLS-1$
271
selectAllButton.setLayoutData( selectAllButtonGridData );
272         selectAllButton.addSelectionListener( new SelectionAdapter()
273         {
274             public void widgetSelected( SelectionEvent e )
275             {
276                 tableViewer.setCheckedElements( userClassWrappers );
277                 refreshTable();
278             }
279         } );
280
281         deselectAllButton = new Button( buttonComposite, SWT.NONE );
282         deselectAllButton.setText( Messages.getString( "ACIItemUserClassesComposite.deselectAll.button" ) ); //$NON-NLS-1$
283
deselectAllButton.setLayoutData( deselectAllButtonGridData );
284         deselectAllButton.addSelectionListener( new SelectionAdapter()
285         {
286             public void widgetSelected( SelectionEvent e )
287             {
288                 tableViewer.setCheckedElements( new ProtectedItem[0] );
289                 refreshTable();
290             }
291         } );
292
293         reverseSelectionButton = new Button( buttonComposite, SWT.NONE );
294         reverseSelectionButton.setText( Messages.getString( "ACIItemUserClassesComposite.revert.buton" ) ); //$NON-NLS-1$
295
reverseSelectionButton.setLayoutData( reverseSelectionButtonGridData );
296         reverseSelectionButton.addSelectionListener( new SelectionAdapter()
297         {
298             public void widgetSelected( SelectionEvent e )
299             {
300                 List JavaDoc<Object JavaDoc> elements = new ArrayList JavaDoc<Object JavaDoc>();
301                 elements.addAll( Arrays.asList( userClassWrappers ) );
302                 elements.removeAll( Arrays.asList( tableViewer.getCheckedElements() ) );
303                 tableViewer.setCheckedElements( elements.toArray() );
304                 refreshTable();
305             }
306         } );
307
308     }
309
310     /**
311      * The label provider used for this table viewer.
312      *
313      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
314      * @version $Rev$, $Date$
315      */

316     private class UserClassesLabelProvider extends LabelProvider
317     {
318
319         /**
320          * Returns the error icon if the user class is checked and invalid.
321          *
322          * @param element the element
323          *
324          * @return the image
325          */

326         public Image getImage( Object JavaDoc element )
327         {
328             if ( element instanceof UserClassWrapper )
329             {
330                 UserClassWrapper wrapper = ( UserClassWrapper ) element;
331                 if ( tableViewer.getChecked( wrapper ) )
332                 {
333                     try
334                     {
335                         wrapper.getUserClass();
336                     }
337                     catch ( ParseException JavaDoc e )
338                     {
339                         return Activator.getDefault().getImage(
340                             Messages.getString( "ACIItemUserClassesComposite.error.icon" ) ); //$NON-NLS-1$
341
}
342                 }
343             }
344
345             return null;
346         }
347     }
348
349
350     /**
351      * Sets the context.
352      *
353      * @param context the context
354      */

355     public void setContext( ACIItemValueWithContext context )
356     {
357         this.context = context;
358     }
359
360
361     /**
362      * Sets the user classes.
363      *
364      * @param userClasses the user classes
365      */

366     public void setUserClasses( Collection JavaDoc<UserClass> userClasses )
367     {
368         // reset first
369
for ( UserClassWrapper userClassWrapper : userClassWrappers )
370         {
371             tableViewer.setChecked( userClassWrapper, false );
372         }
373
374         for ( UserClass userClass : userClasses )
375         {
376             for ( UserClassWrapper userClassWrapper : userClassWrappers )
377             {
378                 if ( userClassWrapper.getClazz() == userClass.getClass() )
379                 {
380                     userClassWrapper.setUserClass( userClass );
381                     tableViewer.setChecked( userClassWrapper, true );
382                 }
383             }
384         }
385
386         refreshTable();
387     }
388
389
390     /**
391      * Returns the user classes as selected by the user.
392      *
393      * @return the user classes
394      * @throws ParseException if the user classes or its values are not valid.
395      */

396     public Collection JavaDoc<UserClass> getUserClasses() throws ParseException JavaDoc
397     {
398         Collection JavaDoc<UserClass> userClasses = new ArrayList JavaDoc<UserClass>();
399
400         for ( UserClassWrapper userClassWrapper : userClassWrappers )
401         {
402             if ( tableViewer.getChecked( userClassWrapper ) )
403             {
404                 UserClass userClass = userClassWrapper.getUserClass();
405                 userClasses.add( userClass );
406             }
407         }
408
409         return userClasses;
410     }
411
412
413     /**
414      * Shows or hides this composite.
415      *
416      * @param visible true if visible
417      */

418     public void setVisible( boolean visible )
419     {
420         super.setVisible( visible );
421         ( ( GridData ) getLayoutData() ).heightHint = visible ? -1 : 0;
422     }
423
424
425     /**
426      *
427      * @return the user class that is selected in the table viewer, or null.
428      */

429     private UserClassWrapper getSelectedUserClassWrapper()
430     {
431         UserClassWrapper userClassWrapper = null;
432
433         IStructuredSelection selection = ( IStructuredSelection ) tableViewer.getSelection();
434         if ( !selection.isEmpty() )
435         {
436             Object JavaDoc element = selection.getFirstElement();
437             if ( element instanceof UserClassWrapper )
438             {
439                 userClassWrapper = ( UserClassWrapper ) element;
440             }
441         }
442
443         return userClassWrapper;
444     }
445
446
447     /**
448      * Called, when a user class is selected in the table viewer.
449      * - enables/disables the edit button
450      *
451      */

452     private void userClassSelected()
453     {
454         UserClassWrapper userClassWrapper = getSelectedUserClassWrapper();
455
456         if ( userClassWrapper == null || !userClassWrapper.isEditable() )
457         {
458             editButton.setEnabled( false );
459         }
460         else
461         {
462             editButton.setEnabled( true );
463         }
464     }
465
466
467     /**
468      * Called, when a user class checkbox is checked or unchecked.
469      *
470      */

471     private void userClassChecked()
472     {
473         refreshTable();
474     }
475
476
477     /**
478      * Called, when pushing the edit button. Opens the editor.
479      */

480     private void editUserClass()
481     {
482         UserClassWrapper userClassWrapper = getSelectedUserClassWrapper();
483
484         AbstractDialogStringValueEditor editor = userClassWrapper.getValueEditor();
485         if ( editor != null )
486         {
487             MultiValuedDialog dialog = new MultiValuedDialog( getShell(), userClassWrapper.getDisplayName(),
488                 userClassWrapper.getValues(), context, editor );
489             dialog.open();
490             refreshTable();
491         }
492     }
493
494
495     /**
496      * Refreshes the table viewer.
497      */

498     private void refreshTable()
499     {
500         tableViewer.refresh();
501     }
502
503 }
504
Popular Tags