KickJava   Java API By Example, From Geeks To Geeks.

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


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.ACIItemProtectedItemsComposite;
29 import org.apache.directory.shared.ldap.aci.GrantAndDenial;
30 import org.apache.directory.shared.ldap.aci.ProtectedItem;
31 import org.apache.directory.shared.ldap.aci.UserPermission;
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 user permissions.
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class UserPermissionDialog extends Dialog
54 {
55
56     /** The context */
57     private ACIItemValueWithContext context;
58     
59     /** The initial value, passed by the constructor */
60     private UserPermission initialUserPermission;
61
62     /** The resulting value returned by getUserPermission() */
63     private UserPermission returnUserPermission;
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 protected items table */
72     private ACIItemProtectedItemsComposite protectedItemsComposite;
73
74     /** The widget with grants and denials table */
75     private ACIItemGrantsAndDenialsComposite grantsAndDenialsComposite;
76
77
78     /**
79      * Creates a new instance of UserPermissionDialog.
80      *
81      * @param parentShell the shell
82      * @param initialUserPermission the initial user permission
83      * @param context the context
84      */

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

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

111     protected void okPressed()
112     {
113         try
114         {
115             int precedence = precedenceCheckbox.getSelection() ? precedenceSpinner.getSelection() : -1;
116             Collection JavaDoc<ProtectedItem> protectedItems = protectedItemsComposite.getProtectedItems();
117             Collection JavaDoc<GrantAndDenial> grantsAndDenials = grantsAndDenialsComposite.getGrantsAndDenials();
118             returnUserPermission = new UserPermission( precedence, grantsAndDenials, protectedItems );
119             super.okPressed();
120         }
121         catch ( Exception JavaDoc e )
122         {
123             MessageDialog.openError( getShell(), Messages.getString("UserPermissionDialog.error.invalidUserPermission"), e.getMessage() ); //$NON-NLS-1$
124
}
125     }
126
127
128     /**
129      * {@inheritDoc}
130      */

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

197     public UserPermission getUserPermission()
198     {
199         return returnUserPermission;
200     }
201     
202 }
203
Popular Tags