1 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 63 public abstract class ViewFormWidget 64 { 65 66 67 protected ViewForm control; 68 69 70 protected Text infoText; 71 72 73 protected ToolBar actionToolBar; 74 75 76 protected IToolBarManager actionToolBarManager; 77 78 79 protected ToolBar menuToolBar; 80 81 82 protected MenuManager menuManager; 83 84 85 protected MenuManager contextMenuManager; 86 87 88 93 public void createWidget( Composite parent ) 94 { 95 96 control = new ViewForm( parent, SWT.NONE ); 97 control.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 102 103 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 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 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 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 this.contextMenuManager = new MenuManager(); 143 Menu menu = this.contextMenuManager.createContextMenu( childControl ); 144 childControl.setMenu( menu ); 145 } 146 147 148 155 protected abstract Control createContent( Composite control ); 156 157 158 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 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 218 public Text getInfoText() 219 { 220 return infoText; 221 } 222 223 224 229 public IToolBarManager getToolBarManager() 230 { 231 return this.actionToolBarManager; 232 } 233 234 235 240 public IMenuManager getMenuManager() 241 { 242 return menuManager; 243 } 244 245 246 251 public IMenuManager getContextMenuManager() 252 { 253 return this.contextMenuManager; 254 } 255 256 } 257 | Popular Tags |