KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > valueeditors > ExclusionDialog


1 package org.apache.directory.ldapstudio.aciitemeditor.valueeditors;
2
3
4 import java.util.regex.Matcher JavaDoc;
5 import java.util.regex.Pattern JavaDoc;
6
7 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
8 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
9 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent;
10 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener;
11 import org.apache.directory.ldapstudio.browser.common.widgets.search.EntryWidget;
12 import org.apache.directory.ldapstudio.browser.core.model.DN;
13 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
14 import org.apache.directory.ldapstudio.browser.core.model.NameException;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.ComboViewer;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Shell;
28
29
30 /**
31  * This class provides a dialog to enter the Exclusion values.
32  *
33  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
34  * @version $Rev$, $Date$
35  */

36 class ExclusionDialog extends Dialog
37 {
38
39     /** The connection. */
40     private IConnection connection;
41
42     /** The base. */
43     private DN base;
44
45     /** The initial typ. */
46     private String JavaDoc initialType;
47
48     /** The inital DN */
49     private String JavaDoc initalDN;
50
51     /** The return type */
52     private String JavaDoc returnType;
53
54     /** The return DN */
55     private String JavaDoc returnDN;
56
57     private static final String JavaDoc EMPTY = ""; //$NON-NLS-1$
58
private static final String JavaDoc CHOP_BEFORE = "chopBefore"; //$NON-NLS-1$
59
private static final String JavaDoc CHOP_AFTER = "chopAfter"; //$NON-NLS-1$
60

61     // UI Fields
62
private Combo typeCombo;
63     private ComboViewer typeComboViewer;
64     private EntryWidget entryWidget;
65
66
67     /**
68      * Creates a new instance of ExclusionDialog.
69      *
70      * @param parentShell the parent shell
71      * @param connection the connection
72      * @param base the base DN
73      * @param exclusion the exclusion string
74      */

75     protected ExclusionDialog( Shell parentShell, IConnection connection, DN base, String JavaDoc exclusion )
76     {
77         super( parentShell );
78         this.connection = connection;
79         this.base = base;
80
81         try
82         {
83             // for example: chopAfter: "ou=A"
84
Pattern JavaDoc pattern = Pattern.compile( "\\s*(chopBefore|chopAfter):\\s*\"(.*)\"\\s*" ); //$NON-NLS-1$
85
Matcher JavaDoc matcher = pattern.matcher( exclusion );
86             initialType = matcher.matches() ? matcher.group( 1 ) : EMPTY;
87             initalDN = matcher.matches() ? matcher.group( 2 ) : EMPTY;
88         }
89         catch ( Exception JavaDoc e )
90         {
91             initialType = EMPTY;
92             initalDN = EMPTY;
93         }
94     }
95
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
99      */

100     protected void configureShell( Shell shell )
101     {
102         super.configureShell( shell );
103         shell.setText( Messages.getString( "ExclusionValueEditor.title" ) ); //$NON-NLS-1$
104
shell.setImage( Activator.getDefault().getImage( Messages.getString( "ExclusionValueEditor.icon" ) ) ); //$NON-NLS-1$
105
}
106
107
108     /* (non-Javadoc)
109      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
110      */

111     protected void okPressed()
112     {
113         returnType = typeCombo.getText();
114         returnDN = entryWidget.getDn().toString();
115
116         // save dn history
117
entryWidget.saveDialogSettings();
118
119         super.okPressed();
120     }
121
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
125      */

126     protected Control createDialogArea( Composite parent )
127     {
128         Composite composite = ( Composite ) super.createDialogArea( parent );
129         GridData gd = new GridData( GridData.FILL_BOTH );
130         gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
131         composite.setLayoutData( gd );
132         composite.setLayout( new GridLayout( 3, false ) );
133
134         BaseWidgetUtils.createLabel( composite, Messages.getString( "ExclusionValueEditor.label.type" ), 1 ); //$NON-NLS-1$
135
typeCombo = new Combo( composite, SWT.READ_ONLY );
136         String JavaDoc[] types = new String JavaDoc[2];
137         types[0] = CHOP_BEFORE;
138         types[1] = CHOP_AFTER;
139         typeComboViewer = new ComboViewer( typeCombo );
140         typeComboViewer.setContentProvider( new ArrayContentProvider() );
141         typeComboViewer.setLabelProvider( new LabelProvider() );
142         typeComboViewer.setInput( types );
143         typeComboViewer.setSelection( new StructuredSelection( CHOP_BEFORE ), true );
144         typeComboViewer.setSelection( new StructuredSelection( initialType ), true );
145         GridData gridData = new GridData();
146         gridData.horizontalSpan = 2;
147         gridData.grabExcessHorizontalSpace = true;
148         gridData.verticalAlignment = GridData.CENTER;
149         gridData.horizontalAlignment = GridData.BEGINNING;
150         typeCombo.setLayoutData( gridData );
151
152         BaseWidgetUtils.createLabel( composite, Messages.getString( "ExclusionValueEditor.label.rdn" ), 1 ); //$NON-NLS-1$
153
entryWidget = new EntryWidget( connection, null, base );
154         entryWidget.createWidget( composite );
155         try
156         {
157             DN dn = new DN( initalDN );
158             entryWidget.setInput( connection, dn, base );
159         }
160         catch ( NameException e )
161         {
162         }
163         entryWidget.addWidgetModifyListener( new WidgetModifyListener()
164         {
165             public void widgetModified( WidgetModifyEvent event )
166             {
167                 validate();
168             }
169         } );
170
171         validate();
172
173         return composite;
174     }
175
176
177     /**
178      * Validates if the dn is valid.
179      */

180     private void validate()
181     {
182         boolean valid = entryWidget.getDn() != null && entryWidget.getDn().getRdns().length > 0;
183
184         if ( getButton( IDialogConstants.OK_ID ) != null )
185         {
186             getButton( IDialogConstants.OK_ID ).setEnabled( valid );
187         }
188     }
189
190
191     /**
192      * Get the type.
193      *
194      * @return
195      * the type, null if canceled
196      */

197     public String JavaDoc getType()
198     {
199         return returnType;
200     }
201
202
203     /**
204      * Gets the DN.
205      *
206      * @return
207      * the DN, null if canceled
208      */

209     public String JavaDoc getDN()
210     {
211         return returnDN;
212     }
213 }
Popular Tags