KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > entry > EntryEditorOutlinePage


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.ui.editors.entry;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedHashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.IValue;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
36 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeDeleteRecord;
37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModDnRecord;
38 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModifyRecord;
39 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
40 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser;
44 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
45 import org.apache.directory.ldapstudio.browser.ui.actions.CopyEntryAsLdifAction;
46 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
47 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
48 import org.eclipse.jface.viewers.DoubleClickEvent;
49 import org.eclipse.jface.viewers.IDoubleClickListener;
50 import org.eclipse.jface.viewers.ISelectionChangedListener;
51 import org.eclipse.jface.viewers.IStructuredSelection;
52 import org.eclipse.jface.viewers.ITreeContentProvider;
53 import org.eclipse.jface.viewers.LabelProvider;
54 import org.eclipse.jface.viewers.SelectionChangedEvent;
55 import org.eclipse.jface.viewers.StructuredSelection;
56 import org.eclipse.jface.viewers.TreeViewer;
57 import org.eclipse.jface.viewers.Viewer;
58 import org.eclipse.swt.graphics.Image;
59 import org.eclipse.swt.widgets.Composite;
60 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
61
62
63 /**
64  * This class implements the Outline Page for the Entry Editor.
65  * It used to display LDAP entries.
66  *
67  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
68  * @version $Rev$, $Date$
69  */

