Skip to main content
summaryrefslogtreecommitdiffstats
blob: 646be562401a5e487655337caa29c363a6ca10b2 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
 * Copyright (c) 2005, 2010 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
 *     Tasktop Technologies - generalized ProgressInfoItem for reuse
 *******************************************************************************/

package org.eclipse.equinox.internal.p2.ui.discovery.util;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.PlatformUI;

/**
 * Based on {@link org.eclipse.ui.internal.progress.ProgressInfoItem}.
 * 
 * @author Steffen Pingel
 */
@SuppressWarnings("restriction")
public abstract class ControlListItem<T> extends Composite {

	static String DARK_COLOR_KEY = "org.eclipse.mylyn.commons.ui.ControlListItem.DARK_COLOR"; //$NON-NLS-1$

	interface IndexListener {

		/**
		 * Select the item previous to the receiver.
		 */
		public void selectPrevious();

		/**
		 * Select the next previous to the receiver.
		 */
		public void selectNext();

		/**
		 * Select the receiver.
		 */
		public void select();

		public void open();

	}

	IndexListener indexListener;

	private int currentIndex;

	private boolean selected;

	private final MouseAdapter mouseListener;

	private boolean isShowing = true;

	private final MouseTrackAdapter mouseTrackListener;

	private boolean hot;

	static {
		// Mac has different Gamma value
		int shift = "carbon".equals(SWT.getPlatform()) ? -25 : -10;//$NON-NLS-1$ 

		Color lightColor = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);

		// Determine a dark color by shifting the list color
		RGB darkRGB = new RGB(Math.max(0, lightColor.getRed() + shift), Math.max(0, lightColor.getGreen() + shift), Math.max(0, lightColor.getBlue() + shift));
		JFaceResources.getColorRegistry().put(DARK_COLOR_KEY, darkRGB);
	}

	/**
	 * Create a new instance of the receiver with the specified parent, style and info object/
	 * 
	 * @param parent
	 * @param style
	 * @param element
	 */
	public ControlListItem(Composite parent, int style, T element) {
		super(parent, style | SWT.NO_FOCUS);
		Assert.isNotNull(element);
		super.setData(element);
		setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		mouseListener = doCreateMouseListener();
		mouseTrackListener = doCreateMouseTrackListener();
		registerChild(this);
		//		Control[] children = getChildren();
		//		for (Control child : children) {
		//			registerChild(child);
		//		}
		setHot(false);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T getData() {
		return (T) super.getData();
	}

	@Override
	public void setData(Object data) {
		throw new IllegalArgumentException();
	}

	private MouseTrackAdapter doCreateMouseTrackListener() {
		return new MouseTrackAdapter() {
			private int enterCount;

			@Override
			public void mouseEnter(MouseEvent e) {
				enterCount++;
				updateHotState();
			}

			@Override
			public void mouseExit(MouseEvent e) {
				enterCount--;
				getDisplay().asyncExec(new Runnable() {
					public void run() {
						if (!isDisposed()) {
							updateHotState();
						}
					}
				});
			}

			private void updateHotState() {
				if (enterCount == 0) {
					if (isHot()) {
						setHot(false);
					}
				} else {
					if (!isHot()) {
						setHot(true);
					}
				}
			}
		};
	}

	private MouseAdapter doCreateMouseListener() {
		return new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				if (indexListener != null) {
					if (e.count == 2) {
						indexListener.open();
					} else {
						indexListener.select();
					}
				}
			}
		};
	}

	public boolean isHot() {
		return hot;
	}

	public void setHot(boolean hot) {
		this.hot = hot;
	}

	protected void registerChild(Control child) {
		child.addMouseListener(mouseListener);
		child.addMouseTrackListener(mouseTrackListener);

	}

	/**
	 * Refresh the contents of the receiver.
	 */
	protected abstract void refresh();

	/**
	 * Set the color base on the index
	 * 
	 * @param index
	 */
	public void updateColors(int index) {
		currentIndex = index;

		if (selected) {
			setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
			setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
		} else {
			if (index % 2 == 0) {
				setBackground(JFaceResources.getColorRegistry().get(DARK_COLOR_KEY));
			} else {
				setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			}
			setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
		}
	}

	@Override
	public void setForeground(Color color) {
		super.setForeground(color);
		Control[] children = getChildren();
		for (Control child : children) {
			child.setForeground(color);
		}
	}

	@Override
	public void setBackground(Color color) {
		super.setBackground(color);
		Control[] children = getChildren();
		for (Control child : children) {
			child.setBackground(color);
		}
	}

	/**
	 * Set the selection colors.
	 * 
	 * @param select
	 *            boolean that indicates whether or not to show selection.
	 */
	public void setSelected(boolean select) {
		selected = select;
		updateColors(currentIndex);
	}

	/**
	 * Set the listener for index changes.
	 * 
	 * @param indexListener
	 */
	void setIndexListener(IndexListener indexListener) {
		this.indexListener = indexListener;
	}

	/**
	 * Return whether or not the receiver is selected.
	 * 
	 * @return boolean
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * Set whether or not the receiver is being displayed based on the top and bottom of the currently visible area.
	 * 
	 * @param top
	 * @param bottom
	 */
	void setDisplayed(int top, int bottom) {
		int itemTop = getLocation().y;
		int itemBottom = itemTop + getBounds().height;
		setDisplayed(itemTop <= bottom && itemBottom > top);

	}

	/**
	 * Set whether or not the receiver is being displayed
	 * 
	 * @param displayed
	 */
	private void setDisplayed(boolean displayed) {
		// See if this element has been turned off
		boolean refresh = !isShowing && displayed;
		isShowing = displayed;
		if (refresh) {
			refresh();
		}
	}

}

Back to the top