KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
30 import org.apache.directory.shared.ldap.aci.GrantAndDenial;
31 import org.eclipse.jface.viewers.ArrayContentProvider;
32 import org.eclipse.jface.viewers.CellEditor;
33 import org.eclipse.jface.viewers.CheckboxCellEditor;
34 import org.eclipse.jface.viewers.ICellModifier;
35 import org.eclipse.jface.viewers.ITableLabelProvider;
36 import org.eclipse.jface.viewers.ITreeContentProvider;
37 import org.eclipse.jface.viewers.LabelProvider;
38 import org.eclipse.jface.viewers.TreeViewer;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Item;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Tree;
50 import org.eclipse.swt.widgets.TreeColumn;
51
52
53 /**
54  * This composite contains GUI elements to edit ACI item grants and denials.
55
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class ACIItemGrantsAndDenialsComposite extends Composite
61 {
62
63     /** The description label */
64     private Label label = null;
65
66     /** The tree control for the tree viewer */
67     private Tree tree = null;
68
69     /** The tree viewer containing all grants and denials */
70     private TreeViewer treeViewer = null;
71
72     /** The composite containing the buttons */
73     private Composite buttonComposite = null;
74
75     /** The grant all button */
76     private Button grantAllButton = null;
77
78     /** The deny all button */
79     private Button denyAllButton = null;
80
81     /** The deselect all button */
82     private Button deselectAllButton = null;
83
84     /** The undo button */
85     private Button undoButton = null;
86
87     /** The redo button */
88     private Button redoButton = null;
89
90     /** Colum 1 */
91     private static String JavaDoc PERMISSION = Messages.getString( "ACIItemGrantsAndDenialsComposite.column1.header" ); //$NON-NLS-1$
92

93     /** Colum2 */
94     private static String JavaDoc STATE = Messages.getString( "ACIItemGrantsAndDenialsComposite.column2.header" ); //$NON-NLS-1$
95

96     /** The colums */
97     private static String JavaDoc[] COLUMNS = new String JavaDoc[]
98         { PERMISSION, STATE };
99
100     /** The undo/redo stack size */
101     private static final int MAX_STACK_SIZE = 25;
102
103     /** Used as input for the tree viewer */
104     private GrantAndDenialCategory[] grantAndDenialCategories = new GrantAndDenialCategory[]
105         {
106             new GrantAndDenialCategory(
107                 Messages.getString( "ACIItemGrantsAndDenialsComposite.category.read" ), true, new GrantAndDenialWrapper[] //$NON-NLS-1$
108
{
109                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_BROWSE, GrantAndDenial.DENY_BROWSE ),
110                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_READ, GrantAndDenial.DENY_READ ),
111                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_COMPARE, GrantAndDenial.DENY_COMPARE ),
112                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_FILTER_MATCH, GrantAndDenial.DENY_FILTER_MATCH ),
113                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_RETURN_DN, GrantAndDenial.DENY_RETURN_DN ) } ),
114             new GrantAndDenialCategory(
115                 Messages.getString( "ACIItemGrantsAndDenialsComposite.category.modify" ), true, new GrantAndDenialWrapper[] //$NON-NLS-1$
116
{ new GrantAndDenialWrapper( GrantAndDenial.GRANT_ADD, GrantAndDenial.DENY_ADD ),
117                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_MODIFY, GrantAndDenial.DENY_MODIFY ),
118                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_REMOVE, GrantAndDenial.DENY_REMOVE ),
119                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_RENAME, GrantAndDenial.DENY_RENAME ) } ),
120             new GrantAndDenialCategory(
121                 Messages.getString( "ACIItemGrantsAndDenialsComposite.category.advanced" ), false, new GrantAndDenialWrapper[] //$NON-NLS-1$
122
{
123                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_EXPORT, GrantAndDenial.DENY_EXPORT ),
124                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_IMPORT, GrantAndDenial.DENY_IMPORT ),
125                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_INVOKE, GrantAndDenial.DENY_INVOKE ),
126                         new GrantAndDenialWrapper( GrantAndDenial.GRANT_DISCLOSE_ON_ERROR,
127                             GrantAndDenial.DENY_DISCLOSE_ON_ERROR ) } ) };
128
129     /**
130      * A GrantAndDenialCategory is used to categorize grants and denials in a tree.
131      */

