KickJava   Java API By Example, From Geeks To Geeks.

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


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.BrowserCommonConstants;
25 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
26 import org.apache.directory.ldapstudio.browser.common.widgets.BrowserWidget;
27 import org.apache.directory.ldapstudio.browser.common.widgets.DialogContentAssistant;
28 import org.apache.directory.ldapstudio.browser.common.widgets.HistoryUtils;
29 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
30 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.swt.events.ModifyEvent;
33 import org.eclipse.swt.events.ModifyListener;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37
38
39 /**
40  * The ReturningAttributesWidget could be used to enter a list of attribute types
41  * return by an LDPA search. It is composed of a combo with content assist
42  * and a history.
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class ReturningAttributesWidget extends BrowserWidget
48 {
49
50     /** The returning attributes combo. */
51     private Combo returningAttributesCombo;
52
53     /** The content assist processor. */
54     private ReturningAttributesContentAssistProcessor contentAssistProcessor;
55
56     /** The connection. */
57     private IConnection connection;
58
59     /** The initial returning attributes. */
60     private String JavaDoc[] initialReturningAttributes;
61
62
63     /**
64      * Creates a new instance of ReturningAttributesWidget.
65      *
66      * @param initialReturningAttributes the initial returning attributes
67      * @param connection the connection
68      */

69     public ReturningAttributesWidget( IConnection connection, String JavaDoc[] initialReturningAttributes )
70     {
71         this.connection = connection;
72         this.initialReturningAttributes = initialReturningAttributes;
73     }
74
75
76     /**
77      * Creates a new instance of ReturningAttributesWidget with no connection
78      * and no initial returning attributes.
79      *
80      */

81     public ReturningAttributesWidget()
82     {
83         this.connection = null;
84         this.initialReturningAttributes = null;
85     }
86
87
88     /**
89      * Creates the widget.
90      *
91      * @param parent the parent
92      */

93     public void createWidget( Composite parent )
94     {
95         // Combo
96
returningAttributesCombo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
97         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
98         gd.horizontalSpan = 1;
99         gd.widthHint = 200;
100         returningAttributesCombo.setLayoutData( gd );
101
102         // Content assist
103
contentAssistProcessor = new ReturningAttributesContentAssistProcessor( new String JavaDoc[0] );
104         DialogContentAssistant raca = new DialogContentAssistant();
105         raca.enableAutoInsert( true );
106         raca.enableAutoActivation( true );
107         raca.setAutoActivationDelay( 500 );
108         raca.setContentAssistProcessor( contentAssistProcessor, IDocument.DEFAULT_CONTENT_TYPE );
109         raca.install( returningAttributesCombo );
110
111         // History
112
String JavaDoc[] history = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_RETURNING_ATTRIBUTES_HISTORY );
113         for ( int i = 0; i < history.length; i++ )
114         {
115             history[i] = Utils.arrayToString( Utils.stringToArray( history[i] ) );
116         }
117         returningAttributesCombo.setItems( history );
118         returningAttributesCombo.setText( Utils.arrayToString( this.initialReturningAttributes ) );
119
120         returningAttributesCombo.addModifyListener( new ModifyListener()
121         {
122             public void modifyText( ModifyEvent e )
123             {
124                 notifyListeners();
125             }
126         } );
127
128         setConnection( connection );
129     }
130
131
132     /**
133      * Sets the connection.
134      *
135      * @param connection the connection
136      */

137     public void setConnection( IConnection connection )
138     {
139         this.connection = connection;
140         contentAssistProcessor.setPossibleAttributeTypes( connection == null ? new String JavaDoc[0] : connection.getSchema()
141             .getAttributeTypeDescriptionNames() );
142     }
143
144
145     /**
146      * Sets the initial returning attributes.
147      *
148      * @param initialReturningAttributes the initial returning attributes
149      */

150     public void setInitialReturningAttributes( String JavaDoc[] initialReturningAttributes )
151     {
152         this.initialReturningAttributes = initialReturningAttributes;
153         returningAttributesCombo.setText( Utils.arrayToString( initialReturningAttributes ) );
154     }
155
156
157     /**
158      * Sets the enabled state of the widget.
159      *
160      * @param b true to enable the widget, false to disable the widget
161      */

162     public void setEnabled( boolean b )
163     {
164         this.returningAttributesCombo.setEnabled( b );
165     }
166
167
168     /**
169      * Gets the returning attributes.
170      *
171      * @return the returning attributes
172      */

173     public String JavaDoc[] getReturningAttributes()
174     {
175         String JavaDoc s = this.returningAttributesCombo.getText();
176         return Utils.stringToArray( s );
177     }
178
179
180     /**
181      * Saves dialog settings.
182      */

183     public void saveDialogSettings()
184     {
185         HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_RETURNING_ATTRIBUTES_HISTORY, Utils
186             .arrayToString( getReturningAttributes() ) );
187     }
188
189
190     /**
191      * Sets the focus.
192      */

193     public void setFocus()
194     {
195         returningAttributesCombo.setFocus();
196
197     }
198
199 }
200
Popular Tags