3131
3232package org .scijava ;
3333
34+ import org .scijava .util .ClassUtils ;
35+ import org .scijava .util .MiscUtils ;
36+
3437/**
3538 * An interface defining details useful for generating relevant user interface
3639 * elements.
@@ -56,7 +59,30 @@ public interface UIDetails extends BasicDetails, Prioritized {
5659 * <li>Item's class name, without package prefix</li>
5760 * </ol>
5861 */
59- String getTitle ();
62+ default String getTitle () {
63+ // use object label, if available
64+ if (getLabel () != null && !getLabel ().isEmpty ()) return getLabel ();
65+
66+ // use name of leaf menu item, if available
67+ final MenuPath menuPath = getMenuPath ();
68+ if (menuPath != null && menuPath .size () > 0 ) {
69+ final MenuEntry menuLeaf = menuPath .getLeaf ();
70+ final String menuName = menuLeaf .getName ();
71+ if (menuName != null && !menuName .isEmpty ()) return menuName ;
72+ }
73+
74+ // use object name, if available
75+ if (getName () != null && !getName ().isEmpty ()) return getName ();
76+
77+ // use the unique identifier, if available
78+ if (this instanceof Identifiable ) {
79+ final String id = ((Identifiable ) this ).getIdentifier ();
80+ if (id != null ) return id ;
81+ }
82+
83+ // use class name as a last resort
84+ return getClass ().getSimpleName ();
85+ }
6086
6187 /** Gets the path to the object's suggested position in the menu structure. */
6288 MenuPath getMenuPath ();
@@ -124,4 +150,32 @@ public interface UIDetails extends BasicDetails, Prioritized {
124150 */
125151 void setSelected (boolean selected );
126152
153+ // -- Comparable methods --
154+
155+ @ Override
156+ default int compareTo (final Prioritized that ) {
157+ if (that == null ) return 1 ;
158+
159+ // compare priorities
160+ final int priorityCompare = Priority .compare (this , that );
161+ if (priorityCompare != 0 ) return priorityCompare ;
162+
163+ // compare classes
164+ final int classCompare = ClassUtils .compare (getClass (), that .getClass ());
165+ if (classCompare != 0 ) return classCompare ;
166+
167+ if (!(that instanceof UIDetails )) return 1 ;
168+ final UIDetails uiDetails = (UIDetails ) that ;
169+
170+ // compare names
171+ final String thisName = getName ();
172+ final String thatName = uiDetails .getName ();
173+ final int nameCompare = MiscUtils .compare (thisName , thatName );
174+ if (nameCompare != 0 ) return nameCompare ;
175+
176+ // compare titles
177+ final String thisTitle = getTitle ();
178+ final String thatTitle = uiDetails .getTitle ();
179+ return MiscUtils .compare (thisTitle , thatTitle );
180+ }
127181}
0 commit comments