Skip to main content
summaryrefslogtreecommitdiffstats
blob: f5149cfb78fd3f45364f76e981c700547d491e0d (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.text.Collator;
import java.util.Comparator;
import javax.swing.Icon;
import org.eclipse.jpt.utility.model.Model;

/**
 * Used by general-purpose UI models and renderers to cast
 * application model objects to something displayable.
 */
public interface Displayable
	extends Model, Comparable<Displayable>
{

	/**
	 * Return a string that can be used to identify the model
	 * in a textual UI setting (typically the object's name).
	 * When the display string changes, the model should fire
	 * the appropriate change notification:
	 *     this.firePropertyChanged(DISPLAY_STRING_PROPERTY, oldDisplayString, this.displayString());
	 */
	String displayString();
		String DISPLAY_STRING_PROPERTY = "displayString";

	/**
	 * Return an icon that can be used to identify the model
	 * in a UI component that supports icons (the icon can be null).
	 * When the icon changes, the model should fire
	 * the appropriate change notification:
	 *     this.firePropertyChanged(ICON_PROPERTY, oldIcon, this.icon());
	 */
	Icon icon();
		String ICON_PROPERTY = "icon";


	// ********** helper implementations **********

	Collator DEFAULT_COLLATOR = Collator.getInstance();

	/**
	 * Since all displayable objects must be comparable, provide a
	 * typical comparator that can be used to sort a collection of
	 * displayable objects.
	 * Sort based on display string:
	 *     - identical objects are equal (which means they cannot
	 *         co-exist in a SortedSet)
	 *     - use the default collator (which typically interleaves
	 *         lower- and upper-case letters)
	 *     - allow duplicate display strings (from different objects)
	 *     - try to return consistent results for same object pairs
	 */
	Comparator<Displayable> DEFAULT_COMPARATOR =
		new Comparator<Displayable>() {
			public int compare(Displayable d1, Displayable d2) {
				// disallow duplicates based on object identity
				if (d1 == d2) {
					return 0;
				}

				// first compare display strings using the default collator
				int result = DEFAULT_COLLATOR.compare(d1.displayString(), d2.displayString());
				if (result != 0) {
					return result;
				}

				// then compare using object-id
				result = System.identityHashCode(d1) - System.identityHashCode(d2);
				if (result != 0) {
					return result;
				}

				// It's unlikely that we get to this point; but, just in case, we will return -1.
				// Unfortunately, this introduces some mild unpredictability to the sort order
				// (unless the objects are always passed into this method in the same order).
				return -1;		// if all else fails, indicate that o1 < o2
			}
			@Override
			public String toString() {
				return "Displayable.DEFAULT_COMPARATOR";
			}
		};

}

Back to the top