mardi 31 janvier 2017

No listening to a flavor change if it is caused by an event from a Swing GUI

            public void flavorsChanged(FlavorEvent e) {
                Transferable contentsTransferable = clipboard.getContents(null);
                // NB the Transferable returned from getContents is NEVER the same as the 
                // clearing Transferable!

                if (!suppressOutput) {
                    System.out.println(String.format("# clipboard UPDATED, src %s, string %s, clearingT? %b", e.getSource(), e.toString(),
                            contentsTransferable == clearingTransferable));
                    try {
                        String stringData = (String)clipboard.getData(DataFlavor.stringFlavor);
                        System.out.println(String.format("# string data |%s|", stringData ));

                    } catch (UnsupportedFlavorException | IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } 
                }

                else {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            suppressOutput = false;                                
                        }
                    });

                }
                suppressOutput = true;
                clipboard.setContents(clearingTransferable, null);
            }
        });

    } 

});

I need to have the above not trigger when I have the event below triggered. The above will still need to trigger if I've copied anything otherwise. This is because I will be replacing the println with code to write what ever has been recently copied into a text file and don't want repeats of anything that would already be in the file.

// selection from first list above placed on clipboard
DefaultListSelectionModel m = new DefaultListSelectionModel();
m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
m.setLeadAnchorNotificationEnabled(false);
clipboardStorageList.setSelectionModel(m);

clipboardStorageList.addListSelectionListener(new ListSelectionListener() {
  public void valueChanged(ListSelectionEvent e) {
        String element = clipboardStorageList.getSelectedValue().toString();
        StringSelection stringSelection = new StringSelection(element);
        Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
        clpbrd.setContents(stringSelection, null);
      }
  });

Aucun commentaire:

Enregistrer un commentaire