KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > views > browser > BrowserView


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.views.browser;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserConfiguration;
28 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserWidget;
29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
30 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
31 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
32 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
34 import org.apache.directory.ldapstudio.browser.core.model.IValue;
35 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
36 import org.eclipse.jface.viewers.StructuredSelection;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.layout.GridLayout;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.part.IShowInTarget;
43 import org.eclipse.ui.part.ShowInContext;
44 import org.eclipse.ui.part.ViewPart;
45
46
47 /**
48  * This class implements the browser view. It displays the DIT, the searches and the bookmarks.
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class BrowserView extends ViewPart
54 {
55
56     /** The configuration */
57     private BrowserConfiguration configuration;
58
59     /** The listeners */
60     private BrowserViewUniversalListener universalListener;
61
62     /** The actions */
63     private BrowserViewActionGroup actionGroup;
64
65     /** The browser's main widget */
66     private BrowserWidget mainWidget;
67
68
69     // private DragAction dragAction;
70
// private DropAction dropAction;
71

72     /**
73      * Returns the browser view ID.
74      *
75      * @return the browser view ID.
76      */

77     public static String JavaDoc getId()
78     {
79         return BrowserView.class.getName();
80     }
81
82
83     /**
84      * Creates a new instance of BrowserView.
85      */

86     public BrowserView()
87     {
88     }
89
90
91     /**
92      * {@inheritDoc}
93      *
94      * This implementation sets focus to the viewer's control.
95      */

96     public void setFocus()
97     {
98         mainWidget.getViewer().getControl().setFocus();
99     }
100
101
102     /**
103      * {@inheritDoc}
104      */

105     public void dispose()
106     {
107         if ( configuration != null )
108         {
109             actionGroup.dispose();
110             actionGroup = null;
111             universalListener.dispose();
112             universalListener = null;
113             configuration.dispose();
114             configuration = null;
115             mainWidget.dispose();
116             mainWidget = null;
117             getSite().setSelectionProvider( null );
118         }
119
120         super.dispose();
121     }
122
123
124     /**
125      * {@inheritDoc}
126      */

127     public void createPartControl( Composite parent )
128     {
129
130         Composite composite = new Composite( parent, SWT.NONE );
131         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
132         GridLayout layout = new GridLayout();
133         layout.marginWidth = 0;
134         layout.marginHeight = 0;
135         composite.setLayout( layout );
136
137         PlatformUI.getWorkbench().getHelpSystem().setHelp( composite,
138             BrowserUIPlugin.PLUGIN_ID + "." + "tools_browser_view" );
139
140         // create configuration
141
configuration = new BrowserConfiguration();
142
143         // create main widget
144
mainWidget = new BrowserWidget( configuration, getViewSite().getActionBars() );
145         mainWidget.createWidget( composite );
146         mainWidget.setInput( getSite() );
147
148         // create actions and context menu (and register global actions)
149
actionGroup = new BrowserViewActionGroup( this );
150         actionGroup.fillToolBar( mainWidget.getToolBarManager() );
151         actionGroup.fillMenu( mainWidget.getMenuManager() );
152         actionGroup.enableGlobalActionHandlers( getViewSite().getActionBars() );
153         actionGroup.fillContextMenu( mainWidget.getContextMenuManager() );
154
155         // create the listener
156
getSite().setSelectionProvider( mainWidget.getViewer() );
157         universalListener = new BrowserViewUniversalListener( this );
158
159         // DND support
160
// int ops = DND.DROP_COPY | DND.DROP_MOVE;
161
// viewer.addDragSupport(ops, new Transfer[]{TextTransfer.getInstance(),
162
// BrowserTransfer.getInstance()}, this.dragAction);
163
// viewer.addDropSupport(ops, new
164
// Transfer[]{BrowserTransfer.getInstance()}, this.dropAction);
165
}
166
167
168     /**
169      * Selects the given object in the tree. The object
170      * must be an IEntry, ISearch, ISearchResult or IBookmark.
171      *
172      * @param obj the object to select
173      */

