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.Schema; 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.editors.schema.SchemaEditor; 32 import org.apache.directory.ldapstudio.schemas.view.views.SchemasView; 33 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.AttributeTypeWrapper; 34 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ITreeNode; 35 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.IntermediateNode; 36 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.ObjectClassWrapper; 37 import org.apache.directory.ldapstudio.schemas.view.views.wrappers.SchemaWrapper; 38 import org.eclipse.jface.action.Action; 39 import org.eclipse.jface.viewers.ISelection; 40 import org.eclipse.jface.viewers.ITreeSelection; 41 import org.eclipse.jface.viewers.StructuredSelection; 42 import org.eclipse.ui.IEditorPart; 43 import org.eclipse.ui.IEditorReference; 44 import org.eclipse.ui.IPartListener2; 45 import org.eclipse.ui.ISelectionListener; 46 import org.eclipse.ui.IWorkbenchPart; 47 import org.eclipse.ui.IWorkbenchPartReference; 48 import org.eclipse.ui.PlatformUI; 49 import org.eclipse.ui.plugin.AbstractUIPlugin; 50 51 52 58 public class LinkWithEditorSchemasView extends Action 59 { 60 61 private static final String LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY = LinkWithEditorSchemasView.class.getName() 62 + ".dialogsettingkey"; 64 65 private SchemasView schemasView; 66 67 68 private IPartListener2 editorListener = new IPartListener2() 69 { 70 73 public void partVisible( IWorkbenchPartReference partRef ) 74 { 75 IWorkbenchPart part = partRef.getPart( true ); 76 77 if ( part instanceof ObjectClassEditor ) 78 { 79 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 80 linkViewWithEditor( ( ( ObjectClassEditor ) part ).getOriginalObjectClass() ); 81 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 82 } 83 else if ( part instanceof AttributeTypeEditor ) 84 { 85 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 86 linkViewWithEditor( ( ( AttributeTypeEditor ) part ).getOriginalAttributeType() ); 87 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 88 } 89 else if ( part instanceof SchemaEditor ) 90 { 91 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 92 linkViewWithEditor( ( ( SchemaEditor ) part ).getSchema() ); 93 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 94 } 95 } 96 97 98 101 public void partActivated( IWorkbenchPartReference partRef ) 102 { 103 } 104 105 106 109 public void partClosed( IWorkbenchPartReference partRef ) 110 { 111 } 112 113 114 117 public void partDeactivated( IWorkbenchPartReference partRef ) 118 { 119 } 120 121 122 125 public void partHidden( IWorkbenchPartReference partRef ) 126 { 127 } 128 129 130 133 public void partInputChanged( IWorkbenchPartReference partRef ) 134 { 135 } 136 137 138 141 public void partOpened( IWorkbenchPartReference partRef ) 142 { 143 } 144 145 146 149 public void partBroughtToTop( IWorkbenchPartReference partRef ) 150 { 151 } 152 }; 153 154 155 private ISelectionListener viewListener = new ISelectionListener() 156 { 157 160 public void selectionChanged( IWorkbenchPart part, ISelection selection ) 161 { 162 ITreeSelection iSelection = ( ITreeSelection ) selection; 163 164 Object selectedObject = iSelection.getFirstElement(); 165 166 if ( ( selectedObject instanceof SchemaWrapper ) || ( selectedObject instanceof ObjectClassWrapper ) 167 || ( selectedObject instanceof AttributeTypeWrapper ) ) 168 { 169 linkEditorWithView( ( ITreeNode ) selectedObject ); 170 } 171 } 172 }; 173 174 175 181 public LinkWithEditorSchemasView( SchemasView view ) 182 { 183 super( Messages.getString( "LinkWithEditorSchemasView.Link_with_Editor" ), AS_CHECK_BOX ); setToolTipText( getText() ); 185 setId( PluginConstants.CMD_LINK_WITH_EDITOR_SCHEMA_VIEW ); 186 setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID, 187 PluginConstants.IMG_LINK_WITH_EDITOR ) ); 188 setEnabled( true ); 189 schemasView = view; 190 191 if ( Activator.getDefault().getDialogSettings().get( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY ) == null ) 193 { 194 Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, false ); 195 } 196 197 setChecked( Activator.getDefault().getDialogSettings().getBoolean( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY ) ); 199 200 if ( isChecked() ) 202 { 203 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener ); 204 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 205 } 206 } 207 208 209 212 public void run() 213 { 214 setChecked( isChecked() ); 215 Activator.getDefault().getDialogSettings().put( LINK_WITH_EDITOR_SCHEMAS_VIEW_DS_KEY, isChecked() ); 216 217 if ( isChecked() ) { 219 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().addPartListener( editorListener ); 220 221 IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() 222 .getActiveEditor(); 223 if ( activeEditor instanceof ObjectClassEditor ) 224 { 225 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 226 linkViewWithEditor( ( ( ObjectClassEditor ) activeEditor ).getOriginalObjectClass() ); 227 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 228 } 229 else if ( activeEditor instanceof AttributeTypeEditor ) 230 { 231 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 232 linkViewWithEditor( ( ( AttributeTypeEditor ) activeEditor ).getOriginalAttributeType() ); 233 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 234 } 235 else if ( activeEditor instanceof SchemaEditor ) 236 { 237 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 238 linkViewWithEditor( ( ( SchemaEditor ) activeEditor ).getSchema() ); 239 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 240 } 241 242 schemasView.getSite().getPage().addPostSelectionListener( viewListener ); 243 } 244 else 245 { 247 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().removePartListener( editorListener ); 248 schemasView.getSite().getPage().removePostSelectionListener( viewListener ); 249 } 250 } 251 252 253 259 private void linkViewWithEditor( Object o ) 260 { 261 StructuredSelection structuredSelection = null; 262 ITreeNode wrapper = null; 263 264 if ( o instanceof AttributeType ) 265 { 266 wrapper = new AttributeTypeWrapper( ( AttributeType ) o, null ); 267 structuredSelection = new StructuredSelection( wrapper ); 268 } 269 else if ( o instanceof ObjectClass ) 270 { 271 wrapper = new ObjectClassWrapper( ( ObjectClass ) o, null ); 272 structuredSelection = new StructuredSelection( wrapper ); 273 } 274 else if ( o instanceof Schema ) 275 { 276 wrapper = new SchemaWrapper( ( Schema ) o, null ); 277 structuredSelection = new StructuredSelection( wrapper ); 278 } 279 else 280 { 281 return; 283 } 284 285 Object foundItem = schemasView.getViewer().testFindItem( wrapper ); 286 if ( foundItem != null ) { 288 schemasView.getViewer().setSelection( structuredSelection, true ); 289 } 290 else 291 { 293 ITreeNode foundElement = schemasView.findElementInTree( wrapper ); 294 295 if ( foundElement != null ) 296 { 297 expandFromTopToBottom( foundElement ); 298 schemasView.getViewer().setSelection( structuredSelection ); 299 } 300 } 301 } 302 303 304 310 private void expandFromTopToBottom( Object element ) 311 { 312 if ( element instanceof SchemaWrapper ) 313 { 314 SchemaWrapper schemaWrapper = ( SchemaWrapper ) element; 315 if ( !schemasView.getViewer().getExpandedState( schemaWrapper ) ) 316 { 317 schemasView.getViewer().setExpandedState( schemaWrapper, true ); 318 } 319 } 320 else if ( element instanceof ObjectClassWrapper ) 321 { 322 ObjectClassWrapper objectClassWrapper = ( ObjectClassWrapper ) element; 323 expandFromTopToBottom( objectClassWrapper.getParent() ); 324 schemasView.getViewer().setExpandedState( objectClassWrapper, true ); 325 } 326 else if ( element instanceof AttributeTypeWrapper ) 327 { 328 AttributeTypeWrapper attributeTypeWrapper = ( AttributeTypeWrapper ) element; 329 expandFromTopToBottom( attributeTypeWrapper.getParent() ); 330 schemasView.getViewer().setExpandedState( attributeTypeWrapper, true ); 331 } 332 else if ( element instanceof IntermediateNode ) 333 { 334 IntermediateNode intermediateNode = ( IntermediateNode ) element; 335 expandFromTopToBottom( intermediateNode.getParent() ); 336 schemasView.getViewer().setExpandedState( intermediateNode, true ); 337 } 338 } 339 340 341 347 private void linkEditorWithView( ITreeNode wrapper ) 348 { 349 IEditorReference[] references = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() 350 .getEditorReferences(); 351 352 for ( IEditorReference reference : references ) 353 { 354 IWorkbenchPart workbenchPart = reference.getPart( true ); 355 356 if ( ( ( workbenchPart instanceof ObjectClassEditor ) && ( wrapper instanceof ObjectClassWrapper ) && ( reference 357 .getPartName().equals( ( ( ObjectClassWrapper ) wrapper ).getMyObjectClass().getNames()[0] ) ) ) 358 || ( ( workbenchPart instanceof AttributeTypeEditor ) && ( wrapper instanceof AttributeTypeWrapper ) && ( reference 359 .getPartName().equals( ( ( AttributeTypeWrapper ) wrapper ).getMyAttributeType().getNames()[0] ) ) ) 360 || ( ( workbenchPart instanceof SchemaEditor ) && ( wrapper instanceof SchemaWrapper ) && ( reference 361 .getPartName().equals( ( ( SchemaWrapper ) wrapper ).getMySchema().getName() ) ) ) ) 362 { 363 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop( workbenchPart ); 364 return; 365 } 366 } 367 } 368 } 369 | Popular Tags |