Skip to content

Commit 23b8b42

Browse files
committed
UIDetails: push default method impls to iface
1 parent 7d95887 commit 23b8b42

File tree

2 files changed

+55
-58
lines changed

2 files changed

+55
-58
lines changed

src/main/java/org/scijava/AbstractUIDetails.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
package org.scijava;
3333

34-
import org.scijava.util.ClassUtils;
35-
import org.scijava.util.MiscUtils;
3634
import org.scijava.util.StringMaker;
3735

3836
/**
@@ -88,31 +86,6 @@ public String toString() {
8886

8987
// -- UIDetails methods --
9088

91-
@Override
92-
public String getTitle() {
93-
// use object label, if available
94-
if (getLabel() != null && !getLabel().isEmpty()) return getLabel();
95-
96-
// use name of leaf menu item, if available
97-
if (menuPath != null && menuPath.size() > 0) {
98-
final MenuEntry menuLeaf = menuPath.getLeaf();
99-
final String menuName = menuLeaf.getName();
100-
if (menuName != null && !menuName.isEmpty()) return menuName;
101-
}
102-
103-
// use object name, if available
104-
if (getName() != null && !getName().isEmpty()) return getName();
105-
106-
// use the unique identifier, if available
107-
if (this instanceof Identifiable) {
108-
final String id = ((Identifiable) this).getIdentifier();
109-
if (id != null) return id;
110-
}
111-
112-
// use class name as a last resort
113-
return getClass().getSimpleName();
114-
}
115-
11689
@Override
11790
public MenuPath getMenuPath() {
11891
return menuPath;
@@ -209,34 +182,4 @@ public double getPriority() {
209182
public void setPriority(final double priority) {
210183
this.priority = priority;
211184
}
212-
213-
// -- Comparable methods --
214-
215-
@Override
216-
public int compareTo(final Prioritized that) {
217-
if (that == null) return 1;
218-
219-
// compare priorities
220-
final int priorityCompare = Priority.compare(this, that);
221-
if (priorityCompare != 0) return priorityCompare;
222-
223-
// compare classes
224-
final int classCompare = ClassUtils.compare(getClass(), that.getClass());
225-
if (classCompare != 0) return classCompare;
226-
227-
if (!(that instanceof UIDetails)) return 1;
228-
final UIDetails uiDetails = (UIDetails) that;
229-
230-
// compare names
231-
final String thisName = getName();
232-
final String thatName = uiDetails.getName();
233-
final int nameCompare = MiscUtils.compare(thisName, thatName);
234-
if (nameCompare != 0) return nameCompare;
235-
236-
// compare titles
237-
final String thisTitle = getTitle();
238-
final String thatTitle = uiDetails.getTitle();
239-
return MiscUtils.compare(thisTitle, thatTitle);
240-
}
241-
242185
}

src/main/java/org/scijava/UIDetails.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
package 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

Comments
 (0)