KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
25
26 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext;
27 import org.apache.directory.shared.ldap.aci.ACIItem;
28 import org.apache.directory.shared.ldap.aci.ACIItemParser;
29 import org.apache.directory.shared.ldap.aci.AuthenticationLevel;
30 import org.apache.directory.shared.ldap.aci.ItemFirstACIItem;
31 import org.apache.directory.shared.ldap.aci.UserFirstACIItem;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.custom.ScrolledComposite;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37
38
39 /**
40  * This is the main widget of the ACI item visual editor. It manages
41  * the lifecyle of all other ACI item widgets. In particular it
42  * shows/hides the userFirst and itemFirst widgets depending on
43  * the user's selection.
44  * <p>
45  * It extends ScrolledComposite.
46  *
47  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
48  * @version $Rev$, $Date$
49  */

50 public class ACIItemVisualEditorComposite extends ScrolledComposite implements WidgetModifyListener
51 {
52
53     /** The inner composite for all the content */
54     private Composite composite = null;
55
56     /** The general composite contains id-tag, precedence, auth-level, userFirst/itemFirst */
57     private ACIItemGeneralComposite generalComposite = null;
58
59     /** The user classes composite used for userFirst selection */
60     private ACIItemUserClassesComposite userFirstUserClassesComposite = null;
61
62     /** The user permission composite used for userFirst selection */
63     private ACIItemUserPermissionsComposite userFirstUserPermissionsComposite = null;
64
65     /** The protected items composite used for itemFirst selection */
66     private ACIItemProtectedItemsComposite itemFirstProtectedItemsComposite = null;
67
68     /** The item permission composite used for itemFirst selection */
69     private ACIItemItemPermissionsComposite itemFirstItemPermissionsComposite = null;
70
71
72     /**
73      * Creates a new instance of ACIItemComposite.
74      *
75      * @param parent
76      * @param style
77      */

78     public ACIItemVisualEditorComposite( Composite parent, int style )
79     {
80         super( parent, style | SWT.H_SCROLL | SWT.V_SCROLL );
81         setExpandHorizontal( true );
82         setExpandVertical( true );
83
84         createComposite();
85
86         setContent( composite );
87         setMinSize( composite.computeSize( SWT.DEFAULT, SWT.DEFAULT ) );
88     }
89
90
91     /**
92      * This method initializes the inner composite with all contained widgets.
93      *
94      */

95     private void createComposite()
96     {
97         GridLayout gridLayout = new GridLayout();
98         gridLayout.numColumns = 1;
99         GridData gridData = new GridData();
100         gridData.horizontalAlignment = GridData.FILL;
101         gridData.grabExcessHorizontalSpace = true;
102         gridData.verticalAlignment = GridData.CENTER;
103
104         composite = new Composite( this, SWT.NONE );
105         composite.setLayout( gridLayout );
106         composite.setLayoutData( gridData );
107
108         generalComposite = new ACIItemGeneralComposite( composite, SWT.NONE );
109         generalComposite.addWidgetModifyListener( this );
110
111         userFirstUserClassesComposite = new ACIItemUserClassesComposite( composite, SWT.NONE );
112         userFirstUserPermissionsComposite = new ACIItemUserPermissionsComposite( composite, SWT.NONE );
113
114         itemFirstProtectedItemsComposite = new ACIItemProtectedItemsComposite( composite, SWT.NONE );
115         itemFirstItemPermissionsComposite = new ACIItemItemPermissionsComposite( composite, SWT.NONE );
116
117         widgetModified( null );
118     }
119
120
121     /**
122      * This method is called from the contained ACIItemXXXComposites
123      * when they are modified.
124      *
125      * @param event the event
126      */

127     public void widgetModified( WidgetModifyEvent event )
128     {
129         // switch userFirst / itemFirst
130
if ( generalComposite.isItemFirst() && !generalComposite.isUserFirst()
131             && !itemFirstProtectedItemsComposite.isVisible() )
132         {
133             userFirstUserClassesComposite.setVisible( false );
134             userFirstUserPermissionsComposite.setVisible( false );
135             itemFirstProtectedItemsComposite.setVisible( true );
136             itemFirstItemPermissionsComposite.setVisible( true );
137
138             setMinSize( composite.computeSize( SWT.DEFAULT, SWT.DEFAULT ) );
139             layout( true, true );
140         }
141         else if ( generalComposite.isUserFirst() && !generalComposite.isItemFirst()
142             && !userFirstUserClassesComposite.isVisible() )
143         {
144             userFirstUserClassesComposite.setVisible( true );
145             userFirstUserPermissionsComposite.setVisible( true );
146             itemFirstProtectedItemsComposite.setVisible( false );
147             itemFirstItemPermissionsComposite.setVisible( false );
148
149             setMinSize( composite.computeSize( SWT.DEFAULT, SWT.DEFAULT ) );
150             layout( true, true );
151         }
152         else if ( !generalComposite.isItemFirst() && !generalComposite.isUserFirst() )
153         {
154             userFirstUserClassesComposite.setVisible( false );
155             userFirstUserPermissionsComposite.setVisible( false );
156             itemFirstProtectedItemsComposite.setVisible( false );
157             itemFirstItemPermissionsComposite.setVisible( false );
158
159             setMinSize( composite.computeSize( SWT.DEFAULT, SWT.DEFAULT ) );
160             layout( true, true );
161         }
162
163     }
164
165
166     /**
167      * Sets the input. The given ACI Item string is parsed and
168      * populated to the GUI elements.
169      *
170      *
171      * @param input The string representation of the ACI item
172      * @throws ParseException if the syntax is invalid
173      */

174     public void setInput( String JavaDoc input ) throws ParseException JavaDoc
175     {
176         ACIItemParser parser = new ACIItemParser( null );
177         ACIItem aciItem = parser.parse( input );
178
179         if ( aciItem != null )
180         {
181             generalComposite.setIdentificationTag( aciItem.getIdentificationTag() );
182             generalComposite.setPrecedence( aciItem.getPrecedence() );
183             generalComposite.setAuthenticationLevel( aciItem.getAuthenticationLevel() );
184
185             if ( aciItem instanceof ItemFirstACIItem )
186             {
187                 ItemFirstACIItem itemFirstACI = ( ItemFirstACIItem ) aciItem;
188                 generalComposite.setItemFirst();
189                 itemFirstProtectedItemsComposite.setProtectedItems( itemFirstACI.getProtectedItems() );
190                 itemFirstItemPermissionsComposite.setItemPermissions( itemFirstACI.getItemPermissions() );
191             }
192             else if ( aciItem instanceof UserFirstACIItem )
193             {
194                 UserFirstACIItem userFirstACI = ( UserFirstACIItem ) aciItem;
195                 generalComposite.setUserFirst();
196                 userFirstUserClassesComposite.setUserClasses( userFirstACI.getUserClasses() );
197                 userFirstUserPermissionsComposite.setUserPermissions( userFirstACI.getUserPermission() );
198             }
199         }
200
201         // force userFirst/itemFirst switch
202
widgetModified( null );
203
204     }
205
206
207     /**
208      * Returns the string representation of the ACI item as defined in GUI.
209      *
210      *
211      * @return the string representation of the ACI item
212      * @throws ParseException if the syntax is invalid
213      */

214     public String JavaDoc getInput() throws ParseException JavaDoc
215     {
216         String JavaDoc identificationTag = generalComposite.getIdentificationTag();
217         int precedence = generalComposite.getPrecedence();
218         AuthenticationLevel authenticationLevel = generalComposite.getAuthenticationLevel();
219
220         ACIItem aciItem = null;
221         if ( generalComposite.isUserFirst() )
222         {
223             Collection JavaDoc userClasses = userFirstUserClassesComposite.getUserClasses();
224             Collection JavaDoc userPermissions = userFirstUserPermissionsComposite.getUserPermissions();
225             aciItem = new UserFirstACIItem( identificationTag, precedence, authenticationLevel, userClasses,
226                 userPermissions );
227         }
228         else if ( generalComposite.isItemFirst() )
229         {
230             Collection JavaDoc protectedItems = itemFirstProtectedItemsComposite.getProtectedItems();
231             Collection JavaDoc itemPermissions = itemFirstItemPermissionsComposite.getItemPermissions();
232             aciItem = new ItemFirstACIItem( identificationTag, precedence, authenticationLevel, protectedItems,
233                 itemPermissions );
234         }
235         else
236         {
237             aciItem = null;
238         }
239
240         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
241         if ( aciItem != null )
242         {
243             aciItem.printToBuffer( buffer );
244         }
245         return buffer.toString();
246     }
247
248
249     /**
250      * Sets the context.
251      *
252      * @param context the context
253      */

254     public void setContext( ACIItemValueWithContext context )
255     {
256         itemFirstProtectedItemsComposite.setContext( context );
257         itemFirstItemPermissionsComposite.setContext( context );
258         userFirstUserClassesComposite.setContext( context );
259         userFirstUserPermissionsComposite.setContext( context );
260     }
261
262 }
263
Popular Tags