KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > schemabrowser > ConnectionComboContributionItem


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.ui.editors.schemabrowser;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionContentProvider;
26 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionLabelProvider;
27 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
28 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateEvent;
29 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateListener;
30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.eclipse.jface.action.ContributionItem;
33 import org.eclipse.jface.util.Assert;
34 import org.eclipse.jface.viewers.ComboViewer;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.ISelectionChangedListener;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39 import org.eclipse.jface.viewers.StructuredSelection;
40 import org.eclipse.swt.SWT;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Menu;
44 import org.eclipse.swt.widgets.ToolBar;
45 import org.eclipse.swt.widgets.ToolItem;
46
47
48 /**
49  * A contribution item that adds a combo with connections to the toolbar.
50  *
51  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
52  * @version $Rev$, $Date$
53  */

54 public class ConnectionComboContributionItem extends ContributionItem implements ConnectionUpdateListener
55 {
56     /** The schema page */
57     private SchemaPage schemaPage;
58
59     /** Flag indicating if the combo's selection is changed programatically */
60     private boolean inChange;
61
62     /** The combo viewer */
63     private ComboViewer comboViewer;
64
65     /** The tool item */
66     private ToolItem toolitem;
67
68
69     /**
70      * Creates a new instance of ConnectionContributionItem.
71      *
72      * @param schemaPage the schema page
73      */

74     public ConnectionComboContributionItem( SchemaPage schemaPage )
75     {
76         this.schemaPage = schemaPage;
77         this.inChange = false;
78     }
79
80
81     /**
82      * Creates and returns the control for this contribution item
83      * under the given parent composite.
84      *
85      * @param parent the parent composite
86      * @return the new control
87      */

88     private Control createControl( Composite parent )
89     {
90         comboViewer = new ComboViewer( parent, SWT.DROP_DOWN | SWT.READ_ONLY );
91         comboViewer.setLabelProvider( new ConnectionLabelProvider() );
92         comboViewer.setContentProvider( new ConnectionContentProvider() );
93         comboViewer.setInput( BrowserCorePlugin.getDefault().getConnectionManager() );
94         comboViewer.addSelectionChangedListener( new ISelectionChangedListener()
95         {
96             public void selectionChanged( SelectionChangedEvent event )
97             {
98                 // Do not set the input of the schema browser if
99
// the selection was changed programatically.
100
if ( !inChange )
101                 {
102                     IConnection connection = getConnection();
103                     schemaPage.getSchemaBrowser().setInput( new SchemaBrowserInput( connection, null ) );
104                 }
105             }
106         } );
107
108         EventRegistry.addConnectionUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
109
110         // Initialize width of combo
111
toolitem.setWidth( comboViewer.getCombo().computeSize( SWT.DEFAULT, SWT.DEFAULT, true ).x );
112
113         return comboViewer.getCombo();
114     }
115
116
117     /**
118      * @see org.eclipse.jface.action.ContributionItem#dispose()
119      */

120     public void dispose()
121     {
122         EventRegistry.removeConnectionUpdateListener( this );
123         comboViewer = null;
124     }
125
126
127     /**
128      * The control item implementation of this <code>IContributionItem</code>
129      * method calls the <code>createControl</code> method.
130      *
131      * @param parent the parent of the control to fill
132      */

133     public final void fill( Composite parent )
134     {
135         createControl( parent );
136     }
137
138
139     /**
140      * The control item implementation of this <code>IContributionItem</code>
141      * method throws an exception since controls cannot be added to menus.
142      *
143      * @param parent the menu
144      * @param index menu index
145      */

146     public final void fill( Menu parent, int index )
147     {
148         Assert.isTrue( false, "Can't add a control to a menu" );//$NON-NLS-1$
149
}
150
151
152     /**
153      * The control item implementation of this <code>IContributionItem</code>
154      * method calls the <code>createControl</code> method to
155      * create a control under the given parent, and then creates
156      * a new tool item to hold it.
157      *
158      * @param parent the ToolBar to add the new control to
159      * @param index the index
160      */

161     public void fill( ToolBar parent, int index )
162     {
163         toolitem = new ToolItem( parent, SWT.SEPARATOR, index );
164         Control control = createControl( parent );
165         toolitem.setControl( control );
166     }
167
168
169     /**
170      * {@inheritDoc}
171      */

172     public void connectionUpdated( ConnectionUpdateEvent connectionUpdateEvent )
173     {
174         if ( comboViewer != null )
175         {
176             this.comboViewer.refresh();
177         }
178     }
179
180
181     /**
182      * Gets the connection.
183      *
184      * @return the connection
185      */

186     public IConnection getConnection()
187     {
188         ISelection selection = comboViewer.getSelection();
189         if ( !selection.isEmpty() )
190         {
191             return ( IConnection ) ( ( IStructuredSelection ) selection ).getFirstElement();
192         }
193
194         return null;
195     }
196
197
198     /**
199      * Sets the connection.
200      *
201      * @param connection the connection
202      */

203     public void setConnection( IConnection connection )
204     {
205         ISelection newSelection = new StructuredSelection( connection );
206         ISelection oldSelection = comboViewer.getSelection();
207         if ( !newSelection.equals( oldSelection ) )
208         {
209             inChange = true;
210             comboViewer.setSelection( newSelection );
211             inChange = false;
212         }
213     }
214
215
216     /**
217      * Updates the enabled state.
218      */

219     public void updateEnabledState()
220     {
221         comboViewer.getCombo().setEnabled( !schemaPage.isShowDefaultSchema() );
222     }
223
224 }
Popular Tags