1 20 21 package org.apache.directory.ldapstudio.ldifeditor.editor.text; 22 23 24 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEOFPart; 25 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile; 26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart; 27 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 28 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 29 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifLineBase; 30 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine; 31 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifValueLineBase; 32 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser; 33 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants; 34 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator; 35 36 import org.eclipse.jface.text.BadLocationException; 37 import org.eclipse.jface.text.DefaultTextDoubleClickStrategy; 38 import org.eclipse.jface.text.IDocument; 39 import org.eclipse.jface.text.ITextDoubleClickStrategy; 40 import org.eclipse.jface.text.ITextViewer; 41 import org.eclipse.jface.text.ITypedRegion; 42 43 44 public class LdifDoubleClickStrategy implements ITextDoubleClickStrategy 45 { 46 47 private static final int OFFSET = 0; 48 49 private static final int LENGTH = 1; 50 51 54 private DefaultTextDoubleClickStrategy delegateDoubleClickStrategy; 55 56 57 public LdifDoubleClickStrategy() 58 { 59 this.delegateDoubleClickStrategy = new DefaultTextDoubleClickStrategy(); 60 } 61 62 63 public void doubleClicked( ITextViewer viewer ) 64 { 65 66 if ( !LdifEditorActivator.getDefault().getPreferenceStore().getBoolean( 67 LdifEditorConstants.PREFERENCE_LDIFEDITOR_DOUBLECLICK_USELDIFDOUBLECLICK ) ) 68 { 69 delegateDoubleClickStrategy.doubleClicked( viewer ); 70 } 71 else 72 { 73 74 int cursorPos = viewer.getSelectedRange().x; 75 if ( cursorPos < 0 ) 76 { 77 return; 78 } 79 80 try 81 { 82 LdifParser parser = new LdifParser(); 83 IDocument document = viewer.getDocument(); 84 ITypedRegion partition = document.getPartition( cursorPos ); 85 86 int offset = partition.getOffset(); 88 int relativePos = cursorPos - offset; 89 90 String s = document.get( partition.getOffset(), partition.getLength() ); 92 LdifFile model = parser.parse( s ); 93 LdifContainer container = LdifFile.getContainer( model, relativePos ); 94 if ( container != null ) 95 { 96 LdifPart part = LdifFile.getContainerContent( container, relativePos ); 97 98 if ( part != null && !( part instanceof LdifSepLine ) && !( part instanceof LdifInvalidPart ) 99 && !( part instanceof LdifEOFPart ) ) 100 { 101 102 int[] range = null; 104 if ( part instanceof LdifValueLineBase ) 105 { 106 LdifValueLineBase line = ( LdifValueLineBase ) part; 107 range = getRange( relativePos, part.getOffset(), new String [] 108 { line.getRawLineStart(), line.getRawValueType(), line.getRawValue() } ); 109 } 110 else if ( part instanceof LdifLineBase ) 111 { 112 LdifLineBase line = ( LdifLineBase ) part; 113 range = new int[] 114 { part.getOffset(), part.getLength() - line.getRawNewLine().length() }; 115 } 116 117 int start = range != null ? range[OFFSET] : part.getOffset(); 119 start += offset; 120 int length = range != null ? range[LENGTH] : part.getLength(); 121 viewer.setSelectedRange( start, length ); 122 } 123 else 124 { 125 delegateDoubleClickStrategy.doubleClicked( viewer ); 127 } 128 } 129 130 } 131 catch ( BadLocationException e ) 132 { 133 e.printStackTrace(); 134 } 135 } 136 } 137 138 139 private int[] getRange( int pos, int offset, String [] parts ) 140 { 141 142 for ( int i = 0; i < parts.length; i++ ) 143 { 144 if ( parts[i] != null ) 145 { 146 if ( pos < offset + parts[i].length() ) 147 { 148 return new int[] 149 { offset, parts[i].length() }; 150 } 151 offset += parts[i].length(); 152 } 153 } 154 return null; 155 } 156 157 } 158 | Popular Tags |