Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e977e0be345a063209adfbf21c3516ab4541b4b8 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * Taskbar snippet: customize the taskbar item with progress indicator, overlay image, and overlay text
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet336 {
	static Display display;
	static Shell shell;

static TaskItem getTaskBarItem () {
	TaskBar bar = display.getSystemTaskBar();
	if (bar == null) return null;
	TaskItem item = bar.getItem(shell);
	if (item == null) item = bar.getItem(null);
	return item;
}

public static void main(String[] args) {
	display = new Display();
	shell = new Shell(display);
	shell.setLayout(new GridLayout());
	TabFolder folder = new TabFolder(shell, SWT.NONE);
	folder.setLayoutData(new GridData(GridData.FILL_BOTH));

	//Progress tab
	TabItem item = new TabItem(folder, SWT.NONE);
	item.setText("Progress");
	Composite composite = new Composite(folder, SWT.NONE);
	composite.setLayout(new GridLayout());
	item.setControl(composite);
	Listener listener = event -> {
		Button button = (Button)event.widget;
		if (!button.getSelection()) return;
		TaskItem item1 = getTaskBarItem();
		if (item1 != null) {
			int state = ((Integer)button.getData()).intValue();
			item1.setProgressState(state);
		}
	};
	Group group = new Group (composite, SWT.NONE);
	group.setText("State");
	group.setLayout(new GridLayout());
	group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	Button button;
	String[] stateLabels = {"SWT.DEFAULT", "SWT.INDETERMINATE", "SWT.NORMAL", "SWT.ERROR", "SWT.PAUSED"};
	int[] states = {SWT.DEFAULT, SWT.INDETERMINATE, SWT.NORMAL, SWT.ERROR, SWT.PAUSED };
	for (int i = 0; i < states.length; i++) {
		button = new Button(group, SWT.RADIO);
		button.setText(stateLabels[i]);
		button.setData(new Integer(states[i]));
		button.addListener(SWT.Selection, listener);
		if (i==0) button.setSelection(true);
	}
	group = new Group (composite, SWT.NONE);
	group.setText("Value");
	group.setLayout(new GridLayout(2, false));
	group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	Label label = new Label(group, SWT.NONE);
	label.setText("Progress");
	final Scale scale = new Scale (group, SWT.NONE);
	scale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	scale.addListener(SWT.Selection, event -> {
		TaskItem item1 = getTaskBarItem();
		if (item1 != null) item1.setProgress(scale.getSelection());
	});

	//Overlay text tab
	item = new TabItem(folder, SWT.NONE);
	item.setText("Text");
	composite = new Composite(folder, SWT.NONE);
	composite.setLayout(new GridLayout());
	item.setControl(composite);
	group = new Group (composite, SWT.NONE);
	group.setText("Enter a short text:");
	group.setLayout(new GridLayout(2, false));
	group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	final Text text = new Text(group, SWT.BORDER | SWT.SINGLE);
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	data.horizontalSpan = 2;
	text.setLayoutData(data);
	button = new Button(group, SWT.PUSH);
	button.setText("Set");
	button.addListener(SWT.Selection, event -> {
		TaskItem item1 = getTaskBarItem();
		if (item1 != null) item1.setOverlayText(text.getText());
	});
	button = new Button(group, SWT.PUSH);
	button.setText("Clear");
	button.addListener(SWT.Selection, event -> {
		text.setText("");
		TaskItem item1 = getTaskBarItem();
		if (item1 != null) item1.setOverlayText("");
	});

	//Overlay image tab
	item = new TabItem(folder, SWT.NONE);
	item.setText("Image");
	composite = new Composite(folder, SWT.NONE);
	composite.setLayout(new GridLayout());
	item.setControl(composite);
	Listener listener3 = event -> {
		Button button1 = (Button)event.widget;
		if (!button1.getSelection()) return;
		TaskItem item1 = getTaskBarItem();
		if (item1 != null) {
			String text1 = button1.getText();
			Image image = null;
			if (!text1.equals("NONE")) image = new Image (display, Snippet336.class.getResourceAsStream(text1));
			Image oldImage = item1.getOverlayImage();
			item1.setOverlayImage (image);
			if (oldImage != null) oldImage.dispose();
		}
	};
	group = new Group (composite, SWT.NONE);
	group.setText("Images");
	group.setLayout(new GridLayout());
	group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	button = new Button(group, SWT.RADIO);
	button.setText("NONE");
	button.addListener(SWT.Selection, listener3);
	button.setSelection(true);
	String[] images = {"eclipse.png", "pause.gif", "run.gif", "warning.gif"};
	for (int i = 0; i < images.length; i++) {
		button = new Button(group, SWT.RADIO);
		button.setText(images[i]);
		button.addListener(SWT.Selection, listener3);
	}
    shell.pack();
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	display.dispose();
}

}

Back to the top