1 20 21 package org.apache.directory.ldapstudio.browser.common.dialogs; 22 23 24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 26 import org.apache.directory.ldapstudio.browser.common.filtereditor.FilterSourceViewerConfiguration; 27 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 28 import org.apache.directory.ldapstudio.browser.core.model.filter.parser.LdapFilterParser; 29 import org.eclipse.jface.dialogs.Dialog; 30 import org.eclipse.jface.dialogs.IDialogConstants; 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.VerticalRuler; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.swt.widgets.Shell; 42 43 44 public class FilterDialog extends Dialog 45 { 46 47 public static final String DIALOG_TITLE = "Filter Editor"; 48 49 private String title; 50 51 private IConnection connection; 52 53 private SourceViewer sourceViewer; 54 55 private FilterSourceViewerConfiguration configuration; 56 57 private LdapFilterParser parser; 58 59 private String filter; 60 61 62 public FilterDialog( Shell parentShell, String title, String filter, IConnection connection ) 63 { 64 super( parentShell ); 65 this.title = title; 66 this.filter = filter; 67 this.connection = connection; 68 this.parser = new LdapFilterParser(); 69 setShellStyle( SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE ); 70 } 71 72 73 public String getFilter() 74 { 75 return this.filter; 76 } 77 78 79 protected void configureShell( Shell newShell ) 80 { 81 super.configureShell( newShell ); 82 newShell.setText( this.title != null ? this.title : DIALOG_TITLE ); 83 newShell.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_FILTER_EDITOR ) ); 84 } 85 86 87 protected void buttonPressed( int buttonId ) 88 { 89 if ( buttonId == IDialogConstants.OK_ID ) 90 { 91 this.parser.parse( sourceViewer.getDocument().get() ); 93 this.filter = this.parser.getModel().toString(); 94 } 95 else if ( buttonId == 987654321 ) 96 { 97 IRegion region = new Region( 0, sourceViewer.getDocument().getLength() ); 98 configuration.getContentFormatter( sourceViewer ).format( sourceViewer.getDocument(), region ); 99 } 100 101 super.buttonPressed( buttonId ); 103 } 104 105 106 protected Control createButtonBar( Composite parent ) 107 { 108 Composite composite = ( Composite ) super.createButtonBar( parent ); 109 super.createButton( composite, 987654321, "Format", false ); 110 return composite; 111 } 112 113 114 protected Control createDialogArea( Composite parent ) 115 { 116 Composite composite = ( Composite ) super.createDialogArea( parent ); 118 GridData gd = new GridData( GridData.FILL_BOTH ); 119 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 120 gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 121 composite.setLayoutData( gd ); 122 123 sourceViewer = new SourceViewer( composite, new VerticalRuler( 0 ), SWT.H_SCROLL | SWT.V_SCROLL ); 125 sourceViewer.getControl().setLayoutData( new GridData( GridData.FILL_BOTH ) ); 126 configuration = new FilterSourceViewerConfiguration( this.sourceViewer, this.parser, this.connection ); 127 sourceViewer.configure( configuration ); 128 129 IDocument document = new Document( this.filter ); 131 sourceViewer.setDocument( document ); 132 133 IRegion region = new Region( 0, sourceViewer.getDocument().getLength() ); 135 configuration.getContentFormatter( sourceViewer ).format( sourceViewer.getDocument(), region ); 136 137 sourceViewer.getTextWidget().setFocus(); 138 139 return composite; 140 } 141 142 143 protected boolean canHandleShellCloseEvent() 144 { 145 return super.canHandleShellCloseEvent(); 147 } 148 149 } 150 | Popular Tags |