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



##########
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"; //NOI18N
+
+    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"); //NOI18N
+                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"); //NOI18N
+            if (fileName.exists()) {
+                JOptionPane.showMessageDialog(null, "Cannot create class 
already present"); //NOI18N
+                return t;
+            }
+            if (!fileName.createNewFile()) {
+                JOptionPane.showMessageDialog(null, "Cannot create file"); 
//NOI18N
+                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);

Review comment:
       Addressed.
   You are correct file manager is not needed in this case. So removed file 
manager passed null in compiler.getTask.




----------------------------------------------------------------
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