KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dialogs > FilterDialog


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

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 JavaDoc DIALOG_TITLE = "Filter Editor";
48
49     private String JavaDoc 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 JavaDoc filter;
60
61
62     public FilterDialog( Shell parentShell, String JavaDoc title, String JavaDoc 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 JavaDoc 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.filter = sourceViewer.getDocument().get();
92
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         // call super implementation
102
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 = parent;
117
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         // create and configure source viewer
124
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         // set document
130
IDocument document = new Document( this.filter );
131         sourceViewer.setDocument( document );
132
133         // preformat
134
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         // proposal popup is opened, don't close dialog!
146
return super.canHandleShellCloseEvent();
147     }
148
149 }
150
Popular Tags