istinnstudio commented on issue #3962:
URL: https://github.com/apache/netbeans/issues/3962#issuecomment-1714262502

   A slightly modified test code that includes console timestamps and fault 
detection:
   
   ```
   import java.awt.Robot;
   import java.awt.Toolkit;
   import java.awt.datatransfer.DataFlavor;
   import java.awt.datatransfer.Transferable;
   import java.awt.datatransfer.UnsupportedFlavorException;
   import java.awt.event.KeyEvent;
   import java.io.IOException;
   import java.text.SimpleDateFormat;
   import java.util.Date;
   
   public class CopyPasteTest {
   
       int maxTries = 12;
   
       public static void main(String[] args) {
           /*
               Put cursor inside this comment block before running.
   
            */
           try {
               new CopyPasteTest();
           } catch (Exception x) {
               x.printStackTrace();
           }
       }
   
       private final Robot robot;
       private int iteration = 0;
   
       public CopyPasteTest() throws Exception {
           robot = new java.awt.Robot();
   
           // open notepad, give it some time to get focus, and write our 
starting text
           Runtime.getRuntime().exec("notepad");
           Thread.sleep(20);
           writeText("sometext");
   
           for (int tryNum = 1; tryNum <= maxTries; tryNum++) {
               iteration++;
               copyLine(); // copy from notepad
               enter(); // go to the next line in notepad (to prep for the next 
paste)
               altTab(); // go to netbeans
               paste(); // paste into netbeans
               logTimestamp("Pasted into NetBeans", iteration);
               writeText(String.valueOf(tryNum)); // add try num to the text in 
netbeans
               copyLine(); // copy from netbeans
               enter(); // go to the next line in netbeans (to prep for the 
next paste)
               altTab(); // go to notepad
               paste(); // paste into notepad
               logTimestamp("Pasted into Notepad", iteration);
           }
       }
   
       private void writeText(String text) {
           for (char c : text.toCharArray()) {
               int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
               _press(keyCode);
               _release(keyCode);
           }
       }
   
       private void copyLine() {
           // highlight this line
           _press(KeyEvent.VK_END);
           _release(KeyEvent.VK_END);
           _press(KeyEvent.VK_SHIFT);
           _press(KeyEvent.VK_HOME);
           _release(KeyEvent.VK_HOME);
           _release(KeyEvent.VK_SHIFT);
   
           // copy highlighted text
           _press(KeyEvent.VK_CONTROL);
           _press(KeyEvent.VK_C);
           _release(KeyEvent.VK_C);
           _release(KeyEvent.VK_CONTROL);
   
           // remove highlight, put cursor at the end of the line
           _press(KeyEvent.VK_END);
           _release(KeyEvent.VK_END);
       }
   
       private void paste() {
           _press(KeyEvent.VK_CONTROL);
           _press(KeyEvent.VK_V);
           _release(KeyEvent.VK_CONTROL);
           _release(KeyEvent.VK_V);
       }
   
       private void enter() {
           _press(KeyEvent.VK_ENTER);
           _release(KeyEvent.VK_ENTER);
       }
   
       private void altTab() {
           _press(KeyEvent.VK_ALT);
           _press(KeyEvent.VK_TAB);
           _release(KeyEvent.VK_ALT);
           _release(KeyEvent.VK_TAB);
           try {
               Thread.sleep(100); // breathing room for windows
           } catch (InterruptedException ex) {
               ex.printStackTrace();
           }
       }
   
       private void _press(int e) {
           robot.keyPress(e);
           try {
               Thread.sleep(20);
           } catch (InterruptedException ex) {
               ex.printStackTrace();
           }
       }
   
       private void _release(int e) {
           robot.keyRelease(e);
           try {
               Thread.sleep(20);
           } catch (InterruptedException ex) {
               ex.printStackTrace();
           }
       }
   
       private void logTimestamp(String event, int iteration) {
           SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss.SSS");
           String timestamp = dateFormat.format(new Date());
           String logMessage = "Iteration " + iteration + ": " + timestamp + " 
- " + event;
   
           // Check if the copied or pasted text is null or empty and print an 
error if so
           if ("".equals(getClipboardText().trim())) {
               System.err.println(logMessage + " - Error: Copied or pasted text 
is null or empty.");
           } else {
               System.out.println(logMessage);
           }
       }
   
       // Helper method to get text from the clipboard
       private String getClipboardText() {
           Transferable clipboardContents = 
Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
           if (clipboardContents != null && 
clipboardContents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
               try {
                   return (String) 
clipboardContents.getTransferData(DataFlavor.stringFlavor);
               } catch (UnsupportedFlavorException | IOException e) {
                   e.printStackTrace();
               }
           }
           return "";
       }
   }
   ```


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

To unsubscribe, e-mail: [email protected]

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