132     private class GrantAndDenialCategory
133     {
134         /** The category name, displayed in tree */
135         private String JavaDoc name;
136
137         /** The initial expanded state */
138         private boolean expanded;
139
140         /** The grants and denials wrappers display under this category */
141         private GrantAndDenialWrapper[] grantAndDenialWrappers;
142
143
144         /**
145          * Creates a new instance of GrantAndDenialCategory.
146          *
147          * @param name the category name, displayed in tree
148          * @param expanded true if category should be initially expanded
149          * @param grantAndDenialWrappers the grants and denials wrappers display under this category
150          */

151         private GrantAndDenialCategory( String JavaDoc name, boolean expanded, GrantAndDenialWrapper[] grantAndDenialWrappers )
152         {
153             this.name = name;
154             this.expanded = expanded;
155             this.grantAndDenialWrappers = grantAndDenialWrappers;
156         }
157     }
158
159     /**
160      * A GrantAndDenialWrapper is used to display grants and denials in tree and to
161      * track the current state (not specified, grant or deny). Additional it provides
162      * undo/redo functionality.
163      *
164      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
165      * @version $Rev$, $Date$
166      */

167     private class GrantAndDenialWrapper
168     {
169         /** The grant */
170         private GrantAndDenial grant;
171
172         /** The denial */
173         private GrantAndDenial denial;
174
175         /** The current state: null=not specified, grant or denial */
176         private GrantAndDenial activeGrantAndDenial;
177
178         /** List containing previous states of activeGrandAndDenial */
179         private List JavaDoc<GrantAndDenial> undoStack;
180
181         /** List containing "future" states of activeGrandAndDenial */
182         private List JavaDoc<GrantAndDenial> redoStack;
183
184
185         /**
186          * Creates a new instance of GrantAndDenialWrapper.
187          *
188          * @param grant
189          * @param denial
190          */

191         private GrantAndDenialWrapper( GrantAndDenial grant, GrantAndDenial denial )
192         {
193             this.grant = grant;
194             this.denial = denial;
195             this.activeGrantAndDenial = null;
196             undoStack = new LinkedList JavaDoc<GrantAndDenial>();
197             redoStack = new LinkedList JavaDoc<GrantAndDenial>();
198         }
199     }
200
201
202     /**
203      * Creates a new instance of ACIItemGrantsAndDenialsComposite.
204      *
205      * @param parent
206      * @param style
207      */

208     public ACIItemGrantsAndDenialsComposite( Composite parent, int style )
209     {
210         super( parent, style );
211
212         GridLayout layout = new GridLayout();
213         layout.makeColumnsEqualWidth = false;
214         layout.numColumns = 2;
215         setLayout( layout );
216
217         GridData layoutData = new GridData();
218         layoutData.grabExcessHorizontalSpace = true;
219         layoutData.grabExcessVerticalSpace = true;
220         layoutData.horizontalAlignment = GridData.FILL;
221         layoutData.verticalAlignment = GridData.FILL;
222         setLayoutData( layoutData );
223
224         GridData labelGridData = new GridData();
225         labelGridData.horizontalSpan = 2;
226         labelGridData.verticalAlignment = GridData.CENTER;
227         labelGridData.grabExcessHorizontalSpace = true;
228         labelGridData.horizontalAlignment = GridData.FILL;
229
230         label = new Label( this, SWT.NONE );
231         label.setText( Messages.getString( "ACIItemGrantsAndDenialsComposite.description" ) ); //$NON-NLS-1$
232
label.setLayoutData( labelGridData );
233
234         createTree();
235
236         createButtonComposite();
237     }
238
239
240     /**
241      * This method initializes tree
242      *
243      */

