Skip to content

Commit 1eaf5de

Browse files
committed
Plugin: add extensible key/value pair list
This makes the annotation much more extensible, so that various sorts of plugins can register various bits of information in a freeform way.
1 parent c92a74f commit 1eaf5de

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.plugin;
37+
38+
import java.lang.annotation.Target;
39+
40+
/**
41+
* A name/value attribute pair, used to extend the @{@link Plugin} annotation.
42+
*
43+
* @author Curtis Rueden
44+
* @see Plugin
45+
* @see PluginInfo#get(String)
46+
* @see PluginInfo#is(String)
47+
*/
48+
@Target({})
49+
public @interface Attr {
50+
51+
/** Name of the attribute. */
52+
String name();
53+
54+
/**
55+
* The attribute's value, if applicable.
56+
* <p>
57+
* If set, the method {@link PluginInfo#get(String)} with the attribute's name
58+
* will retrieve the value; in any case, the method
59+
* {@link PluginInfo#is(String)} will return true.
60+
* </p>
61+
*/
62+
String value() default "";
63+
64+
}

src/main/java/org/scijava/plugin/Plugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,10 @@
180180
*/
181181
boolean activeInAppFrame() default false;
182182

183+
/**
184+
* A list of additional attributes which can be used to extend this annotation
185+
* beyond its built-in capabilities.
186+
*/
187+
Attr[] attrs() default {};
188+
183189
}

src/main/java/org/scijava/plugin/PluginInfo.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
package org.scijava.plugin;
3737

3838
import java.net.URL;
39+
import java.util.HashMap;
40+
import java.util.Map;
3941

4042
import org.scijava.AbstractUIDetails;
4143
import org.scijava.Instantiable;
@@ -74,6 +76,9 @@ public class PluginInfo<PT extends SciJavaPlugin> extends AbstractUIDetails
7476
/** Type of this entry's plugin; e.g., {@link org.scijava.service.Service}. */
7577
private Class<PT> pluginType;
7678

79+
/** Table of extra key/value pairs. */
80+
private Map<String, String> values = new HashMap<String, String>();
81+
7782
/** Annotation describing the plugin. */
7883
private Plugin annotation;
7984

@@ -198,6 +203,16 @@ public Class<PT> getPluginType() {
198203
return pluginType;
199204
}
200205

206+
/** Returns true iff the the given key is defined by the metadata. */
207+
public boolean is(final String key) {
208+
return values.containsKey(key);
209+
}
210+
211+
/** Returns the value of the given key, or null if undefined. */
212+
public String get(final String key) {
213+
return values.get(key);
214+
}
215+
201216
/** Gets the associated @{@link Plugin} annotation. */
202217
public Plugin getAnnotation() {
203218
return annotation;
@@ -321,6 +336,13 @@ private void populateValues() {
321336
menuLeaf.setIconPath(iconPath);
322337
}
323338
}
339+
340+
// populate extra attributes
341+
for (final Attr attr : ann.attrs()) {
342+
final String name = attr.name();
343+
final String value = attr.value();
344+
values.put(name, value);
345+
}
324346
}
325347

326348
private MenuPath parseMenuPath(final Menu[] menu) {

0 commit comments

Comments
 (0)