1 20 21 package org.apache.directory.ldapstudio.ldifeditor.widgets; 22 23 24 import org.apache.directory.ldapstudio.browser.common.widgets.BrowserWidget; 25 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile; 27 import org.apache.directory.ldapstudio.ldifeditor.editor.ILdifEditor; 28 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifDocumentProvider; 29 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifSourceViewerConfiguration; 30 import org.apache.directory.ldapstudio.ldifeditor.editor.NonExistingLdifEditorInput; 31 import org.eclipse.core.runtime.CoreException; 32 import org.eclipse.jface.resource.JFaceResources; 33 import org.eclipse.jface.text.IDocument; 34 import org.eclipse.jface.text.ITextListener; 35 import org.eclipse.jface.text.TextEvent; 36 import org.eclipse.jface.text.source.SourceViewer; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.graphics.Font; 39 import org.eclipse.swt.layout.GridData; 40 import org.eclipse.swt.layout.GridLayout; 41 import org.eclipse.swt.widgets.Composite; 42 43 44 51 public class LdifEditorWidget extends BrowserWidget implements ILdifEditor, ITextListener 52 { 53 54 55 private IConnection connection; 56 57 58 private String initialLdif; 59 60 61 private boolean contentAssistEnabled; 62 63 64 private NonExistingLdifEditorInput editorInput; 65 66 67 private LdifDocumentProvider documentProvider; 68 69 70 private SourceViewer sourceViewer; 71 72 73 private LdifSourceViewerConfiguration sourceViewerConfiguration; 74 75 76 83 public LdifEditorWidget( IConnection connection, String initialLdif, boolean contentAssistEnabled ) 84 { 85 this.connection = connection; 86 this.initialLdif = initialLdif; 87 this.contentAssistEnabled = contentAssistEnabled; 88 } 89 90 91 94 public void dispose() 95 { 96 if ( editorInput != null ) 97 { 98 sourceViewer.removeTextListener( this ); 99 documentProvider.disconnect( editorInput ); 100 editorInput = null; 102 } 103 } 104 105 106 111 public void createWidget( Composite parent ) 112 { 113 Composite composite = new Composite( parent, SWT.NONE ); 114 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 115 GridLayout layout = new GridLayout( 1, false ); 116 layout.marginWidth = 0; 117 layout.marginHeight = 0; 118 composite.setLayout( layout ); 119 120 sourceViewer = new SourceViewer( composite, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL ); 124 sourceViewer.getControl().setLayoutData( new GridData( GridData.FILL_BOTH ) ); 125 126 sourceViewerConfiguration = new LdifSourceViewerConfiguration( this, this.contentAssistEnabled ); 128 sourceViewer.configure( sourceViewerConfiguration ); 129 130 Font font = JFaceResources.getFont( JFaceResources.TEXT_FONT ); 132 sourceViewer.getTextWidget().setFont( font ); 133 134 try 136 { 137 editorInput = new NonExistingLdifEditorInput(); 138 documentProvider = new LdifDocumentProvider(); 139 documentProvider.connect( editorInput ); 140 141 IDocument document = documentProvider.getDocument( editorInput ); 142 document.set( initialLdif ); 143 sourceViewer.setDocument( document ); 144 } 145 catch ( CoreException e ) 146 { 147 e.printStackTrace(); 148 } 149 150 sourceViewer.addTextListener( this ); 152 153 sourceViewer.getControl().setFocus(); 155 } 156 157 158 161 public IConnection getConnection() 162 { 163 return connection; 164 } 165 166 167 170 public LdifFile getLdifModel() 171 { 172 return documentProvider.getLdifModel(); 173 } 174 175 176 179 public Object getAdapter( Class adapter ) 180 { 181 return null; 182 } 183 184 185 188 public void textChanged( TextEvent event ) 189 { 190 super.notifyListeners(); 191 } 192 193 194 199 public SourceViewer getSourceViewer() 200 { 201 return sourceViewer; 202 } 203 204 205 210 public LdifSourceViewerConfiguration getSourceViewerConfiguration() 211 { 212 return sourceViewerConfiguration; 213 } 214 215 } 216 | Popular Tags |