public WizardData(@NotNull final Project project, @NotNull final VirtualFile formFile) throws Generator.MyException { myProject = project; myFormFile = formFile; myBindToNewBean = true; myGenerateIsModified = true; final LwRootContainer[] rootContainer = new LwRootContainer[1]; // Create initial bingings between form fields and bean's properties. // TODO[vova] ask Anton to not throw exception if form-field doesn't have corresponded field in the Java class final FormProperty[] formProperties = Generator.exposeForm(myProject, myFormFile, rootContainer); myBindings = new FormProperty2BeanProperty[formProperties.length]; for(int i = formProperties.length - 1; i >= 0; i--){ myBindings[i] = new FormProperty2BeanProperty(formProperties[i]); } final PsiManager manager = PsiManager.getInstance(myProject); final VirtualFile directory = formFile.getParent(); LOG.assertTrue(directory.isDirectory()); final PsiDirectory psiDirectory = manager.findDirectory(directory); LOG.assertTrue(psiDirectory != null); final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory); if(aPackage != null){ myPackageName = aPackage.getQualifiedName(); } }
public RadNestedForm(final ModuleProvider module, final String formFileName, final String id) throws Exception { super(module, JPanel.class, id); myFormFileName = formFileName; LOG.debug("Loading nested form " + formFileName); VirtualFile formFile = ResourceFileUtil.findResourceFileInDependents(getModule(), formFileName); if (formFile == null) { throw new IllegalArgumentException("Couldn't find virtual file for nested form " + formFileName); } Document doc = FileDocumentManager.getInstance().getDocument(formFile); final ClassLoader classLoader = LoaderFactory.getInstance(getProject()).getLoader(formFile); final LwRootContainer rootContainer = Utils.getRootContainer(doc.getText(), new CompiledClassPropertiesProvider(classLoader)); myRootContainer = XmlReader.createRoot(module, rootContainer, classLoader, null); if (myRootContainer.getComponentCount() > 0) { getDelegee().setLayout(new BorderLayout()); JComponent nestedFormDelegee = myRootContainer.getComponent(0).getDelegee(); getDelegee().add(nestedFormDelegee, BorderLayout.CENTER); setRadComponentRecursive(nestedFormDelegee); } if (isCustomCreateRequired()) { setCustomCreate(true); } }
public static void setPreviewBindings(final LwRootContainer rootContainer, final String classToBindName) { // 1. Prepare form to preview. We have to change container so that it has only one binding. rootContainer.setClassToBind(classToBindName); FormEditingUtil.iterate( rootContainer, new FormEditingUtil.ComponentVisitor<LwComponent>() { public boolean visit(final LwComponent iComponent) { iComponent.setBinding(null); return true; } } ); if (rootContainer.getComponentCount() == 1) { //noinspection HardCodedStringLiteral ((LwComponent)rootContainer.getComponent(0)).setBinding(PREVIEW_BINDING_FIELD); } }
public WizardData(@NotNull final Project project, @NotNull final VirtualFile formFile) throws Generator.MyException { myProject = project; myFormFile = formFile; myBindToNewBean = true; myGenerateIsModified = true; final LwRootContainer[] rootContainer = new LwRootContainer[1]; // Create initial bingings between form fields and bean's properties. // TODO[vova] ask Anton to not throw exception if form-field doesn't have corresponded field in the Java class final FormProperty[] formProperties = Generator.exposeForm(myProject, myFormFile, rootContainer); myBindings = new FormProperty2BeanProperty[formProperties.length]; for(int i = formProperties.length - 1; i >= 0; i--){ myBindings[i] = new FormProperty2BeanProperty(formProperties[i]); } final PsiManager manager = PsiManager.getInstance(myProject); final VirtualFile directory = formFile.getParent(); LOG.assertTrue(directory.isDirectory()); final PsiDirectory psiDirectory = manager.findDirectory(directory); LOG.assertTrue(psiDirectory != null); final PsiJavaPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory); if(aPackage != null){ myPackageName = aPackage.getQualifiedName(); } }
public String getBoundClassName(final VirtualFile formFile) throws Exception { File file = VfsUtil.virtualToIoFile(formFile); String classToBind = getSavedBinding(file); if(classToBind == null) { final Document doc = FileDocumentManager.getInstance().getDocument(formFile); final LwRootContainer rootContainer = Utils.getRootContainer(doc.getText(), null); classToBind = rootContainer.getClassToBind(); } if(classToBind != null) { updateCache(file, classToBind); } return classToBind; }
public LwRootContainer loadForm(String formFilePath) throws Exception { if (myFormCache.containsKey(formFilePath)) { return (LwRootContainer)myFormCache.get(formFilePath); } String lowerFormFilePath = formFilePath.toLowerCase(); log("Searching for form " + lowerFormFilePath, Project.MSG_VERBOSE); for (Iterator iterator = myFormFiles.iterator(); iterator.hasNext();) { File file = (File)iterator.next(); String name = file.getAbsolutePath().replace(File.separatorChar, '/').toLowerCase(); log("Comparing with " + name, Project.MSG_VERBOSE); if (name.endsWith(lowerFormFilePath)) { return loadForm(formFilePath, new FileInputStream(file)); } } if (myNestedFormPathList != null) { for (int i = 0; i < myNestedFormPathList.size(); i++) { PrefixedPath path = (PrefixedPath)myNestedFormPathList.get(i); File formFile = path.findFile(formFilePath); if (formFile != null) { return loadForm(formFilePath, new FileInputStream(formFile)); } } } InputStream resourceStream = myLoader.getResourceAsStream(formFilePath); if (resourceStream != null) { return loadForm(formFilePath, resourceStream); } throw new Exception("Cannot find nested form file " + formFilePath); }
public LwRootContainer loadForm(String formFileName) throws Exception { if (myCache.containsKey(formFileName)) { return myCache.get(formFileName); } final String relPath = FileUtil.toSystemIndependentName(formFileName); for (Map.Entry<File, String> entry : mySourceRoots.entrySet()) { final File sourceRoot = entry.getKey(); final String prefix = entry.getValue(); String path = relPath; if (prefix != null && FileUtil.startsWith(path, prefix)) { path = path.substring(prefix.length()); } final File formFile = new File(sourceRoot, path); if (formFile.exists()) { final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(formFile)); try { return loadForm(formFileName, stream); } finally { stream.close(); } } } throw new Exception("Cannot find nested form file " + formFileName); }
public String getClassToBindName(LwRootContainer container) { final String className = container.getClassToBind(); for (File outputRoot : myOutputRoots) { final String result = getJVMClassName(outputRoot, className.replace('.', '/')); if (result != null) { return result.replace('/', '.'); } } return className; }
private boolean saveNestedForm() { VirtualFile formFile = ResourceFileUtil.findResourceFileInProject(myProject, myTfNestedForm.getText()); if (formFile == null) { Messages.showErrorDialog(getWindow(), UIDesignerBundle.message("add.component.cannot.load.form", myTfNestedForm.getText()), CommonBundle.getErrorTitle()); return false; } LwRootContainer lwRootContainer; try { lwRootContainer = Utils.getRootContainer(formFile.getInputStream(), null); } catch (Exception e) { Messages.showErrorDialog(getWindow(), e.getMessage(), CommonBundle.getErrorTitle()); return false; } if (lwRootContainer.getClassToBind() == null) { Messages.showErrorDialog(getWindow(), UIDesignerBundle.message("add.component.form.not.bound"), CommonBundle.getErrorTitle()); return false; } if (lwRootContainer.getComponent(0).getBinding() == null) { Messages.showErrorDialog(getWindow(), UIDesignerBundle.message("add.component.root.not.bound"), CommonBundle.getErrorTitle()); return false; } PsiClass psiClass = JavaPsiFacade.getInstance(myProject).findClass(lwRootContainer.getClassToBind(), GlobalSearchScope.projectScope(myProject)); if (psiClass != null) { myItemToBeEdited.setClassName(getClassOrInnerName(psiClass)); } else { myItemToBeEdited.setClassName(lwRootContainer.getClassToBind()); } return true; }
@Nullable public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { if (file.getFileType().equals(StdFileTypes.GUI_DESIGNER_FORM)) { 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 LwRootContainer loadForm(String formFileName) throws Exception { if (myFormCache.containsKey(formFileName)) { return myFormCache.get(formFileName); } VirtualFile formFile = ResourceFileUtil.findResourceFileInDependents(myModule, formFileName); if (formFile == null) { throw new Exception("Could not find nested form file " + formFileName); } final LwRootContainer container = Utils.getRootContainer(formFile.getInputStream(), new PsiPropertiesProvider(myModule)); myFormCache.put(formFileName, container); return container; }
public String getClassToBindName(LwRootContainer container) { PsiClass psiClass = JavaPsiFacade.getInstance(myModule.getProject()).findClass(container.getClassToBind(), myModule.getModuleWithDependenciesScope()); if (psiClass != null) { return ClassUtil.getJVMClassName(psiClass); } return container.getClassToBind(); }
public LwRootContainer loadForm(String formFileName) throws Exception { LwRootContainer rootContainer = super.loadForm(formFileName); if (!myGeneratedClasses.contains(formFileName)) { myGeneratedClasses.add(formFileName); String generatedClassName = "FormPreviewFrame" + myGeneratedClasses.size(); PreviewFormAction.setPreviewBindings(rootContainer, generatedClassName); generateStubClass(rootContainer, generatedClassName); } return rootContainer; }
private void generateStubClass(final LwRootContainer rootContainer, final String generatedClassName) throws IOException, CodeGenerationException { @NonNls ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, generatedClassName, null, "java/lang/Object", ArrayUtil.EMPTY_STRING_ARRAY); cw.visitField(Opcodes.ACC_PUBLIC, PreviewFormAction.PREVIEW_BINDING_FIELD, "Ljavax/swing/JComponent;", null, null); @NonNls MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); cw.visitEnd(); ByteArrayInputStream bais = new ByteArrayInputStream(cw.toByteArray()); AsmCodeGenerator acg = new AsmCodeGenerator(rootContainer, myFinder, this, true, new PsiClassWriter(myModule)); byte[] data = acg.patchClass(bais); FormErrorInfo[] errors = acg.getErrors(); if (errors.length > 0) { throw new CodeGenerationException(errors [0].getComponentId(), errors [0].getErrorMessage()); } FileOutputStream fos = new FileOutputStream(new File(myTempPath, generatedClassName + ".class")); try { fos.write(data); } finally { fos.close(); } }
public String getBoundClassName(final VirtualFile formFile) throws Exception { String classToBind = getSavedBinding(formFile); if (classToBind == null) { final Document doc = FileDocumentManager.getInstance().getDocument(formFile); final LwRootContainer rootContainer = Utils.getRootContainer(doc.getText(), null); classToBind = rootContainer.getClassToBind(); } if (classToBind != null) { updateCache(formFile, classToBind); } return classToBind; }
private AsmCodeGenerator initCodeGenerator(final String formFileName, final String className, final String testDataPath) throws Exception { String tmpPath = FileUtil.getTempDirectory(); String formPath = testDataPath + formFileName; String javaPath = testDataPath + className + ".java"; final int rc = Main.compile(new String[]{"-d", tmpPath, javaPath}); assertEquals(0, rc); final String classPath = tmpPath + "/" + className + ".class"; final File classFile = new File(classPath); assertTrue(classFile.exists()); final LwRootContainer rootContainer = loadFormData(formPath); final AsmCodeGenerator codeGenerator = new AsmCodeGenerator(rootContainer, myClassFinder, myNestedFormLoader, false, new ClassWriter(ClassWriter.COMPUTE_FRAMES)); final FileInputStream classStream = new FileInputStream(classFile); try { codeGenerator.patchClass(classStream); } finally { classStream.close(); FileUtil.delete(classFile); final File[] inners = new File(tmpPath).listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith(className + "$") && name.endsWith(".class"); } }); if (inners != null) { for (File file : inners) FileUtil.delete(file); } } return codeGenerator; }
@Override public LwRootContainer loadForm(String formFileName) throws Exception { final String fileName = myFormMap.get(formFileName); if (fileName != null) { return loadFormData(fileName); } throw new UnsupportedOperationException("No nested form found for name " + formFileName); }
private void generateStubClass(final LwRootContainer rootContainer, final String generatedClassName) throws IOException, CodeGenerationException { @NonNls ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, generatedClassName, null, "java/lang/Object", ArrayUtil.EMPTY_STRING_ARRAY); cw.visitField(Opcodes.ACC_PUBLIC, PreviewFormAction.PREVIEW_BINDING_FIELD, "Ljavax/swing/JComponent;", null, null); @NonNls MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); cw.visitEnd(); ByteArrayInputStream bais = new ByteArrayInputStream(cw.toByteArray()); AsmCodeGenerator acg = new AsmCodeGenerator(rootContainer, myFinder, this, true, new PsiClassWriter(myModule)); byte[] data = acg.patchClass(bais); FormErrorInfo[] errors = acg.getErrors(); if (errors.length > 0) { throw new CodeGenerationException(errors [0].getComponentId(), errors [0].getErrorMessage()); } FileOutputStream fos = new FileOutputStream(new File(myTempPath, generatedClassName + ".class")); try { fos.write(data); } finally { fos.close(); } }
private AsmCodeGenerator initCodeGenerator(final String formFileName, final String className, final String testDataPath) throws Exception { String tmpPath = FileUtil.getTempDirectory(); String formPath = testDataPath + formFileName; String javaPath = testDataPath + className + ".java"; com.sun.tools.javac.Main.compile(new String[] { "-d", tmpPath, javaPath } ); String classPath = tmpPath + "/" + className + ".class"; final LwRootContainer rootContainer = loadFormData(formPath); final AsmCodeGenerator codeGenerator = new AsmCodeGenerator(rootContainer, myClassFinder, myNestedFormLoader, false, new ClassWriter(ClassWriter.COMPUTE_FRAMES)); final FileInputStream classStream = new FileInputStream(classPath); try { codeGenerator.patchClass(classStream); } finally { classStream.close(); FileUtil.delete(new File(classPath)); final File[] inners = new File(tmpPath).listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith(className + "$") && name.endsWith(".class"); } }); if (inners != null) { for (File file : inners) FileUtil.delete(file); } } return codeGenerator; }
@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; }