KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > ModWidget


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.widgets;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
28 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
29 import org.eclipse.jface.fieldassist.ComboContentAdapter;
30 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
31 import org.eclipse.jface.fieldassist.DecoratedField;
32 import org.eclipse.jface.fieldassist.FieldDecoration;
33 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
34 import org.eclipse.jface.fieldassist.IControlCreator;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.events.SelectionAdapter;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Group;
46 import org.eclipse.swt.widgets.Shell;
47 import org.eclipse.swt.widgets.Text;
48
49
50 /**
51  * The ModWidget provides input elements to define an LDAP modify
52  * operation.
53  *
54  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
55  * @version $Rev$, $Date$
56  */

57 public class ModWidget extends BrowserWidget implements ModifyListener
58 {
59
60     /** The schema with the possible attribute types */
61     private Schema schema;
62
63     /** The shell */
64     private Shell shell;
65
66     /** The composite that contains the ModSpecs */
67     private Composite modComposite;
68
69     /** The list of ModSpecs */
70     private ArrayList JavaDoc<ModSpec> modSpecList;
71
72     /** The resulting LDIF */
73     private String JavaDoc ldif;
74
75
76     /**
77      * Creates a new instance of ModWidget.
78      *
79      * @param schema the schema with the possible attribute types
80      */

81     public ModWidget( Schema schema )
82     {
83         this.schema = schema;
84         this.modSpecList = new ArrayList JavaDoc<ModSpec>();
85         this.ldif = null;
86     }
87
88
89     /**
90      * Disposes this widget.
91      */

92     public void dispose()
93     {
94     }
95
96
97     /**
98      * Gets the ldif.
99      *
100      * @return the ldif
101      */

102     public String JavaDoc getLdif()
103     {
104         return ldif;
105     }
106
107
108     /**
109      * Creates the contents.
110      *
111      * @param parent the parent composite
112      *
113      * @return the created composite
114      */

115     public Composite createContents( Composite parent )
116     {
117         shell = parent.getShell();
118
119         modComposite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 );
120         addModSpec( modComposite, 0 );
121
122         return modComposite;
123     }
124
125
126     /**
127      * {@inheritDoc}
128      */

129     public void modifyText( ModifyEvent e )
130     {
131         validate();
132     }
133
134
135     /**
136      * Validates the input elements.
137      */

138     public void validate()
139     {
140         for ( int i = 0; i < modSpecList.size(); i++ )
141         {
142             ModSpec modSpec = ( ModSpec ) modSpecList.get( i );
143             if ( modSpecList.size() > 1 )
144             {
145                 modSpec.modDeleteButton.setEnabled( true );
146             }
147             else
148             {
149                 modSpec.modDeleteButton.setEnabled( false );
150             }
151             for ( int k = 0; k < modSpec.valueLineList.size(); k++ )
152             {
153                 ValueLine valueLine = ( ValueLine ) modSpec.valueLineList.get( k );
154                 if ( modSpec.valueLineList.size() > 1 )
155                 {
156                     valueLine.valueDeleteButton.setEnabled( true );
157                 }
158                 else
159                 {
160                     valueLine.valueDeleteButton.setEnabled( false );
161                 }
162             }
163         }
164
165         notifyListeners();
166     }
167
168
169     /**
170      * Adds a modification spec at the given index.
171      *
172      * @param modComposite the composite
173      * @param index the index
174      */

