KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > search > EntryWidget


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.widgets.search;
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.dialogs.SelectEntryDialog;
27 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
28 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
29 import org.apache.directory.ldapstudio.browser.common.widgets.BrowserWidget;
30 import org.apache.directory.ldapstudio.browser.common.widgets.HistoryUtils;
31 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob;
32 import org.apache.directory.ldapstudio.browser.core.model.DN;
33 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
34 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
35 import org.apache.directory.ldapstudio.browser.core.model.NameException;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.ModifyEvent;
38 import org.eclipse.swt.events.ModifyListener;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.widgets.Button;
43 import org.eclipse.swt.widgets.Combo;
44 import org.eclipse.swt.widgets.Composite;
45
46
47 /**
48  * The EntryWidget could be used to select an entry.
49  * It is composed
50  * <ul>
51  * <li>a combo to manually enter an DN or to choose one from
52  * the history
53  * <li>an up button to switch to the parent's DN
54  * <li>a browse button to open a {@link SelectEntryDialog}
55  * </ul>
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class EntryWidget extends BrowserWidget
61 {
62
63     /** The DN combo. */
64     private Combo dnCombo;
65
66     /** The up button. */
67     private Button upButton;
68
69     /** The entry browse button. */
70     private Button entryBrowseButton;
71
72     /** The connection. */
73     private IConnection connection;
74
75     /** The selected DN. */
76     private DN dn;
77
78     /** The suffix. */
79     private DN suffix;
80
81
82     /**
83      * Creates a new instance of EntryWidget.
84      */

85     public EntryWidget()
86     {
87         this.connection = null;
88         this.dn = null;
89     }
90
91
92     /**
93      * Creates a new instance of EntryWidget.
94      *
95      * @param connection the connection
96      * @param dn the initial DN
97      */

98     public EntryWidget( IConnection connection, DN dn )
99     {
100         this( connection, dn, null );
101     }
102
103
104     /**
105      * Creates a new instance of EntryWidget.
106      *
107      * @param connection the connection
108      * @param dn the initial DN
109      * @param suffix the suffix
110      */

111     public EntryWidget( IConnection connection, DN dn, DN suffix )
112     {
113         this.connection = connection;
114         this.dn = dn;
115         this.suffix = suffix;
116     }
117
118
119     /**
120      * Creates the widget.
121      *
122      * @param parent the parent
123      */

124     public void createWidget( final Composite parent )
125     {
126
127         // DN combo
128
Composite textAndUpComposite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 );
129         dnCombo = BaseWidgetUtils.createCombo( textAndUpComposite, new String JavaDoc[0], -1, 1 );
130         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
131         gd.horizontalSpan = 1;
132         gd.widthHint = 200;
133         dnCombo.setLayoutData( gd );
134
135         // DN history
136
String JavaDoc[] history = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_DN_HISTORY );
137         dnCombo.setItems( history );
138         dnCombo.addModifyListener( new ModifyListener()
139         {
140             public void modifyText( ModifyEvent e )
141             {
142                 try
143                 {
144                     dn = new DN( dnCombo.getText() );
145                 }
146                 catch ( NameException e1 )
147                 {
148                     dn = null;
149                 }
150
151                 internalSetEnabled();
152                 notifyListeners();
153             }
154         } );
155
156         // Up button
157
upButton = new Button( textAndUpComposite, SWT.PUSH );
158         upButton.setToolTipText( "Parent" );
159         upButton.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_PARENT ) );
160         upButton.setEnabled( false );
161         upButton.addSelectionListener( new SelectionAdapter()
162         {
163             public void widgetSelected( SelectionEvent e )
164             {
165                 if ( dn != null && dn.getParentDn() != null )
166                 {
167                     dn = dn.getParentDn();
168                     dnChanged();
169                     internalSetEnabled();
170                     notifyListeners();
171                 }
172             }
173         } );
174
175         // Browse button
176
entryBrowseButton = BaseWidgetUtils.createButton( parent, "Br&owse...", 1 );
177         entryBrowseButton.addSelectionListener( new SelectionAdapter()
178         {
179             public void widgetSelected( SelectionEvent e )
180             {
181                 if ( connection != null )
182                 {
183                     // get root entry
184
IEntry rootEntry = connection.getRootDSE();
185                     if( suffix != null && suffix.getRdns().length > 0 )
186                     {
187                         rootEntry = connection.getEntryFromCache( suffix );
188                         if ( rootEntry == null )
189                         {
190                             ReadEntryJob job = new ReadEntryJob( connection, suffix );
191                             RunnableContextJobAdapter.execute( job );
192                             rootEntry = job.getReadEntry();
193                         }
194                     }
195
196                     // calculate initial DN
197
DN initialDN = dn;
198                     if( suffix != null && suffix.getRdns().length > 0 )
199                     {
200                         if( initialDN != null && initialDN.getRdns().length > 0 )
201                         {
202                             initialDN = new DN( initialDN, suffix );
203                         }
204                     }
205
206                     // get initial entry
207
IEntry entry = rootEntry;
208                     if ( initialDN != null && initialDN.getRdns().length > 0 )
209                     {
210                         entry = connection.getEntryFromCache( initialDN );
211                         if ( entry == null )
212                         {
213                             ReadEntryJob job = new ReadEntryJob( connection, initialDN );
214                             RunnableContextJobAdapter.execute( job );
215                             entry = job.getReadEntry();
216                         }
217                     }
218
219
220                     // open dialog
221
SelectEntryDialog dialog = new SelectEntryDialog( parent.getShell(), "Select DN", rootEntry, entry );
222                     dialog.open();
223                     IEntry selectedEntry = dialog.getSelectedEntry();
224
225                     // get selected DN
226
if ( selectedEntry != null )
227                     {
228                         dn = selectedEntry.getDn();
229                         if( suffix != null && suffix.getRdns().length > 0 )
230                         {
231                             dn = dn.getLocalName( suffix );
232                         }
233                         dnChanged();
234                         internalSetEnabled();
235                         notifyListeners();
236                     }
237                 }
238             }
239         } );
240
241         dnChanged();
242         internalSetEnabled();
243     }
244
245
246     /**
247      * Notifies that the DN has been changed.
248      */

