KickJava   Java API By Example, From Geeks To Geeks.

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


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;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.MenuManager;
29 import org.eclipse.jface.action.ToolBarManager;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.custom.ViewForm;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.swt.widgets.ToolBar;
42 import org.eclipse.swt.widgets.ToolItem;
43
44
45 /**
46  * The ViewFormWidget is a widget that provides an info text,
47  * a tool bar, a menu and a main content composite including
48  * a context menu.
49  * It looks like this:
50  * <pre>
51  * -----------------------------------
52  * | info text | tool bar | menu |
53  * -----------------------------------
54  * | |
55  * | main content |
56  * | |
57  * -----------------------------------
58  * </pre>
59  *
60  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
61  * @version $Rev$, $Date$
62  */

63 public abstract class ViewFormWidget
64 {
65
66     /** The view form control */
67     protected ViewForm control;
68
69     /** The info text, positioned at the top left */
70     protected Text infoText;
71
72     /** The action tool bar */
73     protected ToolBar actionToolBar;
74
75     /** The action tool bar manager */
76     protected IToolBarManager actionToolBarManager;
77
78     /** The menu tool bar. */
79     protected ToolBar menuToolBar;
80
81     /** The menu manager. */
82     protected MenuManager menuManager;
83
84     /** The context menu manager. */
85     protected MenuManager contextMenuManager;
86
87
88     /**
89      * Creates the widget.
90      *
91      * @param parent the parent composite
92      */

93     public void createWidget( Composite parent )
94     {
95
96         control = new ViewForm( parent, SWT.NONE );
97         // control.marginWidth = 0;
98
// control.marginHeight = 0;
99
// control.horizontalSpacing = 0;
100
// control.verticalSpacing = 0;
101
control.setLayoutData( new GridData( GridData.FILL_BOTH ) );
102
103         // infoText = BaseWidgetUtils.createLabeledText(control, "", 1);
104
Composite infoTextControl = BaseWidgetUtils.createColumnContainer( control, 1, 1 );
105         infoTextControl.setLayoutData( new GridData( GridData.FILL_BOTH ) );
106         infoText = BaseWidgetUtils.createLabeledText( infoTextControl, "", 1 );
107         infoText.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, true ) );
108         control.setTopLeft( infoTextControl );
109
110         // tool bar
111
actionToolBar = new ToolBar( control, SWT.FLAT | SWT.RIGHT );
112         actionToolBar.setLayoutData( new GridData( SWT.END, SWT.NONE, true, false ) );
113         actionToolBarManager = new ToolBarManager( actionToolBar );
114         control.setTopCenter( actionToolBar );
115
116         // local menu
117
this.menuManager = new MenuManager();
118         menuToolBar = new ToolBar( control, SWT.FLAT | SWT.RIGHT );
119         ToolItem ti = new ToolItem( menuToolBar, SWT.PUSH, 0 );
120         ti.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_PULLDOWN ) );
121         ti.addSelectionListener( new SelectionAdapter()
122         {
123             public void widgetSelected( SelectionEvent e )
124             {
125                 showViewMenu();
126             }
127         } );
128         control.setTopRight( menuToolBar );
129
130         // content
131
Composite composite = BaseWidgetUtils.createColumnContainer( control, 1, 1 );
132         GridLayout gl = new GridLayout();
133         gl.horizontalSpacing = 0;
134         gl.verticalSpacing = 0;
135         gl.marginHeight = 0;
136         gl.marginWidth = 0;
137         composite.setLayout( gl );
138         Control childControl = this.createContent( composite );
139         control.setContent( composite );
140
141         // context menu
142
this.contextMenuManager = new MenuManager();
143         Menu menu = this.contextMenuManager.createContextMenu( childControl );
144         childControl.setMenu( menu );
145     }
146
147
148     /**
149      * Creates the content.
150      *
151      * @param control the control
152      *
153      * @return the control
154      */

155     protected abstract Control createContent( Composite control );
156
157
158     /**
159      * Shows the local view menu.
160      */

161     private void showViewMenu()
162     {
163         Menu aMenu = menuManager.createContextMenu( control );
164         Point topLeft = new Point( 0, 0 );
165         topLeft.y += menuToolBar.getBounds().height;
166         topLeft = menuToolBar.toDisplay( topLeft );
167         aMenu.setLocation( topLeft.x, topLeft.y );
168         aMenu.setVisible( true );
169     }
170
171
172     /**
173      * Disposes this widget.
174      */

175     public void dispose()
176     {
177         if ( control != null )
178         {
179
180             if ( contextMenuManager != null )
181             {
182                 contextMenuManager.removeAll();
183                 contextMenuManager.dispose();
184                 contextMenuManager = null;
185             }
186             if ( menuToolBar != null )
187             {
188                 menuToolBar.dispose();
189                 menuToolBar = null;
190                 menuManager.dispose();
191                 menuManager = null;
192             }
193             if ( actionToolBar != null )
194             {
195                 actionToolBar.dispose();
196                 actionToolBar = null;
197                 actionToolBarManager.removeAll();
198                 actionToolBarManager = null;
199             }
200
201             if ( infoText != null )
202             {
203                 infoText.dispose();
204                 infoText = null;
205             }
206
207             control.dispose();
208             control = null;
209         }
210     }
211
212
213     /**
214      * Gets the info text.
215      *
216      * @return the info text
217      */

218     public Text getInfoText()
219     {
220         return infoText;
221     }
222
223
224     /**
225      * Gets the tool bar manager.
226      *
227      * @return the tool bar manager
228      */

229     public IToolBarManager getToolBarManager()
230     {
231         return this.actionToolBarManager;
232     }
233
234
235     /**
236      * Gets the menu manager.
237      *
238      * @return the menu manager
239      */

240     public IMenuManager getMenuManager()
241     {
242         return menuManager;
243     }
244
245
246     /**
247      * Gets the context menu manager.
248      *
249      * @return the context menu manager
250      */

251     public IMenuManager getContextMenuManager()
252     {
253         return this.contextMenuManager;
254     }
255
256 }
257
Popular Tags