1 20 21 package org.apache.directory.ldapstudio.ldifeditor.editor.actions; 22 23 24 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile; 25 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 26 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 27 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec; 28 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor; 29 30 import org.eclipse.jface.action.Action; 31 import org.eclipse.jface.text.source.ISourceViewer; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.ui.texteditor.IUpdate; 34 35 36 public abstract class AbstractLdifAction extends Action implements IUpdate 37 { 38 39 protected LdifEditor editor; 40 41 42 public AbstractLdifAction( String text, LdifEditor editor ) 43 { 44 super( text ); 45 this.editor = editor; 46 } 47 48 49 public final void run() 50 { 51 if ( this.isEnabled() ) 52 { 53 doRun(); 54 } 55 } 56 57 58 protected abstract void doRun(); 59 60 61 public boolean isEnabled() 62 { 63 update(); 64 return super.isEnabled(); 65 } 66 67 68 protected LdifFile getLdifModel() 69 { 70 LdifFile model = editor.getLdifModel(); 71 return model; 72 } 73 74 75 protected LdifContainer[] getSelectedLdifContainers() 76 { 77 78 LdifContainer[] containers = null; 79 80 ISourceViewer sourceViewer = ( ISourceViewer ) editor.getAdapter( ISourceViewer.class ); 81 if ( sourceViewer != null ) 82 { 83 LdifFile model = editor.getLdifModel(); 84 Point selection = sourceViewer.getSelectedRange(); 85 containers = LdifFile.getContainers( model, selection.x, selection.y ); 86 } 87 88 return containers != null ? containers : new LdifContainer[0]; 89 90 } 91 92 93 protected LdifPart[] getSelectedLdifParts() 94 { 95 96 LdifPart[] parts = null; 97 98 ISourceViewer sourceViewer = ( ISourceViewer ) editor.getAdapter( ISourceViewer.class ); 99 if ( sourceViewer != null ) 100 { 101 LdifFile model = editor.getLdifModel(); 102 Point selection = sourceViewer.getSelectedRange(); 103 parts = LdifFile.getParts( model, selection.x, selection.y ); 104 105 } 106 107 return parts != null ? parts : new LdifPart[0]; 108 109 } 110 111 112 protected LdifModSpec getSelectedLdifModSpec() 113 { 114 115 LdifModSpec modSpec = null; 116 117 LdifContainer[] containers = getSelectedLdifContainers(); 118 if ( containers.length == 1 ) 119 { 120 ISourceViewer sourceViewer = ( ISourceViewer ) editor.getAdapter( ISourceViewer.class ); 121 if ( sourceViewer != null ) 122 { 123 Point selection = sourceViewer.getSelectedRange(); 124 modSpec = LdifFile.getInnerContainer( containers[0], selection.x ); 125 } 126 } 127 128 return modSpec; 129 130 } 131 132 } 133 | Popular Tags |