KickJava   Java API By Example, From Geeks To Geeks.

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


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.preferences;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.SortedMap JavaDoc;
28 import java.util.TreeMap JavaDoc;
29
30 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
31 import org.apache.directory.ldapstudio.browser.core.ConnectionManager;
32 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
33 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
34 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription;
35 import org.apache.directory.ldapstudio.browser.core.model.schema.BinaryAttribute;
36 import org.apache.directory.ldapstudio.browser.core.model.schema.BinarySyntax;
37 import org.apache.directory.ldapstudio.browser.core.model.schema.LdapSyntaxDescription;
38 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
39
40 import org.eclipse.jface.preference.PreferencePage;
41 import org.eclipse.jface.viewers.ArrayContentProvider;
42 import org.eclipse.jface.viewers.DoubleClickEvent;
43 import org.eclipse.jface.viewers.IDoubleClickListener;
44 import org.eclipse.jface.viewers.ITableLabelProvider;
45 import org.eclipse.jface.viewers.LabelProvider;
46 import org.eclipse.jface.viewers.StructuredSelection;
47 import org.eclipse.jface.viewers.TableViewer;
48 import org.eclipse.swt.SWT;
49 import org.eclipse.swt.events.SelectionAdapter;
50 import org.eclipse.swt.events.SelectionEvent;
51 import org.eclipse.swt.graphics.Image;
52 import org.eclipse.swt.layout.GridData;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Control;
56 import org.eclipse.swt.widgets.Table;
57 import org.eclipse.swt.widgets.TableColumn;
58 import org.eclipse.ui.IWorkbench;
59 import org.eclipse.ui.IWorkbenchPreferencePage;
60
61
62 public class BinaryAttributesAndSyntaxesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage
63 {
64
65     private SortedMap JavaDoc attributeOid2AtdMap;
66
67     private SortedMap JavaDoc attributeNames2AtdMap;
68
69     private String JavaDoc[] attributeNamesAndOids;
70
71     private SortedMap JavaDoc syntaxOid2LsdMap;
72
73     private SortedMap JavaDoc syntaxDesc2LsdMap;
74
75     private String JavaDoc[] syntaxOids;
76
77     private List JavaDoc attributeList;
78
79     private TableViewer attributeViewer;
80
81     private Button attributeAddButton;
82
83     private Button attributeEditButton;
84
85     private Button attributeRemoveButton;
86
87     private List JavaDoc syntaxList;
88
89     private TableViewer syntaxViewer;
90
91     private Button syntaxAddButton;
92
93     private Button syntaxEditButton;
94
95     private Button syntaxRemoveButton;
96
97
98     public BinaryAttributesAndSyntaxesPreferencePage()
99     {
100         super();
101         super.setDescription( "Specify attributes to handle as binary:" );
102     }
103
104
105     public void init( IWorkbench workbench )
106     {
107     }
108
109
110     protected Control createContents( Composite parent )
111     {
112
113         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 );
114         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
115
116         // init available attribute types
117
this.attributeNames2AtdMap = new TreeMap JavaDoc();
118         this.attributeOid2AtdMap = new TreeMap JavaDoc();
119         ConnectionManager cm = BrowserCorePlugin.getDefault().getConnectionManager();
120         IConnection[] connections = cm.getConnections();
121         for ( int i = 0; i < connections.length; i++ )
122         {
123             Schema schema = connections[i].getSchema();
124             if ( schema != null )
125             {
126                 createAttributeMaps( schema );
127             }
128         }
129         createAttributeMaps( Schema.DEFAULT_SCHEMA );
130         this.attributeNamesAndOids = new String JavaDoc[this.attributeNames2AtdMap.size() + this.attributeOid2AtdMap.size()];
131         System.arraycopy( this.attributeNames2AtdMap.keySet().toArray(), 0, this.attributeNamesAndOids, 0,
132             this.attributeNames2AtdMap.size() );
133         System.arraycopy( this.attributeOid2AtdMap.keySet().toArray(), 0, this.attributeNamesAndOids,
134             this.attributeNames2AtdMap.size(), this.attributeOid2AtdMap.size() );
135
136         // init available syntaxes
137
this.syntaxOid2LsdMap = new TreeMap JavaDoc();
138         this.syntaxDesc2LsdMap = new TreeMap JavaDoc();
139         for ( int i = 0; i < connections.length; i++ )
140         {
141             Schema schema = connections[i].getSchema();
142             if ( schema != null )
143             {
144                 createSyntaxMaps( schema );
145             }
146         }
147         createSyntaxMaps( Schema.DEFAULT_SCHEMA );
148         this.syntaxOids = new String JavaDoc[this.syntaxOid2LsdMap.size()];
149         System
150             .arraycopy( this.syntaxOid2LsdMap.keySet().toArray(), 0, this.syntaxOids, 0, this.syntaxOid2LsdMap.size() );
151
152         // create attribute contents
153
BaseWidgetUtils.createSpacer( composite, 1 );
154         BaseWidgetUtils.createSpacer( composite, 1 );
155         createAttributeContents( composite );
156         attributeList = new ArrayList JavaDoc( Arrays.asList( BrowserCorePlugin.getDefault().getCorePreferences()
157             .getBinaryAttributes() ) );
158         attributeViewer.setInput( this.attributeList );
159         attributeViewer.getTable().getColumn( 0 ).pack();
160         // attributeViewer.getTable().getColumn(1).pack();
161
attributeViewer.getTable().pack();
162
163         // create syntax contents
164
BaseWidgetUtils.createSpacer( composite, 1 );
165         BaseWidgetUtils.createSpacer( composite, 1 );
166         createSyntaxContents( composite );
167         syntaxList = new ArrayList JavaDoc( Arrays.asList( BrowserCorePlugin.getDefault().getCorePreferences()
168             .getBinarySyntaxes() ) );
169         syntaxViewer.setInput( this.syntaxList );
170         syntaxViewer.getTable().getColumn( 0 ).pack();
171         // syntaxViewer.getTable().getColumn(1).pack();
172
syntaxViewer.getTable().pack();
173
174         return composite;
175     }
176
177
178     private void createAttributeMaps( Schema schema )
179     {
180         AttributeTypeDescription[] atds = schema.getAttributeTypeDescriptions();
181         for ( int i = 0; i < atds.length; i++ )
182         {
183
184             attributeOid2AtdMap.put( atds[i].getNumericOID(), atds[i] );
185
186             String JavaDoc[] names = atds[i].getNames();
187             for ( int j = 0; j < names.length; j++ )
188             {
189                 attributeNames2AtdMap.put( names[j], atds[i] );
190             }
191
192         }
193     }
194
195
196     private void createSyntaxMaps( Schema schema )
197     {
198         LdapSyntaxDescription[] lsds = schema.getLdapSyntaxDescriptions();
199         for ( int i = 0; i < lsds.length; i++ )
200         {
201
202             syntaxOid2LsdMap.put( lsds[i].getNumericOID(), lsds[i] );
203
204             if ( lsds[i].getDesc() != null )
205             {
206                 syntaxDesc2LsdMap.put( lsds[i].getDesc(), lsds[i] );
207             }
208
209         }
210     }
211
212
213     private void createAttributeContents( Composite parent )
214     {
215
216         BaseWidgetUtils.createLabel( parent, "Binary Attributes", 1 );
217
218         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 );
219         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
220         Composite listComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
221         listComposite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
222         Composite buttonComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
223         buttonComposite.setLayoutData( new GridData( GridData.VERTICAL_ALIGN_BEGINNING ) );
224
225         Table table = new Table( listComposite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION );
226         GridData data = new GridData( GridData.FILL_BOTH );
227         data.widthHint = 360;
228         data.heightHint = convertHeightInCharsToPixels( 10 );
229         table.setLayoutData( data );
230         table.setHeaderVisible( true );
231         table.setLinesVisible( true );
232         attributeViewer = new TableViewer( table );
233
234         TableColumn c1 = new TableColumn( table, SWT.NONE );
235         c1.setText( "Attribute" );
236         c1.setWidth( 300 );
237         TableColumn c2 = new TableColumn( table, SWT.NONE );
238         c2.setText( "Alias" );
239         c2.setWidth( 60 );
240
241         attributeViewer.setColumnProperties( new String JavaDoc[]
242             { "Attribute" } );
243         attributeViewer.setContentProvider( new ArrayContentProvider() );
244         attributeViewer.setLabelProvider( new AttributeLabelProvider() );
245
246         attributeViewer.addDoubleClickListener( new IDoubleClickListener()
247         {
248             public void doubleClick( DoubleClickEvent event )
249             {
250                 editAttribute();
251             }
252         } );
253
254         attributeAddButton = BaseWidgetUtils.createButton( buttonComposite, "Add...", 1 );
255         attributeAddButton.addSelectionListener( new SelectionAdapter()
256         {
257             public void widgetSelected( SelectionEvent e )
258             {
259                 addAttribute();
260             }
261         } );
262         attributeEditButton = BaseWidgetUtils.createButton( buttonComposite, "Edit...", 1 );
263         attributeEditButton.addSelectionListener( new SelectionAdapter()
264         {
265             public void widgetSelected( SelectionEvent e )
266             {
267                 editAttribute();
268             }
269         } );
270         attributeRemoveButton = BaseWidgetUtils.createButton( buttonComposite, "Remove", 1 );
271         attributeRemoveButton.addSelectionListener( new SelectionAdapter()
272         {
273             public void widgetSelected( SelectionEvent e )
274             {
275                 removeAttribute();
276             }
277         } );
278
279         // c1.pack();
280
// c2.pack();
281
// table.pack();
282
}
283
284
285     private void createSyntaxContents( Composite parent )
286     {
287
288         BaseWidgetUtils.createLabel( parent, "Binary Syntaxes", 1 );
289
290         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 );
291         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
292         Composite listComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
293         listComposite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
294         Composite buttonComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
295         buttonComposite.setLayoutData( new GridData( GridData.VERTICAL_ALIGN_BEGINNING ) );
296
297         Table table = new Table( listComposite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION );
298         GridData data = new GridData( GridData.FILL_BOTH );
299         data.widthHint = 360;
300         data.heightHint = convertHeightInCharsToPixels( 10 );
301         table.setLayoutData( data );
302         table.setHeaderVisible( true );
303         table.setLinesVisible( true );
304         syntaxViewer = new TableViewer( table );
305
306         TableColumn c1 = new TableColumn( table, SWT.NONE );
307         c1.setText( "Syntax" );
308         c1.setWidth( 300 );
309         TableColumn c2 = new TableColumn( table, SWT.NONE );
310         c2.setText( "Desc" );
311         c2.setWidth( 60 );
312
313         syntaxViewer.setColumnProperties( new String JavaDoc[]
314             { "Syntax" } );
315         syntaxViewer.setContentProvider( new ArrayContentProvider() );
316         syntaxViewer.setLabelProvider( new SyntaxLabelProvider() );
317
318         syntaxViewer.addDoubleClickListener( new IDoubleClickListener()
319         {
320             public void doubleClick( DoubleClickEvent event )
321             {
322                 editSyntax();
323             }
324         } );
325
326         syntaxAddButton = BaseWidgetUtils.createButton( buttonComposite, "Add...", 1 );
327         syntaxAddButton.addSelectionListener( new SelectionAdapter()
328         {
329             public void widgetSelected( SelectionEvent e )
330             {
331                 addSyntax();
332             }
333         } );
334         syntaxEditButton = BaseWidgetUtils.createButton( buttonComposite, "Edit...", 1 );
335         syntaxEditButton.addSelectionListener( new SelectionAdapter()
336         {
337             public void widgetSelected( SelectionEvent e )
338             {
339                 editSyntax();
340             }
341         } );
342         syntaxRemoveButton = BaseWidgetUtils.createButton( buttonComposite, "Remove", 1 );
343         syntaxRemoveButton.addSelectionListener( new SelectionAdapter()
344         {
345             public void widgetSelected( SelectionEvent e )
346             {
347                 removeSyntax();
348             }
349         } );
350
351         // c1.pack();
352
// c2.pack();
353
// table.pack();
354
}
355
356
357     protected void addAttribute()
358     {
359         AttributeDialog dialog = new AttributeDialog( getShell(), null, this.attributeNamesAndOids );
360         if ( dialog.open() == AttributeValueEditorDialog.OK )
361         {
362             this.attributeList.add( dialog.getAttribute() );
363             this.attributeViewer.refresh();
364         }
365     }
366
367
368     protected void removeAttribute()
369     {
370         Object JavaDoc o = ( ( StructuredSelection ) this.attributeViewer.getSelection() ).getFirstElement();
371         this.attributeList.remove( o );
372         this.attributeViewer.refresh();
373     }
374
375
376     protected void editAttribute()
377     {
378         StructuredSelection sel = ( StructuredSelection ) this.attributeViewer.getSelection();
379         if ( !sel.isEmpty() )
380         {
381             BinaryAttribute attribute = ( BinaryAttribute ) sel.getFirstElement();
382             AttributeDialog dialog = new AttributeDialog( getShell(), attribute, this.attributeNamesAndOids );
383             if ( dialog.open() == AttributeValueEditorDialog.OK )
384             {
385                 int index = this.attributeList.indexOf( attribute );
386                 this.attributeList.set( index, dialog.getAttribute() );
387                 this.attributeViewer.refresh();
388             }
389         }
390     }
391
392
393     protected void addSyntax()
394     {
395         SyntaxDialog dialog = new SyntaxDialog( getShell(), null, this.syntaxOids );
396         if ( dialog.open() == SyntaxValueEditorDialog.OK )
397         {
398             this.syntaxList.add( dialog.getSyntax() );
399             this.syntaxViewer.refresh();
400         }
401     }
402
403
404     protected void removeSyntax()
405     {
406         Object JavaDoc o = ( ( StructuredSelection ) this.syntaxViewer.getSelection() ).getFirstElement();
407         this.syntaxList.remove( o );
408         this.syntaxViewer.refresh();
409     }
410
411
412     protected void editSyntax()
413     {
414         StructuredSelection sel = ( StructuredSelection ) this.syntaxViewer.getSelection();
415         if ( !sel.isEmpty() )
416         {
417             BinarySyntax syntax = ( BinarySyntax ) sel.getFirstElement();
418             SyntaxDialog dialog = new SyntaxDialog( getShell(), syntax, this.syntaxOids );
419             if ( dialog.open() == SyntaxValueEditorDialog.OK )
420             {
421                 int index = this.syntaxList.indexOf( syntax );
422                 this.syntaxList.set( index, dialog.getSyntax() );
423                 this.syntaxViewer.refresh();
424             }
425         }
426     }
427
428
429     public boolean performOk()
430     {
431         BinaryAttribute[] attributes = ( BinaryAttribute[] ) this.attributeList
432             .toArray( new BinaryAttribute[this.attributeList.size()] );
433         BrowserCorePlugin.getDefault().getCorePreferences().setBinaryAttributes( attributes );
434
435         BinarySyntax[] syntaxes = ( BinarySyntax[] ) this.syntaxList.toArray( new BinarySyntax[this.syntaxList.size()] );
436         BrowserCorePlugin.getDefault().getCorePreferences().setBinarySyntaxes( syntaxes );
437
438         return true;
439     }
440
441
442     protected void performDefaults()
443     {
444         this.attributeList.clear();
445         this.attributeList.addAll( Arrays.asList( BrowserCorePlugin.getDefault().getCorePreferences()
446             .getDefaultBinaryAttributes() ) );
447         this.attributeViewer.refresh();
448
449         this.syntaxList.clear();
450         this.syntaxList.addAll( Arrays.asList( BrowserCorePlugin.getDefault().getCorePreferences()
451             .getDefaultBinarySyntaxes() ) );
452         this.syntaxViewer.refresh();
453
454         super.performDefaults();
455     }
456
457     class AttributeLabelProvider extends LabelProvider implements ITableLabelProvider
458     {
459         public String JavaDoc getColumnText( Object JavaDoc obj, int index )
460         {
461             if ( obj instanceof BinaryAttribute )
462             {
463                 BinaryAttribute attribute = ( BinaryAttribute ) obj;
464                 if ( index == 0 )
465                 {
466                     return attribute.getAttributeNumericOidOrName();
467                 }
468                 else if ( index == 1 )
469                 {
470                     if ( attribute.getAttributeNumericOidOrName() != null )
471                     {
472                         if ( attributeNames2AtdMap.containsKey( attribute.getAttributeNumericOidOrName() ) )
473                         {
474                             AttributeTypeDescription atd = ( AttributeTypeDescription ) attributeNames2AtdMap
475                                 .get( attribute.getAttributeNumericOidOrName() );
476                             String JavaDoc s = atd.getNumericOID();
477                             for ( int i = 0; i < atd.getNames().length; i++ )
478                             {
479                                 if ( !attribute.getAttributeNumericOidOrName().equals( atd.getNames()[i] ) )
480                                 {
481                                     s += ", " + atd.getNames()[i];
482                                 }
483                             }
484                             return s;
485                         }
486                         else if ( attributeOid2AtdMap.containsKey( attribute.getAttributeNumericOidOrName() ) )
487                         {
488                             AttributeTypeDescription atd = ( AttributeTypeDescription ) attributeOid2AtdMap
489                                 .get( attribute.getAttributeNumericOidOrName() );
490                             return atd.toString();
491                         }
492                     }
493                 }
494             }
495             return null;
496         }
497
498
499         public Image getColumnImage( Object JavaDoc obj, int index )
500         {
501             return null;
502         }
503     }
504
505     class SyntaxLabelProvider extends LabelProvider implements ITableLabelProvider
506     {
507         public String JavaDoc getColumnText( Object JavaDoc obj, int index )
508         {
509             if ( obj instanceof BinarySyntax )
510             {
511                 BinarySyntax syntax = ( BinarySyntax ) obj;
512                 if ( index == 0 )
513                 {
514                     return syntax.getSyntaxNumericOid();
515                 }
516                 else if ( index == 1 )
517                 {
518                     if ( syntax.getSyntaxNumericOid() != null )
519                     {
520                         if ( syntaxOid2LsdMap.containsKey( syntax.getSyntaxNumericOid() ) )
521                         {
522                             LdapSyntaxDescription lsd = ( LdapSyntaxDescription ) syntaxOid2LsdMap.get( syntax
523                                 .getSyntaxNumericOid() );
524                             return lsd.toString();
525                         }
526                     }
527                 }
528             }
529             return null;
530         }
531
532
533         public Image getColumnImage( Object JavaDoc obj, int index )
534         {
535             return null;
536         }
537     }
538 }
539
Popular Tags