244     private void createTree()
245     {
246         GridData tableGridData = new GridData( GridData.FILL_BOTH );
247         tableGridData.grabExcessHorizontalSpace = true;
248         tableGridData.grabExcessVerticalSpace = true;
249         tableGridData.verticalAlignment = GridData.FILL;
250         tableGridData.horizontalAlignment = GridData.FILL;
251         //tableGridData.heightHint = 100;
252

253         tree = new Tree( this, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION
254             | SWT.HIDE_SELECTION );
255         tree.setHeaderVisible( true );
256         tree.setLayoutData( tableGridData );
257         tree.setLinesVisible( true );
258
259         TreeColumn c1 = new TreeColumn( tree, SWT.LEFT, 0 );
260         c1.setText( COLUMNS[0] );
261         c1.setWidth( 160 );
262         TreeColumn c2 = new TreeColumn( tree, SWT.LEFT, 1 );
263         c2.setText( COLUMNS[1] );
264         c2.setWidth( 80 );
265         // TreeColumn c3 = new TreeColumn( tree, SWT.LEFT, 2 );
266
// c3.setText( " " ); //$NON-NLS-1$
267
// c3.setWidth( 0 );
268

269         treeViewer = new TreeViewer( tree );
270         treeViewer.setUseHashlookup( true );
271
272         treeViewer.setColumnProperties( COLUMNS );
273
274         ICellModifier cellModifier = new GrantsAndDenialsCellModifier();
275         treeViewer.setCellModifier( cellModifier );
276         CellEditor[] cellEditors = new CellEditor[]
277             { null, new CheckboxCellEditor( tree ), null };
278         treeViewer.setCellEditors( cellEditors );
279
280         treeViewer.setContentProvider( new GrantsAndDenialsContentProvider() );
281         treeViewer.setLabelProvider( new GrantsAndDenialsLabelProvider() );
282         treeViewer.setInput( grantAndDenialCategories );
283
284         // set expanded state
285
List JavaDoc<GrantAndDenialCategory> expandedList = new ArrayList JavaDoc<GrantAndDenialCategory>();
286         for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
287         {
288             if ( grantAndDenialCategory.expanded )
289             {
290                 expandedList.add( grantAndDenialCategory );
291             }
292         }
293         treeViewer.setExpandedElements( expandedList.toArray() );
294     }
295
296
297     /**
298      * This method initializes buttonComposite
299      *
300      */

