Skip to content

Commit 068b446

Browse files
committed
ToolService: move default method impls to iface
1 parent 34c47d0 commit 068b446

File tree

2 files changed

+36
-53
lines changed

2 files changed

+36
-53
lines changed

src/main/java/org/scijava/tool/DefaultToolService.java

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.scijava.service.Service;
5656
import org.scijava.tool.event.ToolActivatedEvent;
5757
import org.scijava.tool.event.ToolDeactivatedEvent;
58-
import org.scijava.util.RealCoords;
5958

6059
/**
6160
* Default service for keeping track of available tools, including which tool is
@@ -71,8 +70,6 @@ public class DefaultToolService extends AbstractSingletonService<Tool>
7170
implements ToolService
7271
{
7372

74-
private static final double SEPARATOR_DISTANCE = 10;
75-
7673
@Parameter
7774
private EventService eventService;
7875

@@ -96,17 +93,6 @@ public Tool getTool(final String name) {
9693
return tools().get(name);
9794
}
9895

99-
@Override
100-
public <T extends Tool> T getTool(final Class<T> toolClass) {
101-
for (final Tool tool : alwaysActiveToolList()) {
102-
if (toolClass.isInstance(tool)) return toolClass.cast(tool);
103-
}
104-
for (final Tool tool : toolList()) {
105-
if (toolClass.isInstance(tool)) return toolClass.cast(tool);
106-
}
107-
return null;
108-
}
109-
11096
@Override
11197
public List<Tool> getTools() {
11298
return toolList();
@@ -140,14 +126,6 @@ public void setActiveTool(final Tool activeTool) {
140126
eventService.publish(new ToolActivatedEvent(activeTool));
141127
}
142128

143-
@Override
144-
public boolean isSeparatorNeeded(final Tool tool1, final Tool tool2) {
145-
if (tool1 == null || tool2 == null) return false;
146-
final double priority1 = tool1.getInfo().getPriority();
147-
final double priority2 = tool2.getInfo().getPriority();
148-
return Math.abs(priority1 - priority2) >= SEPARATOR_DISTANCE;
149-
}
150-
151129
@Override
152130
public void reportRectangle(final double x, final double y, final double w,
153131
final double h)
@@ -161,15 +139,6 @@ public void reportRectangle(final double x, final double y, final double w,
161139
fh);
162140
}
163141

164-
@Override
165-
public void reportRectangle(final RealCoords p1, final RealCoords p2) {
166-
final double x = Math.min(p1.x, p2.x);
167-
final double y = Math.min(p1.y, p2.y);
168-
final double w = Math.abs(p2.x - p1.x);
169-
final double h = Math.abs(p2.y - p1.y);
170-
reportRectangle(x, y, w, h);
171-
}
172-
173142
@Override
174143
public void reportLine(final double x1, final double y1, final double x2,
175144
final double y2)
@@ -193,11 +162,6 @@ public void reportLine(final double x1, final double y1, final double x2,
193162
", length=" + fl);
194163
}
195164

196-
@Override
197-
public void reportLine(final RealCoords p1, final RealCoords p2) {
198-
reportLine(p1.x, p1.y, p2.x, p2.y);
199-
}
200-
201165
@Override
202166
public void reportPoint(final double x, final double y) {
203167
final DecimalFormat f = new DecimalFormat("0.##");
@@ -206,18 +170,6 @@ public void reportPoint(final double x, final double y) {
206170
statusService.showStatus("x=" + fx + ", y=" + fy);
207171
}
208172

209-
@Override
210-
public void reportPoint(final RealCoords p) {
211-
reportPoint(p.x, p.y);
212-
}
213-
214-
// -- PTService methods --
215-
216-
@Override
217-
public Class<Tool> getPluginType() {
218-
return Tool.class;
219-
}
220-
221173
// -- Event handlers --
222174

223175
@EventHandler

src/main/java/org/scijava/tool/ToolService.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
*/
4646
public interface ToolService extends SingletonService<Tool>, SciJavaService {
4747

48+
double SEPARATOR_DISTANCE = 10;
49+
4850
Tool getTool(String name);
4951

5052
/**
@@ -54,7 +56,15 @@ public interface ToolService extends SingletonService<Tool>, SciJavaService {
5456
* @param toolClass the class of the tool to fetch
5557
* @return the tool, or null if no such tool
5658
*/
57-
<T extends Tool> T getTool(Class<T> toolClass);
59+
default <T extends Tool> T getTool(final Class<T> toolClass) {
60+
for (final Tool tool : getAlwaysActiveTools()) {
61+
if (toolClass.isInstance(tool)) return toolClass.cast(tool);
62+
}
63+
for (final Tool tool : getTools()) {
64+
if (toolClass.isInstance(tool)) return toolClass.cast(tool);
65+
}
66+
return null;
67+
}
5868

5969
List<Tool> getTools();
6070

@@ -68,26 +78,47 @@ public interface ToolService extends SingletonService<Tool>, SciJavaService {
6878
* Returns true if the two specified tools should have a separator between
6979
* them on the tool bar.
7080
*/
71-
boolean isSeparatorNeeded(Tool tool1, Tool tool2);
81+
default boolean isSeparatorNeeded(final Tool tool1, final Tool tool2) {
82+
if (tool1 == null || tool2 == null) return false;
83+
final double priority1 = tool1.getInfo().getPriority();
84+
final double priority2 = tool2.getInfo().getPriority();
85+
return Math.abs(priority1 - priority2) >= SEPARATOR_DISTANCE;
86+
}
7287

7388
/** Publishes rectangle dimensions in the status bar. */
7489
void reportRectangle(final double x, final double y, final double w,
7590
final double h);
7691

7792
/** Publishes rectangle dimensions in the status bar. */
78-
void reportRectangle(final RealCoords p1, final RealCoords p2);
93+
default void reportRectangle(final RealCoords p1, final RealCoords p2) {
94+
final double x = Math.min(p1.x, p2.x);
95+
final double y = Math.min(p1.y, p2.y);
96+
final double w = Math.abs(p2.x - p1.x);
97+
final double h = Math.abs(p2.y - p1.y);
98+
reportRectangle(x, y, w, h);
99+
}
79100

80101
/** Publishes line length and angle in the status bar. */
81102
void reportLine(final double x1, final double y1, final double x2,
82103
final double y2);
83104

84105
/** Publishes line length and angle in the status bar. */
85-
void reportLine(final RealCoords p1, final RealCoords p2);
106+
default void reportLine(final RealCoords p1, final RealCoords p2) {
107+
reportLine(p1.x, p1.y, p2.x, p2.y);
108+
}
86109

87110
/** Publishes point coordinates to the status bar. */
88111
void reportPoint(final double x, final double y);
89112

90113
/** Publishes point coordinates to the status bar. */
91-
void reportPoint(final RealCoords p);
114+
default void reportPoint(final RealCoords p) {
115+
reportPoint(p.x, p.y);
116+
}
117+
118+
// -- PTService methods --
92119

120+
@Override
121+
default Class<Tool> getPluginType() {
122+
return Tool.class;
123+
}
93124
}

0 commit comments

Comments
 (0)