Lines 52-61
Link Here
|
52 |
import java.util.Map; |
52 |
import java.util.Map; |
53 |
import java.util.concurrent.Callable; |
53 |
import java.util.concurrent.Callable; |
54 |
import javax.lang.model.element.Element; |
54 |
import javax.lang.model.element.Element; |
55 |
import javax.lang.model.element.ElementKind; |
|
|
56 |
import javax.lang.model.element.ExecutableElement; |
57 |
import javax.lang.model.element.TypeElement; |
55 |
import javax.lang.model.element.TypeElement; |
58 |
import javax.lang.model.type.TypeMirror; |
|
|
59 |
import org.netbeans.api.java.source.GeneratorUtilities; |
56 |
import org.netbeans.api.java.source.GeneratorUtilities; |
60 |
import org.netbeans.api.java.source.TreeMaker; |
57 |
import org.netbeans.api.java.source.TreeMaker; |
61 |
import org.netbeans.api.java.source.WorkingCopy; |
58 |
import org.netbeans.api.java.source.WorkingCopy; |
Lines 75-89
Link Here
|
75 |
@ServiceProvider(service=Hinter.class) |
72 |
@ServiceProvider(service=Hinter.class) |
76 |
public class ActionRegistrationHinter implements Hinter { |
73 |
public class ActionRegistrationHinter implements Hinter { |
77 |
|
74 |
|
78 |
private static final String[] EAGER_INTERFACES = { |
|
|
79 |
"org.openide.util.actions.Presenter.Menu", |
80 |
"org.openide.util.actions.Presenter.Toolbar", |
81 |
"org.openide.util.actions.Presenter.Popup", |
82 |
"org.openide.util.ContextAwareAction", |
83 |
"org.openide.awt.DynamicMenuContent" |
84 |
}; |
85 |
|
86 |
@Messages({"# {0} - class or method return type", "ActionRegistrationHinter.not_presenter=You cannot use @ActionRegistration on the eager action {0} unless it is assignable to ContextAwareAction, DynamicMenuContent, or some Presenter.* interface."}) |
87 |
public @Override void process(final Context ctx) throws Exception { |
75 |
public @Override void process(final Context ctx) throws Exception { |
88 |
final FileObject file = ctx.file(); |
76 |
final FileObject file = ctx.file(); |
89 |
if (!file.isData() || !file.hasExt("instance")) { |
77 |
if (!file.isData() || !file.hasExt("instance")) { |
Lines 96-102
Link Here
|
96 |
if (!annotationsAvailable(ctx)) { |
84 |
if (!annotationsAvailable(ctx)) { |
97 |
return null; |
85 |
return null; |
98 |
} |
86 |
} |
99 |
ctx.findAndModifyDeclaration(file.getAttribute("literal:delegate"), new RegisterAction(ctx)); |
87 |
ctx.findAndModifyDeclaration(file.getAttribute("literal:delegate"), new RegisterAction(ctx, false)); |
100 |
return null; |
88 |
return null; |
101 |
} |
89 |
} |
102 |
}); |
90 |
}); |
Lines 126-154
Link Here
|
126 |
action = "new:" + file.getName().replace('-', '.'); |
114 |
action = "new:" + file.getName().replace('-', '.'); |
127 |
} |
115 |
} |
128 |
} |
116 |
} |
129 |
ctx.findAndModifyDeclaration(action, new RegisterAction(ctx) { |
117 |
ctx.findAndModifyDeclaration(action, new RegisterAction(ctx, true)); |
130 |
public @Override void run(WorkingCopy wc, Element declaration, ModifiersTree modifiers) throws Exception { |
|
|
131 |
TypeMirror type; |
132 |
if (declaration.getKind() == ElementKind.CLASS) { |
133 |
type = ((TypeElement) declaration).asType(); |
134 |
} else { |
135 |
type = ((ExecutableElement) declaration).getReturnType(); |
136 |
} |
137 |
boolean ok = false; |
138 |
for (String xface : EAGER_INTERFACES) { |
139 |
TypeElement xfaceEl = wc.getElements().getTypeElement(xface); |
140 |
if (xfaceEl != null && wc.getTypes().isAssignable(type, xfaceEl.asType())) { |
141 |
ok = true; |
142 |
break; |
143 |
} |
144 |
} |
145 |
if (!ok) { |
146 |
DialogDisplayer.getDefault().notify(new Message(ActionRegistrationHinter_not_presenter(type), NotifyDescriptor.WARNING_MESSAGE)); |
147 |
return; |
148 |
} |
149 |
super.run(wc, declaration, modifiers); |
150 |
} |
151 |
}); |
152 |
return null; |
118 |
return null; |
153 |
} |
119 |
} |
154 |
}); |
120 |
}); |
Lines 168-176
Link Here
|
168 |
private static class RegisterAction implements Context.ModifyDeclarationTask { |
134 |
private static class RegisterAction implements Context.ModifyDeclarationTask { |
169 |
|
135 |
|
170 |
private final Context ctx; |
136 |
private final Context ctx; |
|
|
137 |
private final boolean eager; |
171 |
|
138 |
|
172 |
RegisterAction(Context ctx) { |
139 |
RegisterAction(Context ctx, boolean eager) { |
173 |
this.ctx = ctx; |
140 |
this.ctx = ctx; |
|
|
141 |
this.eager = eager; |
174 |
} |
142 |
} |
175 |
|
143 |
|
176 |
public @Override void run(WorkingCopy wc, Element declaration, ModifiersTree modifiers) throws Exception { |
144 |
public @Override void run(WorkingCopy wc, Element declaration, ModifiersTree modifiers) throws Exception { |
Lines 192-197
Link Here
|
192 |
params.put("iconInMenu", !((Boolean) noIconInMenu)); |
160 |
params.put("iconInMenu", !((Boolean) noIconInMenu)); |
193 |
} |
161 |
} |
194 |
params.put("asynchronous", file.getAttribute("asynchronous")); |
162 |
params.put("asynchronous", file.getAttribute("asynchronous")); |
|
|
163 |
if (eager) { |
164 |
params.put("lazy", false); |
165 |
} else { |
166 |
// XXX specify lazy=true if implements one of the 5 specials even though using Actions.* factory (but probably rare) |
167 |
} |
195 |
nue = ctx.addAnnotation(wc, nue, "org.openide.awt.ActionRegistration", null, params); |
168 |
nue = ctx.addAnnotation(wc, nue, "org.openide.awt.ActionRegistration", null, params); |
196 |
ctx.delete(file); |
169 |
ctx.delete(file); |
197 |
TreeMaker make = wc.getTreeMaker(); |
170 |
TreeMaker make = wc.getTreeMaker(); |