301     private void createButtonComposite()
302     {
303         GridData deselectAllButtonGridData = new GridData();
304         deselectAllButtonGridData.horizontalAlignment = GridData.FILL;
305         deselectAllButtonGridData.grabExcessHorizontalSpace = false;
306         deselectAllButtonGridData.verticalAlignment = GridData.BEGINNING;
307         deselectAllButtonGridData.widthHint = Activator.getButtonWidth( this );
308
309         GridData denyAllButtonGridData = new GridData();
310         denyAllButtonGridData.horizontalAlignment = GridData.FILL;
311         denyAllButtonGridData.grabExcessHorizontalSpace = false;
312         denyAllButtonGridData.verticalAlignment = GridData.BEGINNING;
313         denyAllButtonGridData.widthHint = Activator.getButtonWidth( this );
314
315         GridData grantAllButtonGridData = new GridData();
316         grantAllButtonGridData.horizontalAlignment = GridData.FILL;
317         grantAllButtonGridData.grabExcessHorizontalSpace = false;
318         grantAllButtonGridData.verticalAlignment = GridData.BEGINNING;
319         grantAllButtonGridData.widthHint = Activator.getButtonWidth( this );
320
321         GridData undoButtonGridData = new GridData();
322         undoButtonGridData.horizontalAlignment = GridData.FILL;
323         undoButtonGridData.grabExcessHorizontalSpace = false;
324         undoButtonGridData.verticalAlignment = GridData.BEGINNING;
325         undoButtonGridData.widthHint = Activator.getButtonWidth( this );
326
327         GridData redoButtonGridData = new GridData();
328         redoButtonGridData.horizontalAlignment = GridData.FILL;
329         redoButtonGridData.grabExcessHorizontalSpace = false;
330         redoButtonGridData.verticalAlignment = GridData.BEGINNING;
331         redoButtonGridData.widthHint = Activator.getButtonWidth( this );
332
333         GridLayout gridLayout = new GridLayout();
334         gridLayout.marginWidth = 0;
335         gridLayout.marginHeight = 0;
336         GridData gridData = new GridData();
337         gridData.horizontalAlignment = GridData.CENTER;
338         gridData.grabExcessHorizontalSpace = false;
339         gridData.grabExcessVerticalSpace = false;
340         gridData.verticalAlignment = GridData.FILL;
341
342         buttonComposite = new Composite( this, SWT.NONE );
343         buttonComposite.setLayoutData( gridData );
344         buttonComposite.setLayout( gridLayout );
345
346         grantAllButton = new Button( buttonComposite, SWT.NONE );
347         grantAllButton.setText( Messages.getString( "ACIItemGrantsAndDenialsComposite.grantAll.button" ) ); //$NON-NLS-1$
348
grantAllButton.setLayoutData( grantAllButtonGridData );
349         grantAllButton.addSelectionListener( new SelectionAdapter()
350         {
351             public void widgetSelected( SelectionEvent e )
352             {
353                 backup();
354                 for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
355                 {
356                     for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
357                     {
358                         grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.grant;
359                     }
360                 }
361                 treeViewer.refresh();
362             }
363         } );
364
365         denyAllButton = new Button( buttonComposite, SWT.NONE );
366         denyAllButton.setText( Messages.getString( "ACIItemGrantsAndDenialsComposite.denyAll.button" ) ); //$NON-NLS-1$
367
denyAllButton.setLayoutData( denyAllButtonGridData );
368         denyAllButton.addSelectionListener( new SelectionAdapter()
369         {
370             public void widgetSelected( SelectionEvent e )
371             {
372                 backup();
373                 for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
374                 {
375                     for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
376                     {
377                         grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.denial;
378                     }
379                 }
380                 treeViewer.refresh();
381             }
382         } );
383
384         deselectAllButton = new Button( buttonComposite, SWT.NONE );
385         deselectAllButton.setText( Messages.getString( "ACIItemGrantsAndDenialsComposite.deselectAll.button" ) ); //$NON-NLS-1$
386
deselectAllButton.setLayoutData( deselectAllButtonGridData );
387         deselectAllButton.addSelectionListener( new SelectionAdapter()
388         {
389             public void widgetSelected( SelectionEvent e )
390             {
391                 backup();
392                 for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
393                 {
394                     for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
395                     {
396                         grantAndDenialWrapper.activeGrantAndDenial = null;
397                     }
398                 }
399                 treeViewer.refresh();
400             }
401         } );
402
403         undoButton = new Button( buttonComposite, SWT.NONE );
404         undoButton.setText( Messages.getString( "ACIItemGrantsAndDenialsComposite.undo.button" ) ); //$NON-NLS-1$
405
undoButton.setLayoutData( undoButtonGridData );
406         undoButton.addSelectionListener( new SelectionAdapter()
407         {
408             public void widgetSelected( SelectionEvent e )
409             {
410                 undo();
411                 treeViewer.refresh();
412             }
413         } );
414         undoButton.setEnabled( false );
415
416         redoButton = new Button( buttonComposite, SWT.NONE );
417         redoButton.setText( Messages.getString( "ACIItemGrantsAndDenialsComposite.redo.button" ) ); //$NON-NLS-1$
418
redoButton.setLayoutData( redoButtonGridData );
419         redoButton.addSelectionListener( new SelectionAdapter()
420         {
421             public void widgetSelected( SelectionEvent e )
422             {
423                 redo();
424                 treeViewer.refresh();
425             }
426         } );
427         redoButton.setEnabled( false );
428
429     }
430
431     /**
432      * The ICellModifier user for this tree viewer.
433      *
434      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
435      * @version $Rev$, $Date$
436      */

437     private class GrantsAndDenialsCellModifier implements ICellModifier
438     {
439
440         /**
441          * Only GrantAndDenialWrappers and the STATE colum is modifyable.
442          *
443          * @param element the element
444          * @param property the property
445          *
446          * @return true, if can modify
447          */

448         public boolean canModify( Object JavaDoc element, String JavaDoc property )
449         {
450             if ( element instanceof GrantAndDenialWrapper )
451             {
452                 return property.equals( STATE );
453             }
454
455             return false;
456         }
457
458
459         /**
460          * The used CheckboxCellEditor accepts only Booleans.
461          *
462          * @param element the element
463          * @param property the property
464          *
465          * @return the value
466          */

467         public Object JavaDoc getValue( Object JavaDoc element, String JavaDoc property )
468         {
469             if ( element instanceof GrantAndDenialWrapper )
470             {
471                 if ( property.equals( STATE ) )
472                 {
473                     return new Boolean JavaDoc( true );
474                 }
475             }
476
477             return null;
478         }
479
480
481         /**
482          * Performs the tree-state transtion.
483          *
484          * @param element the element
485          * @param value the value
486          * @param property the property
487          */

488         public void modify( Object JavaDoc element, String JavaDoc property, Object JavaDoc value )
489         {
490             if ( element != null && element instanceof Item )
491             {
492                 element = ( ( Item ) element ).getData();
493             }
494
495             if ( element instanceof GrantAndDenialWrapper )
496             {
497                 GrantAndDenialWrapper grantAndDenialWrapper = ( GrantAndDenialWrapper ) element;
498
499                 if ( property.equals( STATE ) )
500                 {
501                     backup();
502                     if ( grantAndDenialWrapper.activeGrantAndDenial == null )
503                     {
504                         grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.grant;
505                     }
506                     else if ( grantAndDenialWrapper.activeGrantAndDenial == grantAndDenialWrapper.grant )
507                     {
508                         grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.denial;
509                     }
510                     else if ( grantAndDenialWrapper.activeGrantAndDenial == grantAndDenialWrapper.denial )
511                     {
512                         grantAndDenialWrapper.activeGrantAndDenial = null;
513                     }
514                 }
515             }
516
517             treeViewer.refresh();
518         }
519
520     }
521
522     /**
523      * The content provider used for this tree viewer.
524      *
525      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
526      * @version $Rev$, $Date$
527      */

