1 11 12 package org.eclipse.debug.internal.ui.views.expression; 13 14 import java.text.MessageFormat ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.debug.internal.ui.DebugUIMessages; 19 import org.eclipse.debug.internal.ui.DebugUIPlugin; 20 import org.eclipse.jface.dialogs.IDialogSettings; 21 import org.eclipse.jface.text.IInformationControl; 22 import org.eclipse.jface.text.IInformationControlExtension; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.events.DisposeListener; 25 import org.eclipse.swt.events.FocusListener; 26 import org.eclipse.swt.graphics.Color; 27 import org.eclipse.swt.graphics.Point; 28 import org.eclipse.swt.graphics.Rectangle; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.Label; 35 import org.eclipse.swt.widgets.Shell; 36 import org.eclipse.ui.IWorkbench; 37 import org.eclipse.ui.PlatformUI; 38 import org.eclipse.ui.commands.AbstractHandler; 39 import org.eclipse.ui.commands.ExecutionException; 40 import org.eclipse.ui.commands.HandlerSubmission; 41 import org.eclipse.ui.commands.ICommand; 42 import org.eclipse.ui.commands.ICommandManager; 43 import org.eclipse.ui.commands.IHandler; 44 import org.eclipse.ui.commands.IKeySequenceBinding; 45 import org.eclipse.ui.commands.IWorkbenchCommandSupport; 46 import org.eclipse.ui.commands.Priority; 47 import org.eclipse.ui.contexts.IWorkbenchContextSupport; 48 49 62 public abstract class PopupInformationControl implements IInformationControl, IInformationControlExtension { 63 64 private static final String HEIGHT_STRING = "_DEBUGPOPUP_HEIGHT"; private static final String WIDTH_STRING = "_DEBUGPOPUP_WIDTH"; 67 70 protected Shell shell; 71 72 75 private int maxWidth = 300; 76 77 80 private int maxHeight = 300; 81 82 85 private HandlerSubmission submission; 86 87 90 private IHandler closeHandler = null; 91 92 96 private String commandId = null; 97 98 101 private Control control = null; 102 103 114 public PopupInformationControl(Shell parent, String labelText, String commandId) { 115 this.closeHandler = new CloseHandler(); 116 this.commandId = commandId; 117 118 shell= new Shell(parent, SWT.RESIZE); 119 Display display = shell.getDisplay(); 120 shell.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 121 shell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 122 123 GridLayout layout= new GridLayout(1, false); 124 layout.marginHeight= 0; 125 layout.marginWidth= 0; 126 shell.setLayout(layout); 127 shell.setLayoutData(new GridData(GridData.FILL_BOTH)); 128 control = createControl(shell); 129 register(); 130 131 ICommandManager commandManager= PlatformUI.getWorkbench().getCommandSupport().getCommandManager(); 132 ICommand command = null; 133 if (commandId != null) { 134 command = commandManager.getCommand(commandId); 135 } 136 137 Label separator= new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); 138 separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 139 140 Label label = new Label(shell, SWT.SHADOW_NONE | SWT.RIGHT); 141 label.setText(labelText); 142 label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 143 label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); 144 label.setEnabled(false); 145 label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL)); 146 if (command != null) { 147 List keyBindings = command.getKeySequenceBindings(); 148 if (keyBindings != null && keyBindings.size() > 0) { 149 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding)keyBindings.get(0); 150 label.setText(MessageFormat.format(DebugUIMessages.PopupInformationControl_1, new String [] {keySequenceBinding.getKeySequence().format(), labelText})); label.getParent().layout(); 152 } 153 } 154 } 155 156 159 public void addDisposeListener(DisposeListener listener) { 160 shell.addDisposeListener(listener); 161 } 162 163 166 public void addFocusListener(FocusListener listener) { 167 shell.addFocusListener(listener); 168 } 169 170 173 public Point computeSizeHint() { 174 Point persistedSize = getInitialSize(); 175 if (persistedSize != null) { 176 return persistedSize; 177 } 178 179 Point computedSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 180 if (maxWidth > 0 && computedSize.x > maxWidth) 181 computedSize.x = maxWidth; 182 if (maxHeight > 0 && computedSize.y > maxHeight) 183 computedSize.y = maxHeight; 184 return computedSize; 185 } 186 187 190 public void dispose() { 191 deregister(); 192 persistSize(); 193 shell= null; 194 } 195 196 200 private void deregister() { 201 IWorkbench workbench = PlatformUI.getWorkbench(); 202 IWorkbenchContextSupport contextSupport = workbench.getContextSupport(); 203 IWorkbenchCommandSupport commandSupport = workbench.getCommandSupport(); 204 205 commandSupport.removeHandlerSubmission(submission); 206 contextSupport.unregisterShell(shell); 207 } 208 209 213 private void register() { 214 if (closeHandler != null) { 215 IWorkbench workbench = PlatformUI.getWorkbench(); 216 217 IWorkbenchContextSupport contextSupport = workbench.getContextSupport(); 218 IWorkbenchCommandSupport commandSupport = workbench.getCommandSupport(); 219 220 submission = new HandlerSubmission(null, shell, null, commandId, closeHandler, Priority.MEDIUM); 221 commandSupport.addHandlerSubmission(submission); 222 223 contextSupport.registerShell(shell, IWorkbenchContextSupport.TYPE_DIALOG); 224 } 225 } 226 227 230 public void removeDisposeListener(DisposeListener listener) { 231 shell.removeDisposeListener(listener); 232 } 233 234 237 public void removeFocusListener(FocusListener listener) { 238 shell.removeFocusListener(listener); 239 } 240 241 244 public void setBackgroundColor(Color background) { 245 shell.setBackground(background); 246 } 247 248 251 public void setForegroundColor(Color foreground) { 252 shell.setForeground(foreground); 253 } 254 255 258 public void setLocation(Point location) { 259 Rectangle displayBounds = control.getDisplay().getClientArea(); 260 261 location.x = location.x < 0 ? displayBounds.x + 25 : location.x; 262 location.y = location.y < 0 ? displayBounds.y + 25 : location.y; 263 264 Point shellSize = shell.getSize(); 265 boolean shellSizeChanged = false; 266 if (shellSize.x + location.x > displayBounds.width) { 267 shellSize.x = displayBounds.width - location.x; 268 shellSizeChanged = true; 269 } 270 if (shellSize.y + location.y > displayBounds.height) { 271 shellSize.y = displayBounds.height - location.y; 272 shellSizeChanged = true; 273 } 274 if (shellSizeChanged) { 275 shell.setSize(shellSize); 276 } 277 278 shell.setLocation(location); 279 } 280 281 284 public void setSize(int width, int height) { 285 shell.setSize(width, height); 286 } 287 288 291 public void setSizeConstraints(int maxWidth, int maxHeight) { 292 this.maxWidth = maxWidth; 293 this.maxHeight = maxHeight; 294 } 295 296 299 public void setVisible(boolean visible) { 300 shell.setVisible(visible); 301 if (!visible) { 302 deregister(); 303 shell.dispose(); 304 } 305 } 306 307 313 protected abstract Control createControl(Composite parent); 314 315 319 protected Point getInitialSize() { 320 Point point = null; 321 try { 322 IDialogSettings settings = getDialogSettings(); 323 if (settings != null) { 324 String key = getClass().getName(); 325 326 int height = settings.getInt(key+HEIGHT_STRING); 327 int width = settings.getInt(key+WIDTH_STRING); 328 329 point = new Point(width, height); 330 } 331 } catch (NumberFormatException e) { 332 } 333 334 return point; 335 } 336 337 342 protected IDialogSettings getDialogSettings() { 343 return DebugUIPlugin.getDefault().getDialogSettings(); 344 } 345 346 350 protected void persistSize() { 351 if (shell == null) { 352 return; 353 } 354 355 IDialogSettings settings = getDialogSettings(); 356 if (settings != null) { 357 String key = getClass().getName(); 358 Point size = shell.getSize(); 359 settings.put(key+WIDTH_STRING, size.x); 360 settings.put(key+HEIGHT_STRING, size.y); 361 } 362 } 363 364 367 private class CloseHandler extends AbstractHandler { 368 371 public Object execute(Map parameter) throws ExecutionException { 372 performCommand(); 373 if (shell != null) { 374 shell.dispose(); 375 } 376 return null; 377 } 378 379 } 380 381 385 protected abstract void performCommand(); 386 387 392 protected Shell getShell() { 393 return shell; 394 } 395 398 public boolean isFocusControl() { 399 return control.isFocusControl(); 400 } 401 404 public void setFocus() { 405 control.setFocus(); 406 } 407 } 408 | Popular Tags |