70 public class EntryEditorOutlinePage extends ContentOutlinePage
71 {
72     /** The editor it is attached to */
73     private EntryEditor entryEditor;
74
75
76     /**
77      * Creates a new instance of EntryEditorOutlinePage.
78      *
79      * @param entryEditor
80      * the editor the Outline page is attached to
81      */

82     public EntryEditorOutlinePage( EntryEditor entryEditor )
83     {
84         this.entryEditor = entryEditor;
85     }
86
87
88     /* (non-Javadoc)
89      * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#createControl(org.eclipse.swt.widgets.Composite)
90      */

91     public void createControl( Composite parent )
92     {
93         super.createControl( parent );
94
95         final TreeViewer treeViewer = getTreeViewer();
96         treeViewer.setLabelProvider( new LdifLabelProvider() );
97         treeViewer.setContentProvider( new LdifContentProvider() );
98         // treeViewer.setAutoExpandLevel(1);
99

100         treeViewer.addSelectionChangedListener( new ISelectionChangedListener()
101         {
102             public void selectionChanged( SelectionChangedEvent event )
103             {
104                 if ( !event.getSelection().isEmpty() && event.getSelection() instanceof IStructuredSelection )
105                 {
106
107                     Object JavaDoc o = entryEditor.getMainWidget().getViewer().getInput();
108                     if ( o != null && o instanceof IEntry )
109                     {
110                         IEntry entry = ( IEntry ) o;
111                         IAttribute[] attributes = entry.getAttributes();
112
113                         List JavaDoc selectionList = new ArrayList JavaDoc();
114
115                         Iterator JavaDoc it = ( ( IStructuredSelection ) event.getSelection() ).iterator();
116                         while ( it.hasNext() )
117                         {
118                             Object JavaDoc element = it.next();
119
120                             if ( element instanceof LdifAttrValLine )
121                             {
122                                 // select the value
123
LdifAttrValLine line = ( LdifAttrValLine ) element;
124                                 for ( int a = 0; a < attributes.length; a++ )
125                                 {
126                                     IAttribute attribute = attributes[a];
127                                     if ( attribute.getDescription().equals( line.getUnfoldedAttributeDescription() ) )
128                                     {
129                                         IValue[] values = attribute.getValues();
130                                         for ( int v = 0; v < values.length; v++ )
131                                         {
132                                             IValue value = values[v];
133                                             if ( value.getStringValue().equals( line.getValueAsString() ) )
134                                             {
135                                                 selectionList.add( value );
136                                             }
137                                         }
138                                     }
139                                 }
140                             }
141                             else if ( element instanceof List JavaDoc )
142                             {
143                                 // select attribute and all values
144
List JavaDoc list = ( List JavaDoc ) element;
145                                 if ( !list.isEmpty() && list.get( 0 ) instanceof LdifAttrValLine )
146                                 {
147                                     LdifAttrValLine line = ( LdifAttrValLine ) list.get( 0 );
148                                     for ( int a = 0; a < attributes.length; a++ )
149                                     {
150                                         IAttribute attribute = attributes[a];
151                                         if ( attribute.getDescription().equals(
152                                             line.getUnfoldedAttributeDescription() ) )
153                                         {
154                                             selectionList.add( attribute );
155                                             selectionList.addAll( Arrays.asList( attribute.getValues() ) );
156                                         }
157                                     }
158                                 }
159                             }
160                             else if ( element instanceof LdifRecord )
161                             {
162                                 for ( int a = 0; a < attributes.length; a++ )
163                                 {
164                                     IAttribute attribute = attributes[a];
165                                     selectionList.add( attribute );
166                                     selectionList.addAll( Arrays.asList( attribute.getValues() ) );
167                                 }
168                             }
169                         }
170
171                         IStructuredSelection selection = new StructuredSelection( selectionList );
172                         entryEditor.getMainWidget().getViewer().setSelection( selection );
173                     }
174                 }
175             }
176         } );
177
178         treeViewer.addDoubleClickListener( new IDoubleClickListener()
179         {
180             public void doubleClick( DoubleClickEvent event )
181             {
182                 if ( event.getSelection() instanceof IStructuredSelection )
183                 {
184                     Object JavaDoc obj = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement();
185                     if ( treeViewer.getExpandedState( obj ) )
186                         treeViewer.collapseToLevel( obj, 1 );
187                     else if ( ( ( ITreeContentProvider ) treeViewer.getContentProvider() ).hasChildren( obj ) )
188                         treeViewer.expandToLevel( obj, 1 );
189                 }
190             }
191         } );
192
193         this.refresh();
194     }
195
196
197     /**
198      * Refreshes this viewer starting with the given element.
199      *
200      * @param element
201      * the element
202      */

203     public void refresh( Object JavaDoc element )
204     {
205         final TreeViewer treeViewer = getTreeViewer();
206         if ( treeViewer != null && treeViewer.getTree() != null && !treeViewer.getTree().isDisposed() )
207         {
208             treeViewer.refresh( element );
209         }
210     }
211
212
213     /**
214      * Refreshes this viewer completely with information freshly obtained from this viewer's model.
215      */

216     public void refresh()
217     {
218         final TreeViewer treeViewer = getTreeViewer();
219
220         if ( treeViewer != null && treeViewer.getTree() != null && !treeViewer.getTree().isDisposed() )
221         {
222             // ISelection selection = treeViewer.getSelection();
223
// Object[] expandedElements = treeViewer.getExpandedElements();
224

225             if ( !treeViewer.getTree().isEnabled() )
226             {
227                 treeViewer.getTree().setEnabled( true );
228             }
229
230             if ( entryEditor != null )
231             {
232                 Object JavaDoc o = entryEditor.getMainWidget().getViewer().getInput();
233
234                 if ( o == null )
235                 {
236                     treeViewer.setInput( null );
237                     treeViewer.getTree().setEnabled( false );
238                 }
239                 else
240                 {
241                     if ( o instanceof IEntry )
242                     {
243                         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
244                         new CopyEntryAsLdifAction( CopyEntryAsLdifAction.MODE_INCLUDE_OPERATIONAL_ATTRIBUTES )
245                             .serialializeEntries( new IEntry[]
246                                 { ( IEntry ) o }, sb );
247                         LdifFile model = new LdifParser().parse( sb.toString() );
248                         treeViewer.setInput( model );
249                         treeViewer.expandToLevel( 2 );
250                     }
251                 }
252             }
253
254             treeViewer.refresh();
255
256             // treeViewer.setSelection(selection);
257
// treeViewer.setExpandedElements(expandedElements);
258
}
259     }
260
261
262     /* (non-Javadoc)
263      * @see org.eclipse.ui.part.Page#dispose()
264      */

265     public void dispose()
266     {
267         super.dispose();
268         if ( entryEditor != null )
269         {
270             entryEditor = null;
271         }
272     }
273
274     /**
275      * This class implements the ContentProvider used for the LDIF Outline View
276      *
277      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
278      * @version $Rev$, $Date$
279      */

280     private static class LdifContentProvider implements ITreeContentProvider
281     {
282         /* (non-Javadoc)
283          * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
284          */

285         public Object JavaDoc[] getChildren( Object JavaDoc element )
286         {
287             // file --> records
288
if ( element instanceof LdifFile )
289             {
290                 LdifFile ldifFile = ( LdifFile ) element;
291                 return ldifFile.getRecords();
292             }
293
294             // record --> Array of List of AttrValLine
295
else if ( element instanceof LdifContentRecord )
296             {
297                 LdifContentRecord record = ( LdifContentRecord ) element;
298                 return getUniqueAttrValLineArray( record.getAttrVals() );
299             }
300             else if ( element instanceof LdifChangeAddRecord )
301             {
302                 LdifChangeAddRecord record = ( LdifChangeAddRecord ) element;
303                 return getUniqueAttrValLineArray( record.getAttrVals() );
304             }
305             else if ( element instanceof LdifChangeModifyRecord )
306             {
307                 LdifChangeModifyRecord record = ( LdifChangeModifyRecord ) element;
308                 return record.getModSpecs();
309             }
310             else if ( element instanceof LdifChangeModDnRecord )
311             {
312                 return new Object JavaDoc[0];
313             }
314             else if ( element instanceof LdifChangeDeleteRecord )
315             {
316                 return new Object JavaDoc[0];
317             }
318
319             // List of AttrValLine --> Array of AttrValLine
320
else if ( element instanceof List JavaDoc && ( ( List JavaDoc ) element ).get( 0 ) instanceof LdifAttrValLine )
321             {
322                 List JavaDoc list = ( List JavaDoc ) element;
323                 return list.toArray();
324             }
325             else if ( element instanceof LdifModSpec )
326             {
327                 LdifModSpec modSpec = ( LdifModSpec ) element;
328                 return modSpec.getAttrVals();
329             }
330
331             else
332             {
333                 return new Object JavaDoc[0];
334             }
335         }
336
337
338         /**
339          * Returns a unique line of attribute values from an array of attribute value lines
340          *
341          * @param lines
342          * the attribute value lines
343          * @return
344          * a unique line of attribute values from an array of attribute values lines
345          */

346         private Object JavaDoc[] getUniqueAttrValLineArray( LdifAttrValLine[] lines )
347         {
348             Map JavaDoc uniqueAttrMap = new LinkedHashMap JavaDoc();
349             for ( int i = 0; i < lines.length; i++ )
350             {
351                 if ( !uniqueAttrMap.containsKey( lines[i].getUnfoldedAttributeDescription() ) )
352                 {
353                     uniqueAttrMap.put( lines[i].getUnfoldedAttributeDescription(), new ArrayList JavaDoc() );
354                 }
355                 ( ( List JavaDoc ) uniqueAttrMap.get( lines[i].getUnfoldedAttributeDescription() ) ).add( lines[i] );
356             }
357             return uniqueAttrMap.values().toArray();
358         }
359
360
361         /* (non-Javadoc)
362          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
363          */

364         public Object JavaDoc getParent( Object JavaDoc element )
365         {
366             return null;
367         }
368
369
370         /* (non-Javadoc)
371          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
372          */

373         public boolean hasChildren( Object JavaDoc element )
374         {
375             return getChildren( element ) != null && getChildren( element ).length > 0;
376         }
377
378
379         /* (non-Javadoc)
380          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
381          */

382         public Object JavaDoc[] getElements( Object JavaDoc inputElement )
383         {
384             return getChildren( inputElement );
385         }
386
387
388         /* (non-Javadoc)
389          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
390          */

391         public void dispose()
392         {
393         }
394
395
396         /* (non-Javadoc)
397          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
398          */

399         public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
400         {
401         }
402     }
403
404     /**
405      * This class implements the LabelProvider used for the LDIF Outline View
406      *
407      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
408      * @version $Rev$, $Date$
409      */

410     private static class LdifLabelProvider extends LabelProvider
411     {
412         /* (non-Javadoc)
413          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
414          */

415         public String JavaDoc getText( Object JavaDoc element )
416         {
417             // Record
418
if ( element instanceof LdifRecord )
419             {
420                 LdifRecord ldifRecord = ( LdifRecord ) element;
421                 return ldifRecord.getDnLine().getValueAsString();
422             }
423
424             // List of AttrValLine
425
else if ( element instanceof List JavaDoc && ( ( List JavaDoc ) element ).get( 0 ) instanceof LdifAttrValLine )
426             {
427                 List JavaDoc list = ( List JavaDoc ) element;
428                 return ( ( LdifAttrValLine ) list.get( 0 ) ).getUnfoldedAttributeDescription() + " (" + list.size()
429                     + ")";
430             }
431             else if ( element instanceof LdifModSpec )
432             {
433                 LdifModSpec modSpec = ( LdifModSpec ) element;
434                 return modSpec.getModSpecType().getUnfoldedAttributeDescription() + " (" + modSpec.getAttrVals().length
435                     + ")";
436             }
437
438             // AttrValLine
439
else if ( element instanceof LdifAttrValLine )
440             {
441                 LdifAttrValLine line = ( LdifAttrValLine ) element;
442                 return Utils.getShortenedString( line.getValueAsString(), 20 );
443             }
444
445             else
446             {
447                 return "";
448             }
449         }
450
451
452         /* (non-Javadoc)
453          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
454          */

455         public Image getImage( Object JavaDoc element )
456         {
457             // Record
458
if ( element instanceof LdifContentRecord )
459             {
460                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_ENTRY );
461             }
462             else if ( element instanceof LdifChangeAddRecord )
463             {
464                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_ADD );
465             }
466             else if ( element instanceof LdifChangeModifyRecord )
467             {
468                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MODIFY );
469             }
470             else if ( element instanceof LdifChangeDeleteRecord )
471             {
472                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_DELETE );
473             }
474             else if ( element instanceof LdifChangeModDnRecord )
475             {
476                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_RENAME );
477             }
478
479             // List of AttrValLine
480
else if ( element instanceof List JavaDoc && ( ( List JavaDoc ) element ).get( 0 ) instanceof LdifAttrValLine )
481             {
482                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_ATTRIBUTE );
483             }
484             else if ( element instanceof LdifModSpec )
485             {
486                 LdifModSpec modSpec = ( LdifModSpec ) element;
487                 if ( modSpec.isAdd() )
488                     return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_ADD );
489                 else if ( modSpec.isReplace() )
490                     return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_REPLACE );
491                 else if ( modSpec.isDelete() )
492                     return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_DELETE );
493                 else
494                     return null;
495             }
496
497             // AttrValLine
498
else if ( element instanceof LdifAttrValLine )
499             {
500                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_VALUE );
501             }
502
503             else
504             {
505                 return null;
506             }
507         }
508     }
509 }
510
Popular Tags