175     private void addModSpec( Composite modComposite, int index )
176     {
177
178         ModSpec[] modSpecs = ( ModSpec[] ) modSpecList.toArray( new ModSpec[modSpecList.size()] );
179
180         if ( modSpecs.length > 0 )
181         {
182             for ( int i = 0; i < modSpecs.length; i++ )
183             {
184                 ModSpec oldModSpec = modSpecs[i];
185
186                 // remember values
187
String JavaDoc oldType = oldModSpec.modType.getText();
188                 String JavaDoc oldAttribute = oldModSpec.modAttributeCombo.getText();
189                 String JavaDoc[] oldValues = new String JavaDoc[oldModSpec.valueLineList.size()];
190                 for ( int k = 0; k < oldValues.length; k++ )
191                 {
192                     oldValues[k] = ( ( ValueLine ) oldModSpec.valueLineList.get( k ) ).valueText.getText();
193                 }
194
195                 // delete old
196
oldModSpec.modGroup.dispose();
197                 oldModSpec.modAddButton.dispose();
198                 oldModSpec.modDeleteButton.dispose();
199                 modSpecList.remove( oldModSpec );
200
201                 // add new
202
ModSpec newModSpec = createModSpec( modComposite );
203                 modSpecList.add( newModSpec );
204
205                 // restore values
206
newModSpec.modType.setText( oldType );
207                 newModSpec.modAttributeCombo.setText( oldAttribute );
208                 deleteValueLine( newModSpec, 0 );
209                 for ( int k = 0; k < oldValues.length; k++ )
210                 {
211                     addValueLine( newModSpec, k );
212                     ValueLine newValueLine = ( ValueLine ) newModSpec.valueLineList.get( k );
213                     newValueLine.valueText.setText( oldValues[k] );
214                 }
215
216                 // check
217
if ( index == i + 1 )
218                 {
219                     ModSpec modSpec = createModSpec( modComposite );
220                     modSpecList.add( modSpec );
221                 }
222             }
223         }
224         else
225         {
226             ModSpec modSpec = createModSpec( modComposite );
227             modSpecList.add( modSpec );
228         }
229
230         shell.layout( true, true );
231     }
232
233
234     /**
235      * Creates and returns a modification spec.
236      *
237      * @param modComposite the composite
238      *
239      * @return the created modification spec
240      */

241     private ModSpec createModSpec( final Composite modComposite )
242     {
243         final ModSpec modSpec = new ModSpec();
244
245         modSpec.modGroup = BaseWidgetUtils.createGroup( modComposite, "", 1 );
246         Composite modSpecComposite = BaseWidgetUtils.createColumnContainer( modSpec.modGroup, 2, 1 );
247         modSpec.modType = BaseWidgetUtils.createCombo( modSpecComposite, new String JavaDoc[]
248             { "add", "replace", "delete" }, 0, 1 );
249         modSpec.modType.addModifyListener( this );
250         String JavaDoc[] attributeDescriptions = schema.getAttributeTypeDescriptionNames();
251         Arrays.sort( attributeDescriptions );
252
253         // attribute combo with field decoration
254
final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
255             FieldDecorationRegistry.DEC_CONTENT_PROPOSAL );
256         modSpec.modAttributeComboField = new DecoratedField( modSpecComposite, SWT.NONE, new IControlCreator()
257         {
258             public Control createControl( Composite parent, int style )
259             {
260                 Combo combo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
261                 combo.setVisibleItemCount( 20 );
262                 return combo;
263             }
264         } );
265         modSpec.modAttributeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true );
266         modSpec.modAttributeComboField.getLayoutControl().setLayoutData(
267             new GridData( SWT.FILL, SWT.CENTER, true, false ) );
268         modSpec.modAttributeCombo = ( Combo ) modSpec.modAttributeComboField.getControl();
269         modSpec.modAttributeCombo.setItems( attributeDescriptions );
270         modSpec.modAttributeCombo.addModifyListener( this );
271
272         // content proposal adapter
273
modSpec.modAttributeCPA = new ContentProposalAdapter( modSpec.modAttributeCombo, new ComboContentAdapter(),
274             new ListContentProposalProvider( attributeDescriptions ), null, null );
275         modSpec.modAttributeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE );
276         modSpec.modAttributeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE );
277
278         // add button with listener
279
modSpec.modAddButton = new Button( modComposite, SWT.PUSH );
280         modSpec.modAddButton.setText( " + " );
281         modSpec.modAddButton.addSelectionListener( new SelectionAdapter()
282         {
283             public void widgetSelected( SelectionEvent e )
284             {
285                 int index = modSpecList.size();
286                 for ( int i = 0; i < modSpecList.size(); i++ )
287                 {
288                     ModSpec modSpec = modSpecList.get( i );
289                     if ( modSpec.modAddButton == e.widget )
290                     {
291                         index = i + 1;
292                     }
293                 }
294
295                 addModSpec( modComposite, index );
296
297                 validate();
298             }
299         } );
300
301         // delete button with listener
302
modSpec.modDeleteButton = new Button( modComposite, SWT.PUSH );
303         modSpec.modDeleteButton.setText( " \u2212 " ); // \u2013
304
modSpec.modDeleteButton.addSelectionListener( new SelectionAdapter()
305         {
306             public void widgetSelected( SelectionEvent e )
307             {
308                 int index = 0;
309                 for ( int i = 0; i < modSpecList.size(); i++ )
310                 {
311                     ModSpec modSpec = modSpecList.get( i );
312                     if ( modSpec.modDeleteButton == e.widget )
313                     {
314                         index = i;
315                     }
316                 }
317
318                 deleteModSpec( modComposite, index );
319
320                 validate();
321             }
322         } );
323
324         addValueLine( modSpec, 0 );
325
326         return modSpec;
327     }
328
329
330     /**
331      * Delets a modification spec.
332      *
333      * @param modComposite the composite
334      * @param index the index
335      */

