use of com.intellij.struts2.facet.StrutsFacet in project intellij-plugins by JetBrains.
the class BasicLightHighlightingTestCase method tearDown.
@Override
protected final void tearDown() throws Exception {
try {
performTearDown();
} finally {
// clear existing StrutsFacet configuration before running next test
final StrutsFacet existing = StrutsFacet.getInstance(myModule);
if (existing != null) {
existing.getConfiguration().getFileSets().clear();
}
super.tearDown();
}
}
use of com.intellij.struts2.facet.StrutsFacet in project intellij-plugins by JetBrains.
the class BasicLightHighlightingTestCase method createStrutsFileSet.
/**
* For files located in JAR: {@code [PATH_TO_JAR]!/[PATH_TO_STRUTS_XML]}.
*
* @param strutsXmlPaths Paths to files.
*/
protected void createStrutsFileSet(@NonNls final String... strutsXmlPaths) {
final StrutsFacet strutsFacet = StrutsFacet.getInstance(myModule);
assertNotNull(strutsFacet);
final StrutsFacetConfiguration facetConfiguration = strutsFacet.getConfiguration();
final StrutsFileSet fileSet = new StrutsFileSet("test", "test", facetConfiguration);
for (final String fileName : strutsXmlPaths) {
final VirtualFile file;
final String path;
if (fileName.contains("!")) {
path = PathManager.getHomePath() + TEST_DATA_PATH + "/" + fileName;
file = JarFileSystem.getInstance().refreshAndFindFileByPath(path);
} else {
path = fileName;
file = myFixture.copyFileToProject(fileName);
}
assertNotNull("could not find file: '" + path + "'", file);
fileSet.addFile(file);
}
final Set<StrutsFileSet> strutsFileSetSet = facetConfiguration.getFileSets();
strutsFileSetSet.add(fileSet);
}
use of com.intellij.struts2.facet.StrutsFacet in project intellij-plugins by JetBrains.
the class Struts2IconProvider method getIcon.
@Nullable
public Icon getIcon(@NotNull final PsiElement element, final int flags) {
if (!(element instanceof PsiClass)) {
return null;
}
// IconProvider queries non-physical PSI as well (e.g. completion items); check validity
if (!element.isPhysical() || !element.isValid()) {
return null;
}
// no icons when no facet present
final Module module = ModuleUtilCore.findModuleForPsiElement(element);
if (module == null) {
return null;
}
final StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
if (strutsFacet == null) {
return null;
}
// handle suitable JAVA classes --> overlay icon
final PsiClass psiClass = (PsiClass) element;
if (psiClass.isInterface() || psiClass.isEnum() || !psiClass.hasModifierProperty(PsiModifier.PUBLIC) || psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || !JamCommonUtil.isPlainJavaFile(psiClass.getContainingFile())) {
return null;
}
final StrutsModel strutsModel = StrutsManager.getInstance(psiClass.getProject()).getCombinedModel(module);
if (strutsModel == null || !strutsModel.isActionClass(psiClass)) {
return null;
}
final LayeredIcon layeredIcon = new LayeredIcon(2);
final Icon original = PsiClassImplUtil.getClassIcon(flags, psiClass);
layeredIcon.setIcon(original, 0);
layeredIcon.setIcon(Struts2Icons.Action_small, 1, StrutsIcons.OVERLAY_X_OFFSET, StrutsIcons.OVERLAY_Y_OFFSET);
return JBUI.scale(layeredIcon);
}
use of com.intellij.struts2.facet.StrutsFacet in project intellij-plugins by JetBrains.
the class StrutsFrameworkSupportProvider method onFacetCreated.
@Override
protected void onFacetCreated(final StrutsFacet strutsFacet, final ModifiableRootModel modifiableRootModel, final FrameworkVersion version) {
final Module module = strutsFacet.getModule();
StartupManager.getInstance(module.getProject()).runWhenProjectIsInitialized(() -> DumbService.getInstance(module.getProject()).runWhenSmart(() -> {
final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
if (sourceRoots.length <= 0) {
return;
}
final PsiDirectory directory = PsiManager.getInstance(module.getProject()).findDirectory(sourceRoots[0]);
if (directory == null || directory.findFile(StrutsConstants.STRUTS_XML_DEFAULT_FILENAME) != null) {
return;
}
final StrutsFileTemplateProvider templateProvider = new StrutsFileTemplateProvider(module);
final FileTemplate strutsXmlTemplate = templateProvider.determineFileTemplate(directory.getProject());
try {
final StrutsFacetConfiguration strutsFacetConfiguration = strutsFacet.getConfiguration();
// create empty struts.xml & fileset with all found struts-*.xml files (struts2.jar, plugins)
final PsiElement psiElement = FileTemplateUtil.createFromTemplate(strutsXmlTemplate, StrutsConstants.STRUTS_XML_DEFAULT_FILENAME, null, directory);
final Set<StrutsFileSet> empty = Collections.emptySet();
final StrutsFileSet fileSet = new StrutsFileSet(StrutsFileSet.getUniqueId(empty), StrutsFileSet.getUniqueName("Default File Set", empty), strutsFacetConfiguration);
fileSet.addFile(((XmlFile) psiElement).getVirtualFile());
final StrutsConfigsSearcher searcher = new StrutsConfigsSearcher(module);
DumbService.getInstance(module.getProject()).runWhenSmart(() -> searcher.search());
final MultiMap<VirtualFile, PsiFile> jarConfigFiles = searcher.getJars();
for (final VirtualFile virtualFile : jarConfigFiles.keySet()) {
final Collection<PsiFile> psiFiles = jarConfigFiles.get(virtualFile);
for (final PsiFile psiFile : psiFiles) {
fileSet.addFile(psiFile.getVirtualFile());
}
}
strutsFacetConfiguration.getFileSets().add(fileSet);
// create filter & mapping in web.xml (if present)
new WriteCommandAction.Simple(modifiableRootModel.getProject()) {
protected void run() throws Throwable {
final WebFacet webFacet = strutsFacet.getWebFacet();
final ConfigFile configFile = webFacet.getWebXmlDescriptor();
if (configFile == null)
return;
final XmlFile webXmlFile = configFile.getXmlFile();
final WebApp webApp = JamCommonUtil.getRootElement(webXmlFile, WebApp.class, null);
if (webApp == null)
return;
if (!FileModificationService.getInstance().prepareFileForWrite(webXmlFile))
return;
final Filter strutsFilter = webApp.addFilter();
strutsFilter.getFilterName().setStringValue("struts2");
@NonNls final String filterClass = templateProvider.is21orNewer() ? StrutsConstants.STRUTS_2_1_FILTER_CLASS : StrutsConstants.STRUTS_2_0_FILTER_CLASS;
strutsFilter.getFilterClass().setStringValue(filterClass);
final FilterMapping filterMapping = webApp.addFilterMapping();
filterMapping.getFilterName().setValue(strutsFilter);
filterMapping.addUrlPattern().setStringValue("/*");
}
}.execute();
final NotificationListener showFacetSettingsListener = new NotificationListener() {
public void hyperlinkUpdate(@NotNull final Notification notification, @NotNull final HyperlinkEvent event) {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
notification.expire();
ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null);
}
}
};
Notifications.Bus.notify(new Notification("Struts 2", "Struts 2 Setup", "Struts 2 Facet has been created, please check <a href=\"more\">created fileset</a>", NotificationType.INFORMATION, showFacetSettingsListener), module.getProject());
} catch (Exception e) {
LOG.error("error creating struts.xml from template", e);
}
}));
}
use of com.intellij.struts2.facet.StrutsFacet in project intellij-plugins by JetBrains.
the class StrutsFileSetCheckingAnnotator method annotate.
public void annotate(@NotNull final PsiElement psiElement, @NotNull final AnnotationHolder holder) {
if (!(psiElement instanceof XmlFile)) {
return;
}
if (psiElement instanceof JspFile) {
return;
}
final Module module = ModuleUtilCore.findModuleForPsiElement(psiElement);
if (module == null) {
return;
}
// do not run when facet not enabled
if (StrutsFacet.getInstance(module) == null) {
return;
}
final XmlFile xmlFile = (XmlFile) psiElement;
final Project project = psiElement.getProject();
final StrutsManager strutsManager = StrutsManager.getInstance(project);
if (!strutsManager.isStruts2ConfigFile(xmlFile)) {
return;
}
final VirtualFile currentVirtualFile = xmlFile.getVirtualFile();
assert currentVirtualFile != null;
final Set<StrutsFileSet> allConfigFileSets = strutsManager.getAllConfigFileSets(module);
for (final StrutsFileSet configFileSet : allConfigFileSets) {
if (configFileSet.hasFile(currentVirtualFile)) {
return;
}
}
final boolean fileSetAvailable = allConfigFileSets.size() != 0;
final Annotation annotation = holder.createWarningAnnotation(xmlFile, fileSetAvailable ? StrutsBundle.message("annotators.fileset.file.not.registered") : StrutsBundle.message("annotators.fileset.no.file.sets"));
annotation.setFileLevelAnnotation(true);
if (fileSetAvailable) {
final AddToFileSetFix addToFileSetFix = new AddToFileSetFix(xmlFile.getName());
annotation.registerFix(addToFileSetFix);
} else {
annotation.registerFix(new IntentionAction() {
@SuppressWarnings("DialogTitleCapitalization")
@NotNull
public String getText() {
return StrutsBundle.message("annotators.fileset.edit.facet.settings");
}
@NotNull
public String getFamilyName() {
return StrutsBundle.message("intentions.family.name");
}
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile psiFile) {
return true;
}
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile psiFile) throws IncorrectOperationException {
final StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
assert strutsFacet != null;
ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null);
}
public boolean startInWriteAction() {
return false;
}
});
}
}
Aggregations