174     public void select( Object JavaDoc obj )
175     {
176         Object JavaDoc objectToSelect = null;
177
178         if ( obj instanceof ISearch )
179         {
180             ISearch search = ( ISearch ) obj;
181
182             universalListener.setInput( search.getConnection() );
183
184             mainWidget.getViewer().expandToLevel( search, 0 );
185
186             objectToSelect = search;
187         }
188         if ( obj instanceof ISearchResult )
189         {
190             ISearchResult searchResult = ( ISearchResult ) obj;
191             ISearch search = searchResult.getSearch();
192
193             universalListener.setInput( search.getConnection() );
194
195             mainWidget.getViewer().expandToLevel( search, 1 );
196
197             objectToSelect = searchResult;
198         }
199         if ( obj instanceof IBookmark )
200         {
201             IBookmark bookmark = ( IBookmark ) obj;
202
203             universalListener.setInput( bookmark.getConnection() );
204
205             mainWidget.getViewer().expandToLevel( bookmark, 0 );
206
207             objectToSelect = bookmark;
208         }
209         if ( obj instanceof IEntry )
210         {
211             IEntry entry = ( IEntry ) obj;
212
213             universalListener.setInput( entry.getConnection() );
214
215             List JavaDoc<IEntry> entryList = new ArrayList JavaDoc<IEntry>();
216             IEntry tempEntry = entry;
217             while ( tempEntry.getParententry() != null )
218             {
219                 IEntry parentEntry = tempEntry.getParententry();
220                 entryList.add( parentEntry );
221                 tempEntry = parentEntry;
222             }
223
224             IEntry[] parentEntries = ( IEntry[] ) entryList.toArray( new IEntry[0] );
225             for ( int i = parentEntries.length - 1; i >= 0; i-- )
226             {
227
228                 if ( !parentEntries[i].isChildrenInitialized() )
229                 {
230                     parentEntries[i].setChildrenInitialized( true );
231                     parentEntries[i].setHasMoreChildren( true );
232                 }
233             }
234
235             objectToSelect = entry;
236         }
237
238         if ( objectToSelect != null )
239         {
240             mainWidget.getViewer().reveal( objectToSelect );
241             mainWidget.getViewer().refresh( objectToSelect, true );
242             mainWidget.getViewer().setSelection( new StructuredSelection( objectToSelect ), true );
243
244         }
245     }
246
247
248     /**
249      * {@inheritDoc}
250      */

251     public Object JavaDoc getAdapter( Class JavaDoc required )
252     {
253         if ( IShowInTarget.class.equals( required ) )
254         {
255             return new IShowInTarget()
256             {
257                 public boolean show( ShowInContext context )
258                 {
259                     StructuredSelection selection = ( StructuredSelection ) context.getSelection();
260                     Object JavaDoc obj = selection.getFirstElement();
261                     if ( obj instanceof IValue )
262                     {
263                         IValue value = ( IValue ) obj;
264                         IEntry entry = value.getAttribute().getEntry();
265                         select( entry );
266                     }
267                     else if ( obj instanceof IAttribute )
268                     {
269                         IAttribute attribute = ( IAttribute ) obj;
270                         IEntry entry = attribute.getEntry();
271                         select( entry );
272
273                     }
274                     else if ( obj instanceof ISearchResult )
275                     {
276                         ISearchResult sr = ( ISearchResult ) obj;
277                         ISearch search = sr.getSearch();
278                         select( search );
279                     }
280                     return true;
281                 }
282             };
283         }
284
285         return null;
286     }
287
288
289     /**
290      * Gets the action group.
291      *
292      * @return the action group
293      */

294     public BrowserViewActionGroup getActionGroup()
295     {
296         return actionGroup;
297     }
298
299
300     /**
301      * Gets the configuration.
302      *
303      * @return the configuration
304      */

305     public BrowserConfiguration getConfiguration()
306     {
307         return configuration;
308     }
309
310
311     /**
312      * Gets the main widget.
313      *
314      * @return the main widget
315      */

316     public BrowserWidget getMainWidget()
317     {
318         return mainWidget;
319     }
320
321
322     /**
323      * Gets the universal listener.
324      *
325      * @return the universal listener
326      */

327     public BrowserViewUniversalListener getUniversalListener()
328     {
329         return universalListener;
330     }
331
332 }
333
Popular Tags