Skip to main content
summaryrefslogtreecommitdiffstats
blob: ea649ba33dc23b9cff9f44bfc19d763e14590941 (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
/*******************************************************************************
 * Copyright (c) 2007 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal.swing;

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JList;

/**
 * This renderer should behave the same as the DefaultListCellRenderer;
 * but it slightly refactors the calculation of the icon and text of the list
 * cell so that subclasses can easily override the methods that build
 * the icon and text.
 * 
 * In most cases, you need only override:
 *     #buildIcon(Object value)
 *     #buildText(Object value)
 */
public class SimpleListCellRenderer
	extends DefaultListCellRenderer
{

	/**
	 * Construct a simple renderer.
	 */
	public SimpleListCellRenderer() {
		super();
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		// substitute null for the cell value so nothing is drawn initially...
		super.getListCellRendererComponent(list, null, index, isSelected, cellHasFocus);
		this.setOpaque(true);

		// ...then set the icon and text manually
		this.setIcon(this.buildIcon(list, value, index, isSelected, cellHasFocus));
		this.setText(this.buildText(list, value, index, isSelected, cellHasFocus));

		this.setToolTipText(this.buildToolTipText(list, value, index, isSelected, cellHasFocus));

		// the context will be initialized only if a reader is running
		if (this.accessibleContext != null) {
			this.accessibleContext.setAccessibleName(this.buildAccessibleName(list, value, index, isSelected, cellHasFocus));
		}

		return this;
	}

	/**
	 * Return the icon representation of the specified cell
	 * value and other settings. (Even more settings are
	 * accessible via inherited getters: hasFocus, isEnabled, etc.)
	 */
	protected Icon buildIcon(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		return this.buildIcon(value);
	}

	/**
	 * Return the icon representation of the specified cell
	 * value. The default is to display no icon at all unless the
	 * value itself is an icon.
	 */
	protected Icon buildIcon(Object value) {
		// replicate the default behavior
		return (value instanceof Icon) ? (Icon) value : null;
	}

	/**
	 * Return the textual representation of the specified cell
	 * value and other settings. (Even more settings are
	 * accessible via inherited getters: hasFocus, isEnabled, etc.)
	 */
	protected String buildText(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		return this.buildText(value);
	}

	/**
	 * Return the textual representation of the specified cell
	 * value. The default is to display the object's default string
	 * representation (as returned by #toString()); unless the
	 * value itself is an icon, in which case no text is displayed.
	 */
	protected String buildText(Object value) {
		return (value instanceof Icon) ? "" : ((value == null) ? "" : value.toString());
	}

	/**
	 * Return the text displayed when the cursor lingers over the specified cell.
	 * (Even more settings are accessible via inherited getters: hasFocus, isEnabled, etc.)
	 */
	protected String buildToolTipText(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		return this.buildToolTipText(value);
	}

	/**
	 * Return the text displayed when the cursor lingers over the specified cell.
	 */
	protected String buildToolTipText(Object value) {
		return null;
	}

	/**
	 * Return the accessible name to be given to the component used to render
	 * the given value and other settings. (Even more settings are accessible via
	 * inherited getters: hasFocus, isEnabled, etc.)
	 */
	protected String buildAccessibleName(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		return this.buildAccessibleName(value);
	}

	/**
	 * Return the accessible name to be given to the component used to render
	 * the given value.
	 */
	protected String buildAccessibleName(Object value) {
		return null;
	}

}

Back to the top