singh-akhilesh commented on a change in pull request #2334:
URL: https://github.com/apache/netbeans/pull/2334#discussion_r491839180



##########
File path: 
java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = 
nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + 
files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() 
throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste    
 ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = 
nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + 
files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() 
throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program 
Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       addressed code review comments

##########
File path: 
java/java.project.ui/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java
##########
@@ -721,6 +724,121 @@ public void testCopyPaste () throws Exception {
         }
     }
 
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard() throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("import java.util.*;"
+                + "class C{}"
+                + "public class PC{}"
+                + "interface I{}"
+                + "enum En{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = 
nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + 
files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "class C{}"
+                    + "public class PC{}"
+                    + "interface I{}"
+                    + "enum En{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_removeExistingPackageName() 
throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-rm-package");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("package copy.paste    
 ."
+                + "java"
+                + ";"
+                + "import java.util.*;"
+                + "public class PC{}"
+        );
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = 
nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "PC.java", files[0].getName() + "." + 
files[0].getExt());
+            assertEquals("File contents","import java.util.*;"
+                    + "public class PC{}",
+                     files[0].asText());
+        }
+
+         for (Node n : nodes[0].getChildren().getNodes(true)) {
+            DataObject dobj = n.getLookup().lookup(DataObject.class);
+            if (dobj != null) {
+                dobj.delete();
+            }
+        }
+
+    }
+
+    @RandomlyFails
+    public void testCopyPasteJavaFileFromClipboard_CompilationErrorInCode() 
throws Exception {
+
+        //Setup sourcegroups
+        FileObject root1 = root.createFolder("paste-src-error");
+        SourceGroup group = new SimpleSourceGroup(root1);
+        Node rn = PackageView.createPackageView(group);
+        Node[] nodes = rn.getChildren().getNodes(true);
+
+        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+        StringSelection selection = new StringSelection("C:\\Program 
Files\\Java\\jdk-9.0.4\\bin\\ class Hello java.exe  \\\"-javaagent:\" +\n" +
+                "\"C:\\Program Files\\NB\\Community Edition 
201.3803.71\\lib\\");
+        clipboard.setContents(selection, selection);
+
+        Transferable transferable = clipboard.getContents(selection);
+        if (nodes.length > 0) {
+            PasteType[] pts = nodes[0].getPasteTypes(transferable);
+            pts[0].paste();
+            FileObject[] files = 
nodes[0].getLookup().lookup(DataObject.class).getPrimaryFile().getChildren();
+            assertEquals("File count", 1, files.length);
+            assertEquals("File name", "Hello.java", files[0].getName() + "." + 
files[0].getExt());
+            assertEquals("C:\\Program Files\\Java\\jdk-9.0.4\\bin\\ class 
Hello java.exe  \\\"-javaagent:\" +\n" +

Review comment:
       addressed code review comments

##########
File path: 
java/java.project.ui/src/org/netbeans/spi/java/project/support/ui/CreateJavaClassFileFromClipboard.java
##########
@@ -0,0 +1,258 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.spi.java.project.support.ui;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.util.JavacTask;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.awt.datatransfer.UnsupportedFlavorException;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.swing.JOptionPane;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.source.ClasspathInfo;
+import org.netbeans.api.java.source.CompilationController;
+import org.netbeans.api.java.source.CompilationInfo;
+import org.netbeans.api.java.source.JavaSource;
+import org.netbeans.api.java.source.Task;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.util.Exceptions;
+import org.openide.util.datatransfer.PasteType;
+
+/**
+ *
+ * @author aksinsin
+ */
+public class CreateJavaClassFileFromClipboard extends PasteType {
+    
+    private static final String PUBLIC_MODIFIER = "public";
+
+    private final DataFolder context;
+    private final Transferable t;
+
+    public CreateJavaClassFileFromClipboard(DataFolder context, Transferable 
t) {
+        this.context = context;
+        this.t = t;
+    }
+
+    @Override
+    public Transferable paste() throws IOException {
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            if (!c.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
+                return t;
+            }
+            String copiedData = (String) c.getData(DataFlavor.stringFlavor);
+            CreateJavaClassFileFromClipboard.ClassContent classContent = 
extractPackageAndClassName(copiedData);
+            if (classContent == null) {
+                JOptionPane.showMessageDialog(null, "Code not valid to create 
class");
+                return t;
+            }
+            Set<FileObject> files = this.context.files();
+            if (files.size() != 1) {
+                return t;
+            }
+            String path = files.iterator().next().getPath();
+            File fileName = new File(path + File.separator + 
classContent.getClassName() + ".java");
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class 
already present");
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file");
+                return t;
+            }
+
+            if (classContent.getPackageName() != null) {
+                copiedData = removePackage(copiedData, 
classContent.getPackageName());
+            }
+            try (BufferedWriter bw = new BufferedWriter(new 
FileWriter(fileName))) {
+                String packageLocation = getPackageNameFromFile(fileName);
+                if (packageLocation != null && !packageLocation.isEmpty()) {
+                    copiedData = "package " + packageLocation + ";\n" + 
copiedData;// NOI18N
+                }
+                bw.write(copiedData);
+            }
+
+        } catch (UnsupportedFlavorException ex) {
+            Exceptions.printStackTrace(ex);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        return t;
+    }
+    
+    
+     private static class DeadlockTask implements Task<CompilationController> {
+
+        JavaSource.Phase phase;
+        CompilationInfo info;
+
+        public DeadlockTask(JavaSource.Phase phase) {
+            assert phase != null;
+            this.phase = phase;
+        }
+
+        public void run(CompilationController info) {
+            try {
+                info.toPhase(this.phase);
+                this.info = info;
+            } catch (IOException ioe) {
+            }
+        }
+
+    }
+
+    private ClassContent extractPackageAndClassName(String copiedData) {
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fileManager = 
compiler.getStandardFileManager(null, null, null);
+
+        String publicFirstClassName = null;
+        String nonPublicFirstClassName = null;
+        String packageName = null;
+        int counter = 0;
+        JavaCompiler.CompilationTask task = compiler.getTask(null, 
fileManager, null, null, null, Arrays.asList(new MyFileObject(copiedData)));
+        parse:
+        try {
+            for (CompilationUnitTree compilationUnitTree : ((JavacTask) 
task).parse()) {
+                packageName = compilationUnitTree.getPackageName() != null
+                        ? compilationUnitTree.getPackageName().toString() : 
null;
+                for (Tree tree : compilationUnitTree.getTypeDecls()) {
+                    if (tree instanceof ClassTree) {
+                        final ClassTree classTree = (ClassTree) tree;
+                        if 
(classTree.toString().trim().startsWith(PUBLIC_MODIFIER)) {
+                            publicFirstClassName = 
classTree.getSimpleName().toString();
+                            break parse;
+                        }
+                        if (counter == 0) {
+                            nonPublicFirstClassName = 
classTree.getSimpleName().toString();
+                            counter++;
+                        }
+                    }
+                }
+            }
+        } catch (Exception ex) {
+            Exceptions.printStackTrace(ex);
+        } finally {
+            try {
+                fileManager.close();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+        }
+
+        if (publicFirstClassName != null && 
!publicFirstClassName.equals("<error>")) {
+            return new ClassContent(publicFirstClassName, packageName);
+        } else if (nonPublicFirstClassName != null && 
!nonPublicFirstClassName.equals("<error>")) {
+            return new ClassContent(nonPublicFirstClassName, packageName);
+        } else {
+            return null;
+        }
+
+    }
+
+    private String removePackage(String copiedCode, String oldPacageName) {
+        String packageRegex = oldPacageName.replace(".", "\\s*.\\s*");

Review comment:
       addressed code review comments




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to