KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dialogs > MultivaluedDialog


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
21 package org.apache.directory.ldapstudio.browser.common.dialogs;
22
23
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
28 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidget;
29 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroup;
30 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetConfiguration;
31 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetUniversalListener;
32 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.OpenDefaultEditorAction;
33 import org.apache.directory.ldapstudio.browser.core.events.AttributeDeletedEvent;
34 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueAddedEvent;
35 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueDeletedEvent;
36 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
37 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
38 import org.apache.directory.ldapstudio.browser.core.events.ValueAddedEvent;
39 import org.apache.directory.ldapstudio.browser.core.events.ValueDeletedEvent;
40 import org.apache.directory.ldapstudio.browser.core.events.ValueModifiedEvent;
41 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
42 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
43 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
44 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
45 import org.apache.directory.ldapstudio.browser.core.model.IValue;
46 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
47 import org.eclipse.jface.dialogs.Dialog;
48 import org.eclipse.jface.dialogs.IDialogConstants;
49 import org.eclipse.jface.viewers.StructuredSelection;
50 import org.eclipse.jface.viewers.TreeViewer;
51 import org.eclipse.swt.SWT;
52 import org.eclipse.swt.widgets.Composite;
53 import org.eclipse.swt.widgets.Control;
54 import org.eclipse.swt.widgets.Shell;
55
56
57 public class MultivaluedDialog extends Dialog
58 {
59
60     public static final String JavaDoc DIALOG_TITLE = "Multivalued Editor";
61
62     public static final int MAX_WIDTH = 450;
63
64     public static final int MAX_HEIGHT = 250;
65
66     private AttributeHierarchy attributeHierarchie;
67
68     private EntryEditorWidgetConfiguration configuration;
69
70     private EntryEditorWidgetActionGroup actionGroup;
71
72     private EntryEditorWidget mainWidget;
73
74     private MultiValuedEntryEditorUniversalListener universalListener;
75
76
77     public MultivaluedDialog( Shell parentShell, AttributeHierarchy attributeHierarchie )
78     {
79         super( parentShell );
80         setShellStyle( getShellStyle() | SWT.RESIZE );
81         this.attributeHierarchie = attributeHierarchie;
82     }
83
84
85     protected void configureShell( Shell shell )
86     {
87         super.configureShell( shell );
88         shell.setText( DIALOG_TITLE );
89         shell.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_MULTIVALUEDEDITOR ) );
90     }
91
92
93     protected void createButtonsForButtonBar( Composite parent )
94     {
95         createButton( parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, false );
96
97         getShell().update();
98         getShell().layout( true, true );
99     }
100
101
102     protected void buttonPressed( int buttonId )
103     {
104         if ( IDialogConstants.CLOSE_ID == buttonId )
105         {
106             close();
107         }
108         else
109         {
110             super.buttonPressed( buttonId );
111         }
112     }
113
114
115     public int open()
116     {
117         this.dialogOpened();
118         return super.open();
119     }
120
121
122     public boolean close()
123     {
124         boolean returnValue = super.close();
125         if ( returnValue )
126         {
127             this.dispose();
128             this.dialogClosed();
129         }
130         return returnValue;
131     }
132
133
134     public void dispose()
135     {
136         if ( this.configuration != null )
137         {
138             this.universalListener.dispose();
139             this.universalListener = null;
140             this.mainWidget.dispose();
141             this.mainWidget = null;
142             this.actionGroup.deactivateGlobalActionHandlers();
143             this.actionGroup.dispose();
144             this.actionGroup = null;
145             this.configuration.dispose();
146             this.configuration = null;
147         }
148     }
149
150
151     protected Control createDialogArea( Composite parent )
152     {
153
154         Composite composite = ( Composite ) super.createDialogArea( parent );
155
156         // create configuration
157
this.configuration = new EntryEditorWidgetConfiguration();
158
159         // create main widget
160
this.mainWidget = new EntryEditorWidget( this.configuration );
161         this.mainWidget.createWidget( composite );
162         this.mainWidget.getViewer().setInput( attributeHierarchie );
163         this.mainWidget.getViewer().getTree().setFocus();
164
165         // create actions
166
this.actionGroup = new EntryEditorWidgetActionGroup( this.mainWidget, this.configuration );
167         this.actionGroup.fillToolBar( this.mainWidget.getToolBarManager() );
168         this.actionGroup.fillMenu( this.mainWidget.getMenuManager() );
169         this.actionGroup.fillContextMenu( this.mainWidget.getContextMenuManager() );
170         this.actionGroup.activateGlobalActionHandlers();
171
172         // create the listener
173
this.universalListener = new MultiValuedEntryEditorUniversalListener( this.mainWidget.getViewer(),
174             this.actionGroup.getOpenDefaultEditorAction() );
175
176         // start edit mode if an empty value exists
177
for ( Iterator JavaDoc it = attributeHierarchie.iterator(); it.hasNext(); )
178         {
179             IAttribute attribute = ( IAttribute ) it.next();
180             IValue[] values = attribute.getValues();
181             for ( int i = 0; i < values.length; i++ )
182             {
183                 IValue value = values[i];
184                 if ( value.isEmpty() )
185                 {
186                     this.mainWidget.getViewer().setSelection( new StructuredSelection( value ), true );
187                     if ( this.actionGroup.getOpenDefaultEditorAction().isEnabled() )
188                     {
189                         this.actionGroup.getOpenDefaultEditorAction().run();
190                         break;
191                     }
192                 }
193             }
194         }
195
196         applyDialogFont( composite );
197         return composite;
198     }
199
200
201     private void dialogOpened()
202     {
203         if ( this.attributeHierarchie.getAttribute().getValueSize() == 0 )
204         {
205             this.attributeHierarchie.getAttribute().addEmptyValue();
206         }
207     }
208
209
210     private void dialogClosed()
211     {
212
213         for ( Iterator JavaDoc it = attributeHierarchie.iterator(); it.hasNext(); )
214         {
215             IAttribute attribute = ( IAttribute ) it.next();
216             if ( attribute != null )
217             {
218
219                 // remove empty values
220
IValue[] values = attribute.getValues();
221                 for ( int i = 0; i < values.length; i++ )
222                 {
223                     if ( values[i].isEmpty() )
224                     {
225                         attribute.deleteEmptyValue();
226                     }
227                 }
228
229                 // are all values deleted?
230
if ( attribute.getValueSize() == 0 )
231                 {
232                     try
233                     {
234                         attribute.getEntry().deleteAttribute( attribute );
235                     }
236                     catch ( ModelModificationException e )
237                     {
238                     }
239                     // new DeleteAttributeValueCommand(attribute).execute();
240
}
241             }
242         }
243
244     }
245
246     class MultiValuedEntryEditorUniversalListener extends EntryEditorWidgetUniversalListener
247     {
248
249         public MultiValuedEntryEditorUniversalListener( TreeViewer treeViewer, OpenDefaultEditorAction startEditAction )
250         {
251             super( treeViewer, startEditAction );
252         }
253
254
255         public void entryUpdated( EntryModificationEvent event )
256         {
257
258             if ( this.viewer == null || this.viewer.getTree() == null || this.viewer.getTree().isDisposed() )
259             {
260                 return;
261             }
262
263             if ( this.viewer.isCellEditorActive() )
264                 this.viewer.cancelEditing();
265
266             // set new input because attributes are newly created after a
267
// modification
268
IEntry entry = attributeHierarchie.getEntry();
269             String JavaDoc attributeDescription = attributeHierarchie.getAttributeDescription();
270             attributeHierarchie = entry.getAttributeWithSubtypes( attributeDescription );
271             if ( attributeHierarchie == null )
272             {
273                 EventRegistry.suspendEventFireingInCurrentThread();
274                 try
275                 {
276                     IAttribute attribute = new Attribute( entry, attributeDescription );
277                     entry.addAttribute( attribute );
278                     attribute.addEmptyValue();
279                 }
280                 catch ( ModelModificationException e )
281                 {
282                 }
283                 EventRegistry.resumeEventFireingInCurrentThread();
284                 attributeHierarchie = entry.getAttributeWithSubtypes( attributeDescription );
285             }
286             this.viewer.setInput( attributeHierarchie );
287             this.viewer.refresh();
288
289             // select added/modified value
290
if ( event instanceof ValueAddedEvent )
291             {
292                 ValueAddedEvent vaEvent = ( ValueAddedEvent ) event;
293                 this.viewer.setSelection( new StructuredSelection( vaEvent.getAddedValue() ), true );
294                 this.viewer.refresh();
295             }
296             else if ( event instanceof ValueModifiedEvent )
297             {
298                 ValueModifiedEvent vmEvent = ( ValueModifiedEvent ) event;
299                 this.viewer.setSelection( new StructuredSelection( vmEvent.getNewValue() ), true );
300             }
301             else if ( event instanceof ValueDeletedEvent )
302             {
303                 ValueDeletedEvent vdEvent = ( ValueDeletedEvent ) event;
304                 if ( vdEvent.getDeletedValue().getAttribute().getValueSize() > 0 )
305                 {
306                     this.viewer.setSelection( new StructuredSelection( vdEvent.getDeletedValue().getAttribute()
307                         .getValues()[0] ), true );
308                 }
309             }
310             else if ( event instanceof EmptyValueAddedEvent )
311             {
312                 viewer.refresh();
313                 EmptyValueAddedEvent evaEvent = ( EmptyValueAddedEvent ) event;
314                 viewer.setSelection( new StructuredSelection( evaEvent.getAddedValue() ), true );
315                 if ( startEditAction.isEnabled() )
316                     startEditAction.run();
317             }
318             else if ( event instanceof EmptyValueDeletedEvent )
319             {
320                 EmptyValueDeletedEvent evdEvent = ( EmptyValueDeletedEvent ) event;
321                 if ( viewer.getSelection().isEmpty() && evdEvent.getDeletedValue().getAttribute().getValueSize() > 0 )
322                     viewer.setSelection( new StructuredSelection(
323                         evdEvent.getDeletedValue().getAttribute().getValues()[0] ), true );
324             }
325             else if ( event instanceof AttributeDeletedEvent )
326             {
327
328             }
329         }
330     }
331
332 }
333
Popular Tags