KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > LdifOutlinePage


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

62 public class LdifOutlinePage extends ContentOutlinePage
63 {
64     /** The editor it is attached to */
65     private LdifEditor ldifEditor;
66
67
68     /**
69      * Creates a new instance of LdifOutlinePage.
70      *
71      * @param ldifEditor
72      * the editor the Outline page is attached to
73      */

74     public LdifOutlinePage( LdifEditor ldifEditor )
75     {
76         this.ldifEditor = ldifEditor;
77     }
78
79
80     /* (non-Javadoc)
81      * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#createControl(org.eclipse.swt.widgets.Composite)
82      */

83     public void createControl( Composite parent )
84     {
85         super.createControl( parent );
86
87         final TreeViewer treeViewer = getTreeViewer();
88         treeViewer.setLabelProvider( new LdifLabelProvider() );
89         treeViewer.setContentProvider( new LdifContentProvider() );
90         // treeViewer.setAutoExpandLevel(1);
91

92         treeViewer.addSelectionChangedListener( new ISelectionChangedListener()
93         {
94             public void selectionChanged( SelectionChangedEvent event )
95             {
96                 if ( !event.getSelection().isEmpty() && event.getSelection() instanceof IStructuredSelection )
97                 {
98                     Object JavaDoc element = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement();
99
100                     if ( element instanceof LdifRecord )
101                     {
102                         LdifRecord ldifRecord = ( LdifRecord ) element;
103                         ldifEditor.selectAndReveal( ldifRecord.getDnLine().getOffset(), ldifRecord.getDnLine()
104                             .getLength() );
105                     }
106                     else if ( element instanceof List JavaDoc )
107                     {
108                         List JavaDoc list = ( List JavaDoc ) element;
109                         if ( !list.isEmpty() && list.get( 0 ) instanceof LdifAttrValLine )
110                         {
111                             LdifAttrValLine line = ( LdifAttrValLine ) list.get( 0 );
112                             ldifEditor.selectAndReveal( line.getOffset(), line.getRawAttributeDescription()
113                                 .length() );
114                         }
115                     }
116                     else if ( element instanceof LdifAttrValLine )
117                     {
118                         LdifAttrValLine line = ( LdifAttrValLine ) element;
119                         ldifEditor.selectAndReveal( line.getOffset() + line.getRawAttributeDescription().length()
120                             + line.getRawValueType().length(), line.getRawValue().length() );
121                     }
122                     else if ( element instanceof LdifModSpec )
123                     {
124                         LdifModSpec modSpec = ( LdifModSpec ) element;
125                         ldifEditor.selectAndReveal( modSpec.getOffset(), modSpec.getModSpecType().getLength() );
126                     }
127                 }
128             }
129         } );
130
131         treeViewer.addDoubleClickListener( new IDoubleClickListener()
132         {
133             public void doubleClick( DoubleClickEvent event )
134             {
135                 if ( event.getSelection() instanceof IStructuredSelection )
136                 {
137                     Object JavaDoc obj = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement();
138                     if ( treeViewer.getExpandedState( obj ) )
139                         treeViewer.collapseToLevel( obj, 1 );
140                     else if ( ( ( ITreeContentProvider ) treeViewer.getContentProvider() ).hasChildren( obj ) )
141                         treeViewer.expandToLevel( obj, 1 );
142                 }
143             }
144         } );
145
146         this.refresh();
147     }
148
149
150     /**
151      * Refreshes this viewer starting with the given element.
152      *
153      * @param element
154      * the element
155      */

156     public void refresh( Object JavaDoc element )
157     {
158         final TreeViewer treeViewer = getTreeViewer();
159         if ( treeViewer != null && treeViewer.getTree() != null && !treeViewer.getTree().isDisposed() )
160         {
161             treeViewer.refresh( element );
162         }
163     }
164
165
166     /**
167      * Refreshes this viewer completely with information freshly obtained from this viewer's model.
168      */

169     public void refresh()
170     {
171         final TreeViewer treeViewer = getTreeViewer();
172
173         if ( treeViewer != null && treeViewer.getTree() != null && !treeViewer.getTree().isDisposed() )
174         {
175             // ISelection selection = treeViewer.getSelection();
176
// Object[] expandedElements = treeViewer.getExpandedElements();
177

178             if ( !treeViewer.getTree().isEnabled() )
179             {
180                 treeViewer.getTree().setEnabled( true );
181             }
182
183             if ( ldifEditor != null )
184             {
185                 if ( treeViewer.getInput() != ldifEditor.getLdifModel() )
186                 {
187                     treeViewer.setInput( ldifEditor.getLdifModel() );
188                 }
189             }
190
191             treeViewer.refresh();
192
193             // treeViewer.setSelection(selection);
194
// treeViewer.setExpandedElements(expandedElements);
195
}
196     }
197
198
199     /* (non-Javadoc)
200      * @see org.eclipse.ui.part.Page#dispose()
201      */

202     public void dispose()
203     {
204         super.dispose();
205         if ( ldifEditor != null )
206         {
207             ldifEditor.outlinePageClosed();
208             ldifEditor = null;
209         }
210     }
211
212     /**
213      * This class implements the ContentProvider used for the LDIF Outline View
214      *
215      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
216      * @version $Rev$, $Date$
217      */

218     private static class LdifContentProvider implements ITreeContentProvider
219     {
220         /* (non-Javadoc)
221          * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
222          */

223         public Object JavaDoc[] getChildren( Object JavaDoc element )
224         {
225             // file --> records
226
if ( element instanceof LdifFile )
227             {
228                 LdifFile ldifFile = ( LdifFile ) element;
229                 return ldifFile.getRecords();
230             }
231
232             // record --> Array of List of AttrValLine
233
else if ( element instanceof LdifContentRecord )
234             {
235                 LdifContentRecord record = ( LdifContentRecord ) element;
236                 return getUniqueAttrValLineArray( record.getAttrVals() );
237             }
238             else if ( element instanceof LdifChangeAddRecord )
239             {
240                 LdifChangeAddRecord record = ( LdifChangeAddRecord ) element;
241                 return getUniqueAttrValLineArray( record.getAttrVals() );
242             }
243             else if ( element instanceof LdifChangeModifyRecord )
244             {
245                 LdifChangeModifyRecord record = ( LdifChangeModifyRecord ) element;
246                 return record.getModSpecs();
247             }
248             else if ( element instanceof LdifChangeModDnRecord )
249             {
250                 return new Object JavaDoc[0];
251             }
252             else if ( element instanceof LdifChangeDeleteRecord )
253             {
254                 return new Object JavaDoc[0];
255             }
256
257             // List of AttrValLine --> Array of AttrValLine
258
else if ( element instanceof List JavaDoc && ( ( List JavaDoc ) element ).get( 0 ) instanceof LdifAttrValLine )
259             {
260                 List JavaDoc list = ( List JavaDoc ) element;
261                 return list.toArray();
262             }
263             else if ( element instanceof LdifModSpec )
264             {
265                 LdifModSpec modSpec = ( LdifModSpec ) element;
266                 return modSpec.getAttrVals();
267             }
268
269             else
270             {
271                 return new Object JavaDoc[0];
272             }
273         }
274
275
276         /**
277          * Returns a unique line of attribute values from an array of attribute value lines
278          *
279          * @param lines
280          * the attribute value lines
281          * @return
282          * a unique line of attribute values from an array of attribute values lines
283          */

284         private Object JavaDoc[] getUniqueAttrValLineArray( LdifAttrValLine[] lines )
285         {
286             Map JavaDoc uniqueAttrMap = new LinkedHashMap JavaDoc();
287             for ( int i = 0; i < lines.length; i++ )
288             {
289                 if ( !uniqueAttrMap.containsKey( lines[i].getUnfoldedAttributeDescription() ) )
290                 {
291                     uniqueAttrMap.put( lines[i].getUnfoldedAttributeDescription(), new ArrayList JavaDoc() );
292                 }
293                 ( ( List JavaDoc ) uniqueAttrMap.get( lines[i].getUnfoldedAttributeDescription() ) ).add( lines[i] );
294             }
295             return uniqueAttrMap.values().toArray();
296         }
297
298
299         /* (non-Javadoc)
300          * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
301          */

302         public Object JavaDoc getParent( Object JavaDoc element )
303         {
304             return null;
305         }
306
307
308         /* (non-Javadoc)
309          * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
310          */

311         public boolean hasChildren( Object JavaDoc element )
312         {
313             return getChildren( element ) != null && getChildren( element ).length > 0;
314         }
315
316
317         /* (non-Javadoc)
318          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
319          */

320         public Object JavaDoc[] getElements( Object JavaDoc inputElement )
321         {
322             return getChildren( inputElement );
323         }
324
325
326         /* (non-Javadoc)
327          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
328          */

329         public void dispose()
330         {
331         }
332
333
334         /* (non-Javadoc)
335          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
336          */

337         public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
338         {
339         }
340     }
341
342     /**
343      * This class implements the LabelProvider used for the LDIF Outline View
344      *
345      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
346      * @version $Rev$, $Date$
347      */

348     private static class LdifLabelProvider extends LabelProvider
349     {
350         /* (non-Javadoc)
351          * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
352          */

353         public String JavaDoc getText( Object JavaDoc element )
354         {
355             // Record
356
if ( element instanceof LdifRecord )
357             {
358                 LdifRecord ldifRecord = ( LdifRecord ) element;
359                 return ldifRecord.getDnLine().getValueAsString();
360             }
361
362             // List of AttrValLine
363
else if ( element instanceof List JavaDoc && ( ( List JavaDoc ) element ).get( 0 ) instanceof LdifAttrValLine )
364             {
365                 List JavaDoc list = ( List JavaDoc ) element;
366                 return ( ( LdifAttrValLine ) list.get( 0 ) ).getUnfoldedAttributeDescription() + " (" + list.size()
367                     + ")";
368             }
369             else if ( element instanceof LdifModSpec )
370             {
371                 LdifModSpec modSpec = ( LdifModSpec ) element;
372                 return modSpec.getModSpecType().getUnfoldedAttributeDescription() + " (" + modSpec.getAttrVals().length
373                     + ")";
374             }
375
376             // AttrValLine
377
else if ( element instanceof LdifAttrValLine )
378             {
379                 LdifAttrValLine line = ( LdifAttrValLine ) element;
380                 return Utils.getShortenedString( line.getValueAsString(), 20 );
381             }
382
383             else
384             {
385                 return "";
386             }
387         }
388
389
390         /* (non-Javadoc)
391          * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
392          */

393         public Image getImage( Object JavaDoc element )
394         {
395             // Record
396
if ( element instanceof LdifContentRecord )
397             {
398                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_ENTRY );
399             }
400             else if ( element instanceof LdifChangeAddRecord )
401             {
402                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_ADD );
403             }
404             else if ( element instanceof LdifChangeModifyRecord )
405             {
406                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MODIFY );
407             }
408             else if ( element instanceof LdifChangeDeleteRecord )
409             {
410                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_DELETE );
411             }
412             else if ( element instanceof LdifChangeModDnRecord )
413             {
414                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_RENAME );
415             }
416
417             // List of AttrValLine
418
else if ( element instanceof List JavaDoc && ( ( List JavaDoc ) element ).get( 0 ) instanceof LdifAttrValLine )
419             {
420                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_ATTRIBUTE );
421             }
422             else if ( element instanceof LdifModSpec )
423             {
424                 LdifModSpec modSpec = ( LdifModSpec ) element;
425                 if ( modSpec.isAdd() )
426                     return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_ADD );
427                 else if ( modSpec.isReplace() )
428                     return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_REPLACE );
429                 else if ( modSpec.isDelete() )
430                     return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_MOD_DELETE );
431                 else
432                     return null;
433             }
434
435             // AttrValLine
436
else if ( element instanceof LdifAttrValLine )
437             {
438                 return LdifEditorActivator.getDefault().getImage( LdifEditorConstants.IMG_LDIF_VALUE );
439             }
440
441             else
442             {
443                 return null;
444             }
445         }
446     }
447 }
448
Popular Tags