1 20 21 package org.apache.directory.ldapstudio.ldifeditor.editor; 22 23 24 import java.util.ArrayList ; 25 import java.util.LinkedHashMap ; 26 import java.util.List ; 27 import java.util.Map ; 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 62 public class LdifOutlinePage extends ContentOutlinePage 63 { 64 65 private LdifEditor ldifEditor; 66 67 68 74 public LdifOutlinePage( LdifEditor ldifEditor ) 75 { 76 this.ldifEditor = ldifEditor; 77 } 78 79 80 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 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 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 ) 107 { 108 List list = ( List ) 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 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 156 public void refresh( Object 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 169 public void refresh() 170 { 171 final TreeViewer treeViewer = getTreeViewer(); 172 173 if ( treeViewer != null && treeViewer.getTree() != null && !treeViewer.getTree().isDisposed() ) 174 { 175 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 } 196 } 197 198 199 202 public void dispose() 203 { 204 super.dispose(); 205 if ( ldifEditor != null ) 206 { 207 ldifEditor.outlinePageClosed(); 208 ldifEditor = null; 209 } 210 } 211 212 218 private static class LdifContentProvider implements ITreeContentProvider 219 { 220 223 public Object [] getChildren( Object element ) 224 { 225 if ( element instanceof LdifFile ) 227 { 228 LdifFile ldifFile = ( LdifFile ) element; 229 return ldifFile.getRecords(); 230 } 231 232 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 [0]; 251 } 252 else if ( element instanceof LdifChangeDeleteRecord ) 253 { 254 return new Object [0]; 255 } 256 257 else if ( element instanceof List && ( ( List ) element ).get( 0 ) instanceof LdifAttrValLine ) 259 { 260 List list = ( List ) 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 [0]; 272 } 273 } 274 275 276 284 private Object [] getUniqueAttrValLineArray( LdifAttrValLine[] lines ) 285 { 286 Map uniqueAttrMap = new LinkedHashMap (); 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 () ); 292 } 293 ( ( List ) uniqueAttrMap.get( lines[i].getUnfoldedAttributeDescription() ) ).add( lines[i] ); 294 } 295 return uniqueAttrMap.values().toArray(); 296 } 297 298 299 302 public Object getParent( Object element ) 303 { 304 return null; 305 } 306 307 308 311 public boolean hasChildren( Object element ) 312 { 313 return getChildren( element ) != null && getChildren( element ).length > 0; 314 } 315 316 317 320 public Object [] getElements( Object inputElement ) 321 { 322 return getChildren( inputElement ); 323 } 324 325 326 329 public void dispose() 330 { 331 } 332 333 334 337 public void inputChanged( Viewer viewer, Object oldInput, Object newInput ) 338 { 339 } 340 } 341 342 348 private static class LdifLabelProvider extends LabelProvider 349 { 350 353 public String getText( Object element ) 354 { 355 if ( element instanceof LdifRecord ) 357 { 358 LdifRecord ldifRecord = ( LdifRecord ) element; 359 return ldifRecord.getDnLine().getValueAsString(); 360 } 361 362 else if ( element instanceof List && ( ( List ) element ).get( 0 ) instanceof LdifAttrValLine ) 364 { 365 List list = ( List ) 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 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 393 public Image getImage( Object element ) 394 { 395 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 else if ( element instanceof List && ( ( List ) 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 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 |