KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > dialogs > ItemPermissionDialog


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.dialogs;
21
22
23 import java.util.Collection JavaDoc;
24
25 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
26 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
27 import org.apache.directory.ldapstudio.aciitemeditor.widgets.ACIItemGrantsAndDenialsComposite;
28 import org.apache.directory.ldapstudio.aciitemeditor.widgets.ACIItemUserClassesComposite;
29 import org.apache.directory.shared.ldap.aci.GrantAndDenial;
30 import org.apache.directory.shared.ldap.aci.ItemPermission;
31 import org.apache.directory.shared.ldap.aci.UserClass;
32 import org.eclipse.jface.dialogs.Dialog;
33 import org.eclipse.jface.dialogs.IDialogConstants;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Button;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Spinner;
45
46
47 /**
48  * A dialog to compose item permissions.
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class ItemPermissionDialog extends Dialog
54 {
55
56     /** The context */
57     private ACIItemValueWithContext context;
58
59     /** The initial value, passed by the constructor */
60     private ItemPermission initialItemPermission;
61
62     /** The resulting value returned by getItemPermission() */
63     private ItemPermission returnItemPermission;
64
65     /** The precedence checkbox to enable/disable spinner */
66     private Button precedenceCheckbox = null;
67
68     /** The precedence spinner */
69     private Spinner precedenceSpinner = null;
70
71     /** The widget with user classes table */
72     private ACIItemUserClassesComposite userClassesComposite;
73
74     /** The widget with grants and denials table */
75     private ACIItemGrantsAndDenialsComposite grantsAndDenialsComposite;
76
77
78     /**
79      * Creates a new instance of ItemPermissionDialog.
80      *
81      * @param parentShell the shell
82      * @param initialItemPermission the initial item permission
83      * @param context the context
84      */

85     public ItemPermissionDialog( Shell parentShell, ItemPermission initialItemPermission,
86         ACIItemValueWithContext context )
87     {
88         super( parentShell );
89         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
90         this.context = context;
91         this.initialItemPermission = initialItemPermission;
92         this.returnItemPermission = null;
93     }
94
95
96     /**
97      * Sets the dialog image and text.
98      *
99      * {@inheritDoc}
100      */

101     protected void configureShell( Shell shell )
102     {
103         super.configureShell( shell );
104         shell.setText( Messages.getString( "ItemPermissionDialog.dialog.text" ) ); //$NON-NLS-1$
105
shell.setImage( Activator.getDefault().getImage( Messages.getString( "ItemPermissionDialog.dialog.icon" ) ) ); //$NON-NLS-1$
106
}
107
108
109     /**
110      * Reimplementation: Checks for valid syntax and sets the return value.
111      */

112     protected void okPressed()
113     {
114         try
115         {
116             int precedence = precedenceCheckbox.getSelection() ? precedenceSpinner.getSelection() : -1;
117             Collection JavaDoc<UserClass> userClasses = userClassesComposite.getUserClasses();
118             Collection JavaDoc<GrantAndDenial> grantsAndDenials = grantsAndDenialsComposite.getGrantsAndDenials();
119             returnItemPermission = new ItemPermission( precedence, grantsAndDenials, userClasses );
120             super.okPressed();
121         }
122         catch ( Exception JavaDoc e )
123         {
124             MessageDialog.openError( getShell(), Messages
125                 .getString( "ItemPermissionDialog.error.invalidItemPermission" ), e.getMessage() ); //$NON-NLS-1$
126
}
127     }
128
129
130     /**
131      * Creates all the dialog content.
132      *
133      * @param parent the parent
134      *
135      * @return the control
136      */

137     protected Control createDialogArea( Composite parent )
138     {
139         Composite composite = ( Composite ) super.createDialogArea( parent );
140         GridData gd = new GridData( GridData.FILL_BOTH );
141         gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
142         gd.heightHint = convertVerticalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ) * 4 / 3;
143         composite.setLayoutData( gd );
144
145         // precedence
146
Composite spinnerComposite = new Composite( composite, SWT.NONE );
147         spinnerComposite.setLayout( new GridLayout( 2, false ) );
148         spinnerComposite.setLayoutData( new GridData() );
149         precedenceCheckbox = new Button( spinnerComposite, SWT.CHECK );
150         precedenceCheckbox.setText( Messages.getString( "ItemPermissionDialog.precedence.label" ) ); //$NON-NLS-1$
151
precedenceCheckbox.addSelectionListener( new SelectionAdapter()
152         {
153             public void widgetSelected( SelectionEvent e )
154             {
155                 precedenceSpinner.setEnabled( precedenceCheckbox.getSelection() );
156             }
157         } );
158         precedenceSpinner = new Spinner( spinnerComposite, SWT.BORDER );
159         precedenceSpinner.setMinimum( 0 );
160         precedenceSpinner.setMaximum( 255 );
161         precedenceSpinner.setDigits( 0 );
162         precedenceSpinner.setIncrement( 1 );
163         precedenceSpinner.setPageIncrement( 10 );
164         precedenceSpinner.setSelection( 0 );
165         precedenceSpinner.setEnabled( false );
166         GridData precedenceGridData = new GridData();
167         precedenceGridData.grabExcessHorizontalSpace = true;
168         precedenceGridData.verticalAlignment = GridData.CENTER;
169         precedenceGridData.horizontalAlignment = GridData.BEGINNING;
170         precedenceGridData.widthHint = 3 * 12;
171         precedenceSpinner.setLayoutData( precedenceGridData );
172
173         // user classes
174
userClassesComposite = new ACIItemUserClassesComposite( composite, SWT.NONE );
175         userClassesComposite.setContext( context );
176
177         // grants and denial
178
grantsAndDenialsComposite = new ACIItemGrantsAndDenialsComposite( composite, SWT.NONE );
179
180         // set initial values
181
if ( initialItemPermission != null )
182         {
183             if ( initialItemPermission.getPrecedence() > -1 )
184             {
185                 precedenceCheckbox.setSelection( true );
186                 precedenceSpinner.setEnabled( true );
187                 precedenceSpinner.setSelection( initialItemPermission.getPrecedence() );
188             }
189             userClassesComposite.setUserClasses( initialItemPermission.getUserClasses() );
190             grantsAndDenialsComposite.setGrantsAndDenials( initialItemPermission.getGrantsAndDenials() );
191         }
192
193         applyDialogFont( composite );
194         return composite;
195     }
196
197
198     /**
199      * Returns the item permission. Returns null if Cancel button was pressed.
200      *
201      * @return the composed item permission or null
202      */

203     public ItemPermission getItemPermission()
204     {
205         return returnItemPermission;
206     }
207
208 }
209
Popular Tags