336     private void deleteModSpec( Composite modComposite, int index )
337     {
338         ModSpec modSpec = modSpecList.remove( index );
339         if ( modSpec != null )
340         {
341             modSpec.modGroup.dispose();
342             modSpec.modAddButton.dispose();
343             modSpec.modDeleteButton.dispose();
344
345             if ( !modComposite.isDisposed() )
346             {
347                 shell.layout( true, true );
348             }
349         }
350     }
351
352
353     /**
354      * Adds a value line to the given modification spec.
355      *
356      * @param modSpec the modification spec
357      * @param index the index
358      */

359     private void addValueLine( ModSpec modSpec, int index )
360     {
361
362         ValueLine[] valueLines = modSpec.valueLineList.toArray( new ValueLine[modSpec.valueLineList.size()] );
363
364         if ( valueLines.length > 0 )
365         {
366             for ( int i = 0; i < valueLines.length; i++ )
367             {
368                 ValueLine oldValueLine = valueLines[i];
369
370                 // remember values
371
String JavaDoc oldValue = oldValueLine.valueText.getText();
372
373                 // delete old
374
oldValueLine.valueComposite.dispose();
375                 modSpec.valueLineList.remove( oldValueLine );
376
377                 // add new
378
ValueLine newValueLine = createValueLine( modSpec );
379                 modSpec.valueLineList.add( newValueLine );
380
381                 // restore value
382
newValueLine.valueText.setText( oldValue );
383
384                 // check
385
if ( index == i + 1 )
386                 {
387                     ValueLine valueLine = createValueLine( modSpec );
388                     modSpec.valueLineList.add( valueLine );
389                 }
390             }
391         }
392         else
393         {
394             ValueLine valueLine = createValueLine( modSpec );
395             modSpec.valueLineList.add( valueLine );
396         }
397
398         shell.layout( true, true );
399     }
400
401
402     /**
403      * Creates the value line.
404      *
405      * @param modSpec the modification spec
406      *
407      * @return the value line
408      */

409     private ValueLine createValueLine( final ModSpec modSpec )
410     {
411         final ValueLine valueLine = new ValueLine();
412
413         // text field
414
valueLine.valueComposite = BaseWidgetUtils.createColumnContainer( modSpec.modGroup, 3, 1 );
415         valueLine.valueText = BaseWidgetUtils.createText( valueLine.valueComposite, "", 1 );
416         valueLine.valueText.addModifyListener( this );
417
418         // add button with listener
419
valueLine.valueAddButton = new Button( valueLine.valueComposite, SWT.PUSH );
420         valueLine.valueAddButton.setText( " + " );
421         valueLine.valueAddButton.addSelectionListener( new SelectionAdapter()
422         {
423             public void widgetSelected( SelectionEvent e )
424             {
425                 int index = modSpec.valueLineList.size();
426                 for ( int i = 0; i < modSpec.valueLineList.size(); i++ )
427                 {
428                     ValueLine valueLine = modSpec.valueLineList.get( i );
429                     if ( valueLine.valueAddButton == e.widget )
430                     {
431                         index = i + 1;
432                     }
433                 }
434
435                 addValueLine( modSpec, index );
436
437                 validate();
438             }
439         } );
440
441         // delete button with listener
442
valueLine.valueDeleteButton = new Button( valueLine.valueComposite, SWT.PUSH );
443         valueLine.valueDeleteButton.setText( " \u2212 " ); // \u2013
444
valueLine.valueDeleteButton.addSelectionListener( new SelectionAdapter()
445         {
446             public void widgetSelected( SelectionEvent e )
447             {
448                 int index = 0;
449                 for ( int i = 0; i < modSpec.valueLineList.size(); i++ )
450                 {
451                     ValueLine valueLine = modSpec.valueLineList.get( i );
452                     if ( valueLine.valueDeleteButton == e.widget )
453                     {
454                         index = i;
455                     }
456                 }
457
458                 deleteValueLine( modSpec, index );
459
460                 validate();
461             }
462         } );
463
464         return valueLine;
465     }
466
467
468     /**
469      * Delete value line.
470      *
471      * @param modSpec the mod spec
472      * @param index the index
473      */

