Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f32d420b0d00c12e9550aeb73ad395076165a8c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
 * Copyright (c) 2018 Remain Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     wim.jongman@remainsoftware.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.tips.ui;

import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.widgets.Display;
import org.eclipse.tips.core.TipManager;
import org.eclipse.tips.core.TipProvider;
import org.eclipse.tips.core.internal.LogUtil;
import org.eclipse.tips.ui.internal.TipDialog;

/**
 * Class to manage the tip providers and start the tip of the day UI.
 *
 */
@SuppressWarnings("restriction")
public abstract class DefaultTipManager extends TipManager {

	private TipDialog fTipDialog;

	/**
	 * Opens the Tip of the Day dialog. Subclasses may override if they want to
	 * present the Tips in a different way, e.g. in a view.
	 *
	 * @param startUp
	 *            When called from a startup situation, true must be passed for
	 *            <code>pStartup</code>. If in a manual starting situation, false
	 *            must be passed. This enables the manager to decide to skip opening
	 *            the dialog at startup (e.g., no new tip items).
	 *
	 * @see #isOpen()
	 */
	@Override
	public TipManager open(boolean startUp) {
		if (fOpen && (fTipDialog == null || fTipDialog.isDisposed())) {
			fOpen = false;
		}
		try {
			Assert.isTrue(!fOpen, "Tip of the Day already open.");
		} catch (Exception e) {
			log(LogUtil.error(getClass(), e));
			throw e;
		}

		if (!mustOpen(startUp)) {
			return this;
		}

		fTipDialog = new TipDialog(Display.getCurrent().getActiveShell(), this, TipDialog.DEFAULT_STYLE);
		fTipDialog.addDisposeListener(pE -> {
			try {
				dispose();
			} finally {
				fOpen = false;
			}
		});
		fTipDialog.open();
		fOpen = true;
		return this;
	}

	// Open if not a startup call or if there are unread tips.
	private boolean mustOpen(boolean startUp) {
		if (!startUp) {
			return true;
		}
		if (startUp && isRunAtStartup()) {
			for (TipProvider provider : getProviders()) {
				if (provider.isReady() && !provider.getTips(true).isEmpty()) {
					return true;
				}
			}
		}
		return false;
	}

}

Back to the top