Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 173213 | Differences between
and this patch

Collapse All | Expand All

(-)Eclipse UI/org/eclipse/ui/contexts/IContextService.java (+13 lines)
Lines 71-76 Link Here
71
	 * context should also be active.
71
	 * context should also be active.
72
	 */
72
	 */
73
	public static final int TYPE_WINDOW = 2;
73
	public static final int TYPE_WINDOW = 2;
74
	
75
	/**
76
	 * The type used for registration indicating that the shell should be
77
	 * treated as a dialog. When the given shell is active, the "In Dialogs"
78
	 * context should also be active. The active workbench window should also be
79
	 * available.
80
	 * <p>
81
	 * The return value from getShellType(Shell) will be TYPE_DIALOG.
82
	 * </p>
83
	 * 
84
	 * @since 3.3
85
	 */
86
	public static final int TYPE_DIALOG_SUPPORTS_WORKBENCH = TYPE_DIALOG | 4;
74
87
75
	/**
88
	/**
76
	 * <p>
89
	 * <p>
(-)Eclipse UI/org/eclipse/ui/internal/contexts/ContextAuthority.java (-7 / +46 lines)
Lines 46-51 Link Here
46
 */
46
 */
47
final class ContextAuthority extends ExpressionAuthority {
47
final class ContextAuthority extends ExpressionAuthority {
48
48
49
	private static final String DIALOG_WITH_WORKBENCH = "org.eclipse.ui.contexts.internal.dialog_with_workbench"; //$NON-NLS-1$
50
49
	/**
51
	/**
50
	 * The default size of the set containing the activations to recompute. This
52
	 * The default size of the set containing the activations to recompute. This
51
	 * is more than enough to cover the average case.
53
	 * is more than enough to cover the average case.
Lines 177-183 Link Here
177
		for (int i = 1; i <= 32; i++) {
179
		for (int i = 1; i <= 32; i++) {
178
			if ((sourcePriority & (1 << i)) != 0) {
180
			if ((sourcePriority & (1 << i)) != 0) {
179
				Set activations = activationsBySourcePriority[i];
181
				Set activations = activationsBySourcePriority[i];
180
				if (activations == null) { 
182
				if (activations == null) {
181
					activations = new HashSet(1);
183
					activations = new HashSet(1);
182
					activationsBySourcePriority[i] = activations;
184
					activationsBySourcePriority[i] = activations;
183
				}
185
				}
Lines 378-383 Link Here
378
		return (Shell) getVariable(ISources.ACTIVE_SHELL_NAME);
380
		return (Shell) getVariable(ISources.ACTIVE_SHELL_NAME);
379
	}
381
	}
380
382
383
	public final boolean dialogSupportsWorkbench(final Shell shell) {
384
		if (shell != null) {
385
			final Collection activations = (Collection) registeredWindows
386
					.get(shell);
387
			if (activations != null) {
388
				// The shell is registered, so check what type it was registered
389
				// as.
390
				if (activations.isEmpty()) {
391
					// It was registered as none.
392
					return false;
393
				}
394
395
				// Look for the right type of context id.
396
				final Iterator activationItr = activations.iterator();
397
				while (activationItr.hasNext()) {
398
					final IContextActivation activation = (IContextActivation) activationItr
399
							.next();
400
					final String contextId = activation.getContextId();
401
					if (contextId == DIALOG_WITH_WORKBENCH) {
402
						return true;
403
					}
404
				}
405
			}
406
		}
407
		return false;
408
	}
409
381
	/**
410
	/**
382
	 * Returns the shell type for the given shell.
411
	 * Returns the shell type for the given shell.
383
	 * 
412
	 * 
Lines 418-426 Link Here
418
			}
447
			}
419
448
420
			// This shouldn't be possible.
449
			// This shouldn't be possible.
421
			Assert.isTrue(
450
			Assert
422
						false,
451
					.isTrue(
423
						"A registered shell should have at least one submission matching TYPE_WINDOW or TYPE_DIALOG"); //$NON-NLS-1$
452
							false,
453
							"A registered shell should have at least one submission matching TYPE_WINDOW or TYPE_DIALOG"); //$NON-NLS-1$
424
			return IContextService.TYPE_NONE; // not reachable
454
			return IContextService.TYPE_NONE; // not reachable
425
455
426
		} else if (shell.getParent() != null) {
456
		} else if (shell.getParent() != null) {
Lines 486-491 Link Here
486
			case IContextService.TYPE_DIALOG:
516
			case IContextService.TYPE_DIALOG:
487
				buffer.append("dialog"); //$NON-NLS-1$
517
				buffer.append("dialog"); //$NON-NLS-1$
488
				break;
518
				break;
519
			case IContextService.TYPE_DIALOG_SUPPORTS_WORKBENCH:
520
				buffer.append("dialog-workbench"); //$NON-NLS-1$
521
				break;
489
			case IContextService.TYPE_WINDOW:
522
			case IContextService.TYPE_WINDOW:
490
				buffer.append("window"); //$NON-NLS-1$
523
				buffer.append("window"); //$NON-NLS-1$
491
				break;
524
				break;
Lines 504-509 Link Here
504
		Expression expression;
537
		Expression expression;
505
		IContextActivation dialogWindowActivation;
538
		IContextActivation dialogWindowActivation;
506
		switch (type) {
539
		switch (type) {
540
		case IContextService.TYPE_DIALOG_SUPPORTS_WORKBENCH:
541
			// add the bogus activation for reference purposes
542
			activations.add(new ContextActivation(DIALOG_WITH_WORKBENCH, null,
543
					contextService));
507
		case IContextService.TYPE_DIALOG:
544
		case IContextService.TYPE_DIALOG:
508
			expression = new ActiveShellExpression(shell);
545
			expression = new ActiveShellExpression(shell);
509
			dialogWindowActivation = new ContextActivation(
546
			dialogWindowActivation = new ContextActivation(
Lines 568-574 Link Here
568
			 */
605
			 */
569
			public void widgetDisposed(DisposeEvent e) {
606
			public void widgetDisposed(DisposeEvent e) {
570
				registeredWindows.remove(shell);
607
				registeredWindows.remove(shell);
571
				shell.removeDisposeListener(this);
608
				if (!shell.isDisposed()) {
609
					shell.removeDisposeListener(this);
610
				}
572
611
573
				/*
612
				/*
574
				 * In the case where a dispose has happened, we are expecting an
613
				 * In the case where a dispose has happened, we are expecting an
Lines 605-611 Link Here
605
		if (DEBUG_PERFORMANCE) {
644
		if (DEBUG_PERFORMANCE) {
606
			startTime = System.currentTimeMillis();
645
			startTime = System.currentTimeMillis();
607
		}
646
		}
608
		
647
609
		/*
648
		/*
610
		 * In this first phase, we cycle through all of the activations that
649
		 * In this first phase, we cycle through all of the activations that
611
		 * could have potentially changed. Each such activation is added to a
650
		 * could have potentially changed. Each such activation is added to a
Lines 662-668 Link Here
662
				updateContext(contextId, false);
701
				updateContext(contextId, false);
663
			}
702
			}
664
		}
703
		}
665
		
704
666
		// If tracing performance, then print the results.
705
		// If tracing performance, then print the results.
667
		if (DEBUG_PERFORMANCE) {
706
		if (DEBUG_PERFORMANCE) {
668
			final long elapsedTime = System.currentTimeMillis() - startTime;
707
			final long elapsedTime = System.currentTimeMillis() - startTime;
(-)Eclipse UI/org/eclipse/ui/internal/contexts/ContextService.java (+4 lines)
Lines 209-214 Link Here
209
	public final int getShellType(final Shell shell) {
209
	public final int getShellType(final Shell shell) {
210
		return contextAuthority.getShellType(shell);
210
		return contextAuthority.getShellType(shell);
211
	}
211
	}
212
	
213
	public final boolean dialogSupportsWorkbench(final Shell shell) {
214
		return contextAuthority.dialogSupportsWorkbench(shell);
215
	}
212
216
213
	/*
217
	/*
214
	 * (non-Javadoc)
218
	 * (non-Javadoc)
(-)Eclipse UI/org/eclipse/ui/internal/services/ActiveShellSourceProvider.java (-2 / +8 lines)
Lines 27-32 Link Here
27
import org.eclipse.ui.contexts.IContextService;
27
import org.eclipse.ui.contexts.IContextService;
28
import org.eclipse.ui.internal.Workbench;
28
import org.eclipse.ui.internal.Workbench;
29
import org.eclipse.ui.internal.WorkbenchWindow;
29
import org.eclipse.ui.internal.WorkbenchWindow;
30
import org.eclipse.ui.internal.contexts.ContextService;
30
31
31
/**
32
/**
32
 * A provider of notifications for when the active shell changes.
33
 * A provider of notifications for when the active shell changes.
Lines 300-308 Link Here
300
		 * open.
301
		 * open.
301
		 */
302
		 */
302
		final IContextService contextService = (IContextService) workbench
303
		final IContextService contextService = (IContextService) workbench
303
				.getService(IContextService.class);		
304
				.getService(IContextService.class);
305
		boolean dialogSupportsWorkbench = false;
306
		if (contextService instanceof ContextService) {
307
			dialogSupportsWorkbench = ((ContextService) contextService)
308
					.dialogSupportsWorkbench(newActiveShell);
309
		}
304
		final int shellType = contextService.getShellType(newActiveShell);
310
		final int shellType = contextService.getShellType(newActiveShell);
305
		if (shellType != IContextService.TYPE_DIALOG) {
311
		if (shellType != IContextService.TYPE_DIALOG || dialogSupportsWorkbench) {
306
			final IWorkbenchWindow newActiveWorkbenchWindow = workbench
312
			final IWorkbenchWindow newActiveWorkbenchWindow = workbench
307
					.getActiveWorkbenchWindow();
313
					.getActiveWorkbenchWindow();
308
			final Shell newActiveWorkbenchWindowShell;
314
			final Shell newActiveWorkbenchWindowShell;
(-)Eclipse UI Tests/org/eclipse/ui/tests/commands/CommandsTestSuite.java (+1 lines)
Lines 45-49 Link Here
45
		addTest(new TestSuite(CommandCallbackTest.class));
45
		addTest(new TestSuite(CommandCallbackTest.class));
46
		addTest(new TestSuite(CommandEnablementTest.class));
46
		addTest(new TestSuite(CommandEnablementTest.class));
47
		addTest(new TestSuite(CommandActionTest.class));
47
		addTest(new TestSuite(CommandActionTest.class));
48
		addTest(new TestSuite(WindowDialogTest.class));
48
	}
49
	}
49
}
50
}
(-)Eclipse (+91 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.ui.tests.commands;
13
14
import org.eclipse.core.expressions.IEvaluationContext;
15
import org.eclipse.jface.dialogs.Dialog;
16
import org.eclipse.ui.ISources;
17
import org.eclipse.ui.IWorkbenchWindow;
18
import org.eclipse.ui.contexts.IContextService;
19
import org.eclipse.ui.handlers.IHandlerService;
20
import org.eclipse.ui.tests.harness.util.UITestCase;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class WindowDialogTest extends UITestCase {
27
28
	/**
29
	 * @param testName
30
	 */
31
	public WindowDialogTest(String testName) {
32
		super(testName);
33
	}
34
35
	public void testBasicApplicationContext() throws Exception {
36
		IWorkbenchWindow win = openTestWindow();
37
		IHandlerService handlerService = (IHandlerService) win
38
				.getService(IHandlerService.class);
39
		IEvaluationContext appContext = handlerService.getCurrentState();
40
		Object obj = appContext
41
				.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
42
		assertEquals(win, obj);
43
		Dialog d = new Dialog(win.getShell()) {
44
		};
45
		try {
46
			d.setBlockOnOpen(false);
47
			d.open();
48
			processEvents();
49
			appContext = handlerService.getCurrentState();
50
			obj = appContext.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
51
		} finally {
52
			d.close();
53
		}
54
		processEvents();
55
		assertNull(obj);
56
		appContext = handlerService.getCurrentState();
57
		obj = appContext.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
58
		assertEquals(win, obj);
59
	}
60
61
	public void testDialogWithWindowRef() throws Exception {
62
		IWorkbenchWindow win = openTestWindow();
63
		IHandlerService handlerService = (IHandlerService) win
64
				.getService(IHandlerService.class);
65
		IContextService contextService = (IContextService) win
66
				.getService(IContextService.class);
67
		IEvaluationContext appContext = handlerService.getCurrentState();
68
		Object obj = appContext
69
				.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
70
		assertEquals(win, obj);
71
		Dialog d = new Dialog(win.getShell()) {
72
		};
73
		try {
74
			d.setBlockOnOpen(false);
75
			d.create();
76
			contextService.registerShell(d.getShell(),
77
					IContextService.TYPE_DIALOG_SUPPORTS_WORKBENCH);
78
			d.open();
79
			processEvents();
80
			appContext = handlerService.getCurrentState();
81
			obj = appContext.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
82
		} finally {
83
			d.close();
84
		}
85
		processEvents();
86
		assertEquals(win, obj);
87
		appContext = handlerService.getCurrentState();
88
		obj = appContext.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
89
		assertEquals(win, obj);
90
	}
91
}

Return to bug 173213