474     private void deleteValueLine( ModSpec modSpec, int index )
475     {
476         ValueLine valueLine = ( ValueLine ) modSpec.valueLineList.remove( index );
477         if ( valueLine != null )
478         {
479             valueLine.valueComposite.dispose();
480
481             if ( !modComposite.isDisposed() )
482             {
483                 shell.layout( true, true );
484             }
485         }
486     }
487
488
489     /**
490      * Gets the LDIF fragment.
491      *
492      * @return the LDIF fragment
493      */

494     public String JavaDoc getLdifFragment()
495     {
496
497         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
498         sb.append( "changetype: modify" ).append( BrowserCoreConstants.LINE_SEPARATOR );
499
500         ModSpec[] modSpecs = ( ModSpec[] ) modSpecList.toArray( new ModSpec[modSpecList.size()] );
501
502         if ( modSpecs.length > 0 )
503         {
504             for ( int i = 0; i < modSpecs.length; i++ )
505             {
506                 ModSpec modSpec = modSpecs[i];
507
508                 // get values
509
String JavaDoc type = modSpec.modType.getText();
510                 String JavaDoc attribute = modSpec.modAttributeCombo.getText();
511                 String JavaDoc[] values = new String JavaDoc[modSpec.valueLineList.size()];
512                 for ( int k = 0; k < values.length; k++ )
513                 {
514                     values[k] = ( ( ValueLine ) modSpec.valueLineList.get( k ) ).valueText.getText();
515                 }
516
517                 // build ldif
518
sb.append( type ).append( ": " ).append( attribute ).append( BrowserCoreConstants.LINE_SEPARATOR );
519                 for ( int k = 0; k < values.length; k++ )
520                 {
521                     if ( values[k].length() > 0 )
522                     {
523                         sb.append( attribute ).append( ": " ).append( values[k] ).append(
524                             BrowserCoreConstants.LINE_SEPARATOR );
525                     }
526                 }
527                 sb.append( "-" ).append( BrowserCoreConstants.LINE_SEPARATOR );
528                 // sb.append(BrowserCoreConstants.NEWLINE);
529
}
530         }
531
532         return sb.toString();
533     }
534
535     /**
536      * The Class ModSpec is a wrapper for all input elements
537      * of an modification. It contains a combo for the modify
538      * operation, a combo for the attribute to modify,
539      * value lines and + and - buttons to add and remove
540      * other modifications. It looks like this:
541      * <pre>
542      * ----------------------------------
543      * | operation v | attribute type v |--------
544      * ------------------------ --------| + | - |
545      * | value | + | - |--------
546      * ----------------------------------
547      * </pre>
548      */

549     private class ModSpec
550     {
551
552         /** The mod group. */
553         private Group modGroup;
554
555         /** The mod type. */
556         private Combo modType;
557
558         /** The modification attribute field. */
559         private DecoratedField modAttributeComboField;
560
561         /** The modification attribute. */
562         private Combo modAttributeCombo;
563
564         /** The modification content proposal adapter */
565         private ContentProposalAdapter modAttributeCPA;
566
567         /** The mod add button. */
568         private Button modAddButton;
569
570         /** The mod delete button. */
571         private Button modDeleteButton;
572
573         /** The value line list. */
574         private ArrayList JavaDoc<ValueLine> valueLineList = new ArrayList JavaDoc<ValueLine>();;
575     }
576
577     /**
578      * The Class ValueLine is a wrapper for all input elements
579      * of an value line. It contains an input field for the value
580      * and + and - buttons to add and remove other value lines.
581      * It looks like this:
582      * <pre>
583      * -------------------------------------
584      * | value | + | - |
585      * -------------------------------------
586      * </pre>
587      */

588     private class ValueLine
589     {
590
591         /** The value composite. */
592         private Composite valueComposite;
593
594         /** The value text. */
595         private Text valueText;
596
597         /** The value add button. */
598         private Button valueAddButton;
599
600         /** The value delete button. */
601         private Button valueDeleteButton;
602     }
603
604 }
605
Popular Tags