1 11 package org.eclipse.jface.dialogs; 12 13 import org.eclipse.jface.resource.JFaceResources; 14 import org.eclipse.jface.window.IShellProvider; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.DisposeEvent; 17 import org.eclipse.swt.events.DisposeListener; 18 import org.eclipse.swt.events.SelectionAdapter; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.graphics.Cursor; 21 import org.eclipse.swt.graphics.Image; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Event; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Layout; 30 import org.eclipse.swt.widgets.Link; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Sash; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.swt.widgets.ToolBar; 35 import org.eclipse.swt.widgets.ToolItem; 36 37 55 public abstract class TrayDialog extends Dialog { 56 57 private static boolean dialogHelpAvailable; 58 59 62 private DialogTray tray; 63 64 67 private Control trayControl; 68 69 72 private Label leftSeparator; 73 74 77 private Label rightSeparator; 78 79 82 private Sash sash; 83 84 87 private boolean helpAvailable = isDialogHelpAvailable(); 88 89 95 protected TrayDialog(Shell shell) { 96 super(shell); 97 } 98 99 104 protected TrayDialog(IShellProvider parentShell) { 105 super(parentShell); 106 } 107 108 113 public void closeTray() throws IllegalStateException { 114 if (getTray() == null) { 115 throw new IllegalStateException ("Tray was not open"); } 117 int trayWidth = trayControl.getSize().x + leftSeparator.getSize().x + sash.getSize().x + rightSeparator.getSize().x; 118 trayControl.dispose(); 119 trayControl = null; 120 tray = null; 121 leftSeparator.dispose(); 122 leftSeparator = null; 123 rightSeparator.dispose(); 124 rightSeparator = null; 125 sash.dispose(); 126 sash = null; 127 Shell shell = getShell(); 128 Rectangle bounds = shell.getBounds(); 129 shell.setBounds(bounds.x + ((getDefaultOrientation() == SWT.RIGHT_TO_LEFT) ? trayWidth : 0), bounds.y, bounds.width - trayWidth, bounds.height); 130 } 131 132 135 public boolean close() { 136 140 if (getTray() != null) { 141 closeTray(); 142 } 143 return super.close(); 144 } 145 146 149 protected Control createButtonBar(Composite parent) { 150 Composite composite = new Composite(parent, SWT.NONE); 151 GridLayout layout = new GridLayout(); 152 layout.marginWidth = 0; 153 layout.marginHeight = 0; 154 layout.horizontalSpacing = 0; 155 composite.setLayout(layout); 156 composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); 157 composite.setFont(parent.getFont()); 158 159 if (isHelpAvailable()) { 161 Control helpControl = createHelpControl(composite); 162 ((GridData) helpControl.getLayoutData()).horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 163 } 164 Control buttonSection = super.createButtonBar(composite); 165 ((GridData) buttonSection.getLayoutData()).grabExcessHorizontalSpace = true; 166 return composite; 167 } 168 169 182 protected Control createHelpControl(Composite parent) { 183 Image helpImage = JFaceResources.getImage(DLG_IMG_HELP); 184 if (helpImage != null) { 185 return createHelpImageButton(parent, helpImage); 186 } 187 return createHelpLink(parent); 188 } 189 190 194 private ToolBar createHelpImageButton(Composite parent, Image image) { 195 ToolBar toolBar = new ToolBar(parent, SWT.FLAT | SWT.NO_FOCUS); 196 ((GridLayout) parent.getLayout()).numColumns++; 197 toolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); 198 final Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND); 199 toolBar.setCursor(cursor); 200 toolBar.addDisposeListener(new DisposeListener() { 201 public void widgetDisposed(DisposeEvent e) { 202 cursor.dispose(); 203 } 204 }); 205 ToolItem item = new ToolItem(toolBar, SWT.NONE); 206 item.setImage(image); 207 item.setToolTipText(JFaceResources.getString("helpToolTip")); item.addSelectionListener(new SelectionAdapter() { 209 public void widgetSelected(SelectionEvent e) { 210 helpPressed(); 211 } 212 }); 213 return toolBar; 214 } 215 216 220 private Link createHelpLink(Composite parent) { 221 Link link = new Link(parent, SWT.WRAP | SWT.NO_FOCUS); 222 ((GridLayout) parent.getLayout()).numColumns++; 223 link.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); 224 link.setText("<a>"+IDialogConstants.HELP_LABEL+"</a>"); link.setToolTipText(IDialogConstants.HELP_LABEL); 226 link.addSelectionListener(new SelectionAdapter() { 227 public void widgetSelected(SelectionEvent e) { 228 helpPressed(); 229 } 230 }); 231 return link; 232 } 233 234 237 private boolean isCompatibleLayout(Layout layout) { 238 if (layout != null && layout instanceof GridLayout) { 239 GridLayout grid = (GridLayout)layout; 240 return !grid.makeColumnsEqualWidth && (grid.horizontalSpacing == 0) && 241 (grid.marginWidth == 0) && (grid.marginHeight == 0) && 242 (grid.horizontalSpacing == 0) && (grid.numColumns == 5); 243 } 244 return false; 245 } 246 247 254 public boolean isHelpAvailable() { 255 return helpAvailable; 256 } 257 258 271 protected Layout getLayout() { 272 GridLayout layout = (GridLayout)super.getLayout(); 273 layout.numColumns = 5; 274 layout.horizontalSpacing = 0; 275 return layout; 276 } 277 278 284 public DialogTray getTray() { 285 return tray; 286 } 287 288 294 private void helpPressed() { 295 if (getShell() != null) { 296 Control c = getShell().getDisplay().getFocusControl(); 297 while (c != null) { 298 if (c.isListening(SWT.Help)) { 299 c.notifyListeners(SWT.Help, new Event()); 300 break; 301 } 302 c = c.getParent(); 303 } 304 } 305 } 306 307 316 public void openTray(DialogTray tray) throws IllegalStateException , UnsupportedOperationException { 317 if (tray == null) { 318 throw new NullPointerException ("Tray was null"); } 320 if (getTray() != null) { 321 throw new IllegalStateException ("Tray was already open"); } 323 if (!isCompatibleLayout(getShell().getLayout())) { 324 throw new UnsupportedOperationException ("Trays not supported with custom layouts"); } 326 final Shell shell = getShell(); 327 leftSeparator = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); 328 leftSeparator.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 329 sash = new Sash(shell, SWT.VERTICAL); 330 sash.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 331 rightSeparator = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL); 332 rightSeparator.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 333 trayControl = tray.createContents(shell); 334 Rectangle clientArea = shell.getClientArea(); 335 final GridData data = new GridData(GridData.FILL_VERTICAL); 336 data.widthHint = trayControl.computeSize(SWT.DEFAULT, clientArea.height).x; 337 trayControl.setLayoutData(data); 338 int trayWidth = leftSeparator.computeSize(SWT.DEFAULT, clientArea.height).x + sash.computeSize(SWT.DEFAULT, clientArea.height).x + rightSeparator.computeSize(SWT.DEFAULT, clientArea.height).x + data.widthHint; 339 Rectangle bounds = shell.getBounds(); 340 shell.setBounds(bounds.x - ((getDefaultOrientation() == SWT.RIGHT_TO_LEFT) ? trayWidth : 0), bounds.y, bounds.width + trayWidth, bounds.height); 341 sash.addListener(SWT.Selection, new Listener() { 342 public void handleEvent(Event event) { 343 if (event.detail != SWT.DRAG) { 344 Rectangle clientArea = shell.getClientArea(); 345 int newWidth = clientArea.width - event.x - (sash.getSize().x + rightSeparator.getSize().x); 346 if (newWidth != data.widthHint) { 347 data.widthHint = newWidth; 348 shell.layout(); 349 } 350 } 351 } 352 }); 353 this.tray = tray; 354 } 355 356 363 public void setHelpAvailable(boolean helpAvailable) { 364 this.helpAvailable = helpAvailable; 365 } 366 367 376 public static boolean isDialogHelpAvailable() { 377 return dialogHelpAvailable; 378 } 379 380 389 public static void setDialogHelpAvailable(boolean helpAvailable) { 390 dialogHelpAvailable = helpAvailable; 391 } 392 } 393 | Popular Tags |