528     private class GrantsAndDenialsContentProvider extends ArrayContentProvider implements ITreeContentProvider
529     {
530
531         /**
532          * Only GrantAndDenialCategories have children.
533          *
534          * @param parentElement the parent element
535          *
536          * @return the children
537          */

538         public Object JavaDoc[] getChildren( Object JavaDoc parentElement )
539         {
540             if ( parentElement instanceof GrantAndDenialCategory )
541             {
542                 GrantAndDenialCategory cat = ( GrantAndDenialCategory ) parentElement;
543                 return cat.grantAndDenialWrappers;
544             }
545
546             return null;
547         }
548
549
550         /**
551          * Not used.
552          *
553          * @param element the element
554          *
555          * @return the parent
556          */

557         public Object JavaDoc getParent( Object JavaDoc element )
558         {
559             return null;
560         }
561
562
563         /**
564          * Only GrantAndDenialCategories have children.
565          *
566          * @param element the element
567          *
568          * @return true, if has children
569          */

570         public boolean hasChildren( Object JavaDoc element )
571         {
572             return ( element instanceof GrantAndDenialCategory );
573         }
574
575     }
576
577     /**
578      * The label provider used for this tree viewer.
579      *
580      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
581      * @version $Rev$, $Date$
582      */

583     private class GrantsAndDenialsLabelProvider extends LabelProvider implements ITableLabelProvider
584     {
585
586         /**
587          * The STATE is displayed as image.
588          *
589          * @param element the element
590          * @param columnIndex the column index
591          *
592          * @return the column image
593          */

594         public Image getColumnImage( Object JavaDoc element, int columnIndex )
595         {
596             if ( element instanceof GrantAndDenialWrapper )
597             {
598                 GrantAndDenialWrapper grantAndDenialWrapper = ( GrantAndDenialWrapper ) element;
599                 switch ( columnIndex )
600                 {
601                     case 0:
602                         return null;
603                     case 1:
604                         if ( grantAndDenialWrapper.activeGrantAndDenial == null )
605                         {
606                             return Activator.getDefault().getImage(
607                                 Messages.getString( "ACIItemGrantsAndDenialsComposite.unspecified.icon" ) ); //$NON-NLS-1$
608
}
609                         else if ( grantAndDenialWrapper.activeGrantAndDenial == grantAndDenialWrapper.grant )
610                         {
611                             return Activator.getDefault().getImage(
612                                 Messages.getString( "ACIItemGrantsAndDenialsComposite.grant.icon" ) ); //$NON-NLS-1$
613
}
614                         else if ( grantAndDenialWrapper.activeGrantAndDenial == grantAndDenialWrapper.denial )
615                         {
616                             return Activator.getDefault().getImage(
617                                 Messages.getString( "ACIItemGrantsAndDenialsComposite.deny.icon" ) ); //$NON-NLS-1$
618
}
619                     case 2:
620                         return null;
621                 }
622             }
623             return null;
624         }
625
626
627         /**
628          * Returns GrantAndDenialCategory name or the MicroOperation name.
629          *
630          * @param element the element
631          * @param columnIndex the column index
632          *
633          * @return the column text
634          */

635         public String JavaDoc getColumnText( Object JavaDoc element, int columnIndex )
636         {
637             if ( element instanceof GrantAndDenialCategory )
638             {
639                 if ( columnIndex == 0 )
640                 {
641                     GrantAndDenialCategory cat = ( GrantAndDenialCategory ) element;
642                     return cat.name;
643                 }
644             }
645             else if ( element instanceof GrantAndDenialWrapper )
646             {
647                 if ( columnIndex == 0 )
648                 {
649                     GrantAndDenialWrapper wrapper = ( GrantAndDenialWrapper ) element;
650                     return wrapper.grant.getMicroOperation().getName();
651                 }
652             }
653
654             return ""; //$NON-NLS-1$
655
}
656
657     }
658
659
660     /**
661      * Sets the grants and denials.
662      *
663      * @param grantsAndDenials
664      */

