@Nullable private static PsiElement findElementToAdd(final PsiFile psiFile) { if (psiFile.getFileType().equals(GuiFormFileType.INSTANCE)) { return psiFile; } else if (psiFile.getFileType().equals(JavaFileType.INSTANCE)) { final PsiClass psiClass = PsiTreeUtil.getChildOfType(psiFile, PsiClass.class); Project project = psiFile.getProject(); final PsiClass componentClass = JavaPsiFacade.getInstance(project).findClass(JComponent.class.getName(), ProjectScope.getAllScope(project)); if (psiClass != null && componentClass != null && psiClass.isInheritor(componentClass, true) && psiClass.getQualifiedName() != null) { return psiClass; } } return null; }
private static boolean isFieldUnreferenced(final PsiField field) { try { return ReferencesSearch.search(field).forEach(new Processor<PsiReference>() { public boolean process(final PsiReference t) { PsiFile f = t.getElement().getContainingFile(); if (f != null && f.getFileType().equals(GuiFormFileType.INSTANCE)) { return true; } PsiMethod method = PsiTreeUtil.getParentOfType(t.getElement(), PsiMethod.class); if (method != null && method.getName().equals(AsmCodeGenerator.SETUP_METHOD_NAME)) { return true; } return false; } }); } catch (IndexNotReadyException e) { return false; } }
@NonNls private String suggestFormName() { int count = 0; do { count++; } while(myDirectory.findFile("Form" + count + GuiFormFileType.DOT_DEFAULT_EXTENSION) != null); return "Form" + count; }
@Override protected void doOKAction() { if (getOKAction().isEnabled()) { try { myDirectory.checkCreateFile(getFormName() + GuiFormFileType.DOT_DEFAULT_EXTENSION); } catch (IncorrectOperationException e) { JOptionPane.showMessageDialog(myRootPanel, UIDesignerBundle.message("error.form.already.exists", getFormName())); return; } if (!checkUnknownLayoutManagers(myDirectory.getProject())) return; close(OK_EXIT_CODE); } }
@NotNull @Override public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) { PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false); if (psiClass != null) { while (psiClass != null) { List<PsiFile> forms = FormClassIndex.findFormsBoundToClass(psiClass.getProject(), psiClass); if (!forms.isEmpty()) { return GotoRelatedItem.createItems(forms, "UI Forms"); } psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class); } } else { PsiFile file = context.getContainingFile(); if (file != null && file.getFileType() == GuiFormFileType.INSTANCE) { try { String className = Utils.getBoundClassName(file.getText()); if (className != null) { Project project = file.getProject(); PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project)); if (aClass != null) { return Collections.singletonList(new GotoRelatedItem(aClass, "Java")); } } } catch (Exception ignore) { } } } return Collections.emptyList(); }
@Override public boolean isMoveRedundant(PsiElement source, PsiElement target) { if (source instanceof PsiFile && source.getParent() == target) { final VirtualFile virtualFile = ((PsiFile)source).getVirtualFile(); if (virtualFile != null && virtualFile.getFileType() instanceof GuiFormFileType) { return true; } } return super.isMoveRedundant(source, target); }
@NotNull @Override public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement context) { PsiClass psiClass = PsiTreeUtil.getParentOfType(context, PsiClass.class, false); if (psiClass != null) { while (psiClass != null) { List<PsiFile> forms = FormClassIndex.findFormsBoundToClass(psiClass); if (!forms.isEmpty()) { return GotoRelatedItem.createItems(forms, "UI Forms"); } psiClass = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class); } } else { PsiFile file = context.getContainingFile(); if (file.getFileType() == GuiFormFileType.INSTANCE) { try { String className = Utils.getBoundClassName(file.getText()); if (className != null) { Project project = file.getProject(); PsiClass aClass = JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project)); if (aClass != null) { return Collections.singletonList(new GotoRelatedItem(aClass, "Java")); } } } catch (Exception ignore) { } } } return Collections.emptyList(); }
@NotNull public PsiReference[] getReferencesByElement(@NotNull final PsiElement element, @NotNull final ProcessingContext context) { if (element instanceof PsiPlainTextFile) { final PsiPlainTextFile plainTextFile = (PsiPlainTextFile) element; if (plainTextFile.getFileType().equals(GuiFormFileType.INSTANCE)) { return getCachedData(plainTextFile).myReferences; } } return PsiReference.EMPTY_ARRAY; }
@Override @Nullable public UsageType getUsageType(PsiElement element) { final PsiFile psiFile = element.getContainingFile(); if(psiFile.getFileType() == GuiFormFileType.INSTANCE) { return FORM_USAGE_TYPE; } return null; }
private static boolean scopeCanContainForms(SearchScope scope) { if(!(scope instanceof LocalSearchScope)) { return true; } LocalSearchScope localSearchScope = (LocalSearchScope) scope; final PsiElement[] elements = localSearchScope.getScope(); for(PsiElement element : elements) { if(element instanceof PsiDirectory) { return true; } PsiFile file; if(element instanceof PsiFile) { file = (PsiFile) element; } else { if(!element.isValid()) { continue; } file = element.getContainingFile(); } if(file.getFileType() == GuiFormFileType.INSTANCE) { return true; } } return false; }
private static boolean processReferencesInUIFormsInner(String name, PsiElement element, Processor<PsiReference> processor, GlobalSearchScope scope1, PsiManagerImpl manager, final LocalSearchScope filterScope) { GlobalSearchScope scope = GlobalSearchScope.projectScope(manager.getProject()).intersectWith(scope1); manager.startBatchFilesProcessingMode(); try { List<PsiFile> files = FormClassIndex.findFormsBoundToClass(manager.getProject(), name, scope); for(PsiFile file : files) { ProgressManager.checkCanceled(); if(file.getFileType() != GuiFormFileType.INSTANCE) { continue; } if(!processReferences(processor, file, name, element, filterScope)) { return false; } } } finally { manager.finishBatchFilesProcessingMode(); } return true; }
private static boolean processReferencesInUIForms(final Processor<PsiReference> processor, final PropertiesFile propFile, final GlobalSearchScope globalSearchScope, final LocalSearchScope filterScope) { final Project project = propFile.getProject(); GlobalSearchScope scope = GlobalSearchScope.projectScope(project).intersectWith(globalSearchScope); PsiManagerImpl manager = (PsiManagerImpl) propFile.getContainingFile().getManager(); final String baseName = propFile.getResourceBundle().getBaseName(); manager.startBatchFilesProcessingMode(); try { PsiFile[] files = CacheManager.getInstance(project).getFilesWithWord(baseName, UsageSearchContext.IN_PLAIN_TEXT, scope, true); for(PsiFile file : files) { ProgressManager.checkCanceled(); if(file.getFileType() != GuiFormFileType.INSTANCE) { continue; } if(!processReferences(processor, file, baseName, propFile.getContainingFile(), filterScope)) { return false; } } } finally { manager.finishBatchFilesProcessingMode(); } return true; }
public void update(final AnActionEvent e) { final GuiEditor editor = FormEditingUtil.getActiveEditor(e.getDataContext()); if(editor == null){ e.getPresentation().setVisible(false); return; } final VirtualFile file = editor.getFile(); e.getPresentation().setVisible( FileDocumentManager.getInstance().getDocument(file) != null && file.getFileType() == GuiFormFileType.INSTANCE ); }
@Nullable public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (file.getFileType().equals(GuiFormFileType.INSTANCE)) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) { return null; } final Module module = ModuleUtil.findModuleForFile(virtualFile, file.getProject()); if (module == null) { return null; } final LwRootContainer rootContainer; try { rootContainer = Utils.getRootContainer(file.getText(), new PsiPropertiesProvider(module)); } catch (Exception e) { return null; } if (rootContainer.isInspectionSuppressed(getShortName(), null)) { return null; } final FormFileErrorCollector collector = new FormFileErrorCollector(file, manager, isOnTheFly); startCheckForm(rootContainer); FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor() { public boolean visit(final IComponent component) { if (!rootContainer.isInspectionSuppressed(getShortName(), component.getId())) { checkComponentProperties(module, component, collector); } return true; } }); doneCheckForm(rootContainer); return collector.result(); } return null; }
public void update(PresentationData presentation) { if (getValue() == null || !getValue().isValid()) { setValue(null); } else { presentation.setPresentableText(getValue().getName()); presentation.setIcon(GuiFormFileType.INSTANCE.getIcon()); } }
@Override public boolean isMoveRedundant(PsiElement source, PsiElement target) { if(source instanceof PsiFile && source.getParent() == target) { final VirtualFile virtualFile = ((PsiFile) source).getVirtualFile(); if(virtualFile != null && virtualFile.getFileType() instanceof GuiFormFileType) { return true; } } return super.isMoveRedundant(source, target); }
@Override public boolean accept(@NotNull final Project project, @NotNull final VirtualFile file) { return file.getFileType() == GuiFormFileType.INSTANCE && !GuiFormFileType.INSTANCE.isBinary() && (ModuleUtil.findModuleForFile(file, project) != null || file instanceof LightVirtualFile); }
@Nullable public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (file.getFileType().equals(GuiFormFileType.INSTANCE)) { final PsiDirectory directory = file.getContainingDirectory(); if (directory != null && I18nInspection.isPackageNonNls(JavaDirectoryService.getInstance().getPackage(directory))) { return null; } } return super.checkFile(file, manager, isOnTheFly); }
public String nameToCanonicalName(String name, PsiNamedElement psiFile) { if (name.endsWith(GuiFormFileType.DOT_DEFAULT_EXTENSION)) return name.substring(0, name.length() - GuiFormFileType.DOT_DEFAULT_EXTENSION.length()); return name; }
public String canonicalNameToName(String canonicalName, PsiNamedElement psiFile) { return canonicalName.contains(".") ? canonicalName : canonicalName + GuiFormFileType.DOT_DEFAULT_EXTENSION; }
public PaletteGroup[] getActiveGroups(VirtualFile vFile) { if (vFile.getFileType().equals(GuiFormFileType.INSTANCE)) { return myPalette.getToolWindowGroups(); } return PaletteGroup.EMPTY_ARRAY; }
private static boolean processReferencesInUIForms(Processor<PsiReference> processor, PsiField field, GlobalSearchScope scope1, LocalSearchScope filterScope) { GlobalSearchScope scope = GlobalSearchScope.projectScope(field.getProject()).intersectWith(scope1); PsiManagerImpl manager = (PsiManagerImpl) field.getManager(); PsiClass containingClass = null; String fieldName; final AccessToken token = ReadAction.start(); try { containingClass = field.getContainingClass(); if(containingClass == null) { return true; } fieldName = field.getName(); } finally { token.finish(); } manager.startBatchFilesProcessingMode(); try { final List<PsiFile> files = FormClassIndex.findFormsBoundToClass(containingClass, scope); for(PsiFile file : files) { ProgressManager.checkCanceled(); if(file.getFileType() != GuiFormFileType.INSTANCE) { continue; } if(!processReferences(processor, file, fieldName, field, filterScope)) { return false; } } } finally { manager.finishBatchFilesProcessingMode(); } return true; }
private static boolean processReferencesInUIForms(final Processor<PsiReference> processor, final Property property, final GlobalSearchScope globalSearchScope, final LocalSearchScope filterScope) { final Project project = property.getProject(); final GlobalSearchScope scope = GlobalSearchScope.projectScope(project).intersectWith(globalSearchScope); final PsiManagerImpl manager = (PsiManagerImpl) property.getManager(); String name = ApplicationManager.getApplication().runReadAction(new Computable<String>() { @Override public String compute() { return property.getName(); } }); if(name == null) { return true; } manager.startBatchFilesProcessingMode(); try { CommonProcessors.CollectProcessor<VirtualFile> collector = new CommonProcessors.CollectProcessor<VirtualFile>() { @Override protected boolean accept(VirtualFile virtualFile) { return virtualFile.getFileType() == GuiFormFileType.INSTANCE; } }; ((PsiSearchHelperImpl) PsiSearchHelper.SERVICE.getInstance(project)).processFilesWithText(scope, UsageSearchContext.IN_PLAIN_TEXT, true, name, collector); for(final VirtualFile vfile : collector.getResults()) { ProgressManager.checkCanceled(); PsiFile file = ApplicationManager.getApplication().runReadAction(new Computable<PsiFile>() { @Override public PsiFile compute() { return PsiManager.getInstance(project).findFile(vfile); } }); if(!processReferences(processor, file, name, property, filterScope)) { return false; } } } finally { manager.finishBatchFilesProcessingMode(); } return true; }
@Override public boolean acceptInput(Project project, final VirtualFile file) { return file.getFileType() == GuiFormFileType.INSTANCE; }
@Override public Collection<AbstractTreeNode> modify(AbstractTreeNode parent, Collection<AbstractTreeNode> children, ViewSettings settings) { if(parent.getValue() instanceof Form) { return children; } // Optimization. Check if there are any forms at all. boolean formsFound = false; for(AbstractTreeNode node : children) { if(node.getValue() instanceof PsiFile) { PsiFile file = (PsiFile) node.getValue(); if(file.getFileType() == GuiFormFileType.INSTANCE) { formsFound = true; break; } } } if(!formsFound) { return children; } Collection<AbstractTreeNode> result = new LinkedHashSet<>(children); ProjectViewNode[] copy = children.toArray(new ProjectViewNode[children.size()]); for(ProjectViewNode element : copy) { PsiClass psiClass = null; if(element.getValue() instanceof PsiClass) { psiClass = (PsiClass) element.getValue(); } else if(element.getValue() instanceof PsiClassOwner) { final PsiClass[] psiClasses = ((PsiClassOwner) element.getValue()).getClasses(); if(psiClasses.length == 1) { psiClass = psiClasses[0]; } } if(psiClass == null) { continue; } String qName = psiClass.getQualifiedName(); if(qName == null) { continue; } List<PsiFile> forms; try { forms = FormClassIndex.findFormsBoundToClass(myProject, qName); } catch(ProcessCanceledException e) { continue; } Collection<BasePsiNode<? extends PsiElement>> formNodes = findFormsIn(children, forms); if(!formNodes.isEmpty()) { Collection<PsiFile> formFiles = convertToFiles(formNodes); Collection<BasePsiNode<? extends PsiElement>> subNodes = new ArrayList<>(); //noinspection unchecked subNodes.add((BasePsiNode<? extends PsiElement>) element); subNodes.addAll(formNodes); result.add(new FormNode(myProject, new Form(psiClass, formFiles), settings, subNodes)); result.remove(element); result.removeAll(formNodes); } } return result; }
@Override public boolean isValid() { //TODO[anton,vova] fire when changed return FileDocumentManager.getInstance().getDocument(myFile) != null && myFile.getFileType() == GuiFormFileType.INSTANCE; }
public boolean handlesTemplate(final FileTemplate template) { FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension()); return fileType.equals(GuiFormFileType.INSTANCE); }