1 20 package org.apache.directory.ldapstudio.aciitemeditor.widgets; 21 22 23 import java.text.ParseException ; 24 25 import org.apache.directory.ldapstudio.aciitemeditor.ACIItemValueWithContext; 26 import org.apache.directory.ldapstudio.aciitemeditor.Activator; 27 import org.apache.directory.ldapstudio.aciitemeditor.sourceeditor.ACISourceViewerConfiguration; 28 import org.apache.directory.shared.ldap.aci.ACIItem; 29 import org.apache.directory.shared.ldap.aci.ACIItemParser; 30 import org.eclipse.jface.resource.JFaceResources; 31 import org.eclipse.jface.text.Document; 32 import org.eclipse.jface.text.IDocument; 33 import org.eclipse.jface.text.IRegion; 34 import org.eclipse.jface.text.Region; 35 import org.eclipse.jface.text.source.SourceViewer; 36 import org.eclipse.jface.text.source.SourceViewerConfiguration; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.graphics.Font; 39 import org.eclipse.swt.layout.FillLayout; 40 import org.eclipse.swt.widgets.Composite; 41 42 43 49 public class ACIItemSourceEditorComposite extends Composite 50 { 51 52 53 private SourceViewer sourceEditor; 54 55 56 private SourceViewerConfiguration configuration; 57 58 59 65 public ACIItemSourceEditorComposite( Composite parent, int style ) 66 { 67 super( parent, style ); 68 setLayout( new FillLayout() ); 69 70 createSourceEditor(); 71 } 72 73 74 78 private void createSourceEditor() 79 { 80 sourceEditor = new SourceViewer( this, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL ); 82 83 configuration = new ACISourceViewerConfiguration(); 85 sourceEditor.configure( configuration ); 86 87 Font font = JFaceResources.getFont( JFaceResources.TEXT_FONT ); 89 sourceEditor.getTextWidget().setFont( font ); 90 91 IDocument document = new Document(); 93 sourceEditor.setDocument( document ); 94 } 95 96 97 105 public void setInput( String input ) throws ParseException 106 { 107 ACIItemParser parser = Activator.getDefault().getACIItemParser(); 108 parser.parse( input ); 109 110 forceSetInput( input ); 111 } 112 113 114 119 public void forceSetInput( String input ) 120 { 121 sourceEditor.getDocument().set( input ); 122 123 IRegion region = new Region( 0, sourceEditor.getDocument().getLength() ); 125 configuration.getContentFormatter( sourceEditor ).format( sourceEditor.getDocument(), region ); 126 127 } 128 129 130 138 public String getInput() throws ParseException 139 { 140 String input = forceGetInput(); 141 142 input = input.replaceAll( "\\n", " " ); input = input.replaceAll( "\\r", " " ); 146 ACIItemParser parser = Activator.getDefault().getACIItemParser(); 147 ACIItem aciItem = parser.parse( input ); 148 149 StringBuffer buffer = new StringBuffer (); 150 if ( aciItem != null ) 151 { 152 aciItem.printToBuffer( buffer ); 153 } 154 return buffer.toString(); 155 } 156 157 158 164 public String forceGetInput() 165 { 166 return sourceEditor.getDocument().get(); 167 } 168 169 170 175 public void setContext( ACIItemValueWithContext context ) 176 { 177 178 } 179 180 181 184 public void format() 185 { 186 IRegion region = new Region( 0, sourceEditor.getDocument().getLength() ); 187 configuration.getContentFormatter( sourceEditor ).format( sourceEditor.getDocument(), region ); 188 } 189 190 } 191 | Popular Tags |