665     public void setGrantsAndDenials( Collection JavaDoc<GrantAndDenial> grantsAndDenials )
666     {
667         for ( GrantAndDenial grantAndDenial : grantsAndDenials )
668         {
669             for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
670             {
671                 for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
672                 {
673                     if ( grantAndDenialWrapper.grant == grantAndDenial )
674                     {
675                         grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.grant;
676                     }
677                     else if ( grantAndDenialWrapper.denial == grantAndDenial )
678                     {
679                         grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.denial;
680                     }
681                 }
682             }
683         }
684
685         treeViewer.refresh();
686     }
687
688
689     /**
690      * Returns the grants and denials as selected by the user.
691      *
692      * @return the grants and denials
693      * @throws ParseException
694      */

695     public Collection JavaDoc<GrantAndDenial> getGrantsAndDenials() throws ParseException JavaDoc
696     {
697         Collection JavaDoc<GrantAndDenial> grantsAndDenials = new ArrayList JavaDoc<GrantAndDenial>();
698
699         for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
700         {
701             for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
702             {
703                 if ( grantAndDenialWrapper.activeGrantAndDenial != null )
704                 {
705                     grantsAndDenials.add( grantAndDenialWrapper.activeGrantAndDenial );
706                 }
707             }
708         }
709
710         return grantsAndDenials;
711     }
712
713
714     /**
715      * Undos the last modification.
716      */

717     private void undo()
718     {
719         for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
720         {
721             for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
722             {
723                 if ( grantAndDenialWrapper.undoStack.size() > 0 )
724                 {
725                     grantAndDenialWrapper.redoStack.add( 0, grantAndDenialWrapper.activeGrantAndDenial );
726                     grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.undoStack.remove( 0 );
727                 }
728
729                 undoButton.setEnabled( !grantAndDenialWrapper.undoStack.isEmpty() );
730                 redoButton.setEnabled( !grantAndDenialWrapper.redoStack.isEmpty() );
731             }
732         }
733     }
734
735
736     /**
737      * Redos the last modification
738      */

739     private void redo()
740     {
741         for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
742         {
743             for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
744             {
745                 if ( grantAndDenialWrapper.redoStack.size() > 0 )
746                 {
747                     grantAndDenialWrapper.undoStack.add( 0, grantAndDenialWrapper.activeGrantAndDenial );
748                     grantAndDenialWrapper.activeGrantAndDenial = grantAndDenialWrapper.redoStack.remove( 0 );
749                 }
750
751                 undoButton.setEnabled( !grantAndDenialWrapper.undoStack.isEmpty() );
752                 redoButton.setEnabled( !grantAndDenialWrapper.redoStack.isEmpty() );
753             }
754         }
755     }
756
757
758     /**
759      * Saves the current state to the undo stack.
760      */

761     private void backup()
762     {
763         for ( GrantAndDenialCategory grantAndDenialCategory : grantAndDenialCategories )
764         {
765             for ( GrantAndDenialWrapper grantAndDenialWrapper : grantAndDenialCategory.grantAndDenialWrappers )
766             {
767                 if ( grantAndDenialWrapper.undoStack.size() == MAX_STACK_SIZE )
768                 {
769                     grantAndDenialWrapper.undoStack.remove( grantAndDenialWrapper.undoStack.size() - 1 );
770                 }
771                 grantAndDenialWrapper.undoStack.add( 0, grantAndDenialWrapper.activeGrantAndDenial );
772                 grantAndDenialWrapper.redoStack.clear();
773
774                 undoButton.setEnabled( !grantAndDenialWrapper.undoStack.isEmpty() );
775                 redoButton.setEnabled( !grantAndDenialWrapper.redoStack.isEmpty() );
776             }
777         }
778     }
779
780 }
781
Popular Tags