1 20 package org.apache.directory.ldapstudio.schemas.controller.actions; 21 22 23 import org.apache.directory.ldapstudio.schemas.Activator; 24 import org.apache.directory.ldapstudio.schemas.Messages; 25 import org.apache.directory.ldapstudio.schemas.PluginConstants; 26 import org.apache.directory.ldapstudio.schemas.model.AttributeType; 27 import org.apache.directory.ldapstudio.schemas.model.ObjectClass; 28 import org.apache.directory.ldapstudio.schemas.model.SchemaElement; 29 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor; 30 import org.apache.directory.ldapstudio.schemas.view.editors.objectClass.ObjectClassEditor; 31 import org.apache.directory.ldapstudio.schemas.view.views.SchemaElementsView; 32 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper; 33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode; 34 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode; 35 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper; 36 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper; 37 import org.eclipse.jface.action.Action; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.viewers.ITreeSelection; 40 import org.eclipse.jface.viewers.StructuredSelection; 41 import org.eclipse.ui.IEditorPart; 42 import org.eclipse.ui.IEditorReference; 43 import org.eclipse.ui.IPartListener2; 44 import org.eclipse.ui.ISelectionListener; 45 import org.eclipse.ui.IWorkbenchPart; 46 import org.eclipse.ui.IWorkbenchPartReference; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.plugin.AbstractUIPlugin; 49 50 51 57 public class LinkWithEditorSchemaElementsView extends Action 58 { 59 60 private static final String LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY = LinkWithEditorSchemaElementsView.class.getName() 61 + ".dialogsettingkey"; 63 64 private SchemaElementsView view; 65 66 67 private IPartListener2 editorListener = new IPartListener2() 68 { 69 72 public void partVisible( IWorkbenchPartReference partRef ) 73 { 74 IWorkbenchPart part = partRef.getPart( true ); 75 76 if ( part instanceof ObjectClassEditor ) 77 { 78 view.getSite().getPage().removePostSelectionListener( viewListener ); 79 linkViewWithEditor( ( ( ObjectClassEditor ) part ).getOriginalObjectClass() ); 80 view.getSite().getPage().addPostSelectionListener( viewListener ); 81 } 82 else if ( part instanceof AttributeTypeEditor ) 83 { 84 view.getSite().getPage().removePostSelectionListener( viewListener ); 85 linkViewWithEditor( ( ( AttributeTypeEditor ) part ).getOriginalAttributeType() ); 86 view.getSite().getPage().addPostSelectionListener( viewListener ); 87 } 88 } 89 90 91 94 public void partActivated( IWorkbenchPartReference partRef ) 95 { 96 } 97 98 99 102 public void partClosed( IWorkbenchPartReference partRef ) 103 { 104 } 105 106 107 110 public void partDeactivated( IWorkbenchPartReference partRef ) 111 { 112 } 113 114 115 118 public void partHidden( IWorkbenchPartReference partRef ) 119 { 120 } 121 122 123 126 public void partInputChanged( IWorkbenchPartReference partRef ) 127 { 128 } 129 130 131 134 public void partOpened( IWorkbenchPartReference partRef ) 135 { 136 } 137 138 139 142 public void partBroughtToTop( IWorkbenchPartReference partRef ) 143 { 144 } 145 }; 146 147 148 private ISelectionListener viewListener = new ISelectionListener() 149 { 150 153 public void selectionChanged( IWorkbenchPart part, ISelection selection ) 154 { 155 ITreeSelection iSelection = ( ITreeSelection ) selection; 156 157 Object selectedObject = iSelection.getFirstElement(); 158 159 if ( ( selectedObject instanceof SchemaWrapper ) || ( selectedObject instanceof ObjectClassWrapper ) 160 || ( selectedObject instanceof AttributeTypeWrapper ) ) 161 { 162 linkEditorWithView( ( ITreeNode ) selectedObject ); 163 } 164 } 165 }; 166 167 168 174 public LinkWithEditorSchemaElementsView( SchemaElementsView view ) 175 { 176 super( Messages.getString( "LinkWithEditorSchemaElementsView.Link_with_Editor" ), AS_CHECK_BOX ); setToolTipText( getText() ); 178 setId( PluginConstants.CMD_LINK_WITH_EDITOR_SCHEMA_ELEMENTS_VIEW ); 179 setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID, 180 PluginConstants.IMG_LINK_WITH_EDITOR ) ); 181 setEnabled( true ); 182 this.view = view; 183 184 if ( Activator.getDefault().getDialogSettings().get( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY ) == null ) 186 { 187 Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, false ); 188 } 189 190 setChecked( Activator.getDefault().getDialogSettings().getBoolean( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY ) ); 192 193 if ( isChecked() ) 195 { 196 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener ); 197 view.getSite().getPage().addPostSelectionListener( viewListener ); 198 } 199 } 200 201 202 205 public void run() 206 { 207 setChecked( isChecked() ); 208 Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, isChecked() ); 209 210 if ( isChecked() ) { 212 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener ); 213 214 IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() 215 .getActiveEditor(); 216 if ( activeEditor instanceof ObjectClassEditor ) 217 { 218 linkViewWithEditor( ( ( ObjectClassEditor ) activeEditor ).getOriginalObjectClass() ); 219 } 220 else if ( activeEditor instanceof AttributeTypeEditor ) 221 { 222 linkViewWithEditor( ( ( AttributeTypeEditor ) activeEditor ).getOriginalAttributeType() ); 223 } 224 225 view.getSite().getPage().addPostSelectionListener( viewListener ); 226 } 227 else 228 { 230 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().removePartListener( editorListener ); 231 view.getSite().getPage().removePostSelectionListener( viewListener ); 232 } 233 } 234 235 236 242 private void linkViewWithEditor( SchemaElement schemaElement ) 243 { 244 StructuredSelection structuredSelection = null; 245 ITreeNode wrapper = null; 246 247 if ( schemaElement instanceof AttributeType ) 248 { 249 wrapper = new AttributeTypeWrapper( ( AttributeType ) schemaElement, null ); 250 structuredSelection = new StructuredSelection( wrapper ); 251 252 view.getViewer().setSelection( structuredSelection, true ); 253 } 254 else if ( schemaElement instanceof ObjectClass ) 255 { 256 wrapper = new ObjectClassWrapper( ( ObjectClass ) schemaElement, null ); 257 structuredSelection = new StructuredSelection( wrapper ); 258 } 259 else 260 { 261 return; 262 } 263 264 Object foundItem = view.getViewer().testFindItem( wrapper ); 265 if ( foundItem != null ) { 267 view.getViewer().setSelection( structuredSelection, true ); 268 } 269 else 270 { 272 ITreeNode foundElement = view.findElementInTree( wrapper ); 273 274 if ( foundElement != null ) 275 { 276 expandFromTopToBottom( foundElement ); 277 view.getViewer().setSelection( structuredSelection ); 278 } 279 } 280 } 281 282 283 289 private void expandFromTopToBottom( Object element ) 290 { 291 if ( element instanceof SchemaWrapper ) 292 { 293 SchemaWrapper schemaWrapper = ( SchemaWrapper ) element; 294 if ( !view.getViewer().getExpandedState( schemaWrapper ) ) 295 { 296 view.getViewer().setExpandedState( schemaWrapper, true ); 297 } 298 } 299 else if ( element instanceof ObjectClassWrapper ) 300 { 301 ObjectClassWrapper objectClassWrapper = ( ObjectClassWrapper ) element; 302 expandFromTopToBottom( objectClassWrapper.getParent() ); 303 view.getViewer().setExpandedState( objectClassWrapper, true ); 304 } 305 else if ( element instanceof AttributeTypeWrapper ) 306 { 307 AttributeTypeWrapper attributeTypeWrapper = ( AttributeTypeWrapper ) element; 308 expandFromTopToBottom( attributeTypeWrapper.getParent() ); 309 view.getViewer().setExpandedState( attributeTypeWrapper, true ); 310 } 311 else if ( element instanceof IntermediateNode ) 312 { 313 IntermediateNode intermediateNode = ( IntermediateNode ) element; 314 expandFromTopToBottom( intermediateNode.getParent() ); 315 view.getViewer().setExpandedState( intermediateNode, true ); 316 } 317 } 318 319 320 326 private void linkEditorWithView( ITreeNode wrapper ) 327 { 328 if ( wrapper != null ) 329 { 330 IEditorReference[] references = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() 331 .getEditorReferences(); 332 333 for ( IEditorReference reference : references ) 334 { 335 IWorkbenchPart workbenchPart = reference.getPart( true ); 336 337 if ( ( workbenchPart instanceof ObjectClassEditor ) && ( wrapper instanceof ObjectClassWrapper ) ) 338 { 339 if ( ( ( ObjectClassEditor ) workbenchPart ).getOriginalObjectClass().equals( 340 ( ( ObjectClassWrapper ) wrapper ).getMyObjectClass() ) ) 341 { 342 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop( workbenchPart ); 343 return; 344 } 345 } 346 else if ( ( workbenchPart instanceof AttributeTypeEditor ) 347 && ( wrapper instanceof AttributeTypeWrapper ) ) 348 { 349 if ( ( ( AttributeTypeEditor ) workbenchPart ).getOriginalAttributeType().equals( 350 ( ( AttributeTypeWrapper ) wrapper ).getMyAttributeType() ) ) 351 { 352 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop( workbenchPart ); 353 return; 354 } 355 } 356 } 357 } 358 } 359 } 360 | Popular Tags |