Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5424fdd6821bac957e3efbe915b774a2e74efc21 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 * 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.examples.swttip;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.tips.core.Tip;
import org.eclipse.tips.core.TipImage;
import org.eclipse.tips.core.TipProvider;
import org.eclipse.tips.core.internal.LogUtil;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public class SwtTipsProvider extends TipProvider {

	private TipImage fImage64, fImage48;
	private int fCounter;
	private boolean fFetching;

	@Override
	public String getDescription() {
		return "Never ending list of SWT Tips";
	}

	@Override
	public String getID() {
		return getClass().getName();
	}

	@Override
	public synchronized List<Tip> getTips(boolean pFilter) {
		List<Tip> tips = super.getTips(pFilter);
		if (tips.size() <= 1) {
			Job job = new Job(getDescription() + " is getting more tips.") {

				@Override
				protected IStatus run(IProgressMonitor pMonitor) {
					return loadNewTips(pMonitor);
				}
			};
			job.setUser(true);
			job.schedule();
		}
		return tips;
	}

	@Override
	public TipImage getImage() {
		if (fImage48 == null) {
			Bundle bundle = FrameworkUtil.getBundle(getClass());
			try {
				fImage48 = new TipImage(bundle.getEntry("icons/48/swt.png")).setAspectRatio(1);
			} catch (IOException e) {
				getManager().log(LogUtil.error(getClass(), e));
			}
		}
		return fImage48;
	}


	@Override
	public synchronized IStatus loadNewTips(IProgressMonitor pMonitor) {
		SubMonitor subMonitor = SubMonitor.convert(pMonitor);
		if (fFetching) {
			return Status.CANCEL_STATUS;
		}
		try {
			subMonitor.beginTask("Loading Tips for " + getDescription(), -1);
			List<Tip> tips = new ArrayList<>();
			tips.add(new SwtTipImpl(getID(),1));
			tips.add(new SwtTipImpl(getID(),2));
			tips.add(new SwtTipImpl(getID(),3));
			tips.add(new SwtTipImpl(getID(),4));
			tips.add(new SwtTipImpl(getID(),5));
			addTips(tips);
			return Status.OK_STATUS;
		} finally {
			fFetching = false;
			subMonitor.done();
		}
	}

	@Override
	public void dispose() {
	}

	public int getCounter() {
		return ++fCounter;
	}
}

Back to the top