سؤال

I am trying to create netbeans module which whould work upon standard maven java projects. Is there some better way to do that then how it was adviced in: How can I get a project type on Netbeans Platform? This seems to be pretty dependent on implmenetation (note the *Impl postfix of the implementing class found in lookup). I couldn't find any standard method in the Project API. Any ideas? Or is it safe to rely on some "NbMavenProjectImpl" string?

Currently I am going this way:

Project mainProject = OpenProjects.getDefault().getMainProject();

if (mainProject == null) {
    return;
}
String projectType = mainProject.getClass().getName();
if (projectType.equals("NbMavenProjectImpl")) {
     // do some action with the project here
}
هل كانت مفيدة؟

المحلول

One approach would be

if (mainProject.getLookup().lookup(NbMavenProject.class) != null) {
  // do stuff
}

Another one, which should be preferred, is to register your business logic into the maven project lookup eg. using an annotation

@ProjectServiceProvider(service=MyService.class, projectType="org-netbeans-modules-maven")
public class MyServiceImpl implements MyService {
  @Override
  public void doit() {
  }
}

and then accessing the business logic like

MyService ms = mainProject.getLookup().lookup(MyService.class);
if (ms != null) {
  ms.doit()
}

That way you can decouple your API from implementation and as a bonus you can share the same business logic between more project types, if appropriate.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top