249     private void dnChanged()
250     {
251         if ( dnCombo != null && entryBrowseButton != null )
252         {
253             dnCombo.setText( dn != null ? dn.toString() : "" );
254         }
255     }
256
257
258     /**
259      * Sets the enabled state of the widget.
260      *
261      * @param b true to enable the widget, false to disable the widget
262      */

263     public void setEnabled( boolean b )
264     {
265         dnCombo.setEnabled( b );
266
267         if ( b )
268         {
269             this.dnChanged();
270         }
271
272         internalSetEnabled();
273     }
274
275
276     /**
277      * Internal set enabled.
278      */

279     private void internalSetEnabled()
280     {
281         upButton.setEnabled( dn != null && dn.getParentDn() != null && dnCombo.isEnabled() );
282         entryBrowseButton.setEnabled( connection != null && dnCombo.isEnabled() );
283     }
284
285
286     /**
287      * Saves dialog settings.
288      */

289     public void saveDialogSettings()
290     {
291         HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_DN_HISTORY, this.dnCombo.getText() );
292     }
293
294
295     /**
296      * Gets the suffix DN or <code>null</code> if not set.
297      *
298      * @return the suffix DN or <code>null</code> if not set
299      */

300     public DN getSuffix()
301     {
302         return suffix;
303     }
304
305
306     /**
307      * Gets the DN or <code>null</code> if the DN isn't valid.
308      *
309      * @return the DN or <code>null</code> if the DN isn't valid
310      */

311     public DN getDn()
312     {
313         return dn;
314     }
315
316
317     /**
318      * Gets the connection.
319      *
320      * @return the connection
321      */

322     public IConnection getConnection()
323     {
324         return connection;
325     }
326
327
328     /**
329      * Sets the input.
330      *
331      * @param dn the DN
332      * @param connection the connection
333      */

334     public void setInput( IConnection connection, DN dn )
335     {
336         setInput( connection, dn, null );
337     }
338
339
340     /**
341      * Sets the input.
342      *
343      * @param connection the connection
344      * @param dn the DN
345      * @param suffix the suffix
346      */

347     public void setInput( IConnection connection, DN dn, DN suffix )
348     {
349         if ( this.connection != connection || this.dn != dn || this.suffix != suffix )
350         {
351             this.connection = connection;
352             this.dn = dn;
353             this.suffix = suffix;
354             dnChanged();
355         }
356     }
357
358 }
359
Popular Tags