Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / ApplicationActiveListener.java
blobcb674be07239347284a401e37809bfa4d9669441
1 /*******************************************************************************
2 * Copyright (c) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
11 * Contributors:
12 * Thomas Wolf - factored out of Activator
13 *******************************************************************************/
14 package org.eclipse.egit.ui.internal;
16 import org.eclipse.e4.core.services.events.IEventBroker;
17 import org.eclipse.e4.ui.workbench.UIEvents;
18 import org.eclipse.swt.SWTException;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.ui.IWindowListener;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PlatformUI;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.Deactivate;
25 import org.osgi.service.event.Event;
26 import org.osgi.service.event.EventConstants;
27 import org.osgi.service.event.EventHandler;
29 /**
30 * Determines whether any shell of the application is active and fires an event
31 * when the value changes.
33 @Component(property = {
34 EventConstants.EVENT_TOPIC + '='
35 + UIEvents.UILifeCycle.APP_STARTUP_COMPLETE,
36 EventConstants.EVENT_TOPIC + '='
37 + UIEvents.UILifeCycle.APP_SHUTDOWN_STARTED })
38 public class ApplicationActiveListener implements EventHandler {
40 /**
41 * Event topic for the events posted by this component.
43 public static final String TOPIC_APPLICATION_ACTIVE = "org/eclipse/egit/ui/APPLICATION_ACTIVE"; //$NON-NLS-1$
45 private volatile WindowTracker listener;
47 @Override
48 public void handleEvent(Event event) {
49 String topic = event.getTopic();
50 if (topic == null) {
51 return;
53 switch (topic) {
54 case UIEvents.UILifeCycle.APP_STARTUP_COMPLETE:
55 if (listener == null) {
56 listener = new WindowTracker();
57 listener.update();
58 PlatformUI.getWorkbench().addWindowListener(listener);
60 break;
61 case UIEvents.UILifeCycle.APP_SHUTDOWN_STARTED:
62 shutDown();
63 break;
64 default:
65 break;
69 @Deactivate
70 void shutDown() {
71 if (listener != null) {
72 PlatformUI.getWorkbench().removeWindowListener(listener);
73 listener = null;
77 private static class WindowTracker implements IWindowListener {
79 private boolean isActive;
81 void update() {
82 if (PlatformUI.isWorkbenchRunning()) {
83 Display display = PlatformUI.getWorkbench().getDisplay();
84 if (display != null && !display.isDisposed()) {
85 try {
86 display.asyncExec(() -> {
87 boolean wasActive = isActive;
88 isActive = !display.isDisposed()
89 && display.getActiveShell() != null;
90 if (wasActive != isActive) {
91 notify(isActive);
93 });
94 } catch (SWTException e) {
95 // Silently ignore -- display was disposed already
101 private void notify(boolean active) {
102 if (PlatformUI.isWorkbenchRunning()) {
103 IEventBroker broker = PlatformUI.getWorkbench()
104 .getService(IEventBroker.class);
105 if (broker != null) {
106 broker.post(TOPIC_APPLICATION_ACTIVE,
107 Boolean.valueOf(active));
112 @Override
113 public void windowActivated(IWorkbenchWindow window) {
114 update();
117 @Override
118 public void windowDeactivated(IWorkbenchWindow window) {
119 update();
122 @Override
123 public void windowClosed(IWorkbenchWindow window) {
124 update();
127 @Override
128 public void windowOpened(IWorkbenchWindow window) {
129 update();