1 20 21 package org.apache.directory.ldapstudio.browser.ui.editors.entry; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Iterator ; 27 import java.util.LinkedHashMap ; 28 import java.util.List ; 29 import java.util.Map ; 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 70 public class EntryEditorOutlinePage extends ContentOutlinePage 71 { 72 73 private EntryEditor entryEditor; 74 75 76 82 public EntryEditorOutlinePage( EntryEditor entryEditor ) 83 { 84 this.entryEditor = entryEditor; 85 } 86 87 88 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 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 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 selectionList = new ArrayList (); 114 115 Iterator it = ( ( IStructuredSelection ) event.getSelection() ).iterator(); 116 while ( it.hasNext() ) 117 { 118 Object element = it.next(); 119 120 if ( element instanceof LdifAttrValLine ) 121 { 122 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 ) 142 { 143 List list = ( List ) 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 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 203 public void refresh( Object 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 216 public void refresh() 217 { 218 final TreeViewer treeViewer = getTreeViewer(); 219 220 if ( treeViewer != null && treeViewer.getTree() != null && !treeViewer.getTree().isDisposed() ) 221 { 222 225 if ( !treeViewer.getTree().isEnabled() ) 226 { 227 treeViewer.getTree().setEnabled( true ); 228 } 229 230 if ( entryEditor != null ) 231 { 232 Object 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 sb = new StringBuffer (); 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 } 259 } 260 261 262 265 public void dispose() 266 { 267 super.dispose(); 268 if ( entryEditor != null ) 269 { 270 entryEditor = null; 271 } 272 } 273 274 280 private static class LdifContentProvider implements ITreeContentProvider 281 { 282 285 public Object [] getChildren( Object element ) 286 { 287 if ( element instanceof LdifFile ) 289 { 290 LdifFile ldifFile = ( LdifFile ) element; 291 return ldifFile.getRecords(); 292 } 293 294 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 [0]; 313 } 314 else if ( element instanceof LdifChangeDeleteRecord ) 315 { 316 return new Object [0]; 317 } 318 319 else if ( element instanceof List && ( ( List ) element ).get( 0 ) instanceof LdifAttrValLine ) 321 { 322 List list = ( List ) 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 [0]; 334 } 335 } 336 337 338 346 private Object [] getUniqueAttrValLineArray( LdifAttrValLine[] lines ) 347 { 348 Map uniqueAttrMap = new LinkedHashMap (); 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 () ); 354 } 355 ( ( List ) uniqueAttrMap.get( lines[i].getUnfoldedAttributeDescription() ) ).add( lines[i] ); 356 } 357 return uniqueAttrMap.values().toArray(); 358 } 359 360 361 364 public Object getParent( Object element ) 365 { 366 return null; 367 } 368 369 370 373 public boolean hasChildren( Object element ) 374 { 375 return getChildren( element ) != null && getChildren( element ).length > 0; 376 } 377 378 379 382 public Object [] getElements( Object inputElement ) 383 { 384 return getChildren( inputElement ); 385 } 386 387 388 391 public void dispose() 392 { 393 } 394 395 396 399 public void inputChanged( Viewer viewer, Object oldInput, Object newInput ) 400 { 401 } 402 } 403 404 410 private static class LdifLabelProvider extends LabelProvider 411 { 412 415 public String getText( Object element ) 416 { 417 if ( element instanceof LdifRecord ) 419 { 420 LdifRecord ldifRecord = ( LdifRecord ) element; 421 return ldifRecord.getDnLine().getValueAsString(); 422 } 423 424 else if ( element instanceof List && ( ( List ) element ).get( 0 ) instanceof LdifAttrValLine ) 426 { 427 List list = ( List ) 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 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 455 public Image getImage( Object element ) 456 { 457 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 else if ( element instanceof List && ( ( List ) 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 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 |