Skip to content

Package: MasterDetailFocusAdapter

MasterDetailFocusAdapter

nameinstructionbranchcomplexitylinemethod
MasterDetailFocusAdapter(IMasterDetailSelectionProvider, Supplier)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
focusGained(FocusEvent)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
focusLost(FocusEvent)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isAncestorFocus(Control)
M: 0 C: 23
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$0()
M: 0 C: 19
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.swt.selection;
15:
16: import java.util.function.Supplier;
17:
18: import org.eclipse.swt.events.FocusEvent;
19: import org.eclipse.swt.events.FocusListener;
20: import org.eclipse.swt.widgets.Control;
21:
22: /**
23: * A focus listener that switches the master and detail inputs of
24: * a master-detail selection provider.
25: *
26: * @since 1.21
27: */
28: public final class MasterDetailFocusAdapter implements FocusListener {
29:
30:         private final IMasterDetailSelectionProvider selectionProvider;
31:         private final Supplier<? extends Control> detailSupplier;
32:
33:         /**
34:          * Initializes me with the selection provider and a supplier to query for what
35:          * is the detail control to check for focus.
36:          *
37:          * @param selectionProvider the selection provider
38:          * @param detailSupplier the detail control supplier
39:          */
40:         public MasterDetailFocusAdapter(IMasterDetailSelectionProvider selectionProvider,
41:                 Supplier<? extends Control> detailSupplier) {
42:
43:                 super();
44:
45:                 this.selectionProvider = selectionProvider;
46:                 this.detailSupplier = detailSupplier;
47:         }
48:
49:         @Override
50:         public void focusGained(FocusEvent e) {
51:                 selectionProvider.setDetailSelectionProvider(null);
52:         }
53:
54:         @Override
55:         public void focusLost(FocusEvent e) {
56:                 // We cannot yet know which control will gain focus
57:                 e.display.asyncExec(() -> {
58:                         // Does the detail now have focus?
59:                         final Control detail = detailSupplier.get();
60:•                        if (detail != null && !detail.isDisposed() && isAncestorFocus(detail)) {
61:
62:                                 // TODO: Some kind of API for getting a selection provider for the detail control
63:                                 selectionProvider.setDetailSelectionProvider(NullSelectionProvider.INSTANCE);
64:                         }
65:                 });
66:         }
67:
68:         private boolean isAncestorFocus(Control control) {
69:                 boolean result = false;
70:
71:•                for (Control focus = control.getDisplay().getFocusControl(); !result
72:•                        && focus != null; focus = focus.getParent()) {
73:
74:•                        result = focus == control;
75:                 }
76:
77:                 return result;
78:         }
79:
80: }